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