Use new config_h.SH STARTPERL #define.
[p5sagit/p5-mst-13.2.git] / x2p / walk.c
index 271581b..403d686 100644 (file)
@@ -1,4 +1,4 @@
-/* $RCSfile: walk.c,v $$Revision: 4.0.1.2 $$Date: 91/11/05 19:25:09 $
+/* $RCSfile: walk.c,v $$Revision: 4.1 $$Date: 92/08/07 18:29:31 $
  *
  *    Copyright (c) 1991, Larry Wall
  *
@@ -6,22 +6,11 @@
  *    License or the Artistic License, as specified in the README file.
  *
  * $Log:       walk.c,v $
- * Revision 4.0.1.2  91/11/05  19:25:09  lwall
- * patch11: in a2p, split on whitespace produced extra null field
- * 
- * Revision 4.0.1.1  91/06/07  12:22:04  lwall
- * patch4: new copyright notice
- * patch4: a2p didn't correctly implement -n switch
- * 
- * Revision 4.0  91/03/20  01:58:36  lwall
- * 4.0 baseline.
- * 
  */
 
-#include "handy.h"
 #include "EXTERN.h"
-#include "util.h"
 #include "a2p.h"
+#include "util.h"
 
 bool exitval = FALSE;
 bool realexit = FALSE;
@@ -37,6 +26,16 @@ char *limit;
 STR *subs;
 STR *curargs = Nullstr;
 
+static void addsemi _(( STR *str ));
+static void emit_split _(( STR *str, int level ));
+static void fixtab _(( STR *str, int lvl ));
+static void numericize _(( int node ));
+static void tab _(( STR *str, int lvl ));
+
+int prewalk _(( int numit, int level, int node, int *numericptr ));
+STR * walk _(( int useval, int level, int node, int *numericptr, int minprec ));
+
+
 STR *
 walk(useval,level,node,numericptr,minprec)
 int useval;
@@ -58,7 +57,6 @@ int minprec;                  /* minimum precedence without parens */
     int numeric = FALSE;
     STR *fstr;
     int prec = P_MAX;          /* assume no parens needed */
-    char *index();
 
     if (!node) {
        *numericptr = 0;
@@ -101,7 +99,7 @@ int minprec;                 /* minimum precedence without parens */
            do_chop = TRUE;
        if (fswitch) {
            str_cat(str,"$FS = '");
-           if (index("*+?.[]()|^$\\",fswitch))
+           if (strchr("*+?.[]()|^$\\",fswitch))
                str_cat(str,"\\");
            sprintf(tokenbuf,"%c",fswitch);
            str_cat(str,tokenbuf);
@@ -211,11 +209,8 @@ int minprec;                       /* minimum precedence without parens */
            str_cat(str,"\n\
 sub Pick {\n\
     local($mode,$name,$pipe) = @_;\n\
-    $fh = $opened{$name};\n\
-    if (!$fh) {\n\
-       $fh = $opened{$name} = 'fh_' . ($nextfh++ + 0);\n\
-       open($fh,$mode.$name.$pipe);\n\
-    }\n\
+    $fh = $name;\n\
+    open($name,$mode.$name.$pipe) unless $opened{$name}++;\n\
 }\n\
 ");
        }
@@ -392,8 +387,8 @@ sub Pick {\n\
                str_set(tmpstr,"gt");
            else if (strEQ(t,">="))
                str_set(tmpstr,"ge");
-           if (!index(tmpstr->str_ptr,'\'') && !index(tmpstr->str_ptr,'"') &&
-             !index(tmp2str->str_ptr,'\'') && !index(tmp2str->str_ptr,'"') )
+           if (!strchr(tmpstr->str_ptr,'\'') && !strchr(tmpstr->str_ptr,'"') &&
+             !strchr(tmp2str->str_ptr,'\'') && !strchr(tmp2str->str_ptr,'"') )
                numeric |= 2;
        }
        if (numeric & 2) {
@@ -468,6 +463,8 @@ sub Pick {\n\
        str_scat(str,fstr=walk(1,level,ops[node+3].ival,&numarg,prec));
        str_free(fstr);
        numeric |= numarg;
+       if (strEQ(str->str_ptr,"$/ = ''"))
+           str_set(str, "$/ = \"\\n\\n\"");
        break;
     case OADD:
        prec = P_ADD;
@@ -570,10 +567,9 @@ sub Pick {\n\
        if (useval)
            str_cat(str,"(");
        if (len > 0) {
-           str_cat(str,"$");
            str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
            if (!*fstr->str_ptr) {
-               str_cat(str,"_");
+               str_cat(str,"$_");
                len = 2;                /* a legal fiction */
            }
            str_free(fstr);
@@ -598,7 +594,7 @@ sub Pick {\n\
                    if (!isalpha(*t) && !isdigit(*t))
                        *t = '_';
                }
-               if (!index(tokenbuf,'_'))
+               if (!strchr(tokenbuf,'_'))
                    strcpy(t,"_FH");
                tmp3str = hfetch(symtab,tokenbuf);
                if (!tmp3str) {
@@ -687,7 +683,7 @@ sub Pick {\n\
            fstr = walk(1,level,ops[node+3].ival,&numarg,P_COMMA+1);
            if (str_len(fstr) == 3 && *fstr->str_ptr == '\'') {
                i = fstr->str_ptr[1] & 127;
-               if (index("*+?.[]()|^$\\",i))
+               if (strchr("*+?.[]()|^$\\",i))
                    sprintf(tokenbuf,"/\\%c/",i);
                else if (i == ' ')
                    sprintf(tokenbuf,"' '");
@@ -1129,7 +1125,7 @@ sub Pick {\n\
                if (!isalpha(*t) && !isdigit(*t))
                    *t = '_';
            }
-           if (!index(tokenbuf,'_'))
+           if (!strchr(tokenbuf,'_'))
                strcpy(t,"_FH");
            str_free(tmpstr);
            safefree(s);
@@ -1137,8 +1133,8 @@ sub Pick {\n\
            str_cat(str,tokenbuf);
        }
        else {
-           sprintf(tokenbuf,"$fh = delete $opened{%s} && close($fh)",
-              tmpstr->str_ptr);
+           sprintf(tokenbuf,"delete $opened{%s} && close(%s)",
+              tmpstr->str_ptr, tmpstr->str_ptr);
            str_free(tmpstr);
            str_set(str,tokenbuf);
        }
@@ -1166,7 +1162,7 @@ sub Pick {\n\
                    if (!isalpha(*t) && !isdigit(*t))
                        *t = '_';
                }
-               if (!index(tokenbuf,'_'))
+               if (!strchr(tokenbuf,'_'))
                    strcpy(t,"_FH");
                tmp3str = hfetch(symtab,tokenbuf);
                if (!tmp3str) {
@@ -1415,6 +1411,18 @@ sub Pick {\n\
        str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
        str_free(fstr);
        break;
+    case ODO:
+       str = str_new(0);
+       str_set(str,"do ");
+       str_scat(str,fstr=walk(1,level,ops[node+1].ival,&numarg,P_MIN));
+       str_free(fstr);
+       if (str->str_ptr[str->str_cur - 1] == '\n')
+           --str->str_cur;;
+       str_cat(str," while (");
+       str_scat(str,fstr=walk(0,level,ops[node+2].ival,&numarg,P_MIN));
+       str_free(fstr);
+       str_cat(str,");");
+       break;
     case OFOR:
        str = str_new(0);
        str_set(str,"for (");
@@ -1430,7 +1438,7 @@ sub Pick {\n\
        }
        str_cat(str,"; ");
        fstr=walk(1,level,ops[node+2].ival,&numarg,P_MIN);
-       if (i && (t = index(fstr->str_ptr,0377))) {
+       if (i && (t = strchr(fstr->str_ptr,0377))) {
            if (strnEQ(fstr->str_ptr,s,i))
                *t = ' ';
        }
@@ -1446,12 +1454,12 @@ sub Pick {\n\
        break;
     case OFORIN:
        tmpstr = walk(0,level,ops[node+1].ival,&numarg,P_MIN);
-       d = index(tmpstr->str_ptr,'$');
+       d = strchr(tmpstr->str_ptr,'$');
        if (!d)
            fatal("Illegal for loop: %s",tmpstr->str_ptr);
-       s = index(d,'{');
+       s = strchr(d,'{');
        if (!s)
-           s = index(d,'[');
+           s = strchr(d,'[');
        if (!s)
            fatal("Illegal for loop: %s",d);
        *s++ = '\0';
@@ -1547,6 +1555,7 @@ sub Pick {\n\
     return str;
 }
 
+static void
 tab(str,lvl)
 register STR *str;
 register int lvl;
@@ -1559,6 +1568,7 @@ register int lvl;
        str_cat(str,"    ");
 }
 
+static void
 fixtab(str,lvl)
 register STR *str;
 register int lvl;
@@ -1578,6 +1588,7 @@ register int lvl;
     tab(str,lvl);
 }
 
+static void
 addsemi(str)
 register STR *str;
 {
@@ -1590,6 +1601,7 @@ register STR *str;
        str_cat(str,";");
 }
 
+static void
 emit_split(str,level)
 register STR *str;
 int level;
@@ -1624,6 +1636,7 @@ int level;
     tab(str,level);
 }
 
+int
 prewalk(numit,level,node,numericptr)
 int numit;
 int level;
@@ -1633,8 +1646,6 @@ int *numericptr;
     register int len;
     register int type;
     register int i;
-    char *t;
-    char *d, *s;
     int numarg;
     int numeric = FALSE;
     STR *tmpstr;
@@ -1754,7 +1765,7 @@ int *numericptr;
        prewalk(0,level,ops[node+2].ival,&numarg);
        prewalk(0,level,ops[node+1].ival,&numarg);
        prewalk(0,level,ops[node+3].ival,&numarg);
-       if (numarg || strlen(ops[ops[node+1].ival+1].cval) > 1) {
+       if (numarg || strlen(ops[ops[node+1].ival+1].cval) > (Size_t)1) {
            numericize(ops[node+2].ival);
            if (!numarg)
                numericize(ops[node+3].ival);
@@ -2046,12 +2057,12 @@ int *numericptr;
     return 1;
 }
 
+static void
 numericize(node)
 register int node;
 {
     register int len;
     register int type;
-    register int i;
     STR *tmpstr;
     STR *tmp2str;
     int numarg;