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