Script that generates a script that will convert oddball variable names to K&R
authormarkw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Wed, 14 Mar 2001 21:04:07 +0000 (21:04 +0000)
committermarkw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277>
Wed, 14 Mar 2001 21:04:07 +0000 (21:04 +0000)
style.

git-svn-id: svn://busybox.net/trunk/busybox@2080 69ca8d6d-28ef-0310-b511-8ec308f3f277

examples/mk2knr.pl [new file with mode: 0755]
scripts/mk2knr.pl [new file with mode: 0755]

diff --git a/examples/mk2knr.pl b/examples/mk2knr.pl
new file mode 100755 (executable)
index 0000000..29f94a1
--- /dev/null
@@ -0,0 +1,76 @@
+#!/usr/bin/perl -w
+#
+# @(#) mk2knr.pl - generates a perl script that converts lexemes to K&R-style
+#
+# How to use this script:
+#  - In the busybox directory type 'scripts/mk2knr.pl files-you-want-to-convert'
+#  - Review the 'convertme.pl' script generated and remove / edit any of the
+#    substitutions in there (please especially check for false positives)
+#  - Type './convertme.pl same-files-as-before'
+#  - Compile and see if it works
+#
+# BUGS: This script does not ignore strings inside comments or strings inside
+# quotes (it probably should).
+
+# set this to something else if you want
+$convertme = 'convertme.pl';
+
+# internal-use variables (don't touch)
+$convert = 0;
+%converted = ();
+
+
+# prepare the "convert me" file
+open(CM, ">$convertme") or die "convertme.pl $!";
+print CM "#!/usr/bin/perl -p -i\n\n";
+
+# process each file passed on the cmd line
+while (<>) {
+
+       # if the line says "getopt" in it anywhere, we don't want to muck with it
+       # because option lists tend to include strings like "cxtzvOf:" which get
+       # matched by the javaStyle / Hungarian and PascalStyle regexps below
+       next if /getopt/;
+
+       # tokenize the string into just the variables
+       while (/([a-zA-Z_][a-zA-Z0-9_]*)/g) {
+               $var = $1;
+
+               # ignore the word "BusyBox"
+               next if ($var =~ /BusyBox/);
+
+               # this checks for javaStyle or szHungarianNotation
+               $convert++ if ($var =~ /^[a-z]+[A-Z][a-z]+/);
+
+               # this checks for PascalStyle
+               $convert++ if ($var =~ /^[A-Z][a-z]+[A-Z][a-z]+/);
+
+               if ($convert) {
+                       $convert = 0;
+
+                       # skip ahead if we've already dealt with this one
+                       next if ($converted{$var});
+
+                       # record that we've dealt with this var
+                       $converted{$var} = 1;
+
+                       print CM "s/\\b$var\\b/"; # more to come in just a minute
+
+                       # change the first letter to lower-case
+                       $var = lcfirst($var);
+
+                       # put underscores before all remaining upper-case letters
+                       $var =~ s/([A-Z])/_$1/g;
+
+                       # now change the remaining characters to lower-case
+                       $var = lc($var);
+
+                       print CM "$var/g;\n";
+               }
+       }
+}
+
+# tidy up and make the $convertme script executable
+close(CM);
+chmod 0755, $convertme;
+
diff --git a/scripts/mk2knr.pl b/scripts/mk2knr.pl
new file mode 100755 (executable)
index 0000000..29f94a1
--- /dev/null
@@ -0,0 +1,76 @@
+#!/usr/bin/perl -w
+#
+# @(#) mk2knr.pl - generates a perl script that converts lexemes to K&R-style
+#
+# How to use this script:
+#  - In the busybox directory type 'scripts/mk2knr.pl files-you-want-to-convert'
+#  - Review the 'convertme.pl' script generated and remove / edit any of the
+#    substitutions in there (please especially check for false positives)
+#  - Type './convertme.pl same-files-as-before'
+#  - Compile and see if it works
+#
+# BUGS: This script does not ignore strings inside comments or strings inside
+# quotes (it probably should).
+
+# set this to something else if you want
+$convertme = 'convertme.pl';
+
+# internal-use variables (don't touch)
+$convert = 0;
+%converted = ();
+
+
+# prepare the "convert me" file
+open(CM, ">$convertme") or die "convertme.pl $!";
+print CM "#!/usr/bin/perl -p -i\n\n";
+
+# process each file passed on the cmd line
+while (<>) {
+
+       # if the line says "getopt" in it anywhere, we don't want to muck with it
+       # because option lists tend to include strings like "cxtzvOf:" which get
+       # matched by the javaStyle / Hungarian and PascalStyle regexps below
+       next if /getopt/;
+
+       # tokenize the string into just the variables
+       while (/([a-zA-Z_][a-zA-Z0-9_]*)/g) {
+               $var = $1;
+
+               # ignore the word "BusyBox"
+               next if ($var =~ /BusyBox/);
+
+               # this checks for javaStyle or szHungarianNotation
+               $convert++ if ($var =~ /^[a-z]+[A-Z][a-z]+/);
+
+               # this checks for PascalStyle
+               $convert++ if ($var =~ /^[A-Z][a-z]+[A-Z][a-z]+/);
+
+               if ($convert) {
+                       $convert = 0;
+
+                       # skip ahead if we've already dealt with this one
+                       next if ($converted{$var});
+
+                       # record that we've dealt with this var
+                       $converted{$var} = 1;
+
+                       print CM "s/\\b$var\\b/"; # more to come in just a minute
+
+                       # change the first letter to lower-case
+                       $var = lcfirst($var);
+
+                       # put underscores before all remaining upper-case letters
+                       $var =~ s/([A-Z])/_$1/g;
+
+                       # now change the remaining characters to lower-case
+                       $var = lc($var);
+
+                       print CM "$var/g;\n";
+               }
+       }
+}
+
+# tidy up and make the $convertme script executable
+close(CM);
+chmod 0755, $convertme;
+