1 | /* $NetBSD: show.c,v 1.26 2003/11/14 10:46:13 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[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
|
---|
38 | #else
|
---|
39 | __RCSID("$NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $");
|
---|
40 | #endif /* not lint */
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <stdio.h>
|
---|
44 | #include <stdarg.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <assert.h>
|
---|
47 |
|
---|
48 | #include "shell.h"
|
---|
49 | #include "parser.h"
|
---|
50 | #include "nodes.h"
|
---|
51 | #include "mystring.h"
|
---|
52 | #include "show.h"
|
---|
53 | #include "options.h"
|
---|
54 | #include "shinstance.h"
|
---|
55 |
|
---|
56 |
|
---|
57 | #ifdef DEBUG
|
---|
58 | static void shtree(union node *, int, char *, FILE*);
|
---|
59 | static void shcmd(union node *, FILE *);
|
---|
60 | static void sharg(union node *, FILE *);
|
---|
61 | static void indent(int, char *, FILE *);
|
---|
62 | static void trstring(shinstance *, char *);
|
---|
63 |
|
---|
64 |
|
---|
65 | void
|
---|
66 | showtree(shinstance *psh, union node *n)
|
---|
67 | {
|
---|
68 | trputs(psh, "showtree called\n");
|
---|
69 | shtree(n, 1, NULL, stdout);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | static void
|
---|
74 | shtree(union node *n, int ind, char *pfx, FILE *fp)
|
---|
75 | {
|
---|
76 | struct nodelist *lp;
|
---|
77 | const char *s;
|
---|
78 |
|
---|
79 | if (n == NULL)
|
---|
80 | return;
|
---|
81 |
|
---|
82 | indent(ind, pfx, fp);
|
---|
83 | switch(n->type) {
|
---|
84 | case NSEMI:
|
---|
85 | s = "; ";
|
---|
86 | goto binop;
|
---|
87 | case NAND:
|
---|
88 | s = " && ";
|
---|
89 | goto binop;
|
---|
90 | case NOR:
|
---|
91 | s = " || ";
|
---|
92 | binop:
|
---|
93 | shtree(n->nbinary.ch1, ind, NULL, fp);
|
---|
94 | /* if (ind < 0) */
|
---|
95 | fputs(s, fp);
|
---|
96 | shtree(n->nbinary.ch2, ind, NULL, fp);
|
---|
97 | break;
|
---|
98 | case NCMD:
|
---|
99 | shcmd(n, fp);
|
---|
100 | if (ind >= 0)
|
---|
101 | putc('\n', fp);
|
---|
102 | break;
|
---|
103 | case NPIPE:
|
---|
104 | for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
|
---|
105 | shcmd(lp->n, fp);
|
---|
106 | if (lp->next)
|
---|
107 | fputs(" | ", fp);
|
---|
108 | }
|
---|
109 | if (n->npipe.backgnd)
|
---|
110 | fputs(" &", fp);
|
---|
111 | if (ind >= 0)
|
---|
112 | putc('\n', fp);
|
---|
113 | break;
|
---|
114 | default:
|
---|
115 | fprintf(fp, "<node type %d>", n->type);
|
---|
116 | if (ind >= 0)
|
---|
117 | putc('\n', fp);
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 |
|
---|
124 | static void
|
---|
125 | shcmd(union node *cmd, FILE *fp)
|
---|
126 | {
|
---|
127 | union node *np;
|
---|
128 | int first;
|
---|
129 | const char *s;
|
---|
130 | int dftfd;
|
---|
131 |
|
---|
132 | first = 1;
|
---|
133 | for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
|
---|
134 | if (! first)
|
---|
135 | putchar(' ');
|
---|
136 | sharg(np, fp);
|
---|
137 | first = 0;
|
---|
138 | }
|
---|
139 | for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
|
---|
140 | if (! first)
|
---|
141 | putchar(' ');
|
---|
142 | switch (np->nfile.type) {
|
---|
143 | case NTO: s = ">"; dftfd = 1; break;
|
---|
144 | case NCLOBBER: s = ">|"; dftfd = 1; break;
|
---|
145 | case NAPPEND: s = ">>"; dftfd = 1; break;
|
---|
146 | case NTOFD: s = ">&"; dftfd = 1; break;
|
---|
147 | case NFROM: s = "<"; dftfd = 0; break;
|
---|
148 | case NFROMFD: s = "<&"; dftfd = 0; break;
|
---|
149 | case NFROMTO: s = "<>"; dftfd = 0; break;
|
---|
150 | default: s = "*error*"; dftfd = 0; break;
|
---|
151 | }
|
---|
152 | if (np->nfile.fd != dftfd)
|
---|
153 | fprintf(fp, "%d", np->nfile.fd);
|
---|
154 | fputs(s, fp);
|
---|
155 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
|
---|
156 | fprintf(fp, "%d", np->ndup.dupfd);
|
---|
157 | } else {
|
---|
158 | sharg(np->nfile.fname, fp);
|
---|
159 | }
|
---|
160 | first = 0;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 | static void
|
---|
167 | sharg(union node *arg, FILE *fp)
|
---|
168 | {
|
---|
169 | char *p;
|
---|
170 | struct nodelist *bqlist;
|
---|
171 | int subtype;
|
---|
172 |
|
---|
173 | if (arg->type != NARG) {
|
---|
174 | printf("<node type %d>\n", arg->type);
|
---|
175 | abort();
|
---|
176 | }
|
---|
177 | bqlist = arg->narg.backquote;
|
---|
178 | for (p = arg->narg.text ; *p ; p++) {
|
---|
179 | switch (*p) {
|
---|
180 | case CTLESC:
|
---|
181 | putc(*++p, fp);
|
---|
182 | break;
|
---|
183 | case CTLVAR:
|
---|
184 | putc('$', fp);
|
---|
185 | putc('{', fp);
|
---|
186 | subtype = *++p;
|
---|
187 | if (subtype == VSLENGTH)
|
---|
188 | putc('#', fp);
|
---|
189 |
|
---|
190 | while (*p != '=')
|
---|
191 | putc(*p++, fp);
|
---|
192 |
|
---|
193 | if (subtype & VSNUL)
|
---|
194 | putc(':', fp);
|
---|
195 |
|
---|
196 | switch (subtype & VSTYPE) {
|
---|
197 | case VSNORMAL:
|
---|
198 | putc('}', fp);
|
---|
199 | break;
|
---|
200 | case VSMINUS:
|
---|
201 | putc('-', fp);
|
---|
202 | break;
|
---|
203 | case VSPLUS:
|
---|
204 | putc('+', fp);
|
---|
205 | break;
|
---|
206 | case VSQUESTION:
|
---|
207 | putc('?', fp);
|
---|
208 | break;
|
---|
209 | case VSASSIGN:
|
---|
210 | putc('=', fp);
|
---|
211 | break;
|
---|
212 | case VSTRIMLEFT:
|
---|
213 | putc('#', fp);
|
---|
214 | break;
|
---|
215 | case VSTRIMLEFTMAX:
|
---|
216 | putc('#', fp);
|
---|
217 | putc('#', fp);
|
---|
218 | break;
|
---|
219 | case VSTRIMRIGHT:
|
---|
220 | putc('%', fp);
|
---|
221 | break;
|
---|
222 | case VSTRIMRIGHTMAX:
|
---|
223 | putc('%', fp);
|
---|
224 | putc('%', fp);
|
---|
225 | break;
|
---|
226 | case VSLENGTH:
|
---|
227 | break;
|
---|
228 | default:
|
---|
229 | printf("<subtype %d>", subtype);
|
---|
230 | }
|
---|
231 | break;
|
---|
232 | case CTLENDVAR:
|
---|
233 | putc('}', fp);
|
---|
234 | break;
|
---|
235 | case CTLBACKQ:
|
---|
236 | case CTLBACKQ|CTLQUOTE:
|
---|
237 | putc('$', fp);
|
---|
238 | putc('(', fp);
|
---|
239 | shtree(bqlist->n, -1, NULL, fp);
|
---|
240 | putc(')', fp);
|
---|
241 | break;
|
---|
242 | default:
|
---|
243 | putc(*p, fp);
|
---|
244 | break;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | static void
|
---|
251 | indent(int amount, char *pfx, FILE *fp)
|
---|
252 | {
|
---|
253 | int i;
|
---|
254 |
|
---|
255 | for (i = 0 ; i < amount ; i++) {
|
---|
256 | if (pfx && i == amount - 1)
|
---|
257 | fputs(pfx, fp);
|
---|
258 | putc('\t', fp);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | #endif
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | #ifdef DEBUG
|
---|
266 | /*
|
---|
267 | * Debugging stuff.
|
---|
268 | */
|
---|
269 |
|
---|
270 | /** @def TRY_GET_PSH_OR_RETURN
|
---|
271 | * Make sure @a psh is valid, trying to fetch it from TLS
|
---|
272 | * if it's NULL and returning (void) if that fails. */
|
---|
273 | # define TRY_GET_PSH_OR_RETURN(psh) \
|
---|
274 | if (!(psh)) { \
|
---|
275 | psh = shthread_get_shell(); \
|
---|
276 | if (!psh) \
|
---|
277 | return; \
|
---|
278 | } else do { } while (0)
|
---|
279 |
|
---|
280 | /** @def RETURN_IF_NOT_TRACING
|
---|
281 | * Return if we're not tracing. */
|
---|
282 | # define RETURN_IF_NOT_TRACING(psh) \
|
---|
283 | if (debug(psh) != 1 || psh->tracefd == -1) \
|
---|
284 | return; \
|
---|
285 | else do {} while (0)
|
---|
286 |
|
---|
287 | /* Flushes the tracebuf. */
|
---|
288 | static void
|
---|
289 | trace_flush(shinstance *psh)
|
---|
290 | {
|
---|
291 | size_t pos = psh->tracepos;
|
---|
292 |
|
---|
293 | if (pos > sizeof(psh->tracebuf)) {
|
---|
294 | char *end;
|
---|
295 | assert(0);
|
---|
296 | end = memchr(psh->tracebuf, '\0', sizeof(psh->tracebuf));
|
---|
297 | pos = end ? end - &psh->tracebuf[0] : 0;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (pos) {
|
---|
301 | int s = errno;
|
---|
302 | char prefix[40];
|
---|
303 | size_t len;
|
---|
304 |
|
---|
305 | len = sprintf(prefix, "[%d] ", sh_getpid(psh));
|
---|
306 | shfile_write(&psh->fdtab, psh->tracefd, prefix, len);
|
---|
307 | shfile_write(&psh->fdtab, psh->tracefd, psh->tracebuf, pos);
|
---|
308 |
|
---|
309 | psh->tracepos = 0;
|
---|
310 | psh->tracebuf[0] = '\0';
|
---|
311 |
|
---|
312 | errno = s;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | /* Adds a char to the trace buffer. */
|
---|
317 | static void
|
---|
318 | trace_char(shinstance *psh, int c)
|
---|
319 | {
|
---|
320 | size_t pos = psh->tracepos;
|
---|
321 | if (pos >= sizeof(psh->tracebuf) - 1) {
|
---|
322 | trace_flush(psh);
|
---|
323 | pos = psh->tracepos;
|
---|
324 | }
|
---|
325 | psh->tracebuf[pos] = c;
|
---|
326 | psh->tracepos = pos + 1;
|
---|
327 | if (c == '\n')
|
---|
328 | trace_flush(psh);
|
---|
329 | else
|
---|
330 | psh->tracebuf[pos + 1] = '\0';
|
---|
331 | }
|
---|
332 |
|
---|
333 | /* Add a string to the trace buffer. */
|
---|
334 | static void
|
---|
335 | trace_string(shinstance *psh, const char *str)
|
---|
336 | {
|
---|
337 | /* push it out line by line. */
|
---|
338 | while (*str) {
|
---|
339 | /* find line/string length. */
|
---|
340 | size_t pos;
|
---|
341 | size_t len;
|
---|
342 | const char *end = str;
|
---|
343 | int flush_it = 0;
|
---|
344 | while (*end) {
|
---|
345 | if (*end++ == '\n') {
|
---|
346 | flush_it = 1;
|
---|
347 | break;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | len = end - str;
|
---|
351 |
|
---|
352 | /* copy to the buffer */
|
---|
353 | pos = psh->tracepos;
|
---|
354 | if (pos + len <= sizeof(psh->tracebuf)) {
|
---|
355 | memcpy(&psh->tracebuf[pos], str, len);
|
---|
356 | psh->tracepos = pos + len;
|
---|
357 | if (flush_it)
|
---|
358 | trace_flush(psh);
|
---|
359 | } else {
|
---|
360 | /* it's too big for some reason... */
|
---|
361 | int s = errno;
|
---|
362 | trace_flush(psh);
|
---|
363 | shfile_write(&psh->fdtab, psh->tracefd, str, len);
|
---|
364 | if (!flush_it)
|
---|
365 | shfile_write(&psh->fdtab, psh->tracefd, "[too long]\n", sizeof( "[too long]\n") - 1);
|
---|
366 | errno = s;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /* advance */
|
---|
370 | str = end;
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | void
|
---|
375 | trputc(shinstance *psh, int c)
|
---|
376 | {
|
---|
377 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
378 | RETURN_IF_NOT_TRACING(psh);
|
---|
379 |
|
---|
380 | trace_char(psh, c);
|
---|
381 | }
|
---|
382 |
|
---|
383 | void
|
---|
384 | trace(shinstance *psh, const char *fmt, ...)
|
---|
385 | {
|
---|
386 | va_list va;
|
---|
387 | char buf[2048];
|
---|
388 |
|
---|
389 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
390 | RETURN_IF_NOT_TRACING(psh);
|
---|
391 |
|
---|
392 | va_start(va, fmt);
|
---|
393 | # ifdef _MSC_VER
|
---|
394 | _vsnprintf(buf, sizeof(buf), fmt, va);
|
---|
395 | # else
|
---|
396 | vsnprintf(buf, sizeof(buf), fmt, va);
|
---|
397 | # endif
|
---|
398 | va_end(va);
|
---|
399 | trace_string(psh, buf);
|
---|
400 | }
|
---|
401 |
|
---|
402 | void
|
---|
403 | tracev(shinstance *psh, const char *fmt, va_list va)
|
---|
404 | {
|
---|
405 | char buf[2048];
|
---|
406 |
|
---|
407 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
408 | RETURN_IF_NOT_TRACING(psh);
|
---|
409 |
|
---|
410 | # ifdef _MSC_VER
|
---|
411 | _vsnprintf(buf, sizeof(buf), fmt, va);
|
---|
412 | # else
|
---|
413 | vsnprintf(buf, sizeof(buf), fmt, va);
|
---|
414 | # endif
|
---|
415 | trace_string(psh, buf);
|
---|
416 | }
|
---|
417 |
|
---|
418 | void
|
---|
419 | trputs(shinstance *psh, const char *s)
|
---|
420 | {
|
---|
421 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
422 | RETURN_IF_NOT_TRACING(psh);
|
---|
423 |
|
---|
424 | trace_string(psh, s);
|
---|
425 | trace_char(psh, '\n');
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | static void
|
---|
430 | trstring(shinstance *psh, char *s)
|
---|
431 | {
|
---|
432 | char *p;
|
---|
433 | char c;
|
---|
434 |
|
---|
435 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
436 | RETURN_IF_NOT_TRACING(psh);
|
---|
437 |
|
---|
438 | trace_char(psh, '"');
|
---|
439 | for (p = s ; *p ; p++) {
|
---|
440 | switch (*p) {
|
---|
441 | case '\n': c = 'n'; goto backslash;
|
---|
442 | case '\t': c = 't'; goto backslash;
|
---|
443 | case '\r': c = 'r'; goto backslash;
|
---|
444 | case '"': c = '"'; goto backslash;
|
---|
445 | case '\\': c = '\\'; goto backslash;
|
---|
446 | case CTLESC: c = 'e'; goto backslash;
|
---|
447 | case CTLVAR: c = 'v'; goto backslash;
|
---|
448 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
|
---|
449 | case CTLBACKQ: c = 'q'; goto backslash;
|
---|
450 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
|
---|
451 | backslash: trace_char(psh, '\\');
|
---|
452 | trace_char(psh, c);
|
---|
453 | break;
|
---|
454 | default:
|
---|
455 | if (*p >= ' ' && *p <= '~')
|
---|
456 | trace_char(psh, *p);
|
---|
457 | else {
|
---|
458 | trace_char(psh, '\\');
|
---|
459 | trace_char(psh, *p >> 6 & 03);
|
---|
460 | trace_char(psh, *p >> 3 & 07);
|
---|
461 | trace_char(psh, *p & 07);
|
---|
462 | }
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | }
|
---|
466 | trace_char(psh, '"');
|
---|
467 | }
|
---|
468 |
|
---|
469 | void
|
---|
470 | trargs(shinstance *psh, char **ap)
|
---|
471 | {
|
---|
472 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
473 | RETURN_IF_NOT_TRACING(psh);
|
---|
474 |
|
---|
475 | while (*ap) {
|
---|
476 | trstring(psh, *ap++);
|
---|
477 | if (*ap)
|
---|
478 | trace_char(psh, ' ');
|
---|
479 | else
|
---|
480 | trace_char(psh, '\n');
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | void
|
---|
485 | opentrace(shinstance *psh)
|
---|
486 | {
|
---|
487 | static const char s[] = "./trace";
|
---|
488 |
|
---|
489 | TRY_GET_PSH_OR_RETURN(psh);
|
---|
490 | if (debug(psh) != 1) {
|
---|
491 | /* disabled */
|
---|
492 | if (psh->tracefd != -1) {
|
---|
493 | trace_flush(psh);
|
---|
494 | shfile_close(&psh->fdtab, psh->tracefd);
|
---|
495 | psh->tracefd = -1;
|
---|
496 | }
|
---|
497 | return;
|
---|
498 | }
|
---|
499 | /* else: (re-)enabled */
|
---|
500 |
|
---|
501 | if (psh->tracefd != -1)
|
---|
502 | return;
|
---|
503 |
|
---|
504 | psh->tracefd = shfile_open(&psh->fdtab, s, O_APPEND | O_RDWR | O_CREAT, 0600);
|
---|
505 | if (psh->tracefd != -1) {
|
---|
506 | /* relocate it */
|
---|
507 | int want_fd = 199;
|
---|
508 | while (want_fd > 10)
|
---|
509 | {
|
---|
510 | int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, want_fd);
|
---|
511 | if (fd2 != -1) {
|
---|
512 | shfile_close(&psh->fdtab, psh->tracefd);
|
---|
513 | psh->tracefd = fd2;
|
---|
514 | break;
|
---|
515 | }
|
---|
516 | want_fd = ((want_fd + 1) / 2) - 1;
|
---|
517 | }
|
---|
518 | }
|
---|
519 | if (psh->tracefd == -1) {
|
---|
520 | fprintf(stderr, "Can't open %s\n", s);
|
---|
521 | debug(psh) = 0;
|
---|
522 | return;
|
---|
523 | }
|
---|
524 | trace_string(psh, "Tracing started.\n");
|
---|
525 | }
|
---|
526 |
|
---|
527 | #endif /* DEBUG */
|
---|
528 |
|
---|