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.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
47 static void shtree(union node *, int, char *, FILE*);
48 static void shcmd(union node *, FILE *);
49 static void sharg(union node *, FILE *);
50 static void indent(int, char *, FILE *);
51 static void trstring(char *);
55 showtree(union node *n)
57 trputs("showtree called\n");
58 shtree(n, 1, NULL, stdout);
63 shtree(union node *n, int ind, char *pfx, FILE *fp)
82 shtree(n->nbinary.ch1, ind, NULL, fp);
85 shtree(n->nbinary.ch2, ind, NULL, fp);
93 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
104 fprintf(fp, "<node type %d>", n->type);
114 shcmd(union node *cmd, FILE *fp)
122 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
128 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
131 switch (np->nfile.type) {
132 case NTO: s = ">"; dftfd = 1; break;
133 case NCLOBBER: s = ">|"; dftfd = 1; break;
134 case NAPPEND: s = ">>"; dftfd = 1; break;
135 case NTOFD: s = ">&"; dftfd = 1; break;
136 case NFROM: s = "<"; dftfd = 0; break;
137 case NFROMFD: s = "<&"; dftfd = 0; break;
138 case NFROMTO: s = "<>"; dftfd = 0; break;
139 default: s = "*error*"; dftfd = 0; break;
141 if (np->nfile.fd != dftfd)
142 fprintf(fp, "%d", np->nfile.fd);
144 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
145 fprintf(fp, "%d", np->ndup.dupfd);
147 sharg(np->nfile.fname, fp);
156 sharg(union node *arg, FILE *fp)
159 struct nodelist *bqlist;
162 if (arg->type != NARG) {
163 printf("<node type %d>\n", arg->type);
166 bqlist = arg->narg.backquote;
167 for (p = arg->narg.text ; *p ; p++) {
168 switch ((signed char)*p) {
176 if (subtype == VSLENGTH)
185 switch (subtype & VSTYPE) {
218 printf("<subtype %d>", subtype);
227 shtree(bqlist->n, -1, NULL, fp);
239 indent(int amount, char *pfx, FILE *fp)
243 for (i = 0 ; i < amount ; i++) {
244 if (pfx && i == amount - 1)
269 trace(const char *fmt, ...)
276 (void) vfprintf(tracefile, fmt, va);
281 tracev(const char *fmt, va_list va)
285 (void) vfprintf(tracefile, fmt, va);
290 trputs(const char *s)
306 putc('"', tracefile);
307 for (p = s ; *p ; p++) {
308 switch ((signed char)*p) {
309 case '\n': c = 'n'; goto backslash;
310 case '\t': c = 't'; goto backslash;
311 case '\r': c = 'r'; goto backslash;
312 case '"': c = '"'; goto backslash;
313 case '\\': c = '\\'; goto backslash;
314 case CTLESC: c = 'e'; goto backslash;
315 case CTLVAR: c = 'v'; goto backslash;
316 case CTLBACKQ: c = 'q'; goto backslash;
317 backslash: putc('\\', tracefile);
321 if (*p >= ' ' && *p <= '~')
324 putc('\\', tracefile);
325 putc(*p >> 6 & 03, tracefile);
326 putc(*p >> 3 & 07, tracefile);
327 putc(*p & 07, tracefile);
332 putc('"', tracefile);
344 putc(' ', tracefile);
346 putc('\n', tracefile);
362 /* leave open because libedit might be using it */
368 if ((p = getenv(homestr)) == NULL) {
379 #endif /* not_this_way */
382 if (!freopen(s, "a", tracefile)) {
384 if (!(!fclose(tracefile) && (tracefile = fopen(s, "a")))) {
385 #endif /* __KLIBC__ */
386 fprintf(stderr, "Can't re-open %s\n", s);
391 if ((tracefile = fopen(s, "a")) == NULL) {
392 fprintf(stderr, "Can't open %s\n", s);
398 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
399 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
402 setlinebuf(tracefile);
403 #endif /* __KLIBC__ */
404 fputs("\nTracing started.\n", tracefile);