1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.lex.c,v 3.81 2009/06/25 21:15:37 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("$tcsh: sh.lex.c,v 3.81 2009/06/25 21:15:37 christos Exp $")
40 /* #define DEBUG_INP */
41 /* #define DEBUG_SEEK */
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.
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);
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.
85 * Getdol invokes history substitution, hence the extra peek, peekd,
86 * which it can ungetD to be before history substitutions.
88 static Char peekc = 0, peekd = 0;
89 static Char peekread = 0;
91 /* (Tail of) current word from ! subst */
92 static Char *exclp = NULL;
94 /* The rest of the ! subst words */
95 static struct wordent *exclnxt = NULL;
97 /* Count of remaining words in ! subst */
100 /* "Globp" for alias resubstitution */
101 int aret = TCSH_F_SEEK;
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.
110 struct Strbuf labuf; /* = Strbuf_INIT; */
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
119 static int hadhist = 0;
122 * Avoid alias expansion recursion via \!#
126 struct Strbuf histline; /* = Strbuf_INIT; last line input */
128 int histvalid = 0; /* is histline valid */
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 (Char *);
143 * special parsing rules apply for source -h
145 extern int enterhist;
148 lex(struct wordent *hp)
152 int parsehtime = enterhist;
158 hp->next = hp->prev = hp;
163 while (c == ' ' || c == '\t');
164 if (c == (eChar)HISTSUB && intty)
165 /* ^lef^rit from tty is short !:s^lef^rit */
169 cleanup_push(hp, lex_cleanup);
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.
178 new = xmalloc(sizeof(*new));
185 wdp->word = word(parsehtime);
187 } while (wdp->word[0] != '\n');
190 Strbuf_terminate(&histline);
191 if (histline.len != 0 && histline.s[histline.len - 1] == '\n')
192 histline.s[histline.len - 1] = '\0';
199 a2time_t(Char *wordx)
201 /* Attempt to distinguish timestamps from other possible entries.
202 * Format: "+NNNNNNNNNN" (10 digits, left padded with ascii '0') */
208 if (!wordx || *(s = wordx) != '+')
211 for (++s, ret = 0, ct = 0; *s; ++s, ++ct) {
212 if (!isdigit((unsigned char)*s))
214 ret = ret * 10 + (time_t)((unsigned char)*s - '0');
224 prlex(struct wordent *sp0)
226 struct wordent *sp = sp0->next;
229 xprintf("%S", sp->word);
233 if (sp->word[0] != '\n')
239 copylex(struct wordent *hp, struct wordent *fp)
248 new = xmalloc(sizeof(*new));
255 wdp->word = Strsave(fp->word);
257 } while (wdp->word[0] != '\n');
261 freelex(struct wordent *vp)
265 while (vp->next != vp) {
275 lex_cleanup(void *xvp)
287 struct Strbuf wbuf = Strbuf_INIT;
292 cleanup_push(&wbuf, Strbuf_cleanup);
294 while ((c = getC(DOALL)) == ' ' || c == '\t')
296 if (cmap(c, _META | _ESC))
302 Strbuf_append1(&wbuf, c);
305 Strbuf_append1(&wbuf, c1);
311 if (intty || (enterhist && !parsehtime))
318 if (h < 11 && parsehtime)
323 Htime = a2time_t(hbuf);
333 Strbuf_append1(&wbuf, c);
343 if (c != (eChar)HIST)
344 Strbuf_append1(&wbuf, '\\');
357 else if (c == '\\') {
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.
364 * Actually, all I really want to be able to say is 'foo\'bar' --> foo'bar
366 if (c == (eChar)HIST)
370 ((c == '\'') || (c == '"') ||
371 (c == '\\') || (c == '$'))) {
377 * if (c1 == '`') c = ' '; else
385 else if (c == '\n') {
386 seterror(ERR_UNMATCHED, c1);
391 else if (cmap(c, _META | _QF | _QB | _ESC)) {
399 if (c != (eChar)HIST)
400 Strbuf_append1(&wbuf, '\\');
403 else if (cmap(c, _QF | _QB)) { /* '"` */
405 dolflg = c == '"' ? DOALL : DOEXCL;
407 else if (c != '#' || (!intty && !enterhist)) {
412 Strbuf_append1(&wbuf, c);
416 cleanup_ignore(&wbuf);
417 cleanup_until(&wbuf);
418 return Strbuf_finish(&wbuf);
427 if ((c = peekc) != 0) {
431 if (lap < labuf.len) {
433 if (cmap(c, _META | _QF | _QB))
437 if ((c = peekd) != 0) {
442 if ((c = *exclp++) != 0)
444 if (exclnxt && --exclc >= 0) {
445 exclnxt = exclnxt->next;
446 setexclp(exclnxt->word);
451 /* this will throw away the dummy history entries */
456 exclnxt = exclnxt->next;
460 setexclp(exclnxt->word);
464 if (c == '$' && (flag & DODOL)) {
468 if (c == (eChar)HIST && (flag & DOEXCL)) {
480 struct Strbuf name = Strbuf_INIT;
485 c = sc = getC(DOEXCL);
486 if (any("\t \n", c)) {
491 cleanup_push(&name, Strbuf_cleanup);
492 Strbuf_append1(&name, '$');
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);
504 seterror(ERR_SPDOLLT);
511 seterror(ERR_NEWLINE);
516 seterror(ERR_SPSTAR);
522 /* let $?0 pass for now */
528 while ((c = getC(DOEXCL)) != 0) {
531 Strbuf_append1(&name, c);
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))
539 Strbuf_append1(&name, c);
544 seterror(ERR_VARILL);
554 Strbuf_append1(&name, c);
557 * Michael Greim: Allow $ expansion to take place in selector
558 * expressions. (limits the number of characters returned)
560 c = getC(DOEXCL | DODOL);
564 seterror(ERR_NLINDEX);
567 Strbuf_append1(&name, c);
573 * if the :g modifier is followed by a newline, then error right away!
577 int gmodflag = 0, amodflag = 0;
580 Strbuf_append1(&name, c), c = getC(DOEXCL);
581 if (c == 'g' || c == 'a') {
586 Strbuf_append1(&name, c); c = getC(DOEXCL);
588 if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
593 Strbuf_append1(&name, c); c = getC(DOEXCL);
595 Strbuf_append1(&name, c);
596 /* scan s// [eichin:19910926.0512EST] */
599 eChar delim = getC(0);
601 Strbuf_append1(&name, delim);
602 if (!delim || letter(delim)
603 || Isdigit(delim) || any(" \t\n", delim)) {
604 seterror(ERR_BADSUBST);
607 while ((c = getC(0)) != CHAR_ERR) {
608 Strbuf_append1(&name, c);
609 if(c == delim) delimcnt--;
613 seterror(ERR_BADSUBST);
618 if (!any("htrqxesul", c)) {
619 if ((amodflag || gmodflag) && c == '\n')
620 stderror(ERR_VARSYN); /* strike */
621 seterror(ERR_BADMOD, c);
625 while ((c = getC(DOEXCL)) == ':');
634 seterror(ERR_MISSING, '}');
637 Strbuf_append1(&name, c);
640 cleanup_ignore(&name);
641 cleanup_until(&name);
642 addla(Strbuf_finish(&name));
645 /* xfree()'s its argument */
649 static struct Strbuf buf; /* = Strbuf_INIT; */
652 Strbuf_appendn(&buf, labuf.s + lap, labuf.len - lap);
654 Strbuf_append(&labuf, cp);
655 Strbuf_terminate(&labuf);
656 Strbuf_appendn(&labuf, buf.s, buf.len);
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 */
670 struct wordent *hp, *ip;
671 int left, right, dol;
690 for (ip = hp->next->next; ip != alhistt; ip = ip->next)
693 for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
695 left = 0, right = dol;
697 ungetC('s'), unreadc(HISTSUB), c = ':';
701 if (!any(":^$*-%", c))
707 if (letter(c) || c == '&') {
709 left = 0, right = dol;
715 if (!getsel(&left, &right, dol))
721 if (!getsel(&left, &right, dol))
726 exclc = right - left + 1;
729 if (sc == HISTSUB || c == ':') {
739 seterror(ERR_BADBANG);
744 static struct wordent *
745 getsub(struct wordent *en)
756 while (c == 'g' || c == 'a') {
757 global |= (c == 'g') ? FLAG_G : FLAG_A;
781 seterror(ERR_NOSUBST);
785 Strbuf_append(&lhsb, slhs.s);
786 Strbuf_terminate(&lhsb);
798 if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
801 seterror(ERR_BADSUBST);
804 Strbuf_terminate(&lhsb);
816 if (c != delim && c != '\\')
817 Strbuf_append1(&lhsb, '\\');
819 Strbuf_append1(&lhsb, c);
822 Strbuf_terminate(&lhsb);
823 else if (lhsb.s[0] == 0) {
827 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
839 if (c != delim /* && c != '~' */ )
840 Strbuf_append1(&rhsb, '\\');
842 Strbuf_append1(&rhsb, c);
844 Strbuf_terminate(&rhsb);
850 seterror(ERR_BADBANGMOD, (int)c);
854 if (lhsb.s != NULL && lhsb.len != 0)
855 Strbuf_append(&slhs, lhsb.s);
856 Strbuf_terminate(&slhs);
858 en = dosub(sc, en, global);
860 while ((c = getC(0)) == ':');
867 * From Beto Appleton (beto@aixwiz.austin.ibm.com)
869 * when using history substitution, and the variable
870 * 'history' is set to a value higher than 1000,
871 * the shell might either freeze (hang) or core-dump.
872 * We raise the limit to 50000000
875 #define HIST_PURGE -50000000
876 static struct wordent *
877 dosub(Char sc, struct wordent *en, int global)
880 int didsub = 0, didone = 0;
881 struct wordent *hp = &lexi;
888 struct wordent *new = xcalloc(1, sizeof *wdp);
897 Char *tword, *otword;
899 if ((global & FLAG_G) || didsub == 0) {
903 tword = subword(en->word, sc, &didone, &pos);
906 if (global & FLAG_A) {
907 while (didone && tword != STRNULL) {
909 tword = subword(otword, sc, &didone, &pos);
910 if (Strcmp(tword, otword) == 0) {
920 tword = Strsave(en->word);
925 seterror(ERR_MODFAIL);
928 * ANSI mode HP/UX compiler chokes on
929 * return &enthist(HIST_PURGE, &lexi, 0)->Hlex;
931 hst = enthist(HIST_PURGE, &lexi, 0, 0);
935 /* Return a newly allocated result of one modification of CP using the
936 operation TYPE. Set ADID to 1 if a modification was performed.
937 If TYPE == 's', perform substitutions only from *START_POS on and set
938 *START_POS to the position of next substitution attempt. */
940 subword(Char *cp, Char type, int *adid, size_t *start_pos)
955 wp = domod(cp, type);
958 return (Strsave(cp));
964 for (mp = cp + *start_pos; *mp; mp++) {
965 if (matchs(mp, lhsb.s)) {
966 struct Strbuf wbuf = Strbuf_INIT;
968 Strbuf_appendn(&wbuf, cp, mp - cp);
969 for (np = rhsb.s; *np; np++)
978 Strbuf_append1(&wbuf, *np);
982 Strbuf_append(&wbuf, lhsb.s);
985 *start_pos = wbuf.len;
986 Strbuf_append(&wbuf, mp + lhsb.len);
988 return Strbuf_finish(&wbuf);
992 return (Strsave(cp));
997 domod(Char *cp, Char type)
1007 for (xp = wp; (c = *xp) != 0; xp++)
1008 if ((c != ' ' && c != '\t') || type == 'q')
1013 wp = NLSChangeCase(cp, 1);
1014 return wp ? wp : Strsave(cp);
1017 wp = NLSChangeCase(cp, 0);
1018 return wp ? wp : Strsave(cp);
1022 if (!any(short2str(cp), '/'))
1023 return (type == 't' ? Strsave(cp) : 0);
1024 wp = Strrchr(cp, '/');
1026 xp = Strnsave(cp, wp - cp);
1028 xp = Strsave(wp + 1);
1034 for (wp--; wp >= cp && *wp != '/'; wp--)
1037 xp = Strsave(wp + 1);
1039 xp = Strnsave(cp, wp - cp);
1042 return (Strsave(type == 'e' ? STRNULL : cp));
1050 matchs(const Char *str, const Char *pat)
1052 while (*str && *pat && *str == *pat)
1058 getsel(int *al, int *ar, int dol)
1062 int first = *al < 0;
1067 if (quesarg == -1) {
1068 seterror(ERR_BADBANGARG);
1110 while (Isdigit(c)) {
1111 i = i * 10 + c - '0';
1133 if (*al > *ar || *ar > dol) {
1134 seterror(ERR_BADBANGARG);
1141 static struct wordent *
1150 c = sc == HISTSUB ? (eChar)HIST : getC(0);
1151 if (c == (eChar)HIST) {
1165 if (lastev == eventno && alhistp)
1170 case '#': /* !# is command being typed in (mrh) */
1172 seterror(ERR_HISTLOOP);
1185 if (any("(=~", c)) {
1190 Strbuf_terminate(&lhsb);
1193 while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^*-%${}:#", c)) {
1194 if (event != -1 && Isdigit(c))
1195 event = event * 10 + c - '0';
1198 Strbuf_append1(&lhsb, c);
1202 if (lhsb.len == 0) {
1203 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1207 Strbuf_terminate(&lhsb);
1210 * History had only digits
1213 event = eventno + (alhistp == 0) - event;
1217 Strbuf_append1(&lhsb, '\0'); /* Allocate space */
1218 Strbuf_terminate(&lhsb);
1219 memmove(lhsb.s + 1, lhsb.s, (lhsb.len - 1) * sizeof (*lhsb.s));
1222 hp = findev(lhsb.s, 0);
1228 Strbuf_terminate(&lhsb);
1238 Strbuf_append1(&lhsb, c);
1240 if (lhsb.len == 0) {
1241 lhsb.len = Strlen(lhsb.s); /* lhsb.s wasn't changed */
1242 if (lhsb.len == 0) {
1243 seterror(ERR_NOSEARCH);
1248 Strbuf_terminate(&lhsb);
1249 hp = findev(lhsb.s, 1);
1255 for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1256 if (hp->Hnum == event) {
1262 seterror(ERR_NOEVENT, short2str(np));
1267 static struct Hist *
1268 findev(Char *cp, int anyarg)
1272 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1275 struct wordent *lp = hp->Hlex.next;
1279 * The entries added by alias substitution don't have a newline but do
1280 * have a negative event number. Savehist() trims off these entries,
1281 * but it happens before alias expansion, too early to delete those
1282 * from the previous command.
1286 if (lp->word[0] == '\n')
1294 while (*p++ == *q++);
1298 for (dp = lp->word; *dp; dp++) {
1306 while (*p++ == *q++);
1310 } while (lp->word[0] != '\n');
1312 seterror(ERR_NOEVENT, short2str(cp));
1320 if (cp && cp[0] == '\n')
1328 peekread = (Char) c;
1335 static int sincereal; /* Number of real EOFs we've seen */
1340 if ((c = peekread) != 0) {
1350 xprintf("alvecp %c\n", *alvecp & 0xff);
1353 if ((c = *alvecp++) != 0)
1355 if (alvec && *alvec) {
1367 if ((alvecp = *alvec) != 0) {
1371 /* Infinite source! */
1377 if ((c = *evalp++) != 0)
1379 if (evalvec && *evalvec) {
1387 if (evalvec == INVPPTR) {
1391 if ((evalp = *evalvec) != 0) {
1399 if (arginp == INVPTR || onelflg == 1) {
1405 if ((c = *arginp++) == 0) {
1413 #endif /* BSDJOBS */
1415 if (c == CHAR_ERR) {
1416 #ifndef WINNT_NATIVE
1422 # endif /* TERMIO */
1426 #endif /* !WINNT_NATIVE */
1429 /* was isatty but raw with ignoreeof yields problems */
1430 #ifndef WINNT_NATIVE
1433 if (ioctl(SHIN, TCGETA, (ioctl_t) & tty) == 0 &&
1434 (tty.c_lflag & ICANON))
1436 if (ioctl(SHIN, TIOCGETP, (ioctl_t) & tty) == 0 &&
1437 (tty.sg_flags & RAW) == 0)
1438 # endif /* TERMIO */
1440 if (tcgetattr(SHIN, &tty) == 0 &&
1441 (tty.c_lflag & ICANON))
1443 #else /* WINNT_NATIVE */
1445 #endif /* !WINNT_NATIVE */
1449 #endif /* BSDJOBS */
1451 if (numeof != 0 && ++sincereal >= numeof) /* Too many EOFs? Bye! */
1455 (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1457 (void) tcsetpgrp(FSHTTY, tpgrp);
1460 # endif /* _SEQUENT */
1461 (void) killpg(ctpgrp, SIGHUP);
1464 * With the walking process group fix, this message
1465 * is now obsolete. As the foreground process group
1466 * changes, the shell needs to adjust. Well too bad.
1468 xprintf(CGETS(16, 1, "Reset tty pgrp from %d to %d\n"),
1469 (int)ctpgrp, (int)tpgrp);
1470 # endif /* notdef */
1473 #endif /* BSDJOBS */
1474 /* What follows is complicated EOF handling -- sterling@netcom.com */
1475 /* First, we check to see if we have ignoreeof set */
1476 if (adrof(STRignoreeof)) {
1477 /* If so, we check for any stopped jobs only on the first EOF */
1478 if ((sincereal == 1) && (chkstop == 0)) {
1482 /* If we don't have ignoreeof set, always check for stopped jobs */
1487 /* At this point, if there were stopped jobs, we would have already
1488 * called reset(). If we got this far, assume we can print an
1489 * exit/logout message if we ignoreeof, or just exit.
1491 if (adrof(STRignoreeof)) {
1492 /* If so, tell the user to use exit or logout */
1494 xprintf("%s", CGETS(16, 2,
1495 "\nUse \"logout\" to logout.\n"));
1497 xprintf(CGETS(16, 3,
1498 "\nUse \"exit\" to leave %s.\n"),
1503 /* If we don't have ignoreeof set, just fall through */
1512 if (c == '\n' && onelflg)
1515 Strbuf_append1(&histline, c);
1524 while (buf >= fblocks) {
1525 nfbuf = xcalloc(fblocks + 2, sizeof(Char **));
1527 (void) blkcpy(nfbuf, fbuf);
1531 fbuf[fblocks] = xcalloc(BUFSIZE, sizeof(Char));
1537 wide_read(int fildes, Char *buf, size_t nchars, int use_fclens)
1539 char cbuf[BUFSIZE + 1];
1546 assert (nchars <= sizeof(cbuf) / sizeof(*cbuf));
1552 size_t len = nchars > partial ? nchars - partial : 1;
1554 if (partial + len >= sizeof(cbuf) / sizeof(*cbuf))
1557 r = xread(fildes, cbuf + partial, len);
1559 if (partial == 0 && r <= 0)
1563 while (i < partial && nchars != 0) {
1566 tlen = normal_mbtowc(buf + res, cbuf + i, partial - i);
1569 if ((partial - i) < MB_LEN_MAX && r > 0)
1570 /* Maybe a partial character and there is still a chance
1573 buf[res] = (unsigned char)cbuf[i] | INVALID_BYTE;
1586 memmove(cbuf, cbuf + i, partial - i);
1588 } while (partial != 0 && nchars > 0);
1589 /* Throwing away possible partial multibyte characters on error if the
1590 stream is not seekable */
1592 lseek(fildes, -(off_t)partial, L_INCR);
1594 return res != 0 ? res : r;
1602 int numleft = 0, roomleft;
1605 if (fseekp < fbobp || fseekp > feobp) {
1606 fbobp = feobp = fseekp;
1607 (void) lseek(SHIN, fseekp, L_SET);
1609 if (fseekp == feobp) {
1615 for (i = 0; i < (size_t)(feobp - fbobp); i++)
1617 fseekp = feobp = bytes;
1620 c = wide_read(SHIN, fbuf[0], BUFSIZE, 1);
1623 stderror(ERR_SYSTEM, progname, strerror(errno));
1629 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1630 ch = fbuf[0][fseekp - fbobp];
1634 ch = fbuf[0][fseekp - fbobp];
1636 } while(ch == '\r');
1637 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1641 while (fseekp >= feobp) {
1643 #if defined(FILEC) && defined(TIOCSTI)
1645 #endif /* FILEC && TIOCSTI */
1646 ) && intty) { /* then use twenex routine */
1647 fseekp = feobp; /* where else? */
1648 #if defined(FILEC) && defined(TIOCSTI)
1650 c = numleft = tenex(InputBuf, BUFSIZE);
1652 #endif /* FILEC && TIOCSTI */
1653 c = numleft = Inputl(); /* PWP: get a line */
1654 while (numleft > 0) {
1655 off = (int) feobp % BUFSIZE;
1656 buf = (int) feobp / BUFSIZE;
1658 roomleft = BUFSIZE - off;
1659 if (roomleft > numleft)
1661 (void) memcpy(fbuf[buf] + off, InputBuf + c - numleft,
1662 roomleft * sizeof(Char));
1663 numleft -= roomleft;
1667 off = (int) feobp % BUFSIZE;
1668 buf = (int) feobp / BUFSIZE;
1670 roomleft = BUFSIZE - off;
1671 c = wide_read(SHIN, fbuf[buf] + off, roomleft, 0);
1675 if (c == 0 || (c < 0 && fixio(SHIN, errno) == -1))
1680 (void) check_window_size(0); /* for window systems */
1681 #endif /* SIG_WINDOW */
1682 #if !defined(WINNT_NATIVE) && !defined(__CYGWIN__)
1683 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1687 ch = fbuf[(int) fseekp / BUFSIZE][(int) fseekp % BUFSIZE];
1689 } while(ch == '\r');
1690 #endif /* !WINNT_NATIVE && !__CYGWIN__ */
1703 sb = (int) (fseekp - 1) / BUFSIZE;
1705 for (i = 0; i < sb; i++)
1707 (void) blkcpy(fbuf, &fbuf[sb]);
1708 fseekp -= BUFSIZE * sb;
1709 feobp -= BUFSIZE * sb;
1715 bseek(struct Ain *l)
1717 switch (aret = l->type) {
1719 evalvec = l->a_seek;
1722 xprintf(CGETS(16, 4, "seek to eval %x %x\n"), evalvec, evalp);
1729 xprintf(CGETS(16, 5, "seek to alias %x %x\n"), alvec, alvecp);
1734 xprintf(CGETS(16, 6, "seek to file %x\n"), fseekp);
1739 if (fseekp >= fbobp && feobp >= fbobp) {
1744 for (i = 0; i < (size_t)(feobp - fbobp); i++) {
1756 fbobp = feobp = fseekp + 1; /* To force lseek() */
1761 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1766 /* any similarity to bell telephone is purely accidental */
1768 btell(struct Ain *l)
1770 switch (l->type = aret) {
1772 l->a_seek = evalvec;
1775 xprintf(CGETS(16, 8, "tell eval %x %x\n"), evalvec, evalp);
1782 xprintf(CGETS(16, 9, "tell alias %x %x\n"), alvec, alvecp);
1787 if (cantell && fseekp >= fbobp && fseekp <= feobp) {
1791 for (i = 0; i < (size_t)(fseekp - fbobp); i++)
1792 l->f_seek += fclens[i];
1799 xprintf(CGETS(16, 10, "tell file %x\n"), fseekp);
1803 xprintf(CGETS(16, 7, "Bad seek type %d\n"), aret);
1811 (void) lseek(SHIN, (off_t) 0, L_XTND);
1827 if (arginp || onelflg || intty)
1829 if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
1831 fbuf = xcalloc(2, sizeof(Char **));
1833 fbuf[0] = xcalloc(BUFSIZE, sizeof(Char));
1834 fseekp = fbobp = feobp = x;