home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume23 / rc / part01 / cfix.awk < prev    next >
Text File  |  1991-10-18  |  5KB  |  223 lines

  1. BEGIN                { Pending = 0; }
  2. #
  3. # Special cases.  There are too many, but....
  4. #
  5. $0 == "static int (*realgchar)(void);"    {
  6.                 print "static int (*realgchar)();";
  7.                 next;
  8.                 }
  9. $0 == "static void (*realugchar)(int);"    {
  10.                 print "static void (*realugchar)();";
  11.                 next;
  12.                 }
  13. $0 == "static void b_break(char **), b_cd(char **)," {
  14.                 print "static void b_break(), b_cd(),";
  15.                 next;
  16.                 }
  17. $0 == "\tb_echo(char **), b_eval(char **), b_exit(char **), b_limit(char **)," {
  18.                 print "b_echo(), b_eval(), b_exit(), b_limit(),";
  19.                 next;
  20.                 }
  21. $0 == "\tb_return(char **), b_shift(char **), b_umask(char **), b_wait(char **)," {
  22.                 print "b_return(), b_shift(), b_umask(), b_wait(),";
  23.                 next;
  24.                 }
  25. $0 == "\tb_whatis(char **);"    {
  26.                 print "b_whatis();";
  27.                 next;
  28.                 }
  29. $0 == "Node *treecpy(Node *s, void *(*alloc)(SIZE_T)) {" {
  30.                 print "Node *treecpy(s, alloc)";
  31.                 printf "Node *s;\nvoid *(*alloc)();\n{\n";
  32.                 next;
  33.                 }
  34. $0 == "\tvoid (*handler)(int);"    {
  35.                 print "\tvoid (*handler)();";
  36.                 next;
  37.                 }
  38. $0 == "\textern DIR *opendir(const char *);" {
  39.                 print "#if 0";
  40.                 print;
  41.                 next;
  42.                 }
  43. $0 == "\textern int closedir(DIR *);" {
  44.                 print;
  45.                 print "#endif";
  46.                 next;
  47.                 }
  48. $0 == "\textern int getopt(int, char **, char *);"    {
  49.                 print "\textern int getopt();";
  50.                 next;
  51.                 }
  52. $0 == "\tstatic void (*vectors[])(char *, List *, boolean) = {"    {
  53.                 print "\tstatic void (*vectors[])() = {";
  54.                 next;
  55.                 }
  56. $0 == "\t\tvoid (*handler)(int);"    {
  57.                 print "\t\tvoid (*handler)();";
  58.                 next;
  59.                 }
  60. $0 == "#include <stdarg.h>"    {
  61.                 print "#include <varargs.h>";
  62.                 next;
  63.                 }
  64. #
  65. # General cases.
  66. #
  67. /^static .*\);$/        {
  68.                 n = split($0, tmp1, "(");
  69.                 if (n != 2) {
  70.                     print | "sh -c 'cat >&2'";
  71.                     print "ERROR with left parens: ", n | "sh -c 'cat >&2'";
  72.                     next;
  73.                 }
  74.                 printf "%s();\n", tmp1[1];
  75.                 next;
  76.                 }
  77. /^extern .*\);$/        {
  78.                 n = split($0, tmp1, "(");
  79.                 if (n != 2) {
  80.                     print | "sh -c 'cat >&2'";
  81.                     print "ERROR with left parens: ", n | "sh -c 'cat >&2'";
  82.                     next;
  83.                 }
  84.                 printf "%s();\n", tmp1[1];
  85.                 next;
  86.                 }
  87. /^[a-zA-Z_][a-zA-Z_0-9]*:/    {
  88.                 print;
  89.                 next;
  90.                 }
  91. /^[^ \t#][^(]*\([^)]*\.\.\.\).*{/ {
  92.                 if (Pending != 0) {
  93.                     print "ERROR: Pending != 0 on function entry" | "sh -c 'cat >&2'";
  94.                     next;
  95.                 }
  96.                 n = split($0, tmp1, "(");
  97.                 if (n != 2) {
  98.                     print | "sh -c 'cat >&2'";
  99.                     print "ERROR with left parens: ", n | "sh -c 'cat >&2'";
  100.                     next;
  101.                 }
  102.                 n = split(tmp1[2], tmp2, ")");
  103.                 if (n != 2) {
  104.                     print | "sh -c 'cat >&2'";
  105.                     print "ERROR with right parens: ", n | "sh -c 'cat >&2'";
  106.                     next;
  107.                 }
  108.                 printf "%s(va_alist)\nva_dcl\n{\n", tmp1[1];
  109.                 nargs = split(tmp2[1], args, ",");
  110.                 for (i = 1; i <= nargs; i++) {
  111.                     if (args[i] == "...") {
  112.                         if (i == nargs) {
  113.                             Pending = 1;
  114.                             continue;
  115.                         }
  116.                         print "ERROR: ... isn't last argument", $0 | "sh -c 'cat >&2'";
  117.                         next;
  118.                     }
  119.                     decls[i] = args[i] ";";
  120.                     t = args[i];
  121.                     for (;;) {
  122.                         n = split(t, varname, " ");
  123.                         if (n > 1) {
  124.                             t = varname[n];
  125.                             continue;
  126.                         }
  127.                         n = split(t, varname, "*");
  128.                         if (n > 1) {
  129.                             t = varname[n];
  130.                             continue;
  131.                         }
  132.                         break;
  133.                     }
  134.                     type = substr(args[i], 1, length(args[i]) - length(t));
  135.                     picks[i] = t " = va_arg(ap, " type ");"
  136.  
  137.                 }
  138.                 nargs--;
  139.                 next;
  140.                 }
  141. Pending == 1 && /va_list/    {
  142.                 for (i = 1; i <= nargs; i++)
  143.                     print decls[i];
  144.                 }
  145. Pending == 1 && /va_start/    {
  146.                 n = split($0, tmp1, ",");
  147.                 if (n != 2) {
  148.                     print "ERROR: bad va_start format" | "sh -c 'cat >&2'";
  149.                     next;
  150.                 }
  151.                 printf "%s);\n", tmp1[1];
  152.                 for (i = 1; i <= nargs; i++)
  153.                     print picks[i];
  154.                 Pending = 0;
  155.                 next;
  156.                 }
  157. /^[^ \t#][^(]*\([^)]*\).*{/    {
  158.                 if (Pending != 0) {
  159.                     print "ERROR: Pending != 0 on function entry" | "sh -c 'cat >&2'";
  160.                     next;
  161.                 }
  162.                 n = split($0, tmp1, "(");
  163.                 if (n != 2) {
  164.                     print | "sh -c 'cat >&2'";
  165.                     print "ERROR with left parens: ", n | "sh -c 'cat >&2'";
  166.                     next;
  167.                 }
  168.                 n = split(tmp1[2], tmp2, ")");
  169.                 if (n != 2) {
  170.                     print | "sh -c 'cat >&2'";
  171.                     print "ERROR with right parens: ", n | "sh -c 'cat >&2'";
  172.                     next;
  173.                 }
  174. #                if (tmp2[2] != " {") {
  175. #                    print "ERROR with last component: `", tmp2[2], "'";
  176. #                    next;
  177. #                }
  178.                 if (tmp2[1] ~ /^[ \t]*void[ \t]*$/ || tmp2[1] ~ /^[ \t]*$/) {
  179.                     # no arguments
  180.                     printf "%s() {\n\n", tmp1[1];
  181.                     next;
  182.                 }
  183.                 printf "%s(", tmp1[1];
  184.                 needcomma = 0;
  185.                 nargs = split(tmp2[1], args, ",");
  186.                 for (i = 1; i <= nargs; i++) {
  187.                     if (args[i] == "...") {
  188.                         print "ERROR: ... shouldn't get here", $0 | "sh -c 'cat >&2'";
  189.                         next;
  190.                     }
  191.                     decls[i] = args[i] ";";
  192.                     if (needcomma)
  193.                         printf ", ";
  194.                     needcomma = 1;
  195.                     t = args[i];
  196.                     if (t ~ /\[\]$/)
  197.                         t = substr(t, 1, length(t) - 2);
  198.                     for (;;) {
  199.                         n = split(t, varname, " ");
  200.                         if (n > 1) {
  201.                             t = varname[n];
  202.                             continue;
  203.                         }
  204.                         n = split(t, varname, "*");
  205.                         if (n > 1) {
  206.                             t = varname[n];
  207.                             continue;
  208.                         }
  209.                         break;
  210.                     }
  211.                     printf "%s", varname[n];
  212.                 }
  213.                 print ")";
  214.                 for (i = 1; i <= nargs; i++)
  215.                     print decls[i];
  216.                 print "{";
  217.                 next;
  218.                 }
  219. #
  220. # If we didn't match at all, the line doesn't change.
  221. #
  222.                 { print; }
  223.