home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Unix / Shells / tcsh / Source / sh.lex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  34.4 KB  |  1,819 lines

  1. /* $Header: /u/christos/src/tcsh-6.03/RCS/sh.lex.c,v 3.27 1992/11/13 04:19:10 christos Exp $ */
  2. /*
  3.  * sh.lex.c: Lexical analysis into tokens
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: sh.lex.c,v 3.27 1992/11/13 04:19:10 christos Exp $")
  40.  
  41. #include "ed.h"
  42. /* #define DEBUG_INP */
  43. /* #define DEBUG_SEEK */
  44.  
  45. /*
  46.  * C shell
  47.  */
  48.  
  49. /*
  50.  * These lexical routines read input and form lists of words.
  51.  * There is some involved processing here, because of the complications
  52.  * of input buffering, and especially because of history substitution.
  53.  */
  54. static    Char        *word        __P((void));
  55. static    int          getC1        __P((int));
  56. static    void          getdol        __P((void));
  57. static    void          getexcl    __P((int));
  58. static    struct Hist     *findev        __P((Char *, bool));
  59. static    void          setexclp    __P((Char *));
  60. static    int          bgetc        __P((void));
  61. static    void          bfree        __P((void));
  62. static    struct wordent    *gethent    __P((int));
  63. static    int          matchs        __P((Char *, Char *));
  64. static    int          getsel        __P((int *, int *, int));
  65. static    struct wordent    *getsub        __P((struct wordent *));
  66. static    Char         *subword    __P((Char *, int, bool *));
  67. static    struct wordent    *dosub        __P((int, struct wordent *, bool));
  68.  
  69. /*
  70.  * Peekc is a peek character for getC, peekread for readc.
  71.  * There is a subtlety here in many places... history routines
  72.  * will read ahead and then insert stuff into the input stream.
  73.  * If they push back a character then they must push it behind
  74.  * the text substituted by the history substitution.  On the other
  75.  * hand in several places we need 2 peek characters.  To make this
  76.  * all work, the history routines read with getC, and make use both
  77.  * of ungetC and unreadc.  The key observation is that the state
  78.  * of getC at the call of a history reference is such that calls
  79.  * to getC from the history routines will always yield calls of
  80.  * readc, unless this peeking is involved.  That is to say that during
  81.  * getexcl the variables lap, exclp, and exclnxt are all zero.
  82.  *
  83.  * Getdol invokes history substitution, hence the extra peek, peekd,
  84.  * which it can ungetD to be before history substitutions.
  85.  */
  86. static Char peekc = 0, peekd = 0;
  87. static Char peekread = 0;
  88.  
  89. /* (Tail of) current word from ! subst */
  90. static Char *exclp = NULL;
  91.  
  92. /* The rest of the ! subst words */
  93. static struct wordent *exclnxt = NULL;
  94.  
  95. /* Count of remaining words in ! subst */
  96. static int exclc = 0;
  97.  
  98. /* "Globp" for alias resubstitution */
  99. int aret = F_SEEK;
  100.  
  101. /*
  102.  * Labuf implements a general buffer for lookahead during lexical operations.
  103.  * Text which is to be placed in the input stream can be stuck here.
  104.  * We stick parsed ahead $ constructs during initial input,
  105.  * process id's from `$$', and modified variable values (from qualifiers
  106.  * during expansion in sh.dol.c) here.
  107.  */
  108. static Char labuf[BUFSIZE];
  109.  
  110. /*
  111.  * Lex returns to its caller not only a wordlist (as a "var" parameter)
  112.  * but also whether a history substitution occurred.  This is used in
  113.  * the main (process) routine to determine whether to echo, and also
  114.  * when called by the alias routine to determine whether to keep the
  115.  * argument list.
  116.  */
  117. static bool hadhist = 0;
  118.  
  119. /*
  120.  * Avoid alias expansion recursion via \!#
  121.  */
  122. int     hleft;
  123.  
  124. Char    histline[BUFSIZE + 2];    /* last line input */
  125.  
  126.  /* The +2 is to fool hp's optimizer */
  127. bool    histvalid = 0;        /* is histline valid */
  128. static Char *histlinep = NULL;    /* current pointer into histline */
  129.  
  130. static Char getCtmp;
  131.  
  132. #define getC(f)        (((getCtmp = peekc) != '\0') ? (peekc = 0, getCtmp) : getC1(f))
  133. #define    ungetC(c)    peekc = (Char) c
  134. #define    ungetD(c)    peekd = (Char) c
  135.  
  136. /* Use Htime to store timestamps picked up from history file for enthist()
  137.  * if reading saved history (sg)
  138.  */
  139. time_t Htime = (time_t)0;
  140. static time_t a2time_t __P((Char *));
  141.  
  142.  
  143. int
  144. lex(hp)
  145.     struct wordent *hp;
  146. {
  147.     struct wordent *wdp;
  148.     int     c;
  149.  
  150.  
  151.     histvalid = 0;
  152.     histlinep = histline;
  153.     *histlinep = '\0';
  154.  
  155.     btell(&lineloc);
  156.     hp->next = hp->prev = hp;
  157.     hp->word = STRNULL;
  158.     hadhist = 0;
  159.     do
  160.     c = readc(0);
  161.     while (c == ' ' || c == '\t');
  162.     if (c == HISTSUB && intty)
  163.     /* ^lef^rit    from tty is short !:s^lef^rit */
  164.     getexcl(c);
  165.     else
  166.     unreadc(c);
  167.     wdp = hp;
  168.     /*
  169.      * The following loop is written so that the links needed by freelex will
  170.      * be ready and rarin to go even if it is interrupted.
  171.      */
  172.     do {
  173.     struct wordent *new;
  174.  
  175.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  176.     new->word = STRNULL;
  177.     new->prev = wdp;
  178.     new->next = hp;
  179.     wdp->next = new;
  180.     hp->prev = new;
  181.     wdp = new;
  182.     wdp->word = word();
  183.     } while (wdp->word[0] != '\n');
  184.     if (histlinep < histline + BUFSIZE) {
  185.     *histlinep = '\0';
  186.     if (histlinep > histline && histlinep[-1] == '\n')
  187.         histlinep[-1] = '\0';
  188.     histvalid = 1;
  189.     }
  190.     else {
  191.     histline[BUFSIZE - 1] = '\0';
  192.     }
  193.  
  194.     return (hadhist);
  195. }
  196.  
  197. static time_t
  198. a2time_t(word)
  199.     Char * word;
  200. {
  201.     /* Attempt to distinguish timestamps from other possible entries.
  202.      * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
  203.  
  204.     time_t ret;
  205.     Char *s;
  206.     int ct;
  207.  
  208.     if (!word || *(s = word) != '+')
  209.     return (time_t)0;
  210.  
  211.     for (++s, ret = 0, ct = 0; *s; ++s, ++ct)
  212.     {
  213.     if (!isdigit((unsigned char)*s))
  214.         return (time_t)0;
  215.     ret = ret * 10 + (time_t)((unsigned char)*s - '0');
  216.     }
  217.  
  218.     if (ct != 10)
  219.     return (time_t)0;
  220.  
  221.     return ret;
  222. }
  223.  
  224. void
  225. prlex(sp0)
  226.     struct wordent *sp0;
  227. {
  228.     struct wordent *sp = sp0->next;
  229.  
  230.     for (;;) {
  231.     xprintf("%S", sp->word);
  232.     sp = sp->next;
  233.     if (sp == sp0)
  234.         break;
  235.     if (sp->word[0] != '\n')
  236.         xputchar(' ');
  237.     }
  238. }
  239.  
  240. void
  241. copylex(hp, fp)
  242.     struct wordent *hp;
  243.     struct wordent *fp;
  244. {
  245.     struct wordent *wdp;
  246.  
  247.     wdp = hp;
  248.     fp = fp->next;
  249.     do {
  250.     struct wordent *new;
  251.     
  252.     new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
  253.     new->word = STRNULL;
  254.     new->prev = wdp;
  255.     new->next = hp;
  256.     wdp->next = new;
  257.     hp->prev = new;
  258.     wdp = new;
  259.     wdp->word = Strsave(fp->word);
  260.     fp = fp->next;
  261.     } while (wdp->word[0] != '\n');
  262. }
  263.  
  264. void
  265. freelex(vp)
  266.     struct wordent *vp;
  267. {
  268.     struct wordent *fp;
  269.  
  270.     while (vp->next != vp) {
  271.     fp = vp->next;
  272.     vp->next = fp->next;
  273.     if (fp->word != STRNULL)
  274.         xfree((ptr_t) fp->word);
  275.     xfree((ptr_t) fp);
  276.     }
  277.     vp->prev = vp;
  278. }
  279.  
  280. static Char *
  281. word()
  282. {
  283.     Char c, c1;
  284.     Char *wp;
  285.     Char    wbuf[BUFSIZE];
  286.     Char    hbuf[12], h;
  287.     bool dolflg;
  288.     int i;
  289.  
  290.     wp = wbuf;
  291.     i = BUFSIZE - 4;
  292. loop:
  293.     while ((c = getC(DOALL)) == ' ' || c == '\t')
  294.     continue;
  295.     if (cmap(c, _META | _ESC))
  296.     switch (c) {
  297.     case '&':
  298.     case '|':
  299.     case '<':
  300.     case '>':
  301.         *wp++ = c;
  302.         c1 = getC(DOALL);
  303.         if (c1 == c)
  304.         *wp++ = c1;
  305.         else
  306.         ungetC(c1);
  307.         goto ret;
  308.  
  309.     case '#':
  310.         if (intty)
  311.         break;
  312.         c = 0;
  313.         h = 0;
  314.         do {
  315.         c1 = c;
  316.         c = getC(0);
  317.         if (h < 12)
  318.             hbuf[h++] = c;
  319.         } while (c != '\n');
  320.         hbuf[11] = '\0';
  321.         Htime = a2time_t(hbuf); 
  322.         if (c1 == '\\')
  323.         goto loop;
  324.         /*FALLTHROUGH*/
  325.  
  326.     case ';':
  327.     case '(':
  328.     case ')':
  329.     case '\n':
  330.         *wp++ = c;
  331.         goto ret;
  332.  
  333.     case '\\':
  334.         c = getC(0);
  335.         if (c == '\n') {
  336.         if (onelflg == 1)
  337.             onelflg = 2;
  338.         goto loop;
  339.         }
  340.         if (c != HIST)
  341.         *wp++ = '\\', --i;
  342.         c |= QUOTE;
  343.     default:
  344.         break;
  345.     }
  346.     c1 = 0;
  347.     dolflg = DOALL;
  348.     for (;;) {
  349.     if (c1) {
  350.         if (c == c1) {
  351.         c1 = 0;
  352.         dolflg = DOALL;
  353.         }
  354.         else if (c == '\\') {
  355.         c = getC(0);
  356. /*
  357.  * PWP: this is dumb, but how all of the other shells work.  If \ quotes
  358.  * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
  359.  * following character INSIDE a set of ''s.
  360.  *
  361.  * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
  362.  */
  363.         if (c == HIST)
  364.             c |= QUOTE;
  365.         else {
  366.             if (bslash_quote &&
  367.             ((c == '\'') || (c == '"') ||
  368.              (c == '\\'))) {
  369.             c |= QUOTE;
  370.             }
  371.             else {
  372.             if (c == '\n')
  373.                 /*
  374.                  * if (c1 == '`') c = ' '; else
  375.                  */
  376.                 c |= QUOTE;
  377.             ungetC(c);
  378.             c = '\\';
  379.             }
  380.         }
  381.         }
  382.         else if (c == '\n') {
  383.         seterror(ERR_UNMATCHED, c1);
  384.         ungetC(c);
  385.         break;
  386.         }
  387.     }
  388.     else if (cmap(c, _META | _Q | _Q1 | _ESC)) {
  389.         if (c == '\\') {
  390.         c = getC(0);
  391.         if (c == '\n') {
  392.             if (onelflg == 1)
  393.             onelflg = 2;
  394.             break;
  395.         }
  396.         if (c != HIST)
  397.             *wp++ = '\\', --i;
  398.         c |= QUOTE;
  399.         }
  400.         else if (cmap(c, _Q | _Q1)) {    /* '"` */
  401.         c1 = c;
  402.         dolflg = c == '"' ? DOALL : DOEXCL;
  403.         }
  404.         else if (c != '#' || !intty) {
  405.         ungetC(c);
  406.         break;
  407.         }
  408.     }
  409.     if (--i > 0) {
  410.         *wp++ = c;
  411.         c = getC(dolflg);
  412.     }
  413.     else {
  414.         seterror(ERR_WTOOLONG);
  415.         wp = &wbuf[1];
  416.         break;
  417.     }
  418.     }
  419. ret:
  420.     *wp = 0;
  421.     return (Strsave(wbuf));
  422. }
  423.  
  424. static int
  425. getC1(flag)
  426.     int flag;
  427. {
  428.     Char c;
  429.  
  430.     for (;;) {
  431.     if ((c = peekc) != 0) {
  432.         peekc = 0;
  433.         return (c);
  434.     }
  435.     if (lap) {
  436.         if ((c = *lap++) == 0)
  437.         lap = 0;
  438.         else {
  439.         if (cmap(c, _META | _Q | _Q1))
  440.             c |= QUOTE;
  441.         return (c);
  442.         }
  443.     }
  444.     if ((c = peekd) != 0) {
  445.         peekd = 0;
  446.         return (c);
  447.     }
  448.     if (exclp) {
  449.         if ((c = *exclp++) != 0)
  450.         return (c);
  451.         if (exclnxt && --exclc >= 0) {
  452.         exclnxt = exclnxt->next;
  453.         setexclp(exclnxt->word);
  454.         return (' ');
  455.         }
  456.         exclp = 0;
  457.         exclnxt = 0;
  458.     }
  459.     if (exclnxt) {
  460.         exclnxt = exclnxt->next;
  461.         if (--exclc < 0)
  462.         exclnxt = 0;
  463.         else
  464.         setexclp(exclnxt->word);
  465.         continue;
  466.     }
  467.     c = readc(0);
  468.     if (c == '$' && (flag & DODOL)) {
  469.         getdol();
  470.         continue;
  471.     }
  472.     if (c == HIST && (flag & DOEXCL)) {
  473.         getexcl(0);
  474.         continue;
  475.     }
  476.     break;
  477.     }
  478.     return (c);
  479. }
  480.  
  481. static void
  482. getdol()
  483. {
  484.     Char *np, *ep;
  485.     Char    name[4 * MAXVARLEN + 1];
  486.     int c;
  487.     int     sc;
  488.     bool    special = 0, toolong;
  489.  
  490.     np = name, *np++ = '$';
  491.     c = sc = getC(DOEXCL);
  492.     if (any("\t \n", c)) {
  493.     ungetD(c);
  494.     ungetC('$' | QUOTE);
  495.     return;
  496.     }
  497.     if (c == '{')
  498.     *np++ = (Char) c, c = getC(DOEXCL);
  499.     if (c == '#' || c == '?' || c == '%')
  500.     special++, *np++ = (Char) c, c = getC(DOEXCL);
  501.     *np++ = (Char) c;
  502.     switch (c) {
  503.  
  504.     case '<':
  505.     case '$':
  506.     case '!':
  507.     if (special)
  508.         seterror(ERR_SPDOLLT);
  509.     *np = 0;
  510.     addla(name);
  511.     return;
  512.  
  513.     case '\n':
  514.     ungetD(c);
  515.     np--;
  516.     if (!special)
  517.         seterror(ERR_NEWLINE);
  518.     *np = 0;
  519.     addla(name);
  520.     return;
  521.  
  522.     case '*':
  523.     if (special)
  524.         seterror(ERR_SPSTAR);
  525.     *np = 0;
  526.     addla(name);
  527.     return;
  528.  
  529.     default:
  530.     toolong = 0;
  531.     if (Isdigit(c)) {
  532. #ifdef notdef
  533.         /* let $?0 pass for now */
  534.         if (special) {
  535.         seterror(ERR_DIGIT);
  536.         *np = 0;
  537.         addla(name);
  538.         return;
  539.         }
  540. #endif
  541.         /* we know that np < &name[4] */
  542.         ep = &np[MAXVARLEN];
  543.         while ((c = getC(DOEXCL)) != 0) {
  544.         if (!Isdigit(c))
  545.             break;
  546.         if (np < ep)
  547.             *np++ = (Char) c;
  548.         else
  549.             toolong = 1;
  550.         }
  551.     }
  552.     else if (letter(c)) {
  553.         /* we know that np < &name[4] */
  554.         ep = &np[MAXVARLEN];
  555.         toolong = 0;
  556.         while ((c = getC(DOEXCL)) != 0) {
  557.         /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
  558.         if (!letter(c) && !Isdigit(c))
  559.             break;
  560.         if (np < ep)
  561.             *np++ = (Char) c;
  562.         else
  563.             toolong = 1;
  564.         }
  565.     }
  566.     else {
  567.         if (!special)
  568.         seterror(ERR_VARILL);
  569.         else {
  570.         ungetD(c);
  571.         *--np = 0;
  572.         addla(name);
  573.         return;
  574.         }
  575.     }
  576.     if (toolong) {
  577.         seterror(ERR_VARTOOLONG);
  578.         *np = 0;
  579.         addla(name);
  580.         return;
  581.     }
  582.     break;
  583.     }
  584.     if (c == '[') {
  585.     *np++ = (Char) c;
  586.     /*
  587.      * Name up to here is a max of MAXVARLEN + 8.
  588.      */
  589.     ep = &np[2 * MAXVARLEN + 8];
  590.     do {
  591.         /*
  592.          * Michael Greim: Allow $ expansion to take place in selector
  593.          * expressions. (limits the number of characters returned)
  594.          */
  595.         c = getC(DOEXCL | DODOL);
  596.         if (c == '\n') {
  597.         ungetD(c);
  598.         np--;
  599.         seterror(ERR_NLINDEX);
  600.         *np = 0;
  601.         addla(name);
  602.         return;
  603.         }
  604.         if (np < ep)
  605.         *np++ = (Char) c;
  606.     } while (c != ']');
  607.     *np = '\0';
  608.     if (np >= ep) {
  609.         seterror(ERR_SELOVFL);
  610.         addla(name);
  611.         return;
  612.     }
  613.     c = getC(DOEXCL);
  614.     }
  615.     /*
  616.      * Name up to here is a max of 2 * MAXVARLEN + 8.
  617.      */
  618.     if (c == ':') {
  619.     /*
  620.      * if the :g modifier is followed by a newline, then error right away!
  621.      * -strike
  622.      */
  623.  
  624.     int     gmodflag = 0, amodflag = 0;
  625.  
  626. #ifndef COMPAT
  627.     do {
  628. #endif /* COMPAT */
  629.         *np++ = (Char) c, c = getC(DOEXCL);
  630.         if (c == 'g' || c == 'a') {
  631.         if (c == 'g')
  632.             gmodflag++;
  633.         else
  634.             amodflag++;
  635.         *np++ = (Char) c; c = getC(DOEXCL);
  636.         }
  637.         if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
  638.         if (c == 'g')
  639.             gmodflag++;
  640.         else
  641.             amodflag++;
  642.         *np++ = (Char) c; c = getC(DOEXCL);
  643.         }
  644.         *np++ = (Char) c;
  645.         /* scan s// [eichin:19910926.0512EST] */
  646.         if (c == 's') {
  647.         int delimcnt = 2;
  648.         int delim = getC(0);
  649.         *np++ = delim;
  650.         
  651.         if (!delim || letter(delim)
  652.             || Isdigit(delim) || any(" \t\n", delim)) {
  653.             seterror(ERR_BADSUBST);
  654.             break;
  655.         }    
  656.         while ((c = getC(0)) != (-1)) {
  657.             *np++ = (Char) c;
  658.             if(c == delim) delimcnt--;
  659.             if(!delimcnt) break;
  660.         }
  661.         if(delimcnt) {
  662.             seterror(ERR_BADSUBST);
  663.             break;
  664.         }
  665.         c = 's';
  666.         }
  667.         if (!any("htrqxesul", c)) {
  668.         if ((amodflag || gmodflag) && c == '\n')
  669.             stderror(ERR_VARSYN);    /* strike */
  670.         seterror(ERR_BADMOD, c);
  671.         *np = 0;
  672.         addla(name);
  673.         return;
  674.         }
  675. #ifndef COMPAT
  676.     }
  677.     while ((c = getC(DOEXCL)) == ':');
  678.     ungetD(c);
  679. #endif /* COMPAT */
  680.     }
  681.     else
  682.     ungetD(c);
  683.     if (sc == '{') {
  684.     c = getC(DOEXCL);
  685.     if (c != '}') {
  686.         ungetD(c);
  687.         seterror(ERR_MISSING, '}');
  688.         *np = 0;
  689.         addla(name);
  690.         return;
  691.     }
  692.     *np++ = (Char) c;
  693.     }
  694.     *np = 0;
  695.     addla(name);
  696.     return;
  697. }
  698.  
  699. void
  700. addla(cp)
  701.     Char   *cp;
  702. {
  703.     Char    buf[BUFSIZE];
  704.  
  705.     if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
  706.     (sizeof(labuf) - 4) / sizeof(Char)) {
  707.     seterror(ERR_EXPOVFL);
  708.     return;
  709.     }
  710.     if (lap)
  711.     (void) Strcpy(buf, lap);
  712.     (void) Strcpy(labuf, cp);
  713.     if (lap)
  714.     (void) Strcat(labuf, buf);
  715.     lap = labuf;
  716. }
  717.  
  718. static Char lhsb[32];
  719. static Char slhs[32];
  720. static Char rhsb[64];
  721. static int quesarg;
  722.  
  723. static void
  724. getexcl(sc)
  725.     int    sc;
  726. {
  727.     struct wordent *hp, *ip;
  728.     int     left, right, dol;
  729.     int c;
  730.  
  731.     if (sc == 0) {
  732.     sc = getC(0);
  733.     if (sc != '{') {
  734.         ungetC(sc);
  735.         sc = 0;
  736.     }
  737.     }
  738.     quesarg = -1;
  739.     lastev = eventno;
  740.     hp = gethent(sc);
  741.     if (hp == 0)
  742.     return;
  743.     hadhist = 1;
  744.     dol = 0;
  745.     if (hp == alhistp)
  746.     for (ip = hp->next->next; ip != alhistt; ip = ip->next)
  747.         dol++;
  748.     else
  749.     for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
  750.         dol++;
  751.     left = 0, right = dol;
  752.     if (sc == HISTSUB) {
  753.     ungetC('s'), unreadc(HISTSUB), c = ':';
  754.     goto subst;
  755.     }
  756.     c = getC(0);
  757.     if (!any(":^$*-%", c))
  758.     goto subst;
  759.     left = right = -1;
  760.     if (c == ':') {
  761.     c = getC(0);
  762.     unreadc(c);
  763.     if (letter(c) || c == '&') {
  764.         c = ':';
  765.         left = 0, right = dol;
  766.         goto subst;
  767.     }
  768.     }
  769.     else
  770.     ungetC(c);
  771.     if (!getsel(&left, &right, dol))
  772.     return;
  773.     c = getC(0);
  774.     if (c == '*')
  775.     ungetC(c), c = '-';
  776.     if (c == '-') {
  777.     if (!getsel(&left, &right, dol))
  778.         return;
  779.     c = getC(0);
  780.     }
  781. subst:
  782.     exclc = right - left + 1;
  783.     while (--left >= 0)
  784.     hp = hp->next;
  785.     if (sc == HISTSUB || c == ':') {
  786.     do {
  787.         hp = getsub(hp);
  788.         c = getC(0);
  789.     } while (c == ':');
  790.     }
  791.     unreadc(c);
  792.     if (sc == '{') {
  793.     c = getC(0);
  794.     if (c != '}')
  795.         seterror(ERR_BADBANG);
  796.     }
  797.     exclnxt = hp;
  798. }
  799.  
  800. static struct wordent *
  801. getsub(en)
  802.     struct wordent *en;
  803. {
  804.     Char *cp;
  805.     int     delim;
  806.     int c;
  807.     int     sc;
  808.     bool global;
  809.     Char    orhsb[sizeof(rhsb) / sizeof(Char)];
  810.  
  811. #ifndef COMPAT
  812.     do {
  813. #endif /* COMPAT */
  814.     exclnxt = 0;
  815.     global = 0;
  816.     sc = c = getC(0);
  817.     if (c == 'g' || c == 'a') {
  818.         global |= (c == 'g') ? 1 : 2;
  819.         sc = c = getC(0);
  820.     }
  821.     if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
  822.         global |= (c == 'g') ? 1 : 2;
  823.         sc = c = getC(0);
  824.     }
  825.  
  826.     switch (c) {
  827.     case 'p':
  828.         justpr++;
  829.         return (en);
  830.  
  831.     case 'x':
  832.     case 'q':
  833.         global |= 1;
  834.         /*FALLTHROUGH*/
  835.  
  836.     case 'h':
  837.     case 'r':
  838.     case 't':
  839.     case 'e':
  840.         break;
  841.  
  842.     case '&':
  843.         if (slhs[0] == 0) {
  844.         seterror(ERR_NOSUBST);
  845.         return (en);
  846.         }
  847.         (void) Strcpy(lhsb, slhs);
  848.         break;
  849.  
  850. #ifdef notdef
  851.     case '~':
  852.         if (lhsb[0] == 0)
  853.         goto badlhs;
  854.         break;
  855. #endif
  856.  
  857.     case 's':
  858.         delim = getC(0);
  859.         if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
  860.         unreadc(delim);
  861.         lhsb[0] = 0;
  862.         seterror(ERR_BADSUBST);
  863.         return (en);
  864.         }
  865.         cp = lhsb;
  866.         for (;;) {
  867.         c = getC(0);
  868.         if (c == '\n') {
  869.             unreadc(c);
  870.             break;
  871.         }
  872.         if (c == delim)
  873.             break;
  874.         if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
  875.             lhsb[0] = 0;
  876.             seterror(ERR_BADSUBST);
  877.             return (en);
  878.         }
  879.         if (c == '\\') {
  880.             c = getC(0);
  881.             if (c != delim && c != '\\')
  882.             *cp++ = '\\';
  883.         }
  884.         *cp++ = (Char) c;
  885.         }
  886.         if (cp != lhsb)
  887.         *cp++ = 0;
  888.         else if (lhsb[0] == 0) {
  889.         seterror(ERR_LHS);
  890.         return (en);
  891.         }
  892.         cp = rhsb;
  893.         (void) Strcpy(orhsb, cp);
  894.         for (;;) {
  895.         c = getC(0);
  896.         if (c == '\n') {
  897.             unreadc(c);
  898.             break;
  899.         }
  900.         if (c == delim)
  901.             break;
  902. #ifdef notdef
  903.         if (c == '~') {
  904.             if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
  905.                            sizeof(Char) - 2])
  906.             goto toorhs;
  907.             (void) Strcpy(cp, orhsb);
  908.             cp = Strend(cp);
  909.             continue;
  910.         }
  911. #endif
  912.         if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
  913.             seterror(ERR_RHSLONG);
  914.             return (en);
  915.         }
  916.         if (c == '\\') {
  917.             c = getC(0);
  918.             if (c != delim /* && c != '~' */ )
  919.             *cp++ = '\\';
  920.         }
  921.         *cp++ = (Char) c;
  922.         }
  923.         *cp++ = 0;
  924.         break;
  925.  
  926.     default:
  927.         if (c == '\n')
  928.         unreadc(c);
  929.         seterror(ERR_BADBANGMOD, c);
  930.         return (en);
  931.     }
  932.     (void) Strcpy(slhs, lhsb);
  933.     if (exclc)
  934.         en = dosub(sc, en, global);
  935. #ifndef COMPAT
  936.     }
  937.     while ((c = getC(0)) == ':');
  938.     unreadc(c);
  939. #endif /* COMPAT */
  940.     return (en);
  941. }
  942.  
  943. /*
  944.  * 
  945.  * From Beto Appleton (beto@aixwiz.austin.ibm.com)
  946.  *
  947.  * when using history substitution, and the variable
  948.  * 'history' is set to a value higher than 1000,
  949.  * the shell might either freeze (hang) or core-dump.
  950.  * We raise the limit to 50000000
  951.  */
  952.  
  953. #define HIST_PURGE -50000000
  954. static struct wordent *
  955. dosub(sc, en, global)
  956.     int     sc;
  957.     struct wordent *en;
  958.     bool global;
  959. {
  960.     struct wordent lexi;
  961.     bool    didsub = 0, didone = 0;
  962.     struct wordent *hp = &lexi;
  963.     struct wordent *wdp;
  964.     int i = exclc;
  965.     struct Hist *hst;
  966.  
  967.     wdp = hp;
  968.     while (--i >= 0) {
  969.     struct wordent *new = 
  970.         (struct wordent *) xcalloc(1, sizeof *wdp);
  971.  
  972.     new->word = 0;
  973.     new->prev = wdp;
  974.     new->next = hp;
  975.     wdp->next = new;
  976.     wdp = new;
  977.     en = en->next;
  978.     if (en->word) {
  979.         Char *tword, *otword;
  980.  
  981.         if ((global & 1) || didsub == 0) {
  982.         tword = subword(en->word, sc, &didone);
  983.         if (didone)
  984.             didsub = 1;
  985.         if (global & 2) {
  986.             while (didone && tword != STRNULL) {
  987.             otword = tword;
  988.             tword = subword(otword, sc, &didone);
  989.             if (Strcmp(tword, otword) == 0) {
  990.                 xfree((ptr_t) otword);
  991.                 break;
  992.             }
  993.             else
  994.                 xfree((ptr_t) otword);
  995.             }
  996.         }
  997.         }
  998.         else
  999.         tword = Strsave(en->word);
  1000.         wdp->word = tword;
  1001.     }
  1002.     }
  1003.     if (didsub == 0)
  1004.     seterror(ERR_MODFAIL);
  1005.     hp->prev = wdp;
  1006.     /* 
  1007.      * ANSI mode HP/UX compiler chokes on
  1008.      * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
  1009.      */
  1010.     hst = enthist(HIST_PURGE, &lexi, 0);
  1011.     return &(hst->Hlex);
  1012. }
  1013.  
  1014. static Char *
  1015. subword(cp, type, adid)
  1016.     Char   *cp;
  1017.     int     type;
  1018.     bool   *adid;
  1019. {
  1020.     Char    wbuf[BUFSIZE];
  1021.     Char *wp, *mp, *np;
  1022.     int i;
  1023.  
  1024.     *adid = 0;
  1025.     switch (type) {
  1026.  
  1027.     case 'r':
  1028.     case 'e':
  1029.     case 'h':
  1030.     case 't':
  1031.     case 'q':
  1032.     case 'x':
  1033.     case 'u':
  1034.     case 'l':
  1035.     wp = domod(cp, type);
  1036.     if (wp == 0)
  1037.         return (Strsave(cp));
  1038.     *adid = 1;
  1039.     return (wp);
  1040.  
  1041.     default:
  1042.     wp = wbuf;
  1043.     i = BUFSIZE - 4;
  1044.     for (mp = cp; *mp; mp++)
  1045.         if (matchs(mp, lhsb)) {
  1046.         for (np = cp; np < mp;)
  1047.             *wp++ = *np++, --i;
  1048.         for (np = rhsb; *np; np++)
  1049.             switch (*np) {
  1050.  
  1051.             case '\\':
  1052.             if (np[1] == '&')
  1053.                 np++;
  1054.             /* fall into ... */
  1055.  
  1056.             default:
  1057.             if (--i < 0) {
  1058.                 seterror(ERR_SUBOVFL);
  1059.                 return (STRNULL);
  1060.             }
  1061.             *wp++ = *np;
  1062.             continue;
  1063.  
  1064.             case '&':
  1065.             i -= Strlen(lhsb);
  1066.             if (i < 0) {
  1067.                 seterror(ERR_SUBOVFL);
  1068.                 return (STRNULL);
  1069.             }
  1070.             *wp = 0;
  1071.             (void) Strcat(wp, lhsb);
  1072.             wp = Strend(wp);
  1073.             continue;
  1074.             }
  1075.         mp += Strlen(lhsb);
  1076.         i -= Strlen(mp);
  1077.         if (i < 0) {
  1078.             seterror(ERR_SUBOVFL);
  1079.             return (STRNULL);
  1080.         }
  1081.         *wp = 0;
  1082.         (void) Strcat(wp, mp);
  1083.         *adid = 1;
  1084.         return (Strsave(wbuf));
  1085.         }
  1086.     return (Strsave(cp));
  1087.     }
  1088. }
  1089.  
  1090. Char   *
  1091. domod(cp, type)
  1092.     Char   *cp;
  1093.     int     type;
  1094. {
  1095.     Char *wp, *xp;
  1096.     int c;
  1097.  
  1098.     switch (type) {
  1099.  
  1100.     case 'x':
  1101.     case 'q':
  1102.     wp = Strsave(cp);
  1103.     for (xp = wp; (c = *xp) != 0; xp++)
  1104.         if ((c != ' ' && c != '\t') || type == 'q')
  1105.         *xp |= QUOTE;
  1106.     return (wp);
  1107.  
  1108.     case 'l':
  1109.     wp = Strsave(cp);
  1110.     for (cp = wp; *cp; cp++) 
  1111.         if (Isupper(*cp)) {
  1112.         *cp = Tolower(*cp);
  1113.         return wp;
  1114.         }
  1115.     return wp;
  1116.  
  1117.     case 'u':
  1118.     wp = Strsave(cp);
  1119.     for (cp = wp; *cp; cp++) 
  1120.         if (Islower(*cp)) {
  1121.         *cp = Toupper(*cp);
  1122.         return wp;
  1123.         }
  1124.     return wp;
  1125.  
  1126.     case 'h':
  1127.     case 't':
  1128.     if (!any(short2str(cp), '/'))
  1129.         return (type == 't' ? Strsave(cp) : 0);
  1130.     wp = Strend(cp);
  1131.     while (*--wp != '/')
  1132.         continue;
  1133.     if (type == 'h')
  1134.         xp = Strsave(cp), xp[wp - cp] = 0;
  1135.     else
  1136.         xp = Strsave(wp + 1);
  1137.     return (xp);
  1138.  
  1139.     case 'e':
  1140.     case 'r':
  1141.     wp = Strend(cp);
  1142.     for (wp--; wp >= cp && *wp != '/'; wp--)
  1143.         if (*wp == '.') {
  1144.         if (type == 'e')
  1145.             xp = Strsave(wp + 1);
  1146.         else
  1147.             xp = Strsave(cp), xp[wp - cp] = 0;
  1148.         return (xp);
  1149.         }
  1150.     return (Strsave(type == 'e' ? STRNULL : cp));
  1151.     default:
  1152.     break;
  1153.     }
  1154.     return (0);
  1155. }
  1156.  
  1157. static int
  1158. matchs(str, pat)
  1159.     Char *str, *pat;
  1160. {
  1161.     while (*str && *pat && *str == *pat)
  1162.     str++, pat++;
  1163.     return (*pat == 0);
  1164. }
  1165.  
  1166. static int
  1167. getsel(al, ar, dol)
  1168.     int *al, *ar;
  1169.     int     dol;
  1170. {
  1171.     int c = getC(0);
  1172.     int i;
  1173.     bool    first = *al < 0;
  1174.  
  1175.     switch (c) {
  1176.  
  1177.     case '%':
  1178.     if (quesarg == -1) {
  1179.         seterror(ERR_BADBANGARG);
  1180.         return (0);
  1181.     }
  1182.     if (*al < 0)
  1183.         *al = quesarg;
  1184.     *ar = quesarg;
  1185.     break;
  1186.  
  1187.     case '-':
  1188.     if (*al < 0) {
  1189.         *al = 0;
  1190.         *ar = dol - 1;
  1191.         unreadc(c);
  1192.     }
  1193.     return (1);
  1194.  
  1195.     case '^':
  1196.     if (*al < 0)
  1197.         *al = 1;
  1198.     *ar = 1;
  1199.     break;
  1200.  
  1201.     case '$':
  1202.     if (*al < 0)
  1203.         *al = dol;
  1204.     *ar = dol;
  1205.     break;
  1206.  
  1207.     case '*':
  1208.     if (*al < 0)
  1209.         *al = 1;
  1210.     *ar = dol;
  1211.     if (*ar < *al) {
  1212.         *ar = 0;
  1213.         *al = 1;
  1214.         return (1);
  1215.     }
  1216.     break;
  1217.  
  1218.     default:
  1219.     if (Isdigit(c)) {
  1220.         i = 0;
  1221.         while (Isdigit(c)) {
  1222.         i = i * 10 + c - '0';
  1223.         c = getC(0);
  1224.         }
  1225.         if (i < 0)
  1226.         i = dol + 1;
  1227.         if (*al < 0)
  1228.         *al = i;
  1229.         *ar = i;
  1230.     }
  1231.     else if (*al < 0)
  1232.         *al = 0, *ar = dol;
  1233.     else
  1234.         *ar = dol - 1;
  1235.     unreadc(c);
  1236.     break;
  1237.     }
  1238.     if (first) {
  1239.     c = getC(0);
  1240.     unreadc(c);
  1241.     if (any("-$*", c))
  1242.         return (1);
  1243.     }
  1244.     if (*al > *ar || *ar > dol) {
  1245.     seterror(ERR_BADBANGARG);
  1246.     return (0);
  1247.     }
  1248.     return (1);
  1249.  
  1250. }
  1251.  
  1252. static struct wordent *
  1253. gethent(sc)
  1254.     int     sc;
  1255. {
  1256.     struct Hist *hp;
  1257.     Char *np;
  1258.     int c;
  1259.     int     event;
  1260.     bool    back = 0;
  1261.  
  1262.     c = sc == HISTSUB ? HIST : getC(0);
  1263.     if (c == HIST) {
  1264.     if (alhistp)
  1265.         return (alhistp);
  1266.     event = eventno;
  1267.     }
  1268.     else
  1269.     switch (c) {
  1270.  
  1271.     case ':':
  1272.     case '^':
  1273.     case '$':
  1274.     case '*':
  1275.     case '%':
  1276.         ungetC(c);
  1277.         if (lastev == eventno && alhistp)
  1278.         return (alhistp);
  1279.         event = lastev;
  1280.         break;
  1281.  
  1282.     case '#':        /* !# is command being typed in (mrh) */
  1283.         if (--hleft == 0) {
  1284.         seterror(ERR_HISTLOOP);
  1285.         return (0);
  1286.         }
  1287.         else
  1288.         return (¶ml);
  1289.         /* NOTREACHED */
  1290.  
  1291.     case '-':
  1292.         back = 1;
  1293.         c = getC(0);
  1294.         /* FALLSTHROUGH */
  1295.  
  1296.     default:
  1297.         if (any("(=~", c)) {
  1298.         unreadc(c);
  1299.         ungetC(HIST);
  1300.         return (0);
  1301.         }
  1302.         np = lhsb;
  1303.         event = 0;
  1304.         while (!cmap(c, _ESC | _META | _Q | _Q1) && !any("${}:#", c)) {
  1305.         if (event != -1 && Isdigit(c))
  1306.             event = event * 10 + c - '0';
  1307.         else
  1308.             event = -1;
  1309.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1310.             *np++ = (Char) c;
  1311.         c = getC(0);
  1312.         }
  1313.         unreadc(c);
  1314.         if (np == lhsb) {
  1315.         ungetC(HIST);
  1316.         return (0);
  1317.         }
  1318.         *np++ = 0;
  1319.         if (event != -1) {
  1320.         /*
  1321.          * History had only digits
  1322.          */
  1323.         if (back)
  1324.             event = eventno + (alhistp == 0) - (event ? event : 0);
  1325.         break;
  1326.         }
  1327.         if (back) {
  1328.         event = sizeof(lhsb) / sizeof(lhsb[0]);
  1329.         np = &lhsb[--event];
  1330.         *np-- = '\0';
  1331.         for (event--; np > lhsb; *np-- = lhsb[--event])
  1332.             continue;
  1333.         *np = '-';
  1334.         }
  1335.         hp = findev(lhsb, 0);
  1336.         if (hp)
  1337.         lastev = hp->Hnum;
  1338.         return (&hp->Hlex);
  1339.  
  1340.     case '?':
  1341.         np = lhsb;
  1342.         for (;;) {
  1343.         c = getC(0);
  1344.         if (c == '\n') {
  1345.             unreadc(c);
  1346.             break;
  1347.         }
  1348.         if (c == '?')
  1349.             break;
  1350.         if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
  1351.             *np++ = (Char) c;
  1352.         }
  1353.         if (np == lhsb) {
  1354.         if (lhsb[0] == 0) {
  1355.             seterror(ERR_NOSEARCH);
  1356.             return (0);
  1357.         }
  1358.         }
  1359.         else
  1360.         *np++ = 0;
  1361.         hp = findev(lhsb, 1);
  1362.         if (hp)
  1363.         lastev = hp->Hnum;
  1364.         return (&hp->Hlex);
  1365.     }
  1366.  
  1367.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
  1368.     if (hp->Hnum == event) {
  1369.         hp->Href = eventno;
  1370.         lastev = hp->Hnum;
  1371.         return (&hp->Hlex);
  1372.     }
  1373.     np = putn(event);
  1374.     seterror(ERR_NOEVENT, short2str(np));
  1375.     return (0);
  1376. }
  1377.  
  1378. static struct Hist *
  1379. findev(cp, anyarg)
  1380.     Char   *cp;
  1381.     bool    anyarg;
  1382. {
  1383.     struct Hist *hp;
  1384.  
  1385.     for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
  1386.     Char   *dp;
  1387.     Char *p, *q;
  1388.     struct wordent *lp = hp->Hlex.next;
  1389.     int     argno = 0;
  1390.  
  1391.     /*
  1392.      * The entries added by alias substitution don't have a newline but do
  1393.      * have a negative event number. Savehist() trims off these entries,
  1394.      * but it happens before alias expansion, too early to delete those
  1395.      * from the previous command.
  1396.      */
  1397.     if (hp->Hnum < 0)
  1398.         continue;
  1399.     if (lp->word[0] == '\n')
  1400.         continue;
  1401.     if (!anyarg) {
  1402.         p = cp;
  1403.         q = lp->word;
  1404.         do
  1405.         if (!*p)
  1406.             return (hp);
  1407.         while (*p++ == *q++);
  1408.         continue;
  1409.     }
  1410.     do {
  1411.         for (dp = lp->word; *dp; dp++) {
  1412.         p = cp;
  1413.         q = dp;
  1414.         do
  1415.             if (!*p) {
  1416.             quesarg = argno;
  1417.             return (hp);
  1418.             }
  1419.         while (*p++ == *q++);
  1420.         }
  1421.         lp = lp->next;
  1422.         argno++;
  1423.     } while (lp->word[0] != '\n');
  1424.     }
  1425.     seterror(ERR_NOEVENT, short2str(cp));
  1426.     return (0);
  1427. }
  1428.  
  1429.  
  1430. static void
  1431. setexclp(cp)
  1432.     Char *cp;
  1433. {
  1434.     if (cp && cp[0] == '\n')
  1435.     return;
  1436.     exclp = cp;
  1437. }
  1438.  
  1439. void
  1440. unreadc(c)
  1441.     int    c;
  1442. {
  1443.     peekread = c;
  1444. }
  1445.  
  1446. int
  1447. readc(wanteof)
  1448.     bool    wanteof;
  1449. {
  1450.     int c;
  1451.     static  sincereal;
  1452.  
  1453. #ifdef DEBUG_INP
  1454.     xprintf("readc\n");
  1455. #endif
  1456.     if ((c = peekread) != 0) {
  1457.     peekread = 0;
  1458.     return (c);
  1459.     }
  1460. top:
  1461.     aret = F_SEEK;
  1462.     if (alvecp) {
  1463. #ifdef DEBUG_INP
  1464.     xprintf("alvecp %c\n", *alvecp & 0xff);
  1465. #endif
  1466.     aret = A_SEEK;
  1467.     if ((c = *alvecp++) != 0)
  1468.         return (c);
  1469.     if (alvec && *alvec) {
  1470.         alvecp = *alvec++;
  1471.         return (' ');
  1472.     }
  1473.     else {
  1474.         alvecp = NULL;
  1475.         aret = F_SEEK;
  1476.         return('\n');
  1477.     }
  1478.     }
  1479.     if (alvec) {
  1480.     if ((alvecp = *alvec) != 0) {
  1481.         alvec++;
  1482.         goto top;
  1483.     }
  1484.     /* Infinite source! */
  1485.     return ('\n');
  1486.     }
  1487.     if (evalp) {
  1488.     aret = E_SEEK;
  1489.     if ((c = *evalp++) != 0)
  1490.         return (c);
  1491.     if (evalvec && *evalvec) {
  1492.         evalp = *evalvec++;
  1493.         return (' ');
  1494.     }
  1495.     aret = F_SEEK;
  1496.     evalp = 0;
  1497.     }
  1498.     if (evalvec) {
  1499.     if (evalvec == (Char **) 1) {
  1500.         doneinp = 1;
  1501.         reset();
  1502.     }
  1503.     if ((evalp = *evalvec) != 0) {
  1504.         evalvec++;
  1505.         goto top;
  1506.     }
  1507.     evalvec = (Char **) 1;
  1508.     return ('\n');
  1509.     }
  1510.     do {
  1511.     if (arginp == (Char *) 1 || onelflg == 1) {
  1512.         if (wanteof)
  1513.         return (-1);
  1514.         exitstat();
  1515.     }
  1516.     if (arginp) {
  1517.         if ((c = *arginp++) == 0) {
  1518.         arginp = (Char *) 1;
  1519.         return ('\n');
  1520.         }
  1521.         return (c);
  1522.     }
  1523. #ifdef BSDJOBS
  1524. reread:
  1525. #endif /* BSDJOBS */
  1526.     c = bgetc();
  1527.     if (c < 0) {
  1528. #ifndef POSIX
  1529. # ifdef TERMIO
  1530.         struct termio tty;
  1531. # else /* SGTTYB */
  1532.         struct sgttyb tty;
  1533. # endif /* TERMIO */
  1534. #else /* POSIX */
  1535.         struct termios tty;
  1536. #endif /* POSIX */
  1537.         if (wanteof)
  1538.         return (-1);
  1539.         /* was isatty but raw with ignoreeof yields problems */
  1540. #ifndef POSIX
  1541. # ifdef TERMIO
  1542.         if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
  1543.         (tty.c_lflag & ICANON))
  1544. # else /* GSTTYB */
  1545.         if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
  1546.         (tty.sg_flags & RAW) == 0)
  1547. # endif /* TERMIO */
  1548. #else /* POSIX */
  1549.         if (tcgetattr(SHIN, &tty) == 0 &&
  1550.         (tty.c_lflag & ICANON))
  1551. #endif /* POSIX */
  1552.         {
  1553.         /* was 'short' for FILEC */
  1554. #ifdef BSDJOBS
  1555.         int     ctpgrp;
  1556. #endif /* BSDJOBS */
  1557.  
  1558.         if (++sincereal > 25)
  1559.             goto oops;
  1560. #ifdef BSDJOBS
  1561.         if (tpgrp != -1 &&
  1562.             (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
  1563.             tpgrp != ctpgrp) {
  1564.             (void) tcsetpgrp(FSHTTY, tpgrp);
  1565. # ifdef _SEQUENT_
  1566.             if (ctpgrp)
  1567. # endif /* _SEQUENT */
  1568.             (void) killpg((pid_t) ctpgrp, SIGHUP);
  1569.             xprintf("Reset tty pgrp from %d to %d\n", ctpgrp, tpgrp);
  1570.             goto reread;
  1571.         }
  1572. #endif /* BSDJOBS */
  1573.         if (adrof(STRignoreeof)) {
  1574.             if (loginsh)
  1575.             xprintf("\nUse \"logout\" to logout.\n");
  1576.             else
  1577.             xprintf("\nUse \"exit\" to leave tcsh.\n");
  1578.             reset();
  1579.         }
  1580.         if (chkstop == 0)
  1581.             panystop(1);
  1582.         }
  1583.     oops:
  1584.         doneinp = 1;
  1585.         reset();
  1586.     }
  1587.     sincereal = 0;
  1588.     if (c == '\n' && onelflg)
  1589.         onelflg--;
  1590.     } while (c == 0);
  1591.     if (histlinep < histline + BUFSIZE)
  1592.     *histlinep++ = (Char) c;
  1593.     return (c);
  1594. }
  1595.  
  1596. static int
  1597. bgetc()
  1598. {
  1599.     int buf;
  1600.     int c;
  1601.     int numleft = 0, roomleft;
  1602.     extern Char InputBuf[];
  1603.     char    tbuf[BUFSIZE + 1];
  1604.  
  1605.     if (cantell) {
  1606.     if (fseekp < fbobp || fseekp > feobp) {
  1607.         fbobp = feobp = fseekp;
  1608.         (void) lseek(SHIN, fseekp, L_SET);
  1609.     }
  1610.     if (fseekp == feobp) {
  1611.         int     i;
  1612.  
  1613.         fbobp = feobp;
  1614.         do
  1615.         c = read(SHIN, tbuf, BUFSIZE);
  1616.         while (c < 0 && errno == EINTR);
  1617.         if (c <= 0)
  1618.         return (-1);
  1619.         for (i = 0; i < c; i++)
  1620.         fbuf[0][i] = (unsigned char) tbuf[i];
  1621.         feobp += c;
  1622.     }
  1623.     c = fbuf[0][fseekp - fbobp];
  1624.     fseekp++;
  1625.     return (c);
  1626.     }
  1627. again:
  1628.     buf = (int) fseekp / BUFSIZE;
  1629.     if (buf >= fblocks) {
  1630.     Char **nfbuf =
  1631.     (Char **) xcalloc((size_t) (fblocks + 2),
  1632.               sizeof(Char **));
  1633.  
  1634.     if (fbuf) {
  1635.         (void) blkcpy(nfbuf, fbuf);
  1636.         xfree((ptr_t) fbuf);
  1637.     }
  1638.     fbuf = nfbuf;
  1639.     fbuf[fblocks] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
  1640.     fblocks++;
  1641.     if (!intty)
  1642.         goto again;
  1643.     }
  1644.     if (fseekp >= feobp) {
  1645.     int off;
  1646.     off = (int) feobp % BUFSIZE;
  1647.     buf = (int) feobp / BUFSIZE;
  1648.     roomleft = BUFSIZE - off;
  1649.     for (;;) {
  1650.         if (editing && intty) {    /* then use twenex routine */
  1651.         c = numleft ? numleft : Inputl();    /* PWP: get a line */
  1652.         if (c > roomleft) {    /* No room in this buffer? */
  1653.             /* start with fresh buffer */
  1654.             feobp = fseekp = fblocks * BUFSIZE;
  1655.             numleft = c;
  1656.             goto again;
  1657.         }
  1658.         if (c > 0) {
  1659.             /* 
  1660.              * Cannot really happen, but it does! 
  1661.              * I really cannot explain why the following if 
  1662.              * statement can get executed, but at least when it
  1663.              * does we are not going to core-dump any more.
  1664.              * It has something to do with a weird interaction
  1665.              * with run-fg-editor or interrupts I think, but 
  1666.              * I have not been able to pin it down!
  1667.              */
  1668.             if (buf >= fblocks || off > BUFSIZE) {
  1669.             /* start with fresh buffer */
  1670.             feobp = fseekp = fblocks * BUFSIZE;
  1671.             numleft = c;
  1672.             goto again;
  1673.             }
  1674.             (void) memmove((ptr_t) (fbuf[buf] + off), (ptr_t) InputBuf,
  1675.                    (size_t) (c * sizeof(Char)));
  1676.         }
  1677.         numleft = 0;
  1678.         }
  1679.         else {
  1680.         c = read(SHIN, tbuf, (size_t) roomleft);
  1681.         if (c > 0) {
  1682.             int     i;
  1683.             Char   *ptr = fbuf[buf] + off;
  1684.  
  1685.             for (i = 0; i < c; i++)
  1686.             ptr[i] = (unsigned char) tbuf[i];
  1687.         }
  1688.         }
  1689.         if (c >= 0)
  1690.         break;
  1691.         if ((c = fixio(SHIN, errno)) == -1)
  1692.         break;
  1693.     }
  1694.     if (c <= 0)
  1695.         return (-1);
  1696.     feobp += c;
  1697.     if (editing && !intty)
  1698.         goto again;
  1699.     }
  1700.     c = fbuf[buf][(int) fseekp % BUFSIZE];
  1701.     fseekp++;
  1702.     return (c);
  1703. }
  1704.  
  1705. static void
  1706. bfree()
  1707. {
  1708.     int sb, i;
  1709.  
  1710.     if (cantell)
  1711.     return;
  1712.     if (whyles)
  1713.     return;
  1714.     sb = (int) (fseekp - 1) / BUFSIZE;
  1715.     if (sb > 0) {
  1716.     for (i = 0; i < sb; i++)
  1717.         xfree((ptr_t) fbuf[i]);
  1718.     (void) blkcpy(fbuf, &fbuf[sb]);
  1719.     fseekp -= BUFSIZE * sb;
  1720.     feobp -= BUFSIZE * sb;
  1721.     fblocks -= sb;
  1722.     }
  1723. }
  1724.  
  1725. void
  1726. bseek(l)
  1727.     struct Ain   *l;
  1728. {
  1729.     switch (aret = l->type) {
  1730.     case E_SEEK:
  1731.     evalvec = l->a_seek;
  1732.     evalp = (Char *) l->f_seek;
  1733. #ifdef DEBUG_SEEK
  1734.     xprintf("seek to eval %x %x\n", evalvec, evalp);
  1735. #endif
  1736.     return;
  1737.     case A_SEEK:
  1738.     alvec = l->a_seek;
  1739.     alvecp = (Char *) l->f_seek;
  1740. #ifdef DEBUG_SEEK
  1741.     xprintf("seek to alias %x %x\n", alvec, alvecp);
  1742. #endif
  1743.     return;
  1744.     case F_SEEK:    
  1745. #ifdef DEBUG_SEEK
  1746.     xprintf("seek to file %x\n", fseekp);
  1747. #endif
  1748.     fseekp = l->f_seek;
  1749.     return;
  1750.     default:
  1751.     xprintf("Bad seek type %d\n", aret);
  1752.     abort();
  1753.     }
  1754. }
  1755.  
  1756. /* any similarity to bell telephone is purely accidental */
  1757. void
  1758. btell(l)
  1759. struct Ain *l;
  1760. {
  1761.     switch (l->type = aret) {
  1762.     case E_SEEK:
  1763.     l->a_seek = evalvec;
  1764.     l->f_seek = (off_t) evalp;
  1765. #ifdef DEBUG_SEEK
  1766.     xprintf("tell eval %x %x\n", evalvec, evalp);
  1767. #endif
  1768.     return;
  1769.     case A_SEEK:
  1770.     l->a_seek = alvec;
  1771.     l->f_seek = (off_t) alvecp;
  1772. #ifdef DEBUG_SEEK
  1773.     xprintf("tell alias %x %x\n", alvec, alvecp);
  1774. #endif
  1775.     return;
  1776.     case F_SEEK:
  1777.     /*SUPPRESS 112*/
  1778.     l->f_seek = fseekp;
  1779.     l->a_seek = NULL;
  1780. #ifdef DEBUG_SEEK
  1781.     xprintf("tell file %x\n", fseekp);
  1782. #endif
  1783.     return;
  1784.     default:
  1785.     xprintf("Bad seek type %d\n", aret);
  1786.     abort();
  1787.     }
  1788. }
  1789.  
  1790. void
  1791. btoeof()
  1792. {
  1793.     (void) lseek(SHIN, (off_t) 0, L_XTND);
  1794.     aret = F_SEEK;
  1795.     fseekp = feobp;
  1796.     alvec = NULL;
  1797.     alvecp = NULL;
  1798.     evalvec = NULL;
  1799.     evalp = NULL;
  1800.     wfree();
  1801.     bfree();
  1802. }
  1803.  
  1804. void
  1805. settell()
  1806. {
  1807.     off_t x;
  1808.     cantell = 0;
  1809.     if (arginp || onelflg || intty)
  1810.     return;
  1811.     if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
  1812.     return;
  1813.     fbuf = (Char **) xcalloc(2, sizeof(Char **));
  1814.     fblocks = 1;
  1815.     fbuf[0] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
  1816.     fseekp = fbobp = feobp = x;
  1817.     cantell = 1;
  1818. }
  1819.