show.c revision 1.11 1 /* $NetBSD: show.c,v 1.11 1995/05/11 21:30:24 christos 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
42 #else
43 static char rcsid[] = "$NetBSD: show.c,v 1.11 1995/05/11 21:30:24 christos Exp $";
44 #endif
45 #endif /* not lint */
46
47 #include <stdio.h>
48 #if __STDC__
49 #include <stdarg.h>
50 #else
51 #include <varargs.h>
52 #endif
53
54 #include "shell.h"
55 #include "parser.h"
56 #include "nodes.h"
57 #include "mystring.h"
58 #include "show.h"
59
60
61 #ifdef DEBUG
62 static void shtree __P((union node *, int, char *, FILE*));
63 static void shcmd __P((union node *, FILE *));
64 static void sharg __P((union node *, FILE *));
65 static void indent __P((int, char *, FILE *));
66 static void trstring __P((char *));
67
68
69 void
70 showtree(n)
71 union node *n;
72 {
73 trputs("showtree called\n");
74 shtree(n, 1, NULL, stdout);
75 }
76
77
78 static void
79 shtree(n, ind, pfx, fp)
80 union node *n;
81 int ind;
82 char *pfx;
83 FILE *fp;
84 {
85 struct nodelist *lp;
86 char *s;
87
88 if (n == NULL)
89 return;
90
91 indent(ind, pfx, fp);
92 switch(n->type) {
93 case NSEMI:
94 s = "; ";
95 goto binop;
96 case NAND:
97 s = " && ";
98 goto binop;
99 case NOR:
100 s = " || ";
101 binop:
102 shtree(n->nbinary.ch1, ind, NULL, fp);
103 /* if (ind < 0) */
104 fputs(s, fp);
105 shtree(n->nbinary.ch2, ind, NULL, fp);
106 break;
107 case NCMD:
108 shcmd(n, fp);
109 if (ind >= 0)
110 putc('\n', fp);
111 break;
112 case NPIPE:
113 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
114 shcmd(lp->n, fp);
115 if (lp->next)
116 fputs(" | ", fp);
117 }
118 if (n->npipe.backgnd)
119 fputs(" &", fp);
120 if (ind >= 0)
121 putc('\n', fp);
122 break;
123 default:
124 fprintf(fp, "<node type %d>", n->type);
125 if (ind >= 0)
126 putc('\n', fp);
127 break;
128 }
129 }
130
131
132
133 static void
134 shcmd(cmd, fp)
135 union node *cmd;
136 FILE *fp;
137 {
138 union node *np;
139 int first;
140 char *s;
141 int dftfd;
142
143 first = 1;
144 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
145 if (! first)
146 putchar(' ');
147 sharg(np, fp);
148 first = 0;
149 }
150 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
151 if (! first)
152 putchar(' ');
153 switch (np->nfile.type) {
154 case NTO: s = ">"; dftfd = 1; break;
155 case NAPPEND: s = ">>"; dftfd = 1; break;
156 case NTOFD: s = ">&"; dftfd = 1; break;
157 case NFROM: s = "<"; dftfd = 0; break;
158 case NFROMFD: s = "<&"; dftfd = 0; break;
159 default: s = "*error*"; dftfd = 0; break;
160 }
161 if (np->nfile.fd != dftfd)
162 fprintf(fp, "%d", np->nfile.fd);
163 fputs(s, fp);
164 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
165 fprintf(fp, "%d", np->ndup.dupfd);
166 } else {
167 sharg(np->nfile.fname, fp);
168 }
169 first = 0;
170 }
171 }
172
173
174
175 static void
176 sharg(arg, fp)
177 union node *arg;
178 FILE *fp;
179 {
180 char *p;
181 struct nodelist *bqlist;
182 int subtype;
183
184 if (arg->type != NARG) {
185 printf("<node type %d>\n", arg->type);
186 fflush(stdout);
187 abort();
188 }
189 bqlist = arg->narg.backquote;
190 for (p = arg->narg.text ; *p ; p++) {
191 switch (*p) {
192 case CTLESC:
193 putc(*++p, fp);
194 break;
195 case CTLVAR:
196 putc('$', fp);
197 putc('{', fp);
198 subtype = *++p;
199 if (subtype == VSLENGTH)
200 putc('#', fp);
201
202 while (*p != '=')
203 putc(*p++, fp);
204
205 if (subtype & VSNUL)
206 putc(':', fp);
207
208 switch (subtype & VSTYPE) {
209 case VSNORMAL:
210 putc('}', fp);
211 break;
212 case VSMINUS:
213 putc('-', fp);
214 break;
215 case VSPLUS:
216 putc('+', fp);
217 break;
218 case VSQUESTION:
219 putc('?', fp);
220 break;
221 case VSASSIGN:
222 putc('=', fp);
223 break;
224 case VSTRIMLEFT:
225 putc('#', fp);
226 break;
227 case VSTRIMLEFTMAX:
228 putc('#', fp);
229 putc('#', fp);
230 break;
231 case VSTRIMRIGHT:
232 putc('%', fp);
233 break;
234 case VSTRIMRIGHTMAX:
235 putc('%', fp);
236 putc('%', fp);
237 break;
238 case VSLENGTH:
239 break;
240 default:
241 printf("<subtype %d>", subtype);
242 }
243 break;
244 case CTLENDVAR:
245 putc('}', fp);
246 break;
247 case CTLBACKQ:
248 case CTLBACKQ|CTLQUOTE:
249 putc('$', fp);
250 putc('(', fp);
251 shtree(bqlist->n, -1, NULL, fp);
252 putc(')', fp);
253 break;
254 default:
255 putc(*p, fp);
256 break;
257 }
258 }
259 }
260
261
262 static void
263 indent(amount, pfx, fp)
264 int amount;
265 char *pfx;
266 FILE *fp;
267 {
268 int i;
269
270 for (i = 0 ; i < amount ; i++) {
271 if (pfx && i == amount - 1)
272 fputs(pfx, fp);
273 putc('\t', fp);
274 }
275 }
276 #endif
277
278
279
280 /*
281 * Debugging stuff.
282 */
283
284
285 FILE *tracefile;
286
287 #if DEBUG == 2
288 int debug = 1;
289 #else
290 int debug = 0;
291 #endif
292
293
294 void
295 trputc(c)
296 int c;
297 {
298 #ifdef DEBUG
299 if (tracefile == NULL)
300 return;
301 putc(c, tracefile);
302 if (c == '\n')
303 fflush(tracefile);
304 #endif
305 }
306
307 void
308 #if __STDC__
309 trace(const char *fmt, ...)
310 #else
311 trace(va_alist)
312 va_dcl
313 #endif
314 {
315 #ifdef DEBUG
316 va_list va;
317 #if __STDC__
318 va_start(va, fmt);
319 #else
320 char *fmt;
321 va_start(va);
322 fmt = va_arg(va, char *);
323 #endif
324 if (tracefile != NULL) {
325 (void) vfprintf(tracefile, fmt, va);
326 if (strchr(fmt, '\n'))
327 (void) fflush(tracefile);
328 }
329 va_end(va);
330 #endif
331 }
332
333
334 void
335 trputs(s)
336 char *s;
337 {
338 #ifdef DEBUG
339 if (tracefile == NULL)
340 return;
341 fputs(s, tracefile);
342 if (strchr(s, '\n'))
343 fflush(tracefile);
344 #endif
345 }
346
347
348 static void
349 trstring(s)
350 char *s;
351 {
352 register char *p;
353 char c;
354
355 #ifdef DEBUG
356 if (tracefile == NULL)
357 return;
358 putc('"', tracefile);
359 for (p = s ; *p ; p++) {
360 switch (*p) {
361 case '\n': c = 'n'; goto backslash;
362 case '\t': c = 't'; goto backslash;
363 case '\r': c = 'r'; goto backslash;
364 case '"': c = '"'; goto backslash;
365 case '\\': c = '\\'; goto backslash;
366 case CTLESC: c = 'e'; goto backslash;
367 case CTLVAR: c = 'v'; goto backslash;
368 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
369 case CTLBACKQ: c = 'q'; goto backslash;
370 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
371 backslash: putc('\\', tracefile);
372 putc(c, tracefile);
373 break;
374 default:
375 if (*p >= ' ' && *p <= '~')
376 putc(*p, tracefile);
377 else {
378 putc('\\', tracefile);
379 putc(*p >> 6 & 03, tracefile);
380 putc(*p >> 3 & 07, tracefile);
381 putc(*p & 07, tracefile);
382 }
383 break;
384 }
385 }
386 putc('"', tracefile);
387 #endif
388 }
389
390
391 void
392 trargs(ap)
393 char **ap;
394 {
395 #ifdef DEBUG
396 if (tracefile == NULL)
397 return;
398 while (*ap) {
399 trstring(*ap++);
400 if (*ap)
401 putc(' ', tracefile);
402 else
403 putc('\n', tracefile);
404 }
405 fflush(tracefile);
406 #endif
407 }
408
409
410 void
411 opentrace() {
412 char s[100];
413 char *getenv();
414 #ifdef O_APPEND
415 int flags;
416 #endif
417
418 #ifdef DEBUG
419 if (!debug)
420 return;
421 #ifdef not_this_way
422 {
423 char *p;
424 if ((p = getenv("HOME")) == NULL) {
425 if (geteuid() == 0)
426 p = "/";
427 else
428 p = "/tmp";
429 }
430 scopy(p, s);
431 strcat(s, "/trace");
432 }
433 #else
434 scopy("./trace", s);
435 #endif /* not_this_way */
436 if ((tracefile = fopen(s, "a")) == NULL) {
437 fprintf(stderr, "Can't open %s\n", s);
438 return;
439 }
440 #ifdef O_APPEND
441 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
442 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
443 #endif
444 fputs("\nTracing started.\n", tracefile);
445 fflush(tracefile);
446 #endif /* DEBUG */
447 }
448