show.c revision 1.32 1 1.32 christos /* $NetBSD: show.c,v 1.32 2016/02/29 23:52:04 christos Exp $ */
2 1.10 cgd
3 1.1 cgd /*-
4 1.5 jtc * Copyright (c) 1991, 1993
5 1.5 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Kenneth Almquist.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.25 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.15 christos #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.10 cgd #if 0
38 1.11 christos static char sccsid[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
39 1.10 cgd #else
40 1.32 christos __RCSID("$NetBSD: show.c,v 1.32 2016/02/29 23:52:04 christos Exp $");
41 1.10 cgd #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <stdio.h>
45 1.11 christos #include <stdarg.h>
46 1.26 dsl #include <stdlib.h>
47 1.28 christos #include <unistd.h>
48 1.11 christos
49 1.1 cgd #include "shell.h"
50 1.1 cgd #include "parser.h"
51 1.1 cgd #include "nodes.h"
52 1.1 cgd #include "mystring.h"
53 1.11 christos #include "show.h"
54 1.23 christos #include "options.h"
55 1.30 christos #ifndef SMALL
56 1.30 christos #define DEFINE_NODENAMES
57 1.30 christos #include "nodenames.h"
58 1.30 christos #endif
59 1.1 cgd
60 1.1 cgd
61 1.29 christos FILE *tracefile;
62 1.29 christos
63 1.1 cgd #ifdef DEBUG
64 1.29 christos static int shtree(union node *, int, int, char *, FILE*);
65 1.29 christos static int shcmd(union node *, FILE *);
66 1.32 christos static int shsubsh(union node *, FILE *);
67 1.32 christos static int shredir(union node *, FILE *, int);
68 1.29 christos static int sharg(union node *, FILE *);
69 1.29 christos static int indent(int, char *, FILE *);
70 1.23 christos static void trstring(char *);
71 1.1 cgd
72 1.11 christos void
73 1.23 christos showtree(union node *n)
74 1.8 cgd {
75 1.29 christos FILE *fp;
76 1.29 christos
77 1.29 christos fp = tracefile ? tracefile : stdout;
78 1.29 christos
79 1.29 christos trputs("showtree(");
80 1.29 christos if (n == NULL)
81 1.29 christos trputs("NULL");
82 1.29 christos else if (n == NEOF)
83 1.29 christos trputs("NEOF");
84 1.29 christos trputs(") called\n");
85 1.29 christos if (n != NULL && n != NEOF)
86 1.29 christos shtree(n, 1, 1, NULL, fp);
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd
90 1.29 christos static int
91 1.29 christos shtree(union node *n, int ind, int nl, char *pfx, FILE *fp)
92 1.8 cgd {
93 1.1 cgd struct nodelist *lp;
94 1.18 pk const char *s;
95 1.29 christos int len;
96 1.1 cgd
97 1.29 christos if (n == NULL) {
98 1.29 christos if (nl)
99 1.29 christos fputc('\n', fp);
100 1.29 christos return 0;
101 1.29 christos }
102 1.9 christos
103 1.29 christos len = indent(ind, pfx, fp);
104 1.29 christos switch (n->type) {
105 1.1 cgd case NSEMI:
106 1.1 cgd s = "; ";
107 1.29 christos len += 2;
108 1.1 cgd goto binop;
109 1.1 cgd case NAND:
110 1.1 cgd s = " && ";
111 1.29 christos len += 4;
112 1.1 cgd goto binop;
113 1.1 cgd case NOR:
114 1.1 cgd s = " || ";
115 1.29 christos len += 4;
116 1.1 cgd binop:
117 1.29 christos len += shtree(n->nbinary.ch1, 0, 0, NULL, fp);
118 1.29 christos fputs(s, fp);
119 1.29 christos if (len >= 60) {
120 1.29 christos putc('\n', fp);
121 1.29 christos len = indent(ind < 0 ? 2 : ind + 1, pfx, fp);
122 1.29 christos }
123 1.29 christos len += shtree(n->nbinary.ch2, 0, nl, NULL, fp);
124 1.1 cgd break;
125 1.1 cgd case NCMD:
126 1.29 christos len += shcmd(n, fp);
127 1.31 christos if (nl && len > 0)
128 1.29 christos len = 0, putc('\n', fp);
129 1.1 cgd break;
130 1.1 cgd case NPIPE:
131 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
132 1.29 christos len += shcmd(lp->n, fp);
133 1.29 christos if (lp->next) {
134 1.29 christos len += 3, fputs(" | ", fp);
135 1.29 christos if (len >= 60) {
136 1.29 christos fputc('\n', fp);
137 1.29 christos len = indent(ind < 0 ? 2 : ind + 1,
138 1.29 christos pfx, fp);
139 1.29 christos }
140 1.29 christos }
141 1.1 cgd }
142 1.1 cgd if (n->npipe.backgnd)
143 1.29 christos len += 2, fputs(" &", fp);
144 1.29 christos if (nl || len >= 60)
145 1.29 christos len = 0, fputc('\n', fp);
146 1.1 cgd break;
147 1.32 christos case NSUBSHELL:
148 1.32 christos len += shsubsh(n, fp);
149 1.32 christos if (nl && len > 0)
150 1.32 christos len = 0, putc('\n', fp);
151 1.32 christos break;
152 1.1 cgd default:
153 1.29 christos #ifdef NODETYPENAME
154 1.29 christos len += fprintf(fp, "<node type %d [%s]>", n->type,
155 1.29 christos NODETYPENAME(n->type));
156 1.29 christos #else
157 1.29 christos len += fprintf(fp, "<node type %d>", n->type);
158 1.29 christos #endif
159 1.29 christos if (nl)
160 1.29 christos len = 0, putc('\n', fp);
161 1.1 cgd break;
162 1.1 cgd }
163 1.29 christos return len;
164 1.1 cgd }
165 1.1 cgd
166 1.1 cgd
167 1.1 cgd
168 1.29 christos static int
169 1.23 christos shcmd(union node *cmd, FILE *fp)
170 1.11 christos {
171 1.1 cgd union node *np;
172 1.1 cgd int first;
173 1.18 pk const char *s;
174 1.1 cgd int dftfd;
175 1.29 christos int len = 0;
176 1.1 cgd
177 1.1 cgd first = 1;
178 1.1 cgd for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
179 1.1 cgd if (! first)
180 1.29 christos len++, fputc(' ', fp);
181 1.29 christos len += sharg(np, fp);
182 1.1 cgd first = 0;
183 1.1 cgd }
184 1.32 christos return len + shredir(cmd, fp, first);
185 1.32 christos }
186 1.32 christos
187 1.32 christos static int
188 1.32 christos shsubsh(union node *cmd, FILE *fp)
189 1.32 christos {
190 1.32 christos int len = 6;
191 1.32 christos
192 1.32 christos fputs(" ( ", fp);
193 1.32 christos len += shtree(cmd->nredir.n, -1, 0, NULL, fp);
194 1.32 christos fputs(" ) ", fp);
195 1.32 christos len += shredir(cmd, fp, 1);
196 1.32 christos
197 1.32 christos return len;
198 1.32 christos }
199 1.32 christos
200 1.32 christos static int
201 1.32 christos shredir(union node *cmd, FILE *fp, int first)
202 1.32 christos {
203 1.32 christos union node *np;
204 1.32 christos const char *s;
205 1.32 christos int dftfd;
206 1.32 christos int len = 0;
207 1.32 christos char buf[106];
208 1.32 christos
209 1.1 cgd for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
210 1.1 cgd if (! first)
211 1.29 christos len++, fputc(' ', fp);
212 1.1 cgd switch (np->nfile.type) {
213 1.29 christos case NTO: s = ">"; dftfd = 1; len += 1; break;
214 1.29 christos case NCLOBBER: s = ">|"; dftfd = 1; len += 2; break;
215 1.29 christos case NAPPEND: s = ">>"; dftfd = 1; len += 2; break;
216 1.29 christos case NTOFD: s = ">&"; dftfd = 1; len += 2; break;
217 1.29 christos case NFROM: s = "<"; dftfd = 0; len += 1; break;
218 1.29 christos case NFROMFD: s = "<&"; dftfd = 0; len += 2; break;
219 1.29 christos case NFROMTO: s = "<>"; dftfd = 0; len += 2; break;
220 1.31 christos case NXHERE: /* FALLTHROUGH */
221 1.31 christos case NHERE: s = "<<"; dftfd = 0; len += 2; break;
222 1.29 christos default: s = "*error*"; dftfd = 0; len += 7; break;
223 1.1 cgd }
224 1.1 cgd if (np->nfile.fd != dftfd)
225 1.29 christos len += fprintf(fp, "%d", np->nfile.fd);
226 1.1 cgd fputs(s, fp);
227 1.1 cgd if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
228 1.29 christos len += fprintf(fp, "%d", np->ndup.dupfd);
229 1.31 christos } else
230 1.31 christos if (np->nfile.type == NHERE || np->nfile.type == NXHERE) {
231 1.31 christos if (np->nfile.type == NHERE)
232 1.31 christos fputc('\\', fp);
233 1.31 christos fputs("!!!\n", fp);
234 1.32 christos s = np->nhere.doc->narg.text;
235 1.32 christos if (strlen(s) > 100) {
236 1.32 christos memmove(buf, s, 100);
237 1.32 christos buf[100] = '\0';
238 1.32 christos strcat(buf, " ...");
239 1.32 christos s = buf;
240 1.32 christos }
241 1.32 christos fputs(s, fp);
242 1.31 christos fputs("!!!", fp);
243 1.31 christos len = 3;
244 1.1 cgd } else {
245 1.29 christos len += sharg(np->nfile.fname, fp);
246 1.1 cgd }
247 1.1 cgd first = 0;
248 1.1 cgd }
249 1.29 christos return len;
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd
253 1.1 cgd
254 1.29 christos static int
255 1.23 christos sharg(union node *arg, FILE *fp)
256 1.23 christos {
257 1.1 cgd char *p;
258 1.1 cgd struct nodelist *bqlist;
259 1.1 cgd int subtype;
260 1.29 christos int len = 0;
261 1.1 cgd
262 1.1 cgd if (arg->type != NARG) {
263 1.29 christos fprintf(fp, "<node type %d>\n", arg->type);
264 1.1 cgd abort();
265 1.1 cgd }
266 1.1 cgd bqlist = arg->narg.backquote;
267 1.1 cgd for (p = arg->narg.text ; *p ; p++) {
268 1.1 cgd switch (*p) {
269 1.1 cgd case CTLESC:
270 1.1 cgd putc(*++p, fp);
271 1.29 christos len++;
272 1.1 cgd break;
273 1.1 cgd case CTLVAR:
274 1.1 cgd putc('$', fp);
275 1.1 cgd putc('{', fp);
276 1.29 christos len += 2;
277 1.1 cgd subtype = *++p;
278 1.9 christos if (subtype == VSLENGTH)
279 1.29 christos len++, putc('#', fp);
280 1.9 christos
281 1.29 christos while (*++p != '=')
282 1.29 christos len++, putc(*p, fp);
283 1.9 christos
284 1.1 cgd if (subtype & VSNUL)
285 1.29 christos len++, putc(':', fp);
286 1.9 christos
287 1.1 cgd switch (subtype & VSTYPE) {
288 1.1 cgd case VSNORMAL:
289 1.1 cgd putc('}', fp);
290 1.29 christos len++;
291 1.1 cgd break;
292 1.1 cgd case VSMINUS:
293 1.1 cgd putc('-', fp);
294 1.29 christos len++;
295 1.1 cgd break;
296 1.1 cgd case VSPLUS:
297 1.1 cgd putc('+', fp);
298 1.29 christos len++;
299 1.1 cgd break;
300 1.1 cgd case VSQUESTION:
301 1.1 cgd putc('?', fp);
302 1.29 christos len++;
303 1.1 cgd break;
304 1.1 cgd case VSASSIGN:
305 1.1 cgd putc('=', fp);
306 1.29 christos len++;
307 1.9 christos break;
308 1.9 christos case VSTRIMLEFT:
309 1.9 christos putc('#', fp);
310 1.29 christos len++;
311 1.9 christos break;
312 1.9 christos case VSTRIMLEFTMAX:
313 1.9 christos putc('#', fp);
314 1.9 christos putc('#', fp);
315 1.29 christos len += 2;
316 1.9 christos break;
317 1.9 christos case VSTRIMRIGHT:
318 1.9 christos putc('%', fp);
319 1.29 christos len++;
320 1.9 christos break;
321 1.9 christos case VSTRIMRIGHTMAX:
322 1.9 christos putc('%', fp);
323 1.9 christos putc('%', fp);
324 1.29 christos len += 2;
325 1.9 christos break;
326 1.9 christos case VSLENGTH:
327 1.1 cgd break;
328 1.1 cgd default:
329 1.29 christos len += fprintf(fp, "<subtype %d>", subtype);
330 1.1 cgd }
331 1.1 cgd break;
332 1.1 cgd case CTLENDVAR:
333 1.1 cgd putc('}', fp);
334 1.29 christos len++;
335 1.1 cgd break;
336 1.1 cgd case CTLBACKQ:
337 1.1 cgd case CTLBACKQ|CTLQUOTE:
338 1.1 cgd putc('$', fp);
339 1.1 cgd putc('(', fp);
340 1.29 christos len += shtree(bqlist->n, -1, 0, NULL, fp) + 3;
341 1.1 cgd putc(')', fp);
342 1.1 cgd break;
343 1.1 cgd default:
344 1.1 cgd putc(*p, fp);
345 1.29 christos len++;
346 1.1 cgd break;
347 1.1 cgd }
348 1.1 cgd }
349 1.29 christos return len;
350 1.1 cgd }
351 1.1 cgd
352 1.1 cgd
353 1.29 christos static int
354 1.23 christos indent(int amount, char *pfx, FILE *fp)
355 1.8 cgd {
356 1.1 cgd int i;
357 1.29 christos int len = 0;
358 1.1 cgd
359 1.29 christos /*
360 1.29 christos * in practice, pfx is **always** NULL
361 1.29 christos * but here, we assume if it were not, at least strlen(pfx) < 8
362 1.29 christos * if that is invalid, output will look messy
363 1.29 christos */
364 1.1 cgd for (i = 0 ; i < amount ; i++) {
365 1.1 cgd if (pfx && i == amount - 1)
366 1.1 cgd fputs(pfx, fp);
367 1.1 cgd putc('\t', fp);
368 1.29 christos len |= 7;
369 1.29 christos len++;
370 1.1 cgd }
371 1.29 christos return len;
372 1.1 cgd }
373 1.1 cgd #endif
374 1.1 cgd
375 1.1 cgd
376 1.1 cgd
377 1.1 cgd /*
378 1.1 cgd * Debugging stuff.
379 1.1 cgd */
380 1.1 cgd
381 1.1 cgd
382 1.1 cgd
383 1.1 cgd
384 1.12 christos #ifdef DEBUG
385 1.8 cgd void
386 1.23 christos trputc(int c)
387 1.8 cgd {
388 1.27 christos if (debug != 1 || !tracefile)
389 1.1 cgd return;
390 1.1 cgd putc(c, tracefile);
391 1.12 christos }
392 1.1 cgd #endif
393 1.1 cgd
394 1.11 christos void
395 1.11 christos trace(const char *fmt, ...)
396 1.11 christos {
397 1.11 christos #ifdef DEBUG
398 1.11 christos va_list va;
399 1.22 wiz
400 1.27 christos if (debug != 1 || !tracefile)
401 1.23 christos return;
402 1.11 christos va_start(va, fmt);
403 1.23 christos (void) vfprintf(tracefile, fmt, va);
404 1.11 christos va_end(va);
405 1.1 cgd #endif
406 1.1 cgd }
407 1.1 cgd
408 1.24 dsl void
409 1.24 dsl tracev(const char *fmt, va_list va)
410 1.24 dsl {
411 1.24 dsl #ifdef DEBUG
412 1.28 christos va_list ap;
413 1.27 christos if (debug != 1 || !tracefile)
414 1.24 dsl return;
415 1.28 christos va_copy(ap, va);
416 1.28 christos (void) vfprintf(tracefile, fmt, ap);
417 1.28 christos va_end(ap);
418 1.24 dsl #endif
419 1.24 dsl }
420 1.24 dsl
421 1.1 cgd
422 1.12 christos #ifdef DEBUG
423 1.7 cgd void
424 1.23 christos trputs(const char *s)
425 1.7 cgd {
426 1.27 christos if (debug != 1 || !tracefile)
427 1.1 cgd return;
428 1.1 cgd fputs(s, tracefile);
429 1.1 cgd }
430 1.1 cgd
431 1.1 cgd
432 1.8 cgd static void
433 1.23 christos trstring(char *s)
434 1.8 cgd {
435 1.13 tls char *p;
436 1.1 cgd char c;
437 1.1 cgd
438 1.27 christos if (debug != 1 || !tracefile)
439 1.1 cgd return;
440 1.1 cgd putc('"', tracefile);
441 1.1 cgd for (p = s ; *p ; p++) {
442 1.1 cgd switch (*p) {
443 1.1 cgd case '\n': c = 'n'; goto backslash;
444 1.1 cgd case '\t': c = 't'; goto backslash;
445 1.1 cgd case '\r': c = 'r'; goto backslash;
446 1.1 cgd case '"': c = '"'; goto backslash;
447 1.1 cgd case '\\': c = '\\'; goto backslash;
448 1.1 cgd case CTLESC: c = 'e'; goto backslash;
449 1.1 cgd case CTLVAR: c = 'v'; goto backslash;
450 1.1 cgd case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
451 1.1 cgd case CTLBACKQ: c = 'q'; goto backslash;
452 1.1 cgd case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
453 1.1 cgd backslash: putc('\\', tracefile);
454 1.1 cgd putc(c, tracefile);
455 1.1 cgd break;
456 1.1 cgd default:
457 1.1 cgd if (*p >= ' ' && *p <= '~')
458 1.1 cgd putc(*p, tracefile);
459 1.1 cgd else {
460 1.1 cgd putc('\\', tracefile);
461 1.1 cgd putc(*p >> 6 & 03, tracefile);
462 1.1 cgd putc(*p >> 3 & 07, tracefile);
463 1.1 cgd putc(*p & 07, tracefile);
464 1.1 cgd }
465 1.1 cgd break;
466 1.1 cgd }
467 1.1 cgd }
468 1.1 cgd putc('"', tracefile);
469 1.12 christos }
470 1.1 cgd #endif
471 1.1 cgd
472 1.1 cgd
473 1.7 cgd void
474 1.23 christos trargs(char **ap)
475 1.7 cgd {
476 1.1 cgd #ifdef DEBUG
477 1.27 christos if (debug != 1 || !tracefile)
478 1.1 cgd return;
479 1.1 cgd while (*ap) {
480 1.1 cgd trstring(*ap++);
481 1.1 cgd if (*ap)
482 1.1 cgd putc(' ', tracefile);
483 1.1 cgd else
484 1.1 cgd putc('\n', tracefile);
485 1.1 cgd }
486 1.1 cgd #endif
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd
490 1.12 christos #ifdef DEBUG
491 1.7 cgd void
492 1.23 christos opentrace(void)
493 1.23 christos {
494 1.1 cgd char s[100];
495 1.11 christos #ifdef O_APPEND
496 1.1 cgd int flags;
497 1.11 christos #endif
498 1.1 cgd
499 1.24 dsl if (debug != 1) {
500 1.23 christos if (tracefile)
501 1.23 christos fflush(tracefile);
502 1.23 christos /* leave open because libedit might be using it */
503 1.1 cgd return;
504 1.23 christos }
505 1.5 jtc #ifdef not_this_way
506 1.11 christos {
507 1.11 christos char *p;
508 1.11 christos if ((p = getenv("HOME")) == NULL) {
509 1.11 christos if (geteuid() == 0)
510 1.11 christos p = "/";
511 1.11 christos else
512 1.11 christos p = "/tmp";
513 1.11 christos }
514 1.11 christos scopy(p, s);
515 1.11 christos strcat(s, "/trace");
516 1.1 cgd }
517 1.5 jtc #else
518 1.28 christos snprintf(s, sizeof(s), "./trace.%d", (int)getpid());
519 1.5 jtc #endif /* not_this_way */
520 1.23 christos if (tracefile) {
521 1.23 christos if (!freopen(s, "a", tracefile)) {
522 1.23 christos fprintf(stderr, "Can't re-open %s\n", s);
523 1.27 christos tracefile = NULL;
524 1.23 christos debug = 0;
525 1.23 christos return;
526 1.23 christos }
527 1.23 christos } else {
528 1.23 christos if ((tracefile = fopen(s, "a")) == NULL) {
529 1.23 christos fprintf(stderr, "Can't open %s\n", s);
530 1.23 christos debug = 0;
531 1.23 christos return;
532 1.23 christos }
533 1.1 cgd }
534 1.1 cgd #ifdef O_APPEND
535 1.1 cgd if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
536 1.1 cgd fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
537 1.1 cgd #endif
538 1.23 christos setlinebuf(tracefile);
539 1.1 cgd fputs("\nTracing started.\n", tracefile);
540 1.12 christos }
541 1.5 jtc #endif /* DEBUG */
542