1 /* $Header: /src/pub/tcsh/sh.lex.c,v 3.62 2004/12/25 21:15:07 christos Exp $ */
3 * sh.lex.c: Lexical analysis into tokens
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
35 RCSID("$Id: sh.lex.c,v 3.62 2004/12/25 21:15:07 christos Exp $")
40 /* #define DEBUG_INP */
41 /* #define DEBUG_SEEK */
48 * These lexical routines read input and form lists of words.
49 * There is some involved processing here, because of the complications
50 * of input buffering, and especially because of history substitution.
52 static Char *word __P((int));
53 static eChar getC1 __P((int));
54 static void getdol __P((void));
55 static void getexcl __P((Char));
56 static struct Hist *findev __P((Char *, int));
57 static void setexclp __P((Char *));
58 static eChar bgetc __P((void));
59 static void balloc __P((int));
60 static void bfree __P((void));
61 static struct wordent *gethent __P((Char));
62 static int matchs __P((Char *, Char *));
63 static int getsel __P((int *, int *, int));
64 static struct wordent *getsub __P((struct wordent *));
65 static Char *subword __P((Char *, Char, int *));
66 static struct wordent *dosub __P((Char, struct wordent *, int));
67 static ssize_t wide_read __P((int, Char *, size_t, int));
70 * Peekc is a peek character for getC, peekread for readc.
71 * There is a subtlety here in many places... history routines
72 * will read ahead and then insert stuff into the input stream.
73 * If they push back a character then they must push it behind
74 * the text substituted by the history substitution. On the other
75 * hand in several places we need 2 peek characters. To make this
76 * all work, the history routines read with getC, and make use both
77 * of ungetC and unreadc. The key observation is that the state
78 * of getC at the call of a history reference is such that calls
79 * to getC from the history routines will always yield calls of
80 * readc, unless this peeking is involved. That is to say that during
81 * getexcl the variables lap, exclp, and exclnxt are all zero.
83 * Getdol invokes history substitution, hence the extra peek, peekd,
84 * which it can ungetD to be before history substitutions.
86 static Char peekc = 0, peekd = 0;
87 static Char peekread = 0;
89 /* (Tail of) current word from ! subst */
90 static Char *exclp = NULL;
92 /* The rest of the ! subst words */
93 static struct wordent *exclnxt = NULL;
95 /* Count of remaining words in ! subst */
98 /* "Globp" for alias resubstitution */
99 int aret = TCSH_F_SEEK;
102 * Labuf implements a general buffer for lookahead during lexical operations.
103 * Text which is to be placed in the input stream can be stuck here.
104 * We stick parsed ahead $ constructs during initial input,
105 * process id's from `$$', and modified variable values (from qualifiers
106 * during expansion in sh.dol.c) here.
108 static Char labuf[BUFSIZE];
111 * Lex returns to its caller not only a wordlist (as a "var" parameter)
112 * but also whether a history substitution occurred. This is used in
113 * the main (process) routine to determine whether to echo, and also
114 * when called by the alias routine to determine whether to keep the
117 static int hadhist = 0;
120 * Avoid alias expansion recursion via \!#
124 Char histline[BUFSIZE + 2]; /* last line input */
126 /* The +2 is to fool hp's optimizer */
127 int histvalid = 0; /* is histline valid */
128 static Char *histlinep = NULL; /* current pointer into histline */
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
136 /* Use Htime to store timestamps picked up from history file for enthist()
137 * if reading saved history (sg)
139 time_t Htime = (time_t)0;
140 static time_t a2time_t __P((Char *));
143 * special parsing rules apply for source -h
145 extern int enterhist;
148 * for history event processing
149 * in the command 'echo !?foo?:1 !$' we want the !$ to expand from the line
150 * 'foo' was found instead of the last command
152 static int uselastevent = 1;
160 int parsehtime = enterhist;
165 histlinep = histline;
169 hp->next = hp->prev = hp;
174 while (c == ' ' || c == '\t');
175 if (c == (eChar)HISTSUB && intty)
176 /* ^lef^rit from tty is short !:s^lef^rit */
182 * The following loop is written so that the links needed by freelex will
183 * be ready and rarin to go even if it is interrupted.
188 new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
195 wdp->word = word(parsehtime);
197 } while (wdp->word[0] != '\n');
198 if (histlinep < histline + BUFSIZE) {
200 if (histlinep > histline && histlinep[-1] == '\n')
201 histlinep[-1] = '\0';
205 histline[BUFSIZE - 1] = '\0';
215 /* Attempt to distinguish timestamps from other possible entries.
216 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
222 if (!wordx || *(s = wordx) != '+')
225 for (++s, ret = 0, ct = 0; *s; ++s, ++ct)
227 if (!isdigit((unsigned char)*s))
229 ret = ret * 10 + (time_t)((unsigned char)*s - '0');
242 struct wordent *sp = sp0->next;
245 xprintf("%S", sp->word);
249 if (sp->word[0] != '\n')
266 new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
273 wdp->word = Strsave(fp->word);
275 } while (wdp->word[0] != '\n');
284 while (vp->next != vp) {
287 if (fp->word != STRNULL)
288 xfree((ptr_t) fp->word);
299 Char *wp, *unfinished = 0;
309 while ((c = getC(DOALL)) == ' ' || c == '\t')
311 if (cmap(c, _META | _ESC))
326 if (intty || (enterhist && !parsehtime))
333 if (h < 11 && parsehtime)
338 Htime = a2time_t(hbuf);
358 if (c != (eChar)HIST)
372 else if (c == '\\') {
375 * PWP: this is dumb, but how all of the other shells work. If \ quotes
376 * a character OUTSIDE of a set of ''s, why shouldn't it quote EVERY
377 * following character INSIDE a set of ''s.
379 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
381 if (c == (eChar)HIST)
385 ((c == '\'') || (c == '"') ||
392 * if (c1 == '`') c = ' '; else
400 else if (c == '\n') {
401 seterror(ERR_UNMATCHED, c1);
406 else if (cmap(c, _META | _QF | _QB | _ESC)) {
414 if (c != (eChar)HIST)
418 else if (cmap(c, _QF | _QB)) { /* '"` */
420 dolflg = c == '"' ? DOALL : DOEXCL;
422 else if (c != '#' || (!intty && !enterhist)) {
432 switch (NLSFinished(unfinished, wp - unfinished, c)) {
443 seterror(ERR_WTOOLONG);
450 return (Strsave(wbuf));
460 if ((c = peekc) != 0) {
465 if ((c = *lap++) == 0)
468 if (cmap(c, _META | _QF | _QB))
473 if ((c = peekd) != 0) {
478 if ((c = *exclp++) != 0)
480 if (exclnxt && --exclc >= 0) {
481 exclnxt = exclnxt->next;
482 setexclp(exclnxt->word);
487 /* this will throw away the dummy history entries */
492 exclnxt = exclnxt->next;
496 setexclp(exclnxt->word);
500 if (c == '$' && (flag & DODOL)) {
504 if (c == (eChar)HIST && (flag & DOEXCL)) {
517 Char name[4 * MAXVARLEN + 1];
520 int special = 0, toolong;
522 np = name, *np++ = '$';
523 c = sc = getC(DOEXCL);
524 if (any("\t \n", c)) {
530 *np++ = (Char) c, c = getC(DOEXCL);
531 if (c == '#' || c == '?' || c == '%')
532 special++, *np++ = (Char) c, c = getC(DOEXCL);
540 seterror(ERR_SPDOLLT);
549 seterror(ERR_NEWLINE);
556 seterror(ERR_SPSTAR);
565 /* let $?0 pass for now */
573 /* we know that np < &name[4] */
575 while ((c = getC(DOEXCL)) != 0) {
584 else if (letter(c)) {
585 /* we know that np < &name[4] */
588 while ((c = getC(DOEXCL)) != 0) {
589 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
590 if (!letter(c) && !Isdigit(c))
600 seterror(ERR_VARILL);
610 seterror(ERR_VARTOOLONG);
620 * Name up to here is a max of MAXVARLEN + 8.
622 ep = &np[2 * MAXVARLEN + 8];
625 * Michael Greim: Allow $ expansion to take place in selector
626 * expressions. (limits the number of characters returned)
628 c = getC(DOEXCL | DODOL);
632 seterror(ERR_NLINDEX);
642 seterror(ERR_SELOVFL);
649 * Name up to here is a max of 2 * MAXVARLEN + 8.
653 * if the :g modifier is followed by a newline, then error right away!
657 int gmodflag = 0, amodflag = 0;
662 *np++ = (Char) c, c = getC(DOEXCL);
663 if (c == 'g' || c == 'a') {
668 *np++ = (Char) c; c = getC(DOEXCL);
670 if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
675 *np++ = (Char) c; c = getC(DOEXCL);
678 /* scan s// [eichin:19910926.0512EST] */
681 eChar delim = getC(0);
682 *np++ = (Char) delim;
684 if (!delim || letter(delim)
685 || Isdigit(delim) || any(" \t\n", delim)) {
686 seterror(ERR_BADSUBST);
689 while ((c = getC(0)) != CHAR_ERR) {
691 if(c == delim) delimcnt--;
695 seterror(ERR_BADSUBST);
700 if (!any("htrqxesul", c)) {
701 if ((amodflag || gmodflag) && c == '\n')
702 stderror(ERR_VARSYN); /* strike */
703 seterror(ERR_BADMOD, c);
710 while ((c = getC(DOEXCL)) == ':');
720 seterror(ERR_MISSING, '}');
738 if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
739 (sizeof(labuf) - 4) / sizeof(Char)) {
740 seterror(ERR_EXPOVFL);
744 (void) Strcpy(buf, lap);
745 (void) Strcpy(labuf, cp);
748 (void) Strcat(labuf, buf);
752 static Char lhsb[32];
753 static Char slhs[32];
754 static Char rhsb[64];
761 struct wordent *hp, *ip;
762 int left, right, dol;
786 for (ip = hp->next->next; ip != alhistt; ip = ip->next)
789 for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
791 left = 0, right = dol;
793 ungetC('s'), unreadc(HISTSUB), c = ':';
797 if (!any(":^$*-%", c))
803 if (letter(c) || c == '&') {
805 left = 0, right = dol;
811 if (!getsel(&left, &right, dol))
817 if (!getsel(&left, &right, dol))
822 exclc = right - left + 1;
825 if (sc == HISTSUB || c == ':') {
835 seterror(ERR_BADBANG);
840 static struct wordent *
849 Char orhsb[sizeof(rhsb) / sizeof(Char)];
857 if (c == 'g' || c == 'a') {
858 global |= (c == 'g') ? 1 : 2;
861 if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
862 global |= (c == 'g') ? 1 : 2;
886 seterror(ERR_NOSUBST);
889 (void) Strcpy(lhsb, slhs);
901 if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
904 seterror(ERR_BADSUBST);
916 if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
918 seterror(ERR_BADSUBST);
923 if (c != delim && c != '\\')
930 else if (lhsb[0] == 0) {
935 (void) Strcpy(orhsb, cp);
946 if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
949 (void) Strcpy(cp, orhsb);
954 if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
955 seterror(ERR_RHSLONG);
960 if (c != delim /* && c != '~' */ )
971 seterror(ERR_BADBANGMOD, (int)c);
974 (void) Strcpy(slhs, lhsb);
976 en = dosub(sc, en, global);
979 while ((c = getC(0)) == ':');
987 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
989 * when using history substitution, and the variable
990 * 'history' is set to a value higher than 1000,
991 * the shell might either freeze (hang) or core-dump.
992 * We raise the limit to 50000000
995 #define HIST_PURGE -50000000
996 static struct wordent *
997 dosub(sc, en, global)
1002 struct wordent lexi;
1003 int didsub = 0, didone = 0;
1004 struct wordent *hp = &lexi;
1005 struct wordent *wdp;
1011 struct wordent *new =
1012 (struct wordent *) xcalloc(1, sizeof *wdp);
1021 Char *tword, *otword;
1023 if ((global & 1) || didsub == 0) {
1024 tword = subword(en->word, sc, &didone);
1028 while (didone && tword != STRNULL) {
1030 tword = subword(otword, sc, &didone);
1031 if (Strcmp(tword, otword) == 0) {
1032 xfree((ptr_t) otword);
1036 xfree((ptr_t) otword);
1041 tword = Strsave(en->word);
1046 seterror(ERR_MODFAIL);
1049 * ANSI mode HP/UX compiler chokes on
1050 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
1052 hst = enthist(HIST_PURGE, &lexi, 0, 0);
1053 return &(hst->Hlex);
1057 subword(cp, type, adid)
1077 wp = domod(cp, type);
1079 return (Strsave(cp));
1086 for (mp = cp; *mp; mp++)
1087 if (matchs(mp, lhsb)) {
1088 for (np = cp; np < mp;)
1090 for (np = rhsb; *np; np++)
1100 seterror(ERR_SUBOVFL);
1109 seterror(ERR_SUBOVFL);
1113 (void) Strcat(wp, lhsb);
1120 seterror(ERR_SUBOVFL);
1124 (void) Strcat(wp, mp);
1126 return (Strsave(wbuf));
1128 return (Strsave(cp));
1145 for (xp = wp; (c = *xp) != 0; xp++)
1146 if ((c != ' ' && c != '\t') || type == 'q')
1151 wp = NLSChangeCase(cp, 1);
1152 return wp ? wp : Strsave(cp);
1155 wp = NLSChangeCase(cp, 0);
1156 return wp ? wp : Strsave(cp);
1160 if (!any(short2str(cp), '/'))
1161 return (type == 't' ? Strsave(cp) : 0);
1163 while (*--wp != '/')
1166 xp = Strsave(cp), xp[wp - cp] = 0;
1168 xp = Strsave(wp + 1);
1174 for (wp--; wp >= cp && *wp != '/'; wp--)
1177 xp = Strsave(wp + 1);
1179 xp = Strsave(cp), xp[wp - cp] = 0;
1182 return (Strsave(type == 'e' ? STRNULL : cp));
1193 while (*str && *pat && *str == *pat)
1205 int first = *al < 0;
1210 if (quesarg == -1) {
1211 seterror(ERR_BADBANGARG);
1253 while (Isdigit(c)) {
1254 i = i * 10 + c - '0';
1276 if (*al > *ar || *ar > dol) {
1277 seterror(ERR_BADBANGARG);
1284 static struct wordent *
1294 c = sc == HISTSUB ? (eChar)HIST : getC(0);
1295 if (c == (eChar)HIST) {
1309 if (lastev == eventno && alhistp)
1314 case '#': /* !# is command being typed in (mrh) */
1316 seterror(ERR_HISTLOOP);
1329 if (any("(=~", c)) {
1336 while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) {
1337 if (event != -1 && Isdigit(c))
1338 event = event * 10 + c - '0';
1341 if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1353 * History had only digits
1356 event = eventno + (alhistp == 0) - (event ? event : 0);
1360 event = sizeof(lhsb) / sizeof(lhsb[0]);
1361 np = &lhsb[--event];
1363 for (event--; np > lhsb; *np-- = lhsb[--event])
1367 hp = findev(lhsb, 0);
1382 if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1387 seterror(ERR_NOSEARCH);
1393 hp = findev(lhsb, 1);
1399 for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1400 if (hp->Hnum == event) {
1406 seterror(ERR_NOEVENT, short2str(np));
1410 static struct Hist *
1417 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1420 struct wordent *lp = hp->Hlex.next;
1424 * The entries added by alias substitution don't have a newline but do
1425 * have a negative event number. Savehist() trims off these entries,
1426 * but it happens before alias expansion, too early to delete those
1427 * from the previous command.
1431 if (lp->word[0] == '\n')
1439 while (*p++ == *q++);
1443 for (dp = lp->word; *dp; dp++) {
1451 while (*p++ == *q++);
1455 } while (lp->word[0] != '\n');
1457 seterror(ERR_NOEVENT, short2str(cp));
1466 if (cp && cp[0] == '\n')
1475 peekread = (Char) c;
1483 static int sincereal; /* Number of real EOFs we've seen */
1488 if ((c = peekread) != 0) {
1498 xprintf("alvecp %c\n", *alvecp & 0xff);
1501 if ((c = *alvecp++) != 0)
1503 if (alvec && *alvec) {
1515 if ((alvecp = *alvec) != 0) {
1519 /* Infinite source! */
1525 if ((c = *evalp++) != 0)
1527 if (evalvec && *evalvec) {
1535 if (evalvec == INVPPTR) {
1539 if ((evalp = *evalvec) != 0) {
1547 if (arginp == INVPTR || onelflg == 1) {
1553 if ((c = *arginp++) == 0) {
1561 #endif /* BSDJOBS */
1563 if (c == CHAR_ERR) {
1564 #ifndef WINNT_NATIVE
1570 # endif /* TERMIO */
1574 #endif /* !WINNT_NATIVE */
1577 /* was isatty but raw with ignoreeof yields problems */
1578 #ifndef WINNT_NATIVE
1581 if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
1582 (tty.c_lflag & ICANON))
1584 if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
1585 (tty.sg_flags & RAW) == 0)
1586 # endif /* TERMIO */
1588 if (tcgetattr(SHIN, &tty) == 0 &&
1589 (tty.c_lflag & ICANON))
1591 #else /* WINNT_NATIVE */
1593 #endif /* !WINNT_NATIVE */
1597 #endif /* BSDJOBS */
1599 if (numeof != 0 && ++sincereal >= numeof) /* Too many EOFs? Bye! */
1603 (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1605 (void) tcsetpgrp(FSHTTY, tpgrp);
1608 # endif /* _SEQUENT */
1609 (void) killpg((pid_t) ctpgrp, SIGHUP);
1612 * With the walking process group fix, this message
1613 * is now obsolete. As the foreground process group
1614 * changes, the shell needs to adjust. Well too bad.
1616 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1618 # endif /* notdef */
1621 #endif /* BSDJOBS */
1622 /* What follows is complicated EOF handling -- sterling@netcom.com */
1623 /* First, we check to see if we have ignoreeof set */
1624 if (adrof(STRignoreeof)) {
1625 /* If so, we check for any stopped jobs only on the first EOF */
1626 if ((sincereal == 1) && (chkstop == 0)) {
1630 /* If we don't have ignoreeof set, always check for stopped jobs */
1635 /* At this point, if there were stopped jobs, we would have already
1636 * called reset(). If we got this far, assume we can print an
1637 * exit/logout message if we ignoreeof, or just exit.
1639 if (adrof(STRignoreeof)) {
1640 /* If so, tell the user to use exit or logout */
1642 xprintf(CGETS(16, 2,
1643 "\nUse \"logout\" to logout.\n"));
1645 xprintf(CGETS(16, 3,
1646 "\nUse \"exit\" to leave %s.\n"),
1651 /* If we don't have ignoreeof set, just fall through */
1660 if (c == '\n' && onelflg)
1663 if (histlinep < histline + BUFSIZE)
1664 *histlinep++ = (Char) c;
1674 while (buf >= fblocks) {
1675 nfbuf = (Char **) xcalloc((size_t) (fblocks + 2),
1678 (void) blkcpy(nfbuf, fbuf);
1679 xfree((ptr_t) fbuf);
1682 fbuf[fblocks] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
1688 wide_read(fildes, buf, nchars, use_fclens)
1694 char cbuf[BUFSIZE + 1];
1698 assert (nchars <= sizeof(cbuf)/sizeof(*cbuf));
1706 r = read(fildes, cbuf + partial,
1707 nchars > partial ? nchars - partial : 1);
1708 while (partial != 0 && r < 0 && errno == EINTR);
1709 if (partial == 0 && r <= 0)
1713 while (i < partial) {
1716 len = normal_mbtowc(buf + res, cbuf + i, partial - i);
1719 if (partial < MB_LEN_MAX && r > 0)
1720 /* Maybe a partial character and there is still a chance
1723 buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE;
1736 memmove(cbuf, cbuf + i, partial - i);
1738 } while (partial != 0);
1739 /* Throwing away possible partial multibyte characters on error */
1740 return res != 0 ? res : r;
1748 int numleft = 0, roomleft;
1751 if (fseekp < fbobp || fseekp > feobp) {
1752 fbobp = feobp = fseekp;
1753 (void) lseek(SHIN, fseekp, L_SET);
1755 if (fseekp == feobp) {
1758 c = wide_read(SHIN, fbuf[0], BUFSIZE, 1);
1759 while (c < 0 && errno == EINTR);
1762 stderror(ERR_SYSTEM, progname, strerror(errno));
1768 #ifndef WINNT_NATIVE
1769 ch = fbuf[0][fseekp - fbobp];
1773 ch = fbuf[0][fseekp - fbobp];
1775 } while(ch == '\r');
1776 #endif /* !WINNT_NATIVE */
1780 while (fseekp >= feobp) {
1782 #if defined(FILEC) && defined(TIOCSTI)
1784 #endif /* FILEC && TIOCSTI */
1785 ) && intty) { /* then use twenex routine */
1786 fseekp = feobp; /* where else? */
1787 #if defined(FILEC) && defined(TIOCSTI)
1789 c = numleft = tenex(InputBuf, BUFSIZE);
1791 #endif /* FILEC && TIOCSTI */
1792 c = numleft = Inputl(); /* PWP: get a line */
1793 while (numleft > 0) {
1794 off = (int) feobp % BUFSIZE;
1795 buf = (int) feobp / BUFSIZE;
1797 roomleft = BUFSIZE - off;
1798 if (roomleft > numleft)
1800 (void) memmove((ptr_t) (fbuf[buf] + off),
1801 (ptr_t) (InputBuf + c - numleft),
1802 (size_t) (roomleft * sizeof(Char)));
1803 numleft -= roomleft;
1807 off = (int) feobp % BUFSIZE;
1808 buf = (int) feobp / BUFSIZE;
1810 roomleft = BUFSIZE - off;
1811 c = wide_read(SHIN, fbuf[buf] + off, (size_t) roomleft, 0);
1815 if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1))
1820 (void) check_window_size(0); /* for window systems */
1821 #endif /* SIG_WINDOW */
1822 #ifndef WINNT_NATIVE
1823 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1827 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1829 } while(ch == '\r');
1830 #endif /* !WINNT_NATIVE */
1843 sb = (int) (fseekp - 1) / BUFSIZE;
1845 for (i = 0; i < sb; i++)
1846 xfree((ptr_t) fbuf[i]);
1847 (void) blkcpy(fbuf, &fbuf[sb]);
1848 fseekp -= BUFSIZE * sb;
1849 feobp -= BUFSIZE * sb;
1858 switch (aret = l->type) {
1860 evalvec = l->a_seek;
1863 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp);
1870 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp);
1875 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp);
1880 if (fseekp >= fbobp) {
1885 for (i = 0; i < feobp - fbobp; i++) {
1897 fbobp = feobp = fseekp + 1; /* To force lseek() */
1902 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1907 /* any similarity to bell telephone is purely accidental */
1912 switch (l->type = aret) {
1914 l->a_seek = evalvec;
1917 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp);
1924 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp);
1929 if (cantell && fseekp >= fbobp && fseekp < feobp) {
1933 for (i = 0; i < fseekp - fbobp; i++)
1934 l->f_seek += fclens[i];
1941 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp);
1945 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1953 (void) lseek(SHIN, (off_t) 0, L_XTND);
1969 if (arginp || onelflg || intty)
1971 if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
1973 fbuf = (Char **) xcalloc(2, sizeof(Char **));
1975 fbuf[0] = (Char *) xcalloc(BUFSIZE, sizeof(Char));
1976 fseekp = fbobp = feobp = x;