This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Let state sub fwd decls and nested subs work in anons
[perl5.git] / x2p / a2py.c
index cd396a2..aa48daa 100644 (file)
@@ -1,11 +1,10 @@
-/* $RCSfile: a2py.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:14 $
+/*    a2py.c
  *
- *    Copyright (c) 1991-1997, Larry Wall
+ *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
+ *    2000, 2001, 2002, by Larry Wall and others
  *
  *    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 $
  */
 
 #if defined(OS2) || defined(WIN32) || defined(NETWARE)
 #include "../patchlevel.h"
 #endif
 #include "util.h"
+#include "../unicode_constants.h"
+#define DELETE_CHAR DELETE_NATIVE
 
-char *filename;
-char *myname;
+const char *filename;
+const char *myname;
 
 int checkers = 0;
 
@@ -31,6 +32,10 @@ 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);
+#ifdef NETWARE
+char *savestr(char *str);
+char *cpy2(register char *to, register char *from, register int delim);
+#endif
 
 #if defined(OS2) || defined(WIN32) || defined(NETWARE)
 static void usage(void);
@@ -51,15 +56,20 @@ usage()
 }
 #endif
 
+#ifdef __osf__
+#pragma message disable (mainparm) /* We have the envp in main(). */
+#endif
+
 int
-main(register int argc, register char **argv, register char **env)
+main(register int argc, register const char **argv, register const char **env)
 {
-    register STR *str;
+    STR *str;
     int i;
     STR *tmpstr;
+    /* char *namelist;    */
 
        #ifdef NETWARE
-               fnInitGpfGlobals();     // For importing the CLIB calls in place of Watcom calls
+               fnInitGpfGlobals();     /* For importing the CLIB calls in place of Watcom calls */
        #endif  /* NETWARE */
 
     myname = argv[0];
@@ -109,7 +119,7 @@ main(register int argc, register char **argv, register char **env)
 
     /* open script */
 
-    if (argv[0] == Nullch) {
+    if (argv[0] == NULL) {
 #if defined(OS2) || defined(WIN32) || defined(NETWARE)
        if ( isatty(fileno(stdin)) )
            usage();
@@ -118,14 +128,13 @@ main(register int argc, register char **argv, register char **env)
     }
     filename = savestr(argv[0]);
 
-    filename = savestr(argv[0]);
     if (strEQ(filename,"-"))
        argv[0] = "";
     if (!*argv[0])
        rsfp = stdin;
     else
        rsfp = fopen(argv[0],"r");
-    if (rsfp == Nullfp)
+    if (rsfp == NULL)
        fatal("Awk script \"%s\" doesn't seem to exist.\n",filename);
 
     /* init tokener */
@@ -214,9 +223,9 @@ int idtype;
 int
 yylex(void)
 {
-    register char *s = bufptr;
-    register char *d;
-    register int tmp;
+    char *s = bufptr;
+    char *d;
+    int tmp;
 
   retry:
 #if YYDEBUG
@@ -246,10 +255,10 @@ yylex(void)
        if (!rsfp)
            RETURN(0);
        line++;
-       if ((s = str_gets(linestr, rsfp)) == Nullch) {
+       if ((s = str_gets(linestr, rsfp)) == NULL) {
            if (rsfp != stdin)
                fclose(rsfp);
-           rsfp = Nullfp;
+           rsfp = NULL;
            s = str_get(linestr);
            RETURN(0);
        }
@@ -282,11 +291,7 @@ yylex(void)
     case ':':
        tmp = *s++;
        XOP(tmp);
-#ifdef EBCDIC
-    case 7:
-#else
-    case 127:
-#endif
+    case DELETE_CHAR:
        s++;
        XTERM('}');
     case '}':
@@ -392,7 +397,7 @@ yylex(void)
 
 #define SNARFWORD \
        d = tokenbuf; \
-       while (isALPHA(*s) || isDIGIT(*s) || *s == '_') \
+       while (isWORDCHAR(*s)) \
            *d++ = *s++; \
        *d = '\0'; \
        d = tokenbuf; \
@@ -419,7 +424,14 @@ yylex(void)
                maxfld = tmp;
            XOP(FIELD);
        }
-       split_to_array = set_array_base = TRUE;
+       for (d = s; isWORDCHAR(*s); )
+           s++;
+       split_to_array = TRUE;
+       if (d != s)
+       {
+           yylval = string(d,s-d);
+           XTERM(SVFIELD);
+       }
        XOP(VFIELD);
 
     case '/':                  /* may either be division or pattern */
@@ -450,8 +462,6 @@ yylex(void)
 
     case 'a': case 'A':
        SNARFWORD;
-       if (strEQ(d,"ARGC"))
-           set_array_base = TRUE;
        if (strEQ(d,"ARGV")) {
            yylval=numary(string("ARGV",0));
            XOP(VAR);
@@ -547,7 +557,7 @@ yylex(void)
        else if (strEQ(d,"function"))
            XTERM(FUNCTION);
        if (strEQ(d,"FILENAME"))
-           d = "ARGV";
+           ID("ARGV");
        if (strEQ(d,"foreach"))
            *d = toUPPER(*d);
        else if (strEQ(d,"format"))
@@ -584,7 +594,6 @@ yylex(void)
        if (strEQ(d,"in"))
            XTERM(IN);
        if (strEQ(d,"index")) {
-           set_array_base = TRUE;
            XTERM(INDEX);
        }
        if (strEQ(d,"int")) {
@@ -630,7 +639,6 @@ yylex(void)
     case 'm': case 'M':
        SNARFWORD;
        if (strEQ(d,"match")) {
-           set_array_base = TRUE;
            XTERM(MATCH);
        }
        if (strEQ(d,"m"))
@@ -639,7 +647,7 @@ yylex(void)
     case 'n': case 'N':
        SNARFWORD;
        if (strEQ(d,"NF"))
-           do_chop = do_split = split_to_array = set_array_base = TRUE;
+           do_chop = do_split = split_to_array = TRUE;
        if (strEQ(d,"next")) {
            saw_line_op = TRUE;
            XTERM(NEXT);
@@ -651,14 +659,14 @@ yylex(void)
        SNARFWORD;
        if (strEQ(d,"ORS")) {
            saw_ORS = TRUE;
-           d = "\\";
+           ID("\\");
        }
        if (strEQ(d,"OFS")) {
            saw_OFS = TRUE;
-           d = ",";
+           ID(",");
        }
        if (strEQ(d,"OFMT")) {
-           d = "#";
+           ID("#");
        }
        if (strEQ(d,"open"))
            *d = toUPPER(*d);
@@ -686,8 +694,8 @@ yylex(void)
     case 'r': case 'R':
        SNARFWORD;
        if (strEQ(d,"RS")) {
-           d = "/";
            saw_RS = TRUE;
+           ID("/");
        }
        if (strEQ(d,"rand")) {
            yylval = ORAND;
@@ -705,11 +713,9 @@ yylex(void)
     case 's': case 'S':
        SNARFWORD;
        if (strEQ(d,"split")) {
-           set_array_base = TRUE;
            XOP(SPLIT);
        }
        if (strEQ(d,"substr")) {
-           set_array_base = TRUE;
            XTERM(SUBSTR);
        }
        if (strEQ(d,"sub"))
@@ -728,7 +734,7 @@ yylex(void)
            XTERM(FUN1);
        }
        if (strEQ(d,"SUBSEP")) {
-           d = ";";
+           ID(";");
        }
        if (strEQ(d,"sin")) {
            yylval = OSIN;
@@ -820,7 +826,7 @@ yylex(void)
 char *
 scanpat(register char *s)
 {
-    register char *d;
+    char *d;
 
     switch (*s++) {
     case '/':
@@ -863,7 +869,7 @@ scanpat(register char *s)
 }
 
 void
-yyerror(char *s)
+yyerror(const char *s)
 {
     fprintf(stderr,"%s in file %s at line %d\n",
       s,filename,line);
@@ -872,7 +878,7 @@ yyerror(char *s)
 char *
 scannum(register char *s)
 {
-    register char *d;
+    char *d;
 
     switch (*s) {
     case '1': case '2': case '3': case '4': case '5':
@@ -906,7 +912,7 @@ scannum(register char *s)
 }
 
 int
-string(char *ptr, int len)
+string(const char *ptr, int len)
 {
     int retval = mop;
 
@@ -1019,9 +1025,9 @@ int depth = 0;
 void
 dump(int branch)
 {
-    register int type;
-    register int len;
-    register int i;
+    int type;
+    int len;
+    int i;
 
     type = ops[branch].ival;
     len = type >> 8;
@@ -1059,8 +1065,8 @@ bl(int arg, int maybe)
 void
 fixup(STR *str)
 {
-    register char *s;
-    register char *t;
+    char *s;
+    char *t;
 
     for (s = str->str_ptr; *s; s++) {
        if (*s == ';' && s[1] == ' ' && s[2] == '\n') {
@@ -1084,8 +1090,8 @@ fixup(STR *str)
 void
 putlines(STR *str)
 {
-    register char *d, *s, *t, *e;
-    register int pos, newpos;
+    char *d, *s, *t, *e;
+    int pos, newpos;
 
     d = tokenbuf;
     pos = 0;
@@ -1160,7 +1166,7 @@ putlines(STR *str)
 void
 putone(void)
 {
-    register char *t;
+    char *t;
 
     for (t = tokenbuf; *t; t++) {
        *t &= 127;
@@ -1190,7 +1196,6 @@ numary(int arg)
     str_cat(key,"[]");
     hstore(symtab,key->str_ptr,str_make("1"));
     str_free(key);
-    set_array_base = TRUE;
     return arg;
 }