home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / msdos / perl386 / toke.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  63.5 KB  |  2,764 lines

  1. /* $RCSfile: toke.c,v $$Revision: 4.0.1.9 $$Date: 1993/02/05 19:48:43 $
  2.  *
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log: toke.c,v $
  9.  * Revision 4.0.1.9  1993/02/05  19:48:43  lwall
  10.  * patch36: now detects ambiguous use of filetest operators as well as unary
  11.  * patch36: fixed ambiguity on - within tr///
  12.  *
  13.  * Revision 4.0.1.8  92/06/23  12:33:45  lwall
  14.  * patch35: bad interaction between backslash and hyphen in tr///
  15.  * 
  16.  * Revision 4.0.1.7  92/06/11  21:16:30  lwall
  17.  * patch34: expectterm incorrectly set to indicate start of program or block
  18.  * 
  19.  * Revision 4.0.1.6  92/06/08  16:03:49  lwall
  20.  * patch20: an EXPR may now start with a bareword
  21.  * patch20: print $fh EXPR can now expect term rather than operator in EXPR
  22.  * patch20: added ... as variant on ..
  23.  * patch20: new warning on spurious backslash
  24.  * patch20: new warning on missing $ for foreach variable
  25.  * patch20: "foo"x1024 now legal without space after x
  26.  * patch20: new warning on print accidentally used as function
  27.  * patch20: tr/stuff// wasn't working right
  28.  * patch20: 2. now eats the dot
  29.  * patch20: <@ARGV> now notices @ARGV
  30.  * patch20: tr/// now lets you say \-
  31.  * 
  32.  * Revision 4.0.1.5  91/11/11  16:45:51  lwall
  33.  * patch19: default arg for shift was wrong after first subroutine definition
  34.  * 
  35.  * Revision 4.0.1.4  91/11/05  19:02:48  lwall
  36.  * patch11: \x and \c were subject to double interpretation in regexps
  37.  * patch11: prepared for ctype implementations that don't define isascii()
  38.  * patch11: nested list operators could miscount parens
  39.  * patch11: once-thru blocks didn't display right in the debugger
  40.  * patch11: sort eval "whatever" didn't work
  41.  * patch11: underscore is now allowed within literal octal and hex numbers
  42.  * 
  43.  * Revision 4.0.1.3  91/06/10  01:32:26  lwall
  44.  * patch10: m'$foo' now treats string as single quoted
  45.  * patch10: certain pattern optimizations were botched
  46.  * 
  47.  * Revision 4.0.1.2  91/06/07  12:05:56  lwall
  48.  * patch4: new copyright notice
  49.  * patch4: debugger lost track of lines in eval
  50.  * patch4: //o and s///o now optimize themselves fully at runtime
  51.  * patch4: added global modifier for pattern matches
  52.  * 
  53.  * Revision 4.0.1.1  91/04/12  09:18:18  lwall
  54.  * patch1: perl -de "print" wouldn't stop at the first statement
  55.  * 
  56.  * Revision 4.0  91/03/20  01:42:14  lwall
  57.  * 4.0 baseline.
  58.  * 
  59.  */
  60.  
  61. #include "EXTERN.h"
  62. #include "perl.h"
  63. #include "perly.h"
  64.  
  65. static void set_csh();
  66.  
  67. #ifdef I_FCNTL
  68. #include <fcntl.h>
  69. #endif
  70. #ifdef I_SYS_FILE
  71. #include <sys/file.h>
  72. #endif
  73.  
  74. #ifdef f_next
  75. #undef f_next
  76. #endif
  77.  
  78. /* which backslash sequences to keep in m// or s// */
  79.  
  80. static char *patleave = "\\.^$@dDwWsSbB+*?|()-nrtfeaxc0123456789[{]}";
  81.  
  82. char *reparse;        /* if non-null, scanident found ${foo[$bar]} */
  83.  
  84. void checkcomma();
  85.  
  86. #ifdef CLINE
  87. #undef CLINE
  88. #endif
  89. #define CLINE (cmdline = (curcmd->c_line < cmdline ? curcmd->c_line : cmdline))
  90.  
  91. #ifdef atarist
  92. #define PERL_META(c) ((c) | 128)
  93. #else
  94. #define META(c) ((c) | 128)
  95. #endif
  96.  
  97. #define RETURN(retval) return (bufptr = s,(int)retval)
  98. #define OPERATOR(retval) return (expectterm = TRUE,bufptr = s,(int)retval)
  99. #define TERM(retval) return (CLINE, expectterm = FALSE,bufptr = s,(int)retval)
  100. #define LOOPX(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)LOOPEX)
  101. #define FTST(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)FILETEST)
  102. #define FUN0(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC0)
  103. #define FUN1(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC1)
  104. #define FUN2(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC2)
  105. #define FUN2x(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC2x)
  106. #define FUN3(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC3)
  107. #define FUN4(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC4)
  108. #define FUN5(f) return(yylval.ival = f,expectterm = FALSE,bufptr = s,(int)FUNC5)
  109. #define FL(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FLIST)
  110. #define FL2(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FLIST2)
  111. #define HFUN(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)HSHFUN)
  112. #define HFUN3(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)HSHFUN3)
  113. #define LFUN(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)LVALFUN)
  114. #define AOP(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)ADDOP)
  115. #define MOP(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)MULOP)
  116. #define EOP(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)EQOP)
  117. #define ROP(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)RELOP)
  118. #define FOP(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP)
  119. #define FOP2(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP2)
  120. #define FOP3(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP3)
  121. #define FOP4(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP4)
  122. #define FOP22(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP22)
  123. #define FOP25(f) return(yylval.ival=f,expectterm = FALSE,bufptr = s,(int)FILOP25)
  124.  
  125. static char *last_uni;
  126.  
  127. /* This bit of chicanery makes a unary function followed by
  128.  * a parenthesis into a function with one argument, highest precedence.
  129.  */
  130. #define UNI(f) return(yylval.ival = f, \
  131.     expectterm = TRUE, \
  132.     bufptr = s, \
  133.     last_uni = oldbufptr, \
  134.     (*s == '(' || (s = skipspace(s), *s == '(') ? (int)FUNC1 : (int)UNIOP) )
  135.  
  136. /* This does similarly for list operators, merely by pretending that the
  137.  * paren came before the listop rather than after.
  138.  */
  139. #ifdef atarist
  140. #define LOP(f) return(CLINE, *s == '(' || (s = skipspace(s), *s == '(') ? \
  141.     (*s = (char) PERL_META('('), bufptr = oldbufptr, '(') : \
  142.     (yylval.ival=f,expectterm = TRUE,bufptr = s,(int)LISTOP))
  143. #else
  144. #define LOP(f) return(CLINE, *s == '(' || (s = skipspace(s), *s == '(') ? \
  145.     (*s = (char) META('('), bufptr = oldbufptr, '(') : \
  146.     (yylval.ival=f,expectterm = TRUE,bufptr = s,(int)LISTOP))
  147. #endif
  148. /* grandfather return to old style */
  149. #define OLDLOP(f) return(yylval.ival=f,expectterm = TRUE,bufptr = s,(int)LISTOP)
  150.  
  151. char *
  152. skipspace(s)
  153. register char *s;
  154. {
  155.     while (s < bufend && isSPACE(*s))
  156.     s++;
  157.     return s;
  158. }
  159.  
  160. void
  161. check_uni() {
  162.     char *s;
  163.     char ch;
  164.  
  165.     if (oldoldbufptr != last_uni)
  166.     return;
  167.     while (isSPACE(*last_uni))
  168.     last_uni++;
  169.     for (s = last_uni; isALNUM(*s) || *s == '-'; s++) ;
  170.     ch = *s;
  171.     *s = '\0';
  172.     warn("Warning: Use of \"%s\" without parens is ambiguous", last_uni);
  173.     *s = ch;
  174. }
  175.  
  176. #ifdef CRIPPLED_CC
  177.  
  178. #undef UNI
  179. #undef LOP
  180. #define UNI(f) return uni(f,s)
  181. #define LOP(f) return lop(f,s)
  182.  
  183. int
  184. uni(f,s)
  185. int f;
  186. char *s;
  187. {
  188.     yylval.ival = f;
  189.     expectterm = TRUE;
  190.     bufptr = s;
  191.     last_uni = oldbufptr;
  192.     if (*s == '(')
  193.     return FUNC1;
  194.     s = skipspace(s);
  195.     if (*s == '(')
  196.     return FUNC1;
  197.     else
  198.     return UNIOP;
  199. }
  200.  
  201. int
  202. lop(f,s)
  203. int f;
  204. char *s;
  205. {
  206.     CLINE;
  207.     if (*s != '(')
  208.     s = skipspace(s);
  209.     if (*s == '(') {
  210. #ifdef atarist
  211.     *s = PERL_META('(');
  212. #else
  213.     *s = META('(');
  214. #endif
  215.     bufptr = oldbufptr;
  216.     return '(';
  217.     }
  218.     else {
  219.     yylval.ival=f;
  220.     expectterm = TRUE;
  221.     bufptr = s;
  222.     return LISTOP;
  223.     }
  224. }
  225.  
  226. #endif /* CRIPPLED_CC */
  227.  
  228. int
  229. yylex()
  230. {
  231.     register char *s = bufptr;
  232.     register char *d;
  233.     register int tmp;
  234.     static bool in_format = FALSE;
  235.     static bool firstline = TRUE;
  236.     extern int yychar;        /* last token */
  237.  
  238.     oldoldbufptr = oldbufptr;
  239.     oldbufptr = s;
  240.  
  241.   retry:
  242. #ifdef YYDEBUG
  243.     if (debug & 1)
  244.     if (index(s,'\n'))
  245.         fprintf(stderr,"Tokener at %s",s);
  246.     else
  247.         fprintf(stderr,"Tokener at %s\n",s);
  248. #endif
  249. #ifdef BADSWITCH
  250.     if (*s & 128) {
  251.     if ((*s & 127) == '(') {
  252.         *s++ = '(';
  253.         oldbufptr = s;
  254.     }
  255.     else if ((*s & 127) == '}') {
  256.         *s++ = '}';
  257.         RETURN('}');
  258.     }
  259.     else
  260.         warn("Unrecognized character \\%03o ignored", *s++ & 255);
  261.     goto retry;
  262.     }
  263. #endif
  264.     switch (*s) {
  265.     default:
  266.     if ((*s & 127) == '(') {
  267.         *s++ = '(';
  268.         oldbufptr = s;
  269.     }
  270.     else if ((*s & 127) == '}') {
  271.         *s++ = '}';
  272.         RETURN('}');
  273.     }
  274.     else
  275.         warn("Unrecognized character \\%03o ignored", *s++ & 255);
  276.     goto retry;
  277.     case 4:
  278.     case 26:
  279.     goto fake_eof;            /* emulate EOF on ^D or ^Z */
  280.     case 0:
  281.     if (!rsfp)
  282.         RETURN(0);
  283.     if (s++ < bufend)
  284.         goto retry;            /* ignore stray nulls */
  285.     last_uni = 0;
  286.     if (firstline) {
  287.         firstline = FALSE;
  288.         if (minus_n || minus_p || perldb) {
  289.         str_set(linestr,"");
  290.         if (perldb) {
  291.             char *getenv();
  292.             char *pdb = getenv("PERLDB");
  293.  
  294.             str_cat(linestr, pdb ? pdb : "require 'perldb.pl'");
  295.             str_cat(linestr, ";");
  296.         }
  297.         if (minus_n || minus_p) {
  298.             str_cat(linestr,"line: while (<>) {");
  299.             if (minus_l)
  300.             str_cat(linestr,"chop;");
  301.             if (minus_a)
  302.             str_cat(linestr,"@F=split(' ');");
  303.         }
  304.         oldoldbufptr = oldbufptr = s = str_get(linestr);
  305.         bufend = linestr->str_ptr + linestr->str_cur;
  306.         goto retry;
  307.         }
  308.     }
  309.     if (in_format) {
  310.         bufptr = bufend;
  311.         yylval.formval = load_format();
  312.         in_format = FALSE;
  313.         oldoldbufptr = oldbufptr = s = str_get(linestr) + 1;
  314.         bufend = linestr->str_ptr + linestr->str_cur;
  315.         OPERATOR(FORMLIST);
  316.     }
  317.     curcmd->c_line++;
  318. #ifdef CRYPTSCRIPT
  319.     cryptswitch();
  320. #endif /* CRYPTSCRIPT */
  321.     do {
  322.         if ((s = str_gets(linestr, rsfp, 0)) == Nullch) {
  323.           fake_eof:
  324.         if (rsfp) {
  325.             if (preprocess)
  326.             (void)mypclose(rsfp);
  327.             else if ((FILE*)rsfp == stdin)
  328.             clearerr(stdin);
  329.             else
  330.             (void)fclose(rsfp);
  331.             rsfp = Nullfp;
  332.         }
  333.         if (minus_n || minus_p) {
  334.             str_set(linestr,minus_p ? ";}continue{print" : "");
  335.             str_cat(linestr,";}");
  336.             oldoldbufptr = oldbufptr = s = str_get(linestr);
  337.             bufend = linestr->str_ptr + linestr->str_cur;
  338.             minus_n = minus_p = 0;
  339.             goto retry;
  340.         }
  341.         oldoldbufptr = oldbufptr = s = str_get(linestr);
  342.         str_set(linestr,"");
  343.         RETURN(';');    /* not infinite loop because rsfp is NULL now */
  344.         }
  345.         if (doextract && *linestr->str_ptr == '#')
  346.         doextract = FALSE;
  347.     } while (doextract);
  348.     oldoldbufptr = oldbufptr = bufptr = s;
  349.     if (perldb) {
  350.         STR *str = Str_new(85,0);
  351.  
  352.         str_sset(str,linestr);
  353.         astore(stab_xarray(curcmd->c_filestab),(int)curcmd->c_line,str);
  354.     }
  355. #ifdef DEBUG
  356.     if (firstline) {
  357.         char *showinput();
  358.         s = showinput();
  359.     }
  360. #endif
  361.     bufend = linestr->str_ptr + linestr->str_cur;
  362.     if (curcmd->c_line == 1) {
  363.         if (*s == '#' && s[1] == '!') {
  364.         if (!in_eval && !instr(s,"perl") && instr(origargv[0],"perl")) {
  365.             char **newargv;
  366.             char *cmd;
  367.  
  368.             s += 2;
  369.             if (*s == ' ')
  370.             s++;
  371.             cmd = s;
  372.             while (s < bufend && !isSPACE(*s))
  373.             s++;
  374.             *s++ = '\0';
  375.             while (s < bufend && isSPACE(*s))
  376.             s++;
  377.             if (s < bufend) {
  378.             Newz(899,newargv,origargc+3,char*);
  379.             newargv[1] = s;
  380.             while (s < bufend && !isSPACE(*s))
  381.                 s++;
  382.             *s = '\0';
  383.             Copy(origargv+1, newargv+2, origargc+1, char*);
  384.             }
  385.             else
  386.             newargv = origargv;
  387.             newargv[0] = cmd;
  388. #ifdef DJGPP
  389.             execv(cmd,newargv);
  390. #endif
  391.             fatal("Can't exec %s", cmd);
  392.         }
  393.         }
  394.         else {
  395.         while (s < bufend && isSPACE(*s))
  396.             s++;
  397.         if (*s == ':')    /* for csh's that have to exec sh scripts */
  398.             s++;
  399.         }
  400.     }
  401.     goto retry;
  402.     case ' ': case '\t': case '\f': case '\r': case 013:
  403.     s++;
  404.     goto retry;
  405.     case '#':
  406.     if (preprocess && s == str_get(linestr) &&
  407.            s[1] == ' ' && (isDIGIT(s[2]) || strnEQ(s+2,"line ",5)) ) {
  408.         while (*s && !isDIGIT(*s))
  409.         s++;
  410.         curcmd->c_line = atoi(s)-1;
  411.         while (isDIGIT(*s))
  412.         s++;
  413.         d = bufend;
  414.         while (s < d && isSPACE(*s)) s++;
  415.         s[strlen(s)-1] = '\0';    /* wipe out newline */
  416.         if (*s == '"') {
  417.         s++;
  418.         s[strlen(s)-1] = '\0';    /* wipe out trailing quote */
  419.         }
  420.         if (*s)
  421.         curcmd->c_filestab = fstab(s);
  422.         else
  423.         curcmd->c_filestab = fstab(origfilename);
  424.         oldoldbufptr = oldbufptr = s = str_get(linestr);
  425.     }
  426.     /* FALL THROUGH */
  427.     case '\n':
  428.     if (in_eval && !rsfp) {
  429.         d = bufend;
  430.         while (s < d && *s != '\n')
  431.         s++;
  432.         if (s < d)
  433.         s++;
  434.         if (in_format) {
  435.         bufptr = s;
  436.         yylval.formval = load_format();
  437.         in_format = FALSE;
  438.         oldoldbufptr = oldbufptr = s = bufptr + 1;
  439.         TERM(FORMLIST);
  440.         }
  441.         curcmd->c_line++;
  442.     }
  443.     else {
  444.         *s = '\0';
  445.         bufend = s;
  446.     }
  447.     goto retry;
  448.     case '-':
  449.     if (s[1] && isALPHA(s[1]) && !isALPHA(s[2])) {
  450.         s++;
  451.         last_uni = oldbufptr;
  452.         switch (*s++) {
  453.         case 'r': FTST(O_FTEREAD);
  454.         case 'w': FTST(O_FTEWRITE);
  455.         case 'x': FTST(O_FTEEXEC);
  456.         case 'o': FTST(O_FTEOWNED);
  457.         case 'R': FTST(O_FTRREAD);
  458.         case 'W': FTST(O_FTRWRITE);
  459.         case 'X': FTST(O_FTREXEC);
  460.         case 'O': FTST(O_FTROWNED);
  461.         case 'e': FTST(O_FTIS);
  462.         case 'z': FTST(O_FTZERO);
  463.         case 's': FTST(O_FTSIZE);
  464.         case 'f': FTST(O_FTFILE);
  465.         case 'd': FTST(O_FTDIR);
  466.         case 'l': FTST(O_FTLINK);
  467.         case 'p': FTST(O_FTPIPE);
  468.         case 'S': FTST(O_FTSOCK);
  469.         case 'u': FTST(O_FTSUID);
  470.         case 'g': FTST(O_FTSGID);
  471.         case 'k': FTST(O_FTSVTX);
  472.         case 'b': FTST(O_FTBLK);
  473.         case 'c': FTST(O_FTCHR);
  474.         case 't': FTST(O_FTTTY);
  475.         case 'T': FTST(O_FTTEXT);
  476.         case 'B': FTST(O_FTBINARY);
  477.         case 'M': stabent("\024",TRUE); FTST(O_FTMTIME);
  478.         case 'A': stabent("\024",TRUE); FTST(O_FTATIME);
  479.         case 'C': stabent("\024",TRUE); FTST(O_FTCTIME);
  480.         default:
  481.         s -= 2;
  482.         break;
  483.         }
  484.     }
  485.     tmp = *s++;
  486.     if (*s == tmp) {
  487.         s++;
  488.         RETURN(DEC);
  489.     }
  490.     if (expectterm) {
  491.         if (isSPACE(*s) || !isSPACE(*bufptr))
  492.         check_uni();
  493.         OPERATOR('-');
  494.     }
  495.     else
  496.         AOP(O_SUBTRACT);
  497.     case '+':
  498.     tmp = *s++;
  499.     if (*s == tmp) {
  500.         s++;
  501.         RETURN(INC);
  502.     }
  503.     if (expectterm) {
  504.         if (isSPACE(*s) || !isSPACE(*bufptr))
  505.         check_uni();
  506.         OPERATOR('+');
  507.     }
  508.     else
  509.         AOP(O_ADD);
  510.  
  511.     case '*':
  512.     if (expectterm) {
  513.         check_uni();
  514.         s = scanident(s,bufend,tokenbuf);
  515.         yylval.stabval = stabent(tokenbuf,TRUE);
  516.         TERM(STAR);
  517.     }
  518.     tmp = *s++;
  519.     if (*s == tmp) {
  520.         s++;
  521.         OPERATOR(POW);
  522.     }
  523.     MOP(O_MULTIPLY);
  524.     case '%':
  525.     if (expectterm) {
  526.         if (!isALPHA(s[1]))
  527.         check_uni();
  528.         s = scanident(s,bufend,tokenbuf);
  529.         yylval.stabval = hadd(stabent(tokenbuf,TRUE));
  530.         TERM(HSH);
  531.     }
  532.     s++;
  533.     MOP(O_MODULO);
  534.  
  535.     case '^':
  536.     case '~':
  537.     case '(':
  538.     case ',':
  539.     case ':':
  540.     case '[':
  541.     tmp = *s++;
  542.     OPERATOR(tmp);
  543.     case '{':
  544.     tmp = *s++;
  545.     yylval.ival = curcmd->c_line;
  546.     if (isSPACE(*s) || *s == '#')
  547.         cmdline = NOLINE;   /* invalidate current command line number */
  548.     expectterm = 2;
  549.     RETURN(tmp);
  550.     case ';':
  551.     if (curcmd->c_line < cmdline)
  552.         cmdline = curcmd->c_line;
  553.     tmp = *s++;
  554.     OPERATOR(tmp);
  555.     case ')':
  556.     case ']':
  557.     tmp = *s++;
  558.     TERM(tmp);
  559.     case '}':
  560.     *s |= 128;
  561.     RETURN(';');
  562.     case '&':
  563.     s++;
  564.     tmp = *s++;
  565.     if (tmp == '&')
  566.         OPERATOR(ANDAND);
  567.     s--;
  568.     if (expectterm) {
  569.         d = bufend;
  570.         while (s < d && isSPACE(*s))
  571.         s++;
  572.         if (isALPHA(*s) || *s == '_' || *s == '\'')
  573.         *(--s) = '\\';    /* force next ident to WORD */
  574.         else
  575.         check_uni();
  576.         OPERATOR(AMPER);
  577.     }
  578.     OPERATOR('&');
  579.     case '|':
  580.     s++;
  581.     tmp = *s++;
  582.     if (tmp == '|')
  583.         OPERATOR(OROR);
  584.     s--;
  585.     OPERATOR('|');
  586.     case '=':
  587.     s++;
  588.     tmp = *s++;
  589.     if (tmp == '=')
  590.         EOP(O_EQ);
  591.     if (tmp == '~')
  592.         OPERATOR(MATCH);
  593.     s--;
  594.     OPERATOR('=');
  595.     case '!':
  596.     s++;
  597.     tmp = *s++;
  598.     if (tmp == '=')
  599.         EOP(O_NE);
  600.     if (tmp == '~')
  601.         OPERATOR(NMATCH);
  602.     s--;
  603.     OPERATOR('!');
  604.     case '<':
  605.     if (expectterm) {
  606.         if (s[1] != '<' && !index(s,'>'))
  607.         check_uni();
  608.         s = scanstr(s, SCAN_DEF);
  609.         TERM(RSTRING);
  610.     }
  611.     s++;
  612.     tmp = *s++;
  613.     if (tmp == '<')
  614.         OPERATOR(LS);
  615.     if (tmp == '=') {
  616.         tmp = *s++;
  617.         if (tmp == '>')
  618.         EOP(O_NCMP);
  619.         s--;
  620.         ROP(O_LE);
  621.     }
  622.     s--;
  623.     ROP(O_LT);
  624.     case '>':
  625.     s++;
  626.     tmp = *s++;
  627.     if (tmp == '>')
  628.         OPERATOR(RS);
  629.     if (tmp == '=')
  630.         ROP(O_GE);
  631.     s--;
  632.     ROP(O_GT);
  633.  
  634. #define SNARFWORD \
  635.     d = tokenbuf; \
  636.     while (isALNUM(*s) || *s == '\'') \
  637.         *d++ = *s++; \
  638.     while (d[-1] == '\'') \
  639.         d--,s--; \
  640.     *d = '\0'; \
  641.     d = tokenbuf;
  642.  
  643.     case '$':
  644.     if (s[1] == '#' && (isALPHA(s[2]) || s[2] == '_')) {
  645.         s++;
  646.         s = scanident(s,bufend,tokenbuf);
  647.         yylval.stabval = aadd(stabent(tokenbuf,TRUE));
  648.         TERM(ARYLEN);
  649.     }
  650.     d = s;
  651.     s = scanident(s,bufend,tokenbuf);
  652.     if (reparse) {        /* turn ${foo[bar]} into ($foo[bar]) */
  653.       do_reparse:
  654.         s[-1] = ')';
  655.         s = d;
  656.         s[1] = s[0];
  657.         s[0] = '(';
  658.         goto retry;
  659.     }
  660.     yylval.stabval = stabent(tokenbuf,TRUE);
  661.     expectterm = FALSE;
  662.     if (isSPACE(*s) && oldoldbufptr && oldoldbufptr < bufptr) {
  663.         s++;
  664.         while (isSPACE(*oldoldbufptr))
  665.         oldoldbufptr++;
  666.         if (*oldoldbufptr == 'p' && strnEQ(oldoldbufptr,"print",5)) {
  667.         if (index("&*<%", *s) && isALPHA(s[1]))
  668.             expectterm = TRUE;        /* e.g. print $fh &sub */
  669.         else if (*s == '.' && isDIGIT(s[1]))
  670.             expectterm = TRUE;        /* e.g. print $fh .3 */
  671.         else if (index("/?-+", *s) && !isSPACE(s[1]))
  672.             expectterm = TRUE;        /* e.g. print $fh -1 */
  673.         }
  674.     }
  675.     RETURN(REG);
  676.  
  677.     case '@':
  678.     d = s;
  679.     s = scanident(s,bufend,tokenbuf);
  680.     if (reparse)
  681.         goto do_reparse;
  682.     yylval.stabval = aadd(stabent(tokenbuf,TRUE));
  683.     TERM(ARY);
  684.  
  685.     case '/':            /* may either be division or pattern */
  686.     case '?':            /* may either be conditional or pattern */
  687.     if (expectterm) {
  688.         check_uni();
  689.         s = scanpat(s);
  690.         TERM(PATTERN);
  691.     }
  692.     tmp = *s++;
  693.     if (tmp == '/')
  694.         MOP(O_DIVIDE);
  695.     OPERATOR(tmp);
  696.  
  697.     case '.':
  698.     if (!expectterm || !isDIGIT(s[1])) {
  699.         tmp = *s++;
  700.         if (*s == tmp) {
  701.         s++;
  702.         if (*s == tmp) {
  703.             s++;
  704.             yylval.ival = 0;
  705.         }
  706.         else
  707.             yylval.ival = AF_COMMON;
  708.         OPERATOR(DOTDOT);
  709.         }
  710.         if (expectterm)
  711.         check_uni();
  712.         AOP(O_CONCAT);
  713.     }
  714.     /* FALL THROUGH */
  715.     case '0': case '1': case '2': case '3': case '4':
  716.     case '5': case '6': case '7': case '8': case '9':
  717.     case '\'': case '"': case '`':
  718.     s = scanstr(s, SCAN_DEF);
  719.     TERM(RSTRING);
  720.  
  721.     case '\\':    /* some magic to force next word to be a WORD */
  722.     s++;    /* used by do and sub to force a separate namespace */
  723.     if (!isALPHA(*s) && *s != '_' && *s != '\'') {
  724.         warn("Spurious backslash ignored");
  725.         goto retry;
  726.     }
  727.     /* FALL THROUGH */
  728.     case '_':
  729.     SNARFWORD;
  730.     if (d[1] == '_') {
  731.         if (strEQ(d,"__LINE__") || strEQ(d,"__FILE__")) {
  732.         ARG *arg = op_new(1);
  733.  
  734.         yylval.arg = arg;
  735.         arg->arg_type = O_ITEM;
  736.         if (d[2] == 'L')
  737.             (void)sprintf(tokenbuf,"%ld",(long)curcmd->c_line);
  738.         else
  739.             strcpy(tokenbuf, stab_val(curcmd->c_filestab)->str_ptr);
  740.         arg[1].arg_type = A_SINGLE;
  741.         arg[1].arg_ptr.arg_str = str_make(tokenbuf,strlen(tokenbuf));
  742.         TERM(RSTRING);
  743.         }
  744.         else if (strEQ(d,"__END__")) {
  745.         STAB *stab;
  746.         int fd;
  747.  
  748.         /*SUPPRESS 560*/
  749.         if (!in_eval && (stab = stabent("DATA",FALSE))) {
  750.             stab->str_pok |= SP_MULTI;
  751.             if (!stab_io(stab))
  752.             stab_io(stab) = stio_new();
  753.             stab_io(stab)->ifp = rsfp;
  754. #if defined(HAS_FCNTL) && defined(F_SETFD)
  755.             fd = fileno(rsfp);
  756.             fcntl(fd,F_SETFD,fd >= 3);
  757. #endif
  758.             if (preprocess)
  759.             stab_io(stab)->type = '|';
  760.             else if ((FILE*)rsfp == stdin)
  761.             stab_io(stab)->type = '-';
  762.             else
  763.             stab_io(stab)->type = '<';
  764.             rsfp = Nullfp;
  765.         }
  766.         goto fake_eof;
  767.         }
  768.     }
  769.     break;
  770.     case 'a': case 'A':
  771.     SNARFWORD;
  772.     if (strEQ(d,"alarm"))
  773.         UNI(O_ALARM);
  774.     if (strEQ(d,"accept"))
  775.         FOP22(O_ACCEPT);
  776.     if (strEQ(d,"atan2"))
  777.         FUN2(O_ATAN2);
  778.     break;
  779.     case 'b': case 'B':
  780.     SNARFWORD;
  781.     if (strEQ(d,"bind"))
  782.         FOP2(O_BIND);
  783.     if (strEQ(d,"binmode"))
  784.         FOP(O_BINMODE);
  785.     break;
  786.     case 'c': case 'C':
  787.     SNARFWORD;
  788.     if (strEQ(d,"chop"))
  789.         LFUN(O_CHOP);
  790.     if (strEQ(d,"continue"))
  791.         OPERATOR(CONTINUE);
  792.     if (strEQ(d,"chdir")) {
  793.         (void)stabent("ENV",TRUE);    /* may use HOME */
  794.         UNI(O_CHDIR);
  795.     }
  796.     if (strEQ(d,"close"))
  797.         FOP(O_CLOSE);
  798.     if (strEQ(d,"closedir"))
  799.         FOP(O_CLOSEDIR);
  800.     if (strEQ(d,"cmp"))
  801.         EOP(O_SCMP);
  802.     if (strEQ(d,"caller"))
  803.         UNI(O_CALLER);
  804.     if (strEQ(d,"crypt")) {
  805. #ifdef FCRYPT
  806.         static int cryptseen = 0;
  807.  
  808.         if (!cryptseen++)
  809.         init_des();
  810. #endif
  811.         FUN2(O_CRYPT);
  812.     }
  813.     if (strEQ(d,"chmod"))
  814.         LOP(O_CHMOD);
  815.     if (strEQ(d,"chown"))
  816.         LOP(O_CHOWN);
  817.     if (strEQ(d,"connect"))
  818.         FOP2(O_CONNECT);
  819.     if (strEQ(d,"cos"))
  820.         UNI(O_COS);
  821.     if (strEQ(d,"chroot"))
  822.         UNI(O_CHROOT);
  823.     break;
  824.     case 'd': case 'D':
  825.     SNARFWORD;
  826.     if (strEQ(d,"do")) {
  827.         d = bufend;
  828.         while (s < d && isSPACE(*s))
  829.         s++;
  830.         if (isALPHA(*s) || *s == '_')
  831.         *(--s) = '\\';    /* force next ident to WORD */
  832.         OPERATOR(DO);
  833.     }
  834.     if (strEQ(d,"die"))
  835.         LOP(O_DIE);
  836.     if (strEQ(d,"defined"))
  837.         LFUN(O_DEFINED);
  838.     if (strEQ(d,"delete"))
  839.         OPERATOR(DELETE);
  840.     if (strEQ(d,"dbmopen"))
  841.         HFUN3(O_DBMOPEN);
  842.     if (strEQ(d,"dbmclose"))
  843.         HFUN(O_DBMCLOSE);
  844.     if (strEQ(d,"dump"))
  845.         LOOPX(O_DUMP);
  846.     break;
  847.     case 'e': case 'E':
  848.     SNARFWORD;
  849.     if (strEQ(d,"else"))
  850.         OPERATOR(ELSE);
  851.     if (strEQ(d,"elsif")) {
  852.         yylval.ival = curcmd->c_line;
  853.         OPERATOR(ELSIF);
  854.     }
  855.     if (strEQ(d,"eq") || strEQ(d,"EQ"))
  856.         EOP(O_SEQ);
  857.     if (strEQ(d,"exit"))
  858.         UNI(O_EXIT);
  859.     if (strEQ(d,"eval")) {
  860.         allstabs = TRUE;        /* must initialize everything since */
  861.         UNI(O_EVAL);        /* we don't know what will be used */
  862.     }
  863.     if (strEQ(d,"eof"))
  864.         FOP(O_EOF);
  865.     if (strEQ(d,"exp"))
  866.         UNI(O_EXP);
  867.     if (strEQ(d,"each"))
  868.         HFUN(O_EACH);
  869.     if (strEQ(d,"exec")) {
  870.         set_csh();
  871.         LOP(O_EXEC_OP);
  872.     }
  873.     if (strEQ(d,"endhostent"))
  874.         FUN0(O_EHOSTENT);
  875.     if (strEQ(d,"endnetent"))
  876.         FUN0(O_ENETENT);
  877.     if (strEQ(d,"endservent"))
  878.         FUN0(O_ESERVENT);
  879.     if (strEQ(d,"endprotoent"))
  880.         FUN0(O_EPROTOENT);
  881.     if (strEQ(d,"endpwent"))
  882.         FUN0(O_EPWENT);
  883.     if (strEQ(d,"endgrent"))
  884.         FUN0(O_EGRENT);
  885.     break;
  886.     case 'f': case 'F':
  887.     SNARFWORD;
  888.     if (strEQ(d,"for") || strEQ(d,"foreach")) {
  889.         yylval.ival = curcmd->c_line;
  890.         while (s < bufend && isSPACE(*s))
  891.         s++;
  892.         if (isALPHA(*s))
  893.         fatal("Missing $ on loop variable");
  894.         OPERATOR(FOR);
  895.     }
  896.     if (strEQ(d,"format")) {
  897.         d = bufend;
  898.         while (s < d && isSPACE(*s))
  899.         s++;
  900.         if (isALPHA(*s) || *s == '_')
  901.         *(--s) = '\\';    /* force next ident to WORD */
  902.         in_format = TRUE;
  903.         allstabs = TRUE;        /* must initialize everything since */
  904.         OPERATOR(FORMAT);        /* we don't know what will be used */
  905.     }
  906.     if (strEQ(d,"fork"))
  907.         FUN0(O_FORK);
  908.     if (strEQ(d,"fcntl"))
  909.         FOP3(O_FCNTL);
  910.     if (strEQ(d,"fileno"))
  911.         FOP(O_FILENO);
  912.     if (strEQ(d,"flock"))
  913.         FOP2(O_FLOCK);
  914.     break;
  915.     case 'g': case 'G':
  916.     SNARFWORD;
  917.     if (strEQ(d,"gt") || strEQ(d,"GT"))
  918.         ROP(O_SGT);
  919.     if (strEQ(d,"ge") || strEQ(d,"GE"))
  920.         ROP(O_SGE);
  921.     if (strEQ(d,"grep"))
  922.         FL2(O_GREP);
  923.     if (strEQ(d,"goto"))
  924.         LOOPX(O_GOTO);
  925.     if (strEQ(d,"gmtime"))
  926.         UNI(O_GMTIME);
  927.     if (strEQ(d,"getc"))
  928.         FOP(O_GETC);
  929.     if (strnEQ(d,"get",3)) {
  930.         d += 3;
  931.         if (*d == 'p') {
  932.         if (strEQ(d,"ppid"))
  933.             FUN0(O_GETPPID);
  934.         if (strEQ(d,"pgrp"))
  935.             UNI(O_GETPGRP);
  936.         if (strEQ(d,"priority"))
  937.             FUN2(O_GETPRIORITY);
  938.         if (strEQ(d,"protobyname"))
  939.             UNI(O_GPBYNAME);
  940.         if (strEQ(d,"protobynumber"))
  941.             FUN1(O_GPBYNUMBER);
  942.         if (strEQ(d,"protoent"))
  943.             FUN0(O_GPROTOENT);
  944.         if (strEQ(d,"pwent"))
  945.             FUN0(O_GPWENT);
  946.         if (strEQ(d,"pwnam"))
  947.             FUN1(O_GPWNAM);
  948.         if (strEQ(d,"pwuid"))
  949.             FUN1(O_GPWUID);
  950.         if (strEQ(d,"peername"))
  951.             FOP(O_GETPEERNAME);
  952.         }
  953.         else if (*d == 'h') {
  954.         if (strEQ(d,"hostbyname"))
  955.             UNI(O_GHBYNAME);
  956.         if (strEQ(d,"hostbyaddr"))
  957.             FUN2(O_GHBYADDR);
  958.         if (strEQ(d,"hostent"))
  959.             FUN0(O_GHOSTENT);
  960.         }
  961.         else if (*d == 'n') {
  962.         if (strEQ(d,"netbyname"))
  963.             UNI(O_GNBYNAME);
  964.         if (strEQ(d,"netbyaddr"))
  965.             FUN2(O_GNBYADDR);
  966.         if (strEQ(d,"netent"))
  967.             FUN0(O_GNETENT);
  968.         }
  969.         else if (*d == 's') {
  970.         if (strEQ(d,"servbyname"))
  971.             FUN2(O_GSBYNAME);
  972.         if (strEQ(d,"servbyport"))
  973.             FUN2(O_GSBYPORT);
  974.         if (strEQ(d,"servent"))
  975.             FUN0(O_GSERVENT);
  976.         if (strEQ(d,"sockname"))
  977.             FOP(O_GETSOCKNAME);
  978.         if (strEQ(d,"sockopt"))
  979.             FOP3(O_GSOCKOPT);
  980.         }
  981.         else if (*d == 'g') {
  982.         if (strEQ(d,"grent"))
  983.             FUN0(O_GGRENT);
  984.         if (strEQ(d,"grnam"))
  985.             FUN1(O_GGRNAM);
  986.         if (strEQ(d,"grgid"))
  987.             FUN1(O_GGRGID);
  988.         }
  989.         else if (*d == 'l') {
  990.         if (strEQ(d,"login"))
  991.             FUN0(O_GETLOGIN);
  992.         }
  993.         d -= 3;
  994.     }
  995.     break;
  996.     case 'h': case 'H':
  997.     SNARFWORD;
  998.     if (strEQ(d,"hex"))
  999.         UNI(O_HEX);
  1000.     break;
  1001.     case 'i': case 'I':
  1002.     SNARFWORD;
  1003.     if (strEQ(d,"if")) {
  1004.         yylval.ival = curcmd->c_line;
  1005.         OPERATOR(IF);
  1006.     }
  1007.     if (strEQ(d,"index"))
  1008.         FUN2x(O_INDEX);
  1009.     if (strEQ(d,"int"))
  1010.         UNI(O_INT);
  1011.     if (strEQ(d,"ioctl"))
  1012.         FOP3(O_IOCTL);
  1013.     break;
  1014.     case 'j': case 'J':
  1015.     SNARFWORD;
  1016.     if (strEQ(d,"join"))
  1017.         FL2(O_JOIN);
  1018.     break;
  1019.     case 'k': case 'K':
  1020.     SNARFWORD;
  1021.     if (strEQ(d,"keys"))
  1022.         HFUN(O_KEYS);
  1023.     if (strEQ(d,"kill"))
  1024.         LOP(O_KILL);
  1025.     break;
  1026.     case 'l': case 'L':
  1027.     SNARFWORD;
  1028.     if (strEQ(d,"last"))
  1029.         LOOPX(O_LAST);
  1030.     if (strEQ(d,"local"))
  1031.         OPERATOR(LOCAL);
  1032.     if (strEQ(d,"length"))
  1033.         UNI(O_LENGTH);
  1034.     if (strEQ(d,"lt") || strEQ(d,"LT"))
  1035.         ROP(O_SLT);
  1036.     if (strEQ(d,"le") || strEQ(d,"LE"))
  1037.         ROP(O_SLE);
  1038.     if (strEQ(d,"localtime"))
  1039.         UNI(O_LOCALTIME);
  1040.     if (strEQ(d,"log"))
  1041.         UNI(O_LOG);
  1042.     if (strEQ(d,"link"))
  1043.         FUN2(O_LINK);
  1044.     if (strEQ(d,"listen"))
  1045.         FOP2(O_LISTEN);
  1046.     if (strEQ(d,"lstat"))
  1047.         FOP(O_LSTAT);
  1048.     break;
  1049.     case 'm': case 'M':
  1050.     if (s[1] == '\'') {
  1051.         d = "m";
  1052.         s++;
  1053.     }
  1054.     else {
  1055.         SNARFWORD;
  1056.     }
  1057.     if (strEQ(d,"m")) {
  1058.         s = scanpat(s-1);
  1059.         if (yylval.arg)
  1060.         TERM(PATTERN);
  1061.         else
  1062.         RETURN(1);    /* force error */
  1063.     }
  1064.     switch (d[1]) {
  1065.     case 'k':
  1066.         if (strEQ(d,"mkdir"))
  1067.         FUN2(O_MKDIR);
  1068.         break;
  1069.     case 's':
  1070.         if (strEQ(d,"msgctl"))
  1071.         FUN3(O_MSGCTL);
  1072.         if (strEQ(d,"msgget"))
  1073.         FUN2(O_MSGGET);
  1074.         if (strEQ(d,"msgrcv"))
  1075.         FUN5(O_MSGRCV);
  1076.         if (strEQ(d,"msgsnd"))
  1077.         FUN3(O_MSGSND);
  1078.         break;
  1079.     }
  1080.     break;
  1081.     case 'n': case 'N':
  1082.     SNARFWORD;
  1083.     if (strEQ(d,"next"))
  1084.         LOOPX(O_NEXT);
  1085.     if (strEQ(d,"ne") || strEQ(d,"NE"))
  1086.         EOP(O_SNE);
  1087.     break;
  1088.     case 'o': case 'O':
  1089.     SNARFWORD;
  1090.     if (strEQ(d,"open"))
  1091.         OPERATOR(OPEN);
  1092.     if (strEQ(d,"ord"))
  1093.         UNI(O_ORD);
  1094.     if (strEQ(d,"oct"))
  1095.         UNI(O_OCT);
  1096.     if (strEQ(d,"opendir"))
  1097.         FOP2(O_OPEN_DIR);
  1098.     break;
  1099.     case 'p': case 'P':
  1100.     SNARFWORD;
  1101.     if (strEQ(d,"print")) {
  1102.         checkcomma(s,d,"filehandle");
  1103.         LOP(O_PRINT);
  1104.     }
  1105.     if (strEQ(d,"printf")) {
  1106.         checkcomma(s,d,"filehandle");
  1107.         LOP(O_PRTF);
  1108.     }
  1109.     if (strEQ(d,"push")) {
  1110.         yylval.ival = O_PUSH;
  1111.         OPERATOR(PUSH);
  1112.     }
  1113.     if (strEQ(d,"pop"))
  1114.         OPERATOR(POP);
  1115.     if (strEQ(d,"pack"))
  1116.         FL2(O_PACK);
  1117.     if (strEQ(d,"package"))
  1118.         OPERATOR(PACKAGE);
  1119.     if (strEQ(d,"pipe"))
  1120.         FOP22(O_PIPE_OP);
  1121.     break;
  1122.     case 'q': case 'Q':
  1123.     SNARFWORD;
  1124.     if (strEQ(d,"q")) {
  1125.         s = scanstr(s-1, SCAN_DEF);
  1126.         TERM(RSTRING);
  1127.     }
  1128.     if (strEQ(d,"qq")) {
  1129.         s = scanstr(s-2, SCAN_DEF);
  1130.         TERM(RSTRING);
  1131.     }
  1132.     if (strEQ(d,"qx")) {
  1133.         s = scanstr(s-2, SCAN_DEF);
  1134.         TERM(RSTRING);
  1135.     }
  1136.     break;
  1137.     case 'r': case 'R':
  1138.     SNARFWORD;
  1139.     if (strEQ(d,"return"))
  1140.         OLDLOP(O_RETURN);
  1141.     if (strEQ(d,"require")) {
  1142.         allstabs = TRUE;        /* must initialize everything since */
  1143.         UNI(O_REQUIRE);        /* we don't know what will be used */
  1144.     }
  1145.     if (strEQ(d,"reset"))
  1146.         UNI(O_RESET);
  1147.     if (strEQ(d,"redo"))
  1148.         LOOPX(O_REDO);
  1149.     if (strEQ(d,"rename"))
  1150.         FUN2(O_RENAME);
  1151.     if (strEQ(d,"rand"))
  1152.         UNI(O_RAND);
  1153.     if (strEQ(d,"rmdir"))
  1154.         UNI(O_RMDIR);
  1155.     if (strEQ(d,"rindex"))
  1156.         FUN2x(O_RINDEX);
  1157.     if (strEQ(d,"read"))
  1158.         FOP3(O_READ);
  1159.     if (strEQ(d,"readdir"))
  1160.         FOP(O_READDIR);
  1161.     if (strEQ(d,"rewinddir"))
  1162.         FOP(O_REWINDDIR);
  1163.     if (strEQ(d,"recv"))
  1164.         FOP4(O_RECV);
  1165.     if (strEQ(d,"reverse"))
  1166.         LOP(O_REVERSE);
  1167.     if (strEQ(d,"readlink"))
  1168.         UNI(O_READLINK);
  1169.     break;
  1170.     case 's': case 'S':
  1171.     if (s[1] == '\'') {
  1172.         d = "s";
  1173.         s++;
  1174.     }
  1175.     else {
  1176.         SNARFWORD;
  1177.     }
  1178.     if (strEQ(d,"s")) {
  1179.         s = scansubst(s);
  1180.         if (yylval.arg)
  1181.         TERM(SUBST);
  1182.         else
  1183.         RETURN(1);    /* force error */
  1184.     }
  1185.     switch (d[1]) {
  1186.     case 'a':
  1187.     case 'b':
  1188.         break;
  1189.     case 'c':
  1190.         if (strEQ(d,"scalar"))
  1191.         UNI(O_SCALAR);
  1192.         break;
  1193.     case 'd':
  1194.         break;
  1195.     case 'e':
  1196.         if (strEQ(d,"select"))
  1197.         OPERATOR(SSELECT);
  1198.         if (strEQ(d,"seek"))
  1199.         FOP3(O_SEEK);
  1200.         if (strEQ(d,"semctl"))
  1201.         FUN4(O_SEMCTL);
  1202.         if (strEQ(d,"semget"))
  1203.         FUN3(O_SEMGET);
  1204.         if (strEQ(d,"semop"))
  1205.         FUN2(O_SEMOP);
  1206.         if (strEQ(d,"send"))
  1207.         FOP3(O_SEND);
  1208.         if (strEQ(d,"setpgrp"))
  1209.         FUN2(O_SETPGRP);
  1210.         if (strEQ(d,"setpriority"))
  1211.         FUN3(O_SETPRIORITY);
  1212.         if (strEQ(d,"sethostent"))
  1213.         FUN1(O_SHOSTENT);
  1214.         if (strEQ(d,"setnetent"))
  1215.         FUN1(O_SNETENT);
  1216.         if (strEQ(d,"setservent"))
  1217.         FUN1(O_SSERVENT);
  1218.         if (strEQ(d,"setprotoent"))
  1219.         FUN1(O_SPROTOENT);
  1220.         if (strEQ(d,"setpwent"))
  1221.         FUN0(O_SPWENT);
  1222.         if (strEQ(d,"setgrent"))
  1223.         FUN0(O_SGRENT);
  1224.         if (strEQ(d,"seekdir"))
  1225.         FOP2(O_SEEKDIR);
  1226.         if (strEQ(d,"setsockopt"))
  1227.         FOP4(O_SSOCKOPT);
  1228.         break;
  1229.     case 'f':
  1230.     case 'g':
  1231.         break;
  1232.     case 'h':
  1233.         if (strEQ(d,"shift"))
  1234.         TERM(SHIFT);
  1235.         if (strEQ(d,"shmctl"))
  1236.         FUN3(O_SHMCTL);
  1237.         if (strEQ(d,"shmget"))
  1238.         FUN3(O_SHMGET);
  1239.         if (strEQ(d,"shmread"))
  1240.         FUN4(O_SHMREAD);
  1241.         if (strEQ(d,"shmwrite"))
  1242.         FUN4(O_SHMWRITE);
  1243.         if (strEQ(d,"shutdown"))
  1244.         FOP2(O_SHUTDOWN);
  1245.         break;
  1246.     case 'i':
  1247.         if (strEQ(d,"sin"))
  1248.         UNI(O_SIN);
  1249.         break;
  1250.     case 'j':
  1251.     case 'k':
  1252.         break;
  1253.     case 'l':
  1254.         if (strEQ(d,"sleep"))
  1255.         UNI(O_SLEEP);
  1256.         break;
  1257.     case 'm':
  1258.     case 'n':
  1259.         break;
  1260.     case 'o':
  1261.         if (strEQ(d,"socket"))
  1262.         FOP4(O_SOCKET);
  1263.         if (strEQ(d,"socketpair"))
  1264.         FOP25(O_SOCKPAIR);
  1265.         if (strEQ(d,"sort")) {
  1266.         checkcomma(s,d,"subroutine name");
  1267.         d = bufend;
  1268.         while (s < d && isSPACE(*s)) s++;
  1269.         if (*s == ';' || *s == ')')        /* probably a close */
  1270.             fatal("sort is now a reserved word");
  1271.         if (isALPHA(*s) || *s == '_') {
  1272.             /*SUPPRESS 530*/
  1273.             for (d = s; isALNUM(*d); d++) ;
  1274.             strncpy(tokenbuf,s,d-s);
  1275.             tokenbuf[d-s] = '\0';
  1276.             if (strNE(tokenbuf,"keys") &&
  1277.             strNE(tokenbuf,"values") &&
  1278.             strNE(tokenbuf,"split") &&
  1279.             strNE(tokenbuf,"grep") &&
  1280.             strNE(tokenbuf,"readdir") &&
  1281.             strNE(tokenbuf,"unpack") &&
  1282.             strNE(tokenbuf,"do") &&
  1283.             strNE(tokenbuf,"eval") &&
  1284.             (d >= bufend || isSPACE(*d)) )
  1285.             *(--s) = '\\';    /* force next ident to WORD */
  1286.         }
  1287.         LOP(O_SORT);
  1288.         }
  1289.         break;
  1290.     case 'p':
  1291.         if (strEQ(d,"split"))
  1292.         TERM(SPLIT);
  1293.         if (strEQ(d,"sprintf"))
  1294.         FL(O_SPRINTF);
  1295.         if (strEQ(d,"splice")) {
  1296.         yylval.ival = O_SPLICE;
  1297.         OPERATOR(PUSH);
  1298.         }
  1299.         break;
  1300.     case 'q':
  1301.         if (strEQ(d,"sqrt"))
  1302.         UNI(O_SQRT);
  1303.         break;
  1304.     case 'r':
  1305.         if (strEQ(d,"srand"))
  1306.         UNI(O_SRAND);
  1307.         break;
  1308.     case 's':
  1309.         break;
  1310.     case 't':
  1311.         if (strEQ(d,"stat"))
  1312.         FOP(O_STAT);
  1313.         if (strEQ(d,"study")) {
  1314.         sawstudy++;
  1315.         LFUN(O_STUDY);
  1316.         }
  1317.         break;
  1318.     case 'u':
  1319.         if (strEQ(d,"substr"))
  1320.         FUN2x(O_SUBSTR);
  1321.         if (strEQ(d,"sub")) {
  1322.         yylval.ival = savestack->ary_fill; /* restore stuff on reduce */
  1323.         savelong(&subline);
  1324.         saveitem(subname);
  1325.  
  1326.         subline = curcmd->c_line;
  1327.         d = bufend;
  1328.         while (s < d && isSPACE(*s))
  1329.             s++;
  1330.         if (isALPHA(*s) || *s == '_' || *s == '\'') {
  1331.             str_sset(subname,curstname);
  1332.             str_ncat(subname,"'",1);
  1333.             for (d = s+1; isALNUM(*d) || *d == '\''; d++)
  1334.             /*SUPPRESS 530*/
  1335.             ;
  1336.             if (d[-1] == '\'')
  1337.             d--;
  1338.             str_ncat(subname,s,d-s);
  1339.             *(--s) = '\\';    /* force next ident to WORD */
  1340.         }
  1341.         else
  1342.             str_set(subname,"?");
  1343.         OPERATOR(SUB);
  1344.         }
  1345.         break;
  1346.     case 'v':
  1347.     case 'w':
  1348.     case 'x':
  1349.         break;
  1350.     case 'y':
  1351.         if (strEQ(d,"system")) {
  1352.         set_csh();
  1353.         LOP(O_SYSTEM);
  1354.         }
  1355.         if (strEQ(d,"symlink"))
  1356.         FUN2(O_SYMLINK);
  1357.         if (strEQ(d,"syscall"))
  1358.         LOP(O_SYSCALL);
  1359.         if (strEQ(d,"sysread"))
  1360.         FOP3(O_SYSREAD);
  1361.         if (strEQ(d,"syswrite"))
  1362.         FOP3(O_SYSWRITE);
  1363.         break;
  1364.     case 'z':
  1365.         break;
  1366.     }
  1367.     break;
  1368.     case 't': case 'T':
  1369.     SNARFWORD;
  1370.     if (strEQ(d,"tr")) {
  1371.         s = scantrans(s);
  1372.         if (yylval.arg)
  1373.         TERM(TRANS);
  1374.         else
  1375.         RETURN(1);    /* force error */
  1376.     }
  1377.     if (strEQ(d,"tell"))
  1378.         FOP(O_TELL);
  1379.     if (strEQ(d,"telldir"))
  1380.         FOP(O_TELLDIR);
  1381.     if (strEQ(d,"time"))
  1382.         FUN0(O_TIME);
  1383.     if (strEQ(d,"times"))
  1384.         FUN0(O_TMS);
  1385.     if (strEQ(d,"truncate"))
  1386.         FOP2(O_TRUNCATE);
  1387.     break;
  1388.     case 'u': case 'U':
  1389.     SNARFWORD;
  1390.     if (strEQ(d,"using"))
  1391.         OPERATOR(USING);
  1392.     if (strEQ(d,"until")) {
  1393.         yylval.ival = curcmd->c_line;
  1394.         OPERATOR(UNTIL);
  1395.     }
  1396.     if (strEQ(d,"unless")) {
  1397.         yylval.ival = curcmd->c_line;
  1398.         OPERATOR(UNLESS);
  1399.     }
  1400.     if (strEQ(d,"unlink"))
  1401.         LOP(O_UNLINK);
  1402.     if (strEQ(d,"undef"))
  1403.         LFUN(O_UNDEF);
  1404.     if (strEQ(d,"unpack"))
  1405.         FUN2(O_UNPACK);
  1406.     if (strEQ(d,"utime"))
  1407.         LOP(O_UTIME);
  1408.     if (strEQ(d,"umask"))
  1409.         UNI(O_UMASK);
  1410.     if (strEQ(d,"unshift")) {
  1411.         yylval.ival = O_UNSHIFT;
  1412.         OPERATOR(PUSH);
  1413.     }
  1414.     break;
  1415.     case 'v': case 'V':
  1416.     SNARFWORD;
  1417.     if (strEQ(d,"values"))
  1418.         HFUN(O_VALUES);
  1419.     if (strEQ(d,"vec")) {
  1420.         sawvec = TRUE;
  1421.         FUN3(O_VEC);
  1422.     }
  1423.     break;
  1424.     case 'w': case 'W':
  1425.     SNARFWORD;
  1426.     if (strEQ(d,"while")) {
  1427.         yylval.ival = curcmd->c_line;
  1428.         OPERATOR(WHILE);
  1429.     }
  1430.     if (strEQ(d,"warn"))
  1431.         LOP(O_WARN);
  1432.     if (strEQ(d,"wait"))
  1433.         FUN0(O_WAIT);
  1434.     if (strEQ(d,"waitpid"))
  1435.         FUN2(O_WAITPID);
  1436.     if (strEQ(d,"wantarray")) {
  1437.         yylval.arg = op_new(1);
  1438.         yylval.arg->arg_type = O_ITEM;
  1439.         yylval.arg[1].arg_type = A_WANTARRAY;
  1440.         TERM(RSTRING);
  1441.     }
  1442.     if (strEQ(d,"write"))
  1443.         FOP(O_WRITE);
  1444.     break;
  1445.     case 'x': case 'X':
  1446.     if (*s == 'x' && isDIGIT(s[1]) && !expectterm) {
  1447.         s++;
  1448.         MOP(O_REPEAT);
  1449.     }
  1450.     SNARFWORD;
  1451.     if (strEQ(d,"x")) {
  1452.         if (!expectterm)
  1453.         MOP(O_REPEAT);
  1454.         check_uni();
  1455.     }
  1456.     break;
  1457.     case 'y': case 'Y':
  1458.     if (s[1] == '\'') {
  1459.         d = "y";
  1460.         s++;
  1461.     }
  1462.     else {
  1463.         SNARFWORD;
  1464.     }
  1465.     if (strEQ(d,"y")) {
  1466.         s = scantrans(s);
  1467.         TERM(TRANS);
  1468.     }
  1469.     break;
  1470.     case 'z': case 'Z':
  1471.     SNARFWORD;
  1472.     break;
  1473.     }
  1474.     yylval.cval = savestr(d);
  1475.     if (expectterm == 2) {        /* special case: start of statement */
  1476.     while (isSPACE(*s)) s++;
  1477.     if (*s == ':') {
  1478.         s++;
  1479.         CLINE;
  1480.         OPERATOR(LABEL);
  1481.     }
  1482.     TERM(WORD);
  1483.     }
  1484.     expectterm = FALSE;
  1485.     if (oldoldbufptr && oldoldbufptr < bufptr) {
  1486.     while (isSPACE(*oldoldbufptr))
  1487.         oldoldbufptr++;
  1488.     if (*oldoldbufptr == 'p' && strnEQ(oldoldbufptr,"print",5))
  1489.         expectterm = TRUE;
  1490.     else if (*oldoldbufptr == 's' && strnEQ(oldoldbufptr,"sort",4))
  1491.         expectterm = TRUE;
  1492.     }
  1493.     return (CLINE, bufptr = s, (int)WORD);
  1494. }
  1495.  
  1496. void
  1497. checkcomma(s,name,what)
  1498. register char *s;
  1499. char *name;
  1500. char *what;
  1501. {
  1502.     char *w;
  1503.  
  1504.     if (dowarn && *s == ' ' && s[1] == '(') {
  1505.     w = index(s,')');
  1506.     if (w)
  1507.         for (w++; *w && isSPACE(*w); w++) ;
  1508.     if (!w || !*w || !index(";|}", *w))    /* an advisory hack only... */
  1509.         warn("%s (...) interpreted as function",name);
  1510.     }
  1511.     while (s < bufend && isSPACE(*s))
  1512.     s++;
  1513.     if (*s == '(')
  1514.     s++;
  1515.     while (s < bufend && isSPACE(*s))
  1516.     s++;
  1517.     if (isALPHA(*s) || *s == '_') {
  1518.     w = s++;
  1519.     while (isALNUM(*s))
  1520.         s++;
  1521.     while (s < bufend && isSPACE(*s))
  1522.         s++;
  1523.     if (*s == ',') {
  1524.         *s = '\0';
  1525.         w = instr(
  1526.           "tell eof times getlogin wait length shift umask getppid \
  1527.           cos exp int log rand sin sqrt ord wantarray",
  1528.           w);
  1529.         *s = ',';
  1530.         if (w)
  1531.         return;
  1532.         fatal("No comma allowed after %s", what);
  1533.     }
  1534.     }
  1535. }
  1536.  
  1537. char *
  1538. scanident(s,send,dest)
  1539. register char *s;
  1540. register char *send;
  1541. char *dest;
  1542. {
  1543.     register char *d;
  1544.     int brackets = 0;
  1545.  
  1546.     reparse = Nullch;
  1547.     s++;
  1548.     d = dest;
  1549.     if (isDIGIT(*s)) {
  1550.     while (isDIGIT(*s))
  1551.         *d++ = *s++;
  1552.     }
  1553.     else {
  1554.     while (isALNUM(*s) || *s == '\'')
  1555.         *d++ = *s++;
  1556.     }
  1557.     while (d > dest+1 && d[-1] == '\'')
  1558.     d--,s--;
  1559.     *d = '\0';
  1560.     d = dest;
  1561.     if (!*d) {
  1562.     *d = *s++;
  1563.     if (*d == '{' /* } */ ) {
  1564.         d = dest;
  1565.         brackets++;
  1566.         while (s < send && brackets) {
  1567.         if (!reparse && (d == dest || (*s && isALNUM(*s) ))) {
  1568.             *d++ = *s++;
  1569.             continue;
  1570.         }
  1571.         else if (!reparse)
  1572.             reparse = s;
  1573.         switch (*s++) {
  1574.         /* { */
  1575.         case '}':
  1576.             brackets--;
  1577.             if (reparse && reparse == s - 1)
  1578.             reparse = Nullch;
  1579.             break;
  1580.         case '{':   /* } */
  1581.             brackets++;
  1582.             break;
  1583.         }
  1584.         }
  1585.         *d = '\0';
  1586.         d = dest;
  1587.     }
  1588.     else
  1589.         d[1] = '\0';
  1590.     }
  1591.     if (*d == '^' && (isUPPER(*s) || index("[\\]^_?", *s))) {
  1592. #ifdef DEBUGGING
  1593.     if (*s == 'D')
  1594.         debug |= 32768;
  1595. #endif
  1596.     *d = *s++ ^ 64;
  1597.     }
  1598.     return s;
  1599. }
  1600.  
  1601. void
  1602. scanconst(spat,string,len)
  1603. SPAT *spat;
  1604. char *string;
  1605. int len;
  1606. {
  1607.     register STR *tmpstr;
  1608.     register char *t;
  1609.     register char *d;
  1610.     register char *e;
  1611.     char *origstring = string;
  1612.     static char *vert = "|";
  1613.  
  1614.     if (ninstr(string, string+len, vert, vert+1))
  1615.     return;
  1616.     if (*string == '^')
  1617.     string++, len--;
  1618.     tmpstr = Str_new(86,len);
  1619.     str_nset(tmpstr,string,len);
  1620.     t = str_get(tmpstr);
  1621.     e = t + len;
  1622.     tmpstr->str_u.str_useful = 100;
  1623.     for (d=t; d < e; ) {
  1624.     switch (*d) {
  1625.     case '{':
  1626.         if (isDIGIT(d[1]))
  1627.         e = d;
  1628.         else
  1629.         goto defchar;
  1630.         break;
  1631.     case '.': case '[': case '$': case '(': case ')': case '|': case '+':
  1632.     case '^':
  1633.         e = d;
  1634.         break;
  1635.     case '\\':
  1636.         if (d[1] && index("wWbB0123456789sSdDlLuUExc",d[1])) {
  1637.         e = d;
  1638.         break;
  1639.         }
  1640.         Move(d+1,d,e-d,char);
  1641.         e--;
  1642.         switch(*d) {
  1643.         case 'n':
  1644.         *d = '\n';
  1645.         break;
  1646.         case 't':
  1647.         *d = '\t';
  1648.         break;
  1649.         case 'f':
  1650.         *d = '\f';
  1651.         break;
  1652.         case 'r':
  1653.         *d = '\r';
  1654.         break;
  1655.         case 'e':
  1656.         *d = '\033';
  1657.         break;
  1658.         case 'a':
  1659.         *d = '\007';
  1660.         break;
  1661.         }
  1662.         /* FALL THROUGH */
  1663.     default:
  1664.       defchar:
  1665.         if (d[1] == '*' || (d[1] == '{' && d[2] == '0') || d[1] == '?') {
  1666.         e = d;
  1667.         break;
  1668.         }
  1669.         d++;
  1670.     }
  1671.     }
  1672.     if (d == t) {
  1673.     str_free(tmpstr);
  1674.     return;
  1675.     }
  1676.     *d = '\0';
  1677.     tmpstr->str_cur = d - t;
  1678.     if (d == t+len)
  1679.     spat->spat_flags |= SPAT_ALL;
  1680.     if (*origstring != '^')
  1681.     spat->spat_flags |= SPAT_SCANFIRST;
  1682.     spat->spat_short = tmpstr;
  1683.     spat->spat_slen = d - t;
  1684. }
  1685.  
  1686. char *
  1687. scanpat(s)
  1688. register char *s;
  1689. {
  1690.     register SPAT *spat;
  1691.     register char *d;
  1692.     register char *e;
  1693.     int len;
  1694.     SPAT savespat;
  1695.     STR *str = Str_new(93,0);
  1696.     char delim;
  1697.  
  1698.     Newz(801,spat,1,SPAT);
  1699.     spat->spat_next = curstash->tbl_spatroot;    /* link into spat list */
  1700.     curstash->tbl_spatroot = spat;
  1701.  
  1702.     switch (*s++) {
  1703.     case 'm':
  1704.     s++;
  1705.     break;
  1706.     case '/':
  1707.     break;
  1708.     case '?':
  1709.     spat->spat_flags |= SPAT_ONCE;
  1710.     break;
  1711.     default:
  1712.     fatal("panic: scanpat");
  1713.     }
  1714.     s = str_append_till(str,s,bufend,s[-1],patleave);
  1715.     if (s >= bufend) {
  1716.     str_free(str);
  1717.     yyerror("Search pattern not terminated");
  1718.     yylval.arg = Nullarg;
  1719.     return s;
  1720.     }
  1721.     delim = *s++;
  1722.     while (*s == 'i' || *s == 'o' || *s == 'g') {
  1723.     if (*s == 'i') {
  1724.         s++;
  1725.         sawi = TRUE;
  1726.         spat->spat_flags |= SPAT_FOLD;
  1727.     }
  1728.     if (*s == 'o') {
  1729.         s++;
  1730.         spat->spat_flags |= SPAT_KEEP;
  1731.     }
  1732.     if (*s == 'g') {
  1733.         s++;
  1734.         spat->spat_flags |= SPAT_GLOBAL;
  1735.     }
  1736.     }
  1737.     len = str->str_cur;
  1738.     e = str->str_ptr + len;
  1739.     if (delim == '\'')
  1740.     d = e;
  1741.     else
  1742.     d = str->str_ptr;
  1743.     for (; d < e; d++) {
  1744.     if (*d == '\\')
  1745.         d++;
  1746.     else if ((*d == '$' && d[1] && d[1] != '|' && d[1] != ')') ||
  1747.          (*d == '@')) {
  1748.         register ARG *arg;
  1749.  
  1750.         spat->spat_runtime = arg = op_new(1);
  1751.         arg->arg_type = O_ITEM;
  1752.         arg[1].arg_type = A_DOUBLE;
  1753.         arg[1].arg_ptr.arg_str = str_smake(str);
  1754.         d = scanident(d,bufend,buf);
  1755.         (void)stabent(buf,TRUE);        /* make sure it's created */
  1756.         for (; d < e; d++) {
  1757.         if (*d == '\\')
  1758.             d++;
  1759.         else if (*d == '$' && d[1] && d[1] != '|' && d[1] != ')') {
  1760.             d = scanident(d,bufend,buf);
  1761.             (void)stabent(buf,TRUE);
  1762.         }
  1763.         else if (*d == '@') {
  1764.             d = scanident(d,bufend,buf);
  1765.             if (strEQ(buf,"ARGV") || strEQ(buf,"ENV") ||
  1766.               strEQ(buf,"SIG") || strEQ(buf,"INC"))
  1767.             (void)stabent(buf,TRUE);
  1768.         }
  1769.         }
  1770.         goto got_pat;        /* skip compiling for now */
  1771.     }
  1772.     }
  1773.     if (spat->spat_flags & SPAT_FOLD)
  1774.     StructCopy(spat, &savespat, SPAT);
  1775.     scanconst(spat,str->str_ptr,len);
  1776.     if ((spat->spat_flags & SPAT_ALL) && (spat->spat_flags & SPAT_SCANFIRST)) {
  1777.     fbmcompile(spat->spat_short, spat->spat_flags & SPAT_FOLD);
  1778.     spat->spat_regexp = regcomp(str->str_ptr,str->str_ptr+len,
  1779.         spat->spat_flags & SPAT_FOLD);
  1780.         /* Note that this regexp can still be used if someone says
  1781.          * something like /a/ && s//b/;  so we can't delete it.
  1782.          */
  1783.     }
  1784.     else {
  1785.     if (spat->spat_flags & SPAT_FOLD)
  1786.     StructCopy(&savespat, spat, SPAT);
  1787.     if (spat->spat_short)
  1788.         fbmcompile(spat->spat_short, spat->spat_flags & SPAT_FOLD);
  1789.     spat->spat_regexp = regcomp(str->str_ptr,str->str_ptr+len,
  1790.         spat->spat_flags & SPAT_FOLD);
  1791.     hoistmust(spat);
  1792.     }
  1793.   got_pat:
  1794.     str_free(str);
  1795.     yylval.arg = make_match(O_MATCH,stab2arg(A_STAB,defstab),spat);
  1796.     return s;
  1797. }
  1798.  
  1799. char *
  1800. scansubst(start)
  1801. char *start;
  1802. {
  1803.     register char *s = start;
  1804.     register SPAT *spat;
  1805.     register char *d;
  1806.     register char *e;
  1807.     int len;
  1808.     STR *str = Str_new(93,0);
  1809.     char term = *s;
  1810.  
  1811.     if (term && (d = index("([{< )]}> )]}>",term)))
  1812.     term = d[5];
  1813.  
  1814.     Newz(802,spat,1,SPAT);
  1815.     spat->spat_next = curstash->tbl_spatroot;    /* link into spat list */
  1816.     curstash->tbl_spatroot = spat;
  1817.  
  1818.     s = str_append_till(str,s+1,bufend,term,patleave);
  1819.     if (s >= bufend) {
  1820.     str_free(str);
  1821.     yyerror("Substitution pattern not terminated");
  1822.     yylval.arg = Nullarg;
  1823.     return s;
  1824.     }
  1825.     len = str->str_cur;
  1826.     e = str->str_ptr + len;
  1827.     for (d = str->str_ptr; d < e; d++) {
  1828.     if (*d == '\\')
  1829.         d++;
  1830.     else if ((*d == '$' && d[1] && d[1] != '|' && /*(*/ d[1] != ')') ||
  1831.         *d == '@' ) {
  1832.         register ARG *arg;
  1833.  
  1834.         spat->spat_runtime = arg = op_new(1);
  1835.         arg->arg_type = O_ITEM;
  1836.         arg[1].arg_type = A_DOUBLE;
  1837.         arg[1].arg_ptr.arg_str = str_smake(str);
  1838.         d = scanident(d,e,buf);
  1839.         (void)stabent(buf,TRUE);        /* make sure it's created */
  1840.         for (; *d; d++) {
  1841.         if (*d == '$' && d[1] && d[-1] != '\\' && d[1] != '|') {
  1842.             d = scanident(d,e,buf);
  1843.             (void)stabent(buf,TRUE);
  1844.         }
  1845.         else if (*d == '@' && d[-1] != '\\') {
  1846.             d = scanident(d,e,buf);
  1847.             if (strEQ(buf,"ARGV") || strEQ(buf,"ENV") ||
  1848.               strEQ(buf,"SIG") || strEQ(buf,"INC"))
  1849.             (void)stabent(buf,TRUE);
  1850.         }
  1851.         }
  1852.         goto get_repl;        /* skip compiling for now */
  1853.     }
  1854.     }
  1855.     scanconst(spat,str->str_ptr,len);
  1856. get_repl:
  1857.     if (term != *start)
  1858.     s++;
  1859.     s = scanstr(s, SCAN_REPL);
  1860.     if (s >= bufend) {
  1861.     str_free(str);
  1862.     yyerror("Substitution replacement not terminated");
  1863.     yylval.arg = Nullarg;
  1864.     return s;
  1865.     }
  1866.     spat->spat_repl = yylval.arg;
  1867.     if ((spat->spat_repl[1].arg_type & A_MASK) == A_SINGLE)
  1868.     spat->spat_flags |= SPAT_CONST;
  1869.     else if ((spat->spat_repl[1].arg_type & A_MASK) == A_DOUBLE) {
  1870.     STR *tmpstr;
  1871.     register char *t;
  1872.  
  1873.     spat->spat_flags |= SPAT_CONST;
  1874.     tmpstr = spat->spat_repl[1].arg_ptr.arg_str;
  1875.     e = tmpstr->str_ptr + tmpstr->str_cur;
  1876.     for (t = tmpstr->str_ptr; t < e; t++) {
  1877.         if (*t == '$' && t[1] && (index("`'&+0123456789",t[1]) ||
  1878.           (t[1] == '{' /*}*/ && isDIGIT(t[2])) ))
  1879.         spat->spat_flags &= ~SPAT_CONST;
  1880.     }
  1881.     }
  1882.     while (*s == 'g' || *s == 'i' || *s == 'e' || *s == 'o') {
  1883.     int es = 0;
  1884.  
  1885.     if (*s == 'e') {
  1886.         s++;
  1887.         es++;
  1888.         if ((spat->spat_repl[1].arg_type & A_MASK) == A_DOUBLE)
  1889.         spat->spat_repl[1].arg_type = A_SINGLE;
  1890.         spat->spat_repl = make_op(
  1891.         (!es && spat->spat_repl[1].arg_type == A_SINGLE
  1892.             ? O_EVALONCE
  1893.             : O_EVAL),
  1894.         2,
  1895.         spat->spat_repl,
  1896.         Nullarg,
  1897.         Nullarg);
  1898.         spat->spat_flags &= ~SPAT_CONST;
  1899.     }
  1900.     if (*s == 'g') {
  1901.         s++;
  1902.         spat->spat_flags |= SPAT_GLOBAL;
  1903.     }
  1904.     if (*s == 'i') {
  1905.         s++;
  1906.         sawi = TRUE;
  1907.         spat->spat_flags |= SPAT_FOLD;
  1908.         if (!(spat->spat_flags & SPAT_SCANFIRST)) {
  1909.         str_free(spat->spat_short);    /* anchored opt doesn't do */
  1910.         spat->spat_short = Nullstr;    /* case insensitive match */
  1911.         spat->spat_slen = 0;
  1912.         }
  1913.     }
  1914.     if (*s == 'o') {
  1915.         s++;
  1916.         spat->spat_flags |= SPAT_KEEP;
  1917.     }
  1918.     }
  1919.     if (spat->spat_short && (spat->spat_flags & SPAT_SCANFIRST))
  1920.     fbmcompile(spat->spat_short, spat->spat_flags & SPAT_FOLD);
  1921.     if (!spat->spat_runtime) {
  1922.     spat->spat_regexp = regcomp(str->str_ptr,str->str_ptr+len,
  1923.       spat->spat_flags & SPAT_FOLD);
  1924.     hoistmust(spat);
  1925.     }
  1926.     yylval.arg = make_match(O_SUBST,stab2arg(A_STAB,defstab),spat);
  1927.     str_free(str);
  1928.     return s;
  1929. }
  1930.  
  1931. void
  1932. hoistmust(spat)
  1933. register SPAT *spat;
  1934. {
  1935.     if (!spat->spat_short && spat->spat_regexp->regstart &&
  1936.     (!spat->spat_regexp->regmust || spat->spat_regexp->reganch & ROPT_ANCH)
  1937.        ) {
  1938.     if (!(spat->spat_regexp->reganch & ROPT_ANCH))
  1939.         spat->spat_flags |= SPAT_SCANFIRST;
  1940.     else if (spat->spat_flags & SPAT_FOLD)
  1941.         return;
  1942.     spat->spat_short = str_smake(spat->spat_regexp->regstart);
  1943.     }
  1944.     else if (spat->spat_regexp->regmust) {/* is there a better short-circuit? */
  1945.     if (spat->spat_short &&
  1946.       str_eq(spat->spat_short,spat->spat_regexp->regmust))
  1947.     {
  1948.         if (spat->spat_flags & SPAT_SCANFIRST) {
  1949.         str_free(spat->spat_short);
  1950.         spat->spat_short = Nullstr;
  1951.         }
  1952.         else {
  1953.         str_free(spat->spat_regexp->regmust);
  1954.         spat->spat_regexp->regmust = Nullstr;
  1955.         return;
  1956.         }
  1957.     }
  1958.     if (!spat->spat_short ||    /* promote the better string */
  1959.       ((spat->spat_flags & SPAT_SCANFIRST) &&
  1960.        (spat->spat_short->str_cur < spat->spat_regexp->regmust->str_cur) )){
  1961.         str_free(spat->spat_short);        /* ok if null */
  1962.         spat->spat_short = spat->spat_regexp->regmust;
  1963.         spat->spat_regexp->regmust = Nullstr;
  1964.         spat->spat_flags |= SPAT_SCANFIRST;
  1965.     }
  1966.     }
  1967. }
  1968.  
  1969. char *
  1970. scantrans(start)
  1971. char *start;
  1972. {
  1973.     register char *s = start;
  1974.     ARG *arg =
  1975.     l(make_op(O_TRANS,2,stab2arg(A_STAB,defstab),Nullarg,Nullarg));
  1976.     STR *tstr;
  1977.     STR *rstr;
  1978.     register char *t;
  1979.     register char *r;
  1980.     register short *tbl;
  1981.     register int i;
  1982.     register int j;
  1983.     int tlen, rlen;
  1984.     int squash;
  1985.     int delete;
  1986.     int complement;
  1987.  
  1988.     New(803,tbl,256,short);
  1989.     arg[2].arg_type = A_NULL;
  1990.     arg[2].arg_ptr.arg_cval = (char*) tbl;
  1991.  
  1992.     s = scanstr(s, SCAN_TR);
  1993.     if (s >= bufend) {
  1994.     yyerror("Translation pattern not terminated");
  1995.     yylval.arg = Nullarg;
  1996.     return s;
  1997.     }
  1998.     tstr = yylval.arg[1].arg_ptr.arg_str; 
  1999.     yylval.arg[1].arg_ptr.arg_str = Nullstr; 
  2000.     arg_free(yylval.arg);
  2001.     t = tstr->str_ptr;
  2002.     tlen = tstr->str_cur;
  2003.  
  2004.     if (s[-1] == *start)
  2005.     s--;
  2006.  
  2007.     s = scanstr(s, SCAN_TR|SCAN_REPL);
  2008.     if (s >= bufend) {
  2009.     yyerror("Translation replacement not terminated");
  2010.     yylval.arg = Nullarg;
  2011.     return s;
  2012.     }
  2013.     rstr = yylval.arg[1].arg_ptr.arg_str; 
  2014.     yylval.arg[1].arg_ptr.arg_str = Nullstr; 
  2015.     arg_free(yylval.arg);
  2016.     r = rstr->str_ptr;
  2017.     rlen = rstr->str_cur;
  2018.  
  2019.     complement = delete = squash = 0;
  2020.     while (*s == 'c' || *s == 'd' || *s == 's') {
  2021.     if (*s == 'c')
  2022.         complement = 1;
  2023.     else if (*s == 'd')
  2024.         delete = 2;
  2025.     else
  2026.         squash = 1;
  2027.     s++;
  2028.     }
  2029.     arg[2].arg_len = delete|squash;
  2030.     yylval.arg = arg;
  2031.     if (complement) {
  2032.     Zero(tbl, 256, short);
  2033.     for (i = 0; i < tlen; i++)
  2034.         tbl[t[i] & 0377] = -1;
  2035.     for (i = 0, j = 0; i < 256; i++) {
  2036.         if (!tbl[i]) {
  2037.         if (j >= rlen) {
  2038.             if (delete)
  2039.             tbl[i] = -2;
  2040.             else if (rlen)
  2041.             tbl[i] = r[j-1] & 0377;
  2042.             else
  2043.             tbl[i] = i;
  2044.         }
  2045.         else
  2046.             tbl[i] = r[j++] & 0377;
  2047.         }
  2048.     }
  2049.     }
  2050.     else {
  2051.     if (!rlen && !delete) {
  2052.         r = t; rlen = tlen;
  2053.     }
  2054.     for (i = 0; i < 256; i++)
  2055.         tbl[i] = -1;
  2056.     for (i = 0, j = 0; i < tlen; i++,j++) {
  2057.         if (j >= rlen) {
  2058.         if (delete) {
  2059.             if (tbl[t[i] & 0377] == -1)
  2060.             tbl[t[i] & 0377] = -2;
  2061.             continue;
  2062.         }
  2063.         --j;
  2064.         }
  2065.         if (tbl[t[i] & 0377] == -1)
  2066.         tbl[t[i] & 0377] = r[j] & 0377;
  2067.     }
  2068.     }
  2069.     str_free(tstr);
  2070.     str_free(rstr);
  2071.     return s;
  2072. }
  2073.  
  2074. char *
  2075. scanstr(start, in_what)
  2076. char *start;
  2077. int in_what;
  2078. {
  2079.     register char *s = start;
  2080.     register char term;
  2081.     register char *d;
  2082.     register ARG *arg;
  2083.     register char *send;
  2084.     register bool makesingle = FALSE;
  2085.     register STAB *stab;
  2086.     bool alwaysdollar = FALSE;
  2087.     bool hereis = FALSE;
  2088.     STR *herewas;
  2089.     STR *str;
  2090.     /* which backslash sequences to keep */
  2091.     char *leave = (in_what & SCAN_TR)
  2092.     ? "\\$@nrtfbeacx0123456789-"
  2093.     : "\\$@nrtfbeacx0123456789[{]}lLuUE";
  2094.     int len;
  2095.  
  2096.     arg = op_new(1);
  2097.     yylval.arg = arg;
  2098.     arg->arg_type = O_ITEM;
  2099.  
  2100.     switch (*s) {
  2101.     default:            /* a substitution replacement */
  2102.     arg[1].arg_type = A_DOUBLE;
  2103.     makesingle = TRUE;    /* maybe disable runtime scanning */
  2104.     term = *s;
  2105.     if (term == '\'')
  2106.         leave = Nullch;
  2107.     goto snarf_it;
  2108.     case '0':
  2109.     {
  2110.         unsigned long i;
  2111.         int shift;
  2112.  
  2113.         arg[1].arg_type = A_SINGLE;
  2114.         if (s[1] == 'x') {
  2115.         shift = 4;
  2116.         s += 2;
  2117.         }
  2118.         else if (s[1] == '.')
  2119.         goto decimal;
  2120.         else
  2121.         shift = 3;
  2122.         i = 0;
  2123.         for (;;) {
  2124.         switch (*s) {
  2125.         default:
  2126.             goto out;
  2127.         case '_':
  2128.             s++;
  2129.             break;
  2130.         case '8': case '9':
  2131.             if (shift != 4)
  2132.             yyerror("Illegal octal digit");
  2133.             /* FALL THROUGH */
  2134.         case '0': case '1': case '2': case '3': case '4':
  2135.         case '5': case '6': case '7':
  2136.             i <<= shift;
  2137.             i += *s++ & 15;
  2138.             break;
  2139.         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  2140.         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  2141.             if (shift != 4)
  2142.             goto out;
  2143.             i <<= 4;
  2144.             i += (*s++ & 7) + 9;
  2145.             break;
  2146.         }
  2147.         }
  2148.       out:
  2149.         str = Str_new(92,0);
  2150.         str_numset(str,(double)i);
  2151.         if (str->str_ptr) {
  2152.         Safefree(str->str_ptr);
  2153.         str->str_ptr = Nullch;
  2154.         str->str_len = str->str_cur = 0;
  2155.         }
  2156.         arg[1].arg_ptr.arg_str = str;
  2157.     }
  2158.     break;
  2159.     case '1': case '2': case '3': case '4': case '5':
  2160.     case '6': case '7': case '8': case '9': case '.':
  2161.       decimal:
  2162.     arg[1].arg_type = A_SINGLE;
  2163.     d = tokenbuf;
  2164.     while (isDIGIT(*s) || *s == '_') {
  2165.         if (*s == '_')
  2166.         s++;
  2167.         else
  2168.         *d++ = *s++;
  2169.     }
  2170.     if (*s == '.' && s[1] != '.') {
  2171.         *d++ = *s++;
  2172.         while (isDIGIT(*s) || *s == '_') {
  2173.         if (*s == '_')
  2174.             s++;
  2175.         else
  2176.             *d++ = *s++;
  2177.         }
  2178.     }
  2179.     if (*s && index("eE",*s) && index("+-0123456789",s[1])) {
  2180.         *d++ = *s++;
  2181.         if (*s == '+' || *s == '-')
  2182.         *d++ = *s++;
  2183.         while (isDIGIT(*s))
  2184.         *d++ = *s++;
  2185.     }
  2186.     *d = '\0';
  2187.     str = Str_new(92,0);
  2188.     str_numset(str,atof(tokenbuf));
  2189.     if (str->str_ptr) {
  2190.         Safefree(str->str_ptr);
  2191.         str->str_ptr = Nullch;
  2192.         str->str_len = str->str_cur = 0;
  2193.     }
  2194.     arg[1].arg_ptr.arg_str = str;
  2195.     break;
  2196.     case '<':
  2197.     if (in_what & (SCAN_REPL|SCAN_TR))
  2198.         goto do_double;
  2199.     if (*++s == '<') {
  2200.         hereis = TRUE;
  2201.         d = tokenbuf;
  2202.         if (!rsfp)
  2203.         *d++ = '\n';
  2204.         if (*++s && index("`'\"",*s)) {
  2205.         term = *s++;
  2206.         s = cpytill(d,s,bufend,term,&len);
  2207.         if (s < bufend)
  2208.             s++;
  2209.         d += len;
  2210.         }
  2211.         else {
  2212.         if (*s == '\\')
  2213.             s++, term = '\'';
  2214.         else
  2215.             term = '"';
  2216.         while (isALNUM(*s))
  2217.             *d++ = *s++;
  2218.         }                /* assuming tokenbuf won't clobber */
  2219.         *d++ = '\n';
  2220.         *d = '\0';
  2221.         len = d - tokenbuf;
  2222.         d = "\n";
  2223.         if (rsfp || !(d=ninstr(s,bufend,d,d+1)))
  2224.         herewas = str_make(s,bufend-s);
  2225.         else
  2226.         s--, herewas = str_make(s,d-s);
  2227.         s += herewas->str_cur;
  2228.         if (term == '\'')
  2229.         goto do_single;
  2230.         if (term == '`')
  2231.         goto do_back;
  2232.         goto do_double;
  2233.     }
  2234.     d = tokenbuf;
  2235.     s = cpytill(d,s,bufend,'>',&len);
  2236.     if (s < bufend)
  2237.         s++;
  2238.     else
  2239.         fatal("Unterminated <> operator");
  2240.  
  2241.     if (*d == '$') d++;
  2242.     while (*d && (isALNUM(*d) || *d == '\''))
  2243.         d++;
  2244.     if (d - tokenbuf != len) {
  2245.         s = start;
  2246.         term = *s;
  2247.         arg[1].arg_type = A_GLOB;
  2248.         set_csh();
  2249.         alwaysdollar = TRUE;    /* treat $) and $| as variables */
  2250.         goto snarf_it;
  2251.     }
  2252.     else {
  2253.         d = tokenbuf;
  2254.         if (!len)
  2255.         (void)strcpy(d,"ARGV");
  2256.         if (*d == '$') {
  2257.         arg[1].arg_type = A_INDREAD;
  2258.         arg[1].arg_ptr.arg_stab = stabent(d+1,TRUE);
  2259.         }
  2260.         else {
  2261.         arg[1].arg_type = A_READ;
  2262.         arg[1].arg_ptr.arg_stab = stabent(d,TRUE);
  2263.         if (!stab_io(arg[1].arg_ptr.arg_stab))
  2264.             stab_io(arg[1].arg_ptr.arg_stab) = stio_new();
  2265.         if (strEQ(d,"ARGV")) {
  2266.             (void)aadd(arg[1].arg_ptr.arg_stab);
  2267.             stab_io(arg[1].arg_ptr.arg_stab)->flags |=
  2268.               IOF_ARGV|IOF_START;
  2269.         }
  2270.         }
  2271.     }
  2272.     break;
  2273.  
  2274.     case 'q':
  2275.     s++;
  2276.     if (*s == 'q') {
  2277.         s++;
  2278.         goto do_double;
  2279.     }
  2280.     if (*s == 'x') {
  2281.         s++;
  2282.         goto do_back;
  2283.     }
  2284.     /* FALL THROUGH */
  2285.     case '\'':
  2286.       do_single:
  2287.     term = *s;
  2288.     arg[1].arg_type = A_SINGLE;
  2289.     leave = Nullch;
  2290.     goto snarf_it;
  2291.  
  2292.     case '"': 
  2293.       do_double:
  2294.     term = *s;
  2295.     arg[1].arg_type = A_DOUBLE;
  2296.     makesingle = TRUE;    /* maybe disable runtime scanning */
  2297.     alwaysdollar = TRUE;    /* treat $) and $| as variables */
  2298.     goto snarf_it;
  2299.     case '`':
  2300.       do_back:
  2301.     term = *s;
  2302.     arg[1].arg_type = A_BACKTICK;
  2303.     set_csh();
  2304.     alwaysdollar = TRUE;    /* treat $) and $| as variables */
  2305.       snarf_it:
  2306.     {
  2307.         STR *tmpstr;
  2308.         STR *tmpstr2 = Nullstr;
  2309.         char *tmps;
  2310.         char *start;
  2311.         bool dorange = FALSE;
  2312.  
  2313.         CLINE;
  2314.         multi_start = curcmd->c_line;
  2315.         if (hereis)
  2316.         multi_open = multi_close = '<';
  2317.         else {
  2318.         multi_open = term;
  2319.         if (term && (tmps = index("([{< )]}> )]}>",term)))
  2320.             term = tmps[5];
  2321.         multi_close = term;
  2322.         }
  2323.         tmpstr = Str_new(87,80);
  2324.         if (hereis) {
  2325.         term = *tokenbuf;
  2326.         if (!rsfp) {
  2327.             d = s;
  2328.             while (s < bufend &&
  2329.               (*s != term || bcmp(s,tokenbuf,len) != 0) ) {
  2330.             if (*s++ == '\n')
  2331.                 curcmd->c_line++;
  2332.             }
  2333.             if (s >= bufend) {
  2334.             curcmd->c_line = multi_start;
  2335.             fatal("EOF in string");
  2336.             }
  2337.             str_nset(tmpstr,d+1,s-d);
  2338.             s += len - 1;
  2339.             str_ncat(herewas,s,bufend-s);
  2340.             str_replace(linestr,herewas);
  2341.             oldoldbufptr = oldbufptr = bufptr = s = str_get(linestr);
  2342.             bufend = linestr->str_ptr + linestr->str_cur;
  2343.             hereis = FALSE;
  2344.         }
  2345.         else
  2346.             str_nset(tmpstr,"",0);   /* avoid "uninitialized" warning */
  2347.         }
  2348.         else
  2349.         s = str_append_till(tmpstr,s+1,bufend,term,leave);
  2350.         while (s >= bufend) {    /* multiple line string? */
  2351.         if (!rsfp ||
  2352.          !(oldoldbufptr = oldbufptr = s = str_gets(linestr, rsfp, 0))) {
  2353.             curcmd->c_line = multi_start;
  2354.             fatal("EOF in string");
  2355.         }
  2356.         curcmd->c_line++;
  2357.         if (perldb) {
  2358.             STR *str = Str_new(88,0);
  2359.  
  2360.             str_sset(str,linestr);
  2361.             astore(stab_xarray(curcmd->c_filestab),
  2362.               (int)curcmd->c_line,str);
  2363.         }
  2364.         bufend = linestr->str_ptr + linestr->str_cur;
  2365.         if (hereis) {
  2366.             if (*s == term && bcmp(s,tokenbuf,len) == 0) {
  2367.             s = bufend - 1;
  2368.             *s = ' ';
  2369.             str_scat(linestr,herewas);
  2370.             bufend = linestr->str_ptr + linestr->str_cur;
  2371.             }
  2372.             else {
  2373.             s = bufend;
  2374.             str_scat(tmpstr,linestr);
  2375.             }
  2376.         }
  2377.         else
  2378.             s = str_append_till(tmpstr,s,bufend,term,leave);
  2379.         }
  2380.         multi_end = curcmd->c_line;
  2381.         s++;
  2382.         if (tmpstr->str_cur + 5 < tmpstr->str_len) {
  2383.         tmpstr->str_len = tmpstr->str_cur + 1;
  2384.         Renew(tmpstr->str_ptr, tmpstr->str_len, char);
  2385.         }
  2386.         if (arg[1].arg_type == A_SINGLE) {
  2387.         arg[1].arg_ptr.arg_str = tmpstr;
  2388.         break;
  2389.         }
  2390.         tmps = s;
  2391.         s = tmpstr->str_ptr;
  2392.         send = s + tmpstr->str_cur;
  2393.         while (s < send) {        /* see if we can make SINGLE */
  2394.         if (*s == '\\' && s[1] && isDIGIT(s[1]) && !isDIGIT(s[2]) &&
  2395.           !alwaysdollar && s[1] != '0')
  2396.             *s = '$';        /* grandfather \digit in subst */
  2397.         if ((*s == '$' || *s == '@') && s+1 < send &&
  2398.           (alwaysdollar || (s[1] != ')' && s[1] != '|'))) {
  2399.             makesingle = FALSE;    /* force interpretation */
  2400.         }
  2401.         else if (*s == '\\' && s+1 < send) {
  2402.             if (index("lLuUE",s[1]))
  2403.             makesingle = FALSE;
  2404.             s++;
  2405.         }
  2406.         s++;
  2407.         }
  2408.         s = d = start = tmpstr->str_ptr;    /* assuming shrinkage only */
  2409.         while (s < send || dorange) {
  2410.         if (in_what & SCAN_TR) {
  2411.             if (dorange) {
  2412.             int i;
  2413.             int max;
  2414.             if (!tmpstr2) {    /* oops, have to grow */
  2415.                 tmpstr2 = str_smake(tmpstr);
  2416.                 s = tmpstr2->str_ptr + (s - tmpstr->str_ptr);
  2417.                 send = tmpstr2->str_ptr + (send - tmpstr->str_ptr);
  2418.             }
  2419.             i = d - tmpstr->str_ptr;
  2420.             STR_GROW(tmpstr, tmpstr->str_len + 256);
  2421.             d = tmpstr->str_ptr + i;
  2422.             d -= 2;
  2423.             max = d[1] & 0377;
  2424.             for (i = (*d & 0377); i <= max; i++)
  2425.                 *d++ = i;
  2426.             start = s;
  2427.             dorange = FALSE;
  2428.             continue;
  2429.             }
  2430.             else if (*s == '-' && s+1 < send  && s != start) {
  2431.             dorange = TRUE;
  2432.             s++;
  2433.             }
  2434.         }
  2435.         else {
  2436.             if ((*s == '$' && s+1 < send &&
  2437.             (alwaysdollar || /*(*/(s[1] != ')' && s[1] != '|')) ) ||
  2438.             (*s == '@' && s+1 < send) ) {
  2439.             if (s[1] == '#' && (isALPHA(s[2]) || s[2] == '_'))
  2440.                 *d++ = *s++;
  2441.             len = scanident(s,send,tokenbuf) - s;
  2442.             if (*s == '$' || strEQ(tokenbuf,"ARGV")
  2443.               || strEQ(tokenbuf,"ENV")
  2444.               || strEQ(tokenbuf,"SIG")
  2445.               || strEQ(tokenbuf,"INC") )
  2446.                 (void)stabent(tokenbuf,TRUE); /* add symbol */
  2447.             while (len--)
  2448.                 *d++ = *s++;
  2449.             continue;
  2450.             }
  2451.         }
  2452.         if (*s == '\\' && s+1 < send) {
  2453.             s++;
  2454.             switch (*s) {
  2455.             case '-':
  2456.             if (in_what & SCAN_TR) {
  2457.                 *d++ = *s++;
  2458.                 continue;
  2459.             }
  2460.             /* FALL THROUGH */
  2461.             default:
  2462.             if (!makesingle && (!leave || (*s && index(leave,*s))))
  2463.                 *d++ = '\\';
  2464.             *d++ = *s++;
  2465.             continue;
  2466.             case '0': case '1': case '2': case '3':
  2467.             case '4': case '5': case '6': case '7':
  2468.             *d++ = scanoct(s, 3, &len);
  2469.             s += len;
  2470.             continue;
  2471.             case 'x':
  2472.             *d++ = scanhex(++s, 2, &len);
  2473.             s += len;
  2474.             continue;
  2475.             case 'c':
  2476.             s++;
  2477.             *d = *s++;
  2478.             if (isLOWER(*d))
  2479.                 *d = toupper(*d);
  2480.             *d++ ^= 64;
  2481.             continue;
  2482.             case 'b':
  2483.             *d++ = '\b';
  2484.             break;
  2485.             case 'n':
  2486.             *d++ = '\n';
  2487.             break;
  2488.             case 'r':
  2489.             *d++ = '\r';
  2490.             break;
  2491.             case 'f':
  2492.             *d++ = '\f';
  2493.             break;
  2494.             case 't':
  2495.             *d++ = '\t';
  2496.             break;
  2497.             case 'e':
  2498.             *d++ = '\033';
  2499.             break;
  2500.             case 'a':
  2501.             *d++ = '\007';
  2502.             break;
  2503.             }
  2504.             s++;
  2505.             continue;
  2506.         }
  2507.         *d++ = *s++;
  2508.         }
  2509.         *d = '\0';
  2510.  
  2511.         if (arg[1].arg_type == A_DOUBLE && makesingle)
  2512.         arg[1].arg_type = A_SINGLE;    /* now we can optimize on it */
  2513.  
  2514.         tmpstr->str_cur = d - tmpstr->str_ptr;
  2515.         if (arg[1].arg_type == A_GLOB) {
  2516.         arg[1].arg_ptr.arg_stab = stab = genstab();
  2517.         stab_io(stab) = stio_new();
  2518.         str_sset(stab_val(stab), tmpstr);
  2519.         }
  2520.         else
  2521.         arg[1].arg_ptr.arg_str = tmpstr;
  2522.         s = tmps;
  2523.         if (tmpstr2)
  2524.         str_free(tmpstr2);
  2525.         break;
  2526.     }
  2527.     }
  2528.     if (hereis)
  2529.     str_free(herewas);
  2530.     return s;
  2531. }
  2532.  
  2533. FCMD *
  2534. load_format()
  2535. {
  2536.     FCMD froot;
  2537.     FCMD *flinebeg;
  2538.     char *eol;
  2539.     register FCMD *fprev = &froot;
  2540.     register FCMD *fcmd;
  2541.     register char *s;
  2542.     register char *t;
  2543.     register STR *str;
  2544.     bool noblank;
  2545.     bool repeater;
  2546.  
  2547.     Zero(&froot, 1, FCMD);
  2548.     s = bufptr;
  2549.     while (s < bufend || (rsfp && (s = str_gets(linestr,rsfp, 0)) != Nullch)) {
  2550.     curcmd->c_line++;
  2551.     if (in_eval && !rsfp) {
  2552.         eol = index(s,'\n');
  2553.         if (!eol++)
  2554.         eol = bufend;
  2555.     }
  2556.     else
  2557.         eol = bufend = linestr->str_ptr + linestr->str_cur;
  2558.     if (perldb) {
  2559.         STR *tmpstr = Str_new(89,0);
  2560.  
  2561.         str_nset(tmpstr, s, eol-s);
  2562.         astore(stab_xarray(curcmd->c_filestab), (int)curcmd->c_line,tmpstr);
  2563.     }
  2564.     if (*s == '.') {
  2565.         /*SUPPRESS 530*/
  2566.         for (t = s+1; *t == ' ' || *t == '\t'; t++) ;
  2567.         if (*t == '\n') {
  2568.         bufptr = s;
  2569.         return froot.f_next;
  2570.         }
  2571.     }
  2572.     if (*s == '#') {
  2573.         s = eol;
  2574.         continue;
  2575.     }
  2576.     flinebeg = Nullfcmd;
  2577.     noblank = FALSE;
  2578.     repeater = FALSE;
  2579.     while (s < eol) {
  2580.         Newz(804,fcmd,1,FCMD);
  2581.         fprev->f_next = fcmd;
  2582.         fprev = fcmd;
  2583.         for (t=s; t < eol && *t != '@' && *t != '^'; t++) {
  2584.         if (*t == '~') {
  2585.             noblank = TRUE;
  2586.             *t = ' ';
  2587.             if (t[1] == '~') {
  2588.             repeater = TRUE;
  2589.             t[1] = ' ';
  2590.             }
  2591.         }
  2592.         }
  2593.         fcmd->f_pre = nsavestr(s, t-s);
  2594.         fcmd->f_presize = t-s;
  2595.         s = t;
  2596.         if (s >= eol) {
  2597.         if (noblank)
  2598.             fcmd->f_flags |= FC_NOBLANK;
  2599.         if (repeater)
  2600.             fcmd->f_flags |= FC_REPEAT;
  2601.         break;
  2602.         }
  2603.         if (!flinebeg)
  2604.         flinebeg = fcmd;        /* start values here */
  2605.         if (*s++ == '^')
  2606.         fcmd->f_flags |= FC_CHOP;    /* for doing text filling */
  2607.         switch (*s) {
  2608.         case '*':
  2609.         fcmd->f_type = F_LINES;
  2610.         *s = '\0';
  2611.         break;
  2612.         case '<':
  2613.         fcmd->f_type = F_LEFT;
  2614.         while (*s == '<')
  2615.             s++;
  2616.         break;
  2617.         case '>':
  2618.         fcmd->f_type = F_RIGHT;
  2619.         while (*s == '>')
  2620.             s++;
  2621.         break;
  2622.         case '|':
  2623.         fcmd->f_type = F_CENTER;
  2624.         while (*s == '|')
  2625.             s++;
  2626.         break;
  2627.         case '#':
  2628.         case '.':
  2629.         /* Catch the special case @... and handle it as a string
  2630.            field. */
  2631.         if (*s == '.' && s[1] == '.') {
  2632.             goto default_format;
  2633.         }
  2634.         fcmd->f_type = F_DECIMAL;
  2635.         {
  2636.             char *p;
  2637.  
  2638.             /* Read a format in the form @####.####, where either group
  2639.                of ### may be empty, or the final .### may be missing. */
  2640.             while (*s == '#')
  2641.             s++;
  2642.             if (*s == '.') {
  2643.             s++;
  2644.             p = s;
  2645.             while (*s == '#')
  2646.                 s++;
  2647.             fcmd->f_decimals = s-p;
  2648.             fcmd->f_flags |= FC_DP;
  2649.             } else {
  2650.             fcmd->f_decimals = 0;
  2651.             }
  2652.         }
  2653.         break;
  2654.         default:
  2655.         default_format:
  2656.         fcmd->f_type = F_LEFT;
  2657.         break;
  2658.         }
  2659.         if (fcmd->f_flags & FC_CHOP && *s == '.') {
  2660.         fcmd->f_flags |= FC_MORE;
  2661.         while (*s == '.')
  2662.             s++;
  2663.         }
  2664.         fcmd->f_size = s-t;
  2665.     }
  2666.     if (flinebeg) {
  2667.       again:
  2668.         if (s >= bufend &&
  2669.           (!rsfp || (s = str_gets(linestr, rsfp, 0)) == Nullch) )
  2670.         goto badform;
  2671.         curcmd->c_line++;
  2672.         if (in_eval && !rsfp) {
  2673.         eol = index(s,'\n');
  2674.         if (!eol++)
  2675.             eol = bufend;
  2676.         }
  2677.         else
  2678.         eol = bufend = linestr->str_ptr + linestr->str_cur;
  2679.         if (perldb) {
  2680.         STR *tmpstr = Str_new(90,0);
  2681.  
  2682.         str_nset(tmpstr, s, eol-s);
  2683.         astore(stab_xarray(curcmd->c_filestab),
  2684.             (int)curcmd->c_line,tmpstr);
  2685.         }
  2686.         if (strnEQ(s,".\n",2)) {
  2687.         bufptr = s;
  2688.         yyerror("Missing values line");
  2689.         return froot.f_next;
  2690.         }
  2691.         if (*s == '#') {
  2692.         s = eol;
  2693.         goto again;
  2694.         }
  2695.         str = flinebeg->f_unparsed = Str_new(91,eol - s);
  2696.         str->str_u.str_hash = curstash;
  2697.         str_nset(str,"(",1);
  2698.         flinebeg->f_line = curcmd->c_line;
  2699.         eol[-1] = '\0';
  2700.         if (!flinebeg->f_next->f_type || index(s, ',')) {
  2701.         eol[-1] = '\n';
  2702.         str_ncat(str, s, eol - s - 1);
  2703.         str_ncat(str,",$$);",5);
  2704.         s = eol;
  2705.         }
  2706.         else {
  2707.         eol[-1] = '\n';
  2708.         while (s < eol && isSPACE(*s))
  2709.             s++;
  2710.         t = s;
  2711.         while (s < eol) {
  2712.             switch (*s) {
  2713.             case ' ': case '\t': case '\n': case ';':
  2714.             str_ncat(str, t, s - t);
  2715.             str_ncat(str, "," ,1);
  2716.             while (s < eol && (isSPACE(*s) || *s == ';'))
  2717.                 s++;
  2718.             t = s;
  2719.             break;
  2720.             case '$':
  2721.             str_ncat(str, t, s - t);
  2722.             t = s;
  2723.             s = scanident(s,eol,tokenbuf);
  2724.             str_ncat(str, t, s - t);
  2725.             t = s;
  2726.             if (s < eol && *s && index("$'\"",*s))
  2727.                 str_ncat(str, ",", 1);
  2728.             break;
  2729.             case '"': case '\'':
  2730.             str_ncat(str, t, s - t);
  2731.             t = s;
  2732.             s++;
  2733.             while (s < eol && (*s != *t || s[-1] == '\\'))
  2734.                 s++;
  2735.             if (s < eol)
  2736.                 s++;
  2737.             str_ncat(str, t, s - t);
  2738.             t = s;
  2739.             if (s < eol && *s && index("$'\"",*s))
  2740.                 str_ncat(str, ",", 1);
  2741.             break;
  2742.             default:
  2743.             yyerror("Please use commas to separate fields");
  2744.             }
  2745.         }
  2746.         str_ncat(str,"$$);",4);
  2747.         }
  2748.     }
  2749.     }
  2750.   badform:
  2751.     bufptr = str_get(linestr);
  2752.     yyerror("Format not terminated");
  2753.     return froot.f_next;
  2754. }
  2755.  
  2756. static void
  2757. set_csh()
  2758. {
  2759. #ifdef CSH
  2760.     if (!cshlen)
  2761.     cshlen = strlen(cshname);
  2762. #endif
  2763. }
  2764.