Home | History | Annotate | Line # | Download | only in sh
show.c revision 1.17.4.1
      1  1.17.4.1  wrstuden /*	$NetBSD: show.c,v 1.17.4.1 1999/12/27 18:27:13 wrstuden Exp $	*/
      2      1.10       cgd 
      3       1.1       cgd /*-
      4       1.5       jtc  * Copyright (c) 1991, 1993
      5       1.5       jtc  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Kenneth Almquist.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19       1.1       cgd  *    must display the following acknowledgement:
     20       1.1       cgd  *	This product includes software developed by the University of
     21       1.1       cgd  *	California, Berkeley and its contributors.
     22       1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23       1.1       cgd  *    may be used to endorse or promote products derived from this software
     24       1.1       cgd  *    without specific prior written permission.
     25       1.1       cgd  *
     26       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36       1.1       cgd  * SUCH DAMAGE.
     37       1.1       cgd  */
     38       1.1       cgd 
     39      1.15  christos #include <sys/cdefs.h>
     40       1.1       cgd #ifndef lint
     41      1.10       cgd #if 0
     42      1.11  christos static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
     43      1.10       cgd #else
     44  1.17.4.1  wrstuden __RCSID("$NetBSD: show.c,v 1.17.4.1 1999/12/27 18:27:13 wrstuden Exp $");
     45      1.10       cgd #endif
     46       1.1       cgd #endif /* not lint */
     47       1.1       cgd 
     48       1.1       cgd #include <stdio.h>
     49      1.14  christos #ifdef __STDC__
     50      1.11  christos #include <stdarg.h>
     51      1.11  christos #else
     52      1.11  christos #include <varargs.h>
     53      1.11  christos #endif
     54      1.11  christos 
     55       1.1       cgd #include "shell.h"
     56       1.1       cgd #include "parser.h"
     57       1.1       cgd #include "nodes.h"
     58       1.1       cgd #include "mystring.h"
     59      1.11  christos #include "show.h"
     60       1.1       cgd 
     61       1.1       cgd 
     62       1.1       cgd #ifdef DEBUG
     63      1.11  christos static void shtree __P((union node *, int, char *, FILE*));
     64      1.11  christos static void shcmd __P((union node *, FILE *));
     65      1.11  christos static void sharg __P((union node *, FILE *));
     66      1.11  christos static void indent __P((int, char *, FILE *));
     67       1.8       cgd static void trstring __P((char *));
     68       1.1       cgd 
     69       1.1       cgd 
     70      1.11  christos void
     71       1.1       cgd showtree(n)
     72       1.1       cgd 	union node *n;
     73       1.8       cgd {
     74       1.1       cgd 	trputs("showtree called\n");
     75       1.1       cgd 	shtree(n, 1, NULL, stdout);
     76       1.1       cgd }
     77       1.1       cgd 
     78       1.1       cgd 
     79       1.8       cgd static void
     80       1.1       cgd shtree(n, ind, pfx, fp)
     81       1.1       cgd 	union node *n;
     82       1.8       cgd 	int ind;
     83       1.1       cgd 	char *pfx;
     84       1.1       cgd 	FILE *fp;
     85       1.8       cgd {
     86       1.1       cgd 	struct nodelist *lp;
     87  1.17.4.1  wrstuden 	const char *s;
     88       1.1       cgd 
     89       1.9  christos 	if (n == NULL)
     90       1.9  christos 		return;
     91       1.9  christos 
     92       1.1       cgd 	indent(ind, pfx, fp);
     93       1.1       cgd 	switch(n->type) {
     94       1.1       cgd 	case NSEMI:
     95       1.1       cgd 		s = "; ";
     96       1.1       cgd 		goto binop;
     97       1.1       cgd 	case NAND:
     98       1.1       cgd 		s = " && ";
     99       1.1       cgd 		goto binop;
    100       1.1       cgd 	case NOR:
    101       1.1       cgd 		s = " || ";
    102       1.1       cgd binop:
    103       1.1       cgd 		shtree(n->nbinary.ch1, ind, NULL, fp);
    104       1.1       cgd 	   /*    if (ind < 0) */
    105       1.1       cgd 			fputs(s, fp);
    106       1.1       cgd 		shtree(n->nbinary.ch2, ind, NULL, fp);
    107       1.1       cgd 		break;
    108       1.1       cgd 	case NCMD:
    109       1.1       cgd 		shcmd(n, fp);
    110       1.1       cgd 		if (ind >= 0)
    111       1.1       cgd 			putc('\n', fp);
    112       1.1       cgd 		break;
    113       1.1       cgd 	case NPIPE:
    114       1.1       cgd 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    115       1.1       cgd 			shcmd(lp->n, fp);
    116       1.1       cgd 			if (lp->next)
    117       1.1       cgd 				fputs(" | ", fp);
    118       1.1       cgd 		}
    119       1.1       cgd 		if (n->npipe.backgnd)
    120       1.1       cgd 			fputs(" &", fp);
    121       1.1       cgd 		if (ind >= 0)
    122       1.1       cgd 			putc('\n', fp);
    123       1.1       cgd 		break;
    124       1.1       cgd 	default:
    125       1.1       cgd 		fprintf(fp, "<node type %d>", n->type);
    126       1.1       cgd 		if (ind >= 0)
    127       1.1       cgd 			putc('\n', fp);
    128       1.1       cgd 		break;
    129       1.1       cgd 	}
    130       1.1       cgd }
    131       1.1       cgd 
    132       1.1       cgd 
    133       1.1       cgd 
    134       1.8       cgd static void
    135       1.1       cgd shcmd(cmd, fp)
    136       1.1       cgd 	union node *cmd;
    137       1.1       cgd 	FILE *fp;
    138      1.11  christos {
    139       1.1       cgd 	union node *np;
    140       1.1       cgd 	int first;
    141  1.17.4.1  wrstuden 	const char *s;
    142       1.1       cgd 	int dftfd;
    143       1.1       cgd 
    144       1.1       cgd 	first = 1;
    145       1.1       cgd 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
    146       1.1       cgd 		if (! first)
    147       1.1       cgd 			putchar(' ');
    148       1.1       cgd 		sharg(np, fp);
    149       1.1       cgd 		first = 0;
    150       1.1       cgd 	}
    151       1.1       cgd 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
    152       1.1       cgd 		if (! first)
    153       1.1       cgd 			putchar(' ');
    154       1.1       cgd 		switch (np->nfile.type) {
    155       1.1       cgd 			case NTO:	s = ">";  dftfd = 1; break;
    156       1.1       cgd 			case NAPPEND:	s = ">>"; dftfd = 1; break;
    157       1.1       cgd 			case NTOFD:	s = ">&"; dftfd = 1; break;
    158       1.1       cgd 			case NFROM:	s = "<";  dftfd = 0; break;
    159       1.1       cgd 			case NFROMFD:	s = "<&"; dftfd = 0; break;
    160      1.17  christos 			case NFROMTO:	s = "<>"; dftfd = 0; break;
    161      1.11  christos 			default:  	s = "*error*"; dftfd = 0; break;
    162       1.1       cgd 		}
    163       1.1       cgd 		if (np->nfile.fd != dftfd)
    164       1.1       cgd 			fprintf(fp, "%d", np->nfile.fd);
    165       1.1       cgd 		fputs(s, fp);
    166       1.1       cgd 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
    167       1.1       cgd 			fprintf(fp, "%d", np->ndup.dupfd);
    168       1.1       cgd 		} else {
    169       1.1       cgd 			sharg(np->nfile.fname, fp);
    170       1.1       cgd 		}
    171       1.1       cgd 		first = 0;
    172       1.1       cgd 	}
    173       1.1       cgd }
    174       1.1       cgd 
    175       1.1       cgd 
    176       1.1       cgd 
    177       1.8       cgd static void
    178       1.1       cgd sharg(arg, fp)
    179       1.1       cgd 	union node *arg;
    180       1.1       cgd 	FILE *fp;
    181       1.1       cgd 	{
    182       1.1       cgd 	char *p;
    183       1.1       cgd 	struct nodelist *bqlist;
    184       1.1       cgd 	int subtype;
    185       1.1       cgd 
    186       1.1       cgd 	if (arg->type != NARG) {
    187       1.1       cgd 		printf("<node type %d>\n", arg->type);
    188       1.1       cgd 		fflush(stdout);
    189       1.1       cgd 		abort();
    190       1.1       cgd 	}
    191       1.1       cgd 	bqlist = arg->narg.backquote;
    192       1.1       cgd 	for (p = arg->narg.text ; *p ; p++) {
    193       1.1       cgd 		switch (*p) {
    194       1.1       cgd 		case CTLESC:
    195       1.1       cgd 			putc(*++p, fp);
    196       1.1       cgd 			break;
    197       1.1       cgd 		case CTLVAR:
    198       1.1       cgd 			putc('$', fp);
    199       1.1       cgd 			putc('{', fp);
    200       1.1       cgd 			subtype = *++p;
    201       1.9  christos 			if (subtype == VSLENGTH)
    202       1.9  christos 				putc('#', fp);
    203       1.9  christos 
    204       1.1       cgd 			while (*p != '=')
    205       1.1       cgd 				putc(*p++, fp);
    206       1.9  christos 
    207       1.1       cgd 			if (subtype & VSNUL)
    208       1.1       cgd 				putc(':', fp);
    209       1.9  christos 
    210       1.1       cgd 			switch (subtype & VSTYPE) {
    211       1.1       cgd 			case VSNORMAL:
    212       1.1       cgd 				putc('}', fp);
    213       1.1       cgd 				break;
    214       1.1       cgd 			case VSMINUS:
    215       1.1       cgd 				putc('-', fp);
    216       1.1       cgd 				break;
    217       1.1       cgd 			case VSPLUS:
    218       1.1       cgd 				putc('+', fp);
    219       1.1       cgd 				break;
    220       1.1       cgd 			case VSQUESTION:
    221       1.1       cgd 				putc('?', fp);
    222       1.1       cgd 				break;
    223       1.1       cgd 			case VSASSIGN:
    224       1.1       cgd 				putc('=', fp);
    225       1.9  christos 				break;
    226       1.9  christos 			case VSTRIMLEFT:
    227       1.9  christos 				putc('#', fp);
    228       1.9  christos 				break;
    229       1.9  christos 			case VSTRIMLEFTMAX:
    230       1.9  christos 				putc('#', fp);
    231       1.9  christos 				putc('#', fp);
    232       1.9  christos 				break;
    233       1.9  christos 			case VSTRIMRIGHT:
    234       1.9  christos 				putc('%', fp);
    235       1.9  christos 				break;
    236       1.9  christos 			case VSTRIMRIGHTMAX:
    237       1.9  christos 				putc('%', fp);
    238       1.9  christos 				putc('%', fp);
    239       1.9  christos 				break;
    240       1.9  christos 			case VSLENGTH:
    241       1.1       cgd 				break;
    242       1.1       cgd 			default:
    243       1.1       cgd 				printf("<subtype %d>", subtype);
    244       1.1       cgd 			}
    245       1.1       cgd 			break;
    246       1.1       cgd 		case CTLENDVAR:
    247       1.1       cgd 		     putc('}', fp);
    248       1.1       cgd 		     break;
    249       1.1       cgd 		case CTLBACKQ:
    250       1.1       cgd 		case CTLBACKQ|CTLQUOTE:
    251       1.1       cgd 			putc('$', fp);
    252       1.1       cgd 			putc('(', fp);
    253       1.1       cgd 			shtree(bqlist->n, -1, NULL, fp);
    254       1.1       cgd 			putc(')', fp);
    255       1.1       cgd 			break;
    256       1.1       cgd 		default:
    257       1.1       cgd 			putc(*p, fp);
    258       1.1       cgd 			break;
    259       1.1       cgd 		}
    260       1.1       cgd 	}
    261       1.1       cgd }
    262       1.1       cgd 
    263       1.1       cgd 
    264       1.8       cgd static void
    265       1.1       cgd indent(amount, pfx, fp)
    266       1.8       cgd 	int amount;
    267       1.1       cgd 	char *pfx;
    268       1.1       cgd 	FILE *fp;
    269       1.8       cgd {
    270       1.1       cgd 	int i;
    271       1.1       cgd 
    272       1.1       cgd 	for (i = 0 ; i < amount ; i++) {
    273       1.1       cgd 		if (pfx && i == amount - 1)
    274       1.1       cgd 			fputs(pfx, fp);
    275       1.1       cgd 		putc('\t', fp);
    276       1.1       cgd 	}
    277       1.1       cgd }
    278       1.1       cgd #endif
    279       1.1       cgd 
    280       1.1       cgd 
    281       1.1       cgd 
    282       1.1       cgd /*
    283       1.1       cgd  * Debugging stuff.
    284       1.1       cgd  */
    285       1.1       cgd 
    286       1.1       cgd 
    287       1.1       cgd FILE *tracefile;
    288       1.1       cgd 
    289       1.1       cgd #if DEBUG == 2
    290       1.1       cgd int debug = 1;
    291       1.1       cgd #else
    292       1.1       cgd int debug = 0;
    293       1.1       cgd #endif
    294       1.1       cgd 
    295       1.1       cgd 
    296      1.12  christos #ifdef DEBUG
    297       1.8       cgd void
    298      1.12  christos trputc(c)
    299       1.8       cgd 	int c;
    300       1.8       cgd {
    301       1.1       cgd 	if (tracefile == NULL)
    302       1.1       cgd 		return;
    303       1.1       cgd 	putc(c, tracefile);
    304       1.1       cgd 	if (c == '\n')
    305       1.1       cgd 		fflush(tracefile);
    306      1.12  christos }
    307       1.1       cgd #endif
    308       1.1       cgd 
    309      1.11  christos void
    310      1.14  christos #ifdef __STDC__
    311      1.11  christos trace(const char *fmt, ...)
    312      1.11  christos #else
    313      1.11  christos trace(va_alist)
    314      1.11  christos 	va_dcl
    315      1.11  christos #endif
    316      1.11  christos {
    317      1.11  christos #ifdef DEBUG
    318      1.11  christos 	va_list va;
    319      1.14  christos #ifdef __STDC__
    320      1.11  christos 	va_start(va, fmt);
    321      1.11  christos #else
    322       1.1       cgd 	char *fmt;
    323      1.11  christos 	va_start(va);
    324      1.11  christos 	fmt = va_arg(va, char *);
    325      1.11  christos #endif
    326      1.11  christos 	if (tracefile != NULL) {
    327      1.11  christos 		(void) vfprintf(tracefile, fmt, va);
    328      1.11  christos 		if (strchr(fmt, '\n'))
    329      1.11  christos 			(void) fflush(tracefile);
    330      1.11  christos 	}
    331      1.11  christos 	va_end(va);
    332       1.1       cgd #endif
    333       1.1       cgd }
    334       1.1       cgd 
    335       1.1       cgd 
    336      1.12  christos #ifdef DEBUG
    337       1.7       cgd void
    338       1.1       cgd trputs(s)
    339  1.17.4.1  wrstuden 	const char *s;
    340       1.7       cgd {
    341       1.1       cgd 	if (tracefile == NULL)
    342       1.1       cgd 		return;
    343       1.1       cgd 	fputs(s, tracefile);
    344       1.1       cgd 	if (strchr(s, '\n'))
    345       1.1       cgd 		fflush(tracefile);
    346       1.1       cgd }
    347       1.1       cgd 
    348       1.1       cgd 
    349       1.8       cgd static void
    350       1.1       cgd trstring(s)
    351       1.1       cgd 	char *s;
    352       1.8       cgd {
    353      1.13       tls 	char *p;
    354       1.1       cgd 	char c;
    355       1.1       cgd 
    356       1.1       cgd 	if (tracefile == NULL)
    357       1.1       cgd 		return;
    358       1.1       cgd 	putc('"', tracefile);
    359       1.1       cgd 	for (p = s ; *p ; p++) {
    360       1.1       cgd 		switch (*p) {
    361       1.1       cgd 		case '\n':  c = 'n';  goto backslash;
    362       1.1       cgd 		case '\t':  c = 't';  goto backslash;
    363       1.1       cgd 		case '\r':  c = 'r';  goto backslash;
    364       1.1       cgd 		case '"':  c = '"';  goto backslash;
    365       1.1       cgd 		case '\\':  c = '\\';  goto backslash;
    366       1.1       cgd 		case CTLESC:  c = 'e';  goto backslash;
    367       1.1       cgd 		case CTLVAR:  c = 'v';  goto backslash;
    368       1.1       cgd 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
    369       1.1       cgd 		case CTLBACKQ:  c = 'q';  goto backslash;
    370       1.1       cgd 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
    371       1.1       cgd backslash:	  putc('\\', tracefile);
    372       1.1       cgd 			putc(c, tracefile);
    373       1.1       cgd 			break;
    374       1.1       cgd 		default:
    375       1.1       cgd 			if (*p >= ' ' && *p <= '~')
    376       1.1       cgd 				putc(*p, tracefile);
    377       1.1       cgd 			else {
    378       1.1       cgd 				putc('\\', tracefile);
    379       1.1       cgd 				putc(*p >> 6 & 03, tracefile);
    380       1.1       cgd 				putc(*p >> 3 & 07, tracefile);
    381       1.1       cgd 				putc(*p & 07, tracefile);
    382       1.1       cgd 			}
    383       1.1       cgd 			break;
    384       1.1       cgd 		}
    385       1.1       cgd 	}
    386       1.1       cgd 	putc('"', tracefile);
    387      1.12  christos }
    388       1.1       cgd #endif
    389       1.1       cgd 
    390       1.1       cgd 
    391       1.7       cgd void
    392       1.1       cgd trargs(ap)
    393       1.1       cgd 	char **ap;
    394       1.7       cgd {
    395       1.1       cgd #ifdef DEBUG
    396       1.1       cgd 	if (tracefile == NULL)
    397       1.1       cgd 		return;
    398       1.1       cgd 	while (*ap) {
    399       1.1       cgd 		trstring(*ap++);
    400       1.1       cgd 		if (*ap)
    401       1.1       cgd 			putc(' ', tracefile);
    402       1.1       cgd 		else
    403       1.1       cgd 			putc('\n', tracefile);
    404       1.1       cgd 	}
    405       1.1       cgd 	fflush(tracefile);
    406       1.1       cgd #endif
    407       1.1       cgd }
    408       1.1       cgd 
    409       1.1       cgd 
    410      1.12  christos #ifdef DEBUG
    411       1.7       cgd void
    412       1.1       cgd opentrace() {
    413       1.1       cgd 	char s[100];
    414      1.11  christos #ifdef O_APPEND
    415       1.1       cgd 	int flags;
    416      1.11  christos #endif
    417       1.1       cgd 
    418       1.1       cgd 	if (!debug)
    419       1.1       cgd 		return;
    420       1.5       jtc #ifdef not_this_way
    421      1.11  christos 	{
    422      1.11  christos 		char *p;
    423      1.11  christos 		if ((p = getenv("HOME")) == NULL) {
    424      1.11  christos 			if (geteuid() == 0)
    425      1.11  christos 				p = "/";
    426      1.11  christos 			else
    427      1.11  christos 				p = "/tmp";
    428      1.11  christos 		}
    429      1.11  christos 		scopy(p, s);
    430      1.11  christos 		strcat(s, "/trace");
    431       1.1       cgd 	}
    432       1.5       jtc #else
    433       1.5       jtc 	scopy("./trace", s);
    434       1.5       jtc #endif /* not_this_way */
    435       1.1       cgd 	if ((tracefile = fopen(s, "a")) == NULL) {
    436       1.1       cgd 		fprintf(stderr, "Can't open %s\n", s);
    437       1.1       cgd 		return;
    438       1.1       cgd 	}
    439       1.1       cgd #ifdef O_APPEND
    440       1.1       cgd 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
    441       1.1       cgd 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
    442       1.1       cgd #endif
    443       1.1       cgd 	fputs("\nTracing started.\n", tracefile);
    444       1.1       cgd 	fflush(tracefile);
    445      1.12  christos }
    446       1.5       jtc #endif /* DEBUG */
    447