2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. 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
49 static void shtree(union node *, int, char *, FILE*);
50 static void shcmd(union node *, FILE *);
51 static void sharg(union node *, FILE *);
52 static void indent(int, char *, FILE *);
53 static void trstring(char *);
57 showtree(union node *n)
59 trputs("showtree called\n");
60 shtree(n, 1, NULL, stdout);
65 shtree(union node *n, int ind, char *pfx, FILE *fp)
84 shtree(n->nbinary.ch1, ind, NULL, fp);
87 shtree(n->nbinary.ch2, ind, NULL, fp);
95 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
100 if (n->npipe.backgnd)
106 fprintf(fp, "<node type %d>", n->type);
116 shcmd(union node *cmd, FILE *fp)
124 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
130 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
133 switch (np->nfile.type) {
134 case NTO: s = ">"; dftfd = 1; break;
135 case NAPPEND: s = ">>"; dftfd = 1; break;
136 case NTOFD: s = ">&"; dftfd = 1; break;
137 case NCLOBBER: s = ">|"; dftfd = 1; break;
138 case NFROM: s = "<"; dftfd = 0; break;
139 case NFROMTO: s = "<>"; dftfd = 0; break;
140 case NFROMFD: s = "<&"; dftfd = 0; break;
141 case NHERE: s = "<<"; dftfd = 0; break;
142 case NXHERE: s = "<<"; dftfd = 0; break;
143 default: s = "*error*"; dftfd = 0; break;
145 if (np->nfile.fd != dftfd)
146 fprintf(fp, "%d", np->nfile.fd);
148 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
149 if (np->ndup.dupfd >= 0)
150 fprintf(fp, "%d", np->ndup.dupfd);
153 } else if (np->nfile.type == NHERE) {
155 } else if (np->nfile.type == NXHERE) {
156 fprintf(fp, "XHERE");
158 sharg(np->nfile.fname, fp);
167 sharg(union node *arg, FILE *fp)
170 struct nodelist *bqlist;
173 if (arg->type != NARG) {
174 printf("<node type %d>\n", arg->type);
178 bqlist = arg->narg.backquote;
179 for (p = arg->narg.text ; *p ; p++) {
188 if (subtype == VSLENGTH)
197 switch (subtype & VSTYPE) {
230 printf("<subtype %d>", subtype);
237 case CTLBACKQ|CTLQUOTE:
240 shtree(bqlist->n, -1, NULL, fp);
252 indent(int amount, char *pfx, FILE *fp)
256 for (i = 0 ; i < amount ; i++) {
257 if (pfx && i == amount - 1)
269 static FILE *tracefile;
280 if (tracefile == NULL)
289 sh_trace(const char *fmt, ...)
293 if (tracefile != NULL) {
294 (void) vfprintf(tracefile, fmt, va);
295 if (strchr(fmt, '\n'))
296 (void) fflush(tracefile);
303 trputs(const char *s)
305 if (tracefile == NULL)
319 if (tracefile == NULL)
321 putc('"', tracefile);
322 for (p = s ; *p ; p++) {
324 case '\n': c = 'n'; goto backslash;
325 case '\t': c = 't'; goto backslash;
326 case '\r': c = 'r'; goto backslash;
327 case '"': c = '"'; goto backslash;
328 case '\\': c = '\\'; goto backslash;
329 case CTLESC: c = 'e'; goto backslash;
330 case CTLVAR: c = 'v'; goto backslash;
331 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
332 case CTLBACKQ: c = 'q'; goto backslash;
333 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
334 backslash: putc('\\', tracefile);
338 if (*p >= ' ' && *p <= '~')
341 putc('\\', tracefile);
342 putc(*p >> 6 & 03, tracefile);
343 putc(*p >> 3 & 07, tracefile);
344 putc(*p & 07, tracefile);
349 putc('"', tracefile);
356 if (tracefile == NULL)
361 putc(' ', tracefile);
363 putc('\n', tracefile);
380 if ((p = getenv("HOME")) == NULL) {
390 strcpy(s, "./trace");
391 #endif /* not_this_way */
392 if ((tracefile = fopen(s, "a")) == NULL) {
393 fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
396 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
397 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
398 fputs("\nTracing started.\n", tracefile);