3
0

mk2knr.pl 2.4 KB

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