xref: /netbsd-src/bin/sh/parser.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kenneth Almquist.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 /*static char sccsid[] = "from: @(#)parser.c	8.1 (Berkeley) 5/31/93";*/
39 static char *rcsid = "$Id: parser.c,v 1.18 1994/06/14 05:49:25 jtc Exp $";
40 #endif /* not lint */
41 
42 #include "shell.h"
43 #include "parser.h"
44 #include "nodes.h"
45 #include "expand.h"	/* defines rmescapes() */
46 #include "redir.h"	/* defines copyfd() */
47 #include "syntax.h"
48 #include "options.h"
49 #include "input.h"
50 #include "output.h"
51 #include "var.h"
52 #include "error.h"
53 #include "memalloc.h"
54 #include "mystring.h"
55 #include "alias.h"
56 #ifndef NO_HISTORY
57 #include "myhistedit.h"
58 #endif
59 
60 /*
61  * Shell command parser.
62  */
63 
64 #define EOFMARKLEN 79
65 
66 /* values returned by readtoken */
67 #include "token.def"
68 
69 
70 
71 struct heredoc {
72 	struct heredoc *next;	/* next here document in list */
73 	union node *here;		/* redirection node */
74 	char *eofmark;		/* string indicating end of input */
75 	int striptabs;		/* if set, strip leading tabs */
76 };
77 
78 
79 
80 struct heredoc *heredoclist;	/* list of here documents to read */
81 int parsebackquote;		/* nonzero if we are inside backquotes */
82 int doprompt;			/* if set, prompt the user */
83 int needprompt;			/* true if interactive and at start of line */
84 int lasttoken;			/* last token read */
85 MKINIT int tokpushback;		/* last token pushed back */
86 char *wordtext;			/* text of last word returned by readtoken */
87 MKINIT int checkkwd;            /* 1 == check for kwds, 2 == also eat newlines */
88 struct nodelist *backquotelist;
89 union node *redirnode;
90 struct heredoc *heredoc;
91 int quoteflag;			/* set if (part of) last token was quoted */
92 int startlinno;			/* line # where last token started */
93 
94 
95 #define GDB_HACK 1 /* avoid local declarations which gdb can't handle */
96 #ifdef GDB_HACK
97 static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'};
98 static const char types[] = "}-+?=";
99 #endif
100 
101 
102 STATIC union node *list __P((int));
103 STATIC union node *andor __P((void));
104 STATIC union node *pipeline __P((void));
105 STATIC union node *command __P((void));
106 STATIC union node *simplecmd __P((union node **, union node *));
107 STATIC union node *makename __P((void));
108 STATIC void parsefname __P((void));
109 STATIC void parseheredoc __P((void));
110 STATIC int readtoken __P((void));
111 STATIC int readtoken1 __P((int, char const *, char *, int));
112 STATIC void attyline __P((void));
113 STATIC int noexpand __P((char *));
114 STATIC void synexpect __P((int));
115 STATIC void synerror __P((char *));
116 STATIC void setprompt __P((int));
117 
118 /*
119  * Read and parse a command.  Returns NEOF on end of file.  (NULL is a
120  * valid parse tree indicating a blank line.)
121  */
122 
123 union node *
124 parsecmd(interact) {
125 	int t;
126 
127 	doprompt = interact;
128 	if (doprompt)
129 		setprompt(1);
130 	else
131 		setprompt(0);
132 	needprompt = 0;
133 	t = readtoken();
134 	if (t == TEOF)
135 		return NEOF;
136 	if (t == TNL)
137 		return NULL;
138 	tokpushback++;
139 	return list(1);
140 }
141 
142 
143 STATIC union node *
144 list(nlflag) {
145 	union node *n1, *n2, *n3;
146 	int tok;
147 
148 	checkkwd = 2;
149 	if (nlflag == 0 && tokendlist[peektoken()])
150 		return NULL;
151 	n1 = NULL;
152 	for (;;) {
153 		n2 = andor();
154 		tok = readtoken();
155 		if (tok == TBACKGND) {
156 			if (n2->type == NCMD || n2->type == NPIPE) {
157 				n2->ncmd.backgnd = 1;
158 			} else if (n2->type == NREDIR) {
159 				n2->type = NBACKGND;
160 			} else {
161 				n3 = (union node *)stalloc(sizeof (struct nredir));
162 				n3->type = NBACKGND;
163 				n3->nredir.n = n2;
164 				n3->nredir.redirect = NULL;
165 				n2 = n3;
166 			}
167 		}
168 		if (n1 == NULL) {
169 			n1 = n2;
170 		}
171 		else {
172 			n3 = (union node *)stalloc(sizeof (struct nbinary));
173 			n3->type = NSEMI;
174 			n3->nbinary.ch1 = n1;
175 			n3->nbinary.ch2 = n2;
176 			n1 = n3;
177 		}
178 		switch (tok) {
179 		case TBACKGND:
180 		case TSEMI:
181 			tok = readtoken();
182 			/* fall through */
183 		case TNL:
184 			if (tok == TNL) {
185 				parseheredoc();
186 				if (nlflag)
187 					return n1;
188 			} else {
189 				tokpushback++;
190 			}
191 			checkkwd = 2;
192 			if (tokendlist[peektoken()])
193 				return n1;
194 			break;
195 		case TEOF:
196 			if (heredoclist)
197 				parseheredoc();
198 			else
199 				pungetc();		/* push back EOF on input */
200 			return n1;
201 		default:
202 			if (nlflag)
203 				synexpect(-1);
204 			tokpushback++;
205 			return n1;
206 		}
207 	}
208 }
209 
210 
211 
212 STATIC union node *
213 andor() {
214 	union node *n1, *n2, *n3;
215 	int t;
216 
217 	n1 = pipeline();
218 	for (;;) {
219 		if ((t = readtoken()) == TAND) {
220 			t = NAND;
221 		} else if (t == TOR) {
222 			t = NOR;
223 		} else {
224 			tokpushback++;
225 			return n1;
226 		}
227 		n2 = pipeline();
228 		n3 = (union node *)stalloc(sizeof (struct nbinary));
229 		n3->type = t;
230 		n3->nbinary.ch1 = n1;
231 		n3->nbinary.ch2 = n2;
232 		n1 = n3;
233 	}
234 }
235 
236 
237 
238 STATIC union node *
239 pipeline() {
240 	union node *n1, *pipenode, *notnode;
241 	struct nodelist *lp, *prev;
242 	int negate = 0;
243 
244 	TRACE(("pipeline: entered\n"));
245 	while (readtoken() == TNOT) {
246 		TRACE(("pipeline: TNOT recognized\n"));
247 		negate = !negate;
248 	}
249 	tokpushback++;
250 	n1 = command();
251 	if (readtoken() == TPIPE) {
252 		pipenode = (union node *)stalloc(sizeof (struct npipe));
253 		pipenode->type = NPIPE;
254 		pipenode->npipe.backgnd = 0;
255 		lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
256 		pipenode->npipe.cmdlist = lp;
257 		lp->n = n1;
258 		do {
259 			prev = lp;
260 			lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
261 			lp->n = command();
262 			prev->next = lp;
263 		} while (readtoken() == TPIPE);
264 		lp->next = NULL;
265 		n1 = pipenode;
266 	}
267 	tokpushback++;
268 	if (negate) {
269 		notnode = (union node *)stalloc(sizeof (struct nnot));
270 		notnode->type = NNOT;
271 		notnode->nnot.com = n1;
272 		n1 = notnode;
273 	}
274 	return n1;
275 }
276 
277 
278 
279 STATIC union node *
280 command() {
281 	union node *n1, *n2;
282 	union node *ap, **app;
283 	union node *cp, **cpp;
284 	union node *redir, **rpp;
285 	int t;
286 
287 	checkkwd = 2;
288 	redir = 0;
289 	rpp = &redir;
290 	/* Check for redirection which may precede command */
291 	while (readtoken() == TREDIR) {
292 		*rpp = n2 = redirnode;
293 		rpp = &n2->nfile.next;
294 		parsefname();
295 	}
296 	tokpushback++;
297 
298 	switch (readtoken()) {
299 	case TIF:
300 		n1 = (union node *)stalloc(sizeof (struct nif));
301 		n1->type = NIF;
302 		n1->nif.test = list(0);
303 		if (readtoken() != TTHEN)
304 			synexpect(TTHEN);
305 		n1->nif.ifpart = list(0);
306 		n2 = n1;
307 		while (readtoken() == TELIF) {
308 			n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
309 			n2 = n2->nif.elsepart;
310 			n2->type = NIF;
311 			n2->nif.test = list(0);
312 			if (readtoken() != TTHEN)
313 				synexpect(TTHEN);
314 			n2->nif.ifpart = list(0);
315 		}
316 		if (lasttoken == TELSE)
317 			n2->nif.elsepart = list(0);
318 		else {
319 			n2->nif.elsepart = NULL;
320 			tokpushback++;
321 		}
322 		if (readtoken() != TFI)
323 			synexpect(TFI);
324 		checkkwd = 1;
325 		break;
326 	case TWHILE:
327 	case TUNTIL: {
328 		int got;
329 		n1 = (union node *)stalloc(sizeof (struct nbinary));
330 		n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
331 		n1->nbinary.ch1 = list(0);
332 		if ((got=readtoken()) != TDO) {
333 TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
334 			synexpect(TDO);
335 		}
336 		n1->nbinary.ch2 = list(0);
337 		if (readtoken() != TDONE)
338 			synexpect(TDONE);
339 		checkkwd = 1;
340 		break;
341 	}
342 	case TFOR:
343 		if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
344 			synerror("Bad for loop variable");
345 		n1 = (union node *)stalloc(sizeof (struct nfor));
346 		n1->type = NFOR;
347 		n1->nfor.var = wordtext;
348 		if (readtoken() == TWORD && ! quoteflag && equal(wordtext, "in")) {
349 			app = ≈
350 			while (readtoken() == TWORD) {
351 				n2 = (union node *)stalloc(sizeof (struct narg));
352 				n2->type = NARG;
353 				n2->narg.text = wordtext;
354 				n2->narg.backquote = backquotelist;
355 				*app = n2;
356 				app = &n2->narg.next;
357 			}
358 			*app = NULL;
359 			n1->nfor.args = ap;
360 			if (lasttoken != TNL && lasttoken != TSEMI)
361 				synexpect(-1);
362 		} else {
363 #ifndef GDB_HACK
364 			static const char argvars[5] = {CTLVAR, VSNORMAL|VSQUOTE,
365 								   '@', '=', '\0'};
366 #endif
367 			n2 = (union node *)stalloc(sizeof (struct narg));
368 			n2->type = NARG;
369 			n2->narg.text = (char *)argvars;
370 			n2->narg.backquote = NULL;
371 			n2->narg.next = NULL;
372 			n1->nfor.args = n2;
373 			/*
374 			 * Newline or semicolon here is optional (but note
375 			 * that the original Bourne shell only allowed NL).
376 			 */
377 			if (lasttoken != TNL && lasttoken != TSEMI)
378 				tokpushback++;
379 		}
380 		checkkwd = 2;
381 		if ((t = readtoken()) == TDO)
382 			t = TDONE;
383 		else if (t == TBEGIN)
384 			t = TEND;
385 		else
386 			synexpect(-1);
387 		n1->nfor.body = list(0);
388 		if (readtoken() != t)
389 			synexpect(t);
390 		checkkwd = 1;
391 		break;
392 	case TCASE:
393 		n1 = (union node *)stalloc(sizeof (struct ncase));
394 		n1->type = NCASE;
395 		if (readtoken() != TWORD)
396 			synexpect(TWORD);
397 		n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
398 		n2->type = NARG;
399 		n2->narg.text = wordtext;
400 		n2->narg.backquote = backquotelist;
401 		n2->narg.next = NULL;
402 		while (readtoken() == TNL);
403 		if (lasttoken != TWORD || ! equal(wordtext, "in"))
404 			synerror("expecting \"in\"");
405 		cpp = &n1->ncase.cases;
406 		checkkwd = 2, readtoken();
407 		do {
408 			*cpp = cp = (union node *)stalloc(sizeof (struct nclist));
409 			cp->type = NCLIST;
410 			app = &cp->nclist.pattern;
411 			for (;;) {
412 				*app = ap = (union node *)stalloc(sizeof (struct narg));
413 				ap->type = NARG;
414 				ap->narg.text = wordtext;
415 				ap->narg.backquote = backquotelist;
416 				if (checkkwd = 2, readtoken() != TPIPE)
417 					break;
418 				app = &ap->narg.next;
419 				readtoken();
420 			}
421 			ap->narg.next = NULL;
422 			if (lasttoken != TRP)
423 				synexpect(TRP);
424 			cp->nclist.body = list(0);
425 
426 			checkkwd = 2;
427 			if ((t = readtoken()) != TESAC) {
428 				if (t != TENDCASE)
429 					synexpect(TENDCASE);
430 				else
431 					checkkwd = 2, readtoken();
432 			}
433 			cpp = &cp->nclist.next;
434 		} while(lasttoken != TESAC);
435 		*cpp = NULL;
436 		checkkwd = 1;
437 		break;
438 	case TLP:
439 		n1 = (union node *)stalloc(sizeof (struct nredir));
440 		n1->type = NSUBSHELL;
441 		n1->nredir.n = list(0);
442 		n1->nredir.redirect = NULL;
443 		if (readtoken() != TRP)
444 			synexpect(TRP);
445 		checkkwd = 1;
446 		break;
447 	case TBEGIN:
448 		n1 = list(0);
449 		if (readtoken() != TEND)
450 			synexpect(TEND);
451 		checkkwd = 1;
452 		break;
453 	/* Handle an empty command like other simple commands.  */
454 	case TNL:
455 	case TWORD:
456 		tokpushback++;
457 		return simplecmd(rpp, redir);
458 	default:
459 		synexpect(-1);
460 	}
461 
462 	/* Now check for redirection which may follow command */
463 	while (readtoken() == TREDIR) {
464 		*rpp = n2 = redirnode;
465 		rpp = &n2->nfile.next;
466 		parsefname();
467 	}
468 	tokpushback++;
469 	*rpp = NULL;
470 	if (redir) {
471 		if (n1->type != NSUBSHELL) {
472 			n2 = (union node *)stalloc(sizeof (struct nredir));
473 			n2->type = NREDIR;
474 			n2->nredir.n = n1;
475 			n1 = n2;
476 		}
477 		n1->nredir.redirect = redir;
478 	}
479 	return n1;
480 }
481 
482 
483 STATIC union node *
484 simplecmd(rpp, redir)
485 	union node **rpp, *redir;
486 	{
487 	union node *args, **app;
488 	union node **orig_rpp = rpp;
489 	union node *n;
490 
491 	/* If we don't have any redirections already, then we must reset */
492 	/* rpp to be the address of the local redir variable.  */
493 	if (redir == 0)
494 		rpp = &redir;
495 
496 	args = NULL;
497 	app = &args;
498 	/*
499 	 * We save the incoming value, because we need this for shell
500 	 * functions.  There can not be a redirect or an argument between
501 	 * the function name and the open parenthesis.
502 	 */
503 	orig_rpp = rpp;
504 
505 	for (;;) {
506 		if (readtoken() == TWORD) {
507 			n = (union node *)stalloc(sizeof (struct narg));
508 			n->type = NARG;
509 			n->narg.text = wordtext;
510 			n->narg.backquote = backquotelist;
511 			*app = n;
512 			app = &n->narg.next;
513 		} else if (lasttoken == TREDIR) {
514 			*rpp = n = redirnode;
515 			rpp = &n->nfile.next;
516 			parsefname();	/* read name of redirection file */
517 		} else if (lasttoken == TLP && app == &args->narg.next
518 					    && rpp == orig_rpp) {
519 			/* We have a function */
520 			if (readtoken() != TRP)
521 				synexpect(TRP);
522 #ifdef notdef
523 			if (! goodname(n->narg.text))
524 				synerror("Bad function name");
525 #endif
526 			n->type = NDEFUN;
527 			n->narg.next = command();
528 			return n;
529 		} else {
530 			tokpushback++;
531 			break;
532 		}
533 	}
534 	*app = NULL;
535 	*rpp = NULL;
536 	n = (union node *)stalloc(sizeof (struct ncmd));
537 	n->type = NCMD;
538 	n->ncmd.backgnd = 0;
539 	n->ncmd.args = args;
540 	n->ncmd.redirect = redir;
541 	return n;
542 }
543 
544 STATIC union node *
545 makename() {
546 	union node *n;
547 
548 	n = (union node *)stalloc(sizeof (struct narg));
549 	n->type = NARG;
550 	n->narg.next = NULL;
551 	n->narg.text = wordtext;
552 	n->narg.backquote = backquotelist;
553 	return n;
554 }
555 
556 void fixredir(n, text, err)
557 	union node *n;
558 	const char *text;
559 	int err;
560 	{
561 	TRACE(("Fix redir %s %d\n", text, err));
562 	if (!err)
563 		n->ndup.vname = NULL;
564 
565 	if (is_digit(text[0]) && text[1] == '\0')
566 		n->ndup.dupfd = digit_val(text[0]);
567 	else if (text[0] == '-' && text[1] == '\0')
568 		n->ndup.dupfd = -1;
569 	else {
570 
571 		if (err)
572 			synerror("Bad fd number");
573 		else
574 			n->ndup.vname = makename();
575 	}
576 }
577 
578 
579 STATIC void
580 parsefname() {
581 	union node *n = redirnode;
582 
583 	if (readtoken() != TWORD)
584 		synexpect(-1);
585 	if (n->type == NHERE) {
586 		struct heredoc *here = heredoc;
587 		struct heredoc *p;
588 		int i;
589 
590 		if (quoteflag == 0)
591 			n->type = NXHERE;
592 		TRACE(("Here document %d\n", n->type));
593 		if (here->striptabs) {
594 			while (*wordtext == '\t')
595 				wordtext++;
596 		}
597 		if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
598 			synerror("Illegal eof marker for << redirection");
599 		rmescapes(wordtext);
600 		here->eofmark = wordtext;
601 		here->next = NULL;
602 		if (heredoclist == NULL)
603 			heredoclist = here;
604 		else {
605 			for (p = heredoclist ; p->next ; p = p->next);
606 			p->next = here;
607 		}
608 	} else if (n->type == NTOFD || n->type == NFROMFD) {
609 		fixredir(n, wordtext, 0);
610 	} else {
611 		n->nfile.fname = makename();
612 	}
613 }
614 
615 
616 /*
617  * Input any here documents.
618  */
619 
620 STATIC void
621 parseheredoc() {
622 	struct heredoc *here;
623 	union node *n;
624 
625 	while (heredoclist) {
626 		here = heredoclist;
627 		heredoclist = here->next;
628 		if (needprompt) {
629 			setprompt(2);
630 			needprompt = 0;
631 		}
632 		readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
633 				here->eofmark, here->striptabs);
634 		n = (union node *)stalloc(sizeof (struct narg));
635 		n->narg.type = NARG;
636 		n->narg.next = NULL;
637 		n->narg.text = wordtext;
638 		n->narg.backquote = backquotelist;
639 		here->here->nhere.doc = n;
640 	}
641 }
642 
643 STATIC int
644 peektoken() {
645 	int t;
646 
647 	t = readtoken();
648 	tokpushback++;
649 	return (t);
650 }
651 
652 STATIC int xxreadtoken();
653 
654 STATIC int
655 readtoken() {
656 	int t;
657 	int savecheckkwd = checkkwd;
658 	struct alias *ap;
659 #ifdef DEBUG
660 	int alreadyseen = tokpushback;
661 #endif
662 
663 	top:
664 	t = xxreadtoken();
665 
666 	if (checkkwd) {
667 		/*
668 		 * eat newlines
669 		 */
670 		if (checkkwd == 2) {
671 			checkkwd = 0;
672 			while (t == TNL) {
673 				parseheredoc();
674 				t = xxreadtoken();
675 			}
676 		} else
677 			checkkwd = 0;
678 		/*
679 		 * check for keywords and aliases
680 		 */
681 		if (t == TWORD && !quoteflag) {
682 			register char * const *pp, *s;
683 
684 			for (pp = (char **)parsekwd; *pp; pp++) {
685 				if (**pp == *wordtext && equal(*pp, wordtext)) {
686 					lasttoken = t = pp - parsekwd + KWDOFFSET;
687 					TRACE(("keyword %s recognized\n", tokname[t]));
688 					goto out;
689 				}
690 			}
691 			if (ap = lookupalias(wordtext, 1)) {
692 				pushstring(ap->val, strlen(ap->val), ap);
693 				checkkwd = savecheckkwd;
694 				goto top;
695 			}
696 		}
697 out:
698 		checkkwd = 0;
699 	}
700 #ifdef DEBUG
701 	if (!alreadyseen)
702 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
703 	else
704 	    TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
705 #endif
706 	return (t);
707 }
708 
709 
710 /*
711  * Read the next input token.
712  * If the token is a word, we set backquotelist to the list of cmds in
713  *	backquotes.  We set quoteflag to true if any part of the word was
714  *	quoted.
715  * If the token is TREDIR, then we set redirnode to a structure containing
716  *	the redirection.
717  * In all cases, the variable startlinno is set to the number of the line
718  *	on which the token starts.
719  *
720  * [Change comment:  here documents and internal procedures]
721  * [Readtoken shouldn't have any arguments.  Perhaps we should make the
722  *  word parsing code into a separate routine.  In this case, readtoken
723  *  doesn't need to have any internal procedures, but parseword does.
724  *  We could also make parseoperator in essence the main routine, and
725  *  have parseword (readtoken1?) handle both words and redirection.]
726  */
727 
728 #define RETURN(token)	return lasttoken = token
729 
730 STATIC int
731 xxreadtoken() {
732 	register c;
733 
734 	if (tokpushback) {
735 		tokpushback = 0;
736 		return lasttoken;
737 	}
738 	if (needprompt) {
739 		setprompt(2);
740 		needprompt = 0;
741 	}
742 	startlinno = plinno;
743 	for (;;) {	/* until token or start of word found */
744 		c = pgetc_macro();
745 		if (c == ' ' || c == '\t')
746 			continue;		/* quick check for white space first */
747 		switch (c) {
748 		case ' ': case '\t':
749 			continue;
750 		case '#':
751 			while ((c = pgetc()) != '\n' && c != PEOF);
752 			pungetc();
753 			continue;
754 		case '\\':
755 			if (pgetc() == '\n') {
756 				startlinno = ++plinno;
757 				if (doprompt)
758 					setprompt(2);
759 				else
760 					setprompt(0);
761 				continue;
762 			}
763 			pungetc();
764 			goto breakloop;
765 		case '\n':
766 			plinno++;
767 			needprompt = doprompt;
768 			RETURN(TNL);
769 		case PEOF:
770 			RETURN(TEOF);
771 		case '&':
772 			if (pgetc() == '&')
773 				RETURN(TAND);
774 			pungetc();
775 			RETURN(TBACKGND);
776 		case '|':
777 			if (pgetc() == '|')
778 				RETURN(TOR);
779 			pungetc();
780 			RETURN(TPIPE);
781 		case ';':
782 			if (pgetc() == ';')
783 				RETURN(TENDCASE);
784 			pungetc();
785 			RETURN(TSEMI);
786 		case '(':
787 			RETURN(TLP);
788 		case ')':
789 			RETURN(TRP);
790 		default:
791 			goto breakloop;
792 		}
793 	}
794 breakloop:
795 	return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
796 #undef RETURN
797 }
798 
799 
800 
801 /*
802  * If eofmark is NULL, read a word or a redirection symbol.  If eofmark
803  * is not NULL, read a here document.  In the latter case, eofmark is the
804  * word which marks the end of the document and striptabs is true if
805  * leading tabs should be stripped from the document.  The argument firstc
806  * is the first character of the input token or document.
807  *
808  * Because C does not have internal subroutines, I have simulated them
809  * using goto's to implement the subroutine linkage.  The following macros
810  * will run code that appears at the end of readtoken1.
811  */
812 
813 #define CHECKEND()	{goto checkend; checkend_return:;}
814 #define PARSEREDIR()	{goto parseredir; parseredir_return:;}
815 #define PARSESUB()	{goto parsesub; parsesub_return:;}
816 #define PARSEBACKQOLD()	{oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
817 #define PARSEBACKQNEW()	{oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
818 #define	PARSEARITH()	{goto parsearith; parsearith_return:;}
819 
820 STATIC int
821 readtoken1(firstc, syntax, eofmark, striptabs)
822 	int firstc;
823 	char const *syntax;
824 	char *eofmark;
825 	int striptabs;
826 	{
827 	register c = firstc;
828 	register char *out;
829 	int len;
830 	char line[EOFMARKLEN + 1];
831 	struct nodelist *bqlist;
832 	int quotef;
833 	int dblquote;
834 	int varnest;	/* levels of variables expansion */
835 	int arinest;	/* levels of arithmetic expansion */
836 	int parenlevel;	/* levels of parens in arithmetic */
837 	int oldstyle;
838 	char const *prevsyntax;	/* syntax before arithmetic */
839 
840 	startlinno = plinno;
841 	dblquote = 0;
842 	if (syntax == DQSYNTAX)
843 		dblquote = 1;
844 	quotef = 0;
845 	bqlist = NULL;
846 	varnest = 0;
847 	arinest = 0;
848 	parenlevel = 0;
849 
850 	STARTSTACKSTR(out);
851 	loop: {	/* for each line, until end of word */
852 #if ATTY
853 		if (c == '\034' && doprompt
854 		 && attyset() && ! equal(termval(), "emacs")) {
855 			attyline();
856 			if (syntax == BASESYNTAX)
857 				return readtoken();
858 			c = pgetc();
859 			goto loop;
860 		}
861 #endif
862 		CHECKEND();	/* set c to PEOF if at end of here document */
863 		for (;;) {	/* until end of line or end of word */
864 			CHECKSTRSPACE(3, out);	/* permit 3 calls to USTPUTC */
865 			switch(syntax[c]) {
866 			case CNL:	/* '\n' */
867 				if (syntax == BASESYNTAX)
868 					goto endword;	/* exit outer loop */
869 				USTPUTC(c, out);
870 				plinno++;
871 				if (doprompt)
872 					setprompt(2);
873 				else
874 					setprompt(0);
875 				c = pgetc();
876 				goto loop;		/* continue outer loop */
877 			case CWORD:
878 				USTPUTC(c, out);
879 				break;
880 			case CCTL:
881 				if (eofmark == NULL || dblquote)
882 					USTPUTC(CTLESC, out);
883 				USTPUTC(c, out);
884 				break;
885 			case CBACK:	/* backslash */
886 				c = pgetc();
887 				if (c == PEOF) {
888 					USTPUTC('\\', out);
889 					pungetc();
890 				} else if (c == '\n') {
891 					if (doprompt)
892 						setprompt(2);
893 					else
894 						setprompt(0);
895 				} else {
896 					if (dblquote && c != '\\' && c != '`' && c != '$'
897 							 && (c != '"' || eofmark != NULL))
898 						USTPUTC('\\', out);
899 					if (SQSYNTAX[c] == CCTL)
900 						USTPUTC(CTLESC, out);
901 					USTPUTC(c, out);
902 					quotef++;
903 				}
904 				break;
905 			case CSQUOTE:
906 				syntax = SQSYNTAX;
907 				break;
908 			case CDQUOTE:
909 				syntax = DQSYNTAX;
910 				dblquote = 1;
911 				break;
912 			case CENDQUOTE:
913 				if (eofmark) {
914 					USTPUTC(c, out);
915 				} else {
916 					if (arinest)
917 						syntax = ARISYNTAX;
918 					else
919 						syntax = BASESYNTAX;
920 					quotef++;
921 					dblquote = 0;
922 				}
923 				break;
924 			case CVAR:	/* '$' */
925 				PARSESUB();		/* parse substitution */
926 				break;
927 			case CENDVAR:	/* '}' */
928 				if (varnest > 0) {
929 					varnest--;
930 					USTPUTC(CTLENDVAR, out);
931 				} else {
932 					USTPUTC(c, out);
933 				}
934 				break;
935 			case CLP:	/* '(' in arithmetic */
936 				parenlevel++;
937 				USTPUTC(c, out);
938 				break;
939 			case CRP:	/* ')' in arithmetic */
940 				if (parenlevel > 0) {
941 					USTPUTC(c, out);
942 					--parenlevel;
943 				} else {
944 					if (pgetc() == ')') {
945 						if (--arinest == 0) {
946 							USTPUTC(CTLENDARI, out);
947 							syntax = prevsyntax;
948 						} else
949 							USTPUTC(')', out);
950 					} else {
951 						/*
952 						 * unbalanced parens
953 						 *  (don't 2nd guess - no error)
954 						 */
955 						pungetc();
956 						USTPUTC(')', out);
957 					}
958 				}
959 				break;
960 			case CBQUOTE:	/* '`' */
961 				PARSEBACKQOLD();
962 				break;
963 			case CEOF:
964 				goto endword;		/* exit outer loop */
965 			default:
966 				if (varnest == 0)
967 					goto endword;	/* exit outer loop */
968 				USTPUTC(c, out);
969 			}
970 			c = pgetc_macro();
971 		}
972 	}
973 endword:
974 	if (syntax == ARISYNTAX)
975 		synerror("Missing '))'");
976 	if (syntax != BASESYNTAX && ! parsebackquote && eofmark == NULL)
977 		synerror("Unterminated quoted string");
978 	if (varnest != 0) {
979 		startlinno = plinno;
980 		synerror("Missing '}'");
981 	}
982 	USTPUTC('\0', out);
983 	len = out - stackblock();
984 	out = stackblock();
985 	if (eofmark == NULL) {
986 		if ((c == '>' || c == '<')
987 		 && quotef == 0
988 		 && len <= 2
989 		 && (*out == '\0' || is_digit(*out))) {
990 			PARSEREDIR();
991 			return lasttoken = TREDIR;
992 		} else {
993 			pungetc();
994 		}
995 	}
996 	quoteflag = quotef;
997 	backquotelist = bqlist;
998 	grabstackblock(len);
999 	wordtext = out;
1000 	return lasttoken = TWORD;
1001 /* end of readtoken routine */
1002 
1003 
1004 
1005 /*
1006  * Check to see whether we are at the end of the here document.  When this
1007  * is called, c is set to the first character of the next input line.  If
1008  * we are at the end of the here document, this routine sets the c to PEOF.
1009  */
1010 
1011 checkend: {
1012 	if (eofmark) {
1013 		if (striptabs) {
1014 			while (c == '\t')
1015 				c = pgetc();
1016 		}
1017 		if (c == *eofmark) {
1018 			if (pfgets(line, sizeof line) != NULL) {
1019 				register char *p, *q;
1020 
1021 				p = line;
1022 				for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1023 				if (*p == '\n' && *q == '\0') {
1024 					c = PEOF;
1025 					plinno++;
1026 					needprompt = doprompt;
1027 				} else {
1028 					pushstring(line, strlen(line), NULL);
1029 				}
1030 			}
1031 		}
1032 	}
1033 	goto checkend_return;
1034 }
1035 
1036 
1037 /*
1038  * Parse a redirection operator.  The variable "out" points to a string
1039  * specifying the fd to be redirected.  The variable "c" contains the
1040  * first character of the redirection operator.
1041  */
1042 
1043 parseredir: {
1044 	char fd = *out;
1045 	union node *np;
1046 
1047 	np = (union node *)stalloc(sizeof (struct nfile));
1048 	if (c == '>') {
1049 		np->nfile.fd = 1;
1050 		c = pgetc();
1051 		if (c == '>')
1052 			np->type = NAPPEND;
1053 		else if (c == '&')
1054 			np->type = NTOFD;
1055 		else {
1056 			np->type = NTO;
1057 			pungetc();
1058 		}
1059 	} else {	/* c == '<' */
1060 		np->nfile.fd = 0;
1061 		c = pgetc();
1062 		if (c == '<') {
1063 			if (sizeof (struct nfile) != sizeof (struct nhere)) {
1064 				np = (union node *)stalloc(sizeof (struct nhere));
1065 				np->nfile.fd = 0;
1066 			}
1067 			np->type = NHERE;
1068 			heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1069 			heredoc->here = np;
1070 			if ((c = pgetc()) == '-') {
1071 				heredoc->striptabs = 1;
1072 			} else {
1073 				heredoc->striptabs = 0;
1074 				pungetc();
1075 			}
1076 		} else if (c == '&')
1077 			np->type = NFROMFD;
1078 		else {
1079 			np->type = NFROM;
1080 			pungetc();
1081 		}
1082 	}
1083 	if (fd != '\0')
1084 		np->nfile.fd = digit_val(fd);
1085 	redirnode = np;
1086 	goto parseredir_return;
1087 }
1088 
1089 
1090 /*
1091  * Parse a substitution.  At this point, we have read the dollar sign
1092  * and nothing else.
1093  */
1094 
1095 parsesub: {
1096 	int subtype;
1097 	int typeloc;
1098 	int flags;
1099 	char *p;
1100 #ifndef GDB_HACK
1101 	static const char types[] = "}-+?=";
1102 #endif
1103 
1104 	c = pgetc();
1105 	if (c != '(' && c != '{' && !is_name(c) && !is_special(c)) {
1106 		USTPUTC('$', out);
1107 		pungetc();
1108 	} else if (c == '(') {	/* $(command) or $((arith)) */
1109 		if (pgetc() == '(') {
1110 			PARSEARITH();
1111 		} else {
1112 			pungetc();
1113 			PARSEBACKQNEW();
1114 		}
1115 	} else {
1116 		USTPUTC(CTLVAR, out);
1117 		typeloc = out - stackblock();
1118 		USTPUTC(VSNORMAL, out);
1119 		subtype = VSNORMAL;
1120 		if (c == '{') {
1121 			c = pgetc();
1122 			subtype = 0;
1123 		}
1124 		if (is_name(c)) {
1125 			do {
1126 				STPUTC(c, out);
1127 				c = pgetc();
1128 			} while (is_in_name(c));
1129 		} else {
1130 			if (! is_special(c))
1131 badsub:				synerror("Bad substitution");
1132 			USTPUTC(c, out);
1133 			c = pgetc();
1134 		}
1135 		STPUTC('=', out);
1136 		flags = 0;
1137 		if (subtype == 0) {
1138 			if (c == ':') {
1139 				flags = VSNUL;
1140 				c = pgetc();
1141 			}
1142 			p = strchr(types, c);
1143 			if (p == NULL)
1144 				goto badsub;
1145 			subtype = p - types + VSNORMAL;
1146 		} else {
1147 			pungetc();
1148 		}
1149 		if (dblquote || arinest)
1150 			flags |= VSQUOTE;
1151 		*(stackblock() + typeloc) = subtype | flags;
1152 		if (subtype != VSNORMAL)
1153 			varnest++;
1154 	}
1155 	goto parsesub_return;
1156 }
1157 
1158 
1159 /*
1160  * Called to parse command substitutions.  Newstyle is set if the command
1161  * is enclosed inside $(...); nlpp is a pointer to the head of the linked
1162  * list of commands (passed by reference), and savelen is the number of
1163  * characters on the top of the stack which must be preserved.
1164  */
1165 
1166 parsebackq: {
1167 	struct nodelist **nlpp;
1168 	int savepbq;
1169 	union node *n;
1170 	char *volatile str;
1171 	struct jmploc jmploc;
1172 	struct jmploc *volatile savehandler;
1173 	int savelen;
1174 
1175 	savepbq = parsebackquote;
1176 	if (setjmp(jmploc.loc)) {
1177 		if (str)
1178 			ckfree(str);
1179 		parsebackquote = 0;
1180 		handler = savehandler;
1181 		longjmp(handler->loc, 1);
1182 	}
1183 	INTOFF;
1184 	str = NULL;
1185 	savelen = out - stackblock();
1186 	if (savelen > 0) {
1187 		str = ckmalloc(savelen);
1188 		bcopy(stackblock(), str, savelen);
1189 	}
1190 	savehandler = handler;
1191 	handler = &jmploc;
1192 	INTON;
1193         if (oldstyle) {
1194                 /* We must read until the closing backquote, giving special
1195                    treatment to some slashes, and then push the string and
1196                    reread it as input, interpreting it normally.  */
1197                 register char *out;
1198                 register c;
1199                 int savelen;
1200                 char *str;
1201 
1202                 STARTSTACKSTR(out);
1203                 while ((c = pgetc ()) != '`') {
1204                        if (c == '\\') {
1205                                 c = pgetc ();
1206                                 if (c != '\\' && c != '`' && c != '$'
1207                                     && (!dblquote || c != '"'))
1208                                         STPUTC('\\', out);
1209                        }
1210                        STPUTC(c, out);
1211                 }
1212                 STPUTC('\0', out);
1213                 savelen = out - stackblock();
1214                 if (savelen > 0) {
1215                         str = ckmalloc(savelen);
1216                         bcopy(stackblock(), str, savelen);
1217                 }
1218                 setinputstring(str, 1);
1219         }
1220 	nlpp = &bqlist;
1221 	while (*nlpp)
1222 		nlpp = &(*nlpp)->next;
1223 	*nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1224 	(*nlpp)->next = NULL;
1225 	parsebackquote = oldstyle;
1226 	n = list(0);
1227         if (!oldstyle && (readtoken() != TRP))
1228                 synexpect(TRP);
1229 	(*nlpp)->n = n;
1230         /* Start reading from old file again.  */
1231         if (oldstyle)
1232                 popfile();
1233 	while (stackblocksize() <= savelen)
1234 		growstackblock();
1235 	STARTSTACKSTR(out);
1236 	if (str) {
1237 		bcopy(str, out, savelen);
1238 		STADJUST(savelen, out);
1239 		INTOFF;
1240 		ckfree(str);
1241 		str = NULL;
1242 		INTON;
1243 	}
1244 	parsebackquote = savepbq;
1245 	handler = savehandler;
1246 	if (arinest || dblquote)
1247 		USTPUTC(CTLBACKQ | CTLQUOTE, out);
1248 	else
1249 		USTPUTC(CTLBACKQ, out);
1250 	if (oldstyle)
1251 		goto parsebackq_oldreturn;
1252 	else
1253 		goto parsebackq_newreturn;
1254 }
1255 
1256 /*
1257  * Parse an arithmetic expansion (indicate start of one and set state)
1258  */
1259 parsearith: {
1260 
1261 	if (++arinest == 1) {
1262 		prevsyntax = syntax;
1263 		syntax = ARISYNTAX;
1264 		USTPUTC(CTLARI, out);
1265 	} else {
1266 		/*
1267 		 * we collapse embedded arithmetic expansion to
1268 		 * parenthesis, which should be equivalent
1269 		 */
1270 		USTPUTC('(', out);
1271 	}
1272 	goto parsearith_return;
1273 }
1274 
1275 } /* end of readtoken */
1276 
1277 
1278 
1279 #ifdef mkinit
1280 RESET {
1281 	tokpushback = 0;
1282 	checkkwd = 0;
1283 }
1284 #endif
1285 
1286 /*
1287  * Returns true if the text contains nothing to expand (no dollar signs
1288  * or backquotes).
1289  */
1290 
1291 STATIC int
1292 noexpand(text)
1293 	char *text;
1294 	{
1295 	register char *p;
1296 	register char c;
1297 
1298 	p = text;
1299 	while ((c = *p++) != '\0') {
1300 		if (c == CTLESC)
1301 			p++;
1302 		else if (BASESYNTAX[c] == CCTL)
1303 			return 0;
1304 	}
1305 	return 1;
1306 }
1307 
1308 
1309 /*
1310  * Return true if the argument is a legal variable name (a letter or
1311  * underscore followed by zero or more letters, underscores, and digits).
1312  */
1313 
1314 int
1315 goodname(name)
1316 	char *name;
1317 	{
1318 	register char *p;
1319 
1320 	p = name;
1321 	if (! is_name(*p))
1322 		return 0;
1323 	while (*++p) {
1324 		if (! is_in_name(*p))
1325 			return 0;
1326 	}
1327 	return 1;
1328 }
1329 
1330 
1331 /*
1332  * Called when an unexpected token is read during the parse.  The argument
1333  * is the token that is expected, or -1 if more than one type of token can
1334  * occur at this point.
1335  */
1336 
1337 STATIC void
1338 synexpect(token) {
1339 	char msg[64];
1340 
1341 	if (token >= 0) {
1342 		fmtstr(msg, 64, "%s unexpected (expecting %s)",
1343 			tokname[lasttoken], tokname[token]);
1344 	} else {
1345 		fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1346 	}
1347 	synerror(msg);
1348 }
1349 
1350 
1351 STATIC void
1352 synerror(msg)
1353 	char *msg;
1354 	{
1355 	if (commandname)
1356 		outfmt(&errout, "%s: %d: ", commandname, startlinno);
1357 	outfmt(&errout, "Syntax error: %s\n", msg);
1358 	error((char *)NULL);
1359 }
1360 
1361 STATIC void
1362 setprompt(which)
1363 	int which;
1364 	{
1365 	whichprompt = which;
1366 
1367 #ifndef NO_HISTORY
1368 	if (!el)
1369 #endif
1370 		out2str(getprompt(NULL));
1371 }
1372 
1373 /*
1374  * called by editline -- any expansions to the prompt
1375  *    should be added here.
1376  */
1377 char *
1378 getprompt(unused)
1379 	void *unused;
1380 	{
1381 	switch (whichprompt) {
1382 	case 0:
1383 		return "";
1384 	case 1:
1385 		return ps1val();
1386 	case 2:
1387 		return ps2val();
1388 	default:
1389 		return "<internal prompt error>";
1390 	}
1391 }
1392