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