zlib2ansi 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/perl
  2. # Transform K&R C function definitions into ANSI equivalent.
  3. #
  4. # Author: Paul Marquess
  5. # Version: 1.0
  6. # Date: 3 October 2006
  7. # TODO
  8. #
  9. # Asumes no function pointer parameters. unless they are typedefed.
  10. # Assumes no literal strings that look like function definitions
  11. # Assumes functions start at the beginning of a line
  12. use strict;
  13. use warnings;
  14. local $/;
  15. $_ = <>;
  16. my $sp = qr{ \s* (?: /\* .*? \*/ )? \s* }x; # assume no nested comments
  17. my $d1 = qr{ $sp (?: [\w\*\s]+ $sp)* $sp \w+ $sp [\[\]\s]* $sp }x ;
  18. my $decl = qr{ $sp (?: \w+ $sp )+ $d1 }xo ;
  19. my $dList = qr{ $sp $decl (?: $sp , $d1 )* $sp ; $sp }xo ;
  20. while (s/^
  21. ( # Start $1
  22. ( # Start $2
  23. .*? # Minimal eat content
  24. ( ^ \w [\w\s\*]+ ) # $3 -- function name
  25. \s* # optional whitespace
  26. ) # $2 - Matched up to before parameter list
  27. \( \s* # Literal "(" + optional whitespace
  28. ( [^\)]+ ) # $4 - one or more anythings except ")"
  29. \s* \) # optional whitespace surrounding a Literal ")"
  30. ( (?: $dList )+ ) # $5
  31. $sp ^ { # literal "{" at start of line
  32. ) # Remember to $1
  33. //xsom
  34. )
  35. {
  36. my $all = $1 ;
  37. my $prefix = $2;
  38. my $param_list = $4 ;
  39. my $params = $5;
  40. StripComments($params);
  41. StripComments($param_list);
  42. $param_list =~ s/^\s+//;
  43. $param_list =~ s/\s+$//;
  44. my $i = 0 ;
  45. my %pList = map { $_ => $i++ }
  46. split /\s*,\s*/, $param_list;
  47. my $pMatch = '(\b' . join('|', keys %pList) . '\b)\W*$' ;
  48. my @params = split /\s*;\s*/, $params;
  49. my @outParams = ();
  50. foreach my $p (@params)
  51. {
  52. if ($p =~ /,/)
  53. {
  54. my @bits = split /\s*,\s*/, $p;
  55. my $first = shift @bits;
  56. $first =~ s/^\s*//;
  57. push @outParams, $first;
  58. $first =~ /^(\w+\s*)/;
  59. my $type = $1 ;
  60. push @outParams, map { $type . $_ } @bits;
  61. }
  62. else
  63. {
  64. $p =~ s/^\s+//;
  65. push @outParams, $p;
  66. }
  67. }
  68. my %tmp = map { /$pMatch/; $_ => $pList{$1} }
  69. @outParams ;
  70. @outParams = map { " $_" }
  71. sort { $tmp{$a} <=> $tmp{$b} }
  72. @outParams ;
  73. print $prefix ;
  74. print "(\n" . join(",\n", @outParams) . ")\n";
  75. print "{" ;
  76. }
  77. # Output any trailing code.
  78. print ;
  79. exit 0;
  80. sub StripComments
  81. {
  82. no warnings;
  83. # Strip C & C++ coments
  84. # From the perlfaq
  85. $_[0] =~
  86. s{
  87. /\* ## Start of /* ... */ comment
  88. [^*]*\*+ ## Non-* followed by 1-or-more *'s
  89. (
  90. [^/*][^*]*\*+
  91. )* ## 0-or-more things which don't start with /
  92. ## but do end with '*'
  93. / ## End of /* ... */ comment
  94. | ## OR C++ Comment
  95. // ## Start of C++ comment //
  96. [^\n]* ## followed by 0-or-more non end of line characters
  97. | ## OR various things which aren't comments:
  98. (
  99. " ## Start of " ... " string
  100. (
  101. \\. ## Escaped char
  102. | ## OR
  103. [^"\\] ## Non "\
  104. )*
  105. " ## End of " ... " string
  106. | ## OR
  107. ' ## Start of ' ... ' string
  108. (
  109. \\. ## Escaped char
  110. | ## OR
  111. [^'\\] ## Non '\
  112. )*
  113. ' ## End of ' ... ' string
  114. | ## OR
  115. . ## Anything other char
  116. [^/"'\\]* ## Chars which doesn't start a comment, string or escape
  117. )
  118. }{$2}gxs;
  119. }