tolerate NULL SITELIB_EXP
[p5sagit/p5-mst-13.2.git] / x2p / a2py.c
index ad685fe..3976c86 100644 (file)
@@ -8,7 +8,10 @@
  * $Log:       a2py.c,v $
  */
 
-#ifdef OS2
+#if defined(OS2) || defined(WIN32)
+#if defined(WIN32)
+#include <io.h>
+#endif
 #include "../patchlevel.h"
 #endif
 #include "util.h"
@@ -18,19 +21,21 @@ char *myname;
 
 int checkers = 0;
 
-int oper0();
-int oper1();
-int oper2();
-int oper3();
-int oper4();
-int oper5();
-STR *walk();
+int oper0(int type);
+int oper1(int type, int arg1);
+int oper2(int type, int arg1, int arg2);
+int oper3(int type, int arg1, int arg2, int arg3);
+int oper4(int type, int arg1, int arg2, int arg3, int arg4);
+int oper5(int type, int arg1, int arg2, int arg3, int arg4, int arg5);
+STR *walk(int useval, int level, register int node, int *numericptr, int minprec);
+
+#if defined(OS2) || defined(WIN32)
+static void usage(void);
 
-#ifdef OS2
 static void
 usage()
 {
-    printf("\nThis is the AWK to PERL translator, version 5.0, patchlevel %d\n", PATCHLEVEL);
+    printf("\nThis is the AWK to PERL translator, revision %d.0, version %d\n", PERL_REVISION, PERL_VERSION);
     printf("\nUsage: %s [-D<number>] [-F<char>] [-n<fieldlist>] [-<number>] filename\n", myname);
     printf("\n  -D<number>      sets debugging flags."
            "\n  -F<character>   the awk script to translate is always invoked with"
@@ -44,10 +49,7 @@ usage()
 #endif
 
 int
-main(argc,argv,env)
-register int argc;
-register char **argv;
-register char **env;
+main(register int argc, register char **argv, register char **env)
 {
     register STR *str;
     int i;
@@ -64,7 +66,7 @@ register char **env;
 #ifdef DEBUGGING
        case 'D':
            debug = atoi(argv[0]+2);
-#ifdef YYDEBUG
+#if YYDEBUG
            yydebug = (debug & 1);
 #endif
            break;
@@ -89,9 +91,11 @@ register char **env;
        case 0:
            break;
        default:
-           fatal("Unrecognized switch: %s\n",argv[0]);
-#ifdef OS2
+#if defined(OS2) || defined(WIN32)
+           fprintf(stderr, "Unrecognized switch: %s\n",argv[0]);
             usage();
+#else
+           fatal("Unrecognized switch: %s\n",argv[0]);
 #endif
        }
     }
@@ -100,7 +104,7 @@ register char **env;
     /* open script */
 
     if (argv[0] == Nullch) {
-#ifdef OS2
+#if defined(OS2) || defined(WIN32)
        if ( isatty(fileno(stdin)) )
            usage();
 #endif
@@ -200,14 +204,14 @@ register char **env;
 int idtype;
 
 int
-yylex()
+yylex(void)
 {
     register char *s = bufptr;
     register char *d;
     register int tmp;
 
   retry:
-#ifdef YYDEBUG
+#if YYDEBUG
     if (yydebug)
        if (strchr(s,'\n'))
            fprintf(stderr,"Tokener at %s",s);
@@ -269,7 +273,11 @@ yylex()
     case ':':
        tmp = *s++;
        XOP(tmp);
+#ifdef EBCDIC
+    case 7:
+#else
     case 127:
+#endif
        s++;
        XTERM('}');
     case '}':
@@ -697,8 +705,15 @@ yylex()
        }
        if (strEQ(d,"sub"))
            XTERM(SUB);
-       if (strEQ(d,"sprintf"))
-           XTERM(SPRINTF);
+       if (strEQ(d,"sprintf")) {
+            /* In old awk, { print sprintf("str%sg"),"in" } prints
+             * "string"; in new awk, "in" is not considered an argument to
+             * sprintf, so the statement breaks.  To support both, the
+             * grammar treats arguments to SPRINTF_OLD like old awk,
+             * SPRINTF_NEW like new.  Here we return the appropriate one.
+             */
+           XTERM(old_awk ? SPRINTF_OLD : SPRINTF_NEW);
+        }
        if (strEQ(d,"sqrt")) {
            yylval = OSQRT;
            XTERM(FUN1);
@@ -794,8 +809,7 @@ yylex()
 }
 
 char *
-scanpat(s)
-register char *s;
+scanpat(register char *s)
 {
     register char *d;
 
@@ -840,16 +854,14 @@ register char *s;
 }
 
 void
-yyerror(s)
-char *s;
+yyerror(char *s)
 {
     fprintf(stderr,"%s in file %s at line %d\n",
       s,filename,line);
 }
 
 char *
-scannum(s)
-register char *s;
+scannum(register char *s)
 {
     register char *d;
 
@@ -885,16 +897,14 @@ register char *s;
 }
 
 int
-string(ptr,len)
-char *ptr;
-int len;
+string(char *ptr, int len)
 {
     int retval = mop;
 
     ops[mop++].ival = OSTRING + (1<<8);
     if (!len)
        len = strlen(ptr);
-    ops[mop].cval = safemalloc(len+1);
+    ops[mop].cval = (char *) safemalloc(len+1);
     strncpy(ops[mop].cval,ptr,len);
     ops[mop++].cval[len] = '\0';
     if (mop >= OPSMAX)
@@ -903,8 +913,7 @@ int len;
 }
 
 int
-oper0(type)
-int type;
+oper0(int type)
 {
     int retval = mop;
 
@@ -917,9 +926,7 @@ int type;
 }
 
 int
-oper1(type,arg1)
-int type;
-int arg1;
+oper1(int type, int arg1)
 {
     int retval = mop;
 
@@ -933,10 +940,7 @@ int arg1;
 }
 
 int
-oper2(type,arg1,arg2)
-int type;
-int arg1;
-int arg2;
+oper2(int type, int arg1, int arg2)
 {
     int retval = mop;
 
@@ -951,11 +955,7 @@ int arg2;
 }
 
 int
-oper3(type,arg1,arg2,arg3)
-int type;
-int arg1;
-int arg2;
-int arg3;
+oper3(int type, int arg1, int arg2, int arg3)
 {
     int retval = mop;
 
@@ -971,12 +971,7 @@ int arg3;
 }
 
 int
-oper4(type,arg1,arg2,arg3,arg4)
-int type;
-int arg1;
-int arg2;
-int arg3;
-int arg4;
+oper4(int type, int arg1, int arg2, int arg3, int arg4)
 {
     int retval = mop;
 
@@ -993,13 +988,7 @@ int arg4;
 }
 
 int
-oper5(type,arg1,arg2,arg3,arg4,arg5)
-int type;
-int arg1;
-int arg2;
-int arg3;
-int arg4;
-int arg5;
+oper5(int type, int arg1, int arg2, int arg3, int arg4, int arg5)
 {
     int retval = mop;
 
@@ -1019,8 +1008,7 @@ int arg5;
 int depth = 0;
 
 void
-dump(branch)
-int branch;
+dump(int branch)
 {
     register int type;
     register int len;
@@ -1047,9 +1035,7 @@ int branch;
 }
 
 int
-bl(arg,maybe)
-int arg;
-int maybe;
+bl(int arg, int maybe)
 {
     if (!arg)
        return 0;
@@ -1062,8 +1048,7 @@ int maybe;
 }
 
 void
-fixup(str)
-STR *str;
+fixup(STR *str)
 {
     register char *s;
     register char *t;
@@ -1088,8 +1073,7 @@ STR *str;
 }
 
 void
-putlines(str)
-STR *str;
+putlines(STR *str)
 {
     register char *d, *s, *t, *e;
     register int pos, newpos;
@@ -1165,7 +1149,7 @@ STR *str;
 }
 
 void
-putone()
+putone(void)
 {
     register char *t;
 
@@ -1188,8 +1172,7 @@ putone()
 }
 
 int
-numary(arg)
-int arg;
+numary(int arg)
 {
     STR *key;
     int dummy;
@@ -1203,8 +1186,7 @@ int arg;
 }
 
 int
-rememberargs(arg)
-int arg;
+rememberargs(int arg)
 {
     int type;
     STR *str;
@@ -1226,8 +1208,7 @@ int arg;
 }
 
 int
-aryrefarg(arg)
-int arg;
+aryrefarg(int arg)
 {
     int type = ops[arg].ival & 255;
     STR *str;
@@ -1241,10 +1222,7 @@ int arg;
 }
 
 int
-fixfargs(name,arg,prevargs)
-int name;
-int arg;
-int prevargs;
+fixfargs(int name, int arg, int prevargs)
 {
     int type;
     STR *str;
@@ -1280,10 +1258,7 @@ int prevargs;
 }
 
 int
-fixrargs(name,arg,prevargs)
-char *name;
-int arg;
-int prevargs;
+fixrargs(char *name, int arg, int prevargs)
 {
     int type;
     STR *str;
@@ -1297,10 +1272,10 @@ int prevargs;
        numargs = fixrargs(name,ops[arg+3].ival,numargs);
     }
     else {
-       char tmpbuf[128];
-
+       char *tmpbuf = (char *) safemalloc(strlen(name) + (sizeof(prevargs) * 3) + 5);
        sprintf(tmpbuf,"%s:%d",name,prevargs);
        str = hfetch(curarghash,tmpbuf);
+       safefree(tmpbuf);
        if (str && strEQ(str->str_ptr,"*")) {
            if (type == OVAR || type == OSTAR) {
                ops[arg].ival &= ~255;