1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
#! /usr/bin/env pike 
// -*- Pike -*- 
 
#pike __REAL_VERSION__ 
 
constant description = "Context aware grep."; 
 
// Future expansions: 
// - We already stream files, so we should be able to stream from stdin. 
// - Support for more file formats. Python should be easy, since there 
//   is alrady a Python parser in Pike. It just needs stream support. 
// - Report in which function the match was made. 
// - Support grep contexts (-A -B -C). Rewrite lines to an object. 
// - Mimic -d behaviour. 
// - Implement -Z and -b. 
// - Make something smart with macro definitions. Right now they are 
//   ignored. 
// - Implement support for grepping for the definition of a symbol, 
//   be it a function definition, variable declaration, struct/enum/typedef 
//   definition or a macro definition. 
 
// Search parameters 
int(0..2) in_token; // 1=is_token, 2=in_token 
int(0..2) in_string; // 1=is_string, 2=in_string 
int(0..1) in_comment; 
int(0..1) case_insensitive; 
string target; 
 
// Presentation 
int(0..1) show_lineno; 
int(0..1) show_fn; 
int(0..1) verbose; 
int(0..1) quiet; 
int(0..1) supress_match; 
int(0..3) fn_only; // 1=with, 2=without, 3=count 
 
class File { 
 
  void create(string _fn) { 
    fn = _fn; 
  } 
 
  string fn; 
  array(string) lines = ({}); 
  int line; 
 
  mapping parse_state = ([]); 
  string queue = ""; 
  int matches; 
 
  void found() { 
    if(quiet) exit(0); 
    matches++; 
    if(supress_match) return; 
    if(show_fn) write(fn+":"); 
    if(show_lineno) write("%d:", line+1); 
    write("%s\n", lines[line]); 
  } 
 
  array(string) split(string, void|mapping); 
  void grep(array(string)); 
 
  void feed(string data) { 
    // Taking care of the line state 
    data = queue+data; 
    array(string) newlines = data/"\n"; 
    queue = newlines[-1]; 
    newlines = newlines[..sizeof(newlines)-2]; 
    data = newlines*"\n"+"\n"; 
    lines += newlines; 
 
    if(case_insensitive) 
      data = lower_case(data); 
 
    grep(split(data, parse_state)); 
  } 
 
  int close() { 
    if(sizeof(queue)) feed("\n"); 
    if(fn_only) { 
      if(fn_only==1 && matches) 
        write("%s\n", fn); 
      else if(fn_only==2 && !matches) 
        write("%s\n", fn); 
      else if(fn_only==3 && matches) 
        write("%s:%d\n", fn, matches); 
    } 
    return matches; 
  } 
} 
 
class PikeFile { 
  inherit File; 
 
  array(string) split(string data, mapping state) { 
    // For some reason the Pike parser leaves parts of its 
    // end sentinel in the output data. 
    array(string) ret; 
    array err = catch { 
      ret = Parser.Pike.split(data, state); 
    }; 
    if(err) { 
      werror("%s (%s)\n", err[0][..sizeof(err[0])-2], fn); 
      return ({}); 
    } 
    if(state->in_token) return ret; 
    if(ret[-1]=="\n") return ret[..sizeof(ret)-2]; 
    ret[-1] = ret[-1][..sizeof(ret[-1])-2]; 
    return ret; 
  } 
 
  void grep(array(string) tokens) { 
    foreach(tokens; int i; string token) { 
#if 0 
      if(line!=old) { 
        catch(write("\n%4d:%s\n", line+1, lines[line])); 
        old=line; 
      } 
      write("%O ", token); 
#endif 
      if(in_token==1 && token==target) { 
        found(); 
        line += String.count(token, "\n"); 
        continue; 
      } 
      if(token[0]=='"') { 
        if(in_string==1 && token[1..sizeof(token)-2]==target) 
          found(); 
        else if(in_string==2 && has_value(token[1..sizeof(token)-2], target)) 
          found(); 
        line += String.count(token, "\n"); 
        continue; 
      } 
      if(token[..1]=="//") { 
        if(in_comment && has_value(token[2..], target)) 
          found(); 
        continue; 
      } 
      if(token[..1]=="/*") { 
        foreach(token[2..sizeof(token)-3]/"\n", string l) { 
          if(in_comment && has_value(l, target)) 
            found(); 
          line++; 
        } 
        line--; 
        continue; 
      } 
      if(token[0]=='#') { 
        line += String.count(token, "\n"); 
        continue; 
      } 
      if(in_token==2 && has_value(token, target)) { 
        found(); 
        continue; 
        line += String.count(token, "\n"); 
      } 
      line += String.count(token, "\n"); 
    } 
  } 
} 
 
class CFile { 
  inherit PikeFile; 
 
  array(string) split(string data, mapping state) { 
    // For some reason the C parser leaves parts of its 
    // end sentinel in the output data. 
    array(string) ret = Parser.C.split(data, state); 
    if(state->in_token) return ret; 
    if(ret[-1]=="\n") return ret[..sizeof(ret)-2]; 
    ret[-1] = ret[-1][..sizeof(ret[-1])-2]; 
    return ret; 
  } 
} 
 
void msg(string x, mixed ... y) { 
  if(verbose) 
    werror(x, @y); 
} 
 
int handle_file_safe(string path, string fn) { 
  mixed err = catch { 
    return handle_file( path, fn ); 
  }; 
  if( err ) { 
    msg("Error while tokanizing %O:\n%s", fn, describe_backtrace(err)); 
    return 0; 
  } 
} 
 
int handle_file(string path, string fn) { 
  if(Stdio.is_dir(path)) { 
    msg("%O is a directory.\n", path); 
    return 0; 
  } 
  Stdio.File in; 
  catch { in = Stdio.File(path, "r"); }; 
  if(!in) { 
    msg("File %O could not be read.\n", path); 
    return 0; 
  } 
 
  File f; 
  array(string) parts = fn/"."; 
  string suffix = parts[-1]; 
  if(suffix=="in" && sizeof(parts)>1) 
    suffix = parts[-2]; 
 
  if( (< "pike", "pmod", "java" >)[suffix] ) 
    f = PikeFile(fn); 
  else if( (< "h", "c", "cmod", "cpp" >)[suffix] ) 
    f = CFile(fn); 
 
  // We could add some heuristics here looking for #! rows ending 
  // in pike and emacs -*- things with c or pike in them. 
 
  if(!f) { 
    msg("No tokenizer found for %O\n", path); 
    return 0; 
  } 
 
  string data; 
  while(sizeof(data=in->read(4096))) 
    f->feed(data); 
  return f->close(); 
} 
 
void usage() { 
  werror("Usage: cgrep [OPTION]... TEXT FILE...\n" 
         "Try `grep --help' for more information.\n"); 
  exit(2); 
} 
 
int main(int num, array(string) args) { 
 
  int(0..1) recurse; 
  int(0..1) summarize; 
 
  foreach(Getopt.find_all_options(args, ({ 
    ({ "is_token",   Getopt.NO_ARG, "-T,--is-token"/","   }), 
    ({ "in_token",   Getopt.NO_ARG, "-t,--in-token"/","   }), 
    ({ "in_string",  Getopt.NO_ARG, "-s,--in-string"/","  }), 
    ({ "is_string",  Getopt.NO_ARG, "-S,--is-string"/","  }), 
    ({ "in_comment", Getopt.NO_ARG, "-c,--in-comment"/"," }), 
    ({ "summarize",  Getopt.NO_ARG, "--summarize"         }), 
    ({ "help",       Getopt.NO_ARG, "--help"              }), 
    ({ "version",    Getopt.NO_ARG, "--version"           }), 
    ({ "verbose",    Getopt.NO_ARG, "--verbose"           }), 
    ({ "quiet",      Getopt.NO_ARG, "-q,--quiet"/","      }), 
 
    ({ "-n", Getopt.NO_ARG, "-n,--line-number"/","    }), 
    ({ "-i", Getopt.NO_ARG, "-i,--ignore-case"/","    }), 
    ({ "-H", Getopt.NO_ARG, "-H,--with-filename"/","  }), 
    ({ "-r", Getopt.NO_ARG, "-r,--recursive"/","      }), 
    ({ "-l", Getopt.NO_ARG, "-l,--files-without-match"/"," }), 
    ({ "-L", Getopt.NO_ARG, "-L,--files-with-matches"/","  }), 
    ({ "-c", Getopt.NO_ARG, "--count"                 }), 
  })), array opt) { 
    switch(opt[0]) { 
    case "is_token": in_token=1; break; 
    case "in_token": in_token=2; break; 
    case "is_string": in_string=1; break; 
    case "in_string": in_string=2; break; 
    case "in_comment": in_comment=1; break; 
    case "summarize": summarize=1; break; 
    case "verbose": verbose=1; break; 
    case "quiet": quiet=1; break; 
    case "-n": show_lineno=1; break; 
    case "-i": case_insensitive=1; break; 
    case "-H": show_fn=1; break; 
    case "-r": recurse=1; break; 
    case "-l": supress_match=1; fn_only=1; break; 
    case "-L": supress_match=1; fn_only=2; break; 
    case "-c": supress_match=1; fn_only=3; break; 
    case "help": 
      write(doc); 
      exit(0); 
    case "version": 
      write( replace(version, ([ "$":"", "Revision: ":"" ])) ); 
      exit(0); 
    } 
  } 
  args = Getopt.get_args(args)[1..]; 
 
  if(!sizeof(args)) usage(); 
 
  target = args[0]; 
  if(case_insensitive) target = lower_case(target); 
 
  array(string) files = args[1..]; 
  if(recurse && !sizeof(files)) files = ({ "." }); 
  if(sizeof(files)>1 || recurse) show_fn = 1; 
  if(!sizeof(files)) usage(); 
 
  int matches; 
  foreach(files, string fn) 
    if(recurse && Stdio.is_dir(fn)) 
      foreach(Filesystem.Traversion(fn); string path; string fn) 
        matches += handle_file_safe(path+fn, fn); 
    else 
      matches += handle_file_safe(fn, basename(fn)); 
 
  if(summarize) 
    write("Total matches: %d\n", matches); 
 
  exit(!matches); 
} 
 
constant doc = #"Usage: cgrep [OPTION]... TEXT FILE... 
Search for TEXT in each FILE. 
Example: grep -i -s hello menu.h main.c prog.pike 
 
Text matching: 
  -T, --is-token            TEXT is a complete token 
  -t, --in-token            TEXT is part of a token 
  -S, --is-string           TEXT is a complete string literal 
  -s, --in-string           TEXT is part of a string literal 
  -c, --in-comment          TEXT is part of a comment 
  -i, --ignore-case         ignore case distinctions 
 
Miscellaneous: 
      --version             print version information and exit 
      --help                display this help and exit 
 
Output control: 
  -n, --line-number         print line number with output lines 
  -H, --with-filename       print the filename for each match 
      --verbose             output error messages 
  -q, --quiet               suppress all normal output 
  -r, --recursive           recurse into directories 
  -L, --files-without-match only print FILE names containing no match 
  -l, --files-with-matches  only print FILE names containing matches 
      --count               only print a count of matching lines per FILE 
      --summarize           print a summary of the number of matches 
"; 
 
constant version = sprintf(#"cgrep %d.%d.%d 
A token based grep with UI stolen from GNU grep. 
By Martin Nilsson 2003. 
",(int)__REAL_VERSION__,__REAL_MINOR__,__REAL_BUILD__);