libutil: add O_NOCTTY to old pty open code
[minix3.git] / commands / ash / show.c
blob1b848927e831590cd68acdc4058fcabc57b2175b
1 /*-
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
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/show.c,v 1.21 2004/04/06 20:06:51 markm Exp $");
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stdarg.h>
47 #include <errno.h>
49 #include "shell.h"
50 #include "parser.h"
51 #include "nodes.h"
52 #include "mystring.h"
53 #include "show.h"
55 #if DEBUG
56 static void trputc(int c);
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 *);
62 static void showtree(union node *n);
65 static void
66 showtree(union node *n)
68 trputs("showtree called\n");
69 shtree(n, 1, NULL, stdout);
73 static void
74 shtree(union node *n, int ind, char *pfx, FILE *fp)
76 struct nodelist *lp;
77 char *s;
79 if (n == NULL)
80 return;
82 indent(ind, pfx, fp);
83 switch(n->type) {
84 case NSEMI:
85 s = "; ";
86 goto binop;
87 case NAND:
88 s = " && ";
89 goto binop;
90 case NOR:
91 s = " || ";
92 binop:
93 shtree(n->nbinary.ch1, ind, NULL, fp);
94 /* if (ind < 0) */
95 fputs(s, fp);
96 shtree(n->nbinary.ch2, ind, NULL, fp);
97 break;
98 case NCMD:
99 shcmd(n, fp);
100 if (ind >= 0)
101 putc('\n', fp);
102 break;
103 case NPIPE:
104 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
105 shcmd(lp->n, fp);
106 if (lp->next)
107 fputs(" | ", fp);
109 if (n->npipe.backgnd)
110 fputs(" &", fp);
111 if (ind >= 0)
112 putc('\n', fp);
113 break;
114 default:
115 fprintf(fp, "<node type %d>", n->type);
116 if (ind >= 0)
117 putc('\n', fp);
118 break;
124 static void
125 shcmd(union node *cmd, FILE *fp)
127 union node *np;
128 int first;
129 char *s;
130 int dftfd;
132 first = 1;
133 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
134 if (! first)
135 putchar(' ');
136 sharg(np, fp);
137 first = 0;
139 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
140 if (! first)
141 putchar(' ');
142 switch (np->nfile.type) {
143 case NTO: s = ">"; dftfd = 1; break;
144 case NAPPEND: s = ">>"; dftfd = 1; break;
145 case NTOFD: s = ">&"; dftfd = 1; break;
146 case NCLOBBER: s = ">|"; dftfd = 1; break;
147 case NFROM: s = "<"; dftfd = 0; break;
148 case NFROMTO: s = "<>"; dftfd = 0; break;
149 case NFROMFD: s = "<&"; dftfd = 0; break;
150 default: s = "*error*"; dftfd = 0; break;
152 if (np->nfile.fd != dftfd)
153 fprintf(fp, "%d", np->nfile.fd);
154 fputs(s, fp);
155 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
156 if (np->ndup.dupfd >= 0)
157 fprintf(fp, "%d", np->ndup.dupfd);
158 else
159 fprintf(fp, "-");
160 } else {
161 sharg(np->nfile.fname, fp);
163 first = 0;
169 static void
170 sharg(union node *arg, FILE *fp)
172 char *p;
173 struct nodelist *bqlist;
174 int subtype;
176 if (arg->type != NARG) {
177 printf("<node type %d>\n", arg->type);
178 fflush(stdout);
179 abort();
181 bqlist = arg->narg.backquote;
182 for (p = arg->narg.text ; *p ; p++) {
183 switch (*p) {
184 case CTLESC:
185 putc(*++p, fp);
186 break;
187 case CTLVAR:
188 putc('$', fp);
189 putc('{', fp);
190 subtype = *++p;
191 if (subtype == VSLENGTH)
192 putc('#', fp);
194 while (*p != '=')
195 putc(*p++, fp);
197 if (subtype & VSNUL)
198 putc(':', fp);
200 switch (subtype & VSTYPE) {
201 case VSNORMAL:
202 putc('}', fp);
203 break;
204 case VSMINUS:
205 putc('-', fp);
206 break;
207 case VSPLUS:
208 putc('+', fp);
209 break;
210 case VSQUESTION:
211 putc('?', fp);
212 break;
213 case VSASSIGN:
214 putc('=', fp);
215 break;
216 case VSTRIMLEFT:
217 putc('#', fp);
218 break;
219 case VSTRIMLEFTMAX:
220 putc('#', fp);
221 putc('#', fp);
222 break;
223 case VSTRIMRIGHT:
224 putc('%', fp);
225 break;
226 case VSTRIMRIGHTMAX:
227 putc('%', fp);
228 putc('%', fp);
229 break;
230 case VSLENGTH:
231 break;
232 default:
233 printf("<subtype %d>", subtype);
235 break;
236 case CTLENDVAR:
237 putc('}', fp);
238 break;
239 case CTLBACKQ:
240 case CTLBACKQ|CTLQUOTE:
241 putc('$', fp);
242 putc('(', fp);
243 shtree(bqlist->n, -1, NULL, fp);
244 putc(')', fp);
245 break;
246 default:
247 putc(*p, fp);
248 break;
254 static void
255 indent(int amount, char *pfx, FILE *fp)
257 int i;
259 for (i = 0 ; i < amount ; i++) {
260 if (pfx && i == amount - 1)
261 fputs(pfx, fp);
262 putc('\t', fp);
268 * Debugging stuff.
272 FILE *tracefile;
274 #if DEBUG == 2
275 int debug = 1;
276 #else
277 int debug = 0;
278 #endif
281 static void
282 trputc(int c)
284 if (tracefile == NULL)
285 return;
286 putc(c, tracefile);
287 if (c == '\n')
288 fflush(tracefile);
292 void
293 sh_trace(const char *fmt, ...)
295 va_list va;
296 va_start(va, fmt);
297 if (tracefile != NULL) {
298 (void) vfprintf(tracefile, fmt, va);
299 if (strchr(fmt, '\n'))
300 (void) fflush(tracefile);
302 va_end(va);
306 void
307 trputs(char *s)
309 if (tracefile == NULL)
310 return;
311 fputs(s, tracefile);
312 if (strchr(s, '\n'))
313 fflush(tracefile);
317 static void
318 trstring(char *s)
320 char *p;
321 char c;
323 if (tracefile == NULL)
324 return;
325 putc('"', tracefile);
326 for (p = s ; *p ; p++) {
327 switch (*p) {
328 case '\n': c = 'n'; goto backslash;
329 case '\t': c = 't'; goto backslash;
330 case '\r': c = 'r'; goto backslash;
331 case '"': c = '"'; goto backslash;
332 case '\\': c = '\\'; goto backslash;
333 case CTLESC: c = 'e'; goto backslash;
334 case CTLVAR: c = 'v'; goto backslash;
335 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
336 case CTLBACKQ: c = 'q'; goto backslash;
337 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
338 backslash: putc('\\', tracefile);
339 putc(c, tracefile);
340 break;
341 default:
342 if (*p >= ' ' && *p <= '~')
343 putc(*p, tracefile);
344 else {
345 putc('\\', tracefile);
346 putc(*p >> 6 & 03, tracefile);
347 putc(*p >> 3 & 07, tracefile);
348 putc(*p & 07, tracefile);
350 break;
353 putc('"', tracefile);
357 void
358 trargs(char **ap)
360 if (tracefile == NULL)
361 return;
362 while (*ap) {
363 trstring(*ap++);
364 if (*ap)
365 putc(' ', tracefile);
366 else
367 putc('\n', tracefile);
369 fflush(tracefile);
373 void
374 opentrace(void)
376 char s[100];
377 int flags;
379 if (!debug)
380 return;
381 #ifdef not_this_way
383 char *p;
384 if ((p = getenv("HOME")) == NULL) {
385 if (geteuid() == 0)
386 p = "/";
387 else
388 p = "/tmp";
390 scopy(p, s);
391 strcat(s, "/trace");
393 #else
394 scopy("./trace", s);
395 #endif /* not_this_way */
396 if ((tracefile = fopen(s, "a")) == NULL) {
397 fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
398 return;
400 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
401 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
402 fputs("\nTracing started.\n", tracefile);
403 fflush(tracefile);
405 #endif /* DEBUG */
408 * $PchId: show.c,v 1.6 2006/05/22 12:27:51 philip Exp $