Initial public busybox upstream commit
[busybox4maemo] / 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 'examples/mk2knr.pl files-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 # if no files were specified, print usage
23 die "usage: $0 file.c | file.h\n" if scalar(@ARGV) == 0;
24
25 # prepare the "convert me" file
26 open(CM, ">$convertme") or die "convertme.pl $!";
27 print CM "#!/usr/bin/perl -p -i\n\n";
28
29 # process each file passed on the cmd line
30 while (<>) {
31
32         # if the line says "getopt" in it anywhere, we don't want to muck with it
33         # because option lists tend to include strings like "cxtzvOf:" which get
34         # matched by the "check for mixed case" regexps below
35         next if /getopt/;
36
37         # tokenize the string into just the variables
38         while (/([a-zA-Z_][a-zA-Z0-9_]*)/g) {
39                 $var = $1;
40
41                 # ignore the word "BusyBox"
42                 next if ($var =~ /BusyBox/);
43
44                 # this checks for javaStyle or szHungarianNotation
45                 $convert++ if ($var =~ /^[a-z]+[A-Z][a-z]+/);
46
47                 # this checks for PascalStyle
48                 $convert++ if ($var =~ /^[A-Z][a-z]+[A-Z][a-z]+/);
49
50                 # if we want to add more checks, we can add 'em here, but the above
51                 # checks catch "just enough" and not too much, so prolly not.
52
53                 if ($convert) {
54                         $convert = 0;
55
56                         # skip ahead if we've already dealt with this one
57                         next if ($converted{$var});
58
59                         # record that we've dealt with this var
60                         $converted{$var} = 1;
61
62                         print CM "s/\\b$var\\b/"; # more to come in just a minute
63
64                         # change the first letter to lower-case
65                         $var = lcfirst($var);
66
67                         # put underscores before all remaining upper-case letters
68                         $var =~ s/([A-Z])/_$1/g;
69
70                         # now change the remaining characters to lower-case
71                         $var = lc($var);
72
73                         print CM "$var/g;\n";
74                 }
75         }
76 }
77
78 # tidy up and make the $convertme script executable
79 close(CM);
80 chmod 0755, $convertme;
81
82 # print a helpful help message
83 print "Done. Scheduled name changes are in $convertme.\n";
84 print "Please review/modify it and then type ./$convertme to do the search & replace.\n";