X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=x2p%2Fa2py.c;h=cd396a28de2061a3d59feb8619c7ca66786c58cb;hb=03e60089f91d0a4a765dc827f0f6b27465941491;hp=836d17604cf058cef20a0c8c34f2016754d8b9ad;hpb=39c3038ca76b338006c640ae6da52b407dd9e654;p=p5sagit%2Fp5-mst-13.2.git diff --git a/x2p/a2py.c b/x2p/a2py.c index 836d176..cd396a2 100644 --- a/x2p/a2py.c +++ b/x2p/a2py.c @@ -1,38 +1,44 @@ -/* $Header: a2py.c,v 3.0.1.2 90/10/16 11:30:34 lwall Locked $ +/* $RCSfile: a2py.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:14 $ * - * Copyright (c) 1989, Larry Wall + * Copyright (c) 1991-1997, Larry Wall * - * You may distribute under the terms of the GNU General Public License - * as specified in the README file that comes with the perl 3.0 kit. + * You may distribute under the terms of either the GNU General Public + * License or the Artistic License, as specified in the README file. * * $Log: a2py.c,v $ - * Revision 3.0.1.2 90/10/16 11:30:34 lwall - * patch29: various portability fixes - * - * Revision 3.0.1.1 90/08/09 05:48:53 lwall - * patch19: a2p didn't emit a chop when NF was referenced though split needs it - * - * Revision 3.0 89/10/18 15:34:35 lwall - * 3.0 baseline - * */ -#ifdef MSDOS -#include "../patchlev.h" +#if defined(OS2) || defined(WIN32) || defined(NETWARE) +#if defined(WIN32) +#include +#endif +#if defined(NETWARE) +#include "../netware/clibstuf.h" +#endif +#include "../patchlevel.h" #endif #include "util.h" -char *index(); char *filename; char *myname; int checkers = 0; -STR *walk(); -#ifdef MSDOS +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) || defined(NETWARE) +static void usage(void); + +static void usage() { - printf("\nThis is the AWK to PERL translator, version 3.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] [-F] [-n] [-] filename\n", myname); printf("\n -D sets debugging flags." "\n -F the awk script to translate is always invoked with" @@ -44,28 +50,29 @@ usage() exit(1); } #endif -main(argc,argv,env) -register int argc; -register char **argv; -register char **env; + +int +main(register int argc, register char **argv, register char **env) { register STR *str; - register char *s; int i; STR *tmpstr; + #ifdef NETWARE + fnInitGpfGlobals(); // For importing the CLIB calls in place of Watcom calls + #endif /* NETWARE */ + myname = argv[0]; linestr = str_new(80); str = str_new(0); /* first used for -I flags */ for (argc--,argv++; argc; argc--,argv++) { if (argv[0][0] != '-' || !argv[0][1]) break; - reswitch: switch (argv[0][1]) { #ifdef DEBUGGING case 'D': debug = atoi(argv[0]+2); -#ifdef YYDEBUG +#if YYDEBUG yydebug = (debug & 1); #endif break; @@ -81,15 +88,20 @@ register char **env; case 'n': namelist = savestr(argv[0]+2); break; + case 'o': + old_awk = TRUE; + break; case '-': argc--,argv++; goto switch_end; case 0: break; default: - fatal("Unrecognized switch: %s\n",argv[0]); -#ifdef MSDOS +#if defined(OS2) || defined(WIN32) || defined(NETWARE) + fprintf(stderr, "Unrecognized switch: %s\n",argv[0]); usage(); +#else + fatal("Unrecognized switch: %s\n",argv[0]); #endif } } @@ -98,7 +110,7 @@ register char **env; /* open script */ if (argv[0] == Nullch) { -#ifdef MSDOS +#if defined(OS2) || defined(WIN32) || defined(NETWARE) if ( isatty(fileno(stdin)) ) usage(); #endif @@ -157,16 +169,15 @@ register char **env; /* second pass to produce new program */ tmpstr = walk(0,0,root,&i,P_MIN); - str = str_make("#!"); - str_cat(str, BIN); - str_cat(str, "/perl\neval \"exec "); + str = str_make(STARTPERL); + str_cat(str, "\neval 'exec "); str_cat(str, BIN); - str_cat(str, "/perl -S $0 $*\"\n\ + str_cat(str, "/perl -S $0 ${1+\"$@\"}'\n\ if $running_under_some_shell;\n\ # this emulates #! processing on NIH machines.\n\ # (remove #! line above if indigestible)\n\n"); str_cat(str, - "eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;\n"); + "eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;\n"); str_cat(str, " # process any FOO=bar switches\n\n"); if (do_opens && opens) { @@ -189,6 +200,8 @@ register char **env; "The operation I've selected may be wrong for the operand types.\n"); } exit(0); + /* by ANSI specs return is needed. This also shuts up VC++ and his warnings */ + return(0); } #define RETURN(retval) return (bufptr = s,retval) @@ -198,19 +211,21 @@ register char **env; int idtype; -yylex() +int +yylex(void) { register char *s = bufptr; register char *d; register int tmp; retry: -#ifdef YYDEBUG - if (yydebug) - if (index(s,'\n')) +#if YYDEBUG + if (yydebug) { + if (strchr(s,'\n')) fprintf(stderr,"Tokener at %s",s); else fprintf(stderr,"Tokener at %s\n",s); + } #endif switch (*s) { default: @@ -219,6 +234,12 @@ yylex() *s++,filename,line); goto retry; case '\\': + s++; + if (*s && *s != '\n') { + yyerror("Ignoring spurious backslash"); + goto retry; + } + /*FALLSTHROUGH*/ case 0: s = str_get(linestr); *s = '\0'; @@ -261,11 +282,15 @@ yylex() case ':': tmp = *s++; XOP(tmp); +#ifdef EBCDIC + case 7: +#else case 127: +#endif s++; XTERM('}'); case '}': - for (d = s + 1; isspace(*d); d++) ; + for (d = s + 1; isSPACE(*d); d++) ; if (!*d) s = d - 1; *s = 127; @@ -367,7 +392,7 @@ yylex() #define SNARFWORD \ d = tokenbuf; \ - while (isalpha(*s) || isdigit(*s) || *s == '_') \ + while (isALPHA(*s) || isDIGIT(*s) || *s == '_') \ *d++ = *s++; \ *d = '\0'; \ d = tokenbuf; \ @@ -386,8 +411,8 @@ yylex() ID("0"); } do_split = TRUE; - if (isdigit(*s)) { - for (d = s; isdigit(*s); s++) ; + if (isDIGIT(*s)) { + for (d = s; isDIGIT(*s); s++) ; yylval = string(d,s-d); tmp = atoi(d); if (tmp > maxfld) @@ -457,15 +482,15 @@ yylex() XTERM(FUN1); } if (strEQ(d,"chdir")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"crypt")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"chop")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"chmod")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"chown")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'd': case 'D': SNARFWORD; @@ -474,7 +499,7 @@ yylex() if (strEQ(d,"delete")) XTERM(DELETE); if (strEQ(d,"die")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'e': case 'E': SNARFWORD; @@ -491,26 +516,26 @@ yylex() XTERM(FUN1); } if (strEQ(d,"elsif")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"eq")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"eval")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"eof")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"each")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"exec")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'f': case 'F': SNARFWORD; if (strEQ(d,"FS")) { saw_FS++; if (saw_FS == 1 && in_begin) { - for (d = s; *d && isspace(*d); d++) ; + for (d = s; *d && isSPACE(*d); d++) ; if (*d == '=') { - for (d++; *d && isspace(*d); d++) ; + for (d++; *d && isSPACE(*d); d++) ; if (*d == '"' && d[2] == '"') const_FS = d[1]; } @@ -524,13 +549,13 @@ yylex() if (strEQ(d,"FILENAME")) d = "ARGV"; if (strEQ(d,"foreach")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"format")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"fork")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"fh")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'g': case 'G': SNARFWORD; @@ -539,18 +564,18 @@ yylex() if (strEQ(d,"gsub")) XTERM(GSUB); if (strEQ(d,"ge")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"gt")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"goto")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"gmtime")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'h': case 'H': SNARFWORD; if (strEQ(d,"hex")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'i': case 'I': SNARFWORD; @@ -570,14 +595,14 @@ yylex() case 'j': case 'J': SNARFWORD; if (strEQ(d,"join")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'k': case 'K': SNARFWORD; if (strEQ(d,"keys")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"kill")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'l': case 'L': SNARFWORD; @@ -590,17 +615,17 @@ yylex() XTERM(FUN1); } if (strEQ(d,"last")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"local")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"lt")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"le")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"locatime")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"link")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'm': case 'M': SNARFWORD; @@ -609,7 +634,7 @@ yylex() XTERM(MATCH); } if (strEQ(d,"m")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'n': case 'N': SNARFWORD; @@ -620,7 +645,7 @@ yylex() XTERM(NEXT); } if (strEQ(d,"ne")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'o': case 'O': SNARFWORD; @@ -636,11 +661,11 @@ yylex() d = "#"; } if (strEQ(d,"open")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"ord")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"oct")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'p': case 'P': SNARFWORD; @@ -651,9 +676,9 @@ yylex() XTERM(PRINTF); } if (strEQ(d,"push")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"pop")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'q': case 'Q': SNARFWORD; @@ -671,11 +696,11 @@ yylex() if (strEQ(d,"return")) XTERM(RET); if (strEQ(d,"reset")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"redo")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"rename")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 's': case 'S': SNARFWORD; @@ -689,8 +714,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); @@ -711,73 +743,73 @@ yylex() XTERM(FUN1); } if (strEQ(d,"s")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"shift")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"select")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"seek")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"stat")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"study")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"sleep")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"symlink")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"sort")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 't': case 'T': SNARFWORD; if (strEQ(d,"tr")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"tell")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"time")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"times")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'u': case 'U': SNARFWORD; if (strEQ(d,"until")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"unless")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"umask")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"unshift")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"unlink")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"utime")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'v': case 'V': SNARFWORD; if (strEQ(d,"values")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'w': case 'W': SNARFWORD; if (strEQ(d,"while")) XTERM(WHILE); if (strEQ(d,"write")) - *d = toupper(*d); + *d = toUPPER(*d); else if (strEQ(d,"wait")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'x': case 'X': SNARFWORD; if (strEQ(d,"x")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'y': case 'Y': SNARFWORD; if (strEQ(d,"y")) - *d = toupper(*d); + *d = toUPPER(*d); ID(d); case 'z': case 'Z': SNARFWORD; @@ -786,8 +818,7 @@ yylex() } char * -scanpat(s) -register char *s; +scanpat(register char *s) { register char *d; @@ -805,6 +836,8 @@ register char *s; *d++ = *s++; else if (s[1] == '\\') *d++ = *s++; + else if (s[1] == '[') + *d++ = *s++; } else if (*s == '[') { *d++ = *s++; @@ -829,16 +862,15 @@ register char *s; return s; } -yyerror(s) -char *s; +void +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; @@ -846,20 +878,24 @@ register char *s; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0' : case '.': d = tokenbuf; - while (isdigit(*s)) { + while (isDIGIT(*s)) { *d++ = *s++; } - if (*s == '.' && index("0123456789eE",s[1])) { - *d++ = *s++; - while (isdigit(*s)) { + if (*s == '.') { + if (isDIGIT(s[1])) { *d++ = *s++; + while (isDIGIT(*s)) { + *d++ = *s++; + } } + else + s++; } - if (index("eE",*s) && index("+-0123456789",s[1])) { + if (strchr("eE",*s) && strchr("+-0123456789",s[1])) { *d++ = *s++; if (*s == '+' || *s == '-') *d++ = *s++; - while (isdigit(*s)) + while (isDIGIT(*s)) *d++ = *s++; } *d = '\0'; @@ -869,15 +905,15 @@ register char *s; return s; } -string(ptr,len) -char *ptr; +int +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) @@ -885,8 +921,8 @@ char *ptr; return retval; } -oper0(type) -int type; +int +oper0(int type) { int retval = mop; @@ -898,9 +934,8 @@ int type; return retval; } -oper1(type,arg1) -int type; -int arg1; +int +oper1(int type, int arg1) { int retval = mop; @@ -913,10 +948,8 @@ int arg1; return retval; } -oper2(type,arg1,arg2) -int type; -int arg1; -int arg2; +int +oper2(int type, int arg1, int arg2) { int retval = mop; @@ -930,11 +963,8 @@ int arg2; return retval; } -oper3(type,arg1,arg2,arg3) -int type; -int arg1; -int arg2; -int arg3; +int +oper3(int type, int arg1, int arg2, int arg3) { int retval = mop; @@ -949,12 +979,8 @@ int arg3; return retval; } -oper4(type,arg1,arg2,arg3,arg4) -int type; -int arg1; -int arg2; -int arg3; -int arg4; +int +oper4(int type, int arg1, int arg2, int arg3, int arg4) { int retval = mop; @@ -970,13 +996,8 @@ int arg4; return retval; } -oper5(type,arg1,arg2,arg3,arg4,arg5) -int type; -int arg1; -int arg2; -int arg3; -int arg4; -int arg5; +int +oper5(int type, int arg1, int arg2, int arg3, int arg4, int arg5) { int retval = mop; @@ -995,8 +1016,8 @@ int arg5; int depth = 0; -dump(branch) -int branch; +void +dump(int branch) { register int type; register int len; @@ -1022,9 +1043,8 @@ int branch; } } -bl(arg,maybe) -int arg; -int maybe; +int +bl(int arg, int maybe) { if (!arg) return 0; @@ -1036,8 +1056,8 @@ int maybe; return arg; } -fixup(str) -STR *str; +void +fixup(STR *str) { register char *s; register char *t; @@ -1048,9 +1068,9 @@ STR *str; s++; } else if (*s == '\n') { - for (t = s+1; isspace(*t & 127); t++) ; + for (t = s+1; isSPACE(*t & 127); t++) ; t--; - while (isspace(*t & 127) && *t != '\n') t--; + while (isSPACE(*t & 127) && *t != '\n') t--; if (*t == '\n' && t-s > 1) { if (s[-1] == '{') s--; @@ -1061,8 +1081,8 @@ STR *str; } } -putlines(str) -STR *str; +void +putlines(STR *str) { register char *d, *s, *t, *e; register int pos, newpos; @@ -1083,7 +1103,7 @@ STR *str; if (pos > 78) { /* split a long line? */ *d-- = '\0'; newpos = 0; - for (t = tokenbuf; isspace(*t & 127); t++) { + for (t = tokenbuf; isSPACE(*t & 127); t++) { if (*t == '\t') newpos += 8; else @@ -1115,7 +1135,10 @@ STR *str; d--; } if (d > t+3) { - *d = '\0'; + char save[2048]; + strcpy(save, d); + *d = '\n'; + d[1] = '\0'; putone(); putchar('\n'); if (d[-1] != ';' && !(newpos % 4)) { @@ -1123,7 +1146,7 @@ STR *str; *t++ = ' '; newpos += 2; } - strcpy(t,d+1); + strcpy(t,save+1); newpos += strlen(t); d = t + strlen(t); pos = newpos; @@ -1134,7 +1157,8 @@ STR *str; } } -putone() +void +putone(void) { register char *t; @@ -1156,8 +1180,8 @@ putone() fputs(tokenbuf,stdout); } -numary(arg) -int arg; +int +numary(int arg) { STR *key; int dummy; @@ -1170,8 +1194,8 @@ int arg; return arg; } -rememberargs(arg) -int arg; +int +rememberargs(int arg) { int type; STR *str; @@ -1192,8 +1216,8 @@ int arg; return arg; } -aryrefarg(arg) -int arg; +int +aryrefarg(int arg) { int type = ops[arg].ival & 255; STR *str; @@ -1206,14 +1230,12 @@ int arg; return arg; } -fixfargs(name,arg,prevargs) -int name; -int arg; -int prevargs; +int +fixfargs(int name, int arg, int prevargs) { int type; STR *str; - int numargs; + int numargs = 0; if (!arg) return prevargs; @@ -1244,10 +1266,8 @@ int prevargs; return numargs; } -fixrargs(name,arg,prevargs) -char *name; -int arg; -int prevargs; +int +fixrargs(char *name, int arg, int prevargs) { int type; STR *str; @@ -1261,11 +1281,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); - fprintf(stderr,"Looking for %s\n",tmpbuf); + safefree(tmpbuf); if (str && strEQ(str->str_ptr,"*")) { if (type == OVAR || type == OSTAR) { ops[arg].ival &= ~255; @@ -1279,4 +1298,3 @@ int prevargs; } return numargs; } -