helper.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use File::Find 'find';
  6. use File::Basename 'basename';
  7. use File::Glob 'bsd_glob';
  8. sub read_file {
  9. my $f = shift;
  10. open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
  11. binmode $fh;
  12. return do { local $/; <$fh> };
  13. }
  14. sub write_file {
  15. my ($f, $data) = @_;
  16. die "FATAL: write_file() no data" unless defined $data;
  17. open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
  18. binmode $fh;
  19. print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
  20. close $fh or die "FATAL: write_file() cannot close '$f': $!";
  21. return;
  22. }
  23. sub check_source {
  24. my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl"));
  25. find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src tests demos/);
  26. my $fails = 0;
  27. for my $file (sort @all_files) {
  28. next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i;
  29. my $troubles = {};
  30. my $lineno = 1;
  31. my $content = read_file($file);
  32. push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
  33. for my $l (split /\n/, $content) {
  34. push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
  35. push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
  36. push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
  37. push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
  38. push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
  39. # in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
  40. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
  41. push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmalloc\s*\(/;
  42. push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\brealloc\s*\(/;
  43. push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bcalloc\s*\(/;
  44. push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /[^>.]\bfree\s*\(/;
  45. push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemset\s*\(/;
  46. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
  47. push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemmove\s*\(/;
  48. push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcmp\s*\(/;
  49. push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcmp\s*\(/;
  50. push @{$troubles->{unwanted_strcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcpy\s*\(/;
  51. push @{$troubles->{unwanted_strlen}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrlen\s*\(/;
  52. push @{$troubles->{unwanted_strncpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrncpy\s*\(/;
  53. push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bclock\s*\(/;
  54. push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bqsort\s*\(/;
  55. push @{$troubles->{sizeof_no_brackets}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bsizeof\s*[^\(]/;
  56. if ($file =~ m|src/.*\.c$| &&
  57. $file !~ m|src/ciphers/.*\.c$| &&
  58. $file !~ m|src/math/.+_desc.c$| &&
  59. $file !~ m|src/pk/ec25519/tweetnacl.c$| &&
  60. $file !~ m|src/stream/sober128/sober128_stream.c$| &&
  61. $l =~ /^static(\s+[a-zA-Z0-9_]+)+\s++([^s][a-zA-Z0-9_]+)\s*\(/) {
  62. push @{$troubles->{staticfunc_name}}, "$2";
  63. }
  64. if ($file =~ m|src/.*\.[ch]$| && $l =~ /^\s*#\s*define\s+(_[A-Z_][a-zA-Z0-9_]*)\b/) {
  65. my $n = $1;
  66. push @{$troubles->{invalid_macro_name}}, "$lineno($n)"
  67. unless ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '__has_builtin') ||
  68. ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '_WIN32_WINNT') ||
  69. ($file eq 'src/prngs/rng_get_bytes.c' && $n eq '_WIN32_WINNT');
  70. }
  71. $lineno++;
  72. }
  73. for my $k (sort keys %$troubles) {
  74. warn "[$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
  75. $fails++;
  76. }
  77. }
  78. warn( $fails > 0 ? "check-source: FAIL $fails\n" : "check-source: PASS\n" );
  79. return $fails;
  80. }
  81. sub check_defines {
  82. my $fails = 0;
  83. my $cust_h = read_file("src/headers/tomcrypt_custom.h");
  84. my $cryp_c = read_file("src/misc/crypt/crypt.c");
  85. $cust_h =~ s|/\*.*?\*/||sg; # remove comments
  86. $cryp_c =~ s|/\*.*?\*/||sg; # remove comments
  87. my %def = map { $_ => 1 } map { my $x = $_; $x =~ s/^\s*#define\s+(LTC_\S+).*$/$1/; $x } grep { /^\s*#define\s+LTC_\S+/ } split /\n/, $cust_h;
  88. for my $d (sort keys %def) {
  89. next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
  90. warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q$d\E/;
  91. }
  92. warn( $fails > 0 ? "check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
  93. return $fails;
  94. }
  95. sub check_descriptor {
  96. my $which = shift;
  97. my $what = shift;
  98. my @src;
  99. my @descriptors;
  100. find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
  101. for my $f (@src) {
  102. my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
  103. push @descriptors, @n if @n;
  104. }
  105. my $fails = 0;
  106. for my $d (@descriptors) {
  107. for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
  108. my $txt = read_file($f);
  109. warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
  110. }
  111. }
  112. for my $d (@descriptors) {
  113. for my $f ("./tests/test.c") {
  114. my $txt = read_file($f);
  115. warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
  116. }
  117. }
  118. my $name = sprintf("%-17s", "check-${which}:");
  119. warn( $fails > 0 ? "${name}FAIL $fails\n" : "${name}PASS\n" );
  120. return $fails;
  121. }
  122. sub check_descriptors {
  123. my $fails = 0;
  124. $fails = $fails + check_descriptor("ciphers", "cipher");
  125. $fails = $fails + check_descriptor("hashes", "hash");
  126. $fails = $fails + check_descriptor("prngs", "prng");
  127. return $fails;
  128. }
  129. sub check_comments {
  130. my $fails = 0;
  131. my $first_comment = <<'MARKER';
  132. /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
  133. /* SPDX-License-Identifier: Unlicense */
  134. MARKER
  135. my @all_files;
  136. find({ wanted=> sub { push @all_files, $_ if $_ =~ /\.(c|h)$/ }, no_chdir=>1 }, 'demos', 'src', 'tests');
  137. for my $f (@all_files) {
  138. my $txt = read_file($f);
  139. if ($txt !~ /^\Q$first_comment\E/s) {
  140. warn "[first_comment] $f\n";
  141. $fails++;
  142. }
  143. }
  144. warn( $fails > 0 ? "check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
  145. return $fails;
  146. }
  147. sub prepare_variable {
  148. my ($varname, @list) = @_;
  149. my $output = "$varname=";
  150. my $len = length($output);
  151. foreach my $obj (sort @list) {
  152. $len = $len + length $obj;
  153. $obj =~ s/\*/\$/;
  154. if ($len > 100) {
  155. $output .= "\\\n";
  156. $len = length $obj;
  157. }
  158. $output .= $obj . ' ';
  159. }
  160. $output =~ s/ $//;
  161. return $output;
  162. }
  163. sub prepare_msvc_files_xml {
  164. my ($all, $exclude_re, $targets) = @_;
  165. my $last = [];
  166. my $depth = 2;
  167. # sort files in the same order as visual studio (ugly, I know)
  168. my @parts = ();
  169. for my $orig (@$all) {
  170. my $p = $orig;
  171. $p =~ s|/|/~|g;
  172. $p =~ s|/~([^/]+)$|/$1|g;
  173. # now we have: 'src/pk/rsa/rsa_verify_hash.c' > 'src/~pk/~rsa/rsa_verify_hash.c'
  174. my @l = map { sprintf "% -99s", $_ } split /\//, $p;
  175. push @parts, [ $orig, join(':', @l) ];
  176. }
  177. my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
  178. my $files = "<Files>\r\n";
  179. for my $full (@sorted) {
  180. my @items = split /\//, $full; # split by '/'
  181. $full =~ s|/|\\|g; # replace '/' bt '\'
  182. shift @items; # drop first one (src)
  183. pop @items; # drop last one (filename.ext)
  184. my $current = \@items;
  185. if (join(':', @$current) ne join(':', @$last)) {
  186. my $common = 0;
  187. $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
  188. my $back = @$last - $common;
  189. if ($back > 0) {
  190. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (1..$back);
  191. }
  192. my $fwd = [ @$current ]; splice(@$fwd, 0, $common);
  193. for my $i (0..scalar(@$fwd) - 1) {
  194. $files .= ("\t" x $depth) . "<Filter\r\n";
  195. $files .= ("\t" x $depth) . "\tName=\"$fwd->[$i]\"\r\n";
  196. $files .= ("\t" x $depth) . "\t>\r\n";
  197. $depth++;
  198. }
  199. $last = $current;
  200. }
  201. $files .= ("\t" x $depth) . "<File\r\n";
  202. $files .= ("\t" x $depth) . "\tRelativePath=\"$full\"\r\n";
  203. $files .= ("\t" x $depth) . "\t>\r\n";
  204. if ($full =~ $exclude_re) {
  205. for (@$targets) {
  206. $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
  207. $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
  208. $files .= ("\t" x $depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
  209. $files .= ("\t" x $depth) . "\t\t>\r\n";
  210. $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
  211. $files .= ("\t" x $depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
  212. $files .= ("\t" x $depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
  213. $files .= ("\t" x $depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
  214. $files .= ("\t" x $depth) . "\t\t/>\r\n";
  215. $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
  216. }
  217. }
  218. ########### aes_enc "hack" disabled - discussion: https://github.com/libtom/libtomcrypt/pull/158
  219. # if ($full eq 'src\ciphers\aes\aes.c') { #hack
  220. # my %cmd = (
  221. # 'Debug|Win32' => [ 'Debug/aes.obj;Debug/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/aes_enc.obj&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
  222. # 'Release|Win32' => [ 'Release/aes.obj;Release/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/aes_enc.obj&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
  223. # );
  224. # for (@$targets) {
  225. # next unless $cmd{$_};
  226. # $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
  227. # $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
  228. # $files .= ("\t" x $depth) . "\t\t>\r\n";
  229. # $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
  230. # $files .= ("\t" x $depth) . "\t\t\tName=\"VCCustomBuildTool\"\r\n";
  231. # $files .= ("\t" x $depth) . "\t\t\tCommandLine=\"$cmd{$_}[1]\"\r\n";
  232. # $files .= ("\t" x $depth) . "\t\t\tOutputs=\"$cmd{$_}[0]\"\r\n";
  233. # $files .= ("\t" x $depth) . "\t\t/>\r\n";
  234. # $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
  235. # }
  236. # }
  237. $files .= ("\t" x $depth) . "</File>\r\n";
  238. }
  239. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (@$last);
  240. $files .= "\t</Files>";
  241. return $files;
  242. }
  243. sub patch_file {
  244. my ($content, @variables) = @_;
  245. for my $v (@variables) {
  246. if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
  247. my $name = $1;
  248. $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
  249. }
  250. else {
  251. die "patch_file failed: " . substr($v, 0, 30) . "..";
  252. }
  253. }
  254. return $content;
  255. }
  256. sub version_from_tomcrypt_h {
  257. my $h = read_file(shift);
  258. if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(\S*)"/s) {
  259. return "VERSION_PC=$1.$2.$3", "VERSION_MAJOR=1", "VERSION_MINOR=0", "VERSION_PATCH=1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
  260. }
  261. else {
  262. die "#define SCRYPT not found in tomcrypt.h";
  263. }
  264. }
  265. sub make_sources_cmake {
  266. my ($list, $pub_headers) = @_;
  267. my $output = "set(SOURCES\n";
  268. foreach my $obj (sort @$list) {
  269. $output .= $obj . "\n";
  270. }
  271. $output .= ")\n\n";
  272. if ($pub_headers eq "") {
  273. return $output;
  274. }
  275. $output .= "set(PUBLIC_HEADERS\n";
  276. foreach my $obj (sort @$pub_headers) {
  277. $output .= $obj . "\n";
  278. }
  279. $output .= ")\n\nset(PRIVATE_HEADERS src/headers/tomcrypt_private.h)\n";
  280. $output .= "set_property(GLOBAL PROPERTY PUBLIC_HEADERS \$\{PUBLIC_HEADERS\}\)\n\n";
  281. return $output;
  282. }
  283. sub process_makefiles {
  284. my $write = shift;
  285. my $changed_count = 0;
  286. my @c = ();
  287. find({ no_chdir => 1, wanted => sub { push @c, $_ if -f $_ && $_ =~ /\.c$/ && $_ !~ /tab.c$/ } }, 'src');
  288. my @h = ();
  289. find({ no_chdir => 1, wanted => sub { push @h, $_ if -f $_ && $_ =~ /\.h$/ && $_ !~ /dh_static.h$/ && $_ !~ /tomcrypt_private.h$/ } }, 'src');
  290. my @all = ();
  291. find({ no_chdir => 1, wanted => sub { push @all, $_ if -f $_ && $_ =~ /\.(c|h)$/ } }, 'src');
  292. my @t = qw();
  293. find({ no_chdir => 1, wanted => sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
  294. my @o = sort ('src/ciphers/aes/aes_enc.o', 'src/ciphers/aes/aes_enc_desc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
  295. my $var_o = prepare_variable("OBJECTS", @o);
  296. my $var_h = prepare_variable("HEADERS_PUB", (sort @h));
  297. (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
  298. my @t_srcs = sort (map { my $x = $_; $x =~ s/^tests\///; $x } @t);
  299. my $var_to = prepare_variable("TOBJECTS", sort map { my $x = $_; $x =~ s/\.c$/.o/; $x } @t);
  300. (my $var_tobj = $var_to) =~ s/\.o\b/.obj/sg;
  301. my @ver_version = version_from_tomcrypt_h("src/headers/tomcrypt.h");
  302. # update MSVC project files
  303. my $msvc_files = prepare_msvc_files_xml(\@all, qr/tab\.c$/, ['Debug|Win32', 'Release|Win32', 'Debug|x64', 'Release|x64']);
  304. for my $m (qw/libtomcrypt_VS2008.vcproj/) {
  305. my $old = read_file($m);
  306. my $new = $old;
  307. $new =~ s|<Files>.*</Files>|$msvc_files|s;
  308. if ($old ne $new) {
  309. write_file($m, $new) if $write;
  310. warn "changed: $m\n";
  311. $changed_count++;
  312. }
  313. }
  314. # update OBJECTS + HEADERS in makefile*
  315. for my $m (qw/ makefile makefile.shared makefile.unix makefile.mingw makefile.msvc makefile_include.mk doc\/Doxyfile sources.cmake tests\/sources.cmake /) {
  316. my $old = read_file($m);
  317. my $new = $m eq 'makefile.msvc' ? patch_file($old, $var_obj, $var_h, $var_tobj, @ver_version)
  318. : $m eq 'sources.cmake' ? make_sources_cmake(\@all, \@h)
  319. : $m eq 'tests/sources.cmake' ? make_sources_cmake(\@t_srcs, "")
  320. : patch_file($old, $var_o, $var_h, $var_to, @ver_version);
  321. if ($old ne $new) {
  322. write_file($m, $new) if $write;
  323. warn "changed: $m\n";
  324. $changed_count++;
  325. }
  326. }
  327. if ($write) {
  328. return 0; # no failures
  329. }
  330. else {
  331. warn( $changed_count > 0 ? "check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
  332. return $changed_count;
  333. }
  334. }
  335. sub die_usage {
  336. die <<"MARKER";
  337. usage: $0 -s OR $0 --check-source
  338. $0 -c OR $0 --check-descriptors
  339. $0 -d OR $0 --check-defines
  340. $0 -o OR $0 --check-comments
  341. $0 -m OR $0 --check-makefiles
  342. $0 -a OR $0 --check-all
  343. $0 -u OR $0 --update-makefiles
  344. $0 --fixupind crypt.ind
  345. MARKER
  346. }
  347. GetOptions( "s|check-source" => \my $check_source,
  348. "c|check-descriptors" => \my $check_descriptors,
  349. "d|check-defines" => \my $check_defines,
  350. "o|check-comments" => \my $check_comments,
  351. "m|check-makefiles" => \my $check_makefiles,
  352. "a|check-all" => \my $check_all,
  353. "u|update-makefiles" => \my $update_makefiles,
  354. "f|fixupind=s" => \my $fixupind,
  355. "h|help" => \my $help
  356. ) or die_usage;
  357. if ($fixupind) {
  358. my $txt = read_file($fixupind);
  359. $txt =~ s/^([^\n]*\n)/$1\n\\addcontentsline{toc}{chapter}{Index}\n/s;
  360. write_file($fixupind, $txt);
  361. exit 0;
  362. }
  363. my $failure;
  364. $failure ||= check_source() if $check_all || $check_source;
  365. $failure ||= check_defines() if $check_all || $check_defines;
  366. $failure ||= check_descriptors() if $check_all || $check_descriptors;
  367. $failure ||= check_comments() if $check_all || $check_comments;
  368. $failure ||= process_makefiles(0) if $check_all || $check_makefiles;
  369. $failure ||= process_makefiles(1) if $update_makefiles;
  370. die_usage unless defined $failure;
  371. exit $failure ? 1 : 0;