]>
git.saurik.com Git - apple/shell_cmds.git/blob - sh/show.c
9b55708c9d6b3ac79d350c50f3b5196969b53ca1
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
37 static char sccsid
[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/show.c 326025 2017-11-20 19:49:47Z pfg $");
57 static void shtree(union node
*, int, char *, FILE*);
58 static void shcmd(union node
*, FILE *);
59 static void sharg(union node
*, FILE *);
60 static void indent(int, char *, FILE *);
61 static void trstring(char *);
65 showtree(union node
*n
)
67 trputs("showtree called\n");
68 shtree(n
, 1, NULL
, stdout
);
73 shtree(union node
*n
, int ind
, char *pfx
, FILE *fp
)
92 shtree(n
->nbinary
.ch1
, ind
, NULL
, fp
);
95 shtree(n
->nbinary
.ch2
, ind
, NULL
, fp
);
103 for (lp
= n
->npipe
.cmdlist
; lp
; lp
= lp
->next
) {
108 if (n
->npipe
.backgnd
)
114 fprintf(fp
, "<node type %d>", n
->type
);
124 shcmd(union node
*cmd
, FILE *fp
)
132 for (np
= cmd
->ncmd
.args
; np
; np
= np
->narg
.next
) {
138 for (np
= cmd
->ncmd
.redirect
; np
; np
= np
->nfile
.next
) {
141 switch (np
->nfile
.type
) {
142 case NTO
: s
= ">"; dftfd
= 1; break;
143 case NAPPEND
: s
= ">>"; dftfd
= 1; break;
144 case NTOFD
: s
= ">&"; dftfd
= 1; break;
145 case NCLOBBER
: s
= ">|"; dftfd
= 1; break;
146 case NFROM
: s
= "<"; dftfd
= 0; break;
147 case NFROMTO
: s
= "<>"; dftfd
= 0; break;
148 case NFROMFD
: s
= "<&"; dftfd
= 0; break;
149 case NHERE
: s
= "<<"; dftfd
= 0; break;
150 case NXHERE
: s
= "<<"; dftfd
= 0; break;
151 default: s
= "*error*"; dftfd
= 0; break;
153 if (np
->nfile
.fd
!= dftfd
)
154 fprintf(fp
, "%d", np
->nfile
.fd
);
156 if (np
->nfile
.type
== NTOFD
|| np
->nfile
.type
== NFROMFD
) {
157 if (np
->ndup
.dupfd
>= 0)
158 fprintf(fp
, "%d", np
->ndup
.dupfd
);
161 } else if (np
->nfile
.type
== NHERE
) {
163 } else if (np
->nfile
.type
== NXHERE
) {
164 fprintf(fp
, "XHERE");
166 sharg(np
->nfile
.fname
, fp
);
175 sharg(union node
*arg
, FILE *fp
)
178 struct nodelist
*bqlist
;
181 if (arg
->type
!= NARG
) {
182 printf("<node type %d>\n", arg
->type
);
186 bqlist
= arg
->narg
.backquote
;
187 for (p
= arg
->narg
.text
; *p
; p
++) {
196 if (subtype
== VSLENGTH
)
205 switch (subtype
& VSTYPE
) {
238 printf("<subtype %d>", subtype
);
245 case CTLBACKQ
|CTLQUOTE
:
248 shtree(bqlist
->n
, -1, NULL
, fp
);
260 indent(int amount
, char *pfx
, FILE *fp
)
264 for (i
= 0 ; i
< amount
; i
++) {
265 if (pfx
&& i
== amount
- 1)
289 if (tracefile
== NULL
)
298 sh_trace(const char *fmt
, ...)
302 if (tracefile
!= NULL
) {
303 (void) vfprintf(tracefile
, fmt
, va
);
304 if (strchr(fmt
, '\n'))
305 (void) fflush(tracefile
);
312 trputs(const char *s
)
314 if (tracefile
== NULL
)
328 if (tracefile
== NULL
)
330 putc('"', tracefile
);
331 for (p
= s
; *p
; p
++) {
333 case '\n': c
= 'n'; goto backslash
;
334 case '\t': c
= 't'; goto backslash
;
335 case '\r': c
= 'r'; goto backslash
;
336 case '"': c
= '"'; goto backslash
;
337 case '\\': c
= '\\'; goto backslash
;
338 case CTLESC
: c
= 'e'; goto backslash
;
339 case CTLVAR
: c
= 'v'; goto backslash
;
340 case CTLVAR
+CTLQUOTE
: c
= 'V'; goto backslash
;
341 case CTLBACKQ
: c
= 'q'; goto backslash
;
342 case CTLBACKQ
+CTLQUOTE
: c
= 'Q'; goto backslash
;
343 backslash
: putc('\\', tracefile
);
347 if (*p
>= ' ' && *p
<= '~')
350 putc('\\', tracefile
);
351 putc(*p
>> 6 & 03, tracefile
);
352 putc(*p
>> 3 & 07, tracefile
);
353 putc(*p
& 07, tracefile
);
358 putc('"', tracefile
);
365 if (tracefile
== NULL
)
370 putc(' ', tracefile
);
372 putc('\n', tracefile
);
389 if ((p
= getenv("HOME")) == NULL
) {
399 strcpy(s
, "./trace");
400 #endif /* not_this_way */
401 if ((tracefile
= fopen(s
, "a")) == NULL
) {
402 fprintf(stderr
, "Can't open %s: %s\n", s
, strerror(errno
));
405 if ((flags
= fcntl(fileno(tracefile
), F_GETFL
, 0)) >= 0)
406 fcntl(fileno(tracefile
), F_SETFL
, flags
| O_APPEND
);
407 fputs("\nTracing started.\n", tracefile
);