Home | History | Annotate | Line # | Download | only in sh
show.c revision 1.30
      1 /*	$NetBSD: show.c,v 1.30 2016/02/27 23:50:13 christos 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 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: show.c,v 1.30 2016/02/27 23:50:13 christos Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <stdio.h>
     45 #include <stdarg.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 
     49 #include "shell.h"
     50 #include "parser.h"
     51 #include "nodes.h"
     52 #include "mystring.h"
     53 #include "show.h"
     54 #include "options.h"
     55 #ifndef SMALL
     56 #define DEFINE_NODENAMES
     57 #include "nodenames.h"
     58 #endif
     59 
     60 
     61 FILE *tracefile;
     62 
     63 #ifdef DEBUG
     64 static int shtree(union node *, int, int, char *, FILE*);
     65 static int shcmd(union node *, FILE *);
     66 static int sharg(union node *, FILE *);
     67 static int indent(int, char *, FILE *);
     68 static void trstring(char *);
     69 
     70 void
     71 showtree(union node *n)
     72 {
     73 	FILE *fp;
     74 
     75 	fp = tracefile ? tracefile : stdout;
     76 
     77 	trputs("showtree(");
     78 		if (n == NULL)
     79 			trputs("NULL");
     80 		else if (n == NEOF)
     81 			trputs("NEOF");
     82 	trputs(") called\n");
     83 	if (n != NULL && n != NEOF)
     84 		shtree(n, 1, 1, NULL, fp);
     85 }
     86 
     87 
     88 static int
     89 shtree(union node *n, int ind, int nl, char *pfx, FILE *fp)
     90 {
     91 	struct nodelist *lp;
     92 	const char *s;
     93 	int len;
     94 
     95 	if (n == NULL) {
     96 		if (nl)
     97 			fputc('\n', fp);
     98 		return 0;
     99 	}
    100 
    101 	len = indent(ind, pfx, fp);
    102 	switch (n->type) {
    103 	case NSEMI:
    104 		s = "; ";
    105 		len += 2;
    106 		goto binop;
    107 	case NAND:
    108 		s = " && ";
    109 		len += 4;
    110 		goto binop;
    111 	case NOR:
    112 		s = " || ";
    113 		len += 4;
    114 binop:
    115 		len += shtree(n->nbinary.ch1, 0, 0, NULL, fp);
    116 		fputs(s, fp);
    117 		if (len >= 60) {
    118 			putc('\n', fp);
    119 			len = indent(ind < 0 ? 2 : ind + 1, pfx, fp);
    120 		}
    121 		len += shtree(n->nbinary.ch2, 0, nl, NULL, fp);
    122 		break;
    123 	case NCMD:
    124 		len += shcmd(n, fp);
    125 		if (nl)
    126 			len = 0, putc('\n', fp);
    127 		break;
    128 	case NPIPE:
    129 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    130 			len += shcmd(lp->n, fp);
    131 			if (lp->next) {
    132 				len += 3, fputs(" | ", fp);
    133 				if (len >= 60)  {
    134 					fputc('\n', fp);
    135 					len = indent(ind < 0 ? 2 : ind + 1,
    136 					    pfx, fp);
    137 				}
    138 			}
    139 		}
    140 		if (n->npipe.backgnd)
    141 			len += 2, fputs(" &", fp);
    142 		if (nl || len >= 60)
    143 			len = 0, fputc('\n', fp);
    144 		break;
    145 	default:
    146 #ifdef NODETYPENAME
    147 		len += fprintf(fp, "<node type %d [%s]>", n->type,
    148 		    NODETYPENAME(n->type));
    149 #else
    150 		len += fprintf(fp, "<node type %d>", n->type);
    151 #endif
    152 		if (nl)
    153 			len = 0, putc('\n', fp);
    154 		break;
    155 	}
    156 	return len;
    157 }
    158 
    159 
    160 
    161 static int
    162 shcmd(union node *cmd, FILE *fp)
    163 {
    164 	union node *np;
    165 	int first;
    166 	const char *s;
    167 	int dftfd;
    168 	int len = 0;
    169 
    170 	first = 1;
    171 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
    172 		if (! first)
    173 			len++, fputc(' ', fp);
    174 		len += sharg(np, fp);
    175 		first = 0;
    176 	}
    177 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
    178 		if (! first)
    179 			len++, fputc(' ', fp);
    180 		switch (np->nfile.type) {
    181 			case NTO:	s = ">";  dftfd = 1; len += 1; break;
    182 			case NCLOBBER:	s = ">|"; dftfd = 1; len += 2; break;
    183 			case NAPPEND:	s = ">>"; dftfd = 1; len += 2; break;
    184 			case NTOFD:	s = ">&"; dftfd = 1; len += 2; break;
    185 			case NFROM:	s = "<";  dftfd = 0; len += 1; break;
    186 			case NFROMFD:	s = "<&"; dftfd = 0; len += 2; break;
    187 			case NFROMTO:	s = "<>"; dftfd = 0; len += 2; break;
    188 			default:   s = "*error*"; dftfd = 0; len += 7; break;
    189 		}
    190 		if (np->nfile.fd != dftfd)
    191 			len += fprintf(fp, "%d", np->nfile.fd);
    192 		fputs(s, fp);
    193 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
    194 			len += fprintf(fp, "%d", np->ndup.dupfd);
    195 		} else {
    196 			len += sharg(np->nfile.fname, fp);
    197 		}
    198 		first = 0;
    199 	}
    200 	return len;
    201 }
    202 
    203 
    204 
    205 static int
    206 sharg(union node *arg, FILE *fp)
    207 {
    208 	char *p;
    209 	struct nodelist *bqlist;
    210 	int subtype;
    211 	int len = 0;
    212 
    213 	if (arg->type != NARG) {
    214 		fprintf(fp, "<node type %d>\n", arg->type);
    215 		abort();
    216 	}
    217 	bqlist = arg->narg.backquote;
    218 	for (p = arg->narg.text ; *p ; p++) {
    219 		switch (*p) {
    220 		case CTLESC:
    221 			putc(*++p, fp);
    222 			len++;
    223 			break;
    224 		case CTLVAR:
    225 			putc('$', fp);
    226 			putc('{', fp);
    227 			len += 2;
    228 			subtype = *++p;
    229 			if (subtype == VSLENGTH)
    230 				len++, putc('#', fp);
    231 
    232 			while (*++p != '=')
    233 				len++, putc(*p, fp);
    234 
    235 			if (subtype & VSNUL)
    236 				len++, putc(':', fp);
    237 
    238 			switch (subtype & VSTYPE) {
    239 			case VSNORMAL:
    240 				putc('}', fp);
    241 				len++;
    242 				break;
    243 			case VSMINUS:
    244 				putc('-', fp);
    245 				len++;
    246 				break;
    247 			case VSPLUS:
    248 				putc('+', fp);
    249 				len++;
    250 				break;
    251 			case VSQUESTION:
    252 				putc('?', fp);
    253 				len++;
    254 				break;
    255 			case VSASSIGN:
    256 				putc('=', fp);
    257 				len++;
    258 				break;
    259 			case VSTRIMLEFT:
    260 				putc('#', fp);
    261 				len++;
    262 				break;
    263 			case VSTRIMLEFTMAX:
    264 				putc('#', fp);
    265 				putc('#', fp);
    266 				len += 2;
    267 				break;
    268 			case VSTRIMRIGHT:
    269 				putc('%', fp);
    270 				len++;
    271 				break;
    272 			case VSTRIMRIGHTMAX:
    273 				putc('%', fp);
    274 				putc('%', fp);
    275 				len += 2;
    276 				break;
    277 			case VSLENGTH:
    278 				break;
    279 			default:
    280 				len += fprintf(fp, "<subtype %d>", subtype);
    281 			}
    282 			break;
    283 		case CTLENDVAR:
    284 		     putc('}', fp);
    285 		     len++;
    286 		     break;
    287 		case CTLBACKQ:
    288 		case CTLBACKQ|CTLQUOTE:
    289 			putc('$', fp);
    290 			putc('(', fp);
    291 			len += shtree(bqlist->n, -1, 0, NULL, fp) + 3;
    292 			putc(')', fp);
    293 			break;
    294 		default:
    295 			putc(*p, fp);
    296 			len++;
    297 			break;
    298 		}
    299 	}
    300 	return len;
    301 }
    302 
    303 
    304 static int
    305 indent(int amount, char *pfx, FILE *fp)
    306 {
    307 	int i;
    308 	int len = 0;
    309 
    310 	/*
    311 	 * in practice, pfx is **always** NULL
    312 	 * but here, we assume if it were not, at least strlen(pfx) < 8
    313 	 * if that is invalid, output will look messy
    314 	 */
    315 	for (i = 0 ; i < amount ; i++) {
    316 		if (pfx && i == amount - 1)
    317 			fputs(pfx, fp);
    318 		putc('\t', fp);
    319 		len |= 7;
    320 		len++;
    321 	}
    322 	return len;
    323 }
    324 #endif
    325 
    326 
    327 
    328 /*
    329  * Debugging stuff.
    330  */
    331 
    332 
    333 
    334 
    335 #ifdef DEBUG
    336 void
    337 trputc(int c)
    338 {
    339 	if (debug != 1 || !tracefile)
    340 		return;
    341 	putc(c, tracefile);
    342 }
    343 #endif
    344 
    345 void
    346 trace(const char *fmt, ...)
    347 {
    348 #ifdef DEBUG
    349 	va_list va;
    350 
    351 	if (debug != 1 || !tracefile)
    352 		return;
    353 	va_start(va, fmt);
    354 	(void) vfprintf(tracefile, fmt, va);
    355 	va_end(va);
    356 #endif
    357 }
    358 
    359 void
    360 tracev(const char *fmt, va_list va)
    361 {
    362 #ifdef DEBUG
    363 	va_list ap;
    364 	if (debug != 1 || !tracefile)
    365 		return;
    366 	va_copy(ap, va);
    367 	(void) vfprintf(tracefile, fmt, ap);
    368 	va_end(ap);
    369 #endif
    370 }
    371 
    372 
    373 #ifdef DEBUG
    374 void
    375 trputs(const char *s)
    376 {
    377 	if (debug != 1 || !tracefile)
    378 		return;
    379 	fputs(s, tracefile);
    380 }
    381 
    382 
    383 static void
    384 trstring(char *s)
    385 {
    386 	char *p;
    387 	char c;
    388 
    389 	if (debug != 1 || !tracefile)
    390 		return;
    391 	putc('"', tracefile);
    392 	for (p = s ; *p ; p++) {
    393 		switch (*p) {
    394 		case '\n':  c = 'n';  goto backslash;
    395 		case '\t':  c = 't';  goto backslash;
    396 		case '\r':  c = 'r';  goto backslash;
    397 		case '"':  c = '"';  goto backslash;
    398 		case '\\':  c = '\\';  goto backslash;
    399 		case CTLESC:  c = 'e';  goto backslash;
    400 		case CTLVAR:  c = 'v';  goto backslash;
    401 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
    402 		case CTLBACKQ:  c = 'q';  goto backslash;
    403 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
    404 backslash:	  putc('\\', tracefile);
    405 			putc(c, tracefile);
    406 			break;
    407 		default:
    408 			if (*p >= ' ' && *p <= '~')
    409 				putc(*p, tracefile);
    410 			else {
    411 				putc('\\', tracefile);
    412 				putc(*p >> 6 & 03, tracefile);
    413 				putc(*p >> 3 & 07, tracefile);
    414 				putc(*p & 07, tracefile);
    415 			}
    416 			break;
    417 		}
    418 	}
    419 	putc('"', tracefile);
    420 }
    421 #endif
    422 
    423 
    424 void
    425 trargs(char **ap)
    426 {
    427 #ifdef DEBUG
    428 	if (debug != 1 || !tracefile)
    429 		return;
    430 	while (*ap) {
    431 		trstring(*ap++);
    432 		if (*ap)
    433 			putc(' ', tracefile);
    434 		else
    435 			putc('\n', tracefile);
    436 	}
    437 #endif
    438 }
    439 
    440 
    441 #ifdef DEBUG
    442 void
    443 opentrace(void)
    444 {
    445 	char s[100];
    446 #ifdef O_APPEND
    447 	int flags;
    448 #endif
    449 
    450 	if (debug != 1) {
    451 		if (tracefile)
    452 			fflush(tracefile);
    453 		/* leave open because libedit might be using it */
    454 		return;
    455 	}
    456 #ifdef not_this_way
    457 	{
    458 		char *p;
    459 		if ((p = getenv("HOME")) == NULL) {
    460 			if (geteuid() == 0)
    461 				p = "/";
    462 			else
    463 				p = "/tmp";
    464 		}
    465 		scopy(p, s);
    466 		strcat(s, "/trace");
    467 	}
    468 #else
    469 	snprintf(s, sizeof(s), "./trace.%d", (int)getpid());
    470 #endif /* not_this_way */
    471 	if (tracefile) {
    472 		if (!freopen(s, "a", tracefile)) {
    473 			fprintf(stderr, "Can't re-open %s\n", s);
    474 			tracefile = NULL;
    475 			debug = 0;
    476 			return;
    477 		}
    478 	} else {
    479 		if ((tracefile = fopen(s, "a")) == NULL) {
    480 			fprintf(stderr, "Can't open %s\n", s);
    481 			debug = 0;
    482 			return;
    483 		}
    484 	}
    485 #ifdef O_APPEND
    486 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
    487 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
    488 #endif
    489 	setlinebuf(tracefile);
    490 	fputs("\nTracing started.\n", tracefile);
    491 }
    492 #endif /* DEBUG */
    493