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