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 * 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.
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
36 * @(#)show.c 8.3 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/show.c,v 1.23 2006/04/14 13:59:03 schweikh Exp $
38 * $DragonFly: src/bin/sh/show.c,v 1.4 2007/01/14 06:21:52 pavalos Exp $
55 static void shtree(union node *, int, char *, FILE*);
56 static void shcmd(union node *, FILE *);
57 static void sharg(union node *, FILE *);
58 static void indent(int, char *, FILE *);
59 static void trstring(char *);
63 showtree(union node *n)
65 trputs("showtree called\n");
66 shtree(n, 1, NULL, stdout);
71 shtree(union node *n, int ind, char *pfx, FILE *fp)
90 shtree(n->nbinary.ch1, ind, NULL, fp);
93 shtree(n->nbinary.ch2, ind, NULL, fp);
101 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
106 if (n->npipe.backgnd)
112 fprintf(fp, "<node type %d>", n->type);
122 shcmd(union node *cmd, FILE *fp)
130 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
136 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
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 NCLOBBER: s = ">|"; dftfd = 1; break;
144 case NFROM: s = "<"; dftfd = 0; break;
145 case NFROMTO: s = "<>"; dftfd = 0; break;
146 case NFROMFD: s = "<&"; dftfd = 0; break;
147 case NHERE: s = "<<"; dftfd = 0; break;
148 case NXHERE: s = "<<"; dftfd = 0; break;
149 default: s = "*error*"; dftfd = 0; break;
151 if (np->nfile.fd != dftfd)
152 fprintf(fp, "%d", np->nfile.fd);
154 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
155 if (np->ndup.dupfd >= 0)
156 fprintf(fp, "%d", np->ndup.dupfd);
159 } else if (np->nfile.type == NHERE) {
161 } else if (np->nfile.type == NXHERE) {
162 fprintf(fp, "XHERE");
164 sharg(np->nfile.fname, fp);
173 sharg(union node *arg, FILE *fp)
176 struct nodelist *bqlist;
179 if (arg->type != NARG) {
180 printf("<node type %d>\n", arg->type);
184 bqlist = arg->narg.backquote;
185 for (p = arg->narg.text ; *p ; p++) {
194 if (subtype == VSLENGTH)
203 switch (subtype & VSTYPE) {
236 printf("<subtype %d>", subtype);
243 case CTLBACKQ|CTLQUOTE:
246 shtree(bqlist->n, -1, NULL, fp);
258 indent(int amount, char *pfx, FILE *fp)
262 for (i = 0 ; i < amount ; i++) {
263 if (pfx && i == amount - 1)
287 if (tracefile == NULL)
296 sh_trace(const char *fmt, ...)
300 if (tracefile != NULL) {
301 vfprintf(tracefile, fmt, va);
302 if (strchr(fmt, '\n'))
312 if (tracefile == NULL)
326 if (tracefile == NULL)
328 putc('"', tracefile);
329 for (p = s ; *p ; p++) {
331 case '\n': c = 'n'; goto backslash;
332 case '\t': c = 't'; goto backslash;
333 case '\r': c = 'r'; goto backslash;
334 case '"': c = '"'; goto backslash;
335 case '\\': c = '\\'; goto backslash;
336 case CTLESC: c = 'e'; goto backslash;
337 case CTLVAR: c = 'v'; goto backslash;
338 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
339 case CTLBACKQ: c = 'q'; goto backslash;
340 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
341 backslash: putc('\\', tracefile);
345 if (*p >= ' ' && *p <= '~')
348 putc('\\', tracefile);
349 putc(*p >> 6 & 03, tracefile);
350 putc(*p >> 3 & 07, tracefile);
351 putc(*p & 07, tracefile);
356 putc('"', tracefile);
363 if (tracefile == NULL)
368 putc(' ', tracefile);
370 putc('\n', tracefile);
387 if ((p = getenv("HOME")) == NULL) {
398 #endif /* not_this_way */
399 if ((tracefile = fopen(s, "a")) == NULL) {
400 fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
403 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
404 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
405 fputs("\nTracing started.\n", tracefile);