VirtualBox

source: kBuild/trunk/src/kash/show.c@ 1200

Last change on this file since 1200 was 1199, checked in by bird, 17 years ago

moving globals into shinstance...

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1/* $NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)show.c 8.3 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: show.c,v 1.26 2003/11/14 10:46:13 dsl Exp $");
43#endif
44#endif /* not lint */
45
46#include <stdio.h>
47#include <stdarg.h>
48#include <stdlib.h>
49
50#include "shell.h"
51#include "parser.h"
52#include "nodes.h"
53#include "mystring.h"
54#include "show.h"
55#include "options.h"
56
57
58#ifdef DEBUG
59static void shtree(union node *, int, char *, FILE*);
60static void shcmd(union node *, FILE *);
61static void sharg(union node *, FILE *);
62static void indent(int, char *, FILE *);
63static void trstring(shinstance *, char *);
64
65
66void
67showtree(shinstance *psh, union node *n)
68{
69 trputs(psh, "showtree called\n");
70 shtree(n, 1, NULL, stdout);
71}
72
73
74static void
75shtree(union node *n, int ind, char *pfx, FILE *fp)
76{
77 struct nodelist *lp;
78 const char *s;
79
80 if (n == NULL)
81 return;
82
83 indent(ind, pfx, fp);
84 switch(n->type) {
85 case NSEMI:
86 s = "; ";
87 goto binop;
88 case NAND:
89 s = " && ";
90 goto binop;
91 case NOR:
92 s = " || ";
93binop:
94 shtree(n->nbinary.ch1, ind, NULL, fp);
95 /* if (ind < 0) */
96 fputs(s, fp);
97 shtree(n->nbinary.ch2, ind, NULL, fp);
98 break;
99 case NCMD:
100 shcmd(n, fp);
101 if (ind >= 0)
102 putc('\n', fp);
103 break;
104 case NPIPE:
105 for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
106 shcmd(lp->n, fp);
107 if (lp->next)
108 fputs(" | ", fp);
109 }
110 if (n->npipe.backgnd)
111 fputs(" &", fp);
112 if (ind >= 0)
113 putc('\n', fp);
114 break;
115 default:
116 fprintf(fp, "<node type %d>", n->type);
117 if (ind >= 0)
118 putc('\n', fp);
119 break;
120 }
121}
122
123
124
125static void
126shcmd(union node *cmd, FILE *fp)
127{
128 union node *np;
129 int first;
130 const char *s;
131 int dftfd;
132
133 first = 1;
134 for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
135 if (! first)
136 putchar(' ');
137 sharg(np, fp);
138 first = 0;
139 }
140 for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
141 if (! first)
142 putchar(' ');
143 switch (np->nfile.type) {
144 case NTO: s = ">"; dftfd = 1; break;
145 case NCLOBBER: s = ">|"; dftfd = 1; break;
146 case NAPPEND: s = ">>"; dftfd = 1; break;
147 case NTOFD: s = ">&"; dftfd = 1; break;
148 case NFROM: s = "<"; dftfd = 0; break;
149 case NFROMFD: s = "<&"; dftfd = 0; break;
150 case NFROMTO: s = "<>"; dftfd = 0; break;
151 default: s = "*error*"; dftfd = 0; break;
152 }
153 if (np->nfile.fd != dftfd)
154 fprintf(fp, "%d", np->nfile.fd);
155 fputs(s, fp);
156 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
157 fprintf(fp, "%d", np->ndup.dupfd);
158 } else {
159 sharg(np->nfile.fname, fp);
160 }
161 first = 0;
162 }
163}
164
165
166
167static void
168sharg(union node *arg, FILE *fp)
169{
170 char *p;
171 struct nodelist *bqlist;
172 int subtype;
173
174 if (arg->type != NARG) {
175 printf("<node type %d>\n", arg->type);
176 abort();
177 }
178 bqlist = arg->narg.backquote;
179 for (p = arg->narg.text ; *p ; p++) {
180 switch (*p) {
181 case CTLESC:
182 putc(*++p, fp);
183 break;
184 case CTLVAR:
185 putc('$', fp);
186 putc('{', fp);
187 subtype = *++p;
188 if (subtype == VSLENGTH)
189 putc('#', fp);
190
191 while (*p != '=')
192 putc(*p++, fp);
193
194 if (subtype & VSNUL)
195 putc(':', fp);
196
197 switch (subtype & VSTYPE) {
198 case VSNORMAL:
199 putc('}', fp);
200 break;
201 case VSMINUS:
202 putc('-', fp);
203 break;
204 case VSPLUS:
205 putc('+', fp);
206 break;
207 case VSQUESTION:
208 putc('?', fp);
209 break;
210 case VSASSIGN:
211 putc('=', fp);
212 break;
213 case VSTRIMLEFT:
214 putc('#', fp);
215 break;
216 case VSTRIMLEFTMAX:
217 putc('#', fp);
218 putc('#', fp);
219 break;
220 case VSTRIMRIGHT:
221 putc('%', fp);
222 break;
223 case VSTRIMRIGHTMAX:
224 putc('%', fp);
225 putc('%', fp);
226 break;
227 case VSLENGTH:
228 break;
229 default:
230 printf("<subtype %d>", subtype);
231 }
232 break;
233 case CTLENDVAR:
234 putc('}', fp);
235 break;
236 case CTLBACKQ:
237 case CTLBACKQ|CTLQUOTE:
238 putc('$', fp);
239 putc('(', fp);
240 shtree(bqlist->n, -1, NULL, fp);
241 putc(')', fp);
242 break;
243 default:
244 putc(*p, fp);
245 break;
246 }
247 }
248}
249
250
251static void
252indent(int amount, char *pfx, FILE *fp)
253{
254 int i;
255
256 for (i = 0 ; i < amount ; i++) {
257 if (pfx && i == amount - 1)
258 fputs(pfx, fp);
259 putc('\t', fp);
260 }
261}
262#endif
263
264
265
266/*
267 * Debugging stuff.
268 */
269
270
271FILE *tracefile;
272
273
274#ifdef DEBUG
275void
276trputc(shinstance *psh, int c)
277{
278 if (debug(psh) != 1)
279 return;
280 putc(c, tracefile);
281}
282#endif
283
284void
285trace(shinstance *psh, const char *fmt, ...)
286{
287#ifdef DEBUG
288 va_list va;
289
290 if (debug(psh) != 1)
291 return;
292 fprintf(tracefile, "[%d] ", getpid());
293 va_start(va, fmt);
294 (void) vfprintf(tracefile, fmt, va);
295 va_end(va);
296#endif
297}
298
299void
300tracev(shinstance *psh, const char *fmt, va_list va)
301{
302#ifdef DEBUG
303 if (debug(psh) != 1)
304 return;
305 fprintf(tracefile, "[%d] ", getpid());
306 (void) vfprintf(tracefile, fmt, va);
307#endif
308}
309
310
311#ifdef DEBUG
312void
313trputs(shinstance *psh, const char *s)
314{
315 if (debug(psh) != 1)
316 return;
317 fputs(s, tracefile);
318}
319
320
321static void
322trstring(shinstance *psh, char *s)
323{
324 char *p;
325 char c;
326
327 if (debug(psh) != 1)
328 return;
329 putc('"', tracefile);
330 for (p = s ; *p ; p++) {
331 switch (*p) {
332 case '\n': c = 'n'; goto backslash;
333 case '\t': c = 't'; goto backslash;
334 case '\r': c = 'r'; goto backslash;
335 case '"': c = '"'; goto backslash;
336 case '\\': c = '\\'; goto backslash;
337 case CTLESC: c = 'e'; goto backslash;
338 case CTLVAR: c = 'v'; goto backslash;
339 case CTLVAR+CTLQUOTE: c = 'V'; goto backslash;
340 case CTLBACKQ: c = 'q'; goto backslash;
341 case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash;
342backslash: putc('\\', tracefile);
343 putc(c, tracefile);
344 break;
345 default:
346 if (*p >= ' ' && *p <= '~')
347 putc(*p, tracefile);
348 else {
349 putc('\\', tracefile);
350 putc(*p >> 6 & 03, tracefile);
351 putc(*p >> 3 & 07, tracefile);
352 putc(*p & 07, tracefile);
353 }
354 break;
355 }
356 }
357 putc('"', tracefile);
358}
359#endif
360
361
362void
363trargs(shinstance *psh, char **ap)
364{
365#ifdef DEBUG
366 if (debug(psh) != 1)
367 return;
368 while (*ap) {
369 trstring(psh, *ap++);
370 if (*ap)
371 putc(' ', tracefile);
372 else
373 putc('\n', tracefile);
374 }
375#endif
376}
377
378
379#ifdef DEBUG
380void
381opentrace(shinstance *psh)
382{
383 char s[100];
384#ifdef O_APPEND
385 int flags;
386#endif
387
388 if (debug(psh) != 1) {
389 if (tracefile)
390 fflush(tracefile);
391 /* leave open because libedit might be using it */
392 return;
393 }
394#ifdef not_this_way
395 {
396 char *p;
397 if ((p = getenv("HOME")) == NULL) {
398 if (geteuid() == 0)
399 p = "/";
400 else
401 p = "/tmp";
402 }
403 scopy(p, s);
404 strcat(s, "/trace");
405 }
406#else
407 scopy("./trace", s);
408#endif /* not_this_way */
409 if (tracefile) {
410 if (!freopen(s, "a", tracefile)) {
411 fprintf(stderr, "Can't re-open %s\n", s);
412 debug(psh) = 0;
413 return;
414 }
415 } else {
416 if ((tracefile = fopen(s, "a")) == NULL) {
417 fprintf(stderr, "Can't open %s\n", s);
418 debug(psh) = 0;
419 return;
420 }
421 }
422#ifdef O_APPEND
423 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
424 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
425#endif
426 setvbuf(tracefile, (char *)NULL, _IOLBF, 0); //setlinebuf(tracefile);
427 fputs("\nTracing started.\n", tracefile);
428}
429#endif /* DEBUG */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette