2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
36 static char sccsid[] = "@(#)lex.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
59 * These lexical routines read input and form lists of words.
60 * There is some involved processing here, because of the complications
61 * of input buffering, and especially because of history substitution.
64 static Char *word __P((void));
65 static int getC1 __P((int));
66 static void getdol __P((void));
67 static void getexcl __P((int));
69 *findev __P((Char *, bool));
70 static void setexclp __P((Char *));
71 static int bgetc __P((void));
72 static void bfree __P((void));
75 static int matchs __P((Char *, Char *));
76 static int getsel __P((int *, int *, int));
78 *getsub __P((struct wordent *));
79 static Char *subword __P((Char *, int, bool *));
81 *dosub __P((int, struct wordent *, bool));
84 * Peekc is a peek character for getC, peekread for readc.
85 * There is a subtlety here in many places... history routines
86 * will read ahead and then insert stuff into the input stream.
87 * If they push back a character then they must push it behind
88 * the text substituted by the history substitution. On the other
89 * hand in several places we need 2 peek characters. To make this
90 * all work, the history routines read with getC, and make use both
91 * of ungetC and unreadc. The key observation is that the state
92 * of getC at the call of a history reference is such that calls
93 * to getC from the history routines will always yield calls of
94 * readc, unless this peeking is involved. That is to say that during
95 * getexcl the variables lap, exclp, and exclnxt are all zero.
97 * Getdol invokes history substitution, hence the extra peek, peekd,
98 * which it can ungetD to be before history substitutions.
100 static Char peekc = 0, peekd = 0;
101 static Char peekread = 0;
103 /* (Tail of) current word from ! subst */
104 static Char *exclp = NULL;
106 /* The rest of the ! subst words */
107 static struct wordent *exclnxt = NULL;
109 /* Count of remaining words in ! subst */
110 static int exclc = 0;
112 /* "Globp" for alias resubstitution */
117 * Labuf implements a general buffer for lookahead during lexical operations.
118 * Text which is to be placed in the input stream can be stuck here.
119 * We stick parsed ahead $ constructs during initial input,
120 * process id's from `$$', and modified variable values (from qualifiers
121 * during expansion in sh.dol.c) here.
123 static Char labuf[BUFSIZ];
126 * Lex returns to its caller not only a wordlist (as a "var" parameter)
127 * but also whether a history substitution occurred. This is used in
128 * the main (process) routine to determine whether to echo, and also
129 * when called by the alias routine to determine whether to keep the
132 static bool hadhist = 0;
135 * Avoid alias expansion recursion via \!#
141 #define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
142 #define ungetC(c) peekc = c
143 #define ungetD(c) peekd = c
153 hp->next = hp->prev = hp;
158 while (c == ' ' || c == '\t');
159 if (c == HISTSUB && intty)
160 /* ^lef^rit from tty is short !:s^lef^rit */
166 * The following loop is written so that the links needed by freelex will
167 * be ready and rarin to go even if it is interrupted.
172 new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
179 } while (wdp->word[0] != '\n');
189 struct wordent *sp = sp0->next;
192 (void) fprintf(fp, "%s", vis_str(sp->word));
196 if (sp->word[0] != '\n')
197 (void) fputc(' ', fp);
213 new = (struct wordent *) xmalloc((size_t) sizeof(*wdp));
218 wdp->word = Strsave(fp->word);
220 } while (wdp->word[0] != '\n');
230 while (vp->next != vp) {
233 xfree((ptr_t) fp->word);
251 while ((c = getC(DOALL)) == ' ' || c == '\t')
253 if (cmap(c, _META | _ESC))
305 else if (c == '\\') {
312 * if (c1 == '`') c = ' '; else
319 else if (c == '\n') {
320 seterror(ERR_UNMATCHED, c1);
325 else if (cmap(c, _META | _QF | _QB | _ESC)) {
337 else if (cmap(c, _QF | _QB)) { /* '"` */
339 dolflg = c == '"' ? DOALL : DOEXCL;
341 else if (c != '#' || !intty) {
351 seterror(ERR_WTOOLONG);
358 return (Strsave(wbuf));
368 if ((c = peekc) != '\0') {
373 if ((c = *lap++) == 0)
376 if (cmap(c, _META | _QF | _QB))
381 if ((c = peekd) != '\0') {
386 if ((c = *exclp++) != '\0')
388 if (exclnxt && --exclc >= 0) {
389 exclnxt = exclnxt->next;
390 setexclp(exclnxt->word);
397 exclnxt = exclnxt->next;
401 setexclp(exclnxt->word);
405 if (c == '$' && (flag & DODOL)) {
409 if (c == HIST && (flag & DOEXCL)) {
422 Char name[4 * MAXVARLEN + 1];
425 bool special = 0, toolong;
427 np = name, *np++ = '$';
428 c = sc = getC(DOEXCL);
429 if (any("\t \n", c)) {
435 *np++ = c, c = getC(DOEXCL);
436 if (c == '#' || c == '?')
437 special++, *np++ = c, c = getC(DOEXCL);
445 seterror(ERR_SPDOLLT);
453 seterror(ERR_NEWLINE);
460 seterror(ERR_SPSTAR);
469 /* let $?0 pass for now */
477 /* we know that np < &name[4] */
479 while ((c = getC(DOEXCL)) != '\0'){
488 else if (letter(c)) {
489 /* we know that np < &name[4] */
492 while ((c = getC(DOEXCL)) != '\0') {
493 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
494 if (!letter(c) && !Isdigit(c))
504 seterror(ERR_VARILL);
509 seterror(ERR_VARTOOLONG);
519 * Name up to here is a max of MAXVARLEN + 8.
521 ep = &np[2 * MAXVARLEN + 8];
524 * Michael Greim: Allow $ expansion to take place in selector
525 * expressions. (limits the number of characters returned)
527 c = getC(DOEXCL | DODOL);
531 seterror(ERR_NLINDEX);
541 seterror(ERR_SELOVFL);
548 * Name up to here is a max of 2 * MAXVARLEN + 8.
552 * if the :g modifier is followed by a newline, then error right away!
556 int gmodflag = 0, amodflag = 0;
559 *np++ = c, c = getC(DOEXCL);
560 if (c == 'g' || c == 'a') {
565 *np++ = c; c = getC(DOEXCL);
567 if ((c == 'g' && !gmodflag) || (c == 'a' && !amodflag)) {
572 *np++ = c; c = getC(DOEXCL);
575 /* scan s// [eichin:19910926.0512EST] */
581 if (!delim || letter(delim)
582 || Isdigit(delim) || any(" \t\n", delim)) {
583 seterror(ERR_BADSUBST);
586 while ((c = getC(0)) != (-1)) {
588 if(c == delim) delimcnt--;
592 seterror(ERR_BADSUBST);
597 if (!any("htrqxes", c)) {
598 if ((amodflag || gmodflag) && c == '\n')
599 stderror(ERR_VARSYN); /* strike */
600 seterror(ERR_VARMOD, c);
606 while ((c = getC(DOEXCL)) == ':');
615 seterror(ERR_MISSING, '}');
633 if (Strlen(cp) + (lap ? Strlen(lap) : 0) >=
634 (sizeof(labuf) - 4) / sizeof(Char)) {
635 seterror(ERR_EXPOVFL);
639 (void) Strcpy(buf, lap);
640 (void) Strcpy(labuf, cp);
642 (void) Strcat(labuf, buf);
646 static Char lhsb[32];
647 static Char slhs[32];
648 static Char rhsb[64];
655 struct wordent *hp, *ip;
656 int left, right, dol;
674 for (ip = hp->next->next; ip != alhistt; ip = ip->next)
677 for (ip = hp->next->next; ip != hp->prev; ip = ip->next)
679 left = 0, right = dol;
681 ungetC('s'), unreadc(HISTSUB), c = ':';
685 if (!any(":^$*-%", c))
691 if (letter(c) || c == '&') {
693 left = 0, right = dol;
699 if (!getsel(&left, &right, dol))
705 if (!getsel(&left, &right, dol))
710 exclc = right - left + 1;
713 if (sc == HISTSUB || c == ':') {
723 seterror(ERR_BADBANG);
728 static struct wordent *
737 Char orhsb[sizeof(rhsb) / sizeof(Char)];
743 if (c == 'g' || c == 'a') {
744 global |= (c == 'g') ? 1 : 2;
747 if (((c =='g') && !(global & 1)) || ((c == 'a') && !(global & 2))) {
748 global |= (c == 'g') ? 1 : 2;
771 seterror(ERR_NOSUBST);
774 (void) Strcpy(lhsb, slhs);
786 if (letter(delim) || Isdigit(delim) || any(" \t\n", delim)) {
789 seterror(ERR_BADSUBST);
801 if (cp > &lhsb[sizeof(lhsb) / sizeof(Char) - 2]) {
803 seterror(ERR_BADSUBST);
808 if (c != delim && c != '\\')
815 else if (lhsb[0] == 0) {
820 (void) Strcpy(orhsb, cp);
831 if (&cp[Strlen(orhsb)] > &rhsb[sizeof(rhsb) /
834 (void) Strcpy(cp, orhsb);
839 if (cp > &rhsb[sizeof(rhsb) / sizeof(Char) - 2]) {
840 seterror(ERR_RHSLONG);
845 if (c != delim /* && c != '~' */ )
856 seterror(ERR_BADBANGMOD, c);
859 (void) Strcpy(slhs, lhsb);
861 en = dosub(sc, en, global);
863 while ((c = getC(0)) == ':');
868 static struct wordent *
869 dosub(sc, en, global)
875 bool didsub = 0, didone = 0;
876 struct wordent *hp = &lexi;
882 struct wordent *new =
883 (struct wordent *) xcalloc(1, sizeof *wdp);
892 Char *tword, *otword;
894 if ((global & 1) || didsub == 0) {
895 tword = subword(en->word, sc, &didone);
899 while (didone && tword != STRNULL) {
901 tword = subword(otword, sc, &didone);
902 if (Strcmp(tword, otword) == 0) {
903 xfree((ptr_t) otword);
907 xfree((ptr_t) otword);
912 tword = Strsave(en->word);
917 seterror(ERR_MODFAIL);
919 return (&enthist(-1000, &lexi, 0)->Hlex);
923 subword(cp, type, adid)
941 wp = domod(cp, type);
943 return (Strsave(cp));
950 for (mp = cp; *mp; mp++)
951 if (matchs(mp, lhsb)) {
952 for (np = cp; np < mp;)
954 for (np = rhsb; *np; np++)
964 seterror(ERR_SUBOVFL);
973 seterror(ERR_SUBOVFL);
977 (void) Strcat(wp, lhsb);
984 seterror(ERR_SUBOVFL);
988 (void) Strcat(wp, mp);
990 return (Strsave(wbuf));
992 return (Strsave(cp));
1009 for (xp = wp; (c = *xp) != '\0'; xp++)
1010 if ((c != ' ' && c != '\t') || type == 'q')
1016 if (!any(short2str(cp), '/'))
1017 return (type == 't' ? Strsave(cp) : 0);
1019 while (*--wp != '/')
1022 xp = Strsave(cp), xp[wp - cp] = 0;
1024 xp = Strsave(wp + 1);
1030 for (wp--; wp >= cp && *wp != '/'; wp--)
1033 xp = Strsave(wp + 1);
1035 xp = Strsave(cp), xp[wp - cp] = 0;
1038 return (Strsave(type == 'e' ? STRNULL : cp));
1049 while (*str && *pat && *str == *pat)
1061 bool first = *al < 0;
1066 if (quesarg == -1) {
1067 seterror(ERR_BADBANGARG);
1109 while (Isdigit(c)) {
1110 i = i * 10 + c - '0';
1132 if (*al > *ar || *ar > dol) {
1133 seterror(ERR_BADBANGARG);
1140 static struct wordent *
1150 c = sc == HISTSUB ? HIST : getC(0);
1165 if (lastev == eventno && alhistp)
1170 case '#': /* !# is command being typed in (mrh) */
1172 seterror(ERR_HISTLOOP);
1185 if (any("(=~", c)) {
1192 while (!cmap(c, _ESC | _META | _QF | _QB) && !any("^$*-%{}:", c)) {
1193 if (event != -1 && Isdigit(c))
1194 event = event * 10 + c - '0';
1197 if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1209 * History had only digits
1212 event = eventno + (alhistp == 0) - (event ? event : 0);
1215 hp = findev(lhsb, 0);
1230 if (np < &lhsb[sizeof(lhsb) / sizeof(Char) - 2])
1235 seterror(ERR_NOSEARCH);
1241 hp = findev(lhsb, 1);
1247 for (hp = Histlist.Hnext; hp; hp = hp->Hnext)
1248 if (hp->Hnum == event) {
1254 seterror(ERR_NOEVENT, vis_str(np));
1258 static struct Hist *
1265 for (hp = Histlist.Hnext; hp; hp = hp->Hnext) {
1268 struct wordent *lp = hp->Hlex.next;
1272 * The entries added by alias substitution don't have a newline but do
1273 * have a negative event number. Savehist() trims off these entries,
1274 * but it happens before alias expansion, too early to delete those
1275 * from the previous command.
1279 if (lp->word[0] == '\n')
1287 while (*p++ == *q++);
1291 for (dp = lp->word; *dp; dp++) {
1299 while (*p++ == *q++);
1303 } while (lp->word[0] != '\n');
1305 seterror(ERR_NOEVENT, vis_str(cp));
1314 if (cp && cp[0] == '\n')
1331 static int sincereal;
1334 if ((c = peekread) != '\0') {
1342 if ((c = *alvecp++) != '\0')
1344 if (alvec && *alvec) {
1355 if ((alvecp = *alvec) != '\0') {
1359 /* Infinite source! */
1364 if ((c = *evalp++) != '\0')
1366 if (evalvec && *evalvec) {
1374 if (evalvec == (Char **) 1) {
1378 if ((evalp = *evalvec) != '\0') {
1382 evalvec = (Char **) 1;
1386 if (arginp == (Char *) 1 || onelflg == 1) {
1392 if ((c = *arginp++) == 0) {
1393 arginp = (Char *) 1;
1404 /* was isatty but raw with ignoreeof yields problems */
1405 if (tcgetattr(SHIN, &tty) == 0 && (tty.c_lflag & ICANON))
1407 /* was 'short' for FILEC */
1410 if (++sincereal > 25)
1413 (ctpgrp = tcgetpgrp(FSHTTY)) != -1 &&
1415 (void) tcsetpgrp(FSHTTY, tpgrp);
1416 (void) killpg((pid_t) ctpgrp, SIGHUP);
1417 (void) fprintf(csherr, "Reset tty pgrp from %d to %d\n",
1421 if (adrof(STRignoreeof)) {
1423 (void) fprintf(csherr,"\nUse \"logout\" to logout.\n");
1425 (void) fprintf(csherr,"\nUse \"exit\" to leave csh.\n");
1436 if (c == '\n' && onelflg)
1448 int numleft = 0, roomleft;
1449 Char ttyline[BUFSIZ];
1451 char tbuf[BUFSIZ + 1];
1454 if (fseekp < fbobp || fseekp > feobp) {
1455 fbobp = feobp = fseekp;
1456 (void) lseek(SHIN, fseekp, L_SET);
1458 if (fseekp == feobp) {
1463 c = read(SHIN, tbuf, BUFSIZ);
1464 while (c < 0 && errno == EINTR);
1467 for (i = 0; i < c; i++)
1468 fbuf[0][i] = (unsigned char) tbuf[i];
1471 c = fbuf[0][fseekp - fbobp];
1477 buf = (int) fseekp / BUFSIZ;
1478 if (buf >= fblocks) {
1480 (Char **) xcalloc((size_t) (fblocks + 2),
1484 (void) blkcpy(nfbuf, fbuf);
1485 xfree((ptr_t) fbuf);
1488 fbuf[fblocks] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
1493 if (fseekp >= feobp) {
1494 buf = (int) feobp / BUFSIZ;
1495 off = (int) feobp % BUFSIZ;
1496 roomleft = BUFSIZ - off;
1499 roomleft = BUFSIZ - off;
1501 if (filec && intty) {
1502 c = numleft ? numleft : tenex(ttyline, BUFSIZ);
1504 /* start with fresh buffer */
1505 feobp = fseekp = fblocks * BUFSIZ;
1510 memmove(fbuf[buf] + off, ttyline, c * sizeof(Char));
1515 c = read(SHIN, tbuf, roomleft);
1518 Char *ptr = fbuf[buf] + off;
1520 for (i = 0; i < c; i++)
1521 ptr[i] = (unsigned char) tbuf[i];
1528 if (errno == EWOULDBLOCK) {
1531 (void) ioctl(SHIN, FIONBIO, (ioctl_t) & off);
1533 else if (errno != EINTR)
1542 if (filec && !intty)
1546 c = fbuf[buf][(int) fseekp % BUFSIZ];
1560 sb = (int) (fseekp - 1) / BUFSIZ;
1562 for (i = 0; i < sb; i++)
1563 xfree((ptr_t) fbuf[i]);
1564 (void) blkcpy(fbuf, &fbuf[sb]);
1565 fseekp -= BUFSIZ * sb;
1566 feobp -= BUFSIZ * sb;
1575 switch (aret = l->type) {
1577 evalvec = l->a_seek;
1588 (void) fprintf(csherr, "Bad seek type %d\n", aret);
1597 switch (l->type = aret) {
1599 l->a_seek = evalvec;
1611 (void) fprintf(csherr, "Bad seek type %d\n", aret);
1619 (void) lseek(SHIN, (off_t) 0, L_XTND);
1634 if (arginp || onelflg || intty)
1636 if (lseek(SHIN, (off_t) 0, L_INCR) < 0 || errno == ESPIPE)
1638 fbuf = (Char **) xcalloc(2, sizeof(Char **));
1640 fbuf[0] = (Char *) xcalloc(BUFSIZ, sizeof(Char));
1641 fseekp = fbobp = feobp = lseek(SHIN, (off_t) 0, L_INCR);