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