2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)show.c 8.3 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/show.c 213811 2010-10-13 22:18:03Z obrien $
50 static void shtree(union node *, int, char *, FILE*);
51 static void shcmd(union node *, FILE *);
52 static void sharg(union node *, FILE *);
53 static void indent(int, char *, FILE *);
54 static void trstring(char *);
58 showtree(union node *n)
60 trputs("showtree called\n");
61 shtree(n, 1, NULL, stdout);
66 shtree(union node *n, int ind, char *pfx, FILE *fp)
85 shtree(n->nbinary.ch1, ind, NULL, fp);
88 shtree(n->nbinary.ch2, ind, NULL, fp);
96 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
101 if (n->npipe.backgnd)
107 fprintf(fp, "<node type %d>", n->type);
117 shcmd(union node *cmd, FILE *fp)
125 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
131 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
134 switch (np->nfile.type) {
135 case NTO: s = ">"; dftfd = 1; break;
136 case NAPPEND: s = ">>"; dftfd = 1; break;
137 case NTOFD: s = ">&"; dftfd = 1; break;
138 case NCLOBBER: s = ">|"; dftfd = 1; break;
139 case NFROM: s = "<"; dftfd = 0; break;
140 case NFROMTO: s = "<>"; dftfd = 0; break;
141 case NFROMFD: s = "<&"; dftfd = 0; break;
142 case NHERE: s = "<<"; dftfd = 0; break;
143 case NXHERE: s = "<<"; dftfd = 0; break;
144 default: s = "*error*"; dftfd = 0; break;
146 if (np->nfile.fd != dftfd)
147 fprintf(fp, "%d", np->nfile.fd);
149 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
150 if (np->ndup.dupfd >= 0)
151 fprintf(fp, "%d", np->ndup.dupfd);
154 } else if (np->nfile.type == NHERE) {
156 } else if (np->nfile.type == NXHERE) {
157 fprintf(fp, "XHERE");
159 sharg(np->nfile.fname, fp);
168 sharg(union node *arg, FILE *fp)
171 struct nodelist *bqlist;
174 if (arg->type != NARG) {
175 printf("<node type %d>\n", arg->type);
179 bqlist = arg->narg.backquote;
180 for (p = arg->narg.text ; *p ; p++) {
189 if (subtype == VSLENGTH)
198 switch (subtype & VSTYPE) {
231 printf("<subtype %d>", subtype);
238 case CTLBACKQ|CTLQUOTE:
241 shtree(bqlist->n, -1, NULL, fp);
253 indent(int amount, char *pfx, FILE *fp)
257 for (i = 0 ; i < amount ; i++) {
258 if (pfx && i == amount - 1)
282 if (tracefile == NULL)
291 sh_trace(const char *fmt, ...)
295 if (tracefile != NULL) {
296 vfprintf(tracefile, fmt, va);
297 if (strchr(fmt, '\n'))
305 trputs(const char *s)
307 if (tracefile == NULL)
321 if (tracefile == NULL)
323 putc('"', tracefile);
324 for (p = s ; *p ; p++) {
326 case '\n': c = 'n'; goto backslash;
327 case '\t': c = 't'; goto backslash;
328 case '\r': c = 'r'; goto backslash;
329 case '"': c = '"'; goto backslash;
330 case '\\': c = '\\'; goto backslash;
331 case CTLESC: c = 'e'; goto backslash;
332 case CTLVAR: c = 'v'; goto backslash;
333 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
334 case CTLBACKQ: c = 'q'; goto backslash;
335 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
336 backslash: putc('\\', tracefile);
340 if (*p >= ' ' && *p <= '~')
343 putc('\\', tracefile);
344 putc(*p >> 6 & 03, tracefile);
345 putc(*p >> 3 & 07, tracefile);
346 putc(*p & 07, tracefile);
351 putc('"', tracefile);
358 if (tracefile == NULL)
363 putc(' ', tracefile);
365 putc('\n', tracefile);
382 if ((p = getenv("HOME")) == NULL) {
393 #endif /* not_this_way */
394 if ((tracefile = fopen(s, "a")) == NULL) {
395 fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
398 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
399 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
400 fputs("\nTracing started.\n", tracefile);