-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjsgrep.java
More file actions
1701 lines (1456 loc) · 45.1 KB
/
Copy pathjsgrep.java
File metadata and controls
1701 lines (1456 loc) · 45.1 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of Yahoo! Inc. nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of Yahoo! Inc.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.*;
import java.util.*;
class Config
{
public String root;
public ArrayList directories;
public ArrayList filePatterns;
public ArrayList excludes;
};
class jsgrep
{
// -a # Show after context
public static int showAfter = 0;
// -b # Show before context
public static int showBefore = 0;
// -c Show count of matches
// -c-
// -c+ show matches even if 0
public static boolean showCount = false; // Show number of matches per file
public static boolean showZeroCount = false;
// -d Format output for DevStudio
// -d-
public static boolean devStudio = false;
// -f* response file
// -fp: additional file patterns that get added to default patterns
public static ArrayList extraFilePatterns = new ArrayList();
// -fp-: file pattern. Replaces anything in config file
public static ArrayList filePatterns = new ArrayList();
// -g show args
// -g-
public static boolean showArgs = false;
// -h Supress the file name for each match
// -h+: Show the file name for each match
// -ha: absolute file name
// -hc: file name relative to current directory
// -hr: file name relative to search root
public static boolean showFile = false; // Show filename for every individual match
public static int showFilePath = 0; // 0->no path, 1->relative to current, 2->relative to search root, 3->absolute
// -i Ignore case
// -i-
public static boolean insensitive = false;
// -l List files containing a match
// -l-
// -la
// -lc
// -lr
public static boolean listFiles = true; // List files containing a match
public static int listFilePath = 1; // 0->no path, 1->relative to current, 2->relative to search root, 3->absolute
// -m showMatch (defaults to false, showLine defaults to true)
// -m- don't show the match
// -m+ show the entire matching line
public static boolean showMatch = false;
public static boolean hideMatch = false;
// -n showLineNum
// -n-
public static boolean showLineNum = true; // Show line number for every match
// -o show options being used
// -o-
public static boolean showOptions = false;
// -p Show full path
// -p-
// -pa
// -pc: show path relative to current directory
// -pr: show path relative to search root
public static int filePath = 1; // default value for -l & -h
public static String searchRoot;
// -q quiet - no extra output
public static boolean quiet = false;
// -r Recursive
public static boolean recursive = true;
// -s showSearch
public static boolean showSearch = false; // Show the structure of the pattern we're trying to match
// -t match both token type and value
public static boolean matchTokenType = false;
// -v Invert match
public static boolean invertMatch = false;
// -x* Extended match (show all possible matches?)
// -x: exit after showing args, matches, options
public static boolean exit = false;
public static Hashtable extensions = new Hashtable();
public static ArrayList ordered_extensions = new ArrayList();
public static Hashtable config = new Hashtable();
public static String defaultSearch;
public static int totalCount = 0;
/////////////////////////////////////////////////////////////////////
public static void addFilePattern(ArrayList to, String pattern)
{
// convert pattern from a file pattern to a regex,
// i.e. * -> .* ? -> . . -> \.
pattern = pattern.replace(".", "\\.");
pattern = pattern.replace("*", ".*");
pattern = pattern.replace("?", ".");
to.add(java.util.regex.Pattern.compile(pattern));
}
/////////////////////////////////////////////////////////////////////
public static int readOptions(String[] args, int firstArg, int lastArg)
{
int argNum = firstArg;
while (argNum < lastArg)
{
args[argNum] = args[argNum].intern();
if (args[argNum] == "-a")
{
argNum++;
showAfter = Integer.parseInt(args[argNum]);
}
else if (args[argNum] == "-b")
{
argNum++;
showBefore = Integer.parseInt(args[argNum]);
}
else if (args[argNum] == "-c")
{
listFiles = true;
showCount = true;
showZeroCount = false;
hideMatch = true;
}
else if (args[argNum] == "-c+")
{
listFiles = true;
showCount = true;
showZeroCount = true;
hideMatch = true;
}
else if (args[argNum] == "-c-")
showCount = false;
else if (args[argNum] == "-d")
{
devStudio = true;
showFilePath = 3;
hideMatch = false;
}
else if (args[argNum] == "-d-")
devStudio = false;
else if (args[argNum] == "-fp-")
{
argNum++;
addFilePattern(filePatterns, args[argNum]);
}
else if (args[argNum] == "-fp")
{
argNum++;
addFilePattern(extraFilePatterns, args[argNum]);
}
else if (args[argNum] == "-g")
showArgs = true;
else if (args[argNum] == "-g-")
showArgs = false;
else if (args[argNum] == "-h")
{
showFile = false;
}
else if (args[argNum] == "-ha")
{
showFile = true;
showFilePath = 3;
}
else if (args[argNum] == "-hc")
{
showFile = true;
showFilePath = 1;
}
else if (args[argNum] == "-hf")
{
showFile = true;
showFilePath = 0;
}
else if (args[argNum] == "-hr")
{
showFile = true;
showFilePath = 2;
}
else if (args[argNum] == "-h+")
{
showFile = true;
showFilePath = filePath;
}
else if (args[argNum] == "-i")
insensitive = true;
else if (args[argNum] == "-i-")
insensitive = false;
else if (args[argNum] == "-l")
{
listFiles = true;
listFilePath = filePath;
hideMatch = true;
}
else if (args[argNum] == "-la")
{
listFiles = true;
listFilePath = 3;
hideMatch = true;
}
else if (args[argNum] == "-lc")
{
listFiles = true;
listFilePath = 1;
hideMatch = true;
}
else if (args[argNum] == "-lf")
{
listFiles = true;
listFilePath = 0;
hideMatch = true;
}
else if (args[argNum] == "-lr")
{
listFiles = true;
listFilePath = 2;
hideMatch = true;
}
else if (args[argNum] == "-l-")
listFiles = false;
else if (args[argNum] == "-m")
{
hideMatch = false;
showMatch = true;
}
else if (args[argNum] == "-m+")
{
hideMatch = false;
showMatch = false;
}
else if (args[argNum] == "-m-")
hideMatch = true;
else if (args[argNum] == "-n")
showLineNum = true;
else if (args[argNum] == "-n-")
showLineNum = false;
else if (args[argNum] == "-o")
showOptions = true;
else if (args[argNum] == "-o-")
showOptions = false;
else if (args[argNum] == "-p" || args[argNum] == "-pr")
{
filePath = 2;
listFilePath = 2;
showFilePath = 2;
}
else if (args[argNum] == "-p-" || args[argNum] == "-pf")
{
filePath = 0;
listFilePath = 0;
showFilePath = 0;
}
else if (args[argNum] == "-pa")
{
filePath = 3;
listFilePath = 3;
showFilePath = 3;
}
else if (args[argNum] == "-pc")
{
filePath = 1;
listFilePath = 1;
showFilePath = 1;
}
else if (args[argNum] == "-q")
{
quiet = true;
showSearch = false;
showArgs = false;
showOptions = false;
listFiles = false;
}
else if (args[argNum] == "-q-")
{
quiet = false;
}
else if (args[argNum] == "-r")
recursive = true;
else if (args[argNum] == "-r-")
recursive = false;
else if (args[argNum] == "-s")
showSearch = true;
else if (args[argNum] == "-s-")
showSearch = false;
else if (args[argNum] == "-t")
matchTokenType = true;
else if (args[argNum] == "-t-")
matchTokenType = false;
else if (args[argNum] == "-v")
invertMatch = true;
else if (args[argNum] == "-v-")
invertMatch = false;
else if (args[argNum] == "-x")
exit = true;
else if (args[argNum] == "-x-")
exit = false;
else
break;
argNum++;
}
return argNum - firstArg;
}
/////////////////////////////////////////////////////////////////////
public static void showOptions(ArrayList files)
{
System.out.println("Files/directories to be searched:");
String comma = "";
for (int i = 0; i < files.size(); i++)
{
String s = (String)files.get(i);
System.out.print(comma + s);
comma = ", ";
}
System.out.println();
if (listFiles)
{
System.out.print("File header includes ");
if (showCount)
System.out.print("count of matches and ");
switch (listFilePath)
{
case 0: System.out.println("file name"); break;
case 1: System.out.println("file name relative to current directory"); break;
case 2: System.out.println("file name relative to " + searchRoot); break;
case 3: System.out.println("absolute path to file"); break;
}
}
System.out.print("Each match will ");
if (hideMatch)
System.out.print("be hidden ");
else
{
if (invertMatch)
System.out.print("be inverted and ");
if (showMatch)
System.out.print("show the matching tokens ");
else
System.out.print("show complete lines which contain a match ");
if (showBefore > 0 || showAfter > 0)
System.out.print("with " + showBefore + " items of context before the match and " + showAfter + " items after it");
System.out.println();
System.out.print("Matches will be prefixed with");
if (showFile || showLineNum)
{
if (showFile)
{
System.out.print(" the file name");
if (showLineNum)
System.out.print(" and");
}
if (showLineNum)
{
System.out.print(" the line number");
if (devStudio)
System.out.print(" formatted for DevStudio");
}
}
else
System.out.print(" nothing");
}
System.out.println();
System.out.println();
}
/////////////////////////////////////////////////////////////////////
public static int readFiles(String[] args, ArrayList files, int firstArg, int lastArg)
{
int argNum = firstArg;
while (argNum < lastArg)
{
File f = new File(args[argNum]);
if (!f.exists())
break;
files.add(args[argNum]);
argNum++;
}
return argNum - firstArg;
}
/////////////////////////////////////////////////////////////////////
public static String[] reverseArgs(String[] args)
{
String[] reversed = new String[args.length];
for (int i = 0; i < args.length; i++)
{
int p = args.length - i - 1;
args[i] = args[i].intern();
if (args[i] == "-a" || args[i] == "-b" || args[i] == "-fp")
{
reversed[p-1] = args[i];
reversed[p] = args[i+1];
i++;
args[i] = args[i].intern();
}
else
reversed[p] = args[i];
}
return reversed;
}
/////////////////////////////////////////////////////////////////////
public static void main(String[] args)
throws IOException, Throwable
{
ArrayList files = new ArrayList();
String regex = "";
readConfigFile();
if (args.length == 0)
{
showHelp("--usage");
return;
}
if (args.length == 1 && args[0].intern() == "-h")
{
showHelp("--help");
return;
}
if (args.length == 2 && args[0].intern() == "-h")
{
showHelp(args[1].intern());
return;
}
if (args.length == 1 && args[0].startsWith("--"))
{
showHelp(args[0].intern());
return;
}
String[] reversed = reverseArgs(args);
int firstArg = 0, lastArg = args.length;
Config cfg = null;
while (firstArg < lastArg)
{
int cfirst = firstArg;
int clast = lastArg;
if (cfg == null)
{
cfg = (Config)config.get(args[firstArg]);
if (cfg != null)
firstArg++;
}
if (cfg == null)
{
cfg = (Config)config.get(args[lastArg-1]);
if (cfg != null)
lastArg--;
}
firstArg += readOptions(args, firstArg, lastArg);
lastArg -= readOptions(reversed, args.length - lastArg, args.length - firstArg);
firstArg += readFiles(args, files, firstArg, lastArg);
lastArg -= readFiles(reversed, files, args.length - lastArg, args.length - firstArg);
if (cfirst == firstArg && clast == lastArg)
break;
}
if (cfg == null && defaultSearch != null)
cfg = (Config)config.get(defaultSearch);
if (showArgs)
{
for (int i = 0; i < args.length; i++)
System.out.println("arg " + i + ": " + args[i]);
System.out.println();
}
searchRoot = ".";
if (files.size() == 0 && cfg != null)
{
if (cfg.root != null)
searchRoot = cfg.root;
if (cfg.directories == null)
files.add(searchRoot);
else
{
for (int i = 0; i < cfg.directories.size(); i++)
{
String dir = (String)cfg.directories.get(i);
files.add(searchRoot + File.separator + dir);
}
}
}
if (filePatterns.size() == 0)
{
if (cfg != null && cfg.filePatterns != null)
{
for (int i = 0; i < cfg.filePatterns.size(); i++)
{
String pattern = (String)cfg.filePatterns.get(i);
addFilePattern(filePatterns, pattern);
}
}
else
addFilePattern(filePatterns, "*.js");
}
for (int i = 0; i < extraFilePatterns.size(); i++)
{
java.util.regex.Pattern p = (java.util.regex.Pattern)extraFilePatterns.get(i);
filePatterns.add(p);
}
File sr = new File(searchRoot);
searchRoot = sr.getCanonicalPath();
if (files.size() == 0)
files.add(".");
if (showOptions)
showOptions(files);
regex = "";
if (lastArg > firstArg && args[firstArg].length() != 0)
{
while (lastArg > firstArg)
{
regex += " " + args[firstArg++];
}
}
if (regex.length() == 0)
{
showHelp("--usage");
System.out.println("ERROR: No regex specified\n");
return;
}
if (showSearch)
{
System.out.println(regex);
JsPattern p = (new Maker()).makePattern(new JsMatcher(null, null), regex, extensions, 0, matchTokenType, insensitive);
p.dump(0);
System.out.println();
}
if (!exit)
{
for (int i = 0; i < files.size(); i++)
{
String s = (String)files.get(i);
if (s.startsWith("~"))
s = System.getProperty("user.home") + s.substring(1);
HandleFile(s, cfg, regex, true);
}
}
if (!quiet)
{
if (showCount)
System.out.println(totalCount + " matches found");
System.out.println("jsgrep done");
}
}
/////////////////////////////////////////////////////////////////////
public static void readPatterns(Token[] tokens, int tn, int max)
throws Throwable
{
String name, pattern = null, regex = null, comment;
// NAME COLON STRING COMMA or
// NAME COLON LC NAME COLON STRING COMMA NAME COLON STRING RC COMMA
while (tn < max)
{
Token t = tokens[tn++];
name = t.value_;
tn++; // skip the colon
t = tokens[tn++];
if (t.type == Token.STRING)
{
regex = t.value_;
pattern = null;
comment = null;
}
else
{
int close = match(tokens, tn-1);
pattern = null;
regex = "";
comment = null;
while (tn < close)
{
t = tokens[tn++];
String what = t.value_;
tn++; // skip the colon
t = tokens[tn++];
if (what == "pattern")
pattern = t.value_;
else if (what == "match")
regex = t.value_;
else if (what == "comment")
comment = t.value_;
tn++; // skip the comma or right brace
}
}
tn++; // Skip the comma
Extension e = new Extension(name, regex, pattern, comment);
extensions.put(name, e);
ordered_extensions.add(e);
}
}
/////////////////////////////////////////////////////////////////////
public static ArrayList readArray(Token[] tokens, int tn, int end)
{
ArrayList a = new ArrayList();
while (tn < end)
{
a.add(tokens[tn++].value_);
tn++;
}
return a;
}
/////////////////////////////////////////////////////////////////////
public static ArrayList readFilePatternArray(Token[] tokens, int tn, int end)
{
ArrayList a = readArray(tokens, tn, end);
ArrayList files = new ArrayList();
for (int i = 0; i < a.size(); i++)
{
String p = (String)a.get(i);
addFilePattern(files, p);
}
return files;
}
/////////////////////////////////////////////////////////////////////
public static void readSearches(Token[] tokens, int tn, int max)
throws Throwable
{
int end;
String name;
while (tn < max)
{
Token t = tokens[tn++];
name = t.value_;
Config cfg = new Config();
config.put(name, cfg);
tn++; // skip the colon
int close = match(tokens, tn);
tn++;
while (tn < close)
{
t = tokens[tn++];
tn++;
if (t.value_ == "root")
{
cfg.root = tokens[tn++].value_;
}
else if (t.value_ == "exclude")
{
end = tn+1;
if (tokens[tn].type == Token.LB)
{
end = match(tokens, tn);
tn++;
}
cfg.excludes = readFilePatternArray(tokens, tn, end);
tn = end;
}
else if (t.value_ == "directories")
{
end = tn+1;
if (tokens[tn].type == Token.LB)
{
end = match(tokens, tn);
tn++;
}
cfg.directories = readArray(tokens, tn, end);
tn = end;
}
else if (t.value_ == "filepatterns")
{
end = tn+1;
if (tokens[tn].type == Token.LB)
{
end = match(tokens, tn);
tn++;
}
cfg.filePatterns = readArray(tokens, tn, end);
tn = end;
}
tn++; // skip the comma
}
tn = close + 1;
}
}
/////////////////////////////////////////////////////////////////////
public static void getDefaultOptions(String o)
{
String[] options = o.split("\\s+");
readOptions(options, 0, options.length);
}
/////////////////////////////////////////////////////////////////////
public static void readConfigFile()
throws Exception, Throwable
{
if (readConfigFile("jsgrep.cfg"))
return;
String cfgFile = System.getProperty("user.home") + "/.jsgrep.cfg";
if (readConfigFile(cfgFile))
return;
readConfigFile("/home/y/conf/jsgrep/jsgrep.cfg");
}
/////////////////////////////////////////////////////////////////////
public static boolean readConfigFile(String cfgFile)
throws Exception, Throwable
{
int len = (int)(new File(cfgFile)).length();
if (len == 0)
return false;
char[] filedata = new char[len];
FileReader fr = new FileReader(cfgFile);
len = fr.read(filedata, 0, len);
fr.close();
Token[] tokens = getTokens(filedata);
int tn = 0;
int max = tokens.length;
if (tokens[tn].type == Token.LC)
{
tn++;
max--;
}
while (tn < max)
{
Token t = tokens[tn++];
// if (t.type != Token.NAME)
// throw new Throwable("Invalid JSON in config file. Expected member name on line " + t.lineNum);
String name = t.value_.intern();
t = tokens[tn++];
if (t.type != Token.COLON)
throw new Throwable("Invalid JSON in config file. Expected : on line " + t.lineNum);
t = tokens[tn];
// if (t.type != Token.LC)
// throw new Throwable("Invalid JSON in config file. Expected { on line " + t.lineNum);
int last = match(tokens, tn);
if (name == "patterns")
readPatterns(tokens, tn+1, last-1);
else if (name == "searches")
readSearches(tokens, tn+1, last-1);
else if (name == "default")
{
defaultSearch = tokens[tn].value_;
last = tn + 1;
}
else if (name == "options")
{
getDefaultOptions(tokens[tn].value_);
last = tn + 1;
}
else
System.out.println("Skipping unknown config section " + name + ".");
tn = last+1; // skip the comma
}
return true;
}
/////////////////////////////////////////////////////////////////////
public static int match(Token[] tokens, int tn)
throws Throwable
{
int nesting = 1;
int searchFor = 0;
Token opener = tokens[tn++];
if (opener.type == Token.LP)
searchFor = Token.RP;
else if (opener.type == Token.LC)
searchFor = Token.RC;
else if (opener.type == Token.LB)
searchFor = Token.RB;
else
return tn;
if (searchFor == 0)
return tn;
while (tn < tokens.length)
{
Token t = tokens[tn++];
if (t.type == searchFor)
nesting--;
if (t.type == opener.type)
nesting++;
if (nesting == 0)
return tn;
}
throw new Throwable("Unmatched " + opener.value_ + " on line " + opener.lineNum + " " + opener.charOnLine);
}
/////////////////////////////////////////////////////////////////////
public static void HandleFile(String name, Config cfg, String regex, Boolean ignoreFilter)
throws IOException, Throwable
{
File f = new File(name);
if (!ignoreFilter)
{
if (cfg != null && cfg.excludes != null)
{
for (int i = 0; i < cfg.excludes.size(); i++)
{
java.util.regex.Pattern p = (java.util.regex.Pattern)cfg.excludes.get(i);
java.util.regex.Matcher m = p.matcher(f.getName());
if (m.matches())
return;
}
}
}
if (f.isDirectory())
{
String[] files = f.list();
if (files != null)
{
for (int i = 0; i < files.length; i++)
{
String oneName = name + File.separator + files[i];
if (!recursive)
{
File f2 = new File(oneName);
if (f2.isDirectory())
continue;
}
HandleFile(oneName, cfg, regex, false);
}
}
}
else
{
boolean doFile = ignoreFilter;
for (int i = 0; !doFile && i < filePatterns.size(); i++)
{
java.util.regex.Pattern p = (java.util.regex.Pattern)filePatterns.get(i);
java.util.regex.Matcher m = p.matcher(f.getName());
if (m.matches())
doFile = true;
}
if (doFile)
{
try
{
ProcessFile(f, cfg, regex);
}
catch (Throwable e)
{
if (quiet)
throw e;
System.out.println(e);
e.printStackTrace();
}
}
}
}
/////////////////////////////////////////////////////////////////////
public static String getLinePrefix(String file, int lineNum)