Remove tcpslice(1).
[dragonfly.git] / contrib / tcsh-6 / sh.lex.c
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
35 RCSID("$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  */
54 static  Char            *word           (int);
55 static  eChar            getC1          (int);
56 static  void             getdol         (void);
57 static  void             getexcl        (Char);
58 static  struct Hist     *findev         (Char *, int);
59 static  void             setexclp       (Char *);
60 static  eChar            bgetc          (void);
61 static  void             balloc         (int);
62 static  void             bfree          (void);
63 static  struct wordent  *gethent        (Char);
64 static  int              matchs         (const Char *, const Char *);
65 static  int              getsel         (int *, int *, int);
66 static  struct wordent  *getsub         (struct wordent *);
67 static  Char            *subword        (Char *, Char, int *, size_t *);
68 static  struct wordent  *dosub          (Char, struct wordent *, int);
69 static  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  */
88 static Char peekc = 0, peekd = 0;
89 static Char peekread = 0;
90
91 /* (Tail of) current word from ! subst */
92 static Char *exclp = NULL;
93
94 /* The rest of the ! subst words */
95 static struct wordent *exclnxt = NULL;
96
97 /* Count of remaining words in ! subst */
98 static int exclc = 0;
99
100 /* "Globp" for alias resubstitution */
101 int 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  */
110 struct 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  */
119 static int hadhist = 0;
120
121 /*
122  * Avoid alias expansion recursion via \!#
123  */
124 int     hleft;
125
126 struct Strbuf histline; /* = Strbuf_INIT; last line input */
127
128 int    histvalid = 0;           /* is histline valid */
129
130 static 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  */
139 time_t Htime = (time_t)0;
140 static time_t a2time_t (Char *);
141
142 /*
143  * special parsing rules apply for source -h
144  */
145 extern int enterhist;
146
147 int
148 lex(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
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 freelex(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
274 void
275 lex_cleanup(void *xvp)
276 {
277     struct wordent *vp;
278
279     vp = xvp;
280     freelex(vp);
281 }
282
283 static Char *
284 word(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);
293 loop:
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     }
415 ret:
416     cleanup_ignore(&wbuf);
417     cleanup_until(&wbuf);
418     return Strbuf_finish(&wbuf);
419 }
420
421 static eChar
422 getC1(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
477 static void
478 getdol(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 */
646 void
647 addla(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? */
662 static struct Strbuf lhsb; /* = Strbuf_INIT; */
663 static struct Strbuf slhs; /* = Strbuf_INIT; left-hand side of last :s */
664 static struct Strbuf rhsb; /* = Strbuf_INIT; right-hand side of last :s */
665 static int quesarg;
666
667 static void
668 getexcl(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     }
725 subst:
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
744 static struct wordent *
745 getsub(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         Strbuf_append(&slhs, lhsb.s);
855         Strbuf_terminate(&slhs);
856         if (exclc)
857             en = dosub(sc, en, global);
858     }
859     while ((c = getC(0)) == ':');
860     unreadc(c);
861     return (en);
862 }
863
864 /*
865  * 
866  * From Beto Appleton (beto@aixwiz.austin.ibm.com)
867  *
868  * when using history substitution, and the variable
869  * 'history' is set to a value higher than 1000,
870  * the shell might either freeze (hang) or core-dump.
871  * We raise the limit to 50000000
872  */
873
874 #define HIST_PURGE -50000000
875 static struct wordent *
876 dosub(Char sc, struct wordent *en, int global)
877 {
878     struct wordent lexi;
879     int    didsub = 0, didone = 0;
880     struct wordent *hp = &lexi;
881     struct wordent *wdp;
882     int i = exclc;
883     struct Hist *hst;
884
885     wdp = hp;
886     while (--i >= 0) {
887         struct wordent *new = xcalloc(1, sizeof *wdp);
888
889         new->word = 0;
890         new->prev = wdp;
891         new->next = hp;
892         wdp->next = new;
893         wdp = new;
894         en = en->next;
895         if (en->word) {
896             Char *tword, *otword;
897
898             if ((global & FLAG_G) || didsub == 0) {
899                 size_t pos;
900
901                 pos = 0;
902                 tword = subword(en->word, sc, &didone, &pos);
903                 if (didone)
904                     didsub = 1;
905                 if (global & FLAG_A) {
906                     while (didone && tword != STRNULL) {
907                         otword = tword;
908                         tword = subword(otword, sc, &didone, &pos);
909                         if (Strcmp(tword, otword) == 0) {
910                             xfree(otword);
911                             break;
912                         }
913                         else
914                             xfree(otword);
915                     }
916                 }
917             }
918             else
919                 tword = Strsave(en->word);
920             wdp->word = tword;
921         }
922     }
923     if (didsub == 0)
924         seterror(ERR_MODFAIL);
925     hp->prev = wdp;
926     /* 
927      * ANSI mode HP/UX compiler chokes on
928      * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
929      */
930     hst = enthist(HIST_PURGE, &lexi, 0, 0);
931     return &(hst->Hlex);
932 }
933
934 /* Return a newly allocated result of one modification of CP using the
935    operation TYPE.  Set ADID to 1 if a modification was performed.
936    If TYPE == 's', perform substitutions only from *START_POS on and set
937    *START_POS to the position of next substitution attempt. */
938 static Char *
939 subword(Char *cp, Char type, int *adid, size_t *start_pos)
940 {
941     Char *wp;
942     const Char *mp, *np;
943
944     switch (type) {
945
946     case 'r':
947     case 'e':
948     case 'h':
949     case 't':
950     case 'q':
951     case 'x':
952     case 'u':
953     case 'l':
954         wp = domod(cp, type);
955         if (wp == 0) {
956             *adid = 0;
957             return (Strsave(cp));
958         }
959         *adid = 1;
960         return (wp);
961
962     default:
963         for (mp = cp + *start_pos; *mp; mp++) {
964             if (matchs(mp, lhsb.s)) {
965                 struct Strbuf wbuf = Strbuf_INIT;
966
967                 Strbuf_appendn(&wbuf, cp, mp - cp);
968                 for (np = rhsb.s; *np; np++)
969                     switch (*np) {
970
971                     case '\\':
972                         if (np[1] == '&')
973                             np++;
974                         /* fall into ... */
975
976                     default:
977                         Strbuf_append1(&wbuf, *np);
978                         continue;
979
980                     case '&':
981                         Strbuf_append(&wbuf, lhsb.s);
982                         continue;
983                     }
984                 *start_pos = wbuf.len;
985                 Strbuf_append(&wbuf, mp + lhsb.len);
986                 *adid = 1;
987                 return Strbuf_finish(&wbuf);
988             }
989         }
990         *adid = 0;
991         return (Strsave(cp));
992     }
993 }
994
995 Char   *
996 domod(Char *cp, Char type)
997 {
998     Char *wp, *xp;
999     int c;
1000
1001     switch (type) {
1002
1003     case 'x':
1004     case 'q':
1005         wp = Strsave(cp);
1006         for (xp = wp; (c = *xp) != 0; xp++)
1007             if ((c != ' ' && c != '\t') || type == 'q')
1008                 *xp |= QUOTE;
1009         return (wp);
1010
1011     case 'l':
1012         wp = NLSChangeCase(cp, 1);
1013         return wp ? wp : Strsave(cp);
1014
1015     case 'u':
1016         wp = NLSChangeCase(cp, 0);
1017         return wp ? wp : Strsave(cp);
1018
1019     case 'h':
1020     case 't':
1021         if (!any(short2str(cp), '/'))
1022             return (type == 't' ? Strsave(cp) : 0);
1023         wp = Strrchr(cp, '/');
1024         if (type == 'h')
1025             xp = Strnsave(cp, wp - cp);
1026         else
1027             xp = Strsave(wp + 1);
1028         return (xp);
1029
1030     case 'e':
1031     case 'r':
1032         wp = Strend(cp);
1033         for (wp--; wp >= cp && *wp != '/'; wp--)
1034             if (*wp == '.') {
1035                 if (type == 'e')
1036                     xp = Strsave(wp + 1);
1037                 else
1038                     xp = Strnsave(cp, wp - cp);
1039                 return (xp);
1040             }
1041         return (Strsave(type == 'e' ? STRNULL : cp));
1042     default:
1043         break;
1044     }
1045     return (0);
1046 }
1047
1048 static int
1049 matchs(const Char *str, const Char *pat)
1050 {
1051     while (*str && *pat && *str == *pat)
1052         str++, pat++;
1053     return (*pat == 0);
1054 }
1055
1056 static int
1057 getsel(int *al, int *ar, int dol)
1058 {
1059     eChar c = getC(0);
1060     int i;
1061     int    first = *al < 0;
1062
1063     switch (c) {
1064
1065     case '%':
1066         if (quesarg == -1) {
1067             seterror(ERR_BADBANGARG);
1068             return (0);
1069         }
1070         if (*al < 0)
1071             *al = quesarg;
1072         *ar = quesarg;
1073         break;
1074
1075     case '-':
1076         if (*al < 0) {
1077             *al = 0;
1078             *ar = dol - 1;
1079             unreadc(c);
1080         }
1081         return (1);
1082
1083     case '^':
1084         if (*al < 0)
1085             *al = 1;
1086         *ar = 1;
1087         break;
1088
1089     case '$':
1090         if (*al < 0)
1091             *al = dol;
1092         *ar = dol;
1093         break;
1094
1095     case '*':
1096         if (*al < 0)
1097             *al = 1;
1098         *ar = dol;
1099         if (*ar < *al) {
1100             *ar = 0;
1101             *al = 1;
1102             return (1);
1103         }
1104         break;
1105
1106     default:
1107         if (Isdigit(c)) {
1108             i = 0;
1109             while (Isdigit(c)) {
1110                 i = i * 10 + c - '0';
1111                 c = getC(0);
1112             }
1113             if (i < 0)
1114                 i = dol + 1;
1115             if (*al < 0)
1116                 *al = i;
1117             *ar = i;
1118         }
1119         else if (*al < 0)
1120             *al = 0, *ar = dol;
1121         else
1122             *ar = dol - 1;
1123         unreadc(c);
1124         break;
1125     }
1126     if (first) {
1127         c = getC(0);
1128         unreadc(c);
1129         if (any("-$*", c))
1130             return (1);
1131     }
1132     if (*al > *ar || *ar > dol) {
1133         seterror(ERR_BADBANGARG);
1134         return (0);
1135     }
1136     return (1);
1137
1138 }
1139
1140 static struct wordent *
1141 gethent(Char sc)
1142 {
1143     struct Hist *hp;
1144     Char *np;
1145     eChar c;
1146     int     event;
1147     int    back = 0;
1148
1149     c = sc == HISTSUB ? (eChar)HIST : getC(0);
1150     if (c == (eChar)HIST) {
1151         if (alhistp)
1152             return (alhistp);
1153         event = eventno;
1154     }
1155     else
1156         switch (c) {
1157
1158         case ':':
1159         case '^':
1160         case '$':
1161         case '*':
1162         case '%':
1163             ungetC(c);
1164             if (lastev == eventno && alhistp)
1165                 return (alhistp);
1166             event = lastev;
1167             break;
1168
1169         case '#':               /* !# is command being typed in (mrh) */
1170             if (--hleft == 0) {
1171                 seterror(ERR_HISTLOOP);
1172                 return (0);
1173             }
1174             else
1175                 return (&paraml);
1176             /* NOTREACHED */
1177
1178         case '-':
1179             back = 1;
1180             c = getC(0);
1181             /* FALLSTHROUGH */
1182
1183         default:
1184             if (any("(=~", c)) {
1185                 unreadc(c);
1186                 ungetC(HIST);
1187                 return (0);
1188             }
1189             Strbuf_terminate(&lhsb);
1190             lhsb.len = 0;
1191             event = 0;
1192             while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) {
1193                 if (event != -1 && Isdigit(c))
1194                     event = event * 10 + c - '0';
1195                 else
1196                     event = -1;
1197                 Strbuf_append1(&lhsb, c);
1198                 c = getC(0);
1199             }
1200             unreadc(c);
1201             if (lhsb.len == 0) {
1202                 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1203                 ungetC(HIST);
1204                 return (0);
1205             }
1206             Strbuf_terminate(&lhsb);
1207             if (event != -1) {
1208                 /*
1209                  * History had only digits
1210                  */
1211                 if (back)
1212                     event = eventno + (alhistp == 0) - event;
1213                 break;
1214             }
1215             if (back) {
1216                 Strbuf_append1(&lhsb, '\0'); /* Allocate space */
1217                 Strbuf_terminate(&lhsb);
1218                 memmove(lhsb.s + 1, lhsb.s, (lhsb.len - 1) * sizeof (*lhsb.s));
1219                 lhsb.s[0] = '-';
1220             }
1221             hp = findev(lhsb.s, 0);
1222             if (hp)
1223                 lastev = hp->Hnum;
1224             return (&hp->Hlex);
1225
1226         case '?':
1227             Strbuf_terminate(&lhsb);
1228             lhsb.len = 0;
1229             for (;;) {
1230                 c = getC(0);
1231                 if (c == '\n') {
1232                     unreadc(c);
1233                     break;
1234                 }
1235                 if (c == '?')
1236                     break;
1237                 Strbuf_append1(&lhsb, c);
1238             }
1239             if (lhsb.len == 0) {
1240                 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1241                 if (lhsb.len == 0) {
1242                     seterror(ERR_NOSEARCH);
1243                     return (0);
1244                 }
1245             }
1246             else
1247                 Strbuf_terminate(&lhsb);
1248             hp = findev(lhsb.s, 1);
1249             if (hp)
1250                 lastev = hp->Hnum;
1251             return (&hp->Hlex);
1252         }
1253
1254     for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1255         if (hp->Hnum == event) {
1256             hp->Href = eventno;
1257             lastev = hp->Hnum;
1258             return (&hp->Hlex);
1259         }
1260     np = putn(event);
1261     seterror(ERR_NOEVENT, short2str(np));
1262     xfree(np);
1263     return (0);
1264 }
1265
1266 static struct Hist *
1267 findev(Char *cp, int anyarg)
1268 {
1269     struct Hist *hp;
1270
1271     for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1272         Char   *dp;
1273         Char *p, *q;
1274         struct wordent *lp = hp->Hlex.next;
1275         int     argno = 0;
1276
1277         /*
1278          * The entries added by alias substitution don't have a newline but do
1279          * have a negative event number. Savehist() trims off these entries,
1280          * but it happens before alias expansion, too early to delete those
1281          * from the previous command.
1282          */
1283         if (hp->Hnum < 0)
1284             continue;
1285         if (lp->word[0] == '\n')
1286             continue;
1287         if (!anyarg) {
1288             p = cp;
1289             q = lp->word;
1290             do
1291                 if (!*p)
1292                     return (hp);
1293             while (*p++ == *q++);
1294             continue;
1295         }
1296         do {
1297             for (dp = lp->word; *dp; dp++) {
1298                 p = cp;
1299                 q = dp;
1300                 do
1301                     if (!*p) {
1302                         quesarg = argno;
1303                         return (hp);
1304                     }
1305                 while (*p++ == *q++);
1306             }
1307             lp = lp->next;
1308             argno++;
1309         } while (lp->word[0] != '\n');
1310     }
1311     seterror(ERR_NOEVENT, short2str(cp));
1312     return (0);
1313 }
1314
1315
1316 static void
1317 setexclp(Char *cp)
1318 {
1319     if (cp && cp[0] == '\n')
1320         return;
1321     exclp = cp;
1322 }
1323
1324 void
1325 unreadc(Char c)
1326 {
1327     peekread = (Char) c;
1328 }
1329
1330 eChar
1331 readc(int wanteof)
1332 {
1333     eChar c;
1334     static  int sincereal;      /* Number of real EOFs we've seen */
1335
1336 #ifdef DEBUG_INP
1337     xprintf("readc\n");
1338 #endif
1339     if ((c = peekread) != 0) {
1340         peekread = 0;
1341         return (c);
1342     }
1343
1344 top:
1345     aret = TCSH_F_SEEK;
1346     if (alvecp) {
1347         arun = 1;
1348 #ifdef DEBUG_INP
1349         xprintf("alvecp %c\n", *alvecp & 0xff);
1350 #endif
1351         aret = TCSH_A_SEEK;
1352         if ((c = *alvecp++) != 0)
1353             return (c);
1354         if (alvec && *alvec) {
1355                 alvecp = *alvec++;
1356                 return (' ');
1357         }
1358         else {
1359             alvecp = NULL;
1360             aret = TCSH_F_SEEK;
1361             return('\n');
1362         }
1363     }
1364     if (alvec) {
1365         arun = 1;
1366         if ((alvecp = *alvec) != 0) {
1367             alvec++;
1368             goto top;
1369         }
1370         /* Infinite source! */
1371         return ('\n');
1372     }
1373     arun = 0;
1374     if (evalp) {
1375         aret = TCSH_E_SEEK;
1376         if ((c = *evalp++) != 0)
1377             return (c);
1378         if (evalvec && *evalvec) {
1379             evalp = *evalvec++;
1380             return (' ');
1381         }
1382         aret = TCSH_F_SEEK;
1383         evalp = 0;
1384     }
1385     if (evalvec) {
1386         if (evalvec == INVPPTR) {
1387             doneinp = 1;
1388             reset();
1389         }
1390         if ((evalp = *evalvec) != 0) {
1391             evalvec++;
1392             goto top;
1393         }
1394         evalvec = INVPPTR;
1395         return ('\n');
1396     }
1397     do {
1398         if (arginp == INVPTR || onelflg == 1) {
1399             if (wanteof)
1400                 return CHAR_ERR;
1401             exitstat();
1402         }
1403         if (arginp) {
1404             if ((c = *arginp++) == 0) {
1405                 arginp = INVPTR;
1406                 return ('\n');
1407             }
1408             return (c);
1409         }
1410 #ifdef BSDJOBS
1411 reread:
1412 #endif /* BSDJOBS */
1413         c = bgetc();
1414         if (c == CHAR_ERR) {
1415 #ifndef WINNT_NATIVE
1416 # ifndef POSIX
1417 #  ifdef TERMIO
1418             struct termio tty;
1419 #  else /* SGTTYB */
1420             struct sgttyb tty;
1421 #  endif /* TERMIO */
1422 # else /* POSIX */
1423             struct termios tty;
1424 # endif /* POSIX */
1425 #endif /* !WINNT_NATIVE */
1426             if (wanteof)
1427                 return CHAR_ERR;
1428             /* was isatty but raw with ignoreeof yields problems */
1429 #ifndef WINNT_NATIVE
1430 # ifndef POSIX
1431 #  ifdef TERMIO
1432             if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
1433                 (tty.c_lflag & ICANON))
1434 #  else /* GSTTYB */
1435             if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
1436                 (tty.sg_flags & RAW) == 0)
1437 #  endif /* TERMIO */
1438 # else /* POSIX */
1439             if (tcgetattr(SHIN, &tty) == 0 &&
1440                 (tty.c_lflag & ICANON))
1441 # endif /* POSIX */
1442 #else /* WINNT_NATIVE */
1443             if (isatty(SHIN))
1444 #endif /* !WINNT_NATIVE */
1445             {
1446 #ifdef BSDJOBS
1447                 pid_t ctpgrp;
1448 #endif /* BSDJOBS */
1449
1450                 if (numeof != 0 && ++sincereal >= numeof)       /* Too many EOFs?  Bye! */
1451                     goto oops;
1452 #ifdef BSDJOBS
1453                 if (tpgrp != -1 &&
1454                     (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1455                     tpgrp != ctpgrp) {
1456                     (void) tcsetpgrp(FSHTTY, tpgrp);
1457 # ifdef _SEQUENT_
1458                     if (ctpgrp)
1459 # endif /* _SEQUENT */
1460                     (void) killpg(ctpgrp, SIGHUP);
1461 # ifdef notdef
1462                     /*
1463                      * With the walking process group fix, this message
1464                      * is now obsolete. As the foreground process group
1465                      * changes, the shell needs to adjust. Well too bad.
1466                      */
1467                     xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1468                             (int)ctpgrp, (int)tpgrp);
1469 # endif /* notdef */
1470                     goto reread;
1471                 }
1472 #endif /* BSDJOBS */
1473                 /* What follows is complicated EOF handling -- sterling@netcom.com */
1474                 /* First, we check to see if we have ignoreeof set */
1475                 if (adrof(STRignoreeof)) {
1476                         /* If so, we check for any stopped jobs only on the first EOF */
1477                         if ((sincereal == 1) && (chkstop == 0)) {
1478                                 panystop(1);
1479                         }
1480                 } else {
1481                         /* If we don't have ignoreeof set, always check for stopped jobs */
1482                         if (chkstop == 0) {
1483                                 panystop(1);
1484                         }
1485                 }
1486                 /* At this point, if there were stopped jobs, we would have already
1487                  * called reset().  If we got this far, assume we can print an
1488                  * exit/logout message if we ignoreeof, or just exit.
1489                  */
1490                 if (adrof(STRignoreeof)) {
1491                         /* If so, tell the user to use exit or logout */
1492                     if (loginsh) {
1493                                 xprintf(CGETS(16, 2,
1494                                         "\nUse \"logout\" to logout.\n"));
1495                         } else {
1496                                 xprintf(CGETS(16, 3,
1497                                         "\nUse \"exit\" to leave %s.\n"),
1498                                         progname);
1499                         }
1500                     reset();
1501                 } else {
1502                         /* If we don't have ignoreeof set, just fall through */
1503                         ;       /* EMPTY */
1504                 }
1505             }
1506     oops:
1507             doneinp = 1;
1508             reset();
1509         }
1510         sincereal = 0;
1511         if (c == '\n' && onelflg)
1512             onelflg--;
1513     } while (c == 0);
1514     Strbuf_append1(&histline, c);
1515     return (c);
1516 }
1517
1518 static void
1519 balloc(int buf)
1520 {
1521     Char **nfbuf;
1522
1523     while (buf >= fblocks) {
1524         nfbuf = xcalloc(fblocks + 2, sizeof(Char **));
1525         if (fbuf) {
1526             (void) blkcpy(nfbuf, fbuf);
1527             xfree(fbuf);
1528         }
1529         fbuf = nfbuf;
1530         fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char));
1531         fblocks++;
1532     }
1533 }
1534
1535 static ssize_t
1536 wide_read(int fildes, Char *buf, size_t nchars, int use_fclens)
1537 {
1538     char cbuf[BUFSIZE + 1];
1539     ssize_t res, r = 0;
1540     size_t partial;
1541     int err;
1542
1543     if (nchars == 0)
1544         return 0;
1545     assert (nchars <= sizeof(cbuf) / sizeof(*cbuf));
1546     USE(use_fclens);
1547     res = 0;
1548     partial = 0;
1549     do {
1550         size_t i;
1551         size_t len = nchars > partial ? nchars - partial : 1;
1552
1553         if (partial + len >= sizeof(cbuf) / sizeof(*cbuf))
1554             break;
1555         
1556         r = xread(fildes, cbuf + partial, len);
1557                   
1558         if (partial == 0 && r <= 0)
1559             break;
1560         partial += r;
1561         i = 0;
1562         while (i < partial && nchars != 0) {
1563             int tlen;
1564
1565             tlen = normal_mbtowc(buf + res, cbuf + i, partial - i);
1566             if (tlen == -1) {
1567                 reset_mbtowc();
1568                 if ((partial - i) < MB_LEN_MAX && r > 0)
1569                     /* Maybe a partial character and there is still a chance
1570                        to read more */
1571                     break;
1572                 buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE;
1573             }
1574             if (tlen <= 0)
1575                 tlen = 1;
1576 #ifdef WIDE_STRINGS
1577             if (use_fclens)
1578                 fclens[res] = tlen;
1579 #endif
1580             i += tlen;
1581             res++;
1582             nchars--;
1583         }
1584         if (i != partial)
1585             memmove(cbuf, cbuf + i, partial - i);
1586         partial -= i;
1587     } while (partial != 0 && nchars > 0);
1588     /* Throwing away possible partial multibyte characters on error if the
1589        stream is not seekable */
1590     err = errno;
1591     lseek(fildes, -(off_t)partial, L_INCR);
1592     errno = err;
1593     return res != 0 ? res : r;
1594 }
1595
1596 static eChar
1597 bgetc(void)
1598 {
1599     Char ch;
1600     int c, off, buf;
1601     int numleft = 0, roomleft;
1602
1603     if (cantell) {
1604         if (fseekp < fbobp || fseekp > feobp) {
1605             fbobp = feobp = fseekp;
1606             (void) lseek(SHIN, fseekp, L_SET);
1607         }
1608         if (fseekp == feobp) {
1609 #ifdef WIDE_STRINGS
1610             off_t bytes;
1611             size_t i;
1612
1613             bytes = fbobp;
1614             for (i = 0; i < (size_t)(feobp - fbobp); i++)
1615                 bytes += fclens[i];
1616             fseekp = feobp = bytes;
1617 #endif
1618             fbobp = feobp;
1619             c = wide_read(SHIN, fbuf[0], BUFSIZE, 1);
1620 #ifdef convex
1621             if (c < 0)
1622                 stderror(ERR_SYSTEM, progname, strerror(errno));
1623 #endif /* convex */
1624             if (c <= 0)
1625                 return CHAR_ERR;
1626             feobp += c;
1627         }
1628 #ifndef WINNT_NATIVE
1629         ch = fbuf[0][fseekp - fbobp];
1630         fseekp++;
1631 #else
1632         do {
1633             ch = fbuf[0][fseekp - fbobp];
1634             fseekp++;
1635         } while(ch == '\r');
1636 #endif /* !WINNT_NATIVE */
1637         return (ch);
1638     }
1639
1640     while (fseekp >= feobp) {
1641         if ((editing
1642 #if defined(FILEC) && defined(TIOCSTI)
1643             || filec
1644 #endif /* FILEC && TIOCSTI */
1645             ) && intty) {               /* then use twenex routine */
1646             fseekp = feobp;             /* where else? */
1647 #if defined(FILEC) && defined(TIOCSTI)
1648             if (!editing)
1649                 c = numleft = tenex(InputBuf, BUFSIZE);
1650             else
1651 #endif /* FILEC && TIOCSTI */
1652             c = numleft = Inputl();     /* PWP: get a line */
1653             while (numleft > 0) {
1654                 off = (int) feobp % BUFSIZE;
1655                 buf = (int) feobp / BUFSIZE;
1656                 balloc(buf);
1657                 roomleft = BUFSIZE - off;
1658                 if (roomleft > numleft)
1659                     roomleft = numleft;
1660                 (void) memcpy(fbuf[buf] + off, InputBuf + c - numleft,
1661                               roomleft * sizeof(Char));
1662                 numleft -= roomleft;
1663                 feobp += roomleft;
1664             }
1665         } else {
1666             off = (int) feobp % BUFSIZE;
1667             buf = (int) feobp / BUFSIZE;
1668             balloc(buf);
1669             roomleft = BUFSIZE - off;
1670             c = wide_read(SHIN, fbuf[buf] + off, roomleft, 0);
1671             if (c > 0)
1672                 feobp += c;
1673         }
1674         if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1))
1675             return CHAR_ERR;
1676     }
1677 #ifdef SIG_WINDOW
1678     if (windowchg)
1679         (void) check_window_size(0);    /* for window systems */
1680 #endif /* SIG_WINDOW */
1681 #ifndef WINNT_NATIVE
1682     ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1683     fseekp++;
1684 #else
1685     do {
1686         ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1687         fseekp++;
1688     } while(ch == '\r');
1689 #endif /* !WINNT_NATIVE */
1690     return (ch);
1691 }
1692
1693 static void
1694 bfree(void)
1695 {
1696     int sb, i;
1697
1698     if (cantell)
1699         return;
1700     if (whyles)
1701         return;
1702     sb = (int) (fseekp - 1) / BUFSIZE;
1703     if (sb > 0) {
1704         for (i = 0; i < sb; i++)
1705             xfree(fbuf[i]);
1706         (void) blkcpy(fbuf, &fbuf[sb]);
1707         fseekp -= BUFSIZE * sb;
1708         feobp -= BUFSIZE * sb;
1709         fblocks -= sb;
1710     }
1711 }
1712
1713 void
1714 bseek(struct Ain *l)
1715 {
1716     switch (aret = l->type) {
1717     case TCSH_E_SEEK:
1718         evalvec = l->a_seek;
1719         evalp = l->c_seek;
1720 #ifdef DEBUG_SEEK
1721         xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp);
1722 #endif
1723         return;
1724     case TCSH_A_SEEK:
1725         alvec = l->a_seek;
1726         alvecp = l->c_seek;
1727 #ifdef DEBUG_SEEK
1728         xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp);
1729 #endif
1730         return;
1731     case TCSH_F_SEEK:   
1732 #ifdef DEBUG_SEEK
1733         xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp);
1734 #endif
1735         fseekp = l->f_seek;
1736 #ifdef WIDE_STRINGS
1737         if (cantell) {
1738             if (fseekp >= fbobp && feobp >= fbobp) {
1739                 size_t i;
1740                 off_t o;
1741
1742                 o = fbobp;
1743                 for (i = 0; i < (size_t)(feobp - fbobp); i++) {
1744                     if (fseekp == o) {
1745                         fseekp = fbobp + i;
1746                         return;
1747                     }
1748                     o += fclens[i];
1749                 }
1750                 if (fseekp == o) {
1751                     fseekp = feobp;
1752                     return;
1753                 }
1754             }
1755             fbobp = feobp = fseekp + 1; /* To force lseek() */
1756         }
1757 #endif
1758         return;
1759     default:
1760         xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1761         abort();
1762     }
1763 }
1764
1765 /* any similarity to bell telephone is purely accidental */
1766 void
1767 btell(struct Ain *l)
1768 {
1769     switch (l->type = aret) {
1770     case TCSH_E_SEEK:
1771         l->a_seek = evalvec;
1772         l->c_seek = evalp;
1773 #ifdef DEBUG_SEEK
1774         xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp);
1775 #endif
1776         return;
1777     case TCSH_A_SEEK:
1778         l->a_seek = alvec;
1779         l->c_seek = alvecp;
1780 #ifdef DEBUG_SEEK
1781         xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp);
1782 #endif
1783         return;
1784     case TCSH_F_SEEK:
1785 #ifdef WIDE_STRINGS
1786         if (cantell && fseekp >= fbobp && fseekp <= feobp) {
1787             size_t i;
1788
1789             l->f_seek = fbobp;
1790             for (i = 0; i < (size_t)(fseekp - fbobp); i++)
1791                 l->f_seek += fclens[i];
1792         } else
1793 #endif
1794             /*SUPPRESS 112*/
1795             l->f_seek = fseekp;
1796         l->a_seek = NULL;
1797 #ifdef DEBUG_SEEK
1798         xprintf(CGETS(16, 10, "tell file %x\n"), fseekp);
1799 #endif
1800         return;
1801     default:
1802         xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1803         abort();
1804     }
1805 }
1806
1807 void
1808 btoeof(void)
1809 {
1810     (void) lseek(SHIN, (off_t) 0, L_XTND);
1811     aret = TCSH_F_SEEK;
1812     fseekp = feobp;
1813     alvec = NULL;
1814     alvecp = NULL;
1815     evalvec = NULL;
1816     evalp = NULL;
1817     wfree();
1818     bfree();
1819 }
1820
1821 void
1822 settell(void)
1823 {
1824     off_t x;
1825     cantell = 0;
1826     if (arginp || onelflg || intty)
1827         return;
1828     if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
1829         return;
1830     fbuf = xcalloc(2, sizeof(Char **));
1831     fblocks = 1;
1832     fbuf[0] = xcalloc(BUFSIZE, sizeof(Char));
1833     fseekp = fbobp = feobp = x;
1834     cantell = 1;
1835 }