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