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