-
Notifications
You must be signed in to change notification settings - Fork 0
/
break_filelist
executable file
·1373 lines (1176 loc) · 44.3 KB
/
break_filelist
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
#!/usr/bin/perl -w
# break_filelist
# Take a list of dirs which contain a "filelist";
# creates files in each directory identifying which are C, C++, Perl, etc.
# For example, "ansic.dat" lists all ANSI C files contained in filelist.
# Note: ".h" files are ambiguous (they could be C or C++); the program
# uses heuristics to determine this.
# The list of .h files is also contained in h_list.dat.
# This is part of SLOCCount, a toolsuite that counts
# source lines of code (SLOC).
# Copyright (C) 2001-2004 David A. Wheeler.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# To contact David A. Wheeler, see his website at:
# http://www.dwheeler.com.
# If adding a new language: add the logic to open the file,
# close the file, and detect & write to the file listing that language.
# Debatable decisions:
# Doesn't count .dsl files (stylesheets, which are partially LISP).
# Doesn't count .sql files (SQL queries & commands)
# Note - I don't try to distinguish between TCL and [incr TCL] (itcl),
# an OO extended version of TCL. For our purposes, it's all TCL.
use FileHandle;
# Set default configuration:
$duplicates_okay = 0; # Set to 1 if you want to count file duplicates.
$crossdups_okay = 0; # Set to 1 if duplicates okay in different filelists.
$autogen_okay = 0; # Set to 1 if you want to count autogen'ed files.
$noisy = 0; # Set to 1 if you want noisy reports.
%lang_list_files = ();
# The following extensions are NOT code:
%not_code_extensions = (
"html" => 1,
"in" => 1, # Debatable.
"xpm" => 1,
"po" => 1,
"am" => 1, # Debatable.
"1" => 1, # Man pages (documentation):
"2" => 1,
"3" => 1,
"4" => 1,
"5" => 1,
"6" => 1,
"7" => 1,
"8" => 1,
"9" => 1,
"n" => 1,
"gif" => 1,
"tfm" => 1,
"png" => 1,
"m4" => 1, # Debatable.
"bdf" => 1,
"sgml" => 1,
"mf" => 1,
"txt" => 1, "text" => 1,
"man" => 1,
"xbm" => 1,
"Tag" => 1,
"sgm" => 1,
"vf" => 1,
"tex" => 1,
"elc" => 1,
"gz" => 1,
"dic" => 1,
"pfb" => 1,
"fig" => 1,
"afm" => 1, # font metrics
"jpg" => 1,
"bmp" => 1,
"htm" => 1,
"kdelnk" => 1,
"desktop" => 1,
"pbm" => 1,
"pdf" => 1,
"ps" => 1, # Postscript is _USUALLY_ generated automatically.
"eps" => 1,
"doc" => 1,
"man" => 1,
"o" => 1, # Object code is generated from source code.
"a" => 1, # Static object code.
"so" => 1, # Dynamically-loaded object code.
"Y" => 1, # file compressed with "Yabba"
"Z" => 1, # file compressed with "compress"
"ad" => 1, # X application default resource file.
"arc" => 1, # arc(1) archive
"arj" => 1, # arj(1) archive
"au" => 1, # Audio sound filearj(1) archive
"wav" => 1,
"bak" => 1, # Backup files - we only want to count the "real" files.
"bz2" => 1, # bzip2(1) compressed file
"mp3" => 1, # zip archive
"tgz" => 1, # tarball
"zip" => 1, # zip archive
);
# The following filenames are NOT code:
%not_code_filenames = (
"README" => 1,
"Readme" => 1,
"readme" => 1,
"README.tk" => 1, # used in kdemultimedia, it's confusing.
"Changelog" => 1,
"ChangeLog" => 1,
"Repository" => 1,
"CHANGES" => 1,
"Changes" => 1,
".cvsignore" => 1,
"Root" => 1, # CVS.
"BUGS" => 1,
"TODO" => 1,
"COPYING" => 1,
"MAINTAINERS" => 1,
"Entries" => 1,
# Skip "iconfig.h" files; they're used in Imakefiles
# (used in xlockmore):
"iconfig.h" => 1,
);
# A filename ending in the following extensions usually maps to the
# given language:
# TODO: See suffixes(7)
# .al Perl autoload file
# .am automake input
%file_extensions = (
"c" => "ansic",
"ec" => "ansic", # Informix C.
"ecp" => "ansic", # Informix C.
"pgc" => "ansic", # Postgres embedded C/C++ (guess C)
"C" => "cpp", "cpp" => "cpp", "cxx" => "cpp", "cc" => "cpp",
"pcc" => "cpp", # Input to Oracle C++ preproc.
"m" => "objc",
"mm" => "objc",
# C# (C-sharp) is named 'cs', not 'c#', because
# the '#' is a comment character and I'm trying to
# avoid bug-prone conventions.
# C# doesn't support header files.
"cs" => "cs",
# Header files are allocated to the "h" language, and then
# copied to the correct location later so that C/C++/Objective-C
# can be separated.
"h" => "h", "H" => "h", "hpp" => "h", "hh" => "h",
"ada" => "ada", "adb" => "ada", "ads" => "ada",
"pad" => "ada", # Oracle Ada preprocessor.
"f" => "fortran", "F" => "fortran", # This catches "wokka.F" as Fortran.
# Warning: "Freeze" format also uses .f. Haven't heard of problems,
# freeze is extremely rare and even more rare in source code directories.
"f77" => "fortran", "F77" => "fortran",
"f90" => "f90", "F90" => "f90",
"cob" => "cobol", "cbl" => "cobol",
"COB" => "cobol", "CBL" => "cobol", # Yes, people do create wokka.CBL files
"p" => "pascal", "pas" => "pascal", "pp" => "pascal", "dpr" => "pascal",
"pig" => "pig", "piglet" => "pig",
"py" => "python",
"s" => "asm", "S" => "asm", "asm" => "asm",
"sh" => "sh", "bash" => "sh",
"csh" => "csh", "tcsh" => "csh",
"java" => "java",
"lisp" => "lisp", "el" => "lisp", "scm" => "lisp", "sc" => "lisp",
"lsp" => "lisp", "cl" => "lisp",
"jl" => "lisp",
"tcl" => "tcl", "tk" => "tcl", "itk" => "tcl",
"exp" => "exp",
"pl" => "perl", "pm" => "perl", "perl" => "perl", "ph" => "perl",
"awk" => "awk",
"sed" => "sed",
"y" => "yacc",
"l" => "lex",
"makefile" => "makefile",
"sql" => "sql",
"php" => "php", "php3" => "php", "php4" => "php", "php5" => "php",
"php6" => "php",
"inc" => "inc", # inc MAY be PHP - we'll handle it specially.
"m3" => "modula3", "i3" => "modula3",
"mg" => "modula3", "ig" => "modula3",
"ml" => "ml", "mli" => "ml",
"mly" => "ml", # ocamlyacc. In fact this is half-yacc half-ML, especially
# comments in yacc part are C-like, not ML like.
"mll" => "ml", # ocamllex, no such problems as in ocamlyacc
"rb" => "ruby",
"hs" => "haskell", "lhs" => "haskell",
# ???: .pco is Oracle Cobol
"jsp" => "jsp", # Java server pages
"bas" => "vbasic", "frm" => "vbasic", "cls" => "vbasic", "dsr" => "vbasic",
"vbs" => "vbscript", "wsh" => "vbscript",
"js" => "javascript",
"ahk" => "autohotkey",
"au3" => "autoit",
"bat" => "batch", "cmd" => "batch",
"iss" => "innosetup",
"go" => "golang",
);
# GLOBAL VARIABLES
$dup_count = 0;
$warning_from_first_line = "";
%examined_directories = (); # Keys = Names of directories examined this run.
$duplistfile = "";
###########
# Handle re-opening individual CODE_FILEs.
# CODE_FILE is public
# Private value:
$opened_file_name = "";
sub reopen {
# Open file if it isn't already, else rewind.
# If filename is "", close any open file.
my $filename = shift;
chomp($filename);
# print("DEBUG: reopen($filename)\n");
if ($filename eq "") {
if ($opened_file_name) {close(CODE_FILE);}
$opened_file_name = "";
return;
}
if ($filename eq $opened_file_name) {
seek CODE_FILE, 0, 0; # Rewind.
} else { # We're opening a new file.
if ($opened_file_name) {close(CODE_FILE)}
open(CODE_FILE, "<$filename\0") || die "Can't open $filename";
$opened_file_name = $filename;
}
}
###########
sub looks_like_cpp {
# returns a confidence level - does the file looks like it's C++?
my $filename = shift;
my $confidence = 0;
chomp($filename);
open( SUSPECT, "<$filename");
while (defined($_ = <SUSPECT>)) {
if (m/^\s*class\b.*\{/) { # "}"
close(SUSPECT);
return 2;
}
if (m/^\s*class\b/) {
$confidence = 1;
}
}
close(SUSPECT);
return $confidence;
}
# Cache which files are objective-C or not.
# Key is the full file pathname; value is 1 if objective-C (else 0).
%objective_c_files = ();
sub really_is_objc {
# Given filename, returns TRUE if its contents really are objective-C.
my $filename = shift;
chomp($filename);
my $is_objc = 0; # Value to determine.
my $brace_lines = 0; # Lines that begin/end with curly braces.
my $plus_minus = 0; # Lines that begin with + or -.
my $word_main = 0; # Did we find "main("?
my $special = 0; # Did we find a special Objective-C pattern?
# Return cached result, if available:
if ($objective_c_files{$filename}) { return $objective_c_files{$filename};}
open(OBJC_FILE, "<$filename") ||
die "Can't open $filename to determine if it's objective C.\n";
while(<OBJC_FILE>) {
if (m/^\s*[{}]/ || m/[{}];?\s*$/) { $brace_lines++;}
if (m/^\s*[+-]/) {$plus_minus++;}
if (m/\bmain\s*\(/) {$word_main++;} # "main" followed by "("?
# Handle /usr/src/redhat/BUILD/egcs-1.1.2/gcc/objc/linking.m:
if (m/^\s*\[object name\];\s*$/i) {$special=1;}
}
close(OBJC_FILE);
if (($brace_lines > 1) && (($plus_minus > 1) || $word_main || $special))
{$is_objc = 1;}
$objective_c_files{$filename} = $is_objc; # Store result in cache.
return $is_objc;
}
# Cache which files are lex or not.
# Key is the full file pathname; value is 1 if lex (else 0).
%lex_files = ();
sub really_is_lex {
# Given filename, returns TRUE if its contents really is lex.
# lex file must have "%%", "%{", and "%}".
# In theory, a lex file doesn't need "%{" and "%}", but in practice
# they all have them, and requiring them avoid mislabeling a
# non-lexfile as a lex file.
my $filename = shift;
chomp($filename);
my $is_lex = 0; # Value to determine.
my $percent_percent = 0;
my $percent_opencurly = 0;
my $percent_closecurly = 0;
# Return cached result, if available:
if ($lex_files{$filename}) { return $lex_files{$filename};}
open(LEX_FILE, "<$filename") ||
die "Can't open $filename to determine if it's lex.\n";
while(<LEX_FILE>) {
$percent_percent++ if (m/^\s*\%\%/);
$percent_opencurly++ if (m/^\s*\%\{/);
$percent_closecurly++ if (m/^\s*\%\}/);
}
close(LEX_FILE);
if ($percent_percent && $percent_opencurly && $percent_closecurly)
{$is_lex = 1;}
$lex_files{$filename} = $is_lex; # Store result in cache.
return $is_lex;
}
# Cache which files are expect or not.
# Key is the full file pathname; value is 1 if it is (else 0).
%expect_files = ();
sub really_is_expect {
# Given filename, returns TRUE if its contents really are Expect.
# Many "exp" files (such as in Apache and Mesa) are just "export" data,
# summarizing something else # (e.g., its interface).
# Sometimes (like in RPM) it's just misc. data.
# Thus, we need to look at the file to determine
# if it's really an "expect" file.
my $filename = shift;
chomp($filename);
# The heuristic is as follows: it's Expect _IF_ it:
# 1. has "load_lib" command and either "#" comments or {}.
# 2. {, }, and one of: proc, if, [...], expect
my $is_expect = 0; # Value to determine.
my $begin_brace = 0; # Lines that begin with curly braces.
my $end_brace = 0; # Lines that begin with curly braces.
my $load_lib = 0; # Lines with the Load_lib command.
my $found_proc = 0;
my $found_if = 0;
my $found_brackets = 0;
my $found_expect = 0;
my $found_pound = 0;
# Return cached result, if available:
if ($expect_files{$filename}) { return expect_files{$filename};}
open(EXPECT_FILE, "<$filename") ||
die "Can't open $filename to determine if it's expect.\n";
while(<EXPECT_FILE>) {
if (m/#/) {$found_pound++; s/#.*//;}
if (m/^\s*\{/) { $begin_brace++;}
if (m/\{\s*$/) { $begin_brace++;}
if (m/^\s*\}/) { $end_brace++;}
if (m/\};?\s*$/) { $end_brace++;}
if (m/^\s*load_lib\s+\S/) { $load_lib++;}
if (m/^\s*proc\s/) { $found_proc++;}
if (m/^\s*if\s/) { $found_if++;}
if (m/\[.*\]/) { $found_brackets++;}
if (m/^\s*expect\s/) { $found_expect++;}
}
close(EXPECT_FILE);
if ($load_lib && ($found_pound || ($begin_brace && $end_brace)))
{$is_expect = 1;}
if ( $begin_brace && $end_brace &&
($found_proc || $found_if || $found_brackets || $found_expect))
{$is_expect = 1;}
$expect_files{$filename} = $is_expect; # Store result in cache.
return $is_expect;
}
# Cached values.
%pascal_files = ();
sub really_is_pascal {
# Given filename, returns TRUE if its contents really are Pascal.
# This isn't as obvious as it seems.
# Many ".p" files are Perl files
# (such as /usr/src/redhat/BUILD/ispell-3.1/dicts/czech/glob.p),
# others are C extractions
# (such as /usr/src/redhat/BUILD/linux/include/linux/umsdos_fs.p
# and some files in linuxconf).
# However, test files in "p2c" really are Pascal, for example.
# Note that /usr/src/redhat/BUILD/ucd-snmp-4.1.1/ov/bitmaps/UCD.20.p
# is actually C code. The heuristics determine that they're not Pascal,
# but because it ends in ".p" it's not counted as C code either.
# I believe this is actually correct behavior, because frankly it
# looks like it's automatically generated (it's a bitmap expressed as code).
# Rather than guess otherwise, we don't include it in a list of
# source files. Let's face it, someone who creates C files ending in ".p"
# and expects them to be counted by default as C files in SLOCCount needs
# their head examined. I suggest examining their head
# with a sucker rod (see syslogd(8) for more on sucker rods).
# This heuristic counts as Pascal such files such as:
# /usr/src/redhat/BUILD/teTeX-1.0/texk/web2c/tangleboot.p
# Which is hand-generated. We don't count woven documents now anyway,
# so this is justifiable.
my $filename = shift;
chomp($filename);
# The heuristic is as follows: it's Pascal _IF_ it has all of the following
# (ignoring {...} and (*...*) comments):
# 1. "^..program NAME" or "^..unit NAME",
# 2. "procedure", "function", "^..interface", or "^..implementation",
# 3. a "begin", and
# 4. it ends with "end.",
#
# Or it has all of the following:
# 1. "^..module NAME" and
# 2. it ends with "end.".
#
# Or it has all of the following:
# 1. "^..program NAME",
# 2. a "begin", and
# 3. it ends with "end.".
#
# The "end." requirements in particular filter out non-Pascal.
#
# Note (jgb): this does not detect Pascal main files in fpc, like
# fpc-1.0.4/api/test/testterminfo.pas, which does not have "program" in
# it
my $is_pascal = 0; # Value to determine.
my $has_program = 0;
my $has_unit = 0;
my $has_module = 0;
my $has_procedure_or_function = 0;
my $found_begin = 0;
my $found_terminating_end = 0;
# Return cached result, if available:
if ($pascal_files{$filename}) { return pascal_files{$filename};}
open(PASCAL_FILE, "<$filename") ||
die "Can't open $filename to determine if it's pascal.\n";
while(<PASCAL_FILE>) {
s/\{.*?\}//g; # Ignore {...} comments on this line; imperfect, but effective.
s/\(\*.*?\*\)//g; # Ignore (*...*) comments on this line; imperfect, but effective.
if (m/\bprogram\s+[A-Za-z]/i) {$has_program=1;}
if (m/\bunit\s+[A-Za-z]/i) {$has_unit=1;}
if (m/\bmodule\s+[A-Za-z]/i) {$has_module=1;}
if (m/\bprocedure\b/i) { $has_procedure_or_function = 1; }
if (m/\bfunction\b/i) { $has_procedure_or_function = 1; }
if (m/^\s*interface\s+/i) { $has_procedure_or_function = 1; }
if (m/^\s*implementation\s+/i) { $has_procedure_or_function = 1; }
if (m/\bbegin\b/i) { $has_begin = 1; }
# Originally I said:
# "This heuristic fails if there are multi-line comments after
# "end."; I haven't seen that in real Pascal programs:"
# But jgb found there are a good quantity of them in Debian, specially in
# fpc (at the end of a lot of files there is a multiline comment
# with the changelog for the file).
# Therefore, assume Pascal if "end." appears anywhere in the file.
if (m/end\.\s*$/i) {$found_terminating_end = 1;}
# elsif (m/\S/) {$found_terminating_end = 0;}
}
close(PASCAL_FILE);
# Okay, we've examined the entire file looking for clues;
# let's use those clues to determine if it's really Pascal:
if ( ( ($has_unit || $has_program) && $has_procedure_or_function &&
$has_begin && $found_terminating_end ) ||
( $has_module && $found_terminating_end ) ||
( $has_program && $has_begin && $found_terminating_end ) )
{$is_pascal = 1;}
$pascal_files{$filename} = $is_pascal; # Store result in cache.
return $is_pascal;
}
sub really_is_incpascal {
# Given filename, returns TRUE if its contents really are Pascal.
# For .inc files (mainly seen in fpc)
my $filename = shift;
chomp($filename);
# The heuristic is as follows: it is Pacal if any of the following:
# 1. really_is_pascal returns true
# 2. Any usual reserverd word is found (program, unit, const, begin...)
# If the general routine for Pascal files works, we have it
if (&really_is_pascal ($filename)) {
$pascal_files{$filename} = 1;
return 1;
}
my $is_pascal = 0; # Value to determine.
my $found_begin = 0;
open(PASCAL_FILE, "<$filename") ||
die "Can't open $filename to determine if it's pascal.\n";
while(<PASCAL_FILE>) {
s/\{.*?\}//g; # Ignore {...} comments on this line; imperfect, but effective.
s/\(\*.*?\*\)//g; # Ignore (*...*) comments on this line; imperfect, but effective.
if (m/\bprogram\s+[A-Za-z]/i) {$is_pascal=1;}
if (m/\bunit\s+[A-Za-z]/i) {$is_pascal=1;}
if (m/\bmodule\s+[A-Za-z]/i) {$is_pascal=1;}
if (m/\bprocedure\b/i) {$is_pascal = 1; }
if (m/\bfunction\b/i) {$is_pascal = 1; }
if (m/^\s*interface\s+/i) {$is_pascal = 1; }
if (m/^\s*implementation\s+/i) {$is_pascal = 1; }
if (m/\bconstant\s+/i) {$is_pascal=1;}
if (m/\bbegin\b/i) { $found_begin = 1; }
if ((m/end\.\s*$/i) && ($found_begin = 1)) {$is_pascal = 1;}
if ($is_pascal) {
last;
}
}
close(PASCAL_FILE);
$pascal_files{$filename} = $is_pascal; # Store result in cache.
return $is_pascal;
}
# Cache which files are php or not.
# Key is the full file pathname; value is 1 if it is (else 0).
%php_files = ();
sub really_is_php {
# Given filename, returns TRUE if its contents really is php.
my $filename = shift;
chomp($filename);
my $is_php = 0; # Value to determine.
# Need to find a matching pair of surrounds, with ending after beginning:
my $normal_surround = 0; # <?; bit 0 = <?, bit 1 = ?>
my $script_surround = 0; # <script..>; bit 0 = <script language="php">
my $asp_surround = 0; # <%; bit 0 = <%, bit 1 = %>
# Return cached result, if available:
if ($php_files{$filename}) { return $php_files{$filename};}
open(PHP_FILE, "<$filename") ||
die "Can't open $filename to determine if it's php.\n";
while(<PHP_FILE>) {
if (m/\<\?/) { $normal_surround |= 1; }
if (m/\?\>/ && ($normal_surround & 1)) { $normal_surround |= 2; }
if (m/\<script.*language="?php"?/i) { $script_surround |= 1; }
if (m/\<\/script\>/i && ($script_surround & 1)) { $script_surround |= 2; }
if (m/\<\%/) { $asp_surround |= 1; }
if (m/\%\>/ && ($asp_surround & 1)) { $asp_surround |= 2; }
}
close(PHP_FILE);
if ( ($normal_surround == 3) || ($script_surround == 3) ||
($asp_surround == 3)) {
$is_php = 1;
}
$php_files{$filename} = $is_php; # Store result in cache.
return $is_php;
}
# Cache which files are pig or not.
# Key is the full file pathname; value is 1 if it is (else 0).
%pig_files = ();
sub really_is_pig {
# Given filename, returns TRUE if its contents really is pig.
my $filename = shift;
chomp($filename);
my $is_pig = 0; # Value to determine.
# Need to find a FOREACH, LOAD or DUMP, and a semicolon
# Return cached result, if available:
if ($pig_files{$filename}) { return $pig_files{$filename}; }
open(PIG_FILE, "<$filename") ||
die "Can't open $filename to determine if it's pig.\n";
while(<PIG_FILE>) {
# most Pig opterations need a terminating semicolon and equals
# signs to define a relation
if (m/;/i) { $script_semicolon |= 1; }
if (m/=/i) { $script_equals |= 1; }
# all FOREACH's need a GENERATE
if (m/FOREACH/i) { $script_foreach |= 1; }
if (m/GENERATE/i) { $script_foreach |= 2; }
# all LOAD's & DUMP's need a USING
if (m/(LOAD|DUMP)/i) { $script_input_output |= 1; }
if (m/USING/i) { $script_input_output |= 2; }
# all JOIN's, GROUP's & FILTER's need a BY
if (m/(JOIN|GROUP|FILTER)/i) { $script_dataset |= 1; }
if (m/BY/i) { $script_dataset |= 2; }
}
close(PIG_FILE);
if ( $script_semicolon == 1 && $script_equals == 1 && ($script_foreach == 3 ||
$script_dataset == 3 || $script_input_output == 3) ) {
$is_pig = 1;
}
$pig_files{$filename} = $is_pig; # Store result in cache.
return $is_pig;
}
sub examine_dir {
# Given a file, determine if there are only C++, OBJC, C, or a mixture
# in the same directory. Returns "ansic", "cpp", "objc" or "mix"
my $filename = shift;
chomp($filename);
my $dirname = $filename;
$dirname =~ s/\/[^\/]*$//;
my $saw_ansic_in_dir = 0;
my $saw_pc_in_dir = 0; # ".pc" may mean Oracle C.
my $saw_pcc_in_dir = 0; # ".pc" may mean Oracle C++.
my $saw_cpp_in_dir = 0;
my $saw_objc_in_dir = 0;
opendir(DIR, $dirname) || die "can't opendir $dirname";
while (defined($_ = readdir(DIR))) {
chomp;
next if (!$_);
if (m/\.(cpp|C|cxx|cc)$/ && -f "$dirname/$_") {$saw_cpp_in_dir = 1;}
if (m/\.c$/ && -f "$dirname/$_") {$saw_ansic_in_dir = 1;}
if (m/\.pc$/ && -f "$dirname/$_") {$saw_pc_in_dir = 1;}
if (m/\.pcc$/ && -f "$dirname/$_") {$saw_pcc_in_dir = 1;}
if (m/\.m$/ && -f "$dirname/$_" && &really_is_objc($dirname . "/" . $_))
{$saw_objc_in_dir = 1;}
if (($saw_ansic_in_dir + $saw_cpp_in_dir + $saw_objc_in_dir) > 1) {
closedir(DIR);
return "mix";
}
}
# Done searching; we saw at most one type.
if ($saw_ansic_in_dir) {return "c";}
elsif ($saw_cpp_in_dir) {return "cpp";}
elsif ($saw_objc_in_dir) {return "objc";}
elsif ($saw_pc_in_dir && (!$saw_pcc_in_dir)) {return "c";} # Guess "C".
elsif ($saw_pcc_in_dir && (!$saw_pc_in_dir)) {return "cpp";} # Guess "C".
else {return "mix";} # We didn't see anything... so let's say "mix".
}
sub was_generated_automatically() {
# Determine if the file was generated automatically.
# Use a simple heuristic: check if first few lines have phrases like
# "generated automatically", "automatically generated", "Generated by",
# or "do not edit" as the first
# words in the line (after possible comment markers and spaces).
my $filename = shift;
if ($autogen_okay) {return 0;};
chomp($filename);
reopen($filename);
$i = 15; # Look at first 15 lines.
while (defined($_ = <CODE_FILE>)) {
if (m/^[\s#\/\*;\-\%]*generated automatically/i ||
m/^[\s#\/\*;\-\%]*automatically generated/i ||
m/^[\s#\/\*;\-\%]*generated by /i || # libtool uses this.
m/^[\s#\/\*;\-\%]*a lexical scanner generated by flex/i ||
m/^[\s#\/\*;\-\%]*this is a generated file/i || # TeTex uses this.
m/^[\s#\/\*;\-\%]*generated with the.*utility/i || # TeTex uses this.
m/^[\s#\/\*;\-\%]*do not edit/i) {
return 1;
}
$i--;
last if $i <= 0;
}
return 0;
}
# Previous files added, indexed by digest:
%previous_files = ();
$cached_digest = "";
$cached_digest_filename = "";
$digest_method = undef;
sub compute_digest_given_method {
my $filename = shift;
my $method = shift;
my $result;
if ($method eq "md5sum") {
open(FH, "-|", "md5sum", $filename) or return undef;
$result = <FH>;
close FH;
return undef if ! defined($result);
chomp($result);
$result =~ s/^\s*//; # Not needed for GNU Textutils.
$result =~ s/[^a-fA-F0-9].*//; # Strip away end.
} elsif ($method eq "md5") {
open(FH, "-|", "md5", $filename) or return undef;
$result = <FH>;
close FH;
return undef if ! defined($result);
chomp($result);
$result =~ s/^.* //; # Strip away beginning.
} elsif ($method eq "openssl") {
open(FH, "-|", "openssl", "dgst", "-md5", $filename) or return undef;
$result = <FH>;
close FH;
return undef if ! defined($result);
chomp($result);
$result =~ s/^.* //; # Strip away beginning.
} else {
# "Can't happen"
die "Unknown method";
}
return $result;
}
sub compute_digest {
my $filename = shift;
my $result;
if (defined($digest_method)) {
$result = compute_digest_given_method($filename, $digest_method);
} else {
# Try each method in turn until one works.
# There doesn't seem to be a way in perl to disable an error message
# display if the command is missing, which is annoying. However, the
# program is more robust if we check for the command each time we run.
print STDERR "Finding a working MD5 command....\n";
foreach $m ("md5sum", "md5", "openssl") {
$result = compute_digest_given_method($filename, $m);
if (defined($result)) {
$digest_method = $m;
last;
}
}
if (!defined($digest_method)) {
die "Failure - could not find a working md5 program using $filename.";
}
print STDERR "Found a working MD5 command.\n";
}
return $result;
}
sub get_digest {
my $filename = shift;
my $result;
# First, check the cache -- did we just compute this?
if ($filename eq $cached_digest_filename) {
return $cached_digest; # We did, so here's what it was.
}
$result = compute_digest($filename);
# Store in most-recently-used cache.
$cached_digest = $result;
$cached_digest_filename = $filename;
return $result;
}
sub already_added {
# returns the first file's name with the same contents,
# else returns the empty string.
my $filename = shift;
my $digest = &get_digest($filename);
if ($previous_files{$digest}) {
return $previous_files{$digest};
} else {
return "";
}
}
sub close_lang_lists {
my $lang;
my $file;
while (($lang, $file) = each(%lang_list_files)) {
$file->close(); # Ignore any errors on close, there's little we can do.
}
%lang_list_files = ();
}
sub force_record_file_type {
my ($filename, $type) = @_;
if (!$type) {die "ERROR! File $filename, type $file_type\n";}
if ($type eq "c") {$type = "ansic";};
if (!defined($lang_list_files{$type})) {
$lang_list_files{$type} = new FileHandle("${dir}/${type}_list.dat", "w") ||
die "Could not open ${dir}/${type}_list.dat";
}
$lang_list_files{$type}->printf("%s\n", $filename);
}
sub record_file_type {
my ($filename, $type) = @_;
# First check if the file should be auto, dup, or zero - and add there
# if so. Otherwise, add to record of 'type'.
my $first_filename;
if (-z $filename) {
force_record_file_type($filename, "zero");
return;
}
if (&was_generated_automatically($filename)) {
force_record_file_type($filename, "auto");
return;
}
unless (($duplicates_okay) || ($type eq "not") || ($type eq "unknown")) {
$first_filename = &already_added($filename);
if ($first_filename) {
print "Note: $filename dups $first_filename\n" if $noisy;
force_record_file_type("$filename dups $first_filename", "dup");
$dup_count++;
return;
} else { # This isn't a duplicate - record that info, as needed.
my $digest = &get_digest($filename);
$previous_files{$digest} = $filename;
if ($duplistfile) {
print DUPLIST "$digest $filename\n";
}
}
}
force_record_file_type($filename, $type);
}
sub file_type_from_contents() {
# Determine if file type is a scripting language, and if so, return it.
# Returns its type as a string, or the empty string if it's undetermined.
my $filename = shift;
my $command;
chomp($filename);
reopen($filename);
# Don't do $firstline = <CODE_FILE> here because the file may be binary;
# instead, read in a fixed number of bytes:
read CODE_FILE, $firstline, 200;
return "" if (!$_);
chomp($firstline);
if (!$_) {return "";}
if (!$firstline) {return "";}
# Handle weirdness: If there's a ".cpp" file beginning with .\"
# then it clearly isn't C/C++... it's a man page. People who create
# and distribute man pages with such filename extensions should have
# a fingernail removed, slowly :-).
if (($firstline =~ m@^[,.]\\"@) &&
$filename =~ m@\.(c|cpp|C|cxx|cc)$@) {return "not";}
if (!($firstline =~ m@^#!@)) {return "";} # No script indicator here.
# studying $firstline doesn't speed things up, unfortunately.
# I once used a pattern that only acknowledged very specific directories,
# but I found that many test cases use unusual script locations
# (to ensure that they're invoking the correct program they're testing).
# Thus, we depend on the program being named with postfixed whitespace,
# and either begin named by itself or with a series of lowercase
# directories ending in "/".
# I developed these patterns by starting with patterns that appeared
# correct, and then examined the output (esp. warning messages) to see
# what I'd missed.
$command = "";
# Strip out any calls to sudo
if ($firstline =~ m@^#!\s*/(usr/)?bin/sudo\s+(/.*)@) {
$firstline = "#!" . $2;
}
if ($firstline =~ m@^#!\s*/(usr/)?bin/env\s+([a-zA-Z0-9\._]+)(\s|\Z)@i) {
$command = $2;
} elsif ($firstline =~ m@^#!\s*([a-zA-Z0-9\/\.]+\/)?([a-zA-Z0-9\._]+)(\s|\Z)@) {
$command = $2;
}
if ( ($command =~ m/^(bash|ksh|zsh|pdksh|sh)[0-9\.]*(\.exe)?$/i) ||
($firstline =~
m~^#!\s*\@_?(SCRIPT_)?(PATH_)?(BA|K)?SH(ELL)?(\d+)?\@?(\s|\Z)~)) {
# Note: wish(1) uses a funny trick; see wish(1) for more info.
# The following code detects this unusual wish convention.
if ($firstline =~ m@exec wish(\s|\Z)@i) {
return "tcl"; # return the type for wish.
}
# Otherwise, it's shell.
return "sh";
}
if ( ($command =~ m/^(t?csh\d*)[0-9\.]*(\.exe)?$/i) ||
($firstline =~ m@^#!\s*xCSH_PATHx(\s|\Z)@)) {
return "csh";
}
if ( ($command =~ m/^(mini)?perl[0-9\.]*(\.exe)?$/i) ||
($command =~ m/^speedycgi[0-9\.]*(\.exe)?$/i) ||
($firstline =~ m~^#!\s*\@_?(PATH_)?PERL\d*(PROG)?\@(\s|\Z)~) ||
($firstline =~ m~^#!\s*xPERL_PATHx(\s|\Z)~)) {
return "perl";
}
if ($command =~ m/^python[0-9\.]*(\.exe)?$/i) {
return "python";
}
if ($command =~ m/^ruby[0-9\.]*(\.exe)?$/i) {
return "ruby";
}
if ($command =~ m/^pig[0-9-\.]*/) {
return "pig";
}
if ($command =~ m/^(tcl|tclsh|bltwish|wish|wishx|WISH)[0-9\.]*(\.exe)?$/i) {
return "tcl";
}
if ($command =~ m/^expectk?[0-9\.]*(\.exe)?$/i) { return "exp"; }
if ($command =~ m/^[ng]?awk[0-9\.]*(\.exe)?$/i) { return "awk"; }
if ($command =~ m/^sed$/i) { return "sed"; }
if ($command =~ m/^guile[0-9\.]*$/i) { return "lisp"; }
if ($firstline =~ m@^#!.*make\b@i) { # We'll claim that #! make is a makefile.
return "makefile";
}
if ($firstline =~ m@^#!\s*\.(\s|\Z)@) { # Lonely period.
return ""; # Ignore the first line, it's not helping.
}
if ($firstline =~ m@^#!\s*\Z@) { # Empty line.
return ""; # Ignore the first line, it's not helping.
}
if ($firstline =~ m@^#!\s*/dev/null@) { # /dev/null is the script?!?
return ""; # Ignore nonsense ("/dev/null").
}
if ($firstline =~ m@^#!\s*/unix(\s|Z)@) {
return ""; # Ignore nonsense ("/unix").
}
if (($filename =~ m@\.pl$@) || ($filename =~ m@\.pm$@)) {
return ""; # Don't warn about files that will be ID'd as perl files.
}
if (($filename =~ m@\.sh$@)) {
return ""; # Don't warn about files that will be ID'd as sh files.
}
if ($firstline =~ m@^#!\s*\S@) {
$firstline =~ s/\n.*//s; # Delete everything after first line.
$warning_from_first_line = "WARNING! File $filename has unknown start: $firstline";
return "";
}
return "";
}