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