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