Home | History | Annotate | Line # | Download | only in sh
show.c revision 1.46
      1 /*	$NetBSD: show.c,v 1.46 2017/06/17 12:16:16 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Copyright (c) 2017 The NetBSD Foundation, Inc.  All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Kenneth Almquist.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
     41 #else
     42 __RCSID("$NetBSD: show.c,v 1.46 2017/06/17 12:16:16 kre Exp $");
     43 #endif
     44 #endif /* not lint */
     45 
     46 #include <stdio.h>
     47 #include <stdarg.h>
     48 #include <stdlib.h>
     49 #include <unistd.h>
     50 #include <fcntl.h>
     51 #include <errno.h>
     52 #include <limits.h>
     53 
     54 #include <sys/types.h>
     55 #include <sys/uio.h>
     56 
     57 #include "shell.h"
     58 #include "parser.h"
     59 #include "nodes.h"
     60 #include "mystring.h"
     61 #include "show.h"
     62 #include "options.h"
     63 #include "redir.h"
     64 #include "error.h"
     65 #include "syntax.h"
     66 #include "input.h"
     67 #include "output.h"
     68 #include "var.h"
     69 #include "builtins.h"
     70 
     71 #if defined(DEBUG) && !defined(DBG_PID)
     72 /*
     73  * If this is compiled, it means this is being compiled in a shell that still
     74  * has an older shell.h (a simpler TRACE() mechanism than is coming soon.)
     75  *
     76  * Compensate for as much of that as is missing and is needed here
     77  * to compile and operate at all.   After the other changes have appeared,
     78  * this little block can (and should be) deleted (sometime).
     79  *
     80  * Try to avoid waiting 22 years...
     81  */
     82 #define	DBG_PID		1
     83 #define	DBG_NEST	2
     84 #endif
     85 
     86 #define DEFINE_NODENAMES
     87 #include "nodenames.h"		/* does almost nothing if !defined(DEBUG) */
     88 
     89 #define	TR_STD_WIDTH	60	/* tend to fold lines wider than this */
     90 #define	TR_IOVECS	10	/* number of lines or trace (max) / write */
     91 
     92 typedef struct traceinfo {
     93 	int	tfd;	/* file descriptor for open trace file */
     94 	int	nxtiov;	/* the buffer we should be writing to */
     95 	char	lastc;	/* the last non-white character output */
     96 	uint8_t	supr;	/* char classes to suppress after \n */
     97 	pid_t	pid;	/* process id of process that opened that file */
     98 	size_t	llen;	/* number of chars in current output line */
     99 	size_t	blen;	/* chars used in current buffer being filled */
    100 	char *	tracefile;		/* name of the tracefile */
    101 	struct iovec lines[TR_IOVECS];	/* filled, flling, pending buffers */
    102 } TFILE;
    103 
    104 /* These are auto turned off when non white space is printed */
    105 #define	SUP_NL	0x01	/* don't print \n */
    106 #define	SUP_SP	0x03	/* suppress spaces */
    107 #define	SUP_WSP	0x04	/* suppress all white space */
    108 
    109 #ifdef DEBUG		/* from here to end of file ... */
    110 
    111 TFILE tracedata, *tracetfile;
    112 FILE *tracefile;		/* just for histedit */
    113 
    114 uint64_t	DFlags;		/* currently enabled debug flags */
    115 int ShNest;			/* depth of shell (internal) nesting */
    116 
    117 static void shtree(union node *, int, int, int, TFILE *);
    118 static void shcmd(union node *, TFILE *);
    119 static void shsubsh(union node *, TFILE *);
    120 static void shredir(union node *, TFILE *, int);
    121 static void sharg(union node *, TFILE *);
    122 static void indent(int, TFILE *);
    123 static void trstring(const char *);
    124 static void trace_putc(char, TFILE *);
    125 static void trace_puts(const char *, TFILE *);
    126 static void trace_flush(TFILE *, int);
    127 static char *trace_id(TFILE *);
    128 static void trace_fd_swap(int, int);
    129 
    130 inline static int trlinelen(TFILE *);
    131 
    132 
    133 /*
    134  * These functions are the externally visible interface
    135  *  (but only for a DEBUG shell.)
    136  */
    137 
    138 void
    139 opentrace(void)
    140 {
    141 	char *s;
    142 	int fd;
    143 	int i;
    144 	pid_t pid;
    145 
    146 	if (debug != 1) {
    147 		/* leave fd open because libedit might be using it */
    148 		if (tracefile)
    149 			fflush(tracefile);
    150 		if (tracetfile)
    151 			trace_flush(tracetfile, 1);
    152 		return;
    153 	}
    154 #if DBG_PID == 1		/* using old shell.h, old tracing method */
    155 	DFlags = DBG_PID;	/* just force DBG_PID on, and leave it ... */
    156 #endif
    157 	pid = getpid();
    158 	if (asprintf(&s, "trace.%jd", (intmax_t)pid) <= 0) {
    159 		debug = 0;
    160 		error("Cannot asprintf tracefilename");
    161 	};
    162 
    163 	fd = open(s, O_WRONLY|O_APPEND|O_CREAT, 0666);
    164 	if (fd == -1) {
    165 		debug = 0;
    166 		error("Can't open tracefile: %s (%s)\n", s, strerror(errno));
    167 	}
    168 	fd = to_upper_fd(fd);
    169 	if (fd <= 2) {
    170 		(void) close(fd);
    171 		debug = 0;
    172 		error("Attempt to use fd %d as tracefile thwarted\n", fd);
    173 	}
    174 	register_sh_fd(fd, trace_fd_swap);
    175 
    176 	/*
    177 	 * This stuff is just so histedit has a FILE * to use
    178 	 */
    179 	if (tracefile)
    180 		(void) fclose(tracefile);	/* also closes tfd */
    181 	tracefile = fdopen(fd, "a");	/* don't care if it is NULL */
    182 	if (tracefile)			/* except here... */
    183 		setlinebuf(tracefile);
    184 
    185 	/*
    186 	 * Now the real tracing setup
    187 	 */
    188 	if (tracedata.tfd > 0 && tracedata.tfd != fd)
    189 		(void) close(tracedata.tfd);	/* usually done by fclose() */
    190 
    191 	tracedata.tfd = fd;
    192 	tracedata.pid = pid;
    193 	tracedata.nxtiov = 0;
    194 	tracedata.blen = 0;
    195 	tracedata.llen = 0;
    196 	tracedata.lastc = '\0';
    197 	tracedata.supr = SUP_NL | SUP_WSP;
    198 
    199 #define	replace(f, v) do {				\
    200 		if (tracedata.f != NULL)		\
    201 			free(tracedata.f);		\
    202 		tracedata.f = v;			\
    203 	} while (/*CONSTCOND*/ 0)
    204 
    205 	replace(tracefile, s);
    206 
    207 	for (i = 0; i < TR_IOVECS; i++) {
    208 		replace(lines[i].iov_base, NULL);
    209 		tracedata.lines[i].iov_len = 0;
    210 	}
    211 
    212 #undef replace
    213 
    214 	tracetfile = &tracedata;
    215 
    216 	trace_puts("\nTracing started.\n", tracetfile);
    217 }
    218 
    219 void
    220 trace(const char *fmt, ...)
    221 {
    222 	va_list va;
    223 	char *s;
    224 
    225 	if (debug != 1 || !tracetfile)
    226 		return;
    227 	va_start(va, fmt);
    228 	(void) vasprintf(&s, fmt, va);
    229 	va_end(va);
    230 
    231 	trace_puts(s, tracetfile);
    232 	free(s);
    233 	if (tracetfile->llen == 0)
    234 		trace_flush(tracetfile, 0);
    235 }
    236 
    237 void
    238 tracev(const char *fmt, va_list va)
    239 {
    240 	va_list ap;
    241 	char *s;
    242 
    243 	if (debug != 1 || !tracetfile)
    244 		return;
    245 	va_copy(ap, va);
    246 	(void) vasprintf(&s, fmt, ap);
    247 	va_end(ap);
    248 
    249 	trace_puts(s, tracetfile);
    250 	free(s);
    251 	if (tracetfile->llen == 0)
    252 		trace_flush(tracetfile, 0);
    253 }
    254 
    255 
    256 void
    257 trputs(const char *s)
    258 {
    259 	if (debug != 1 || !tracetfile)
    260 		return;
    261 	trace_puts(s, tracetfile);
    262 }
    263 
    264 void
    265 trputc(int c)
    266 {
    267 	if (debug != 1 || !tracetfile)
    268 		return;
    269 	trace_putc(c, tracetfile);
    270 }
    271 
    272 void
    273 showtree(union node *n)
    274 {
    275 	TFILE *fp;
    276 
    277 	if ((fp = tracetfile) == NULL)
    278 		return;
    279 
    280 	trace_puts("showtree(", fp);
    281 		if (n == NULL)
    282 			trace_puts("NULL", fp);
    283 		else if (n == NEOF)
    284 			trace_puts("NEOF", fp);
    285 		else
    286 			trace("%p", n);
    287 	trace_puts(") called\n", fp);
    288 	if (n != NULL && n != NEOF)
    289 		shtree(n, 1, 1, 1, fp);
    290 }
    291 
    292 void
    293 trargs(char **ap)
    294 {
    295 	if (debug != 1 || !tracetfile)
    296 		return;
    297 	while (*ap) {
    298 		trstring(*ap++);
    299 		if (*ap)
    300 			trace_putc(' ', tracetfile);
    301 	}
    302 	trace_putc('\n', tracetfile);
    303 }
    304 
    305 
    306 /*
    307  * Beyond here we just have the implementation of all of that
    308  */
    309 
    310 
    311 inline static int
    312 trlinelen(TFILE * fp)
    313 {
    314 	return fp->llen;
    315 }
    316 
    317 static void
    318 shtree(union node *n, int ind, int ilvl, int nl, TFILE *fp)
    319 {
    320 	struct nodelist *lp;
    321 	const char *s;
    322 
    323 	if (n == NULL) {
    324 		if (nl)
    325 			trace_putc('\n', fp);
    326 		return;
    327 	}
    328 
    329 	indent(ind, fp);
    330 	switch (n->type) {
    331 	case NSEMI:
    332 		s = NULL;
    333 		goto binop;
    334 	case NAND:
    335 		s = " && ";
    336 		goto binop;
    337 	case NOR:
    338 		s = " || ";
    339 binop:
    340 		shtree(n->nbinary.ch1, 0, ilvl, 0, fp);
    341 		if (s != NULL)
    342 			trace_puts(s, fp);
    343 		if (trlinelen(fp) >= TR_STD_WIDTH) {
    344 			trace_putc('\n', fp);
    345 			indent(ind < 0 ? 2 : ind + 1, fp);
    346 		} else if (s == NULL) {
    347 			if (fp->lastc != '&')
    348 				trace_puts("; ", fp);
    349 			else
    350 				trace_putc(' ', fp);
    351 		}
    352 		shtree(n->nbinary.ch2, 0, ilvl, nl, fp);
    353 		break;
    354 	case NCMD:
    355 		shcmd(n, fp);
    356 		if (n->ncmd.backgnd)
    357 			trace_puts(" &", fp);
    358 		if (nl && trlinelen(fp) > 0)
    359 			trace_putc('\n', fp);
    360 		break;
    361 	case NPIPE:
    362 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
    363 			shtree(lp->n, 0, ilvl, 0, fp);
    364 			if (lp->next) {
    365 				trace_puts(" |", fp);
    366 				if (trlinelen(fp) >= TR_STD_WIDTH)  {
    367 					trace_putc('\n', fp);
    368 					indent((ind < 0 ? ilvl : ind) + 1, fp);
    369 				} else
    370 					trace_putc(' ', fp);
    371 			}
    372 		}
    373 		if (n->npipe.backgnd)
    374 			trace_puts(" &", fp);
    375 		if (nl || trlinelen(fp) >= TR_STD_WIDTH)
    376 			trace_putc('\n', fp);
    377 		break;
    378 	case NBACKGND:
    379 	case NSUBSHELL:
    380 		shsubsh(n, fp);
    381 		if (n->type == NBACKGND)
    382 			trace_puts(" &", fp);
    383 		if (nl && trlinelen(fp) > 0)
    384 			trace_putc('\n', fp);
    385 		break;
    386 	case NDEFUN:
    387 		trace_puts(n->narg.text, fp);
    388 		trace_puts("() {\n", fp);
    389 		indent(ind, fp);
    390 		shtree(n->narg.next, (ind < 0 ? ilvl : ind) + 1, ilvl+1, 1, fp);
    391 		indent(ind, fp);
    392 		trace_puts("}\n", fp);
    393 		break;
    394 	case NDNOT:
    395 		trace_puts("! ", fp);
    396 		/* FALLTHROUGH */
    397 	case NNOT:
    398 		trace_puts("! ", fp);
    399 		shtree(n->nnot.com, -1, ilvl, nl, fp);
    400 		break;
    401 	case NREDIR:
    402 		shtree(n->nredir.n, -1, ilvl, 0, fp);
    403 		shredir(n->nredir.redirect, fp, n->nredir.n == NULL);
    404 		if (nl)
    405 			trace_putc('\n', fp);
    406 		break;
    407 
    408 	case NIF:
    409 	itsif:
    410 		trace_puts("if ", fp);
    411 		shtree(n->nif.test, -1, ilvl, 0, fp);
    412 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
    413 			if (fp->lastc != '&')
    414 				trace_puts(" ;", fp);
    415 		} else
    416 			indent(ilvl, fp);
    417 		trace_puts(" then ", fp);
    418 		if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
    419 			indent(ilvl+1, fp);
    420 		shtree(n->nif.ifpart, -1, ilvl + 1, 0, fp);
    421 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
    422 			if (fp->lastc != '&')
    423 				trace_puts(" ;", fp);
    424 		} else
    425 			indent(ilvl, fp);
    426 		if (n->nif.elsepart && n->nif.elsepart->type == NIF) {
    427 			if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
    428 				indent(ilvl, fp);
    429 			n = n->nif.elsepart;
    430 			trace_puts(" el", fp);
    431 			goto itsif;
    432 		}
    433 		if (n->nif.elsepart) {
    434 			if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
    435 				indent(ilvl+1, fp);
    436 			trace_puts(" else ", fp);
    437 			shtree(n->nif.elsepart, -1, ilvl + 1, 0, fp);
    438 			if (fp->lastc != '&')
    439 				trace_puts(" ;", fp);
    440 		}
    441 		trace_puts(" fi", fp);
    442 		if (nl)
    443 			trace_putc('\n', fp);
    444 		break;
    445 
    446 	case NWHILE:
    447 		trace_puts("while ", fp);
    448 		goto aloop;
    449 	case NUNTIL:
    450 		trace_puts("until ", fp);
    451 	aloop:
    452 		shtree(n->nbinary.ch1, -1, ilvl, 0, fp);
    453 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
    454 			if (fp->lastc != '&')
    455 				trace_puts(" ;", fp);
    456 		} else
    457 			trace_putc('\n', fp);
    458 		trace_puts(" do ", fp);
    459 		shtree(n->nbinary.ch1, -1, ilvl + 1, 1, fp);
    460 		trace_puts(" done ", fp);
    461 		if (nl)
    462 			trace_putc('\n', fp);
    463 		break;
    464 
    465 	case NFOR:
    466 		trace_puts("for ", fp);
    467 		trace_puts(n->nfor.var, fp);
    468 		if (n->nfor.args) {
    469 			union node *argp;
    470 
    471 			trace_puts(" in ", fp);
    472 			for (argp = n->nfor.args; argp; argp=argp->narg.next) {
    473 				sharg(argp, fp);
    474 				trace_putc(' ', fp);
    475 			}
    476 			if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
    477 				if (fp->lastc != '&')
    478 					trace_putc(';', fp);
    479 			} else
    480 				trace_putc('\n', fp);
    481 		}
    482 		trace_puts(" do ", fp);
    483 		shtree(n->nfor.body, -1, ilvl + 1, 0, fp);
    484 		if (fp->lastc != '&')
    485 			trace_putc(';', fp);
    486 		trace_puts(" done", fp);
    487 		if (nl)
    488 			trace_putc('\n', fp);
    489 		break;
    490 
    491 	case NCASE:
    492 		trace_puts("case ", fp);
    493 		sharg(n->ncase.expr, fp);
    494 		trace_puts(" in", fp);
    495 		if (nl)
    496 			trace_putc('\n', fp);
    497 		{
    498 			union node *cp;
    499 
    500 			for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
    501 				union node *patp;
    502 
    503 				if (nl || trlinelen(fp) > TR_STD_WIDTH - 16)
    504 					indent(ilvl, fp);
    505 				else
    506 					trace_putc(' ', fp);
    507 				trace_putc('(', fp);
    508 				patp = cp->nclist.pattern;
    509 				while (patp != NULL) {
    510 				    trace_putc(' ', fp);
    511 				    sharg(patp, fp);
    512 				    trace_putc(' ', fp);
    513 				    if ((patp = patp->narg.next) != NULL)
    514 					trace_putc('|', fp);
    515 				}
    516 				trace_putc(')', fp);
    517 				if (nl)
    518 					indent(ilvl + 1, fp);
    519 				else
    520 					trace_putc(' ', fp);
    521 				shtree(cp->nclist.body, -1, ilvl+2, 0, fp);
    522 				if (cp->type == NCLISTCONT)
    523 					trace_puts(" ;&", fp);
    524 				else
    525 					trace_puts(" ;;", fp);
    526 				if (nl)
    527 					trace_putc('\n', fp);
    528 			}
    529 		}
    530 		if (nl) {
    531 			trace_putc('\n', fp);
    532 			indent(ind, fp);
    533 		} else
    534 			trace_putc(' ', fp);
    535 		trace_puts("esac", fp);
    536 		if (nl)
    537 			trace_putc('\n', fp);
    538 		break;
    539 
    540 	default: {
    541 			char *str;
    542 
    543 			asprintf(&str, "<node type %d [%s]>", n->type,
    544 			    NODETYPENAME(n->type));
    545 			trace_puts(str, fp);
    546 			free(str);
    547 			if (nl)
    548 				trace_putc('\n', fp);
    549 		}
    550 		break;
    551 	}
    552 }
    553 
    554 
    555 static void
    556 shcmd(union node *cmd, TFILE *fp)
    557 {
    558 	union node *np;
    559 	int first;
    560 
    561 	first = 1;
    562 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
    563 		if (! first)
    564 			trace_putc(' ', fp);
    565 		sharg(np, fp);
    566 		first = 0;
    567 	}
    568 	shredir(cmd->ncmd.redirect, fp, first);
    569 }
    570 
    571 static void
    572 shsubsh(union node *cmd, TFILE *fp)
    573 {
    574 	trace_puts(" ( ", fp);
    575 	shtree(cmd->nredir.n, -1, 3, 0, fp);
    576 	trace_puts(" ) ", fp);
    577 	shredir(cmd->ncmd.redirect, fp, 1);
    578 }
    579 
    580 static void
    581 shredir(union node *np, TFILE *fp, int first)
    582 {
    583 	const char *s;
    584 	int dftfd;
    585 	char buf[106];
    586 
    587 	for ( ; np ; np = np->nfile.next) {
    588 		if (! first)
    589 			trace_putc(' ', fp);
    590 		switch (np->nfile.type) {
    591 			case NTO:	s = ">";  dftfd = 1; break;
    592 			case NCLOBBER:	s = ">|"; dftfd = 1; break;
    593 			case NAPPEND:	s = ">>"; dftfd = 1; break;
    594 			case NTOFD:	s = ">&"; dftfd = 1; break;
    595 			case NFROM:	s = "<";  dftfd = 0; break;
    596 			case NFROMFD:	s = "<&"; dftfd = 0; break;
    597 			case NFROMTO:	s = "<>"; dftfd = 0; break;
    598 			case NXHERE:	/* FALLTHROUGH */
    599 			case NHERE:	s = "<<"; dftfd = 0; break;
    600 			default:   s = "*error*"; dftfd = 0; break;
    601 		}
    602 		if (np->nfile.fd != dftfd) {
    603 			sprintf(buf, "%d", np->nfile.fd);
    604 			trace_puts(buf, fp);
    605 		}
    606 		trace_puts(s, fp);
    607 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
    608 			if (np->ndup.vname)
    609 				sharg(np->ndup.vname, fp);
    610 			else {
    611 				sprintf(buf, "%d", np->ndup.dupfd);
    612 				trace_puts(buf, fp);
    613 			}
    614 		} else
    615 		    if (np->nfile.type == NHERE || np->nfile.type == NXHERE) {
    616 			if (np->nfile.type == NHERE)
    617 				trace_putc('\\', fp);
    618 			trace_puts("!!!\n", fp);
    619 			s = np->nhere.doc->narg.text;
    620 			if (strlen(s) > 100) {
    621 				memmove(buf, s, 100);
    622 				buf[100] = '\0';
    623 				strcat(buf, " ...\n");
    624 				s = buf;
    625 			}
    626 			trace_puts(s, fp);
    627 			trace_puts("!!! ", fp);
    628 		} else {
    629 			sharg(np->nfile.fname, fp);
    630 		}
    631 		first = 0;
    632 	}
    633 }
    634 
    635 static void
    636 sharg(union node *arg, TFILE *fp)
    637 {
    638 	char *p, *s;
    639 	struct nodelist *bqlist;
    640 	int subtype = 0;
    641 	int quoted = 0;
    642 
    643 	if (arg->type != NARG) {
    644 		asprintf(&s, "<node type %d> ! NARG\n", arg->type);
    645 		trace_puts(s, fp);
    646 		abort();	/* no need to free s, better not to */
    647 	}
    648 
    649 	bqlist = arg->narg.backquote;
    650 	for (p = arg->narg.text ; *p ; p++) {
    651 		switch (*p) {
    652 		case CTLESC:
    653 			trace_putc('\\', fp);
    654 			trace_putc(*++p, fp);
    655 			break;
    656 
    657 		case CTLNONL:
    658 			trace_putc('\\', fp);
    659 			trace_putc('\n', fp);
    660 			break;
    661 
    662 		case CTLVAR:
    663 			subtype = *++p;
    664 			if (!quoted != !(subtype & VSQUOTE))
    665 				trace_putc('"', fp);
    666 			trace_putc('$', fp);
    667 			trace_putc('{', fp);	/*}*/
    668 			if ((subtype & VSTYPE) == VSLENGTH)
    669 				trace_putc('#', fp);
    670 			if (subtype & VSLINENO)
    671 				trace_puts("LINENO=", fp);
    672 
    673 			while (*++p != '=')
    674 				trace_putc(*p, fp);
    675 
    676 			if (subtype & VSNUL)
    677 				trace_putc(':', fp);
    678 
    679 			switch (subtype & VSTYPE) {
    680 			case VSNORMAL:
    681 				/* { */
    682 				trace_putc('}', fp);
    683 				if (!quoted != !(subtype & VSQUOTE))
    684 					trace_putc('"', fp);
    685 				break;
    686 			case VSMINUS:
    687 				trace_putc('-', fp);
    688 				break;
    689 			case VSPLUS:
    690 				trace_putc('+', fp);
    691 				break;
    692 			case VSQUESTION:
    693 				trace_putc('?', fp);
    694 				break;
    695 			case VSASSIGN:
    696 				trace_putc('=', fp);
    697 				break;
    698 			case VSTRIMLEFTMAX:
    699 				trace_putc('#', fp);
    700 				/* FALLTHROUGH */
    701 			case VSTRIMLEFT:
    702 				trace_putc('#', fp);
    703 				break;
    704 			case VSTRIMRIGHTMAX:
    705 				trace_putc('%', fp);
    706 				/* FALLTHROUGH */
    707 			case VSTRIMRIGHT:
    708 				trace_putc('%', fp);
    709 				break;
    710 			case VSLENGTH:
    711 				break;
    712 			default: {
    713 					char str[32];
    714 
    715 					snprintf(str, sizeof str,
    716 					    "<subtype %d>", subtype);
    717 					trace_puts(str, fp);
    718 				}
    719 				break;
    720 			}
    721 			break;
    722 		case CTLENDVAR:
    723 			/* { */
    724 			trace_putc('}', fp);
    725 			if (!quoted != !(subtype & VSQUOTE))
    726 				trace_putc('"', fp);
    727 			subtype = 0;
    728 			break;
    729 
    730 		case CTLBACKQ|CTLQUOTE:
    731 			if (!quoted)
    732 				trace_putc('"', fp);
    733 			/* FALLTHRU */
    734 		case CTLBACKQ:
    735 			trace_putc('$', fp);
    736 			trace_putc('(', fp);
    737 			if (bqlist) {
    738 				shtree(bqlist->n, -1, 3, 0, fp);
    739 				bqlist = bqlist->next;
    740 			} else
    741 				trace_puts("???", fp);
    742 			trace_putc(')', fp);
    743 			if (!quoted && *p == (CTLBACKQ|CTLQUOTE))
    744 				trace_putc('"', fp);
    745 			break;
    746 
    747 		case CTLQUOTEMARK:
    748 			if (subtype != 0 || !quoted) {
    749 				trace_putc('"', fp);
    750 				quoted++;
    751 			}
    752 			break;
    753 		case CTLQUOTEEND:
    754 			trace_putc('"', fp);
    755 			quoted--;
    756 			break;
    757 		case CTLARI:
    758 			if (*p == ' ')
    759 				p++;
    760 			trace_puts("$(( ", fp);
    761 			break;
    762 		case CTLENDARI:
    763 			trace_puts(" ))", fp);
    764 			break;
    765 
    766 		default:
    767 			if (*p == '$')
    768 				trace_putc('\\', fp);
    769 			trace_putc(*p, fp);
    770 			break;
    771 		}
    772 	}
    773 	if (quoted)
    774 		trace_putc('"', fp);
    775 }
    776 
    777 
    778 static void
    779 indent(int amount, TFILE *fp)
    780 {
    781 	int i;
    782 
    783 	if (amount <= 0)
    784 		return;
    785 
    786 	amount <<= 2;	/* indent slots -> chars */
    787 
    788 	i = trlinelen(fp);
    789 	fp->supr = SUP_NL;
    790 	if (i > amount) {
    791 		trace_putc('\n', fp);
    792 		i = 0;
    793 	}
    794 	fp->supr = 0;
    795 	for (; i < amount - 7 ; i++) {
    796 		trace_putc('\t', fp);
    797 		i |= 7;
    798 	}
    799 	while (i < amount) {
    800 		trace_putc(' ', fp);
    801 		i++;
    802 	}
    803 	fp->supr = SUP_WSP;
    804 }
    805 
    806 static void
    807 trace_putc(char c, TFILE *fp)
    808 {
    809 	char *p;
    810 
    811 	if (c == '\0')
    812 		return;
    813 	if (debug == 0 || fp == NULL)
    814 		return;
    815 
    816 	if (fp->llen == 0) {
    817 		if (fp->blen != 0)
    818 			abort();
    819 
    820 		if ((fp->supr & SUP_NL) && c == '\n')
    821 			return;
    822 		if ((fp->supr & (SUP_WSP|SUP_SP)) && c == ' ')
    823 			return;
    824 		if ((fp->supr & SUP_WSP) && c == '\t')
    825 			return;
    826 
    827 		if (fp->nxtiov >= TR_IOVECS - 1)	/* should be rare */
    828 			trace_flush(fp, 0);
    829 
    830 		p = trace_id(fp);
    831 		if (p != NULL) {
    832 			fp->lines[fp->nxtiov].iov_base = p;
    833 			fp->lines[fp->nxtiov].iov_len = strlen(p);
    834 			fp->nxtiov++;
    835 		}
    836 	} else if (fp->blen && fp->blen >= fp->lines[fp->nxtiov].iov_len) {
    837 		fp->blen = 0;
    838 		if (++fp->nxtiov >= TR_IOVECS)
    839 			trace_flush(fp, 0);
    840 	}
    841 
    842 	if (fp->lines[fp->nxtiov].iov_len == 0) {
    843 		p = (char *)malloc(2 * TR_STD_WIDTH);
    844 		if (p == NULL) {
    845 			trace_flush(fp, 1);
    846 			debug = 0;
    847 			return;
    848 		}
    849 		*p = '\0';
    850 		fp->lines[fp->nxtiov].iov_base = p;
    851 		fp->lines[fp->nxtiov].iov_len = 2 * TR_STD_WIDTH;
    852 		fp->blen = 0;
    853 	}
    854 
    855 	p = (char *)fp->lines[fp->nxtiov].iov_base + fp->blen++;
    856 	*p++ = c;
    857 	*p = 0;
    858 
    859 	if (c != ' ' && c != '\t' && c != '\n') {
    860 		fp->lastc = c;
    861 		fp->supr = 0;
    862 	}
    863 
    864 	if (c == '\n') {
    865 		fp->lines[fp->nxtiov++].iov_len = fp->blen;
    866 		fp->blen = 0;
    867 		fp->llen = 0;
    868 		fp->supr |= SUP_NL;
    869 		return;
    870 	}
    871 
    872 	if (c == '\t')
    873 		fp->llen |=  7;
    874 	fp->llen++;
    875 }
    876 
    877 void
    878 trace_flush(TFILE *fp, int all)
    879 {
    880 	int niov, i;
    881 	ssize_t written;
    882 
    883 	niov = fp->nxtiov;
    884 	if (all && fp->blen > 0) {
    885 		fp->lines[niov].iov_len = fp->blen;
    886 		fp->blen = 0;
    887 		fp->llen = 0;
    888 		niov++;
    889 	}
    890 	if (niov == 0)
    891 		return;
    892 	if (fp->blen > 0 && --niov == 0)
    893 		return;
    894 	written = writev(fp->tfd, fp->lines, niov);
    895 	for (i = 0; i < niov; i++) {
    896 		free(fp->lines[i].iov_base);
    897 		fp->lines[i].iov_base = NULL;
    898 		fp->lines[i].iov_len = 0;
    899 	}
    900 	if (written == -1) {
    901 		if (fp->blen > 0) {
    902 			free(fp->lines[niov].iov_base);
    903 			fp->lines[niov].iov_base = NULL;
    904 			fp->lines[niov].iov_len = 0;
    905 		}
    906 		debug = 0;
    907 		fp->blen = 0;
    908 		fp->llen = 0;
    909 		return;
    910 	}
    911 	if (fp->blen > 0) {
    912 		fp->lines[0].iov_base = fp->lines[niov].iov_base;
    913 		fp->lines[0].iov_len = fp->lines[niov].iov_len;
    914 		fp->lines[niov].iov_base = NULL;
    915 		fp->lines[niov].iov_len = 0;
    916 	}
    917 	fp->nxtiov = 0;
    918 }
    919 
    920 void
    921 trace_puts(const char *s, TFILE *fp)
    922 {
    923 	char c;
    924 
    925 	while ((c = *s++) != '\0')
    926 		trace_putc(c, fp);
    927 }
    928 
    929 inline static char *
    930 trace_id(TFILE *tf)
    931 {
    932 	int i;
    933 	char indent[16];
    934 	char *p;
    935 	int lno;
    936 	char c;
    937 
    938 	if (DFlags & DBG_NEST) {
    939 		if ((unsigned)ShNest >= sizeof indent - 1) {
    940 			(void) snprintf(indent, sizeof indent,
    941 			    "### %*d ###", (int)(sizeof indent) - 9, ShNest);
    942 			p = strchr(indent, '\0');
    943 		} else {
    944 			p = indent;
    945 			for (i = 0; i < 6; i++)
    946 				*p++ = (i < ShNest) ? '#' : ' ';
    947 			while (i++ < ShNest && p < &indent[sizeof indent - 1])
    948 				*p++ = '#';
    949 			*p = '\0';
    950 		}
    951 	} else
    952 		indent[0] = '\0';
    953 
    954 	/*
    955 	 * If we are in the parser, then plinno is the current line
    956 	 * number being processed (parser line no).
    957 	 * If we are elsewhere, then line_number gives the source
    958 	 * line of whatever we are currently doing (close enough.)
    959 	 */
    960 	if (parsing)
    961 		lno = plinno;
    962 	else
    963 		lno = line_number;
    964 
    965 	c = ((i = getpid()) == tf->pid) ? ':' : '=';
    966 
    967 	if (DFlags & DBG_PID) {
    968 		if (DFlags & DBG_LINE)
    969 			(void) asprintf(&p, "%5d%c%s\t%4d%c@\t", i, c,
    970 			    indent, lno, parsing?'-':'+');
    971 		else
    972 			(void) asprintf(&p, "%5d%c%s\t", i, c, indent);
    973 		return p;
    974 	} else if (DFlags & DBG_NEST) {
    975 		if (DFlags & DBG_LINE)
    976 			(void) asprintf(&p, "%c%s\t%4d%c@\t", c, indent, lno,
    977 			    parsing?'-':'+');
    978 		else
    979 			(void) asprintf(&p, "%c%s\t", c, indent);
    980 		return p;
    981 	} else if (DFlags & DBG_LINE) {
    982 		(void) asprintf(&p, "%c%4d%c@\t", c, lno, parsing?'-':'+');
    983 		return p;
    984 	}
    985 	return NULL;
    986 }
    987 
    988 /*
    989  * Used only from trargs(), which itself is used only to print
    990  * arg lists (argv[]) either that passed into this shell, or
    991  * the arg list about to be given to some other command (incl
    992  * builtin, and function) as their argv[].  If any of the CTL*
    993  * chars seem to appear, they really should be just treated as data,
    994  * not special...   But this is just debug, so, who cares!
    995  */
    996 static void
    997 trstring(const char *s)
    998 {
    999 	const char *p;
   1000 	char c;
   1001 	TFILE *fp;
   1002 
   1003 	if (debug != 1 || !tracetfile)
   1004 		return;
   1005 	fp = tracetfile;
   1006 	trace_putc('"', fp);
   1007 	for (p = s ; *p ; p++) {
   1008 		switch (*p) {
   1009 		case '\n':  c = 'n';  goto backslash;
   1010 		case '\t':  c = 't';  goto backslash;
   1011 		case '\r':  c = 'r';  goto backslash;
   1012 		case '"':  c = '"';  goto backslash;
   1013 		case '\\':  c = '\\';  goto backslash;
   1014 		case CTLESC:  c = 'e';  goto backslash;
   1015 		case CTLVAR:  c = 'v';  goto backslash;
   1016 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
   1017 		case CTLBACKQ:  c = 'q';  goto backslash;
   1018 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
   1019 backslash:		trace_putc('\\', fp);
   1020 			trace_putc(c, fp);
   1021 			break;
   1022 		default:
   1023 			if (*p >= ' ' && *p <= '~')
   1024 				trace_putc(*p, fp);
   1025 			else {
   1026 				trace_putc('\\', fp);
   1027 				trace_putc(*p >> 6 & 03, fp);
   1028 				trace_putc(*p >> 3 & 07, fp);
   1029 				trace_putc(*p & 07, fp);
   1030 			}
   1031 			break;
   1032 		}
   1033 	}
   1034 	trace_putc('"', fp);
   1035 }
   1036 
   1037 /*
   1038  * deal with the user "accidentally" picking our fd to use.
   1039  */
   1040 static void
   1041 trace_fd_swap(int from, int to)
   1042 {
   1043 	if (tracetfile == NULL || from == to)
   1044 		return;
   1045 
   1046 	tracetfile->tfd = to;
   1047 
   1048 	/*
   1049 	 * This is just so histedit has a stdio FILE* to use.
   1050 	 */
   1051 	if (tracefile)
   1052 		fclose(tracefile);
   1053 	tracefile = fdopen(to, "a");
   1054 	if (tracefile)
   1055 		setlinebuf(tracefile);
   1056 }
   1057 
   1058 
   1059 static struct debug_flag {
   1060 	char		label;
   1061 	uint64_t	flag;
   1062 } debug_flags[] = {
   1063 	{ 'a',	DBG_ARITH	},	/* arithmetic ( $(( )) ) */
   1064 	{ 'c',	DBG_CMDS	},	/* command searching, ... */
   1065 	{ 'e',	DBG_EVAL	},	/* evaluation of the parse tree */
   1066 	{ 'f',	DBG_REDIR	},	/* file descriptors & redirections */
   1067 	{ 'h',	DBG_HISTORY	},	/* history & cmd line editing */
   1068 	{ 'i',	DBG_INPUT	},	/* shell input routines */
   1069 	{ 'j',	DBG_JOBS	},	/* job control, structures */
   1070 	{ 'm',	DBG_MEM		},	/* memory management */
   1071 	{ 'o',	DBG_OUTPUT	},	/* output routines */
   1072 	{ 'p',	DBG_PROCS	},	/* process management, fork, ... */
   1073 	{ 'r',	DBG_PARSE	},	/* parser, lexer, ... tree building */
   1074 	{ 's',	DBG_SIG		},	/* signals and everything related */
   1075 	{ 't',	DBG_TRAP	},	/* traps & signals */
   1076 	{ 'v',	DBG_VARS	},	/* variables and parameters */
   1077 	{ 'w',	DBG_WAIT	},	/* waits for processes to finish */
   1078 	{ 'x',	DBG_EXPAND	},	/* word expansion ${} $() $(( )) */
   1079 	{ 'z',	DBG_ERRS	},	/* error control, jumps, cleanup */
   1080 
   1081 	{ '0',	DBG_U0		},	/* ad-hoc temp debug flag #0 */
   1082 	{ '1',	DBG_U1		},	/* ad-hoc temp debug flag #1 */
   1083 	{ '2',	DBG_U2		},	/* ad-hoc temp debug flag #2 */
   1084 
   1085 	{ '@',	DBG_LINE	},	/* prefix trace lines with line# */
   1086 	{ '$',	DBG_PID		},	/* prefix trace lines with sh pid */
   1087 	{ '^',	DBG_NEST	},	/* show shell nesting level */
   1088 
   1089 			/* alpha options only */
   1090 	{ '_',	DBG_PARSE | DBG_EVAL | DBG_EXPAND | DBG_JOBS |
   1091 		    DBG_PROCS | DBG_REDIR | DBG_CMDS | DBG_ERRS |
   1092 		    DBG_WAIT | DBG_TRAP | DBG_VARS | DBG_MEM |
   1093 		    DBG_INPUT | DBG_OUTPUT | DBG_ARITH | DBG_HISTORY },
   1094 
   1095    /*   { '*',	DBG_ALLVERBOSE	}, 	   is handled in the code */
   1096 
   1097 	{ '#',	DBG_U0 | DBG_U1 | DBG_U2 },
   1098 
   1099 	{ 0,	0		}
   1100 };
   1101 
   1102 void
   1103 set_debug(const char * flags, int on)
   1104 {
   1105 	char f;
   1106 	struct debug_flag *df;
   1107 	int verbose;
   1108 
   1109 	while ((f = *flags++) != '\0') {
   1110 		verbose = 0;
   1111 		if (is_upper(f)) {
   1112 			verbose = 1;
   1113 			f += 'a' - 'A';
   1114 		}
   1115 		if (f == '*')
   1116 			f = '_', verbose = 1;
   1117 		if (f == '+') {
   1118 			if (*flags == '+')
   1119 				flags++, verbose=1;
   1120 		}
   1121 
   1122 		/*
   1123 		 * Note: turning on any debug option also enables DBG_ALWAYS
   1124 		 * turning on any verbose option also enables DBG_VERBOSE
   1125 		 * Once enabled, those flags cannot be disabled.
   1126 		 * (tracing can still be turned off with "set +o debug")
   1127 		 */
   1128 		for (df = debug_flags; df->label != '\0'; df++) {
   1129 			if (f == '+' || df->label == f) {
   1130 				if (on) {
   1131 					DFlags |= DBG_ALWAYS | df->flag;
   1132 					if (verbose)
   1133 					    DFlags |= DBG_VERBOSE |
   1134 						(df->flag << DBG_VBOSE_SHIFT);
   1135 				} else {
   1136 					DFlags &= ~(df->flag<<DBG_VBOSE_SHIFT);
   1137 					if (!verbose)
   1138 						DFlags &= ~df->flag;
   1139 				}
   1140 			}
   1141 		}
   1142 	}
   1143 }
   1144 
   1145 
   1146 int
   1147 debugcmd(int argc, char **argv)
   1148 {
   1149 	if (argc == 1) {
   1150 		struct debug_flag *df;
   1151 
   1152 		out1fmt("Debug: %sabled.  Flags: ", debug ? "en" : "dis");
   1153 		for (df = debug_flags; df->label != '\0'; df++) {
   1154 			if (df->flag & (df->flag - 1))
   1155 				continue;
   1156 			if (is_alpha(df->label) &&
   1157 			    (df->flag << DBG_VBOSE_SHIFT) & DFlags)
   1158 				out1c(df->label - ('a' - 'A'));
   1159 			else if (df->flag & DFlags)
   1160 				out1c(df->label);
   1161 		}
   1162 		out1c('\n');
   1163 		return 0;
   1164 	}
   1165 
   1166 	while (*++argv) {
   1167 		if (**argv == '-')
   1168 			set_debug(*argv + 1, 1);
   1169 		else if (**argv == '+')
   1170 			set_debug(*argv + 1, 0);
   1171 		else
   1172 			return 1;
   1173 	}
   1174 	return 0;
   1175 }
   1176 
   1177 #endif /* DEBUG */
   1178