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