sh.lex.c revision 195609
1195609Smp/* $Header: /p/tcsh/cvsroot/tcsh/sh.lex.c,v 3.81 2009/06/25 21:15:37 christos Exp $ */
259243Sobrien/*
359243Sobrien * sh.lex.c: Lexical analysis into tokens
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#include "sh.h"
3459243Sobrien
35195609SmpRCSID("$tcsh: sh.lex.c,v 3.81 2009/06/25 21:15:37 christos Exp $")
3659243Sobrien
3759243Sobrien#include "ed.h"
38145479Smp
39145479Smp#include <assert.h>
4059243Sobrien/* #define DEBUG_INP */
4159243Sobrien/* #define DEBUG_SEEK */
4259243Sobrien
4359243Sobrien/*
4459243Sobrien * C shell
4559243Sobrien */
4659243Sobrien
47167465Smp#define FLAG_G	1
48167465Smp#define FLAG_A	2
4959243Sobrien/*
5059243Sobrien * These lexical routines read input and form lists of words.
5159243Sobrien * There is some involved processing here, because of the complications
5259243Sobrien * of input buffering, and especially because of history substitution.
5359243Sobrien */
54167465Smpstatic	Char		*word		(int);
55167465Smpstatic	eChar	 	 getC1		(int);
56167465Smpstatic	void	 	 getdol		(void);
57167465Smpstatic	void	 	 getexcl	(Char);
58167465Smpstatic	struct Hist 	*findev		(Char *, int);
59167465Smpstatic	void	 	 setexclp	(Char *);
60167465Smpstatic	eChar	 	 bgetc		(void);
61167465Smpstatic	void		 balloc		(int);
62167465Smpstatic	void	 	 bfree		(void);
63167465Smpstatic	struct wordent	*gethent	(Char);
64167465Smpstatic	int	 	 matchs		(const Char *, const Char *);
65167465Smpstatic	int	 	 getsel		(int *, int *, int);
66167465Smpstatic	struct wordent	*getsub		(struct wordent *);
67167465Smpstatic	Char 		*subword	(Char *, Char, int *, size_t *);
68167465Smpstatic	struct wordent	*dosub		(Char, struct wordent *, int);
69167465Smpstatic	ssize_t		 wide_read	(int, Char *, size_t, int);
7059243Sobrien
7159243Sobrien/*
7259243Sobrien * Peekc is a peek character for getC, peekread for readc.
7359243Sobrien * There is a subtlety here in many places... history routines
7459243Sobrien * will read ahead and then insert stuff into the input stream.
7559243Sobrien * If they push back a character then they must push it behind
7659243Sobrien * the text substituted by the history substitution.  On the other
7759243Sobrien * hand in several places we need 2 peek characters.  To make this
7859243Sobrien * all work, the history routines read with getC, and make use both
7959243Sobrien * of ungetC and unreadc.  The key observation is that the state
8059243Sobrien * of getC at the call of a history reference is such that calls
8159243Sobrien * to getC from the history routines will always yield calls of
8259243Sobrien * readc, unless this peeking is involved.  That is to say that during
8359243Sobrien * getexcl the variables lap, exclp, and exclnxt are all zero.
8459243Sobrien *
8559243Sobrien * Getdol invokes history substitution, hence the extra peek, peekd,
8659243Sobrien * which it can ungetD to be before history substitutions.
8759243Sobrien */
8859243Sobrienstatic Char peekc = 0, peekd = 0;
8959243Sobrienstatic Char peekread = 0;
9059243Sobrien
9159243Sobrien/* (Tail of) current word from ! subst */
9259243Sobrienstatic Char *exclp = NULL;
9359243Sobrien
9459243Sobrien/* The rest of the ! subst words */
9559243Sobrienstatic struct wordent *exclnxt = NULL;
9659243Sobrien
9759243Sobrien/* Count of remaining words in ! subst */
9859243Sobrienstatic int exclc = 0;
9959243Sobrien
10059243Sobrien/* "Globp" for alias resubstitution */
10169408Sacheint aret = TCSH_F_SEEK;
10259243Sobrien
10359243Sobrien/*
10459243Sobrien * Labuf implements a general buffer for lookahead during lexical operations.
10559243Sobrien * Text which is to be placed in the input stream can be stuck here.
10659243Sobrien * We stick parsed ahead $ constructs during initial input,
10759243Sobrien * process id's from `$$', and modified variable values (from qualifiers
10859243Sobrien * during expansion in sh.dol.c) here.
10959243Sobrien */
110167465Smpstruct Strbuf labuf; /* = Strbuf_INIT; */
11159243Sobrien
11259243Sobrien/*
11359243Sobrien * Lex returns to its caller not only a wordlist (as a "var" parameter)
11459243Sobrien * but also whether a history substitution occurred.  This is used in
11559243Sobrien * the main (process) routine to determine whether to echo, and also
11659243Sobrien * when called by the alias routine to determine whether to keep the
11759243Sobrien * argument list.
11859243Sobrien */
119145479Smpstatic int hadhist = 0;
12059243Sobrien
12159243Sobrien/*
12259243Sobrien * Avoid alias expansion recursion via \!#
12359243Sobrien */
12459243Sobrienint     hleft;
12559243Sobrien
126167465Smpstruct Strbuf histline; /* = Strbuf_INIT; last line input */
12759243Sobrien
128145479Smpint    histvalid = 0;		/* is histline valid */
12959243Sobrien
13059243Sobrienstatic Char getCtmp;
13159243Sobrien
132145479Smp#define getC(f)		(((getCtmp = peekc) != '\0') ? (peekc = 0, (eChar)getCtmp) : getC1(f))
13359243Sobrien#define	ungetC(c)	peekc = (Char) c
13459243Sobrien#define	ungetD(c)	peekd = (Char) c
13559243Sobrien
13659243Sobrien/* Use Htime to store timestamps picked up from history file for enthist()
13759243Sobrien * if reading saved history (sg)
13859243Sobrien */
13959243Sobrientime_t Htime = (time_t)0;
140167465Smpstatic time_t a2time_t (Char *);
14159243Sobrien
14259243Sobrien/*
143145479Smp * special parsing rules apply for source -h
144145479Smp */
145145479Smpextern int enterhist;
146145479Smp
14759243Sobrienint
148167465Smplex(struct wordent *hp)
14959243Sobrien{
15059243Sobrien    struct wordent *wdp;
151145479Smp    eChar    c;
152145479Smp    int     parsehtime = enterhist;
15359243Sobrien
15459243Sobrien    histvalid = 0;
155167465Smp    histline.len = 0;
15659243Sobrien
15759243Sobrien    btell(&lineloc);
15859243Sobrien    hp->next = hp->prev = hp;
15959243Sobrien    hp->word = STRNULL;
16059243Sobrien    hadhist = 0;
16159243Sobrien    do
16259243Sobrien	c = readc(0);
16359243Sobrien    while (c == ' ' || c == '\t');
164145479Smp    if (c == (eChar)HISTSUB && intty)
16559243Sobrien	/* ^lef^rit	from tty is short !:s^lef^rit */
16659243Sobrien	getexcl(c);
16759243Sobrien    else
16859243Sobrien	unreadc(c);
169167465Smp    cleanup_push(hp, lex_cleanup);
17059243Sobrien    wdp = hp;
17159243Sobrien    /*
17259243Sobrien     * The following loop is written so that the links needed by freelex will
17359243Sobrien     * be ready and rarin to go even if it is interrupted.
17459243Sobrien     */
17559243Sobrien    do {
17659243Sobrien	struct wordent *new;
17759243Sobrien
178167465Smp	new = xmalloc(sizeof(*new));
179167465Smp	new->word = NULL;
18059243Sobrien	new->prev = wdp;
18159243Sobrien	new->next = hp;
18259243Sobrien	wdp->next = new;
18359243Sobrien	hp->prev = new;
18459243Sobrien	wdp = new;
185145479Smp	wdp->word = word(parsehtime);
186145479Smp	parsehtime = 0;
18759243Sobrien    } while (wdp->word[0] != '\n');
188167465Smp    cleanup_ignore(hp);
189167465Smp    cleanup_until(hp);
190167465Smp    Strbuf_terminate(&histline);
191167465Smp    if (histline.len != 0 && histline.s[histline.len - 1] == '\n')
192167465Smp	histline.s[histline.len - 1] = '\0';
193167465Smp    histvalid = 1;
19459243Sobrien
19559243Sobrien    return (hadhist);
19659243Sobrien}
19759243Sobrien
19859243Sobrienstatic time_t
199167465Smpa2time_t(Char *wordx)
20059243Sobrien{
20159243Sobrien    /* Attempt to distinguish timestamps from other possible entries.
20259243Sobrien     * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
20359243Sobrien
20459243Sobrien    time_t ret;
20559243Sobrien    Char *s;
20659243Sobrien    int ct;
20759243Sobrien
208145479Smp    if (!wordx || *(s = wordx) != '+')
20959243Sobrien	return (time_t)0;
21059243Sobrien
211167465Smp    for (++s, ret = 0, ct = 0; *s; ++s, ++ct) {
21259243Sobrien	if (!isdigit((unsigned char)*s))
21359243Sobrien	    return (time_t)0;
21459243Sobrien	ret = ret * 10 + (time_t)((unsigned char)*s - '0');
21559243Sobrien    }
21659243Sobrien
21759243Sobrien    if (ct != 10)
21859243Sobrien	return (time_t)0;
21959243Sobrien
22059243Sobrien    return ret;
22159243Sobrien}
22259243Sobrien
22359243Sobrienvoid
224167465Smpprlex(struct wordent *sp0)
22559243Sobrien{
22659243Sobrien    struct wordent *sp = sp0->next;
22759243Sobrien
22859243Sobrien    for (;;) {
22959243Sobrien	xprintf("%S", sp->word);
23059243Sobrien	sp = sp->next;
23159243Sobrien	if (sp == sp0)
23259243Sobrien	    break;
23359243Sobrien	if (sp->word[0] != '\n')
23459243Sobrien	    xputchar(' ');
23559243Sobrien    }
23659243Sobrien}
23759243Sobrien
23859243Sobrienvoid
239167465Smpcopylex(struct wordent *hp, struct wordent *fp)
24059243Sobrien{
24159243Sobrien    struct wordent *wdp;
24259243Sobrien
24359243Sobrien    wdp = hp;
24459243Sobrien    fp = fp->next;
24559243Sobrien    do {
24659243Sobrien	struct wordent *new;
247167465Smp
248167465Smp	new = xmalloc(sizeof(*new));
249167465Smp	new->word = NULL;
25059243Sobrien	new->prev = wdp;
25159243Sobrien	new->next = hp;
25259243Sobrien	wdp->next = new;
25359243Sobrien	hp->prev = new;
25459243Sobrien	wdp = new;
25559243Sobrien	wdp->word = Strsave(fp->word);
25659243Sobrien	fp = fp->next;
25759243Sobrien    } while (wdp->word[0] != '\n');
25859243Sobrien}
25959243Sobrien
26059243Sobrienvoid
261167465Smpfreelex(struct wordent *vp)
26259243Sobrien{
26359243Sobrien    struct wordent *fp;
26459243Sobrien
26559243Sobrien    while (vp->next != vp) {
26659243Sobrien	fp = vp->next;
26759243Sobrien	vp->next = fp->next;
268167465Smp	xfree(fp->word);
269167465Smp	xfree(fp);
27059243Sobrien    }
27159243Sobrien    vp->prev = vp;
27259243Sobrien}
27359243Sobrien
274167465Smpvoid
275167465Smplex_cleanup(void *xvp)
276167465Smp{
277167465Smp    struct wordent *vp;
278167465Smp
279167465Smp    vp = xvp;
280167465Smp    freelex(vp);
281167465Smp}
282167465Smp
28359243Sobrienstatic Char *
284167465Smpword(int parsehtime)
28559243Sobrien{
286145479Smp    eChar c, c1;
287167465Smp    struct Strbuf wbuf = Strbuf_INIT;
28859243Sobrien    Char    hbuf[12];
28959243Sobrien    int	    h;
290145479Smp    int dolflg;
29159243Sobrien
292167465Smp    cleanup_push(&wbuf, Strbuf_cleanup);
29359243Sobrienloop:
29459243Sobrien    while ((c = getC(DOALL)) == ' ' || c == '\t')
29559243Sobrien	continue;
29659243Sobrien    if (cmap(c, _META | _ESC))
29759243Sobrien	switch (c) {
29859243Sobrien	case '&':
29959243Sobrien	case '|':
30059243Sobrien	case '<':
30159243Sobrien	case '>':
302167465Smp	    Strbuf_append1(&wbuf, c);
30359243Sobrien	    c1 = getC(DOALL);
30459243Sobrien	    if (c1 == c)
305167465Smp		Strbuf_append1(&wbuf, c1);
30659243Sobrien	    else
30759243Sobrien		ungetC(c1);
30859243Sobrien	    goto ret;
30959243Sobrien
31059243Sobrien	case '#':
311145479Smp	    if (intty || (enterhist && !parsehtime))
31259243Sobrien		break;
31359243Sobrien	    c = 0;
31459243Sobrien	    h = 0;
31559243Sobrien	    do {
31659243Sobrien		c1 = c;
31759243Sobrien		c = getC(0);
318145479Smp		if (h < 11 && parsehtime)
31959243Sobrien		    hbuf[h++] = c;
32059243Sobrien	    } while (c != '\n');
321145479Smp	    if (parsehtime) {
322145479Smp		hbuf[11] = '\0';
323145479Smp		Htime = a2time_t(hbuf);
324145479Smp	    }
32559243Sobrien	    if (c1 == '\\')
32659243Sobrien		goto loop;
32759243Sobrien	    /*FALLTHROUGH*/
32859243Sobrien
32959243Sobrien	case ';':
33059243Sobrien	case '(':
33159243Sobrien	case ')':
33259243Sobrien	case '\n':
333167465Smp	    Strbuf_append1(&wbuf, c);
33459243Sobrien	    goto ret;
33559243Sobrien
33659243Sobrien	case '\\':
33759243Sobrien	    c = getC(0);
33859243Sobrien	    if (c == '\n') {
33959243Sobrien		if (onelflg == 1)
34059243Sobrien		    onelflg = 2;
34159243Sobrien		goto loop;
34259243Sobrien	    }
343145479Smp	    if (c != (eChar)HIST)
344167465Smp		Strbuf_append1(&wbuf, '\\');
34559243Sobrien	    c |= QUOTE;
34659243Sobrien	default:
34759243Sobrien	    break;
34859243Sobrien	}
34959243Sobrien    c1 = 0;
35059243Sobrien    dolflg = DOALL;
35159243Sobrien    for (;;) {
35259243Sobrien	if (c1) {
35359243Sobrien	    if (c == c1) {
35459243Sobrien		c1 = 0;
35559243Sobrien		dolflg = DOALL;
35659243Sobrien	    }
35759243Sobrien	    else if (c == '\\') {
35859243Sobrien		c = getC(0);
35959243Sobrien/*
36059243Sobrien * PWP: this is dumb, but how all of the other shells work.  If \ quotes
36159243Sobrien * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
36259243Sobrien * following character INSIDE a set of ''s.
36359243Sobrien *
36459243Sobrien * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
36559243Sobrien */
366145479Smp		if (c == (eChar)HIST)
36759243Sobrien		    c |= QUOTE;
36859243Sobrien		else {
36959243Sobrien		    if (bslash_quote &&
37059243Sobrien			((c == '\'') || (c == '"') ||
371195609Smp			 (c == '\\') || (c == '$'))) {
37259243Sobrien			c |= QUOTE;
37359243Sobrien		    }
37459243Sobrien		    else {
37559243Sobrien			if (c == '\n')
37659243Sobrien			    /*
37759243Sobrien			     * if (c1 == '`') c = ' '; else
37859243Sobrien			     */
37959243Sobrien			    c |= QUOTE;
38059243Sobrien			ungetC(c);
38159243Sobrien			c = '\\';
38259243Sobrien		    }
38359243Sobrien		}
38459243Sobrien	    }
38559243Sobrien	    else if (c == '\n') {
38659243Sobrien		seterror(ERR_UNMATCHED, c1);
38759243Sobrien		ungetC(c);
38859243Sobrien		break;
38959243Sobrien	    }
39059243Sobrien	}
39159243Sobrien	else if (cmap(c, _META | _QF | _QB | _ESC)) {
39259243Sobrien	    if (c == '\\') {
39359243Sobrien		c = getC(0);
39459243Sobrien		if (c == '\n') {
39559243Sobrien		    if (onelflg == 1)
39659243Sobrien			onelflg = 2;
39759243Sobrien		    break;
39859243Sobrien		}
399145479Smp		if (c != (eChar)HIST)
400167465Smp		    Strbuf_append1(&wbuf, '\\');
40159243Sobrien		c |= QUOTE;
40259243Sobrien	    }
40359243Sobrien	    else if (cmap(c, _QF | _QB)) {	/* '"` */
40459243Sobrien		c1 = c;
40559243Sobrien		dolflg = c == '"' ? DOALL : DOEXCL;
40659243Sobrien	    }
407145479Smp	    else if (c != '#' || (!intty && !enterhist)) {
40859243Sobrien		ungetC(c);
40959243Sobrien		break;
41059243Sobrien	    }
41159243Sobrien	}
412167465Smp	Strbuf_append1(&wbuf, c);
413167465Smp	c = getC(dolflg);
41459243Sobrien    }
41559243Sobrienret:
416167465Smp    cleanup_ignore(&wbuf);
417167465Smp    cleanup_until(&wbuf);
418167465Smp    return Strbuf_finish(&wbuf);
41959243Sobrien}
42059243Sobrien
421145479Smpstatic eChar
422167465SmpgetC1(int flag)
42359243Sobrien{
424145479Smp    eChar c;
42559243Sobrien
42659243Sobrien    for (;;) {
42759243Sobrien	if ((c = peekc) != 0) {
42859243Sobrien	    peekc = 0;
42959243Sobrien	    return (c);
43059243Sobrien	}
431167465Smp	if (lap < labuf.len) {
432167465Smp	    c = labuf.s[lap++];
433167465Smp	    if (cmap(c, _META | _QF | _QB))
434167465Smp		c |= QUOTE;
435167465Smp	    return (c);
43659243Sobrien	}
43759243Sobrien	if ((c = peekd) != 0) {
43859243Sobrien	    peekd = 0;
43959243Sobrien	    return (c);
44059243Sobrien	}
44159243Sobrien	if (exclp) {
44259243Sobrien	    if ((c = *exclp++) != 0)
44359243Sobrien		return (c);
44459243Sobrien	    if (exclnxt && --exclc >= 0) {
44559243Sobrien		exclnxt = exclnxt->next;
44659243Sobrien		setexclp(exclnxt->word);
44759243Sobrien		return (' ');
44859243Sobrien	    }
44959243Sobrien	    exclp = 0;
45059243Sobrien	    exclnxt = 0;
45159243Sobrien	    /* this will throw away the dummy history entries */
45259243Sobrien	    savehist(NULL, 0);
45359243Sobrien
45459243Sobrien	}
45559243Sobrien	if (exclnxt) {
45659243Sobrien	    exclnxt = exclnxt->next;
45759243Sobrien	    if (--exclc < 0)
45859243Sobrien		exclnxt = 0;
45959243Sobrien	    else
46059243Sobrien		setexclp(exclnxt->word);
46159243Sobrien	    continue;
46259243Sobrien	}
46359243Sobrien	c = readc(0);
46459243Sobrien	if (c == '$' && (flag & DODOL)) {
46559243Sobrien	    getdol();
46659243Sobrien	    continue;
46759243Sobrien	}
468145479Smp	if (c == (eChar)HIST && (flag & DOEXCL)) {
46959243Sobrien	    getexcl(0);
47059243Sobrien	    continue;
47159243Sobrien	}
47259243Sobrien	break;
47359243Sobrien    }
47459243Sobrien    return (c);
47559243Sobrien}
47659243Sobrien
47759243Sobrienstatic void
478167465Smpgetdol(void)
47959243Sobrien{
480167465Smp    struct Strbuf name = Strbuf_INIT;
481145479Smp    eChar c;
482145479Smp    eChar   sc;
483167465Smp    int    special = 0;
48459243Sobrien
48559243Sobrien    c = sc = getC(DOEXCL);
48659243Sobrien    if (any("\t \n", c)) {
48759243Sobrien	ungetD(c);
48859243Sobrien	ungetC('$' | QUOTE);
48959243Sobrien	return;
49059243Sobrien    }
491167465Smp    cleanup_push(&name, Strbuf_cleanup);
492167465Smp    Strbuf_append1(&name, '$');
49359243Sobrien    if (c == '{')
494167465Smp	Strbuf_append1(&name, c), c = getC(DOEXCL);
49559243Sobrien    if (c == '#' || c == '?' || c == '%')
496167465Smp	special++, Strbuf_append1(&name, c), c = getC(DOEXCL);
497167465Smp    Strbuf_append1(&name, c);
49859243Sobrien    switch (c) {
49959243Sobrien
50059243Sobrien    case '<':
50159243Sobrien    case '$':
50259243Sobrien    case '!':
50359243Sobrien	if (special)
50459243Sobrien	    seterror(ERR_SPDOLLT);
505167465Smp	goto end;
50659243Sobrien
50759243Sobrien    case '\n':
50859243Sobrien	ungetD(c);
509167465Smp	name.len--;
51059243Sobrien	if (!special)
51159243Sobrien	    seterror(ERR_NEWLINE);
512167465Smp	goto end;
51359243Sobrien
51459243Sobrien    case '*':
51559243Sobrien	if (special)
51659243Sobrien	    seterror(ERR_SPSTAR);
517167465Smp	goto end;
51859243Sobrien
51959243Sobrien    default:
52059243Sobrien	if (Isdigit(c)) {
52159243Sobrien#ifdef notdef
52259243Sobrien	    /* let $?0 pass for now */
52359243Sobrien	    if (special) {
52459243Sobrien		seterror(ERR_DIGIT);
525167465Smp		goto end;
52659243Sobrien	    }
52759243Sobrien#endif
52859243Sobrien	    while ((c = getC(DOEXCL)) != 0) {
52959243Sobrien		if (!Isdigit(c))
53059243Sobrien		    break;
531167465Smp		Strbuf_append1(&name, c);
53259243Sobrien	    }
53359243Sobrien	}
53459243Sobrien	else if (letter(c)) {
53559243Sobrien	    while ((c = getC(DOEXCL)) != 0) {
53659243Sobrien		/* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
53759243Sobrien		if (!letter(c) && !Isdigit(c))
53859243Sobrien		    break;
539167465Smp		Strbuf_append1(&name, c);
54059243Sobrien	    }
54159243Sobrien	}
54259243Sobrien	else {
54359243Sobrien	    if (!special)
54459243Sobrien		seterror(ERR_VARILL);
54559243Sobrien	    else {
54659243Sobrien		ungetD(c);
547167465Smp		name.len--;
54859243Sobrien	    }
549167465Smp	    goto end;
55059243Sobrien	}
55159243Sobrien	break;
55259243Sobrien    }
55359243Sobrien    if (c == '[') {
554167465Smp	Strbuf_append1(&name, c);
55559243Sobrien	do {
55659243Sobrien	    /*
55759243Sobrien	     * Michael Greim: Allow $ expansion to take place in selector
55859243Sobrien	     * expressions. (limits the number of characters returned)
55959243Sobrien	     */
56059243Sobrien	    c = getC(DOEXCL | DODOL);
56159243Sobrien	    if (c == '\n') {
56259243Sobrien		ungetD(c);
563167465Smp		name.len--;
56459243Sobrien		seterror(ERR_NLINDEX);
565167465Smp		goto end;
56659243Sobrien	    }
567167465Smp	    Strbuf_append1(&name, c);
56859243Sobrien	} while (c != ']');
56959243Sobrien	c = getC(DOEXCL);
57059243Sobrien    }
57159243Sobrien    if (c == ':') {
57259243Sobrien	/*
57359243Sobrien	 * if the :g modifier is followed by a newline, then error right away!
57459243Sobrien	 * -strike
57559243Sobrien	 */
57659243Sobrien
57759243Sobrien	int     gmodflag = 0, amodflag = 0;
57859243Sobrien
57959243Sobrien	do {
580167465Smp	    Strbuf_append1(&name, c), c = getC(DOEXCL);
58159243Sobrien	    if (c == 'g' || c == 'a') {
58259243Sobrien		if (c == 'g')
58359243Sobrien		    gmodflag++;
58459243Sobrien		else
58559243Sobrien		    amodflag++;
586167465Smp		Strbuf_append1(&name, c); c = getC(DOEXCL);
58759243Sobrien	    }
58859243Sobrien	    if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
58959243Sobrien		if (c == 'g')
59059243Sobrien		    gmodflag++;
59159243Sobrien		else
59259243Sobrien		    amodflag++;
593167465Smp		Strbuf_append1(&name, c); c = getC(DOEXCL);
59459243Sobrien	    }
595167465Smp	    Strbuf_append1(&name, c);
59659243Sobrien	    /* scan s// [eichin:19910926.0512EST] */
59759243Sobrien	    if (c == 's') {
59859243Sobrien		int delimcnt = 2;
599145479Smp		eChar delim = getC(0);
600167465Smp
601167465Smp		Strbuf_append1(&name, delim);
60259243Sobrien		if (!delim || letter(delim)
60359243Sobrien		    || Isdigit(delim) || any(" \t\n", delim)) {
60459243Sobrien		    seterror(ERR_BADSUBST);
60559243Sobrien		    break;
606167465Smp		}
607145479Smp		while ((c = getC(0)) != CHAR_ERR) {
608167465Smp		    Strbuf_append1(&name, c);
60959243Sobrien		    if(c == delim) delimcnt--;
61059243Sobrien		    if(!delimcnt) break;
61159243Sobrien		}
61259243Sobrien		if(delimcnt) {
61359243Sobrien		    seterror(ERR_BADSUBST);
61459243Sobrien		    break;
61559243Sobrien		}
61659243Sobrien		c = 's';
61759243Sobrien	    }
61859243Sobrien	    if (!any("htrqxesul", c)) {
61959243Sobrien		if ((amodflag || gmodflag) && c == '\n')
62059243Sobrien		    stderror(ERR_VARSYN);	/* strike */
62159243Sobrien		seterror(ERR_BADMOD, c);
622167465Smp		goto end;
62359243Sobrien	    }
62459243Sobrien	}
62559243Sobrien	while ((c = getC(DOEXCL)) == ':');
62659243Sobrien	ungetD(c);
62759243Sobrien    }
62859243Sobrien    else
62959243Sobrien	ungetD(c);
63059243Sobrien    if (sc == '{') {
63159243Sobrien	c = getC(DOEXCL);
63259243Sobrien	if (c != '}') {
63359243Sobrien	    ungetD(c);
63459243Sobrien	    seterror(ERR_MISSING, '}');
635167465Smp	    goto end;
63659243Sobrien	}
637167465Smp	Strbuf_append1(&name, c);
63859243Sobrien    }
639167465Smp end:
640167465Smp    cleanup_ignore(&name);
641167465Smp    cleanup_until(&name);
642167465Smp    addla(Strbuf_finish(&name));
64359243Sobrien}
64459243Sobrien
645167465Smp/* xfree()'s its argument */
64659243Sobrienvoid
647167465Smpaddla(Char *cp)
64859243Sobrien{
649167465Smp    static struct Strbuf buf; /* = Strbuf_INIT; */
65059243Sobrien
651167465Smp    buf.len = 0;
652167465Smp    Strbuf_appendn(&buf, labuf.s + lap, labuf.len - lap);
653167465Smp    labuf.len = 0;
654167465Smp    Strbuf_append(&labuf, cp);
655167465Smp    Strbuf_terminate(&labuf);
656167465Smp    Strbuf_appendn(&labuf, buf.s, buf.len);
657167465Smp    xfree(cp);
658167465Smp    lap = 0;
65959243Sobrien}
66059243Sobrien
661167465Smp/* left-hand side of last :s or search string of last ?event? */
662167465Smpstatic struct Strbuf lhsb; /* = Strbuf_INIT; */
663167465Smpstatic struct Strbuf slhs; /* = Strbuf_INIT; left-hand side of last :s */
664167465Smpstatic struct Strbuf rhsb; /* = Strbuf_INIT; right-hand side of last :s */
66559243Sobrienstatic int quesarg;
66659243Sobrien
66759243Sobrienstatic void
668167465Smpgetexcl(Char sc)
66959243Sobrien{
67059243Sobrien    struct wordent *hp, *ip;
67159243Sobrien    int     left, right, dol;
672145479Smp    eChar c;
67359243Sobrien
67459243Sobrien    if (sc == 0) {
67559243Sobrien	sc = getC(0);
67659243Sobrien	if (sc != '{') {
67759243Sobrien	    ungetC(sc);
67859243Sobrien	    sc = 0;
67959243Sobrien	}
68059243Sobrien    }
68159243Sobrien    quesarg = -1;
68259243Sobrien
683167465Smp    lastev = eventno;
68459243Sobrien    hp = gethent(sc);
68559243Sobrien    if (hp == 0)
68659243Sobrien	return;
68759243Sobrien    hadhist = 1;
68859243Sobrien    dol = 0;
68959243Sobrien    if (hp == alhistp)
69059243Sobrien	for (ip = hp->next->next; ip != alhistt; ip = ip->next)
69159243Sobrien	    dol++;
69259243Sobrien    else
69359243Sobrien	for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
69459243Sobrien	    dol++;
69559243Sobrien    left = 0, right = dol;
69659243Sobrien    if (sc == HISTSUB) {
69759243Sobrien	ungetC('s'), unreadc(HISTSUB), c = ':';
69859243Sobrien	goto subst;
69959243Sobrien    }
70059243Sobrien    c = getC(0);
70159243Sobrien    if (!any(":^$*-%", c))
70259243Sobrien	goto subst;
70359243Sobrien    left = right = -1;
70459243Sobrien    if (c == ':') {
70559243Sobrien	c = getC(0);
70659243Sobrien	unreadc(c);
70759243Sobrien	if (letter(c) || c == '&') {
70859243Sobrien	    c = ':';
70959243Sobrien	    left = 0, right = dol;
71059243Sobrien	    goto subst;
71159243Sobrien	}
71259243Sobrien    }
71359243Sobrien    else
71459243Sobrien	ungetC(c);
71559243Sobrien    if (!getsel(&left, &right, dol))
71659243Sobrien	return;
71759243Sobrien    c = getC(0);
71859243Sobrien    if (c == '*')
71959243Sobrien	ungetC(c), c = '-';
72059243Sobrien    if (c == '-') {
72159243Sobrien	if (!getsel(&left, &right, dol))
72259243Sobrien	    return;
72359243Sobrien	c = getC(0);
72459243Sobrien    }
72559243Sobriensubst:
72659243Sobrien    exclc = right - left + 1;
72759243Sobrien    while (--left >= 0)
72859243Sobrien	hp = hp->next;
72959243Sobrien    if (sc == HISTSUB || c == ':') {
73059243Sobrien	do {
73159243Sobrien	    hp = getsub(hp);
73259243Sobrien	    c = getC(0);
73359243Sobrien	} while (c == ':');
73459243Sobrien    }
73559243Sobrien    unreadc(c);
73659243Sobrien    if (sc == '{') {
73759243Sobrien	c = getC(0);
73859243Sobrien	if (c != '}')
73959243Sobrien	    seterror(ERR_BADBANG);
74059243Sobrien    }
74159243Sobrien    exclnxt = hp;
74259243Sobrien}
74359243Sobrien
74459243Sobrienstatic struct wordent *
745167465Smpgetsub(struct wordent *en)
74659243Sobrien{
747145479Smp    eChar   delim;
748145479Smp    eChar   c;
749145479Smp    eChar   sc;
750145479Smp    int global;
75159243Sobrien
75259243Sobrien    do {
75359243Sobrien	exclnxt = 0;
75459243Sobrien	global = 0;
75559243Sobrien	sc = c = getC(0);
756167465Smp	while (c == 'g' || c == 'a') {
757167465Smp	    global |= (c == 'g') ? FLAG_G : FLAG_A;
75859243Sobrien	    sc = c = getC(0);
75959243Sobrien	}
76059243Sobrien
76159243Sobrien	switch (c) {
76259243Sobrien	case 'p':
76359243Sobrien	    justpr++;
76459243Sobrien	    return (en);
76559243Sobrien
76659243Sobrien	case 'x':
76759243Sobrien	case 'q':
768167465Smp	    global |= FLAG_G;
76959243Sobrien	    /*FALLTHROUGH*/
77059243Sobrien
77159243Sobrien	case 'h':
77259243Sobrien	case 'r':
77359243Sobrien	case 't':
77459243Sobrien	case 'e':
77559243Sobrien	case 'u':
77659243Sobrien	case 'l':
77759243Sobrien	    break;
77859243Sobrien
77959243Sobrien	case '&':
780167465Smp	    if (slhs.len == 0) {
78159243Sobrien		seterror(ERR_NOSUBST);
78259243Sobrien		return (en);
78359243Sobrien	    }
784167465Smp	    lhsb.len = 0;
785167465Smp	    Strbuf_append(&lhsb, slhs.s);
786167465Smp	    Strbuf_terminate(&lhsb);
78759243Sobrien	    break;
78859243Sobrien
78959243Sobrien#ifdef notdef
79059243Sobrien	case '~':
791167465Smp	    if (lhsb.len == 0)
79259243Sobrien		goto badlhs;
79359243Sobrien	    break;
79459243Sobrien#endif
79559243Sobrien
79659243Sobrien	case 's':
79759243Sobrien	    delim = getC(0);
79859243Sobrien	    if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
79959243Sobrien		unreadc(delim);
800167465Smp		lhsb.len = 0;
80159243Sobrien		seterror(ERR_BADSUBST);
80259243Sobrien		return (en);
80359243Sobrien	    }
804167465Smp	    Strbuf_terminate(&lhsb);
805167465Smp	    lhsb.len = 0;
80659243Sobrien	    for (;;) {
80759243Sobrien		c = getC(0);
80859243Sobrien		if (c == '\n') {
80959243Sobrien		    unreadc(c);
81059243Sobrien		    break;
81159243Sobrien		}
81259243Sobrien		if (c == delim)
81359243Sobrien		    break;
81459243Sobrien		if (c == '\\') {
81559243Sobrien		    c = getC(0);
81659243Sobrien		    if (c != delim && c != '\\')
817167465Smp			Strbuf_append1(&lhsb, '\\');
81859243Sobrien		}
819167465Smp		Strbuf_append1(&lhsb, c);
82059243Sobrien	    }
821167465Smp	    if (lhsb.len != 0)
822167465Smp		Strbuf_terminate(&lhsb);
823167465Smp	    else if (lhsb.s[0] == 0) {
82459243Sobrien		seterror(ERR_LHS);
82559243Sobrien		return (en);
826167465Smp	    } else
827167465Smp		lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
828167465Smp	    rhsb.len = 0;
82959243Sobrien	    for (;;) {
83059243Sobrien		c = getC(0);
83159243Sobrien		if (c == '\n') {
83259243Sobrien		    unreadc(c);
83359243Sobrien		    break;
83459243Sobrien		}
83559243Sobrien		if (c == delim)
83659243Sobrien		    break;
83759243Sobrien		if (c == '\\') {
83859243Sobrien		    c = getC(0);
83959243Sobrien		    if (c != delim /* && c != '~' */ )
840167465Smp			Strbuf_append1(&rhsb,  '\\');
84159243Sobrien		}
842167465Smp		Strbuf_append1(&rhsb, c);
84359243Sobrien	    }
844167465Smp	    Strbuf_terminate(&rhsb);
84559243Sobrien	    break;
84659243Sobrien
84759243Sobrien	default:
84859243Sobrien	    if (c == '\n')
84959243Sobrien		unreadc(c);
850145479Smp	    seterror(ERR_BADBANGMOD, (int)c);
85159243Sobrien	    return (en);
85259243Sobrien	}
853167465Smp	slhs.len = 0;
854177128Sdelphij	if (lhsb.s != NULL && lhsb.len != 0)
855177128Sdelphij	    Strbuf_append(&slhs, lhsb.s);
856167465Smp	Strbuf_terminate(&slhs);
85759243Sobrien	if (exclc)
85859243Sobrien	    en = dosub(sc, en, global);
85959243Sobrien    }
86059243Sobrien    while ((c = getC(0)) == ':');
86159243Sobrien    unreadc(c);
86259243Sobrien    return (en);
86359243Sobrien}
86459243Sobrien
86559243Sobrien/*
86659243Sobrien *
86759243Sobrien * From Beto Appleton (beto@aixwiz.austin.ibm.com)
86859243Sobrien *
86959243Sobrien * when using history substitution, and the variable
87059243Sobrien * 'history' is set to a value higher than 1000,
87159243Sobrien * the shell might either freeze (hang) or core-dump.
87259243Sobrien * We raise the limit to 50000000
87359243Sobrien */
87459243Sobrien
87559243Sobrien#define HIST_PURGE -50000000
87659243Sobrienstatic struct wordent *
877167465Smpdosub(Char sc, struct wordent *en, int global)
87859243Sobrien{
87959243Sobrien    struct wordent lexi;
880145479Smp    int    didsub = 0, didone = 0;
88159243Sobrien    struct wordent *hp = &lexi;
88259243Sobrien    struct wordent *wdp;
88359243Sobrien    int i = exclc;
88459243Sobrien    struct Hist *hst;
88559243Sobrien
88659243Sobrien    wdp = hp;
88759243Sobrien    while (--i >= 0) {
888167465Smp	struct wordent *new = xcalloc(1, sizeof *wdp);
88959243Sobrien
89059243Sobrien	new->word = 0;
89159243Sobrien	new->prev = wdp;
89259243Sobrien	new->next = hp;
89359243Sobrien	wdp->next = new;
89459243Sobrien	wdp = new;
89559243Sobrien	en = en->next;
89659243Sobrien	if (en->word) {
89759243Sobrien	    Char *tword, *otword;
89859243Sobrien
899167465Smp	    if ((global & FLAG_G) || didsub == 0) {
900167465Smp		size_t pos;
901167465Smp
902167465Smp		pos = 0;
903167465Smp		tword = subword(en->word, sc, &didone, &pos);
90459243Sobrien		if (didone)
90559243Sobrien		    didsub = 1;
906167465Smp		if (global & FLAG_A) {
90759243Sobrien		    while (didone && tword != STRNULL) {
90859243Sobrien			otword = tword;
909167465Smp			tword = subword(otword, sc, &didone, &pos);
91059243Sobrien			if (Strcmp(tword, otword) == 0) {
911167465Smp			    xfree(otword);
91259243Sobrien			    break;
91359243Sobrien			}
91459243Sobrien			else
915167465Smp			    xfree(otword);
91659243Sobrien		    }
91759243Sobrien		}
91859243Sobrien	    }
91959243Sobrien	    else
92059243Sobrien		tword = Strsave(en->word);
92159243Sobrien	    wdp->word = tword;
92259243Sobrien	}
92359243Sobrien    }
92459243Sobrien    if (didsub == 0)
92559243Sobrien	seterror(ERR_MODFAIL);
92659243Sobrien    hp->prev = wdp;
92759243Sobrien    /*
92859243Sobrien     * ANSI mode HP/UX compiler chokes on
92959243Sobrien     * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
93059243Sobrien     */
93159243Sobrien    hst = enthist(HIST_PURGE, &lexi, 0, 0);
93259243Sobrien    return &(hst->Hlex);
93359243Sobrien}
93459243Sobrien
935167465Smp/* Return a newly allocated result of one modification of CP using the
936167465Smp   operation TYPE.  Set ADID to 1 if a modification was performed.
937167465Smp   If TYPE == 's', perform substitutions only from *START_POS on and set
938167465Smp   *START_POS to the position of next substitution attempt. */
93959243Sobrienstatic Char *
940167465Smpsubword(Char *cp, Char type, int *adid, size_t *start_pos)
94159243Sobrien{
942167465Smp    Char *wp;
943167465Smp    const Char *mp, *np;
94459243Sobrien
94559243Sobrien    switch (type) {
94659243Sobrien
94759243Sobrien    case 'r':
94859243Sobrien    case 'e':
94959243Sobrien    case 'h':
95059243Sobrien    case 't':
95159243Sobrien    case 'q':
95259243Sobrien    case 'x':
95359243Sobrien    case 'u':
95459243Sobrien    case 'l':
95559243Sobrien	wp = domod(cp, type);
956167465Smp	if (wp == 0) {
957167465Smp	    *adid = 0;
95859243Sobrien	    return (Strsave(cp));
959167465Smp	}
96059243Sobrien	*adid = 1;
96159243Sobrien	return (wp);
96259243Sobrien
96359243Sobrien    default:
964167465Smp	for (mp = cp + *start_pos; *mp; mp++) {
965167465Smp	    if (matchs(mp, lhsb.s)) {
966167465Smp		struct Strbuf wbuf = Strbuf_INIT;
967167465Smp
968167465Smp		Strbuf_appendn(&wbuf, cp, mp - cp);
969167465Smp		for (np = rhsb.s; *np; np++)
97059243Sobrien		    switch (*np) {
97159243Sobrien
97259243Sobrien		    case '\\':
97359243Sobrien			if (np[1] == '&')
97459243Sobrien			    np++;
97559243Sobrien			/* fall into ... */
97659243Sobrien
97759243Sobrien		    default:
978167465Smp			Strbuf_append1(&wbuf, *np);
97959243Sobrien			continue;
98059243Sobrien
98159243Sobrien		    case '&':
982167465Smp			Strbuf_append(&wbuf, lhsb.s);
98359243Sobrien			continue;
98459243Sobrien		    }
985167465Smp		*start_pos = wbuf.len;
986167465Smp		Strbuf_append(&wbuf, mp + lhsb.len);
98759243Sobrien		*adid = 1;
988167465Smp		return Strbuf_finish(&wbuf);
98959243Sobrien	    }
990167465Smp	}
991167465Smp	*adid = 0;
99259243Sobrien	return (Strsave(cp));
99359243Sobrien    }
99459243Sobrien}
99559243Sobrien
99659243SobrienChar   *
997167465Smpdomod(Char *cp, Char type)
99859243Sobrien{
99959243Sobrien    Char *wp, *xp;
100059243Sobrien    int c;
100159243Sobrien
100259243Sobrien    switch (type) {
100359243Sobrien
100459243Sobrien    case 'x':
100559243Sobrien    case 'q':
100659243Sobrien	wp = Strsave(cp);
100759243Sobrien	for (xp = wp; (c = *xp) != 0; xp++)
100859243Sobrien	    if ((c != ' ' && c != '\t') || type == 'q')
100959243Sobrien		*xp |= QUOTE;
101059243Sobrien	return (wp);
101159243Sobrien
101259243Sobrien    case 'l':
1013145479Smp	wp = NLSChangeCase(cp, 1);
1014145479Smp	return wp ? wp : Strsave(cp);
101559243Sobrien
101659243Sobrien    case 'u':
1017145479Smp	wp = NLSChangeCase(cp, 0);
1018145479Smp	return wp ? wp : Strsave(cp);
101959243Sobrien
102059243Sobrien    case 'h':
102159243Sobrien    case 't':
102259243Sobrien	if (!any(short2str(cp), '/'))
102359243Sobrien	    return (type == 't' ? Strsave(cp) : 0);
1024167465Smp	wp = Strrchr(cp, '/');
102559243Sobrien	if (type == 'h')
1026167465Smp	    xp = Strnsave(cp, wp - cp);
102759243Sobrien	else
102859243Sobrien	    xp = Strsave(wp + 1);
102959243Sobrien	return (xp);
103059243Sobrien
103159243Sobrien    case 'e':
103259243Sobrien    case 'r':
103359243Sobrien	wp = Strend(cp);
103459243Sobrien	for (wp--; wp >= cp && *wp != '/'; wp--)
103559243Sobrien	    if (*wp == '.') {
103659243Sobrien		if (type == 'e')
103759243Sobrien		    xp = Strsave(wp + 1);
103859243Sobrien		else
1039167465Smp		    xp = Strnsave(cp, wp - cp);
104059243Sobrien		return (xp);
104159243Sobrien	    }
104259243Sobrien	return (Strsave(type == 'e' ? STRNULL : cp));
104359243Sobrien    default:
104459243Sobrien	break;
104559243Sobrien    }
104659243Sobrien    return (0);
104759243Sobrien}
104859243Sobrien
104959243Sobrienstatic int
1050167465Smpmatchs(const Char *str, const Char *pat)
105159243Sobrien{
105259243Sobrien    while (*str && *pat && *str == *pat)
105359243Sobrien	str++, pat++;
105459243Sobrien    return (*pat == 0);
105559243Sobrien}
105659243Sobrien
105759243Sobrienstatic int
1058167465Smpgetsel(int *al, int *ar, int dol)
105959243Sobrien{
1060145479Smp    eChar c = getC(0);
106159243Sobrien    int i;
1062145479Smp    int    first = *al < 0;
106359243Sobrien
106459243Sobrien    switch (c) {
106559243Sobrien
106659243Sobrien    case '%':
106759243Sobrien	if (quesarg == -1) {
106859243Sobrien	    seterror(ERR_BADBANGARG);
106959243Sobrien	    return (0);
107059243Sobrien	}
107159243Sobrien	if (*al < 0)
107259243Sobrien	    *al = quesarg;
107359243Sobrien	*ar = quesarg;
107459243Sobrien	break;
107559243Sobrien
107659243Sobrien    case '-':
107759243Sobrien	if (*al < 0) {
107859243Sobrien	    *al = 0;
107959243Sobrien	    *ar = dol - 1;
108059243Sobrien	    unreadc(c);
108159243Sobrien	}
108259243Sobrien	return (1);
108359243Sobrien
108459243Sobrien    case '^':
108559243Sobrien	if (*al < 0)
108659243Sobrien	    *al = 1;
108759243Sobrien	*ar = 1;
108859243Sobrien	break;
108959243Sobrien
109059243Sobrien    case '$':
109159243Sobrien	if (*al < 0)
109259243Sobrien	    *al = dol;
109359243Sobrien	*ar = dol;
109459243Sobrien	break;
109559243Sobrien
109659243Sobrien    case '*':
109759243Sobrien	if (*al < 0)
109859243Sobrien	    *al = 1;
109959243Sobrien	*ar = dol;
110059243Sobrien	if (*ar < *al) {
110159243Sobrien	    *ar = 0;
110259243Sobrien	    *al = 1;
110359243Sobrien	    return (1);
110459243Sobrien	}
110559243Sobrien	break;
110659243Sobrien
110759243Sobrien    default:
110859243Sobrien	if (Isdigit(c)) {
110959243Sobrien	    i = 0;
111059243Sobrien	    while (Isdigit(c)) {
111159243Sobrien		i = i * 10 + c - '0';
111259243Sobrien		c = getC(0);
111359243Sobrien	    }
111459243Sobrien	    if (i < 0)
111559243Sobrien		i = dol + 1;
111659243Sobrien	    if (*al < 0)
111759243Sobrien		*al = i;
111859243Sobrien	    *ar = i;
111959243Sobrien	}
112059243Sobrien	else if (*al < 0)
112159243Sobrien	    *al = 0, *ar = dol;
112259243Sobrien	else
112359243Sobrien	    *ar = dol - 1;
112459243Sobrien	unreadc(c);
112559243Sobrien	break;
112659243Sobrien    }
112759243Sobrien    if (first) {
112859243Sobrien	c = getC(0);
112959243Sobrien	unreadc(c);
113059243Sobrien	if (any("-$*", c))
113159243Sobrien	    return (1);
113259243Sobrien    }
113359243Sobrien    if (*al > *ar || *ar > dol) {
113459243Sobrien	seterror(ERR_BADBANGARG);
113559243Sobrien	return (0);
113659243Sobrien    }
113759243Sobrien    return (1);
113859243Sobrien
113959243Sobrien}
114059243Sobrien
114159243Sobrienstatic struct wordent *
1142167465Smpgethent(Char sc)
114359243Sobrien{
114459243Sobrien    struct Hist *hp;
114559243Sobrien    Char *np;
1146145479Smp    eChar c;
114759243Sobrien    int     event;
1148145479Smp    int    back = 0;
114959243Sobrien
1150145479Smp    c = sc == HISTSUB ? (eChar)HIST : getC(0);
1151145479Smp    if (c == (eChar)HIST) {
115259243Sobrien	if (alhistp)
115359243Sobrien	    return (alhistp);
115459243Sobrien	event = eventno;
115559243Sobrien    }
115659243Sobrien    else
115759243Sobrien	switch (c) {
115859243Sobrien
115959243Sobrien	case ':':
116059243Sobrien	case '^':
116159243Sobrien	case '$':
116259243Sobrien	case '*':
116359243Sobrien	case '%':
116459243Sobrien	    ungetC(c);
116559243Sobrien	    if (lastev == eventno && alhistp)
116659243Sobrien		return (alhistp);
116759243Sobrien	    event = lastev;
116859243Sobrien	    break;
116959243Sobrien
117059243Sobrien	case '#':		/* !# is command being typed in (mrh) */
117159243Sobrien	    if (--hleft == 0) {
117259243Sobrien		seterror(ERR_HISTLOOP);
117359243Sobrien		return (0);
117459243Sobrien	    }
117559243Sobrien	    else
117659243Sobrien		return (&paraml);
117759243Sobrien	    /* NOTREACHED */
117859243Sobrien
117959243Sobrien	case '-':
118059243Sobrien	    back = 1;
118159243Sobrien	    c = getC(0);
118259243Sobrien	    /* FALLSTHROUGH */
118359243Sobrien
118459243Sobrien	default:
118559243Sobrien	    if (any("(=~", c)) {
118659243Sobrien		unreadc(c);
118759243Sobrien		ungetC(HIST);
118859243Sobrien		return (0);
118959243Sobrien	    }
1190167465Smp	    Strbuf_terminate(&lhsb);
1191167465Smp	    lhsb.len = 0;
119259243Sobrien	    event = 0;
119359243Sobrien	    while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) {
119459243Sobrien		if (event != -1 && Isdigit(c))
119559243Sobrien		    event = event * 10 + c - '0';
119659243Sobrien		else
119759243Sobrien		    event = -1;
1198167465Smp		Strbuf_append1(&lhsb, c);
119959243Sobrien		c = getC(0);
120059243Sobrien	    }
120159243Sobrien	    unreadc(c);
1202167465Smp	    if (lhsb.len == 0) {
1203167465Smp		lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
120459243Sobrien		ungetC(HIST);
120559243Sobrien		return (0);
120659243Sobrien	    }
1207167465Smp	    Strbuf_terminate(&lhsb);
120859243Sobrien	    if (event != -1) {
120959243Sobrien		/*
121059243Sobrien		 * History had only digits
121159243Sobrien		 */
121259243Sobrien		if (back)
1213167465Smp		    event = eventno + (alhistp == 0) - event;
121459243Sobrien		break;
121559243Sobrien	    }
121659243Sobrien	    if (back) {
1217167465Smp		Strbuf_append1(&lhsb, '\0'); /* Allocate space */
1218167465Smp		Strbuf_terminate(&lhsb);
1219167465Smp		memmove(lhsb.s + 1, lhsb.s, (lhsb.len - 1) * sizeof (*lhsb.s));
1220167465Smp		lhsb.s[0] = '-';
122159243Sobrien	    }
1222167465Smp	    hp = findev(lhsb.s, 0);
122359243Sobrien	    if (hp)
122459243Sobrien		lastev = hp->Hnum;
122559243Sobrien	    return (&hp->Hlex);
122659243Sobrien
122759243Sobrien	case '?':
1228167465Smp	    Strbuf_terminate(&lhsb);
1229167465Smp	    lhsb.len = 0;
123059243Sobrien	    for (;;) {
123159243Sobrien		c = getC(0);
123259243Sobrien		if (c == '\n') {
123359243Sobrien		    unreadc(c);
123459243Sobrien		    break;
123559243Sobrien		}
123659243Sobrien		if (c == '?')
123759243Sobrien		    break;
1238167465Smp		Strbuf_append1(&lhsb, c);
123959243Sobrien	    }
1240167465Smp	    if (lhsb.len == 0) {
1241167465Smp		lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1242167465Smp		if (lhsb.len == 0) {
124359243Sobrien		    seterror(ERR_NOSEARCH);
124459243Sobrien		    return (0);
124559243Sobrien		}
124659243Sobrien	    }
124759243Sobrien	    else
1248167465Smp		Strbuf_terminate(&lhsb);
1249167465Smp	    hp = findev(lhsb.s, 1);
125059243Sobrien	    if (hp)
125159243Sobrien		lastev = hp->Hnum;
125259243Sobrien	    return (&hp->Hlex);
125359243Sobrien	}
125459243Sobrien
125559243Sobrien    for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
125659243Sobrien	if (hp->Hnum == event) {
125759243Sobrien	    hp->Href = eventno;
125859243Sobrien	    lastev = hp->Hnum;
125959243Sobrien	    return (&hp->Hlex);
126059243Sobrien	}
126159243Sobrien    np = putn(event);
126259243Sobrien    seterror(ERR_NOEVENT, short2str(np));
1263167465Smp    xfree(np);
126459243Sobrien    return (0);
126559243Sobrien}
126659243Sobrien
126759243Sobrienstatic struct Hist *
1268167465Smpfindev(Char *cp, int anyarg)
126959243Sobrien{
127059243Sobrien    struct Hist *hp;
127159243Sobrien
127259243Sobrien    for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
127359243Sobrien	Char   *dp;
127459243Sobrien	Char *p, *q;
127559243Sobrien	struct wordent *lp = hp->Hlex.next;
127659243Sobrien	int     argno = 0;
127759243Sobrien
127859243Sobrien	/*
127959243Sobrien	 * The entries added by alias substitution don't have a newline but do
128059243Sobrien	 * have a negative event number. Savehist() trims off these entries,
128159243Sobrien	 * but it happens before alias expansion, too early to delete those
128259243Sobrien	 * from the previous command.
128359243Sobrien	 */
128459243Sobrien	if (hp->Hnum < 0)
128559243Sobrien	    continue;
128659243Sobrien	if (lp->word[0] == '\n')
128759243Sobrien	    continue;
128859243Sobrien	if (!anyarg) {
128959243Sobrien	    p = cp;
129059243Sobrien	    q = lp->word;
129159243Sobrien	    do
129259243Sobrien		if (!*p)
129359243Sobrien		    return (hp);
129459243Sobrien	    while (*p++ == *q++);
129559243Sobrien	    continue;
129659243Sobrien	}
129759243Sobrien	do {
129859243Sobrien	    for (dp = lp->word; *dp; dp++) {
129959243Sobrien		p = cp;
130059243Sobrien		q = dp;
130159243Sobrien		do
130259243Sobrien		    if (!*p) {
130359243Sobrien			quesarg = argno;
130459243Sobrien			return (hp);
130559243Sobrien		    }
130659243Sobrien		while (*p++ == *q++);
130759243Sobrien	    }
130859243Sobrien	    lp = lp->next;
130959243Sobrien	    argno++;
131059243Sobrien	} while (lp->word[0] != '\n');
131159243Sobrien    }
131259243Sobrien    seterror(ERR_NOEVENT, short2str(cp));
131359243Sobrien    return (0);
131459243Sobrien}
131559243Sobrien
131659243Sobrien
131759243Sobrienstatic void
1318167465Smpsetexclp(Char *cp)
131959243Sobrien{
132059243Sobrien    if (cp && cp[0] == '\n')
132159243Sobrien	return;
132259243Sobrien    exclp = cp;
132359243Sobrien}
132459243Sobrien
132559243Sobrienvoid
1326167465Smpunreadc(Char c)
132759243Sobrien{
132859243Sobrien    peekread = (Char) c;
132959243Sobrien}
133059243Sobrien
1331145479SmpeChar
1332167465Smpreadc(int wanteof)
133359243Sobrien{
1334145479Smp    eChar c;
133559243Sobrien    static  int sincereal;	/* Number of real EOFs we've seen */
133659243Sobrien
133759243Sobrien#ifdef DEBUG_INP
133859243Sobrien    xprintf("readc\n");
133959243Sobrien#endif
134059243Sobrien    if ((c = peekread) != 0) {
134159243Sobrien	peekread = 0;
134259243Sobrien	return (c);
134359243Sobrien    }
134459243Sobrien
134559243Sobrientop:
134669408Sache    aret = TCSH_F_SEEK;
134759243Sobrien    if (alvecp) {
134859243Sobrien	arun = 1;
134959243Sobrien#ifdef DEBUG_INP
135059243Sobrien	xprintf("alvecp %c\n", *alvecp & 0xff);
135159243Sobrien#endif
135269408Sache	aret = TCSH_A_SEEK;
135359243Sobrien	if ((c = *alvecp++) != 0)
135459243Sobrien	    return (c);
135559243Sobrien	if (alvec && *alvec) {
135659243Sobrien		alvecp = *alvec++;
135759243Sobrien		return (' ');
135859243Sobrien	}
135959243Sobrien	else {
136059243Sobrien	    alvecp = NULL;
136169408Sache	    aret = TCSH_F_SEEK;
136259243Sobrien	    return('\n');
136359243Sobrien	}
136459243Sobrien    }
136559243Sobrien    if (alvec) {
136659243Sobrien	arun = 1;
136759243Sobrien	if ((alvecp = *alvec) != 0) {
136859243Sobrien	    alvec++;
136959243Sobrien	    goto top;
137059243Sobrien	}
137159243Sobrien	/* Infinite source! */
137259243Sobrien	return ('\n');
137359243Sobrien    }
137459243Sobrien    arun = 0;
137559243Sobrien    if (evalp) {
137669408Sache	aret = TCSH_E_SEEK;
137759243Sobrien	if ((c = *evalp++) != 0)
137859243Sobrien	    return (c);
137959243Sobrien	if (evalvec && *evalvec) {
138059243Sobrien	    evalp = *evalvec++;
138159243Sobrien	    return (' ');
138259243Sobrien	}
138369408Sache	aret = TCSH_F_SEEK;
138459243Sobrien	evalp = 0;
138559243Sobrien    }
138659243Sobrien    if (evalvec) {
138759243Sobrien	if (evalvec == INVPPTR) {
138859243Sobrien	    doneinp = 1;
138959243Sobrien	    reset();
139059243Sobrien	}
139159243Sobrien	if ((evalp = *evalvec) != 0) {
139259243Sobrien	    evalvec++;
139359243Sobrien	    goto top;
139459243Sobrien	}
139559243Sobrien	evalvec = INVPPTR;
139659243Sobrien	return ('\n');
139759243Sobrien    }
139859243Sobrien    do {
139959243Sobrien	if (arginp == INVPTR || onelflg == 1) {
140059243Sobrien	    if (wanteof)
1401145479Smp		return CHAR_ERR;
140259243Sobrien	    exitstat();
140359243Sobrien	}
140459243Sobrien	if (arginp) {
140559243Sobrien	    if ((c = *arginp++) == 0) {
140659243Sobrien		arginp = INVPTR;
140759243Sobrien		return ('\n');
140859243Sobrien	    }
140959243Sobrien	    return (c);
141059243Sobrien	}
141159243Sobrien#ifdef BSDJOBS
141259243Sobrienreread:
141359243Sobrien#endif /* BSDJOBS */
141459243Sobrien	c = bgetc();
1415145479Smp	if (c == CHAR_ERR) {
141669408Sache#ifndef WINNT_NATIVE
141759243Sobrien# ifndef POSIX
141859243Sobrien#  ifdef TERMIO
141959243Sobrien	    struct termio tty;
142059243Sobrien#  else /* SGTTYB */
142159243Sobrien	    struct sgttyb tty;
142259243Sobrien#  endif /* TERMIO */
142359243Sobrien# else /* POSIX */
142459243Sobrien	    struct termios tty;
142559243Sobrien# endif /* POSIX */
142669408Sache#endif /* !WINNT_NATIVE */
142759243Sobrien	    if (wanteof)
1428145479Smp		return CHAR_ERR;
142959243Sobrien	    /* was isatty but raw with ignoreeof yields problems */
143069408Sache#ifndef WINNT_NATIVE
143159243Sobrien# ifndef POSIX
143259243Sobrien#  ifdef TERMIO
143359243Sobrien	    if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
143459243Sobrien		(tty.c_lflag & ICANON))
143559243Sobrien#  else /* GSTTYB */
143659243Sobrien	    if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
143759243Sobrien		(tty.sg_flags & RAW) == 0)
143859243Sobrien#  endif /* TERMIO */
143959243Sobrien# else /* POSIX */
144059243Sobrien	    if (tcgetattr(SHIN, &tty) == 0 &&
144159243Sobrien		(tty.c_lflag & ICANON))
144259243Sobrien# endif /* POSIX */
144369408Sache#else /* WINNT_NATIVE */
144459243Sobrien	    if (isatty(SHIN))
144569408Sache#endif /* !WINNT_NATIVE */
144659243Sobrien	    {
144759243Sobrien#ifdef BSDJOBS
1448167465Smp		pid_t ctpgrp;
144959243Sobrien#endif /* BSDJOBS */
145059243Sobrien
1451100616Smp		if (numeof != 0 && ++sincereal >= numeof)	/* Too many EOFs?  Bye! */
145259243Sobrien		    goto oops;
145359243Sobrien#ifdef BSDJOBS
145459243Sobrien		if (tpgrp != -1 &&
145559243Sobrien		    (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
145659243Sobrien		    tpgrp != ctpgrp) {
145759243Sobrien		    (void) tcsetpgrp(FSHTTY, tpgrp);
145859243Sobrien# ifdef _SEQUENT_
145959243Sobrien		    if (ctpgrp)
146059243Sobrien# endif /* _SEQUENT */
1461167465Smp		    (void) killpg(ctpgrp, SIGHUP);
146259243Sobrien# ifdef notdef
146359243Sobrien		    /*
146459243Sobrien		     * With the walking process group fix, this message
146559243Sobrien		     * is now obsolete. As the foreground process group
146659243Sobrien		     * changes, the shell needs to adjust. Well too bad.
146759243Sobrien		     */
146859243Sobrien		    xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1469167465Smp			    (int)ctpgrp, (int)tpgrp);
147059243Sobrien# endif /* notdef */
147159243Sobrien		    goto reread;
147259243Sobrien		}
147359243Sobrien#endif /* BSDJOBS */
147459243Sobrien		/* What follows is complicated EOF handling -- sterling@netcom.com */
147559243Sobrien		/* First, we check to see if we have ignoreeof set */
147659243Sobrien		if (adrof(STRignoreeof)) {
147759243Sobrien			/* If so, we check for any stopped jobs only on the first EOF */
147859243Sobrien			if ((sincereal == 1) && (chkstop == 0)) {
147959243Sobrien				panystop(1);
148059243Sobrien			}
148159243Sobrien		} else {
148259243Sobrien			/* If we don't have ignoreeof set, always check for stopped jobs */
148359243Sobrien			if (chkstop == 0) {
148459243Sobrien				panystop(1);
148559243Sobrien			}
148659243Sobrien		}
148759243Sobrien		/* At this point, if there were stopped jobs, we would have already
148859243Sobrien		 * called reset().  If we got this far, assume we can print an
148959243Sobrien		 * exit/logout message if we ignoreeof, or just exit.
149059243Sobrien		 */
149159243Sobrien		if (adrof(STRignoreeof)) {
149259243Sobrien			/* If so, tell the user to use exit or logout */
149359243Sobrien		    if (loginsh) {
1494195609Smp				xprintf("%s", CGETS(16, 2,
149559243Sobrien					"\nUse \"logout\" to logout.\n"));
149659243Sobrien		   	} else {
149759243Sobrien				xprintf(CGETS(16, 3,
149859243Sobrien					"\nUse \"exit\" to leave %s.\n"),
149959243Sobrien					progname);
150059243Sobrien			}
1501167465Smp		    reset();
150259243Sobrien		} else {
150359243Sobrien			/* If we don't have ignoreeof set, just fall through */
150459243Sobrien			;	/* EMPTY */
150559243Sobrien		}
150659243Sobrien	    }
150759243Sobrien    oops:
150859243Sobrien	    doneinp = 1;
150959243Sobrien	    reset();
151059243Sobrien	}
151159243Sobrien	sincereal = 0;
151259243Sobrien	if (c == '\n' && onelflg)
151359243Sobrien	    onelflg--;
151459243Sobrien    } while (c == 0);
1515167465Smp    Strbuf_append1(&histline, c);
151659243Sobrien    return (c);
151759243Sobrien}
151859243Sobrien
151959243Sobrienstatic void
1520167465Smpballoc(int buf)
152159243Sobrien{
152259243Sobrien    Char **nfbuf;
152359243Sobrien
152459243Sobrien    while (buf >= fblocks) {
1525167465Smp	nfbuf = xcalloc(fblocks + 2, sizeof(Char **));
152659243Sobrien	if (fbuf) {
152759243Sobrien	    (void) blkcpy(nfbuf, fbuf);
1528167465Smp	    xfree(fbuf);
152959243Sobrien	}
153059243Sobrien	fbuf = nfbuf;
1531167465Smp	fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char));
153259243Sobrien	fblocks++;
153359243Sobrien    }
153459243Sobrien}
153559243Sobrien
1536145479Smpstatic ssize_t
1537167465Smpwide_read(int fildes, Char *buf, size_t nchars, int use_fclens)
1538145479Smp{
1539145479Smp    char cbuf[BUFSIZE + 1];
1540167465Smp    ssize_t res, r = 0;
1541145479Smp    size_t partial;
1542167465Smp    int err;
1543167465Smp
1544167465Smp    if (nchars == 0)
1545167465Smp	return 0;
1546167465Smp    assert (nchars <= sizeof(cbuf) / sizeof(*cbuf));
1547145479Smp    USE(use_fclens);
1548145479Smp    res = 0;
1549145479Smp    partial = 0;
1550145479Smp    do {
1551145479Smp	size_t i;
1552167465Smp	size_t len = nchars > partial ? nchars - partial : 1;
1553167465Smp
1554167465Smp	if (partial + len >= sizeof(cbuf) / sizeof(*cbuf))
1555167465Smp	    break;
1556145479Smp
1557167465Smp	r = xread(fildes, cbuf + partial, len);
1558167465Smp
1559145479Smp	if (partial == 0 && r <= 0)
1560145479Smp	    break;
1561145479Smp	partial += r;
1562145479Smp	i = 0;
1563167465Smp	while (i < partial && nchars != 0) {
1564167465Smp	    int tlen;
1565145479Smp
1566167465Smp	    tlen = normal_mbtowc(buf + res, cbuf + i, partial - i);
1567167465Smp	    if (tlen == -1) {
1568145479Smp	        reset_mbtowc();
1569167465Smp		if ((partial - i) < MB_LEN_MAX && r > 0)
1570145479Smp		    /* Maybe a partial character and there is still a chance
1571145479Smp		       to read more */
1572145479Smp		    break;
1573145479Smp		buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE;
1574145479Smp	    }
1575167465Smp	    if (tlen <= 0)
1576167465Smp		tlen = 1;
1577145479Smp#ifdef WIDE_STRINGS
1578145479Smp	    if (use_fclens)
1579167465Smp		fclens[res] = tlen;
1580145479Smp#endif
1581167465Smp	    i += tlen;
1582145479Smp	    res++;
1583145479Smp	    nchars--;
1584145479Smp	}
1585145479Smp	if (i != partial)
1586145479Smp	    memmove(cbuf, cbuf + i, partial - i);
1587145479Smp	partial -= i;
1588167465Smp    } while (partial != 0 && nchars > 0);
1589167465Smp    /* Throwing away possible partial multibyte characters on error if the
1590167465Smp       stream is not seekable */
1591167465Smp    err = errno;
1592167465Smp    lseek(fildes, -(off_t)partial, L_INCR);
1593167465Smp    errno = err;
1594145479Smp    return res != 0 ? res : r;
1595145479Smp}
1596145479Smp
1597145479Smpstatic eChar
1598167465Smpbgetc(void)
159959243Sobrien{
1600145479Smp    Char ch;
160159243Sobrien    int c, off, buf;
160259243Sobrien    int numleft = 0, roomleft;
160359243Sobrien
160459243Sobrien    if (cantell) {
160559243Sobrien	if (fseekp < fbobp || fseekp > feobp) {
160659243Sobrien	    fbobp = feobp = fseekp;
160759243Sobrien	    (void) lseek(SHIN, fseekp, L_SET);
160859243Sobrien	}
160959243Sobrien	if (fseekp == feobp) {
1610167465Smp#ifdef WIDE_STRINGS
1611167465Smp	    off_t bytes;
1612167465Smp	    size_t i;
1613167465Smp
1614167465Smp	    bytes = fbobp;
1615167465Smp	    for (i = 0; i < (size_t)(feobp - fbobp); i++)
1616167465Smp		bytes += fclens[i];
1617167465Smp	    fseekp = feobp = bytes;
1618167465Smp#endif
161959243Sobrien	    fbobp = feobp;
1620167465Smp	    c = wide_read(SHIN, fbuf[0], BUFSIZE, 1);
162159243Sobrien#ifdef convex
162259243Sobrien	    if (c < 0)
162359243Sobrien		stderror(ERR_SYSTEM, progname, strerror(errno));
162459243Sobrien#endif /* convex */
162559243Sobrien	    if (c <= 0)
1626145479Smp		return CHAR_ERR;
162759243Sobrien	    feobp += c;
162859243Sobrien	}
1629195609Smp#if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1630145479Smp	ch = fbuf[0][fseekp - fbobp];
163159243Sobrien	fseekp++;
163259243Sobrien#else
163359243Sobrien	do {
1634145479Smp	    ch = fbuf[0][fseekp - fbobp];
163559243Sobrien	    fseekp++;
1636145479Smp	} while(ch == '\r');
1637195609Smp#endif /* !WINNT_NATIVE && !__CYGWIN__ */
1638145479Smp	return (ch);
163959243Sobrien    }
164059243Sobrien
164159243Sobrien    while (fseekp >= feobp) {
1642100616Smp	if ((editing
1643100616Smp#if defined(FILEC) && defined(TIOCSTI)
1644100616Smp	    || filec
1645100616Smp#endif /* FILEC && TIOCSTI */
1646100616Smp	    ) && intty) {		/* then use twenex routine */
164759243Sobrien	    fseekp = feobp;		/* where else? */
1648100616Smp#if defined(FILEC) && defined(TIOCSTI)
1649100616Smp	    if (!editing)
1650100616Smp		c = numleft = tenex(InputBuf, BUFSIZE);
1651100616Smp	    else
1652100616Smp#endif /* FILEC && TIOCSTI */
165359243Sobrien	    c = numleft = Inputl();	/* PWP: get a line */
165459243Sobrien	    while (numleft > 0) {
165559243Sobrien		off = (int) feobp % BUFSIZE;
165659243Sobrien		buf = (int) feobp / BUFSIZE;
165759243Sobrien		balloc(buf);
165859243Sobrien		roomleft = BUFSIZE - off;
165959243Sobrien		if (roomleft > numleft)
166059243Sobrien		    roomleft = numleft;
1661167465Smp		(void) memcpy(fbuf[buf] + off, InputBuf + c - numleft,
1662167465Smp			      roomleft * sizeof(Char));
166359243Sobrien		numleft -= roomleft;
166459243Sobrien		feobp += roomleft;
166559243Sobrien	    }
1666100616Smp	} else {
166759243Sobrien	    off = (int) feobp % BUFSIZE;
166859243Sobrien	    buf = (int) feobp / BUFSIZE;
166959243Sobrien	    balloc(buf);
167059243Sobrien	    roomleft = BUFSIZE - off;
1671167465Smp	    c = wide_read(SHIN, fbuf[buf] + off, roomleft, 0);
1672145479Smp	    if (c > 0)
167359243Sobrien		feobp += c;
167459243Sobrien	}
167559243Sobrien	if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1))
1676145479Smp	    return CHAR_ERR;
167759243Sobrien    }
1678145479Smp#ifdef SIG_WINDOW
1679145479Smp    if (windowchg)
1680145479Smp	(void) check_window_size(0);	/* for window systems */
1681145479Smp#endif /* SIG_WINDOW */
1682195609Smp#if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1683145479Smp    ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
168459243Sobrien    fseekp++;
168559243Sobrien#else
168659243Sobrien    do {
1687145479Smp	ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
168859243Sobrien	fseekp++;
1689145479Smp    } while(ch == '\r');
1690195609Smp#endif /* !WINNT_NATIVE && !__CYGWIN__ */
1691145479Smp    return (ch);
169259243Sobrien}
169359243Sobrien
169459243Sobrienstatic void
1695167465Smpbfree(void)
169659243Sobrien{
169759243Sobrien    int sb, i;
169859243Sobrien
169959243Sobrien    if (cantell)
170059243Sobrien	return;
170159243Sobrien    if (whyles)
170259243Sobrien	return;
170359243Sobrien    sb = (int) (fseekp - 1) / BUFSIZE;
170459243Sobrien    if (sb > 0) {
170559243Sobrien	for (i = 0; i < sb; i++)
1706167465Smp	    xfree(fbuf[i]);
170759243Sobrien	(void) blkcpy(fbuf, &fbuf[sb]);
170859243Sobrien	fseekp -= BUFSIZE * sb;
170959243Sobrien	feobp -= BUFSIZE * sb;
171059243Sobrien	fblocks -= sb;
171159243Sobrien    }
171259243Sobrien}
171359243Sobrien
171459243Sobrienvoid
1715167465Smpbseek(struct Ain *l)
171659243Sobrien{
171759243Sobrien    switch (aret = l->type) {
171869408Sache    case TCSH_E_SEEK:
171959243Sobrien	evalvec = l->a_seek;
172059243Sobrien	evalp = l->c_seek;
172159243Sobrien#ifdef DEBUG_SEEK
172259243Sobrien	xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp);
172359243Sobrien#endif
172459243Sobrien	return;
172569408Sache    case TCSH_A_SEEK:
172659243Sobrien	alvec = l->a_seek;
172759243Sobrien	alvecp = l->c_seek;
172859243Sobrien#ifdef DEBUG_SEEK
172959243Sobrien	xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp);
173059243Sobrien#endif
173159243Sobrien	return;
173269408Sache    case TCSH_F_SEEK:
173359243Sobrien#ifdef DEBUG_SEEK
173459243Sobrien	xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp);
173559243Sobrien#endif
173659243Sobrien	fseekp = l->f_seek;
1737145479Smp#ifdef WIDE_STRINGS
1738145479Smp	if (cantell) {
1739167465Smp	    if (fseekp >= fbobp && feobp >= fbobp) {
1740145479Smp		size_t i;
1741145479Smp		off_t o;
1742145479Smp
1743145479Smp		o = fbobp;
1744167465Smp		for (i = 0; i < (size_t)(feobp - fbobp); i++) {
1745145479Smp		    if (fseekp == o) {
1746145479Smp			fseekp = fbobp + i;
1747145479Smp			return;
1748145479Smp		    }
1749145479Smp		    o += fclens[i];
1750145479Smp		}
1751145479Smp		if (fseekp == o) {
1752145479Smp		    fseekp = feobp;
1753145479Smp		    return;
1754145479Smp		}
1755145479Smp	    }
1756145479Smp	    fbobp = feobp = fseekp + 1; /* To force lseek() */
1757145479Smp	}
1758145479Smp#endif
175959243Sobrien	return;
176059243Sobrien    default:
176159243Sobrien	xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
176259243Sobrien	abort();
176359243Sobrien    }
176459243Sobrien}
176559243Sobrien
176659243Sobrien/* any similarity to bell telephone is purely accidental */
176759243Sobrienvoid
1768167465Smpbtell(struct Ain *l)
176959243Sobrien{
177059243Sobrien    switch (l->type = aret) {
177169408Sache    case TCSH_E_SEEK:
177259243Sobrien	l->a_seek = evalvec;
177359243Sobrien	l->c_seek = evalp;
177459243Sobrien#ifdef DEBUG_SEEK
177559243Sobrien	xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp);
177659243Sobrien#endif
177759243Sobrien	return;
177869408Sache    case TCSH_A_SEEK:
177959243Sobrien	l->a_seek = alvec;
178059243Sobrien	l->c_seek = alvecp;
178159243Sobrien#ifdef DEBUG_SEEK
178259243Sobrien	xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp);
178359243Sobrien#endif
178459243Sobrien	return;
178569408Sache    case TCSH_F_SEEK:
1786145479Smp#ifdef WIDE_STRINGS
1787167465Smp	if (cantell && fseekp >= fbobp && fseekp <= feobp) {
1788145479Smp	    size_t i;
1789167465Smp
1790145479Smp	    l->f_seek = fbobp;
1791167465Smp	    for (i = 0; i < (size_t)(fseekp - fbobp); i++)
1792145479Smp		l->f_seek += fclens[i];
1793145479Smp	} else
1794145479Smp#endif
1795145479Smp	    /*SUPPRESS 112*/
1796145479Smp	    l->f_seek = fseekp;
179759243Sobrien	l->a_seek = NULL;
179859243Sobrien#ifdef DEBUG_SEEK
179959243Sobrien	xprintf(CGETS(16, 10, "tell file %x\n"), fseekp);
180059243Sobrien#endif
180159243Sobrien	return;
180259243Sobrien    default:
180359243Sobrien	xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
180459243Sobrien	abort();
180559243Sobrien    }
180659243Sobrien}
180759243Sobrien
180859243Sobrienvoid
1809167465Smpbtoeof(void)
181059243Sobrien{
181159243Sobrien    (void) lseek(SHIN, (off_t) 0, L_XTND);
181269408Sache    aret = TCSH_F_SEEK;
181359243Sobrien    fseekp = feobp;
181459243Sobrien    alvec = NULL;
181559243Sobrien    alvecp = NULL;
181659243Sobrien    evalvec = NULL;
181759243Sobrien    evalp = NULL;
181859243Sobrien    wfree();
181959243Sobrien    bfree();
182059243Sobrien}
182159243Sobrien
182259243Sobrienvoid
1823167465Smpsettell(void)
182459243Sobrien{
182559243Sobrien    off_t x;
182659243Sobrien    cantell = 0;
182759243Sobrien    if (arginp || onelflg || intty)
182859243Sobrien	return;
182959243Sobrien    if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
183059243Sobrien	return;
1831167465Smp    fbuf = xcalloc(2, sizeof(Char **));
183259243Sobrien    fblocks = 1;
1833167465Smp    fbuf[0] = xcalloc(BUFSIZE, sizeof(Char));
183459243Sobrien    fseekp = fbobp = feobp = x;
183559243Sobrien    cantell = 1;
183659243Sobrien}
1837