Fixed option parsing. Grep would continue grepping, even when given a bad /
[oweals/busybox.git] / examples / mk2knr.pl
1 #!/usr/bin/perl -w
2 #
3 # @(#) mk2knr.pl - generates a perl script that converts lexemes to K&R-style
4 #
5 # How to use this script:
6 #  - In the busybox directory type 'scripts/mk2knr.pl files-you-want-to-convert'
7 #  - Review the 'convertme.pl' script generated and remove / edit any of the
8 #    substitutions in there (please especially check for false positives)
9 #  - Type './convertme.pl same-files-as-before'
10 #  - Compile and see if it works
11 #
12 # BUGS: This script does not ignore strings inside comments or strings inside
13 # quotes (it probably should).
14
15 # set this to something else if you want
16 $convertme = 'convertme.pl';
17
18 # internal-use variables (don't touch)
19 $convert = 0;
20 %converted = ();
21
22
23 # prepare the "convert me" file
24 open(CM, ">$convertme") or die "convertme.pl $!";
25 print CM "#!/usr/bin/perl -p -i\n\n";
26
27 # process each file passed on the cmd line
28 while (<>) {
29
30         # if the line says "getopt" in it anywhere, we don't want to muck with it
31         # because option lists tend to include strings like "cxtzvOf:" which get
32         # matched by the javaStyle / Hungarian and PascalStyle regexps below
33         next if /getopt/;
34
35         # tokenize the string into just the variables
36         while (/([a-zA-Z_][a-zA-Z0-9_]*)/g) {
37                 $var = $1;
38
39                 # ignore the word "BusyBox"
40                 next if ($var =~ /BusyBox/);
41
42                 # this checks for javaStyle or szHungarianNotation
43                 $convert++ if ($var =~ /^[a-z]+[A-Z][a-z]+/);
44
45                 # this checks for PascalStyle
46                 $convert++ if ($var =~ /^[A-Z][a-z]+[A-Z][a-z]+/);
47
48                 if ($convert) {
49                         $convert = 0;
50
51                         # skip ahead if we've already dealt with this one
52                         next if ($converted{$var});
53
54                         # record that we've dealt with this var
55                         $converted{$var} = 1;
56
57                         print CM "s/\\b$var\\b/"; # more to come in just a minute
58
59                         # change the first letter to lower-case
60                         $var = lcfirst($var);
61
62                         # put underscores before all remaining upper-case letters
63                         $var =~ s/([A-Z])/_$1/g;
64
65                         # now change the remaining characters to lower-case
66                         $var = lc($var);
67
68                         print CM "$var/g;\n";
69                 }
70         }
71 }
72
73 # tidy up and make the $convertme script executable
74 close(CM);
75 chmod 0755, $convertme;
76