1 /* $NetBSD: lex.c,v 1.31 2013/08/06 05:42:43 christos Exp $ */
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)lex.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: lex.c,v 1.31 2013/08/06 05:42:43 christos Exp $");
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
55 * These lexical routines read input and form lists of words.
56 * There is some involved processing here, because of the complications
57 * of input buffering, and especially because of history substitution.
60 static Char
*word(void);
61 static int getC1(int);
62 static void getdol(void);
63 static void getexcl(int);
64 static struct Hist
*findev(Char
*, int);
65 static void setexclp(Char
*);
66 static int bgetc(void);
67 static void bfree(void);
68 static struct wordent
*gethent(int);
69 static int matchs(Char
*, Char
*);
70 static int getsel(int *, int *, int);
71 static struct wordent
*getsub(struct wordent
*);
72 static Char
*subword(Char
*, int, int *);
73 static struct wordent
*dosub(int, struct wordent
*, int);
76 * Peekc is a peek character for getC, peekread for readc.
77 * There is a subtlety here in many places... history routines
78 * will read ahead and then insert stuff into the input stream.
79 * If they push back a character then they must push it behind
80 * the text substituted by the history substitution. On the other
81 * hand in several places we need 2 peek characters. To make this
82 * all work, the history routines read with getC, and make use both
83 * of ungetC and unreadc. The key observation is that the state
84 * of getC at the call of a history reference is such that calls
85 * to getC from the history routines will always yield calls of
86 * readc, unless this peeking is involved. That is to say that during
87 * getexcl the variables lap, exclp, and exclnxt are all zero.
89 * Getdol invokes history substitution, hence the extra peek, peekd,
90 * which it can ungetD to be before history substitutions.
92 static int peekc
= 0, peekd
= 0;
93 static int peekread
= 0;
95 /* (Tail of) current word from ! subst */
96 static Char
*exclp
= NULL
;
98 /* The rest of the ! subst words */
99 static struct wordent
*exclnxt
= NULL
;
101 /* Count of remaining words in ! subst */
102 static int exclc
= 0;
104 /* "Globp" for alias resubstitution */
105 Char
**alvec
, *alvecp
;
109 * Labuf implements a general buffer for lookahead during lexical operations.
110 * Text which is to be placed in the input stream can be stuck here.
111 * We stick parsed ahead $ constructs during initial input,
112 * process id's from `$$', and modified variable values (from qualifiers
113 * during expansion in sh.dol.c) here.
115 static Char labuf
[BUFSIZE
];
118 * Lex returns to its caller not only a wordlist (as a "var" parameter)
119 * but also whether a history substitution occurred. This is used in
120 * the main (process) routine to determine whether to echo, and also
121 * when called by the alias routine to determine whether to keep the
124 static int hadhist
= 0;
127 * Avoid alias expansion recursion via \!#
133 #define getC(f) ((getCtmp = peekc) ? (peekc = 0, getCtmp) : getC1(f))
134 #define ungetC(c) peekc = c
135 #define ungetD(c) peekd = c
138 lex(struct wordent
*hp
)
144 hp
->next
= hp
->prev
= hp
;
149 while (c
== ' ' || c
== '\t');
150 if (c
== HISTSUB
&& intty
)
151 /* ^lef^rit from tty is short !:s^lef^rit */
157 * The following loop is written so that the links needed by freelex will
158 * be ready and rarin to go even if it is interrupted.
163 new = xmalloc(sizeof(*wdp
));
170 } while (wdp
->word
[0] != '\n');
176 prlex(FILE *fp
, struct wordent
*sp0
)
182 (void)fprintf(fp
, "%s", vis_str(sp
->word
));
186 if (sp
->word
[0] != '\n')
187 (void) fputc(' ', fp
);
193 sprlex(char **s
, struct wordent
*sp0
)
200 char *w
= vis_str(sp
->word
);
202 if (asprintf(s
, "%s", w
) < 0)
205 } else if (*os
!= '\n') {
206 if (asprintf(s
, "%s %s", os
, w
) < 0) {
222 copylex(struct wordent
*hp
, struct wordent
*fp
)
231 new = xmalloc(sizeof(*wdp
));
236 wdp
->word
= Strsave(fp
->word
);
238 } while (wdp
->word
[0] != '\n');
243 freelex(struct wordent
*vp
)
247 while (vp
->next
!= vp
) {
250 xfree((ptr_t
) fp
->word
);
259 Char wbuf
[BUFSIZE
], *wp
;
266 while ((c
= getC(DOALL
)) == ' ' || c
== '\t')
268 if (cmap(c
, _META
| _ESC
))
321 else if (c
== '\\') {
328 * if (c1 == '`') c = ' '; else
335 else if (c
== '\n') {
336 seterror(ERR_UNMATCHED
, c1
);
341 else if (cmap(c
, _META
| _QF
| _QB
| _ESC
)) {
353 else if (cmap(c
, _QF
| _QB
)) { /* '"` */
355 dolflg
= c
== '"' ? DOALL
: DOEXCL
;
357 else if (c
!= '#' || !intty
) {
367 seterror(ERR_WTOOLONG
);
374 return (Strsave(wbuf
));
383 if ((c
= peekc
) != '\0') {
388 if ((c
= *lap
++) == 0)
391 if (cmap(c
, _META
| _QF
| _QB
))
396 if ((c
= peekd
) != '\0') {
401 if ((c
= *exclp
++) != '\0')
403 if (exclnxt
&& --exclc
>= 0) {
404 exclnxt
= exclnxt
->next
;
405 setexclp(exclnxt
->word
);
412 exclnxt
= exclnxt
->next
;
416 setexclp(exclnxt
->word
);
420 if (c
== '$' && (flag
& DODOL
)) {
424 if (c
== HIST
&& (flag
& DOEXCL
)) {
436 Char name
[4*MAXVARLEN
+1], *ep
, *np
;
438 int special
, toolong
;
441 np
= name
, *np
++ = '$';
442 c
= sc
= getC(DOEXCL
);
443 if (any("\t \n", c
)) {
449 *np
++ = (Char
)c
, c
= getC(DOEXCL
);
450 if (c
== '#' || c
== '?')
451 special
++, *np
++ = (Char
)c
, c
= getC(DOEXCL
);
458 seterror(ERR_SPDOLLT
);
465 seterror(ERR_NEWLINE
);
471 seterror(ERR_SPSTAR
);
479 /* let $?0 pass for now */
487 /* we know that np < &name[4] */
489 while ((c
= getC(DOEXCL
)) != '\0'){
498 else if (letter(c
)) {
499 /* we know that np < &name[4] */
502 while ((c
= getC(DOEXCL
)) != '\0') {
503 /* Bugfix for ${v123x} from Chris Torek, DAS DEC-90. */
504 if (!letter(c
) && !Isdigit(c
))
514 seterror(ERR_VARILL
);
519 seterror(ERR_VARTOOLONG
);
529 * Name up to here is a max of MAXVARLEN + 8.
531 ep
= &np
[2 * MAXVARLEN
+ 8];
534 * Michael Greim: Allow $ expansion to take place in selector
535 * expressions. (limits the number of characters returned)
537 c
= getC(DOEXCL
| DODOL
);
541 seterror(ERR_NLINDEX
);
551 seterror(ERR_SELOVFL
);
558 * Name up to here is a max of 2 * MAXVARLEN + 8.
562 * if the :g modifier is followed by a newline, then error right away!
565 int amodflag
, gmodflag
;
570 *np
++ = (Char
)c
, c
= getC(DOEXCL
);
571 if (c
== 'g' || c
== 'a') {
576 *np
++ = (Char
)c
; c
= getC(DOEXCL
);
578 if ((c
== 'g' && !gmodflag
) || (c
== 'a' && !amodflag
)) {
583 *np
++ = (Char
)c
, c
= getC(DOEXCL
);
586 /* scan s// [eichin:19910926.0512EST] */
592 if (!delim
|| letter(delim
)
593 || Isdigit(delim
) || any(" \t\n", delim
)) {
594 seterror(ERR_BADSUBST
);
597 while ((c
= getC(0)) != -1) {
599 if(c
== delim
) delimcnt
--;
603 seterror(ERR_BADSUBST
);
608 if (!any("htrqxes", c
)) {
609 if ((amodflag
|| gmodflag
) && c
== '\n')
610 stderror(ERR_VARSYN
); /* strike */
611 seterror(ERR_VARMOD
, c
);
617 while ((c
= getC(DOEXCL
)) == ':');
626 seterror(ERR_MISSING
, '}');
643 if (Strlen(cp
) + (lap
? Strlen(lap
) : 0) >=
644 (sizeof(labuf
) - 4) / sizeof(Char
)) {
645 seterror(ERR_EXPOVFL
);
649 (void)Strcpy(buf
, lap
);
650 (void)Strcpy(labuf
, cp
);
652 (void)Strcat(labuf
, buf
);
656 static Char lhsb
[32];
657 static Char slhs
[32];
658 static Char rhsb
[64];
664 struct wordent
*hp
, *ip
;
665 int c
, dol
, left
, right
;
682 for (ip
= hp
->next
->next
; ip
!= alhistt
; ip
= ip
->next
)
685 for (ip
= hp
->next
->next
; ip
!= hp
->prev
; ip
= ip
->next
)
687 left
= 0, right
= dol
;
689 ungetC('s'), unreadc(HISTSUB
), c
= ':';
693 if (!any(":^$*-%", c
))
699 if (letter(c
) || c
== '&') {
701 left
= 0, right
= dol
;
707 if (!getsel(&left
, &right
, dol
))
713 if (!getsel(&left
, &right
, dol
))
718 exclc
= right
- left
+ 1;
721 if (sc
== HISTSUB
|| c
== ':') {
731 seterror(ERR_BADBANG
);
736 static struct wordent
*
737 getsub(struct wordent
*en
)
739 Char orhsb
[sizeof(rhsb
) / sizeof(Char
)];
748 if (c
== 'g' || c
== 'a') {
749 global
|= (c
== 'g') ? 1 : 2;
752 if (((c
=='g') && !(global
& 1)) || ((c
== 'a') && !(global
& 2))) {
753 global
|= (c
== 'g') ? 1 : 2;
772 seterror(ERR_NOSUBST
);
775 (void) Strcpy(lhsb
, slhs
);
785 if (letter(delim
) || Isdigit(delim
) || any(" \t\n", delim
)) {
788 seterror(ERR_BADSUBST
);
800 if (cp
> &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2]) {
802 seterror(ERR_BADSUBST
);
807 if (c
!= delim
&& c
!= '\\')
814 else if (lhsb
[0] == 0) {
819 (void)Strcpy(orhsb
, cp
);
830 if (&cp
[Strlen(orhsb
)] > &rhsb
[sizeof(rhsb
) /
833 (void)Strcpy(cp
, orhsb
);
838 if (cp
> &rhsb
[sizeof(rhsb
) / sizeof(Char
) - 2]) {
839 seterror(ERR_RHSLONG
);
844 if (c
!= delim
/* && c != '~' */ )
854 seterror(ERR_BADBANGMOD
, c
);
857 (void)Strcpy(slhs
, lhsb
);
859 en
= dosub(sc
, en
, global
);
861 while ((c
= getC(0)) == ':');
866 static struct wordent
*
867 dosub(int sc
, struct wordent
*en
, int global
)
869 struct wordent lexi
, *hp
, *wdp
;
880 struct wordent
*new = (struct wordent
*)xcalloc(1, sizeof *wdp
);
889 Char
*tword
, *otword
;
891 if ((global
& 1) || didsub
== 0) {
892 tword
= subword(en
->word
, sc
, &didone
);
896 while (didone
&& tword
!= STRNULL
) {
898 tword
= subword(otword
, sc
, &didone
);
899 if (Strcmp(tword
, otword
) == 0) {
900 xfree((ptr_t
) otword
);
904 xfree((ptr_t
)otword
);
909 tword
= Strsave(en
->word
);
914 seterror(ERR_MODFAIL
);
916 return (&enthist(-1000, &lexi
, 0)->Hlex
);
920 subword(Char
*cp
, int type
, int *adid
)
934 wp
= domod(cp
, type
);
936 return (Strsave(cp
));
942 for (mp
= cp
; *mp
; mp
++)
943 if (matchs(mp
, lhsb
)) {
944 for (np
= cp
; np
< mp
;)
946 for (np
= rhsb
; *np
; np
++)
954 seterror(ERR_SUBOVFL
);
960 i
-= (ssize_t
)Strlen(lhsb
);
962 seterror(ERR_SUBOVFL
);
966 (void) Strcat(wp
, lhsb
);
971 i
-= (ssize_t
)Strlen(mp
);
973 seterror(ERR_SUBOVFL
);
977 (void) Strcat(wp
, mp
);
979 return (Strsave(wbuf
));
981 return (Strsave(cp
));
986 domod(Char
*cp
, int type
)
995 for (xp
= wp
; (c
= *xp
) != '\0'; xp
++)
996 if ((c
!= ' ' && c
!= '\t') || type
== 'q')
1001 if (!any(short2str(cp
), '/'))
1002 return (type
== 't' ? Strsave(cp
) : 0);
1004 while (*--wp
!= '/')
1007 xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
1009 xp
= Strsave(wp
+ 1);
1014 for (wp
--; wp
>= cp
&& *wp
!= '/'; wp
--)
1017 xp
= Strsave(wp
+ 1);
1019 xp
= Strsave(cp
), xp
[wp
- cp
] = 0;
1022 return (Strsave(type
== 'e' ? STRNULL
: cp
));
1030 matchs(Char
*str
, Char
*pat
)
1032 while (*str
&& *pat
&& *str
== *pat
)
1038 getsel(int *al
, int *ar
, int dol
)
1048 if (quesarg
== -1) {
1049 seterror(ERR_BADBANGARG
);
1086 while (Isdigit(c
)) {
1087 i
= i
* 10 + c
- '0';
1109 if (*al
> *ar
|| *ar
> dol
) {
1110 seterror(ERR_BADBANGARG
);
1117 static struct wordent
*
1127 c
= sc
== HISTSUB
? HIST
: getC(0);
1141 if (lastev
== eventno
&& alhistp
)
1145 case '#': /* !# is command being typed in (mrh) */
1147 seterror(ERR_HISTLOOP
);
1158 if (any("(=~", c
)) {
1165 while (!cmap(c
, _ESC
| _META
| _QF
| _QB
) && !any("${}:", c
)) {
1166 if (event
!= -1 && Isdigit(c
))
1167 event
= event
* 10 + c
- '0';
1170 if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
1182 * History had only digits
1185 event
= eventno
+ (alhistp
== 0) - (event
? event
: 0);
1188 hp
= findev(lhsb
, 0);
1202 if (np
< &lhsb
[sizeof(lhsb
) / sizeof(Char
) - 2])
1207 seterror(ERR_NOSEARCH
);
1213 hp
= findev(lhsb
, 1);
1219 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
)
1220 if (hp
->Hnum
== event
) {
1228 seterror(ERR_NOEVENT
, str
);
1232 static struct Hist
*
1233 findev(Char
*cp
, int anyarg
)
1237 for (hp
= Histlist
.Hnext
; hp
; hp
= hp
->Hnext
) {
1246 * The entries added by alias substitution don't have a newline but do
1247 * have a negative event number. Savehist() trims off these entries,
1248 * but it happens before alias expansion, too early to delete those
1249 * from the previous command.
1253 if (lp
->word
[0] == '\n')
1261 while (*p
++ == *q
++);
1265 for (dp
= lp
->word
; *dp
; dp
++) {
1273 while (*p
++ == *q
++);
1277 } while (lp
->word
[0] != '\n');
1279 seterror(ERR_NOEVENT
, vis_str(cp
));
1287 if (cp
&& cp
[0] == '\n')
1301 static int sincereal
;
1305 if ((c
= peekread
) != '\0') {
1313 if ((c
= *alvecp
++) != '\0')
1315 if (alvec
&& *alvec
) {
1326 if ((alvecp
= *alvec
) != '\0') {
1330 /* Infinite source! */
1335 if ((c
= *evalp
++) != '\0')
1337 if (evalvec
&& *evalvec
) {
1345 if (evalvec
== (Char
**) 1) {
1349 if ((evalp
= *evalvec
) != '\0') {
1353 evalvec
= (Char
**) 1;
1357 if (arginp
== (Char
*) 1 || onelflg
== 1) {
1363 if ((c
= *arginp
++) == 0) {
1364 arginp
= (Char
*) 1;
1375 /* was isatty but raw with ignoreeof yields problems */
1376 if (tcgetattr(SHIN
, &tty
) == 0 && (tty
.c_lflag
& ICANON
))
1378 /* was 'short' for FILEC */
1381 if (++sincereal
> 25)
1384 (ctpgrp
= tcgetpgrp(FSHTTY
)) != -1 &&
1386 (void)tcsetpgrp(FSHTTY
, tpgrp
);
1387 (void)kill(-ctpgrp
, SIGHUP
);
1388 (void)fprintf(csherr
, "Reset tty pgrp from %ld to %ld\n",
1389 (long)ctpgrp
, (long)tpgrp
);
1392 if (adrof(STRignoreeof
)) {
1394 (void)fprintf(csherr
,"\nUse \"logout\" to logout.\n");
1396 (void)fprintf(csherr
,"\nUse \"exit\" to leave csh.\n");
1407 if (c
== '\n' && onelflg
)
1417 char tbuf
[BUFSIZE
+ 1];
1418 Char ttyline
[BUFSIZE
];
1420 ssize_t c
, numleft
, roomleft
;
1424 char tbuf
[BUFSIZE
+ 1];
1429 if (fseekp
< fbobp
|| fseekp
> feobp
) {
1430 fbobp
= feobp
= fseekp
;
1431 (void)lseek(SHIN
, fseekp
, SEEK_SET
);
1433 if (fseekp
== feobp
) {
1438 c
= read(SHIN
, tbuf
, BUFSIZE
);
1439 while (c
< 0 && errno
== EINTR
);
1442 for (i
= 0; i
< c
; i
++)
1443 fbuf
[0][i
] = (unsigned char) tbuf
[i
];
1446 c
= fbuf
[0][fseekp
- fbobp
];
1452 buf
= (int) fseekp
/ BUFSIZE
;
1453 if (buf
>= fblocks
) {
1456 nfbuf
= (Char
**)xcalloc((size_t) (fblocks
+ 2), sizeof(char **));
1458 (void)blkcpy(nfbuf
, fbuf
);
1459 xfree((ptr_t
) fbuf
);
1462 fbuf
[fblocks
] = (Char
*)xcalloc(BUFSIZE
, sizeof(Char
));
1467 if (fseekp
>= feobp
) {
1468 buf
= (int) feobp
/ BUFSIZE
;
1469 off
= (int) feobp
% BUFSIZE
;
1470 roomleft
= BUFSIZE
- off
;
1474 if ((editing
|| filec
) && intty
) {
1479 if ((p
= el_gets(el
, &d
)) != NULL
) {
1481 /* XXX: Truncation */
1482 numleft
= d
> BUFSIZE
? BUFSIZE
: d
;
1483 for (i
= 0; *p
&& i
< BUFSIZE
; i
++, p
++)
1485 ttyline
[i
- (i
== BUFSIZE
)] = '\0';
1489 c
= numleft
? numleft
: tenex(ttyline
, BUFSIZE
);
1491 /* start with fresh buffer */
1492 feobp
= fseekp
= fblocks
* BUFSIZE
;
1497 (void)memcpy(fbuf
[buf
] + off
, ttyline
,
1498 (size_t)c
* sizeof(**fbuf
));
1503 c
= read(SHIN
, tbuf
, (size_t)roomleft
);
1506 Char
*ptr
= fbuf
[buf
] + off
;
1508 for (i
= 0; i
< c
; i
++)
1509 ptr
[i
] = (unsigned char) tbuf
[i
];
1516 if (errno
== EWOULDBLOCK
) {
1519 (void)ioctl(SHIN
, FIONBIO
, (ioctl_t
) & iooff
);
1521 else if (errno
!= EINTR
)
1532 if (filec
&& !intty
)
1536 c
= fbuf
[buf
][(int)fseekp
% BUFSIZE
];
1550 sb
= (int)(fseekp
- 1) / BUFSIZE
;
1552 for (i
= 0; i
< sb
; i
++)
1553 xfree((ptr_t
) fbuf
[i
]);
1554 (void)blkcpy(fbuf
, &fbuf
[sb
]);
1555 fseekp
-= BUFSIZE
* sb
;
1556 feobp
-= BUFSIZE
* sb
;
1562 bseek(struct Ain
*l
)
1564 switch (aret
= l
->type
) {
1570 evalvec
= l
->a_seek
;
1577 (void)fprintf(csherr
, "Bad seek type %d\n", aret
);
1583 btell(struct Ain
*l
)
1585 switch (l
->type
= aret
) {
1591 l
->a_seek
= evalvec
;
1599 (void)fprintf(csherr
, "Bad seek type %d\n", aret
);
1607 (void)lseek(SHIN
, (off_t
) 0, SEEK_END
);
1622 if (arginp
|| onelflg
|| intty
)
1624 if (lseek(SHIN
, (off_t
) 0, SEEK_CUR
) < 0 || errno
== ESPIPE
)
1626 fbuf
= (Char
**)xcalloc(2, sizeof(Char
**));
1628 fbuf
[0] = (Char
*)xcalloc(BUFSIZE
, sizeof(Char
));
1629 fseekp
= fbobp
= feobp
= lseek(SHIN
, (off_t
) 0, SEEK_CUR
);