Deleted Added
full compact
parser.c (213760) parser.c (213811)
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34#if 0
35static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
36#endif
37#endif /* not lint */
38#include <sys/cdefs.h>
39__FBSDID("$FreeBSD: head/bin/sh/parser.c 213760 2010-10-13 04:01:01Z obrien $");
39__FBSDID("$FreeBSD: head/bin/sh/parser.c 213811 2010-10-13 22:18:03Z obrien $");
40
41#include <stdlib.h>
42#include <unistd.h>
43#include <stdio.h>
44
45#include "shell.h"
46#include "parser.h"
47#include "nodes.h"
48#include "expand.h" /* defines rmescapes() */
49#include "syntax.h"
50#include "options.h"
51#include "input.h"
52#include "output.h"
53#include "var.h"
54#include "error.h"
55#include "memalloc.h"
56#include "mystring.h"
57#include "alias.h"
58#include "show.h"
59#include "eval.h"
60#ifndef NO_HISTORY
61#include "myhistedit.h"
62#endif
63
64/*
65 * Shell command parser.
66 */
67
68#define EOFMARKLEN 79
69#define PROMPTLEN 128
70
71/* values returned by readtoken */
72#include "token.h"
73
74
75
76struct heredoc {
77 struct heredoc *next; /* next here document in list */
78 union node *here; /* redirection node */
79 char *eofmark; /* string indicating end of input */
80 int striptabs; /* if set, strip leading tabs */
81};
82
83struct parser_temp {
84 struct parser_temp *next;
85 void *data;
86};
87
88
89static struct heredoc *heredoclist; /* list of here documents to read */
90static int doprompt; /* if set, prompt the user */
91static int needprompt; /* true if interactive and at start of line */
92static int lasttoken; /* last token read */
93MKINIT int tokpushback; /* last token pushed back */
94static char *wordtext; /* text of last word returned by readtoken */
95MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
96static struct nodelist *backquotelist;
97static union node *redirnode;
98static struct heredoc *heredoc;
99static int quoteflag; /* set if (part of) last token was quoted */
100static int startlinno; /* line # where last token started */
101static int funclinno; /* line # where the current function started */
102static struct parser_temp *parser_temp;
103
104/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
105static int noaliases = 0;
106
107
40
41#include <stdlib.h>
42#include <unistd.h>
43#include <stdio.h>
44
45#include "shell.h"
46#include "parser.h"
47#include "nodes.h"
48#include "expand.h" /* defines rmescapes() */
49#include "syntax.h"
50#include "options.h"
51#include "input.h"
52#include "output.h"
53#include "var.h"
54#include "error.h"
55#include "memalloc.h"
56#include "mystring.h"
57#include "alias.h"
58#include "show.h"
59#include "eval.h"
60#ifndef NO_HISTORY
61#include "myhistedit.h"
62#endif
63
64/*
65 * Shell command parser.
66 */
67
68#define EOFMARKLEN 79
69#define PROMPTLEN 128
70
71/* values returned by readtoken */
72#include "token.h"
73
74
75
76struct heredoc {
77 struct heredoc *next; /* next here document in list */
78 union node *here; /* redirection node */
79 char *eofmark; /* string indicating end of input */
80 int striptabs; /* if set, strip leading tabs */
81};
82
83struct parser_temp {
84 struct parser_temp *next;
85 void *data;
86};
87
88
89static struct heredoc *heredoclist; /* list of here documents to read */
90static int doprompt; /* if set, prompt the user */
91static int needprompt; /* true if interactive and at start of line */
92static int lasttoken; /* last token read */
93MKINIT int tokpushback; /* last token pushed back */
94static char *wordtext; /* text of last word returned by readtoken */
95MKINIT int checkkwd; /* 1 == check for kwds, 2 == also eat newlines */
96static struct nodelist *backquotelist;
97static union node *redirnode;
98static struct heredoc *heredoc;
99static int quoteflag; /* set if (part of) last token was quoted */
100static int startlinno; /* line # where last token started */
101static int funclinno; /* line # where the current function started */
102static struct parser_temp *parser_temp;
103
104/* XXX When 'noaliases' is set to one, no alias expansion takes place. */
105static int noaliases = 0;
106
107
108STATIC union node *list(int);
109STATIC union node *andor(void);
110STATIC union node *pipeline(void);
111STATIC union node *command(void);
112STATIC union node *simplecmd(union node **, union node *);
113STATIC union node *makename(void);
114STATIC void parsefname(void);
115STATIC void parseheredoc(void);
116STATIC int peektoken(void);
117STATIC int readtoken(void);
118STATIC int xxreadtoken(void);
119STATIC int readtoken1(int, char const *, char *, int);
120STATIC int noexpand(char *);
121STATIC void synexpect(int) __dead2;
122STATIC void synerror(const char *) __dead2;
123STATIC void setprompt(int);
108static union node *list(int);
109static union node *andor(void);
110static union node *pipeline(void);
111static union node *command(void);
112static union node *simplecmd(union node **, union node *);
113static union node *makename(void);
114static void parsefname(void);
115static void parseheredoc(void);
116static int peektoken(void);
117static int readtoken(void);
118static int xxreadtoken(void);
119static int readtoken1(int, char const *, char *, int);
120static int noexpand(char *);
121static void synexpect(int) __dead2;
122static void synerror(const char *) __dead2;
123static void setprompt(int);
124
125
124
125
126STATIC void *
126static void *
127parser_temp_alloc(size_t len)
128{
129 struct parser_temp *t;
130
131 INTOFF;
132 t = ckmalloc(sizeof(*t));
133 t->data = NULL;
134 t->next = parser_temp;
135 parser_temp = t;
136 t->data = ckmalloc(len);
137 INTON;
138 return t->data;
139}
140
141
127parser_temp_alloc(size_t len)
128{
129 struct parser_temp *t;
130
131 INTOFF;
132 t = ckmalloc(sizeof(*t));
133 t->data = NULL;
134 t->next = parser_temp;
135 parser_temp = t;
136 t->data = ckmalloc(len);
137 INTON;
138 return t->data;
139}
140
141
142STATIC void *
142static void *
143parser_temp_realloc(void *ptr, size_t len)
144{
145 struct parser_temp *t;
146
147 INTOFF;
148 t = parser_temp;
149 if (ptr != t->data)
150 error("bug: parser_temp_realloc misused");
151 t->data = ckrealloc(t->data, len);
152 INTON;
153 return t->data;
154}
155
156
143parser_temp_realloc(void *ptr, size_t len)
144{
145 struct parser_temp *t;
146
147 INTOFF;
148 t = parser_temp;
149 if (ptr != t->data)
150 error("bug: parser_temp_realloc misused");
151 t->data = ckrealloc(t->data, len);
152 INTON;
153 return t->data;
154}
155
156
157STATIC void
157static void
158parser_temp_free_upto(void *ptr)
159{
160 struct parser_temp *t;
161 int done = 0;
162
163 INTOFF;
164 while (parser_temp != NULL && !done) {
165 t = parser_temp;
166 parser_temp = t->next;
167 done = t->data == ptr;
168 ckfree(t->data);
169 ckfree(t);
170 }
171 INTON;
172 if (!done)
173 error("bug: parser_temp_free_upto misused");
174}
175
176
158parser_temp_free_upto(void *ptr)
159{
160 struct parser_temp *t;
161 int done = 0;
162
163 INTOFF;
164 while (parser_temp != NULL && !done) {
165 t = parser_temp;
166 parser_temp = t->next;
167 done = t->data == ptr;
168 ckfree(t->data);
169 ckfree(t);
170 }
171 INTON;
172 if (!done)
173 error("bug: parser_temp_free_upto misused");
174}
175
176
177STATIC void
177static void
178parser_temp_free_all(void)
179{
180 struct parser_temp *t;
181
182 INTOFF;
183 while (parser_temp != NULL) {
184 t = parser_temp;
185 parser_temp = t->next;
186 ckfree(t->data);
187 ckfree(t);
188 }
189 INTON;
190}
191
192
193/*
194 * Read and parse a command. Returns NEOF on end of file. (NULL is a
195 * valid parse tree indicating a blank line.)
196 */
197
198union node *
199parsecmd(int interact)
200{
201 int t;
202
203 /* This assumes the parser is not re-entered,
204 * which could happen if we add command substitution on PS1/PS2.
205 */
206 parser_temp_free_all();
207 heredoclist = NULL;
208
209 tokpushback = 0;
210 doprompt = interact;
211 if (doprompt)
212 setprompt(1);
213 else
214 setprompt(0);
215 needprompt = 0;
216 t = readtoken();
217 if (t == TEOF)
218 return NEOF;
219 if (t == TNL)
220 return NULL;
221 tokpushback++;
222 return list(1);
223}
224
225
178parser_temp_free_all(void)
179{
180 struct parser_temp *t;
181
182 INTOFF;
183 while (parser_temp != NULL) {
184 t = parser_temp;
185 parser_temp = t->next;
186 ckfree(t->data);
187 ckfree(t);
188 }
189 INTON;
190}
191
192
193/*
194 * Read and parse a command. Returns NEOF on end of file. (NULL is a
195 * valid parse tree indicating a blank line.)
196 */
197
198union node *
199parsecmd(int interact)
200{
201 int t;
202
203 /* This assumes the parser is not re-entered,
204 * which could happen if we add command substitution on PS1/PS2.
205 */
206 parser_temp_free_all();
207 heredoclist = NULL;
208
209 tokpushback = 0;
210 doprompt = interact;
211 if (doprompt)
212 setprompt(1);
213 else
214 setprompt(0);
215 needprompt = 0;
216 t = readtoken();
217 if (t == TEOF)
218 return NEOF;
219 if (t == TNL)
220 return NULL;
221 tokpushback++;
222 return list(1);
223}
224
225
226STATIC union node *
226static union node *
227list(int nlflag)
228{
229 union node *n1, *n2, *n3;
230 int tok;
231
232 checkkwd = 2;
233 if (nlflag == 0 && tokendlist[peektoken()])
234 return NULL;
235 n1 = NULL;
236 for (;;) {
237 n2 = andor();
238 tok = readtoken();
239 if (tok == TBACKGND) {
240 if (n2->type == NCMD || n2->type == NPIPE) {
241 n2->ncmd.backgnd = 1;
242 } else if (n2->type == NREDIR) {
243 n2->type = NBACKGND;
244 } else {
245 n3 = (union node *)stalloc(sizeof (struct nredir));
246 n3->type = NBACKGND;
247 n3->nredir.n = n2;
248 n3->nredir.redirect = NULL;
249 n2 = n3;
250 }
251 }
252 if (n1 == NULL) {
253 n1 = n2;
254 }
255 else {
256 n3 = (union node *)stalloc(sizeof (struct nbinary));
257 n3->type = NSEMI;
258 n3->nbinary.ch1 = n1;
259 n3->nbinary.ch2 = n2;
260 n1 = n3;
261 }
262 switch (tok) {
263 case TBACKGND:
264 case TSEMI:
265 tok = readtoken();
266 /* FALLTHROUGH */
267 case TNL:
268 if (tok == TNL) {
269 parseheredoc();
270 if (nlflag)
271 return n1;
272 } else if (tok == TEOF && nlflag) {
273 parseheredoc();
274 return n1;
275 } else {
276 tokpushback++;
277 }
278 checkkwd = 2;
279 if (tokendlist[peektoken()])
280 return n1;
281 break;
282 case TEOF:
283 if (heredoclist)
284 parseheredoc();
285 else
286 pungetc(); /* push back EOF on input */
287 return n1;
288 default:
289 if (nlflag)
290 synexpect(-1);
291 tokpushback++;
292 return n1;
293 }
294 }
295}
296
297
298
227list(int nlflag)
228{
229 union node *n1, *n2, *n3;
230 int tok;
231
232 checkkwd = 2;
233 if (nlflag == 0 && tokendlist[peektoken()])
234 return NULL;
235 n1 = NULL;
236 for (;;) {
237 n2 = andor();
238 tok = readtoken();
239 if (tok == TBACKGND) {
240 if (n2->type == NCMD || n2->type == NPIPE) {
241 n2->ncmd.backgnd = 1;
242 } else if (n2->type == NREDIR) {
243 n2->type = NBACKGND;
244 } else {
245 n3 = (union node *)stalloc(sizeof (struct nredir));
246 n3->type = NBACKGND;
247 n3->nredir.n = n2;
248 n3->nredir.redirect = NULL;
249 n2 = n3;
250 }
251 }
252 if (n1 == NULL) {
253 n1 = n2;
254 }
255 else {
256 n3 = (union node *)stalloc(sizeof (struct nbinary));
257 n3->type = NSEMI;
258 n3->nbinary.ch1 = n1;
259 n3->nbinary.ch2 = n2;
260 n1 = n3;
261 }
262 switch (tok) {
263 case TBACKGND:
264 case TSEMI:
265 tok = readtoken();
266 /* FALLTHROUGH */
267 case TNL:
268 if (tok == TNL) {
269 parseheredoc();
270 if (nlflag)
271 return n1;
272 } else if (tok == TEOF && nlflag) {
273 parseheredoc();
274 return n1;
275 } else {
276 tokpushback++;
277 }
278 checkkwd = 2;
279 if (tokendlist[peektoken()])
280 return n1;
281 break;
282 case TEOF:
283 if (heredoclist)
284 parseheredoc();
285 else
286 pungetc(); /* push back EOF on input */
287 return n1;
288 default:
289 if (nlflag)
290 synexpect(-1);
291 tokpushback++;
292 return n1;
293 }
294 }
295}
296
297
298
299STATIC union node *
299static union node *
300andor(void)
301{
302 union node *n1, *n2, *n3;
303 int t;
304
305 n1 = pipeline();
306 for (;;) {
307 if ((t = readtoken()) == TAND) {
308 t = NAND;
309 } else if (t == TOR) {
310 t = NOR;
311 } else {
312 tokpushback++;
313 return n1;
314 }
315 n2 = pipeline();
316 n3 = (union node *)stalloc(sizeof (struct nbinary));
317 n3->type = t;
318 n3->nbinary.ch1 = n1;
319 n3->nbinary.ch2 = n2;
320 n1 = n3;
321 }
322}
323
324
325
300andor(void)
301{
302 union node *n1, *n2, *n3;
303 int t;
304
305 n1 = pipeline();
306 for (;;) {
307 if ((t = readtoken()) == TAND) {
308 t = NAND;
309 } else if (t == TOR) {
310 t = NOR;
311 } else {
312 tokpushback++;
313 return n1;
314 }
315 n2 = pipeline();
316 n3 = (union node *)stalloc(sizeof (struct nbinary));
317 n3->type = t;
318 n3->nbinary.ch1 = n1;
319 n3->nbinary.ch2 = n2;
320 n1 = n3;
321 }
322}
323
324
325
326STATIC union node *
326static union node *
327pipeline(void)
328{
329 union node *n1, *n2, *pipenode;
330 struct nodelist *lp, *prev;
331 int negate;
332
333 negate = 0;
334 checkkwd = 2;
335 TRACE(("pipeline: entered\n"));
336 while (readtoken() == TNOT)
337 negate = !negate;
338 tokpushback++;
339 n1 = command();
340 if (readtoken() == TPIPE) {
341 pipenode = (union node *)stalloc(sizeof (struct npipe));
342 pipenode->type = NPIPE;
343 pipenode->npipe.backgnd = 0;
344 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
345 pipenode->npipe.cmdlist = lp;
346 lp->n = n1;
347 do {
348 prev = lp;
349 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
350 lp->n = command();
351 prev->next = lp;
352 } while (readtoken() == TPIPE);
353 lp->next = NULL;
354 n1 = pipenode;
355 }
356 tokpushback++;
357 if (negate) {
358 n2 = (union node *)stalloc(sizeof (struct nnot));
359 n2->type = NNOT;
360 n2->nnot.com = n1;
361 return n2;
362 } else
363 return n1;
364}
365
366
367
327pipeline(void)
328{
329 union node *n1, *n2, *pipenode;
330 struct nodelist *lp, *prev;
331 int negate;
332
333 negate = 0;
334 checkkwd = 2;
335 TRACE(("pipeline: entered\n"));
336 while (readtoken() == TNOT)
337 negate = !negate;
338 tokpushback++;
339 n1 = command();
340 if (readtoken() == TPIPE) {
341 pipenode = (union node *)stalloc(sizeof (struct npipe));
342 pipenode->type = NPIPE;
343 pipenode->npipe.backgnd = 0;
344 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
345 pipenode->npipe.cmdlist = lp;
346 lp->n = n1;
347 do {
348 prev = lp;
349 lp = (struct nodelist *)stalloc(sizeof (struct nodelist));
350 lp->n = command();
351 prev->next = lp;
352 } while (readtoken() == TPIPE);
353 lp->next = NULL;
354 n1 = pipenode;
355 }
356 tokpushback++;
357 if (negate) {
358 n2 = (union node *)stalloc(sizeof (struct nnot));
359 n2->type = NNOT;
360 n2->nnot.com = n1;
361 return n2;
362 } else
363 return n1;
364}
365
366
367
368STATIC union node *
368static union node *
369command(void)
370{
371 union node *n1, *n2;
372 union node *ap, **app;
373 union node *cp, **cpp;
374 union node *redir, **rpp;
375 int t, negate = 0;
376
377 checkkwd = 2;
378 redir = NULL;
379 n1 = NULL;
380 rpp = &redir;
381
382 /* Check for redirection which may precede command */
383 while (readtoken() == TREDIR) {
384 *rpp = n2 = redirnode;
385 rpp = &n2->nfile.next;
386 parsefname();
387 }
388 tokpushback++;
389
390 while (readtoken() == TNOT) {
391 TRACE(("command: TNOT recognized\n"));
392 negate = !negate;
393 }
394 tokpushback++;
395
396 switch (readtoken()) {
397 case TIF:
398 n1 = (union node *)stalloc(sizeof (struct nif));
399 n1->type = NIF;
400 if ((n1->nif.test = list(0)) == NULL)
401 synexpect(-1);
402 if (readtoken() != TTHEN)
403 synexpect(TTHEN);
404 n1->nif.ifpart = list(0);
405 n2 = n1;
406 while (readtoken() == TELIF) {
407 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
408 n2 = n2->nif.elsepart;
409 n2->type = NIF;
410 if ((n2->nif.test = list(0)) == NULL)
411 synexpect(-1);
412 if (readtoken() != TTHEN)
413 synexpect(TTHEN);
414 n2->nif.ifpart = list(0);
415 }
416 if (lasttoken == TELSE)
417 n2->nif.elsepart = list(0);
418 else {
419 n2->nif.elsepart = NULL;
420 tokpushback++;
421 }
422 if (readtoken() != TFI)
423 synexpect(TFI);
424 checkkwd = 1;
425 break;
426 case TWHILE:
427 case TUNTIL: {
428 int got;
429 n1 = (union node *)stalloc(sizeof (struct nbinary));
430 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
431 if ((n1->nbinary.ch1 = list(0)) == NULL)
432 synexpect(-1);
433 if ((got=readtoken()) != TDO) {
434TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
435 synexpect(TDO);
436 }
437 n1->nbinary.ch2 = list(0);
438 if (readtoken() != TDONE)
439 synexpect(TDONE);
440 checkkwd = 1;
441 break;
442 }
443 case TFOR:
444 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
445 synerror("Bad for loop variable");
446 n1 = (union node *)stalloc(sizeof (struct nfor));
447 n1->type = NFOR;
448 n1->nfor.var = wordtext;
449 while (readtoken() == TNL)
450 ;
451 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
452 app = &ap;
453 while (readtoken() == TWORD) {
454 n2 = (union node *)stalloc(sizeof (struct narg));
455 n2->type = NARG;
456 n2->narg.text = wordtext;
457 n2->narg.backquote = backquotelist;
458 *app = n2;
459 app = &n2->narg.next;
460 }
461 *app = NULL;
462 n1->nfor.args = ap;
463 if (lasttoken != TNL && lasttoken != TSEMI)
464 synexpect(-1);
465 } else {
466 static char argvars[5] = {
467 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
468 };
469 n2 = (union node *)stalloc(sizeof (struct narg));
470 n2->type = NARG;
471 n2->narg.text = argvars;
472 n2->narg.backquote = NULL;
473 n2->narg.next = NULL;
474 n1->nfor.args = n2;
475 /*
476 * Newline or semicolon here is optional (but note
477 * that the original Bourne shell only allowed NL).
478 */
479 if (lasttoken != TNL && lasttoken != TSEMI)
480 tokpushback++;
481 }
482 checkkwd = 2;
483 if ((t = readtoken()) == TDO)
484 t = TDONE;
485 else if (t == TBEGIN)
486 t = TEND;
487 else
488 synexpect(-1);
489 n1->nfor.body = list(0);
490 if (readtoken() != t)
491 synexpect(t);
492 checkkwd = 1;
493 break;
494 case TCASE:
495 n1 = (union node *)stalloc(sizeof (struct ncase));
496 n1->type = NCASE;
497 if (readtoken() != TWORD)
498 synexpect(TWORD);
499 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
500 n2->type = NARG;
501 n2->narg.text = wordtext;
502 n2->narg.backquote = backquotelist;
503 n2->narg.next = NULL;
504 while (readtoken() == TNL);
505 if (lasttoken != TWORD || ! equal(wordtext, "in"))
506 synerror("expecting \"in\"");
507 cpp = &n1->ncase.cases;
508 noaliases = 1; /* turn off alias expansion */
509 checkkwd = 2, readtoken();
510 while (lasttoken != TESAC) {
511 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
512 cp->type = NCLIST;
513 app = &cp->nclist.pattern;
514 if (lasttoken == TLP)
515 readtoken();
516 for (;;) {
517 *app = ap = (union node *)stalloc(sizeof (struct narg));
518 ap->type = NARG;
519 ap->narg.text = wordtext;
520 ap->narg.backquote = backquotelist;
521 if (checkkwd = 2, readtoken() != TPIPE)
522 break;
523 app = &ap->narg.next;
524 readtoken();
525 }
526 ap->narg.next = NULL;
527 if (lasttoken != TRP)
528 noaliases = 0, synexpect(TRP);
529 cp->nclist.body = list(0);
530
531 checkkwd = 2;
532 if ((t = readtoken()) != TESAC) {
533 if (t != TENDCASE)
534 noaliases = 0, synexpect(TENDCASE);
535 else
536 checkkwd = 2, readtoken();
537 }
538 cpp = &cp->nclist.next;
539 }
540 noaliases = 0; /* reset alias expansion */
541 *cpp = NULL;
542 checkkwd = 1;
543 break;
544 case TLP:
545 n1 = (union node *)stalloc(sizeof (struct nredir));
546 n1->type = NSUBSHELL;
547 n1->nredir.n = list(0);
548 n1->nredir.redirect = NULL;
549 if (readtoken() != TRP)
550 synexpect(TRP);
551 checkkwd = 1;
552 break;
553 case TBEGIN:
554 n1 = list(0);
555 if (readtoken() != TEND)
556 synexpect(TEND);
557 checkkwd = 1;
558 break;
559 /* Handle an empty command like other simple commands. */
560 case TBACKGND:
561 case TSEMI:
562 case TAND:
563 case TOR:
564 /*
565 * An empty command before a ; doesn't make much sense, and
566 * should certainly be disallowed in the case of `if ;'.
567 */
568 if (!redir)
569 synexpect(-1);
570 case TNL:
571 case TEOF:
572 case TWORD:
573 case TRP:
574 tokpushback++;
575 n1 = simplecmd(rpp, redir);
576 goto checkneg;
577 default:
578 synexpect(-1);
579 }
580
581 /* Now check for redirection which may follow command */
582 while (readtoken() == TREDIR) {
583 *rpp = n2 = redirnode;
584 rpp = &n2->nfile.next;
585 parsefname();
586 }
587 tokpushback++;
588 *rpp = NULL;
589 if (redir) {
590 if (n1->type != NSUBSHELL) {
591 n2 = (union node *)stalloc(sizeof (struct nredir));
592 n2->type = NREDIR;
593 n2->nredir.n = n1;
594 n1 = n2;
595 }
596 n1->nredir.redirect = redir;
597 }
598
599checkneg:
600 if (negate) {
601 n2 = (union node *)stalloc(sizeof (struct nnot));
602 n2->type = NNOT;
603 n2->nnot.com = n1;
604 return n2;
605 }
606 else
607 return n1;
608}
609
610
369command(void)
370{
371 union node *n1, *n2;
372 union node *ap, **app;
373 union node *cp, **cpp;
374 union node *redir, **rpp;
375 int t, negate = 0;
376
377 checkkwd = 2;
378 redir = NULL;
379 n1 = NULL;
380 rpp = &redir;
381
382 /* Check for redirection which may precede command */
383 while (readtoken() == TREDIR) {
384 *rpp = n2 = redirnode;
385 rpp = &n2->nfile.next;
386 parsefname();
387 }
388 tokpushback++;
389
390 while (readtoken() == TNOT) {
391 TRACE(("command: TNOT recognized\n"));
392 negate = !negate;
393 }
394 tokpushback++;
395
396 switch (readtoken()) {
397 case TIF:
398 n1 = (union node *)stalloc(sizeof (struct nif));
399 n1->type = NIF;
400 if ((n1->nif.test = list(0)) == NULL)
401 synexpect(-1);
402 if (readtoken() != TTHEN)
403 synexpect(TTHEN);
404 n1->nif.ifpart = list(0);
405 n2 = n1;
406 while (readtoken() == TELIF) {
407 n2->nif.elsepart = (union node *)stalloc(sizeof (struct nif));
408 n2 = n2->nif.elsepart;
409 n2->type = NIF;
410 if ((n2->nif.test = list(0)) == NULL)
411 synexpect(-1);
412 if (readtoken() != TTHEN)
413 synexpect(TTHEN);
414 n2->nif.ifpart = list(0);
415 }
416 if (lasttoken == TELSE)
417 n2->nif.elsepart = list(0);
418 else {
419 n2->nif.elsepart = NULL;
420 tokpushback++;
421 }
422 if (readtoken() != TFI)
423 synexpect(TFI);
424 checkkwd = 1;
425 break;
426 case TWHILE:
427 case TUNTIL: {
428 int got;
429 n1 = (union node *)stalloc(sizeof (struct nbinary));
430 n1->type = (lasttoken == TWHILE)? NWHILE : NUNTIL;
431 if ((n1->nbinary.ch1 = list(0)) == NULL)
432 synexpect(-1);
433 if ((got=readtoken()) != TDO) {
434TRACE(("expecting DO got %s %s\n", tokname[got], got == TWORD ? wordtext : ""));
435 synexpect(TDO);
436 }
437 n1->nbinary.ch2 = list(0);
438 if (readtoken() != TDONE)
439 synexpect(TDONE);
440 checkkwd = 1;
441 break;
442 }
443 case TFOR:
444 if (readtoken() != TWORD || quoteflag || ! goodname(wordtext))
445 synerror("Bad for loop variable");
446 n1 = (union node *)stalloc(sizeof (struct nfor));
447 n1->type = NFOR;
448 n1->nfor.var = wordtext;
449 while (readtoken() == TNL)
450 ;
451 if (lasttoken == TWORD && ! quoteflag && equal(wordtext, "in")) {
452 app = &ap;
453 while (readtoken() == TWORD) {
454 n2 = (union node *)stalloc(sizeof (struct narg));
455 n2->type = NARG;
456 n2->narg.text = wordtext;
457 n2->narg.backquote = backquotelist;
458 *app = n2;
459 app = &n2->narg.next;
460 }
461 *app = NULL;
462 n1->nfor.args = ap;
463 if (lasttoken != TNL && lasttoken != TSEMI)
464 synexpect(-1);
465 } else {
466 static char argvars[5] = {
467 CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0'
468 };
469 n2 = (union node *)stalloc(sizeof (struct narg));
470 n2->type = NARG;
471 n2->narg.text = argvars;
472 n2->narg.backquote = NULL;
473 n2->narg.next = NULL;
474 n1->nfor.args = n2;
475 /*
476 * Newline or semicolon here is optional (but note
477 * that the original Bourne shell only allowed NL).
478 */
479 if (lasttoken != TNL && lasttoken != TSEMI)
480 tokpushback++;
481 }
482 checkkwd = 2;
483 if ((t = readtoken()) == TDO)
484 t = TDONE;
485 else if (t == TBEGIN)
486 t = TEND;
487 else
488 synexpect(-1);
489 n1->nfor.body = list(0);
490 if (readtoken() != t)
491 synexpect(t);
492 checkkwd = 1;
493 break;
494 case TCASE:
495 n1 = (union node *)stalloc(sizeof (struct ncase));
496 n1->type = NCASE;
497 if (readtoken() != TWORD)
498 synexpect(TWORD);
499 n1->ncase.expr = n2 = (union node *)stalloc(sizeof (struct narg));
500 n2->type = NARG;
501 n2->narg.text = wordtext;
502 n2->narg.backquote = backquotelist;
503 n2->narg.next = NULL;
504 while (readtoken() == TNL);
505 if (lasttoken != TWORD || ! equal(wordtext, "in"))
506 synerror("expecting \"in\"");
507 cpp = &n1->ncase.cases;
508 noaliases = 1; /* turn off alias expansion */
509 checkkwd = 2, readtoken();
510 while (lasttoken != TESAC) {
511 *cpp = cp = (union node *)stalloc(sizeof (struct nclist));
512 cp->type = NCLIST;
513 app = &cp->nclist.pattern;
514 if (lasttoken == TLP)
515 readtoken();
516 for (;;) {
517 *app = ap = (union node *)stalloc(sizeof (struct narg));
518 ap->type = NARG;
519 ap->narg.text = wordtext;
520 ap->narg.backquote = backquotelist;
521 if (checkkwd = 2, readtoken() != TPIPE)
522 break;
523 app = &ap->narg.next;
524 readtoken();
525 }
526 ap->narg.next = NULL;
527 if (lasttoken != TRP)
528 noaliases = 0, synexpect(TRP);
529 cp->nclist.body = list(0);
530
531 checkkwd = 2;
532 if ((t = readtoken()) != TESAC) {
533 if (t != TENDCASE)
534 noaliases = 0, synexpect(TENDCASE);
535 else
536 checkkwd = 2, readtoken();
537 }
538 cpp = &cp->nclist.next;
539 }
540 noaliases = 0; /* reset alias expansion */
541 *cpp = NULL;
542 checkkwd = 1;
543 break;
544 case TLP:
545 n1 = (union node *)stalloc(sizeof (struct nredir));
546 n1->type = NSUBSHELL;
547 n1->nredir.n = list(0);
548 n1->nredir.redirect = NULL;
549 if (readtoken() != TRP)
550 synexpect(TRP);
551 checkkwd = 1;
552 break;
553 case TBEGIN:
554 n1 = list(0);
555 if (readtoken() != TEND)
556 synexpect(TEND);
557 checkkwd = 1;
558 break;
559 /* Handle an empty command like other simple commands. */
560 case TBACKGND:
561 case TSEMI:
562 case TAND:
563 case TOR:
564 /*
565 * An empty command before a ; doesn't make much sense, and
566 * should certainly be disallowed in the case of `if ;'.
567 */
568 if (!redir)
569 synexpect(-1);
570 case TNL:
571 case TEOF:
572 case TWORD:
573 case TRP:
574 tokpushback++;
575 n1 = simplecmd(rpp, redir);
576 goto checkneg;
577 default:
578 synexpect(-1);
579 }
580
581 /* Now check for redirection which may follow command */
582 while (readtoken() == TREDIR) {
583 *rpp = n2 = redirnode;
584 rpp = &n2->nfile.next;
585 parsefname();
586 }
587 tokpushback++;
588 *rpp = NULL;
589 if (redir) {
590 if (n1->type != NSUBSHELL) {
591 n2 = (union node *)stalloc(sizeof (struct nredir));
592 n2->type = NREDIR;
593 n2->nredir.n = n1;
594 n1 = n2;
595 }
596 n1->nredir.redirect = redir;
597 }
598
599checkneg:
600 if (negate) {
601 n2 = (union node *)stalloc(sizeof (struct nnot));
602 n2->type = NNOT;
603 n2->nnot.com = n1;
604 return n2;
605 }
606 else
607 return n1;
608}
609
610
611STATIC union node *
611static union node *
612simplecmd(union node **rpp, union node *redir)
613{
614 union node *args, **app;
615 union node **orig_rpp = rpp;
616 union node *n = NULL;
617
618 /* If we don't have any redirections already, then we must reset */
619 /* rpp to be the address of the local redir variable. */
620 if (redir == 0)
621 rpp = &redir;
622
623 args = NULL;
624 app = &args;
625 /*
626 * We save the incoming value, because we need this for shell
627 * functions. There can not be a redirect or an argument between
628 * the function name and the open parenthesis.
629 */
630 orig_rpp = rpp;
631
632 for (;;) {
633 if (readtoken() == TWORD) {
634 n = (union node *)stalloc(sizeof (struct narg));
635 n->type = NARG;
636 n->narg.text = wordtext;
637 n->narg.backquote = backquotelist;
638 *app = n;
639 app = &n->narg.next;
640 } else if (lasttoken == TREDIR) {
641 *rpp = n = redirnode;
642 rpp = &n->nfile.next;
643 parsefname(); /* read name of redirection file */
644 } else if (lasttoken == TLP && app == &args->narg.next
645 && rpp == orig_rpp) {
646 /* We have a function */
647 if (readtoken() != TRP)
648 synexpect(TRP);
649 funclinno = plinno;
650#ifdef notdef
651 if (! goodname(n->narg.text))
652 synerror("Bad function name");
653#endif
654 n->type = NDEFUN;
655 n->narg.next = command();
656 funclinno = 0;
657 return n;
658 } else {
659 tokpushback++;
660 break;
661 }
662 }
663 *app = NULL;
664 *rpp = NULL;
665 n = (union node *)stalloc(sizeof (struct ncmd));
666 n->type = NCMD;
667 n->ncmd.backgnd = 0;
668 n->ncmd.args = args;
669 n->ncmd.redirect = redir;
670 return n;
671}
672
612simplecmd(union node **rpp, union node *redir)
613{
614 union node *args, **app;
615 union node **orig_rpp = rpp;
616 union node *n = NULL;
617
618 /* If we don't have any redirections already, then we must reset */
619 /* rpp to be the address of the local redir variable. */
620 if (redir == 0)
621 rpp = &redir;
622
623 args = NULL;
624 app = &args;
625 /*
626 * We save the incoming value, because we need this for shell
627 * functions. There can not be a redirect or an argument between
628 * the function name and the open parenthesis.
629 */
630 orig_rpp = rpp;
631
632 for (;;) {
633 if (readtoken() == TWORD) {
634 n = (union node *)stalloc(sizeof (struct narg));
635 n->type = NARG;
636 n->narg.text = wordtext;
637 n->narg.backquote = backquotelist;
638 *app = n;
639 app = &n->narg.next;
640 } else if (lasttoken == TREDIR) {
641 *rpp = n = redirnode;
642 rpp = &n->nfile.next;
643 parsefname(); /* read name of redirection file */
644 } else if (lasttoken == TLP && app == &args->narg.next
645 && rpp == orig_rpp) {
646 /* We have a function */
647 if (readtoken() != TRP)
648 synexpect(TRP);
649 funclinno = plinno;
650#ifdef notdef
651 if (! goodname(n->narg.text))
652 synerror("Bad function name");
653#endif
654 n->type = NDEFUN;
655 n->narg.next = command();
656 funclinno = 0;
657 return n;
658 } else {
659 tokpushback++;
660 break;
661 }
662 }
663 *app = NULL;
664 *rpp = NULL;
665 n = (union node *)stalloc(sizeof (struct ncmd));
666 n->type = NCMD;
667 n->ncmd.backgnd = 0;
668 n->ncmd.args = args;
669 n->ncmd.redirect = redir;
670 return n;
671}
672
673STATIC union node *
673static union node *
674makename(void)
675{
676 union node *n;
677
678 n = (union node *)stalloc(sizeof (struct narg));
679 n->type = NARG;
680 n->narg.next = NULL;
681 n->narg.text = wordtext;
682 n->narg.backquote = backquotelist;
683 return n;
684}
685
686void
687fixredir(union node *n, const char *text, int err)
688{
689 TRACE(("Fix redir %s %d\n", text, err));
690 if (!err)
691 n->ndup.vname = NULL;
692
693 if (is_digit(text[0]) && text[1] == '\0')
694 n->ndup.dupfd = digit_val(text[0]);
695 else if (text[0] == '-' && text[1] == '\0')
696 n->ndup.dupfd = -1;
697 else {
698
699 if (err)
700 synerror("Bad fd number");
701 else
702 n->ndup.vname = makename();
703 }
704}
705
706
674makename(void)
675{
676 union node *n;
677
678 n = (union node *)stalloc(sizeof (struct narg));
679 n->type = NARG;
680 n->narg.next = NULL;
681 n->narg.text = wordtext;
682 n->narg.backquote = backquotelist;
683 return n;
684}
685
686void
687fixredir(union node *n, const char *text, int err)
688{
689 TRACE(("Fix redir %s %d\n", text, err));
690 if (!err)
691 n->ndup.vname = NULL;
692
693 if (is_digit(text[0]) && text[1] == '\0')
694 n->ndup.dupfd = digit_val(text[0]);
695 else if (text[0] == '-' && text[1] == '\0')
696 n->ndup.dupfd = -1;
697 else {
698
699 if (err)
700 synerror("Bad fd number");
701 else
702 n->ndup.vname = makename();
703 }
704}
705
706
707STATIC void
707static void
708parsefname(void)
709{
710 union node *n = redirnode;
711
712 if (readtoken() != TWORD)
713 synexpect(-1);
714 if (n->type == NHERE) {
715 struct heredoc *here = heredoc;
716 struct heredoc *p;
717 int i;
718
719 if (quoteflag == 0)
720 n->type = NXHERE;
721 TRACE(("Here document %d\n", n->type));
722 if (here->striptabs) {
723 while (*wordtext == '\t')
724 wordtext++;
725 }
726 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
727 synerror("Illegal eof marker for << redirection");
728 rmescapes(wordtext);
729 here->eofmark = wordtext;
730 here->next = NULL;
731 if (heredoclist == NULL)
732 heredoclist = here;
733 else {
734 for (p = heredoclist ; p->next ; p = p->next);
735 p->next = here;
736 }
737 } else if (n->type == NTOFD || n->type == NFROMFD) {
738 fixredir(n, wordtext, 0);
739 } else {
740 n->nfile.fname = makename();
741 }
742}
743
744
745/*
746 * Input any here documents.
747 */
748
708parsefname(void)
709{
710 union node *n = redirnode;
711
712 if (readtoken() != TWORD)
713 synexpect(-1);
714 if (n->type == NHERE) {
715 struct heredoc *here = heredoc;
716 struct heredoc *p;
717 int i;
718
719 if (quoteflag == 0)
720 n->type = NXHERE;
721 TRACE(("Here document %d\n", n->type));
722 if (here->striptabs) {
723 while (*wordtext == '\t')
724 wordtext++;
725 }
726 if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
727 synerror("Illegal eof marker for << redirection");
728 rmescapes(wordtext);
729 here->eofmark = wordtext;
730 here->next = NULL;
731 if (heredoclist == NULL)
732 heredoclist = here;
733 else {
734 for (p = heredoclist ; p->next ; p = p->next);
735 p->next = here;
736 }
737 } else if (n->type == NTOFD || n->type == NFROMFD) {
738 fixredir(n, wordtext, 0);
739 } else {
740 n->nfile.fname = makename();
741 }
742}
743
744
745/*
746 * Input any here documents.
747 */
748
749STATIC void
749static void
750parseheredoc(void)
751{
752 struct heredoc *here;
753 union node *n;
754
755 while (heredoclist) {
756 here = heredoclist;
757 heredoclist = here->next;
758 if (needprompt) {
759 setprompt(2);
760 needprompt = 0;
761 }
762 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
763 here->eofmark, here->striptabs);
764 n = (union node *)stalloc(sizeof (struct narg));
765 n->narg.type = NARG;
766 n->narg.next = NULL;
767 n->narg.text = wordtext;
768 n->narg.backquote = backquotelist;
769 here->here->nhere.doc = n;
770 }
771}
772
750parseheredoc(void)
751{
752 struct heredoc *here;
753 union node *n;
754
755 while (heredoclist) {
756 here = heredoclist;
757 heredoclist = here->next;
758 if (needprompt) {
759 setprompt(2);
760 needprompt = 0;
761 }
762 readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX,
763 here->eofmark, here->striptabs);
764 n = (union node *)stalloc(sizeof (struct narg));
765 n->narg.type = NARG;
766 n->narg.next = NULL;
767 n->narg.text = wordtext;
768 n->narg.backquote = backquotelist;
769 here->here->nhere.doc = n;
770 }
771}
772
773STATIC int
773static int
774peektoken(void)
775{
776 int t;
777
778 t = readtoken();
779 tokpushback++;
780 return (t);
781}
782
774peektoken(void)
775{
776 int t;
777
778 t = readtoken();
779 tokpushback++;
780 return (t);
781}
782
783STATIC int
783static int
784readtoken(void)
785{
786 int t;
787 int savecheckkwd = checkkwd;
788 struct alias *ap;
789#ifdef DEBUG
790 int alreadyseen = tokpushback;
791#endif
792
793 top:
794 t = xxreadtoken();
795
796 if (checkkwd) {
797 /*
798 * eat newlines
799 */
800 if (checkkwd == 2) {
801 checkkwd = 0;
802 while (t == TNL) {
803 parseheredoc();
804 t = xxreadtoken();
805 }
806 } else
807 checkkwd = 0;
808 /*
809 * check for keywords and aliases
810 */
811 if (t == TWORD && !quoteflag)
812 {
813 const char * const *pp;
814
815 for (pp = parsekwd; *pp; pp++) {
816 if (**pp == *wordtext && equal(*pp, wordtext))
817 {
818 lasttoken = t = pp - parsekwd + KWDOFFSET;
819 TRACE(("keyword %s recognized\n", tokname[t]));
820 goto out;
821 }
822 }
823 if (noaliases == 0 &&
824 (ap = lookupalias(wordtext, 1)) != NULL) {
825 pushstring(ap->val, strlen(ap->val), ap);
826 checkkwd = savecheckkwd;
827 goto top;
828 }
829 }
830out:
831 checkkwd = (t == TNOT) ? savecheckkwd : 0;
832 }
833#ifdef DEBUG
834 if (!alreadyseen)
835 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
836 else
837 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
838#endif
839 return (t);
840}
841
842
843/*
844 * Read the next input token.
845 * If the token is a word, we set backquotelist to the list of cmds in
846 * backquotes. We set quoteflag to true if any part of the word was
847 * quoted.
848 * If the token is TREDIR, then we set redirnode to a structure containing
849 * the redirection.
850 * In all cases, the variable startlinno is set to the number of the line
851 * on which the token starts.
852 *
853 * [Change comment: here documents and internal procedures]
854 * [Readtoken shouldn't have any arguments. Perhaps we should make the
855 * word parsing code into a separate routine. In this case, readtoken
856 * doesn't need to have any internal procedures, but parseword does.
857 * We could also make parseoperator in essence the main routine, and
858 * have parseword (readtoken1?) handle both words and redirection.]
859 */
860
861#define RETURN(token) return lasttoken = token
862
784readtoken(void)
785{
786 int t;
787 int savecheckkwd = checkkwd;
788 struct alias *ap;
789#ifdef DEBUG
790 int alreadyseen = tokpushback;
791#endif
792
793 top:
794 t = xxreadtoken();
795
796 if (checkkwd) {
797 /*
798 * eat newlines
799 */
800 if (checkkwd == 2) {
801 checkkwd = 0;
802 while (t == TNL) {
803 parseheredoc();
804 t = xxreadtoken();
805 }
806 } else
807 checkkwd = 0;
808 /*
809 * check for keywords and aliases
810 */
811 if (t == TWORD && !quoteflag)
812 {
813 const char * const *pp;
814
815 for (pp = parsekwd; *pp; pp++) {
816 if (**pp == *wordtext && equal(*pp, wordtext))
817 {
818 lasttoken = t = pp - parsekwd + KWDOFFSET;
819 TRACE(("keyword %s recognized\n", tokname[t]));
820 goto out;
821 }
822 }
823 if (noaliases == 0 &&
824 (ap = lookupalias(wordtext, 1)) != NULL) {
825 pushstring(ap->val, strlen(ap->val), ap);
826 checkkwd = savecheckkwd;
827 goto top;
828 }
829 }
830out:
831 checkkwd = (t == TNOT) ? savecheckkwd : 0;
832 }
833#ifdef DEBUG
834 if (!alreadyseen)
835 TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
836 else
837 TRACE(("reread token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
838#endif
839 return (t);
840}
841
842
843/*
844 * Read the next input token.
845 * If the token is a word, we set backquotelist to the list of cmds in
846 * backquotes. We set quoteflag to true if any part of the word was
847 * quoted.
848 * If the token is TREDIR, then we set redirnode to a structure containing
849 * the redirection.
850 * In all cases, the variable startlinno is set to the number of the line
851 * on which the token starts.
852 *
853 * [Change comment: here documents and internal procedures]
854 * [Readtoken shouldn't have any arguments. Perhaps we should make the
855 * word parsing code into a separate routine. In this case, readtoken
856 * doesn't need to have any internal procedures, but parseword does.
857 * We could also make parseoperator in essence the main routine, and
858 * have parseword (readtoken1?) handle both words and redirection.]
859 */
860
861#define RETURN(token) return lasttoken = token
862
863STATIC int
863static int
864xxreadtoken(void)
865{
866 int c;
867
868 if (tokpushback) {
869 tokpushback = 0;
870 return lasttoken;
871 }
872 if (needprompt) {
873 setprompt(2);
874 needprompt = 0;
875 }
876 startlinno = plinno;
877 for (;;) { /* until token or start of word found */
878 c = pgetc_macro();
879 if (c == ' ' || c == '\t')
880 continue; /* quick check for white space first */
881 switch (c) {
882 case ' ': case '\t':
883 continue;
884 case '#':
885 while ((c = pgetc()) != '\n' && c != PEOF);
886 pungetc();
887 continue;
888 case '\\':
889 if (pgetc() == '\n') {
890 startlinno = ++plinno;
891 if (doprompt)
892 setprompt(2);
893 else
894 setprompt(0);
895 continue;
896 }
897 pungetc();
898 goto breakloop;
899 case '\n':
900 plinno++;
901 needprompt = doprompt;
902 RETURN(TNL);
903 case PEOF:
904 RETURN(TEOF);
905 case '&':
906 if (pgetc() == '&')
907 RETURN(TAND);
908 pungetc();
909 RETURN(TBACKGND);
910 case '|':
911 if (pgetc() == '|')
912 RETURN(TOR);
913 pungetc();
914 RETURN(TPIPE);
915 case ';':
916 if (pgetc() == ';')
917 RETURN(TENDCASE);
918 pungetc();
919 RETURN(TSEMI);
920 case '(':
921 RETURN(TLP);
922 case ')':
923 RETURN(TRP);
924 default:
925 goto breakloop;
926 }
927 }
928breakloop:
929 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
930#undef RETURN
931}
932
933
864xxreadtoken(void)
865{
866 int c;
867
868 if (tokpushback) {
869 tokpushback = 0;
870 return lasttoken;
871 }
872 if (needprompt) {
873 setprompt(2);
874 needprompt = 0;
875 }
876 startlinno = plinno;
877 for (;;) { /* until token or start of word found */
878 c = pgetc_macro();
879 if (c == ' ' || c == '\t')
880 continue; /* quick check for white space first */
881 switch (c) {
882 case ' ': case '\t':
883 continue;
884 case '#':
885 while ((c = pgetc()) != '\n' && c != PEOF);
886 pungetc();
887 continue;
888 case '\\':
889 if (pgetc() == '\n') {
890 startlinno = ++plinno;
891 if (doprompt)
892 setprompt(2);
893 else
894 setprompt(0);
895 continue;
896 }
897 pungetc();
898 goto breakloop;
899 case '\n':
900 plinno++;
901 needprompt = doprompt;
902 RETURN(TNL);
903 case PEOF:
904 RETURN(TEOF);
905 case '&':
906 if (pgetc() == '&')
907 RETURN(TAND);
908 pungetc();
909 RETURN(TBACKGND);
910 case '|':
911 if (pgetc() == '|')
912 RETURN(TOR);
913 pungetc();
914 RETURN(TPIPE);
915 case ';':
916 if (pgetc() == ';')
917 RETURN(TENDCASE);
918 pungetc();
919 RETURN(TSEMI);
920 case '(':
921 RETURN(TLP);
922 case ')':
923 RETURN(TRP);
924 default:
925 goto breakloop;
926 }
927 }
928breakloop:
929 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
930#undef RETURN
931}
932
933
934#define MAXNEST_STATIC 8
934#define MAXNEST_static 8
935struct tokenstate
936{
937 const char *syntax; /* *SYNTAX */
938 int parenlevel; /* levels of parentheses in arithmetic */
939 enum tokenstate_category
940 {
941 TSTATE_TOP,
942 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
943 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
944 TSTATE_ARITH
945 } category;
946};
947
948
949/*
950 * Called to parse command substitutions.
951 */
952
935struct tokenstate
936{
937 const char *syntax; /* *SYNTAX */
938 int parenlevel; /* levels of parentheses in arithmetic */
939 enum tokenstate_category
940 {
941 TSTATE_TOP,
942 TSTATE_VAR_OLD, /* ${var+-=?}, inherits dquotes */
943 TSTATE_VAR_NEW, /* other ${var...}, own dquote state */
944 TSTATE_ARITH
945 } category;
946};
947
948
949/*
950 * Called to parse command substitutions.
951 */
952
953STATIC char *
953static char *
954parsebackq(char *out, struct nodelist **pbqlist,
955 int oldstyle, int dblquote, int quoted)
956{
957 struct nodelist **nlpp;
958 union node *n;
959 char *volatile str;
960 struct jmploc jmploc;
961 struct jmploc *const savehandler = handler;
962 int savelen;
963 int saveprompt;
964 const int bq_startlinno = plinno;
965 char *volatile ostr = NULL;
966 struct parsefile *const savetopfile = getcurrentfile();
967 struct heredoc *const saveheredoclist = heredoclist;
968 struct heredoc *here;
969
970 str = NULL;
971 if (setjmp(jmploc.loc)) {
972 popfilesupto(savetopfile);
973 if (str)
974 ckfree(str);
975 if (ostr)
976 ckfree(ostr);
977 heredoclist = saveheredoclist;
978 handler = savehandler;
979 if (exception == EXERROR) {
980 startlinno = bq_startlinno;
981 synerror("Error in command substitution");
982 }
983 longjmp(handler->loc, 1);
984 }
985 INTOFF;
986 savelen = out - stackblock();
987 if (savelen > 0) {
988 str = ckmalloc(savelen);
989 memcpy(str, stackblock(), savelen);
990 }
991 handler = &jmploc;
992 heredoclist = NULL;
993 INTON;
994 if (oldstyle) {
995 /* We must read until the closing backquote, giving special
996 treatment to some slashes, and then push the string and
997 reread it as input, interpreting it normally. */
998 char *oout;
999 int c;
1000 int olen;
1001
1002
1003 STARTSTACKSTR(oout);
1004 for (;;) {
1005 if (needprompt) {
1006 setprompt(2);
1007 needprompt = 0;
1008 }
1009 switch (c = pgetc()) {
1010 case '`':
1011 goto done;
1012
1013 case '\\':
1014 if ((c = pgetc()) == '\n') {
1015 plinno++;
1016 if (doprompt)
1017 setprompt(2);
1018 else
1019 setprompt(0);
1020 /*
1021 * If eating a newline, avoid putting
1022 * the newline into the new character
1023 * stream (via the STPUTC after the
1024 * switch).
1025 */
1026 continue;
1027 }
1028 if (c != '\\' && c != '`' && c != '$'
1029 && (!dblquote || c != '"'))
1030 STPUTC('\\', oout);
1031 break;
1032
1033 case '\n':
1034 plinno++;
1035 needprompt = doprompt;
1036 break;
1037
1038 case PEOF:
1039 startlinno = plinno;
1040 synerror("EOF in backquote substitution");
1041 break;
1042
1043 default:
1044 break;
1045 }
1046 STPUTC(c, oout);
1047 }
1048done:
1049 STPUTC('\0', oout);
1050 olen = oout - stackblock();
1051 INTOFF;
1052 ostr = ckmalloc(olen);
1053 memcpy(ostr, stackblock(), olen);
1054 setinputstring(ostr, 1);
1055 INTON;
1056 }
1057 nlpp = pbqlist;
1058 while (*nlpp)
1059 nlpp = &(*nlpp)->next;
1060 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1061 (*nlpp)->next = NULL;
1062
1063 if (oldstyle) {
1064 saveprompt = doprompt;
1065 doprompt = 0;
1066 }
1067
1068 n = list(0);
1069
1070 if (oldstyle)
1071 doprompt = saveprompt;
1072 else {
1073 if (readtoken() != TRP)
1074 synexpect(TRP);
1075 }
1076
1077 (*nlpp)->n = n;
1078 if (oldstyle) {
1079 /*
1080 * Start reading from old file again, ignoring any pushed back
1081 * tokens left from the backquote parsing
1082 */
1083 popfile();
1084 tokpushback = 0;
1085 }
1086 while (stackblocksize() <= savelen)
1087 growstackblock();
1088 STARTSTACKSTR(out);
1089 INTOFF;
1090 if (str) {
1091 memcpy(out, str, savelen);
1092 STADJUST(savelen, out);
1093 ckfree(str);
1094 str = NULL;
1095 }
1096 if (ostr) {
1097 ckfree(ostr);
1098 ostr = NULL;
1099 }
1100 here = saveheredoclist;
1101 if (here != NULL) {
1102 while (here->next != NULL)
1103 here = here->next;
1104 here->next = heredoclist;
1105 heredoclist = saveheredoclist;
1106 }
1107 handler = savehandler;
1108 INTON;
1109 if (quoted)
1110 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1111 else
1112 USTPUTC(CTLBACKQ, out);
1113 return out;
1114}
1115
1116
1117/*
1118 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
1119 * is not NULL, read a here document. In the latter case, eofmark is the
1120 * word which marks the end of the document and striptabs is true if
1121 * leading tabs should be stripped from the document. The argument firstc
1122 * is the first character of the input token or document.
1123 *
1124 * Because C does not have internal subroutines, I have simulated them
1125 * using goto's to implement the subroutine linkage. The following macros
1126 * will run code that appears at the end of readtoken1.
1127 */
1128
1129#define CHECKEND() {goto checkend; checkend_return:;}
1130#define PARSEREDIR() {goto parseredir; parseredir_return:;}
1131#define PARSESUB() {goto parsesub; parsesub_return:;}
1132#define PARSEARITH() {goto parsearith; parsearith_return:;}
1133
954parsebackq(char *out, struct nodelist **pbqlist,
955 int oldstyle, int dblquote, int quoted)
956{
957 struct nodelist **nlpp;
958 union node *n;
959 char *volatile str;
960 struct jmploc jmploc;
961 struct jmploc *const savehandler = handler;
962 int savelen;
963 int saveprompt;
964 const int bq_startlinno = plinno;
965 char *volatile ostr = NULL;
966 struct parsefile *const savetopfile = getcurrentfile();
967 struct heredoc *const saveheredoclist = heredoclist;
968 struct heredoc *here;
969
970 str = NULL;
971 if (setjmp(jmploc.loc)) {
972 popfilesupto(savetopfile);
973 if (str)
974 ckfree(str);
975 if (ostr)
976 ckfree(ostr);
977 heredoclist = saveheredoclist;
978 handler = savehandler;
979 if (exception == EXERROR) {
980 startlinno = bq_startlinno;
981 synerror("Error in command substitution");
982 }
983 longjmp(handler->loc, 1);
984 }
985 INTOFF;
986 savelen = out - stackblock();
987 if (savelen > 0) {
988 str = ckmalloc(savelen);
989 memcpy(str, stackblock(), savelen);
990 }
991 handler = &jmploc;
992 heredoclist = NULL;
993 INTON;
994 if (oldstyle) {
995 /* We must read until the closing backquote, giving special
996 treatment to some slashes, and then push the string and
997 reread it as input, interpreting it normally. */
998 char *oout;
999 int c;
1000 int olen;
1001
1002
1003 STARTSTACKSTR(oout);
1004 for (;;) {
1005 if (needprompt) {
1006 setprompt(2);
1007 needprompt = 0;
1008 }
1009 switch (c = pgetc()) {
1010 case '`':
1011 goto done;
1012
1013 case '\\':
1014 if ((c = pgetc()) == '\n') {
1015 plinno++;
1016 if (doprompt)
1017 setprompt(2);
1018 else
1019 setprompt(0);
1020 /*
1021 * If eating a newline, avoid putting
1022 * the newline into the new character
1023 * stream (via the STPUTC after the
1024 * switch).
1025 */
1026 continue;
1027 }
1028 if (c != '\\' && c != '`' && c != '$'
1029 && (!dblquote || c != '"'))
1030 STPUTC('\\', oout);
1031 break;
1032
1033 case '\n':
1034 plinno++;
1035 needprompt = doprompt;
1036 break;
1037
1038 case PEOF:
1039 startlinno = plinno;
1040 synerror("EOF in backquote substitution");
1041 break;
1042
1043 default:
1044 break;
1045 }
1046 STPUTC(c, oout);
1047 }
1048done:
1049 STPUTC('\0', oout);
1050 olen = oout - stackblock();
1051 INTOFF;
1052 ostr = ckmalloc(olen);
1053 memcpy(ostr, stackblock(), olen);
1054 setinputstring(ostr, 1);
1055 INTON;
1056 }
1057 nlpp = pbqlist;
1058 while (*nlpp)
1059 nlpp = &(*nlpp)->next;
1060 *nlpp = (struct nodelist *)stalloc(sizeof (struct nodelist));
1061 (*nlpp)->next = NULL;
1062
1063 if (oldstyle) {
1064 saveprompt = doprompt;
1065 doprompt = 0;
1066 }
1067
1068 n = list(0);
1069
1070 if (oldstyle)
1071 doprompt = saveprompt;
1072 else {
1073 if (readtoken() != TRP)
1074 synexpect(TRP);
1075 }
1076
1077 (*nlpp)->n = n;
1078 if (oldstyle) {
1079 /*
1080 * Start reading from old file again, ignoring any pushed back
1081 * tokens left from the backquote parsing
1082 */
1083 popfile();
1084 tokpushback = 0;
1085 }
1086 while (stackblocksize() <= savelen)
1087 growstackblock();
1088 STARTSTACKSTR(out);
1089 INTOFF;
1090 if (str) {
1091 memcpy(out, str, savelen);
1092 STADJUST(savelen, out);
1093 ckfree(str);
1094 str = NULL;
1095 }
1096 if (ostr) {
1097 ckfree(ostr);
1098 ostr = NULL;
1099 }
1100 here = saveheredoclist;
1101 if (here != NULL) {
1102 while (here->next != NULL)
1103 here = here->next;
1104 here->next = heredoclist;
1105 heredoclist = saveheredoclist;
1106 }
1107 handler = savehandler;
1108 INTON;
1109 if (quoted)
1110 USTPUTC(CTLBACKQ | CTLQUOTE, out);
1111 else
1112 USTPUTC(CTLBACKQ, out);
1113 return out;
1114}
1115
1116
1117/*
1118 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
1119 * is not NULL, read a here document. In the latter case, eofmark is the
1120 * word which marks the end of the document and striptabs is true if
1121 * leading tabs should be stripped from the document. The argument firstc
1122 * is the first character of the input token or document.
1123 *
1124 * Because C does not have internal subroutines, I have simulated them
1125 * using goto's to implement the subroutine linkage. The following macros
1126 * will run code that appears at the end of readtoken1.
1127 */
1128
1129#define CHECKEND() {goto checkend; checkend_return:;}
1130#define PARSEREDIR() {goto parseredir; parseredir_return:;}
1131#define PARSESUB() {goto parsesub; parsesub_return:;}
1132#define PARSEARITH() {goto parsearith; parsearith_return:;}
1133
1134STATIC int
1134static int
1135readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1136{
1137 int c = firstc;
1138 char *out;
1139 int len;
1140 char line[EOFMARKLEN + 1];
1141 struct nodelist *bqlist;
1142 int quotef;
1143 int newvarnest;
1144 int level;
1145 int synentry;
1135readtoken1(int firstc, char const *initialsyntax, char *eofmark, int striptabs)
1136{
1137 int c = firstc;
1138 char *out;
1139 int len;
1140 char line[EOFMARKLEN + 1];
1141 struct nodelist *bqlist;
1142 int quotef;
1143 int newvarnest;
1144 int level;
1145 int synentry;
1146 struct tokenstate state_static[MAXNEST_STATIC];
1147 int maxnest = MAXNEST_STATIC;
1146 struct tokenstate state_static[MAXNEST_static];
1147 int maxnest = MAXNEST_static;
1148 struct tokenstate *state = state_static;
1149
1150 startlinno = plinno;
1151 quotef = 0;
1152 bqlist = NULL;
1153 newvarnest = 0;
1154 level = 0;
1155 state[level].syntax = initialsyntax;
1156 state[level].parenlevel = 0;
1157 state[level].category = TSTATE_TOP;
1158
1159 STARTSTACKSTR(out);
1160 loop: { /* for each line, until end of word */
1161 CHECKEND(); /* set c to PEOF if at end of here document */
1162 for (;;) { /* until end of line or end of word */
1163 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
1164
1165 synentry = state[level].syntax[c];
1166
1167 switch(synentry) {
1168 case CNL: /* '\n' */
1169 if (state[level].syntax == BASESYNTAX)
1170 goto endword; /* exit outer loop */
1171 USTPUTC(c, out);
1172 plinno++;
1173 if (doprompt)
1174 setprompt(2);
1175 else
1176 setprompt(0);
1177 c = pgetc();
1178 goto loop; /* continue outer loop */
1179 case CWORD:
1180 USTPUTC(c, out);
1181 break;
1182 case CCTL:
1183 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1184 USTPUTC(CTLESC, out);
1185 USTPUTC(c, out);
1186 break;
1187 case CBACK: /* backslash */
1188 c = pgetc();
1189 if (c == PEOF) {
1190 USTPUTC('\\', out);
1191 pungetc();
1192 } else if (c == '\n') {
1193 plinno++;
1194 if (doprompt)
1195 setprompt(2);
1196 else
1197 setprompt(0);
1198 } else {
1199 if (state[level].syntax == DQSYNTAX &&
1200 c != '\\' && c != '`' && c != '$' &&
1201 (c != '"' || (eofmark != NULL &&
1202 newvarnest == 0)) &&
1203 (c != '}' || state[level].category != TSTATE_VAR_OLD))
1204 USTPUTC('\\', out);
1205 if (SQSYNTAX[c] == CCTL)
1206 USTPUTC(CTLESC, out);
1207 else if (eofmark == NULL ||
1208 newvarnest > 0)
1209 USTPUTC(CTLQUOTEMARK, out);
1210 USTPUTC(c, out);
1211 quotef++;
1212 }
1213 break;
1214 case CSQUOTE:
1215 USTPUTC(CTLQUOTEMARK, out);
1216 state[level].syntax = SQSYNTAX;
1217 break;
1218 case CDQUOTE:
1219 USTPUTC(CTLQUOTEMARK, out);
1220 state[level].syntax = DQSYNTAX;
1221 break;
1222 case CENDQUOTE:
1223 if (eofmark != NULL && newvarnest == 0)
1224 USTPUTC(c, out);
1225 else {
1226 if (state[level].category == TSTATE_ARITH)
1227 state[level].syntax = ARISYNTAX;
1228 else
1229 state[level].syntax = BASESYNTAX;
1230 quotef++;
1231 }
1232 break;
1233 case CVAR: /* '$' */
1234 PARSESUB(); /* parse substitution */
1235 break;
1236 case CENDVAR: /* '}' */
1237 if (level > 0 &&
1238 (state[level].category == TSTATE_VAR_OLD ||
1239 state[level].category == TSTATE_VAR_NEW)) {
1240 if (state[level].category == TSTATE_VAR_OLD)
1241 state[level - 1].syntax = state[level].syntax;
1242 else
1243 newvarnest--;
1244 level--;
1245 USTPUTC(CTLENDVAR, out);
1246 } else {
1247 USTPUTC(c, out);
1248 }
1249 break;
1250 case CLP: /* '(' in arithmetic */
1251 state[level].parenlevel++;
1252 USTPUTC(c, out);
1253 break;
1254 case CRP: /* ')' in arithmetic */
1255 if (state[level].parenlevel > 0) {
1256 USTPUTC(c, out);
1257 --state[level].parenlevel;
1258 } else {
1259 if (pgetc() == ')') {
1260 if (level > 0 &&
1261 state[level].category == TSTATE_ARITH) {
1262 level--;
1263 USTPUTC(CTLENDARI, out);
1264 } else
1265 USTPUTC(')', out);
1266 } else {
1267 /*
1268 * unbalanced parens
1269 * (don't 2nd guess - no error)
1270 */
1271 pungetc();
1272 USTPUTC(')', out);
1273 }
1274 }
1275 break;
1276 case CBQUOTE: /* '`' */
1277 out = parsebackq(out, &bqlist, 1,
1278 state[level].syntax == DQSYNTAX &&
1279 (eofmark == NULL || newvarnest > 0),
1280 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1281 break;
1282 case CEOF:
1283 goto endword; /* exit outer loop */
1284 default:
1285 if (level == 0)
1286 goto endword; /* exit outer loop */
1287 USTPUTC(c, out);
1288 }
1289 c = pgetc_macro();
1290 }
1291 }
1292endword:
1293 if (state[level].syntax == ARISYNTAX)
1294 synerror("Missing '))'");
1295 if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1296 synerror("Unterminated quoted string");
1297 if (state[level].category == TSTATE_VAR_OLD ||
1298 state[level].category == TSTATE_VAR_NEW) {
1299 startlinno = plinno;
1300 synerror("Missing '}'");
1301 }
1302 if (state != state_static)
1303 parser_temp_free_upto(state);
1304 USTPUTC('\0', out);
1305 len = out - stackblock();
1306 out = stackblock();
1307 if (eofmark == NULL) {
1308 if ((c == '>' || c == '<')
1309 && quotef == 0
1310 && len <= 2
1311 && (*out == '\0' || is_digit(*out))) {
1312 PARSEREDIR();
1313 return lasttoken = TREDIR;
1314 } else {
1315 pungetc();
1316 }
1317 }
1318 quoteflag = quotef;
1319 backquotelist = bqlist;
1320 grabstackblock(len);
1321 wordtext = out;
1322 return lasttoken = TWORD;
1323/* end of readtoken routine */
1324
1325
1326/*
1327 * Check to see whether we are at the end of the here document. When this
1328 * is called, c is set to the first character of the next input line. If
1329 * we are at the end of the here document, this routine sets the c to PEOF.
1330 */
1331
1332checkend: {
1333 if (eofmark) {
1334 if (striptabs) {
1335 while (c == '\t')
1336 c = pgetc();
1337 }
1338 if (c == *eofmark) {
1339 if (pfgets(line, sizeof line) != NULL) {
1340 char *p, *q;
1341
1342 p = line;
1343 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1344 if (*p == '\n' && *q == '\0') {
1345 c = PEOF;
1346 plinno++;
1347 needprompt = doprompt;
1348 } else {
1349 pushstring(line, strlen(line), NULL);
1350 }
1351 }
1352 }
1353 }
1354 goto checkend_return;
1355}
1356
1357
1358/*
1359 * Parse a redirection operator. The variable "out" points to a string
1360 * specifying the fd to be redirected. The variable "c" contains the
1361 * first character of the redirection operator.
1362 */
1363
1364parseredir: {
1365 char fd = *out;
1366 union node *np;
1367
1368 np = (union node *)stalloc(sizeof (struct nfile));
1369 if (c == '>') {
1370 np->nfile.fd = 1;
1371 c = pgetc();
1372 if (c == '>')
1373 np->type = NAPPEND;
1374 else if (c == '&')
1375 np->type = NTOFD;
1376 else if (c == '|')
1377 np->type = NCLOBBER;
1378 else {
1379 np->type = NTO;
1380 pungetc();
1381 }
1382 } else { /* c == '<' */
1383 np->nfile.fd = 0;
1384 c = pgetc();
1385 if (c == '<') {
1386 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1387 np = (union node *)stalloc(sizeof (struct nhere));
1388 np->nfile.fd = 0;
1389 }
1390 np->type = NHERE;
1391 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1392 heredoc->here = np;
1393 if ((c = pgetc()) == '-') {
1394 heredoc->striptabs = 1;
1395 } else {
1396 heredoc->striptabs = 0;
1397 pungetc();
1398 }
1399 } else if (c == '&')
1400 np->type = NFROMFD;
1401 else if (c == '>')
1402 np->type = NFROMTO;
1403 else {
1404 np->type = NFROM;
1405 pungetc();
1406 }
1407 }
1408 if (fd != '\0')
1409 np->nfile.fd = digit_val(fd);
1410 redirnode = np;
1411 goto parseredir_return;
1412}
1413
1414
1415/*
1416 * Parse a substitution. At this point, we have read the dollar sign
1417 * and nothing else.
1418 */
1419
1420parsesub: {
1421 char buf[10];
1422 int subtype;
1423 int typeloc;
1424 int flags;
1425 char *p;
1426 static const char types[] = "}-+?=";
1427 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1428 int i;
1429 int linno;
1430 int length;
1431
1432 c = pgetc();
1433 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1434 !is_special(c)) {
1435 USTPUTC('$', out);
1436 pungetc();
1437 } else if (c == '(') { /* $(command) or $((arith)) */
1438 if (pgetc() == '(') {
1439 PARSEARITH();
1440 } else {
1441 pungetc();
1442 out = parsebackq(out, &bqlist, 0,
1443 state[level].syntax == DQSYNTAX &&
1444 (eofmark == NULL || newvarnest > 0),
1445 state[level].syntax == DQSYNTAX ||
1446 state[level].syntax == ARISYNTAX);
1447 }
1448 } else {
1449 USTPUTC(CTLVAR, out);
1450 typeloc = out - stackblock();
1451 USTPUTC(VSNORMAL, out);
1452 subtype = VSNORMAL;
1453 flags = 0;
1454 if (c == '{') {
1455 bracketed_name = 1;
1456 c = pgetc();
1457 if (c == '#') {
1458 if ((c = pgetc()) == '}')
1459 c = '#';
1460 else
1461 subtype = VSLENGTH;
1462 }
1463 else
1464 subtype = 0;
1465 }
1466 if (!is_eof(c) && is_name(c)) {
1467 length = 0;
1468 do {
1469 STPUTC(c, out);
1470 c = pgetc();
1471 length++;
1472 } while (!is_eof(c) && is_in_name(c));
1473 if (length == 6 &&
1474 strncmp(out - length, "LINENO", length) == 0) {
1475 /* Replace the variable name with the
1476 * current line number. */
1477 linno = plinno;
1478 if (funclinno != 0)
1479 linno -= funclinno - 1;
1480 snprintf(buf, sizeof(buf), "%d", linno);
1481 STADJUST(-6, out);
1482 for (i = 0; buf[i] != '\0'; i++)
1483 STPUTC(buf[i], out);
1484 flags |= VSLINENO;
1485 }
1486 } else if (is_digit(c)) {
1487 if (bracketed_name) {
1488 do {
1489 STPUTC(c, out);
1490 c = pgetc();
1491 } while (is_digit(c));
1492 } else {
1493 STPUTC(c, out);
1494 c = pgetc();
1495 }
1496 } else {
1497 if (! is_special(c)) {
1498 subtype = VSERROR;
1499 if (c == '}')
1500 pungetc();
1501 else if (c == '\n' || c == PEOF)
1502 synerror("Unexpected end of line in substitution");
1503 else
1504 USTPUTC(c, out);
1505 } else {
1506 USTPUTC(c, out);
1507 c = pgetc();
1508 }
1509 }
1510 if (subtype == 0) {
1511 switch (c) {
1512 case ':':
1513 flags |= VSNUL;
1514 c = pgetc();
1515 /*FALLTHROUGH*/
1516 default:
1517 p = strchr(types, c);
1518 if (p == NULL) {
1519 if (c == '\n' || c == PEOF)
1520 synerror("Unexpected end of line in substitution");
1521 if (flags == VSNUL)
1522 STPUTC(':', out);
1523 STPUTC(c, out);
1524 subtype = VSERROR;
1525 } else
1526 subtype = p - types + VSNORMAL;
1527 break;
1528 case '%':
1529 case '#':
1530 {
1531 int cc = c;
1532 subtype = c == '#' ? VSTRIMLEFT :
1533 VSTRIMRIGHT;
1534 c = pgetc();
1535 if (c == cc)
1536 subtype++;
1537 else
1538 pungetc();
1539 break;
1540 }
1541 }
1542 } else if (subtype != VSERROR) {
1543 pungetc();
1544 }
1545 STPUTC('=', out);
1546 if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX ||
1547 state[level].syntax == ARISYNTAX))
1548 flags |= VSQUOTE;
1549 *(stackblock() + typeloc) = subtype | flags;
1550 if (subtype != VSNORMAL) {
1551 if (level + 1 >= maxnest) {
1552 maxnest *= 2;
1553 if (state == state_static) {
1554 state = parser_temp_alloc(
1555 maxnest * sizeof(*state));
1556 memcpy(state, state_static,
1148 struct tokenstate *state = state_static;
1149
1150 startlinno = plinno;
1151 quotef = 0;
1152 bqlist = NULL;
1153 newvarnest = 0;
1154 level = 0;
1155 state[level].syntax = initialsyntax;
1156 state[level].parenlevel = 0;
1157 state[level].category = TSTATE_TOP;
1158
1159 STARTSTACKSTR(out);
1160 loop: { /* for each line, until end of word */
1161 CHECKEND(); /* set c to PEOF if at end of here document */
1162 for (;;) { /* until end of line or end of word */
1163 CHECKSTRSPACE(3, out); /* permit 3 calls to USTPUTC */
1164
1165 synentry = state[level].syntax[c];
1166
1167 switch(synentry) {
1168 case CNL: /* '\n' */
1169 if (state[level].syntax == BASESYNTAX)
1170 goto endword; /* exit outer loop */
1171 USTPUTC(c, out);
1172 plinno++;
1173 if (doprompt)
1174 setprompt(2);
1175 else
1176 setprompt(0);
1177 c = pgetc();
1178 goto loop; /* continue outer loop */
1179 case CWORD:
1180 USTPUTC(c, out);
1181 break;
1182 case CCTL:
1183 if (eofmark == NULL || initialsyntax != SQSYNTAX)
1184 USTPUTC(CTLESC, out);
1185 USTPUTC(c, out);
1186 break;
1187 case CBACK: /* backslash */
1188 c = pgetc();
1189 if (c == PEOF) {
1190 USTPUTC('\\', out);
1191 pungetc();
1192 } else if (c == '\n') {
1193 plinno++;
1194 if (doprompt)
1195 setprompt(2);
1196 else
1197 setprompt(0);
1198 } else {
1199 if (state[level].syntax == DQSYNTAX &&
1200 c != '\\' && c != '`' && c != '$' &&
1201 (c != '"' || (eofmark != NULL &&
1202 newvarnest == 0)) &&
1203 (c != '}' || state[level].category != TSTATE_VAR_OLD))
1204 USTPUTC('\\', out);
1205 if (SQSYNTAX[c] == CCTL)
1206 USTPUTC(CTLESC, out);
1207 else if (eofmark == NULL ||
1208 newvarnest > 0)
1209 USTPUTC(CTLQUOTEMARK, out);
1210 USTPUTC(c, out);
1211 quotef++;
1212 }
1213 break;
1214 case CSQUOTE:
1215 USTPUTC(CTLQUOTEMARK, out);
1216 state[level].syntax = SQSYNTAX;
1217 break;
1218 case CDQUOTE:
1219 USTPUTC(CTLQUOTEMARK, out);
1220 state[level].syntax = DQSYNTAX;
1221 break;
1222 case CENDQUOTE:
1223 if (eofmark != NULL && newvarnest == 0)
1224 USTPUTC(c, out);
1225 else {
1226 if (state[level].category == TSTATE_ARITH)
1227 state[level].syntax = ARISYNTAX;
1228 else
1229 state[level].syntax = BASESYNTAX;
1230 quotef++;
1231 }
1232 break;
1233 case CVAR: /* '$' */
1234 PARSESUB(); /* parse substitution */
1235 break;
1236 case CENDVAR: /* '}' */
1237 if (level > 0 &&
1238 (state[level].category == TSTATE_VAR_OLD ||
1239 state[level].category == TSTATE_VAR_NEW)) {
1240 if (state[level].category == TSTATE_VAR_OLD)
1241 state[level - 1].syntax = state[level].syntax;
1242 else
1243 newvarnest--;
1244 level--;
1245 USTPUTC(CTLENDVAR, out);
1246 } else {
1247 USTPUTC(c, out);
1248 }
1249 break;
1250 case CLP: /* '(' in arithmetic */
1251 state[level].parenlevel++;
1252 USTPUTC(c, out);
1253 break;
1254 case CRP: /* ')' in arithmetic */
1255 if (state[level].parenlevel > 0) {
1256 USTPUTC(c, out);
1257 --state[level].parenlevel;
1258 } else {
1259 if (pgetc() == ')') {
1260 if (level > 0 &&
1261 state[level].category == TSTATE_ARITH) {
1262 level--;
1263 USTPUTC(CTLENDARI, out);
1264 } else
1265 USTPUTC(')', out);
1266 } else {
1267 /*
1268 * unbalanced parens
1269 * (don't 2nd guess - no error)
1270 */
1271 pungetc();
1272 USTPUTC(')', out);
1273 }
1274 }
1275 break;
1276 case CBQUOTE: /* '`' */
1277 out = parsebackq(out, &bqlist, 1,
1278 state[level].syntax == DQSYNTAX &&
1279 (eofmark == NULL || newvarnest > 0),
1280 state[level].syntax == DQSYNTAX || state[level].syntax == ARISYNTAX);
1281 break;
1282 case CEOF:
1283 goto endword; /* exit outer loop */
1284 default:
1285 if (level == 0)
1286 goto endword; /* exit outer loop */
1287 USTPUTC(c, out);
1288 }
1289 c = pgetc_macro();
1290 }
1291 }
1292endword:
1293 if (state[level].syntax == ARISYNTAX)
1294 synerror("Missing '))'");
1295 if (state[level].syntax != BASESYNTAX && eofmark == NULL)
1296 synerror("Unterminated quoted string");
1297 if (state[level].category == TSTATE_VAR_OLD ||
1298 state[level].category == TSTATE_VAR_NEW) {
1299 startlinno = plinno;
1300 synerror("Missing '}'");
1301 }
1302 if (state != state_static)
1303 parser_temp_free_upto(state);
1304 USTPUTC('\0', out);
1305 len = out - stackblock();
1306 out = stackblock();
1307 if (eofmark == NULL) {
1308 if ((c == '>' || c == '<')
1309 && quotef == 0
1310 && len <= 2
1311 && (*out == '\0' || is_digit(*out))) {
1312 PARSEREDIR();
1313 return lasttoken = TREDIR;
1314 } else {
1315 pungetc();
1316 }
1317 }
1318 quoteflag = quotef;
1319 backquotelist = bqlist;
1320 grabstackblock(len);
1321 wordtext = out;
1322 return lasttoken = TWORD;
1323/* end of readtoken routine */
1324
1325
1326/*
1327 * Check to see whether we are at the end of the here document. When this
1328 * is called, c is set to the first character of the next input line. If
1329 * we are at the end of the here document, this routine sets the c to PEOF.
1330 */
1331
1332checkend: {
1333 if (eofmark) {
1334 if (striptabs) {
1335 while (c == '\t')
1336 c = pgetc();
1337 }
1338 if (c == *eofmark) {
1339 if (pfgets(line, sizeof line) != NULL) {
1340 char *p, *q;
1341
1342 p = line;
1343 for (q = eofmark + 1 ; *q && *p == *q ; p++, q++);
1344 if (*p == '\n' && *q == '\0') {
1345 c = PEOF;
1346 plinno++;
1347 needprompt = doprompt;
1348 } else {
1349 pushstring(line, strlen(line), NULL);
1350 }
1351 }
1352 }
1353 }
1354 goto checkend_return;
1355}
1356
1357
1358/*
1359 * Parse a redirection operator. The variable "out" points to a string
1360 * specifying the fd to be redirected. The variable "c" contains the
1361 * first character of the redirection operator.
1362 */
1363
1364parseredir: {
1365 char fd = *out;
1366 union node *np;
1367
1368 np = (union node *)stalloc(sizeof (struct nfile));
1369 if (c == '>') {
1370 np->nfile.fd = 1;
1371 c = pgetc();
1372 if (c == '>')
1373 np->type = NAPPEND;
1374 else if (c == '&')
1375 np->type = NTOFD;
1376 else if (c == '|')
1377 np->type = NCLOBBER;
1378 else {
1379 np->type = NTO;
1380 pungetc();
1381 }
1382 } else { /* c == '<' */
1383 np->nfile.fd = 0;
1384 c = pgetc();
1385 if (c == '<') {
1386 if (sizeof (struct nfile) != sizeof (struct nhere)) {
1387 np = (union node *)stalloc(sizeof (struct nhere));
1388 np->nfile.fd = 0;
1389 }
1390 np->type = NHERE;
1391 heredoc = (struct heredoc *)stalloc(sizeof (struct heredoc));
1392 heredoc->here = np;
1393 if ((c = pgetc()) == '-') {
1394 heredoc->striptabs = 1;
1395 } else {
1396 heredoc->striptabs = 0;
1397 pungetc();
1398 }
1399 } else if (c == '&')
1400 np->type = NFROMFD;
1401 else if (c == '>')
1402 np->type = NFROMTO;
1403 else {
1404 np->type = NFROM;
1405 pungetc();
1406 }
1407 }
1408 if (fd != '\0')
1409 np->nfile.fd = digit_val(fd);
1410 redirnode = np;
1411 goto parseredir_return;
1412}
1413
1414
1415/*
1416 * Parse a substitution. At this point, we have read the dollar sign
1417 * and nothing else.
1418 */
1419
1420parsesub: {
1421 char buf[10];
1422 int subtype;
1423 int typeloc;
1424 int flags;
1425 char *p;
1426 static const char types[] = "}-+?=";
1427 int bracketed_name = 0; /* used to handle ${[0-9]*} variables */
1428 int i;
1429 int linno;
1430 int length;
1431
1432 c = pgetc();
1433 if (c != '(' && c != '{' && (is_eof(c) || !is_name(c)) &&
1434 !is_special(c)) {
1435 USTPUTC('$', out);
1436 pungetc();
1437 } else if (c == '(') { /* $(command) or $((arith)) */
1438 if (pgetc() == '(') {
1439 PARSEARITH();
1440 } else {
1441 pungetc();
1442 out = parsebackq(out, &bqlist, 0,
1443 state[level].syntax == DQSYNTAX &&
1444 (eofmark == NULL || newvarnest > 0),
1445 state[level].syntax == DQSYNTAX ||
1446 state[level].syntax == ARISYNTAX);
1447 }
1448 } else {
1449 USTPUTC(CTLVAR, out);
1450 typeloc = out - stackblock();
1451 USTPUTC(VSNORMAL, out);
1452 subtype = VSNORMAL;
1453 flags = 0;
1454 if (c == '{') {
1455 bracketed_name = 1;
1456 c = pgetc();
1457 if (c == '#') {
1458 if ((c = pgetc()) == '}')
1459 c = '#';
1460 else
1461 subtype = VSLENGTH;
1462 }
1463 else
1464 subtype = 0;
1465 }
1466 if (!is_eof(c) && is_name(c)) {
1467 length = 0;
1468 do {
1469 STPUTC(c, out);
1470 c = pgetc();
1471 length++;
1472 } while (!is_eof(c) && is_in_name(c));
1473 if (length == 6 &&
1474 strncmp(out - length, "LINENO", length) == 0) {
1475 /* Replace the variable name with the
1476 * current line number. */
1477 linno = plinno;
1478 if (funclinno != 0)
1479 linno -= funclinno - 1;
1480 snprintf(buf, sizeof(buf), "%d", linno);
1481 STADJUST(-6, out);
1482 for (i = 0; buf[i] != '\0'; i++)
1483 STPUTC(buf[i], out);
1484 flags |= VSLINENO;
1485 }
1486 } else if (is_digit(c)) {
1487 if (bracketed_name) {
1488 do {
1489 STPUTC(c, out);
1490 c = pgetc();
1491 } while (is_digit(c));
1492 } else {
1493 STPUTC(c, out);
1494 c = pgetc();
1495 }
1496 } else {
1497 if (! is_special(c)) {
1498 subtype = VSERROR;
1499 if (c == '}')
1500 pungetc();
1501 else if (c == '\n' || c == PEOF)
1502 synerror("Unexpected end of line in substitution");
1503 else
1504 USTPUTC(c, out);
1505 } else {
1506 USTPUTC(c, out);
1507 c = pgetc();
1508 }
1509 }
1510 if (subtype == 0) {
1511 switch (c) {
1512 case ':':
1513 flags |= VSNUL;
1514 c = pgetc();
1515 /*FALLTHROUGH*/
1516 default:
1517 p = strchr(types, c);
1518 if (p == NULL) {
1519 if (c == '\n' || c == PEOF)
1520 synerror("Unexpected end of line in substitution");
1521 if (flags == VSNUL)
1522 STPUTC(':', out);
1523 STPUTC(c, out);
1524 subtype = VSERROR;
1525 } else
1526 subtype = p - types + VSNORMAL;
1527 break;
1528 case '%':
1529 case '#':
1530 {
1531 int cc = c;
1532 subtype = c == '#' ? VSTRIMLEFT :
1533 VSTRIMRIGHT;
1534 c = pgetc();
1535 if (c == cc)
1536 subtype++;
1537 else
1538 pungetc();
1539 break;
1540 }
1541 }
1542 } else if (subtype != VSERROR) {
1543 pungetc();
1544 }
1545 STPUTC('=', out);
1546 if (subtype != VSLENGTH && (state[level].syntax == DQSYNTAX ||
1547 state[level].syntax == ARISYNTAX))
1548 flags |= VSQUOTE;
1549 *(stackblock() + typeloc) = subtype | flags;
1550 if (subtype != VSNORMAL) {
1551 if (level + 1 >= maxnest) {
1552 maxnest *= 2;
1553 if (state == state_static) {
1554 state = parser_temp_alloc(
1555 maxnest * sizeof(*state));
1556 memcpy(state, state_static,
1557 MAXNEST_STATIC * sizeof(*state));
1557 MAXNEST_static * sizeof(*state));
1558 } else
1559 state = parser_temp_realloc(state,
1560 maxnest * sizeof(*state));
1561 }
1562 level++;
1563 state[level].parenlevel = 0;
1564 if (subtype == VSMINUS || subtype == VSPLUS ||
1565 subtype == VSQUESTION || subtype == VSASSIGN) {
1566 /*
1567 * For operators that were in the Bourne shell,
1568 * inherit the double-quote state.
1569 */
1570 state[level].syntax = state[level - 1].syntax;
1571 state[level].category = TSTATE_VAR_OLD;
1572 } else {
1573 /*
1574 * The other operators take a pattern,
1575 * so go to BASESYNTAX.
1576 * Also, ' and " are now special, even
1577 * in here documents.
1578 */
1579 state[level].syntax = BASESYNTAX;
1580 state[level].category = TSTATE_VAR_NEW;
1581 newvarnest++;
1582 }
1583 }
1584 }
1585 goto parsesub_return;
1586}
1587
1588
1589/*
1590 * Parse an arithmetic expansion (indicate start of one and set state)
1591 */
1592parsearith: {
1593
1594 if (level + 1 >= maxnest) {
1595 maxnest *= 2;
1596 if (state == state_static) {
1597 state = parser_temp_alloc(
1598 maxnest * sizeof(*state));
1599 memcpy(state, state_static,
1558 } else
1559 state = parser_temp_realloc(state,
1560 maxnest * sizeof(*state));
1561 }
1562 level++;
1563 state[level].parenlevel = 0;
1564 if (subtype == VSMINUS || subtype == VSPLUS ||
1565 subtype == VSQUESTION || subtype == VSASSIGN) {
1566 /*
1567 * For operators that were in the Bourne shell,
1568 * inherit the double-quote state.
1569 */
1570 state[level].syntax = state[level - 1].syntax;
1571 state[level].category = TSTATE_VAR_OLD;
1572 } else {
1573 /*
1574 * The other operators take a pattern,
1575 * so go to BASESYNTAX.
1576 * Also, ' and " are now special, even
1577 * in here documents.
1578 */
1579 state[level].syntax = BASESYNTAX;
1580 state[level].category = TSTATE_VAR_NEW;
1581 newvarnest++;
1582 }
1583 }
1584 }
1585 goto parsesub_return;
1586}
1587
1588
1589/*
1590 * Parse an arithmetic expansion (indicate start of one and set state)
1591 */
1592parsearith: {
1593
1594 if (level + 1 >= maxnest) {
1595 maxnest *= 2;
1596 if (state == state_static) {
1597 state = parser_temp_alloc(
1598 maxnest * sizeof(*state));
1599 memcpy(state, state_static,
1600 MAXNEST_STATIC * sizeof(*state));
1600 MAXNEST_static * sizeof(*state));
1601 } else
1602 state = parser_temp_realloc(state,
1603 maxnest * sizeof(*state));
1604 }
1605 level++;
1606 state[level].syntax = ARISYNTAX;
1607 state[level].parenlevel = 0;
1608 state[level].category = TSTATE_ARITH;
1609 USTPUTC(CTLARI, out);
1610 if (state[level - 1].syntax == DQSYNTAX)
1611 USTPUTC('"',out);
1612 else
1613 USTPUTC(' ',out);
1614 goto parsearith_return;
1615}
1616
1617} /* end of readtoken */
1618
1619
1620
1621#ifdef mkinit
1622RESET {
1623 tokpushback = 0;
1624 checkkwd = 0;
1625}
1626#endif
1627
1628/*
1629 * Returns true if the text contains nothing to expand (no dollar signs
1630 * or backquotes).
1631 */
1632
1601 } else
1602 state = parser_temp_realloc(state,
1603 maxnest * sizeof(*state));
1604 }
1605 level++;
1606 state[level].syntax = ARISYNTAX;
1607 state[level].parenlevel = 0;
1608 state[level].category = TSTATE_ARITH;
1609 USTPUTC(CTLARI, out);
1610 if (state[level - 1].syntax == DQSYNTAX)
1611 USTPUTC('"',out);
1612 else
1613 USTPUTC(' ',out);
1614 goto parsearith_return;
1615}
1616
1617} /* end of readtoken */
1618
1619
1620
1621#ifdef mkinit
1622RESET {
1623 tokpushback = 0;
1624 checkkwd = 0;
1625}
1626#endif
1627
1628/*
1629 * Returns true if the text contains nothing to expand (no dollar signs
1630 * or backquotes).
1631 */
1632
1633STATIC int
1633static int
1634noexpand(char *text)
1635{
1636 char *p;
1637 char c;
1638
1639 p = text;
1640 while ((c = *p++) != '\0') {
1641 if ( c == CTLQUOTEMARK)
1642 continue;
1643 if (c == CTLESC)
1644 p++;
1645 else if (BASESYNTAX[(int)c] == CCTL)
1646 return 0;
1647 }
1648 return 1;
1649}
1650
1651
1652/*
1653 * Return true if the argument is a legal variable name (a letter or
1654 * underscore followed by zero or more letters, underscores, and digits).
1655 */
1656
1657int
1658goodname(const char *name)
1659{
1660 const char *p;
1661
1662 p = name;
1663 if (! is_name(*p))
1664 return 0;
1665 while (*++p) {
1666 if (! is_in_name(*p))
1667 return 0;
1668 }
1669 return 1;
1670}
1671
1672
1673/*
1674 * Called when an unexpected token is read during the parse. The argument
1675 * is the token that is expected, or -1 if more than one type of token can
1676 * occur at this point.
1677 */
1678
1634noexpand(char *text)
1635{
1636 char *p;
1637 char c;
1638
1639 p = text;
1640 while ((c = *p++) != '\0') {
1641 if ( c == CTLQUOTEMARK)
1642 continue;
1643 if (c == CTLESC)
1644 p++;
1645 else if (BASESYNTAX[(int)c] == CCTL)
1646 return 0;
1647 }
1648 return 1;
1649}
1650
1651
1652/*
1653 * Return true if the argument is a legal variable name (a letter or
1654 * underscore followed by zero or more letters, underscores, and digits).
1655 */
1656
1657int
1658goodname(const char *name)
1659{
1660 const char *p;
1661
1662 p = name;
1663 if (! is_name(*p))
1664 return 0;
1665 while (*++p) {
1666 if (! is_in_name(*p))
1667 return 0;
1668 }
1669 return 1;
1670}
1671
1672
1673/*
1674 * Called when an unexpected token is read during the parse. The argument
1675 * is the token that is expected, or -1 if more than one type of token can
1676 * occur at this point.
1677 */
1678
1679STATIC void
1679static void
1680synexpect(int token)
1681{
1682 char msg[64];
1683
1684 if (token >= 0) {
1685 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1686 tokname[lasttoken], tokname[token]);
1687 } else {
1688 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1689 }
1690 synerror(msg);
1691}
1692
1693
1680synexpect(int token)
1681{
1682 char msg[64];
1683
1684 if (token >= 0) {
1685 fmtstr(msg, 64, "%s unexpected (expecting %s)",
1686 tokname[lasttoken], tokname[token]);
1687 } else {
1688 fmtstr(msg, 64, "%s unexpected", tokname[lasttoken]);
1689 }
1690 synerror(msg);
1691}
1692
1693
1694STATIC void
1694static void
1695synerror(const char *msg)
1696{
1697 if (commandname)
1698 outfmt(out2, "%s: %d: ", commandname, startlinno);
1699 outfmt(out2, "Syntax error: %s\n", msg);
1700 error((char *)NULL);
1701}
1702
1695synerror(const char *msg)
1696{
1697 if (commandname)
1698 outfmt(out2, "%s: %d: ", commandname, startlinno);
1699 outfmt(out2, "Syntax error: %s\n", msg);
1700 error((char *)NULL);
1701}
1702
1703STATIC void
1703static void
1704setprompt(int which)
1705{
1706 whichprompt = which;
1707
1708#ifndef NO_HISTORY
1709 if (!el)
1710#endif
1711 {
1712 out2str(getprompt(NULL));
1713 flushout(out2);
1714 }
1715}
1716
1717/*
1718 * called by editline -- any expansions to the prompt
1719 * should be added here.
1720 */
1721char *
1722getprompt(void *unused __unused)
1723{
1724 static char ps[PROMPTLEN];
1725 char *fmt;
1726 const char *pwd;
1727 int i, trim;
1728 static char internal_error[] = "<internal prompt error>";
1729
1730 /*
1731 * Select prompt format.
1732 */
1733 switch (whichprompt) {
1734 case 0:
1735 fmt = nullstr;
1736 break;
1737 case 1:
1738 fmt = ps1val();
1739 break;
1740 case 2:
1741 fmt = ps2val();
1742 break;
1743 default:
1744 return internal_error;
1745 }
1746
1747 /*
1748 * Format prompt string.
1749 */
1750 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1751 if (*fmt == '\\')
1752 switch (*++fmt) {
1753
1754 /*
1755 * Hostname.
1756 *
1757 * \h specifies just the local hostname,
1758 * \H specifies fully-qualified hostname.
1759 */
1760 case 'h':
1761 case 'H':
1762 ps[i] = '\0';
1763 gethostname(&ps[i], PROMPTLEN - i);
1764 /* Skip to end of hostname. */
1765 trim = (*fmt == 'h') ? '.' : '\0';
1766 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1767 i++;
1768 break;
1769
1770 /*
1771 * Working directory.
1772 *
1773 * \W specifies just the final component,
1774 * \w specifies the entire path.
1775 */
1776 case 'W':
1777 case 'w':
1778 pwd = lookupvar("PWD");
1779 if (pwd == NULL)
1780 pwd = "?";
1781 if (*fmt == 'W' &&
1782 *pwd == '/' && pwd[1] != '\0')
1783 strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1784 PROMPTLEN - i);
1785 else
1786 strlcpy(&ps[i], pwd, PROMPTLEN - i);
1787 /* Skip to end of path. */
1788 while (ps[i + 1] != '\0')
1789 i++;
1790 break;
1791
1792 /*
1793 * Superuser status.
1794 *
1795 * '$' for normal users, '#' for root.
1796 */
1797 case '$':
1798 ps[i] = (geteuid() != 0) ? '$' : '#';
1799 break;
1800
1801 /*
1802 * A literal \.
1803 */
1804 case '\\':
1805 ps[i] = '\\';
1806 break;
1807
1808 /*
1809 * Emit unrecognized formats verbatim.
1810 */
1811 default:
1812 ps[i++] = '\\';
1813 ps[i] = *fmt;
1814 break;
1815 }
1816 else
1817 ps[i] = *fmt;
1818 ps[i] = '\0';
1819 return (ps);
1820}
1704setprompt(int which)
1705{
1706 whichprompt = which;
1707
1708#ifndef NO_HISTORY
1709 if (!el)
1710#endif
1711 {
1712 out2str(getprompt(NULL));
1713 flushout(out2);
1714 }
1715}
1716
1717/*
1718 * called by editline -- any expansions to the prompt
1719 * should be added here.
1720 */
1721char *
1722getprompt(void *unused __unused)
1723{
1724 static char ps[PROMPTLEN];
1725 char *fmt;
1726 const char *pwd;
1727 int i, trim;
1728 static char internal_error[] = "<internal prompt error>";
1729
1730 /*
1731 * Select prompt format.
1732 */
1733 switch (whichprompt) {
1734 case 0:
1735 fmt = nullstr;
1736 break;
1737 case 1:
1738 fmt = ps1val();
1739 break;
1740 case 2:
1741 fmt = ps2val();
1742 break;
1743 default:
1744 return internal_error;
1745 }
1746
1747 /*
1748 * Format prompt string.
1749 */
1750 for (i = 0; (i < 127) && (*fmt != '\0'); i++, fmt++)
1751 if (*fmt == '\\')
1752 switch (*++fmt) {
1753
1754 /*
1755 * Hostname.
1756 *
1757 * \h specifies just the local hostname,
1758 * \H specifies fully-qualified hostname.
1759 */
1760 case 'h':
1761 case 'H':
1762 ps[i] = '\0';
1763 gethostname(&ps[i], PROMPTLEN - i);
1764 /* Skip to end of hostname. */
1765 trim = (*fmt == 'h') ? '.' : '\0';
1766 while ((ps[i+1] != '\0') && (ps[i+1] != trim))
1767 i++;
1768 break;
1769
1770 /*
1771 * Working directory.
1772 *
1773 * \W specifies just the final component,
1774 * \w specifies the entire path.
1775 */
1776 case 'W':
1777 case 'w':
1778 pwd = lookupvar("PWD");
1779 if (pwd == NULL)
1780 pwd = "?";
1781 if (*fmt == 'W' &&
1782 *pwd == '/' && pwd[1] != '\0')
1783 strlcpy(&ps[i], strrchr(pwd, '/') + 1,
1784 PROMPTLEN - i);
1785 else
1786 strlcpy(&ps[i], pwd, PROMPTLEN - i);
1787 /* Skip to end of path. */
1788 while (ps[i + 1] != '\0')
1789 i++;
1790 break;
1791
1792 /*
1793 * Superuser status.
1794 *
1795 * '$' for normal users, '#' for root.
1796 */
1797 case '$':
1798 ps[i] = (geteuid() != 0) ? '$' : '#';
1799 break;
1800
1801 /*
1802 * A literal \.
1803 */
1804 case '\\':
1805 ps[i] = '\\';
1806 break;
1807
1808 /*
1809 * Emit unrecognized formats verbatim.
1810 */
1811 default:
1812 ps[i++] = '\\';
1813 ps[i] = *fmt;
1814 break;
1815 }
1816 else
1817 ps[i] = *fmt;
1818 ps[i] = '\0';
1819 return (ps);
1820}