show.c revision 1.31 1 1.31 christos /* $NetBSD: show.c,v 1.31 2016/02/28 23:12:23 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.31 christos __RCSID("$NetBSD: show.c,v 1.31 2016/02/28 23:12:23 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.29 christos static int sharg(union node *, FILE *);
67 1.29 christos static int indent(int, char *, FILE *);
68 1.23 christos static void trstring(char *);
69 1.1 cgd
70 1.11 christos void
71 1.23 christos showtree(union node *n)
72 1.8 cgd {
73 1.29 christos FILE *fp;
74 1.29 christos
75 1.29 christos fp = tracefile ? tracefile : stdout;
76 1.29 christos
77 1.29 christos trputs("showtree(");
78 1.29 christos if (n == NULL)
79 1.29 christos trputs("NULL");
80 1.29 christos else if (n == NEOF)
81 1.29 christos trputs("NEOF");
82 1.29 christos trputs(") called\n");
83 1.29 christos if (n != NULL && n != NEOF)
84 1.29 christos shtree(n, 1, 1, NULL, fp);
85 1.1 cgd }
86 1.1 cgd
87 1.1 cgd
88 1.29 christos static int
89 1.29 christos shtree(union node *n, int ind, int nl, char *pfx, FILE *fp)
90 1.8 cgd {
91 1.1 cgd struct nodelist *lp;
92 1.18 pk const char *s;
93 1.29 christos int len;
94 1.1 cgd
95 1.29 christos if (n == NULL) {
96 1.29 christos if (nl)
97 1.29 christos fputc('\n', fp);
98 1.29 christos return 0;
99 1.29 christos }
100 1.9 christos
101 1.29 christos len = indent(ind, pfx, fp);
102 1.29 christos switch (n->type) {
103 1.1 cgd case NSEMI:
104 1.1 cgd s = "; ";
105 1.29 christos len += 2;
106 1.1 cgd goto binop;
107 1.1 cgd case NAND:
108 1.1 cgd s = " && ";
109 1.29 christos len += 4;
110 1.1 cgd goto binop;
111 1.1 cgd case NOR:
112 1.1 cgd s = " || ";
113 1.29 christos len += 4;
114 1.1 cgd binop:
115 1.29 christos len += shtree(n->nbinary.ch1, 0, 0, NULL, fp);
116 1.29 christos fputs(s, fp);
117 1.29 christos if (len >= 60) {
118 1.29 christos putc('\n', fp);
119 1.29 christos len = indent(ind < 0 ? 2 : ind + 1, pfx, fp);
120 1.29 christos }
121 1.29 christos len += shtree(n->nbinary.ch2, 0, nl, NULL, fp);
122 1.1 cgd break;
123 1.1 cgd case NCMD:
124 1.29 christos len += shcmd(n, fp);
125 1.31 christos if (nl && len > 0)
126 1.29 christos len = 0, putc('\n', fp);
127 1.1 cgd break;
128 1.1 cgd case NPIPE:
129 1.1 cgd for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
130 1.29 christos len += shcmd(lp->n, fp);
131 1.29 christos if (lp->next) {
132 1.29 christos len += 3, fputs(" | ", fp);
133 1.29 christos if (len >= 60) {
134 1.29 christos fputc('\n', fp);
135 1.29 christos len = indent(ind < 0 ? 2 : ind + 1,
136 1.29 christos pfx, fp);
137 1.29 christos }
138 1.29 christos }
139 1.1 cgd }
140 1.1 cgd if (n->npipe.backgnd)
141 1.29 christos len += 2, fputs(" &", fp);
142 1.29 christos if (nl || len >= 60)
143 1.29 christos len = 0, fputc('\n', fp);
144 1.1 cgd break;
145 1.1 cgd default:
146 1.29 christos #ifdef NODETYPENAME
147 1.29 christos len += fprintf(fp, "<node type %d [%s]>", n->type,
148 1.29 christos NODETYPENAME(n->type));
149 1.29 christos #else
150 1.29 christos len += fprintf(fp, "<node type %d>", n->type);
151 1.29 christos #endif
152 1.29 christos if (nl)
153 1.29 christos len = 0, putc('\n', fp);
154 1.1 cgd break;
155 1.1 cgd }
156 1.29 christos return len;
157 1.1 cgd }
158 1.1 cgd
159 1.1 cgd
160 1.1 cgd
161 1.29 christos static int
162 1.23 christos shcmd(union node *cmd, FILE *fp)
163 1.11 christos {
164 1.1 cgd union node *np;
165 1.1 cgd int first;
166 1.18 pk const char *s;
167 1.1 cgd int dftfd;
168 1.29 christos int len = 0;
169 1.1 cgd
170 1.1 cgd first = 1;
171 1.1 cgd for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
172 1.1 cgd if (! first)
173 1.29 christos len++, fputc(' ', fp);
174 1.29 christos len += sharg(np, fp);
175 1.1 cgd first = 0;
176 1.1 cgd }
177 1.1 cgd for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
178 1.1 cgd if (! first)
179 1.29 christos len++, fputc(' ', fp);
180 1.1 cgd switch (np->nfile.type) {
181 1.29 christos case NTO: s = ">"; dftfd = 1; len += 1; break;
182 1.29 christos case NCLOBBER: s = ">|"; dftfd = 1; len += 2; break;
183 1.29 christos case NAPPEND: s = ">>"; dftfd = 1; len += 2; break;
184 1.29 christos case NTOFD: s = ">&"; dftfd = 1; len += 2; break;
185 1.29 christos case NFROM: s = "<"; dftfd = 0; len += 1; break;
186 1.29 christos case NFROMFD: s = "<&"; dftfd = 0; len += 2; break;
187 1.29 christos case NFROMTO: s = "<>"; dftfd = 0; len += 2; break;
188 1.31 christos case NXHERE: /* FALLTHROUGH */
189 1.31 christos case NHERE: s = "<<"; dftfd = 0; len += 2; break;
190 1.29 christos default: s = "*error*"; dftfd = 0; len += 7; break;
191 1.1 cgd }
192 1.1 cgd if (np->nfile.fd != dftfd)
193 1.29 christos len += fprintf(fp, "%d", np->nfile.fd);
194 1.1 cgd fputs(s, fp);
195 1.1 cgd if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
196 1.29 christos len += fprintf(fp, "%d", np->ndup.dupfd);
197 1.31 christos } else
198 1.31 christos if (np->nfile.type == NHERE || np->nfile.type == NXHERE) {
199 1.31 christos if (np->nfile.type == NHERE)
200 1.31 christos fputc('\\', fp);
201 1.31 christos fputs("!!!\n", fp);
202 1.31 christos fputs(np->nhere.doc->narg.text, fp);
203 1.31 christos fputs("!!!", fp);
204 1.31 christos len = 3;
205 1.1 cgd } else {
206 1.29 christos len += sharg(np->nfile.fname, fp);
207 1.1 cgd }
208 1.1 cgd first = 0;
209 1.1 cgd }
210 1.29 christos return len;
211 1.1 cgd }
212 1.1 cgd
213 1.1 cgd
214 1.1 cgd
215 1.29 christos static int
216 1.23 christos sharg(union node *arg, FILE *fp)
217 1.23 christos {
218 1.1 cgd char *p;
219 1.1 cgd struct nodelist *bqlist;
220 1.1 cgd int subtype;
221 1.29 christos int len = 0;
222 1.1 cgd
223 1.1 cgd if (arg->type != NARG) {
224 1.29 christos fprintf(fp, "<node type %d>\n", arg->type);
225 1.1 cgd abort();
226 1.1 cgd }
227 1.1 cgd bqlist = arg->narg.backquote;
228 1.1 cgd for (p = arg->narg.text ; *p ; p++) {
229 1.1 cgd switch (*p) {
230 1.1 cgd case CTLESC:
231 1.1 cgd putc(*++p, fp);
232 1.29 christos len++;
233 1.1 cgd break;
234 1.1 cgd case CTLVAR:
235 1.1 cgd putc('$', fp);
236 1.1 cgd putc('{', fp);
237 1.29 christos len += 2;
238 1.1 cgd subtype = *++p;
239 1.9 christos if (subtype == VSLENGTH)
240 1.29 christos len++, putc('#', fp);
241 1.9 christos
242 1.29 christos while (*++p != '=')
243 1.29 christos len++, putc(*p, fp);
244 1.9 christos
245 1.1 cgd if (subtype & VSNUL)
246 1.29 christos len++, putc(':', fp);
247 1.9 christos
248 1.1 cgd switch (subtype & VSTYPE) {
249 1.1 cgd case VSNORMAL:
250 1.1 cgd putc('}', fp);
251 1.29 christos len++;
252 1.1 cgd break;
253 1.1 cgd case VSMINUS:
254 1.1 cgd putc('-', fp);
255 1.29 christos len++;
256 1.1 cgd break;
257 1.1 cgd case VSPLUS:
258 1.1 cgd putc('+', fp);
259 1.29 christos len++;
260 1.1 cgd break;
261 1.1 cgd case VSQUESTION:
262 1.1 cgd putc('?', fp);
263 1.29 christos len++;
264 1.1 cgd break;
265 1.1 cgd case VSASSIGN:
266 1.1 cgd putc('=', fp);
267 1.29 christos len++;
268 1.9 christos break;
269 1.9 christos case VSTRIMLEFT:
270 1.9 christos putc('#', fp);
271 1.29 christos len++;
272 1.9 christos break;
273 1.9 christos case VSTRIMLEFTMAX:
274 1.9 christos putc('#', fp);
275 1.9 christos putc('#', fp);
276 1.29 christos len += 2;
277 1.9 christos break;
278 1.9 christos case VSTRIMRIGHT:
279 1.9 christos putc('%', fp);
280 1.29 christos len++;
281 1.9 christos break;
282 1.9 christos case VSTRIMRIGHTMAX:
283 1.9 christos putc('%', fp);
284 1.9 christos putc('%', fp);
285 1.29 christos len += 2;
286 1.9 christos break;
287 1.9 christos case VSLENGTH:
288 1.1 cgd break;
289 1.1 cgd default:
290 1.29 christos len += fprintf(fp, "<subtype %d>", subtype);
291 1.1 cgd }
292 1.1 cgd break;
293 1.1 cgd case CTLENDVAR:
294 1.1 cgd putc('}', fp);
295 1.29 christos len++;
296 1.1 cgd break;
297 1.1 cgd case CTLBACKQ:
298 1.1 cgd case CTLBACKQ|CTLQUOTE:
299 1.1 cgd putc('$', fp);
300 1.1 cgd putc('(', fp);
301 1.29 christos len += shtree(bqlist->n, -1, 0, NULL, fp) + 3;
302 1.1 cgd putc(')', fp);
303 1.1 cgd break;
304 1.1 cgd default:
305 1.1 cgd putc(*p, fp);
306 1.29 christos len++;
307 1.1 cgd break;
308 1.1 cgd }
309 1.1 cgd }
310 1.29 christos return len;
311 1.1 cgd }
312 1.1 cgd
313 1.1 cgd
314 1.29 christos static int
315 1.23 christos indent(int amount, char *pfx, FILE *fp)
316 1.8 cgd {
317 1.1 cgd int i;
318 1.29 christos int len = 0;
319 1.1 cgd
320 1.29 christos /*
321 1.29 christos * in practice, pfx is **always** NULL
322 1.29 christos * but here, we assume if it were not, at least strlen(pfx) < 8
323 1.29 christos * if that is invalid, output will look messy
324 1.29 christos */
325 1.1 cgd for (i = 0 ; i < amount ; i++) {
326 1.1 cgd if (pfx && i == amount - 1)
327 1.1 cgd fputs(pfx, fp);
328 1.1 cgd putc('\t', fp);
329 1.29 christos len |= 7;
330 1.29 christos len++;
331 1.1 cgd }
332 1.29 christos return len;
333 1.1 cgd }
334 1.1 cgd #endif
335 1.1 cgd
336 1.1 cgd
337 1.1 cgd
338 1.1 cgd /*
339 1.1 cgd * Debugging stuff.
340 1.1 cgd */
341 1.1 cgd
342 1.1 cgd
343 1.1 cgd
344 1.1 cgd
345 1.12 christos #ifdef DEBUG
346 1.8 cgd void
347 1.23 christos trputc(int c)
348 1.8 cgd {
349 1.27 christos if (debug != 1 || !tracefile)
350 1.1 cgd return;
351 1.1 cgd putc(c, tracefile);
352 1.12 christos }
353 1.1 cgd #endif
354 1.1 cgd
355 1.11 christos void
356 1.11 christos trace(const char *fmt, ...)
357 1.11 christos {
358 1.11 christos #ifdef DEBUG
359 1.11 christos va_list va;
360 1.22 wiz
361 1.27 christos if (debug != 1 || !tracefile)
362 1.23 christos return;
363 1.11 christos va_start(va, fmt);
364 1.23 christos (void) vfprintf(tracefile, fmt, va);
365 1.11 christos va_end(va);
366 1.1 cgd #endif
367 1.1 cgd }
368 1.1 cgd
369 1.24 dsl void
370 1.24 dsl tracev(const char *fmt, va_list va)
371 1.24 dsl {
372 1.24 dsl #ifdef DEBUG
373 1.28 christos va_list ap;
374 1.27 christos if (debug != 1 || !tracefile)
375 1.24 dsl return;
376 1.28 christos va_copy(ap, va);
377 1.28 christos (void) vfprintf(tracefile, fmt, ap);
378 1.28 christos va_end(ap);
379 1.24 dsl #endif
380 1.24 dsl }
381 1.24 dsl
382 1.1 cgd
383 1.12 christos #ifdef DEBUG
384 1.7 cgd void
385 1.23 christos trputs(const char *s)
386 1.7 cgd {
387 1.27 christos if (debug != 1 || !tracefile)
388 1.1 cgd return;
389 1.1 cgd fputs(s, tracefile);
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd
393 1.8 cgd static void
394 1.23 christos trstring(char *s)
395 1.8 cgd {
396 1.13 tls char *p;
397 1.1 cgd char c;
398 1.1 cgd
399 1.27 christos if (debug != 1 || !tracefile)
400 1.1 cgd return;
401 1.1 cgd putc('"', tracefile);
402 1.1 cgd for (p = s ; *p ; p++) {
403 1.1 cgd switch (*p) {
404 1.1 cgd case '\n': c = 'n'; goto backslash;
405 1.1 cgd case '\t': c = 't'; goto backslash;
406 1.1 cgd case '\r': c = 'r'; goto backslash;
407 1.1 cgd case '"': c = '"'; goto backslash;
408 1.1 cgd case '\\': c = '\\'; goto backslash;
409 1.1 cgd case CTLESC: c = 'e'; goto backslash;
410 1.1 cgd case CTLVAR: c = 'v'; goto backslash;
411 1.1 cgd case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
412 1.1 cgd case CTLBACKQ: c = 'q'; goto backslash;
413 1.1 cgd case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
414 1.1 cgd backslash: putc('\\', tracefile);
415 1.1 cgd putc(c, tracefile);
416 1.1 cgd break;
417 1.1 cgd default:
418 1.1 cgd if (*p >= ' ' && *p <= '~')
419 1.1 cgd putc(*p, tracefile);
420 1.1 cgd else {
421 1.1 cgd putc('\\', tracefile);
422 1.1 cgd putc(*p >> 6 & 03, tracefile);
423 1.1 cgd putc(*p >> 3 & 07, tracefile);
424 1.1 cgd putc(*p & 07, tracefile);
425 1.1 cgd }
426 1.1 cgd break;
427 1.1 cgd }
428 1.1 cgd }
429 1.1 cgd putc('"', tracefile);
430 1.12 christos }
431 1.1 cgd #endif
432 1.1 cgd
433 1.1 cgd
434 1.7 cgd void
435 1.23 christos trargs(char **ap)
436 1.7 cgd {
437 1.1 cgd #ifdef DEBUG
438 1.27 christos if (debug != 1 || !tracefile)
439 1.1 cgd return;
440 1.1 cgd while (*ap) {
441 1.1 cgd trstring(*ap++);
442 1.1 cgd if (*ap)
443 1.1 cgd putc(' ', tracefile);
444 1.1 cgd else
445 1.1 cgd putc('\n', tracefile);
446 1.1 cgd }
447 1.1 cgd #endif
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd
451 1.12 christos #ifdef DEBUG
452 1.7 cgd void
453 1.23 christos opentrace(void)
454 1.23 christos {
455 1.1 cgd char s[100];
456 1.11 christos #ifdef O_APPEND
457 1.1 cgd int flags;
458 1.11 christos #endif
459 1.1 cgd
460 1.24 dsl if (debug != 1) {
461 1.23 christos if (tracefile)
462 1.23 christos fflush(tracefile);
463 1.23 christos /* leave open because libedit might be using it */
464 1.1 cgd return;
465 1.23 christos }
466 1.5 jtc #ifdef not_this_way
467 1.11 christos {
468 1.11 christos char *p;
469 1.11 christos if ((p = getenv("HOME")) == NULL) {
470 1.11 christos if (geteuid() == 0)
471 1.11 christos p = "/";
472 1.11 christos else
473 1.11 christos p = "/tmp";
474 1.11 christos }
475 1.11 christos scopy(p, s);
476 1.11 christos strcat(s, "/trace");
477 1.1 cgd }
478 1.5 jtc #else
479 1.28 christos snprintf(s, sizeof(s), "./trace.%d", (int)getpid());
480 1.5 jtc #endif /* not_this_way */
481 1.23 christos if (tracefile) {
482 1.23 christos if (!freopen(s, "a", tracefile)) {
483 1.23 christos fprintf(stderr, "Can't re-open %s\n", s);
484 1.27 christos tracefile = NULL;
485 1.23 christos debug = 0;
486 1.23 christos return;
487 1.23 christos }
488 1.23 christos } else {
489 1.23 christos if ((tracefile = fopen(s, "a")) == NULL) {
490 1.23 christos fprintf(stderr, "Can't open %s\n", s);
491 1.23 christos debug = 0;
492 1.23 christos return;
493 1.23 christos }
494 1.1 cgd }
495 1.1 cgd #ifdef O_APPEND
496 1.1 cgd if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
497 1.1 cgd fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
498 1.1 cgd #endif
499 1.23 christos setlinebuf(tracefile);
500 1.1 cgd fputs("\nTracing started.\n", tracefile);
501 1.12 christos }
502 1.5 jtc #endif /* DEBUG */
503