xref: /netbsd-src/bin/sh/show.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: show.c,v 1.47 2017/06/30 23:00:40 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.47 2017/06/30 23:00:40 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 void
306 trargstr(union node *n)
307 {
308 	sharg(n, tracetfile);
309 }
310 
311 
312 /*
313  * Beyond here we just have the implementation of all of that
314  */
315 
316 
317 inline static int
318 trlinelen(TFILE * fp)
319 {
320 	return fp->llen;
321 }
322 
323 static void
324 shtree(union node *n, int ind, int ilvl, int nl, TFILE *fp)
325 {
326 	struct nodelist *lp;
327 	const char *s;
328 
329 	if (n == NULL) {
330 		if (nl)
331 			trace_putc('\n', fp);
332 		return;
333 	}
334 
335 	indent(ind, fp);
336 	switch (n->type) {
337 	case NSEMI:
338 		s = NULL;
339 		goto binop;
340 	case NAND:
341 		s = " && ";
342 		goto binop;
343 	case NOR:
344 		s = " || ";
345 binop:
346 		shtree(n->nbinary.ch1, 0, ilvl, 0, fp);
347 		if (s != NULL)
348 			trace_puts(s, fp);
349 		if (trlinelen(fp) >= TR_STD_WIDTH) {
350 			trace_putc('\n', fp);
351 			indent(ind < 0 ? 2 : ind + 1, fp);
352 		} else if (s == NULL) {
353 			if (fp->lastc != '&')
354 				trace_puts("; ", fp);
355 			else
356 				trace_putc(' ', fp);
357 		}
358 		shtree(n->nbinary.ch2, 0, ilvl, nl, fp);
359 		break;
360 	case NCMD:
361 		shcmd(n, fp);
362 		if (n->ncmd.backgnd)
363 			trace_puts(" &", fp);
364 		if (nl && trlinelen(fp) > 0)
365 			trace_putc('\n', fp);
366 		break;
367 	case NPIPE:
368 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
369 			shtree(lp->n, 0, ilvl, 0, fp);
370 			if (lp->next) {
371 				trace_puts(" |", fp);
372 				if (trlinelen(fp) >= TR_STD_WIDTH)  {
373 					trace_putc('\n', fp);
374 					indent((ind < 0 ? ilvl : ind) + 1, fp);
375 				} else
376 					trace_putc(' ', fp);
377 			}
378 		}
379 		if (n->npipe.backgnd)
380 			trace_puts(" &", fp);
381 		if (nl || trlinelen(fp) >= TR_STD_WIDTH)
382 			trace_putc('\n', fp);
383 		break;
384 	case NBACKGND:
385 	case NSUBSHELL:
386 		shsubsh(n, fp);
387 		if (n->type == NBACKGND)
388 			trace_puts(" &", fp);
389 		if (nl && trlinelen(fp) > 0)
390 			trace_putc('\n', fp);
391 		break;
392 	case NDEFUN:
393 		trace_puts(n->narg.text, fp);
394 		trace_puts("() {\n", fp);
395 		indent(ind, fp);
396 		shtree(n->narg.next, (ind < 0 ? ilvl : ind) + 1, ilvl+1, 1, fp);
397 		indent(ind, fp);
398 		trace_puts("}\n", fp);
399 		break;
400 	case NDNOT:
401 		trace_puts("! ", fp);
402 		/* FALLTHROUGH */
403 	case NNOT:
404 		trace_puts("! ", fp);
405 		shtree(n->nnot.com, -1, ilvl, nl, fp);
406 		break;
407 	case NREDIR:
408 		shtree(n->nredir.n, -1, ilvl, 0, fp);
409 		shredir(n->nredir.redirect, fp, n->nredir.n == NULL);
410 		if (nl)
411 			trace_putc('\n', fp);
412 		break;
413 
414 	case NIF:
415 	itsif:
416 		trace_puts("if ", fp);
417 		shtree(n->nif.test, -1, ilvl, 0, fp);
418 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
419 			if (fp->lastc != '&')
420 				trace_puts(" ;", fp);
421 		} else
422 			indent(ilvl, fp);
423 		trace_puts(" then ", fp);
424 		if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
425 			indent(ilvl+1, fp);
426 		shtree(n->nif.ifpart, -1, ilvl + 1, 0, fp);
427 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
428 			if (fp->lastc != '&')
429 				trace_puts(" ;", fp);
430 		} else
431 			indent(ilvl, fp);
432 		if (n->nif.elsepart && n->nif.elsepart->type == NIF) {
433 			if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
434 				indent(ilvl, fp);
435 			n = n->nif.elsepart;
436 			trace_puts(" el", fp);
437 			goto itsif;
438 		}
439 		if (n->nif.elsepart) {
440 			if (nl || trlinelen(fp) > TR_STD_WIDTH - 24)
441 				indent(ilvl+1, fp);
442 			trace_puts(" else ", fp);
443 			shtree(n->nif.elsepart, -1, ilvl + 1, 0, fp);
444 			if (fp->lastc != '&')
445 				trace_puts(" ;", fp);
446 		}
447 		trace_puts(" fi", fp);
448 		if (nl)
449 			trace_putc('\n', fp);
450 		break;
451 
452 	case NWHILE:
453 		trace_puts("while ", fp);
454 		goto aloop;
455 	case NUNTIL:
456 		trace_puts("until ", fp);
457 	aloop:
458 		shtree(n->nbinary.ch1, -1, ilvl, 0, fp);
459 		if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
460 			if (fp->lastc != '&')
461 				trace_puts(" ;", fp);
462 		} else
463 			trace_putc('\n', fp);
464 		trace_puts(" do ", fp);
465 		shtree(n->nbinary.ch1, -1, ilvl + 1, 1, fp);
466 		trace_puts(" done ", fp);
467 		if (nl)
468 			trace_putc('\n', fp);
469 		break;
470 
471 	case NFOR:
472 		trace_puts("for ", fp);
473 		trace_puts(n->nfor.var, fp);
474 		if (n->nfor.args) {
475 			union node *argp;
476 
477 			trace_puts(" in ", fp);
478 			for (argp = n->nfor.args; argp; argp=argp->narg.next) {
479 				sharg(argp, fp);
480 				trace_putc(' ', fp);
481 			}
482 			if (trlinelen(fp) > 0 && trlinelen(fp) < TR_STD_WIDTH) {
483 				if (fp->lastc != '&')
484 					trace_putc(';', fp);
485 			} else
486 				trace_putc('\n', fp);
487 		}
488 		trace_puts(" do ", fp);
489 		shtree(n->nfor.body, -1, ilvl + 1, 0, fp);
490 		if (fp->lastc != '&')
491 			trace_putc(';', fp);
492 		trace_puts(" done", fp);
493 		if (nl)
494 			trace_putc('\n', fp);
495 		break;
496 
497 	case NCASE:
498 		trace_puts("case ", fp);
499 		sharg(n->ncase.expr, fp);
500 		trace_puts(" in", fp);
501 		if (nl)
502 			trace_putc('\n', fp);
503 		{
504 			union node *cp;
505 
506 			for (cp = n->ncase.cases ; cp ; cp = cp->nclist.next) {
507 				union node *patp;
508 
509 				if (nl || trlinelen(fp) > TR_STD_WIDTH - 16)
510 					indent(ilvl, fp);
511 				else
512 					trace_putc(' ', fp);
513 				trace_putc('(', fp);
514 				patp = cp->nclist.pattern;
515 				while (patp != NULL) {
516 				    trace_putc(' ', fp);
517 				    sharg(patp, fp);
518 				    trace_putc(' ', fp);
519 				    if ((patp = patp->narg.next) != NULL)
520 					trace_putc('|', fp);
521 				}
522 				trace_putc(')', fp);
523 				if (nl)
524 					indent(ilvl + 1, fp);
525 				else
526 					trace_putc(' ', fp);
527 				shtree(cp->nclist.body, -1, ilvl+2, 0, fp);
528 				if (cp->type == NCLISTCONT)
529 					trace_puts(" ;&", fp);
530 				else
531 					trace_puts(" ;;", fp);
532 				if (nl)
533 					trace_putc('\n', fp);
534 			}
535 		}
536 		if (nl) {
537 			trace_putc('\n', fp);
538 			indent(ind, fp);
539 		} else
540 			trace_putc(' ', fp);
541 		trace_puts("esac", fp);
542 		if (nl)
543 			trace_putc('\n', fp);
544 		break;
545 
546 	default: {
547 			char *str;
548 
549 			asprintf(&str, "<node type %d [%s]>", n->type,
550 			    NODETYPENAME(n->type));
551 			trace_puts(str, fp);
552 			free(str);
553 			if (nl)
554 				trace_putc('\n', fp);
555 		}
556 		break;
557 	}
558 }
559 
560 
561 static void
562 shcmd(union node *cmd, TFILE *fp)
563 {
564 	union node *np;
565 	int first;
566 
567 	first = 1;
568 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
569 		if (! first)
570 			trace_putc(' ', fp);
571 		sharg(np, fp);
572 		first = 0;
573 	}
574 	shredir(cmd->ncmd.redirect, fp, first);
575 }
576 
577 static void
578 shsubsh(union node *cmd, TFILE *fp)
579 {
580 	trace_puts(" ( ", fp);
581 	shtree(cmd->nredir.n, -1, 3, 0, fp);
582 	trace_puts(" ) ", fp);
583 	shredir(cmd->ncmd.redirect, fp, 1);
584 }
585 
586 static void
587 shredir(union node *np, TFILE *fp, int first)
588 {
589 	const char *s;
590 	int dftfd;
591 	char buf[106];
592 
593 	for ( ; np ; np = np->nfile.next) {
594 		if (! first)
595 			trace_putc(' ', fp);
596 		switch (np->nfile.type) {
597 			case NTO:	s = ">";  dftfd = 1; break;
598 			case NCLOBBER:	s = ">|"; dftfd = 1; break;
599 			case NAPPEND:	s = ">>"; dftfd = 1; break;
600 			case NTOFD:	s = ">&"; dftfd = 1; break;
601 			case NFROM:	s = "<";  dftfd = 0; break;
602 			case NFROMFD:	s = "<&"; dftfd = 0; break;
603 			case NFROMTO:	s = "<>"; dftfd = 0; break;
604 			case NXHERE:	/* FALLTHROUGH */
605 			case NHERE:	s = "<<"; dftfd = 0; break;
606 			default:   s = "*error*"; dftfd = 0; break;
607 		}
608 		if (np->nfile.fd != dftfd) {
609 			sprintf(buf, "%d", np->nfile.fd);
610 			trace_puts(buf, fp);
611 		}
612 		trace_puts(s, fp);
613 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
614 			if (np->ndup.vname)
615 				sharg(np->ndup.vname, fp);
616 			else {
617 				sprintf(buf, "%d", np->ndup.dupfd);
618 				trace_puts(buf, fp);
619 			}
620 		} else
621 		    if (np->nfile.type == NHERE || np->nfile.type == NXHERE) {
622 			if (np->nfile.type == NHERE)
623 				trace_putc('\\', fp);
624 			trace_puts("!!!\n", fp);
625 			s = np->nhere.doc->narg.text;
626 			if (strlen(s) > 100) {
627 				memmove(buf, s, 100);
628 				buf[100] = '\0';
629 				strcat(buf, " ...\n");
630 				s = buf;
631 			}
632 			trace_puts(s, fp);
633 			trace_puts("!!! ", fp);
634 		} else {
635 			sharg(np->nfile.fname, fp);
636 		}
637 		first = 0;
638 	}
639 }
640 
641 static void
642 sharg(union node *arg, TFILE *fp)
643 {
644 	char *p, *s;
645 	struct nodelist *bqlist;
646 	int subtype = 0;
647 	int quoted = 0;
648 
649 	if (arg->type != NARG) {
650 		asprintf(&s, "<node type %d> ! NARG\n", arg->type);
651 		trace_puts(s, fp);
652 		abort();	/* no need to free s, better not to */
653 	}
654 
655 	bqlist = arg->narg.backquote;
656 	for (p = arg->narg.text ; *p ; p++) {
657 		switch (*p) {
658 		case CTLESC:
659 			trace_putc('\\', fp);
660 			trace_putc(*++p, fp);
661 			break;
662 
663 		case CTLNONL:
664 			trace_putc('\\', fp);
665 			trace_putc('\n', fp);
666 			break;
667 
668 		case CTLVAR:
669 			subtype = *++p;
670 			if (!quoted != !(subtype & VSQUOTE))
671 				trace_putc('"', fp);
672 			trace_putc('$', fp);
673 			trace_putc('{', fp);	/*}*/
674 			if ((subtype & VSTYPE) == VSLENGTH)
675 				trace_putc('#', fp);
676 			if (subtype & VSLINENO)
677 				trace_puts("LINENO=", fp);
678 
679 			while (*++p != '=')
680 				trace_putc(*p, fp);
681 
682 			if (subtype & VSNUL)
683 				trace_putc(':', fp);
684 
685 			switch (subtype & VSTYPE) {
686 			case VSNORMAL:
687 				/* { */
688 				trace_putc('}', fp);
689 				if (!quoted != !(subtype & VSQUOTE))
690 					trace_putc('"', fp);
691 				break;
692 			case VSMINUS:
693 				trace_putc('-', fp);
694 				break;
695 			case VSPLUS:
696 				trace_putc('+', fp);
697 				break;
698 			case VSQUESTION:
699 				trace_putc('?', fp);
700 				break;
701 			case VSASSIGN:
702 				trace_putc('=', fp);
703 				break;
704 			case VSTRIMLEFTMAX:
705 				trace_putc('#', fp);
706 				/* FALLTHROUGH */
707 			case VSTRIMLEFT:
708 				trace_putc('#', fp);
709 				break;
710 			case VSTRIMRIGHTMAX:
711 				trace_putc('%', fp);
712 				/* FALLTHROUGH */
713 			case VSTRIMRIGHT:
714 				trace_putc('%', fp);
715 				break;
716 			case VSLENGTH:
717 				break;
718 			default: {
719 					char str[32];
720 
721 					snprintf(str, sizeof str,
722 					    "<subtype %d>", subtype);
723 					trace_puts(str, fp);
724 				}
725 				break;
726 			}
727 			break;
728 		case CTLENDVAR:
729 			/* { */
730 			trace_putc('}', fp);
731 			if (!quoted != !(subtype & VSQUOTE))
732 				trace_putc('"', fp);
733 			subtype = 0;
734 			break;
735 
736 		case CTLBACKQ|CTLQUOTE:
737 			if (!quoted)
738 				trace_putc('"', fp);
739 			/* FALLTHRU */
740 		case CTLBACKQ:
741 			trace_putc('$', fp);
742 			trace_putc('(', fp);
743 			if (bqlist) {
744 				shtree(bqlist->n, -1, 3, 0, fp);
745 				bqlist = bqlist->next;
746 			} else
747 				trace_puts("???", fp);
748 			trace_putc(')', fp);
749 			if (!quoted && *p == (CTLBACKQ|CTLQUOTE))
750 				trace_putc('"', fp);
751 			break;
752 
753 		case CTLQUOTEMARK:
754 			if (subtype != 0 || !quoted) {
755 				trace_putc('"', fp);
756 				quoted++;
757 			}
758 			break;
759 		case CTLQUOTEEND:
760 			trace_putc('"', fp);
761 			quoted--;
762 			break;
763 		case CTLARI:
764 			if (*p == ' ')
765 				p++;
766 			trace_puts("$(( ", fp);
767 			break;
768 		case CTLENDARI:
769 			trace_puts(" ))", fp);
770 			break;
771 
772 		default:
773 			if (*p == '$')
774 				trace_putc('\\', fp);
775 			trace_putc(*p, fp);
776 			break;
777 		}
778 	}
779 	if (quoted)
780 		trace_putc('"', fp);
781 }
782 
783 
784 static void
785 indent(int amount, TFILE *fp)
786 {
787 	int i;
788 
789 	if (amount <= 0)
790 		return;
791 
792 	amount <<= 2;	/* indent slots -> chars */
793 
794 	i = trlinelen(fp);
795 	fp->supr = SUP_NL;
796 	if (i > amount) {
797 		trace_putc('\n', fp);
798 		i = 0;
799 	}
800 	fp->supr = 0;
801 	for (; i < amount - 7 ; i++) {
802 		trace_putc('\t', fp);
803 		i |= 7;
804 	}
805 	while (i < amount) {
806 		trace_putc(' ', fp);
807 		i++;
808 	}
809 	fp->supr = SUP_WSP;
810 }
811 
812 static void
813 trace_putc(char c, TFILE *fp)
814 {
815 	char *p;
816 
817 	if (c == '\0')
818 		return;
819 	if (debug == 0 || fp == NULL)
820 		return;
821 
822 	if (fp->llen == 0) {
823 		if (fp->blen != 0)
824 			abort();
825 
826 		if ((fp->supr & SUP_NL) && c == '\n')
827 			return;
828 		if ((fp->supr & (SUP_WSP|SUP_SP)) && c == ' ')
829 			return;
830 		if ((fp->supr & SUP_WSP) && c == '\t')
831 			return;
832 
833 		if (fp->nxtiov >= TR_IOVECS - 1)	/* should be rare */
834 			trace_flush(fp, 0);
835 
836 		p = trace_id(fp);
837 		if (p != NULL) {
838 			fp->lines[fp->nxtiov].iov_base = p;
839 			fp->lines[fp->nxtiov].iov_len = strlen(p);
840 			fp->nxtiov++;
841 		}
842 	} else if (fp->blen && fp->blen >= fp->lines[fp->nxtiov].iov_len) {
843 		fp->blen = 0;
844 		if (++fp->nxtiov >= TR_IOVECS)
845 			trace_flush(fp, 0);
846 	}
847 
848 	if (fp->lines[fp->nxtiov].iov_len == 0) {
849 		p = (char *)malloc(2 * TR_STD_WIDTH);
850 		if (p == NULL) {
851 			trace_flush(fp, 1);
852 			debug = 0;
853 			return;
854 		}
855 		*p = '\0';
856 		fp->lines[fp->nxtiov].iov_base = p;
857 		fp->lines[fp->nxtiov].iov_len = 2 * TR_STD_WIDTH;
858 		fp->blen = 0;
859 	}
860 
861 	p = (char *)fp->lines[fp->nxtiov].iov_base + fp->blen++;
862 	*p++ = c;
863 	*p = 0;
864 
865 	if (c != ' ' && c != '\t' && c != '\n') {
866 		fp->lastc = c;
867 		fp->supr = 0;
868 	}
869 
870 	if (c == '\n') {
871 		fp->lines[fp->nxtiov++].iov_len = fp->blen;
872 		fp->blen = 0;
873 		fp->llen = 0;
874 		fp->supr |= SUP_NL;
875 		return;
876 	}
877 
878 	if (c == '\t')
879 		fp->llen |=  7;
880 	fp->llen++;
881 }
882 
883 void
884 trace_flush(TFILE *fp, int all)
885 {
886 	int niov, i;
887 	ssize_t written;
888 
889 	niov = fp->nxtiov;
890 	if (all && fp->blen > 0) {
891 		fp->lines[niov].iov_len = fp->blen;
892 		fp->blen = 0;
893 		fp->llen = 0;
894 		niov++;
895 	}
896 	if (niov == 0)
897 		return;
898 	if (fp->blen > 0 && --niov == 0)
899 		return;
900 	written = writev(fp->tfd, fp->lines, niov);
901 	for (i = 0; i < niov; i++) {
902 		free(fp->lines[i].iov_base);
903 		fp->lines[i].iov_base = NULL;
904 		fp->lines[i].iov_len = 0;
905 	}
906 	if (written == -1) {
907 		if (fp->blen > 0) {
908 			free(fp->lines[niov].iov_base);
909 			fp->lines[niov].iov_base = NULL;
910 			fp->lines[niov].iov_len = 0;
911 		}
912 		debug = 0;
913 		fp->blen = 0;
914 		fp->llen = 0;
915 		return;
916 	}
917 	if (fp->blen > 0) {
918 		fp->lines[0].iov_base = fp->lines[niov].iov_base;
919 		fp->lines[0].iov_len = fp->lines[niov].iov_len;
920 		fp->lines[niov].iov_base = NULL;
921 		fp->lines[niov].iov_len = 0;
922 	}
923 	fp->nxtiov = 0;
924 }
925 
926 void
927 trace_puts(const char *s, TFILE *fp)
928 {
929 	char c;
930 
931 	while ((c = *s++) != '\0')
932 		trace_putc(c, fp);
933 }
934 
935 inline static char *
936 trace_id(TFILE *tf)
937 {
938 	int i;
939 	char indent[16];
940 	char *p;
941 	int lno;
942 	char c;
943 
944 	if (DFlags & DBG_NEST) {
945 		if ((unsigned)ShNest >= sizeof indent - 1) {
946 			(void) snprintf(indent, sizeof indent,
947 			    "### %*d ###", (int)(sizeof indent) - 9, ShNest);
948 			p = strchr(indent, '\0');
949 		} else {
950 			p = indent;
951 			for (i = 0; i < 6; i++)
952 				*p++ = (i < ShNest) ? '#' : ' ';
953 			while (i++ < ShNest && p < &indent[sizeof indent - 1])
954 				*p++ = '#';
955 			*p = '\0';
956 		}
957 	} else
958 		indent[0] = '\0';
959 
960 	/*
961 	 * If we are in the parser, then plinno is the current line
962 	 * number being processed (parser line no).
963 	 * If we are elsewhere, then line_number gives the source
964 	 * line of whatever we are currently doing (close enough.)
965 	 */
966 	if (parsing)
967 		lno = plinno;
968 	else
969 		lno = line_number;
970 
971 	c = ((i = getpid()) == tf->pid) ? ':' : '=';
972 
973 	if (DFlags & DBG_PID) {
974 		if (DFlags & DBG_LINE)
975 			(void) asprintf(&p, "%5d%c%s\t%4d%c@\t", i, c,
976 			    indent, lno, parsing?'-':'+');
977 		else
978 			(void) asprintf(&p, "%5d%c%s\t", i, c, indent);
979 		return p;
980 	} else if (DFlags & DBG_NEST) {
981 		if (DFlags & DBG_LINE)
982 			(void) asprintf(&p, "%c%s\t%4d%c@\t", c, indent, lno,
983 			    parsing?'-':'+');
984 		else
985 			(void) asprintf(&p, "%c%s\t", c, indent);
986 		return p;
987 	} else if (DFlags & DBG_LINE) {
988 		(void) asprintf(&p, "%c%4d%c@\t", c, lno, parsing?'-':'+');
989 		return p;
990 	}
991 	return NULL;
992 }
993 
994 /*
995  * Used only from trargs(), which itself is used only to print
996  * arg lists (argv[]) either that passed into this shell, or
997  * the arg list about to be given to some other command (incl
998  * builtin, and function) as their argv[].  If any of the CTL*
999  * chars seem to appear, they really should be just treated as data,
1000  * not special...   But this is just debug, so, who cares!
1001  */
1002 static void
1003 trstring(const char *s)
1004 {
1005 	const char *p;
1006 	char c;
1007 	TFILE *fp;
1008 
1009 	if (debug != 1 || !tracetfile)
1010 		return;
1011 	fp = tracetfile;
1012 	trace_putc('"', fp);
1013 	for (p = s ; *p ; p++) {
1014 		switch (*p) {
1015 		case '\n':  c = 'n';  goto backslash;
1016 		case '\t':  c = 't';  goto backslash;
1017 		case '\r':  c = 'r';  goto backslash;
1018 		case '"':  c = '"';  goto backslash;
1019 		case '\\':  c = '\\';  goto backslash;
1020 		case CTLESC:  c = 'e';  goto backslash;
1021 		case CTLVAR:  c = 'v';  goto backslash;
1022 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
1023 		case CTLBACKQ:  c = 'q';  goto backslash;
1024 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
1025 backslash:		trace_putc('\\', fp);
1026 			trace_putc(c, fp);
1027 			break;
1028 		default:
1029 			if (*p >= ' ' && *p <= '~')
1030 				trace_putc(*p, fp);
1031 			else {
1032 				trace_putc('\\', fp);
1033 				trace_putc(*p >> 6 & 03, fp);
1034 				trace_putc(*p >> 3 & 07, fp);
1035 				trace_putc(*p & 07, fp);
1036 			}
1037 			break;
1038 		}
1039 	}
1040 	trace_putc('"', fp);
1041 }
1042 
1043 /*
1044  * deal with the user "accidentally" picking our fd to use.
1045  */
1046 static void
1047 trace_fd_swap(int from, int to)
1048 {
1049 	if (tracetfile == NULL || from == to)
1050 		return;
1051 
1052 	tracetfile->tfd = to;
1053 
1054 	/*
1055 	 * This is just so histedit has a stdio FILE* to use.
1056 	 */
1057 	if (tracefile)
1058 		fclose(tracefile);
1059 	tracefile = fdopen(to, "a");
1060 	if (tracefile)
1061 		setlinebuf(tracefile);
1062 }
1063 
1064 
1065 static struct debug_flag {
1066 	char		label;
1067 	uint64_t	flag;
1068 } debug_flags[] = {
1069 	{ 'a',	DBG_ARITH	},	/* arithmetic ( $(( )) ) */
1070 	{ 'c',	DBG_CMDS	},	/* command searching, ... */
1071 	{ 'e',	DBG_EVAL	},	/* evaluation of the parse tree */
1072 	{ 'f',	DBG_REDIR	},	/* file descriptors & redirections */
1073 	{ 'h',	DBG_HISTORY	},	/* history & cmd line editing */
1074 	{ 'i',	DBG_INPUT	},	/* shell input routines */
1075 	{ 'j',	DBG_JOBS	},	/* job control, structures */
1076 	{ 'm',	DBG_MEM		},	/* memory management */
1077 	{ 'o',	DBG_OUTPUT	},	/* output routines */
1078 	{ 'p',	DBG_PROCS	},	/* process management, fork, ... */
1079 	{ 'r',	DBG_PARSE	},	/* parser, lexer, ... tree building */
1080 	{ 's',	DBG_SIG		},	/* signals and everything related */
1081 	{ 't',	DBG_TRAP	},	/* traps & signals */
1082 	{ 'v',	DBG_VARS	},	/* variables and parameters */
1083 	{ 'w',	DBG_WAIT	},	/* waits for processes to finish */
1084 	{ 'x',	DBG_EXPAND	},	/* word expansion ${} $() $(( )) */
1085 	{ 'z',	DBG_ERRS	},	/* error control, jumps, cleanup */
1086 
1087 	{ '0',	DBG_U0		},	/* ad-hoc temp debug flag #0 */
1088 	{ '1',	DBG_U1		},	/* ad-hoc temp debug flag #1 */
1089 	{ '2',	DBG_U2		},	/* ad-hoc temp debug flag #2 */
1090 
1091 	{ '@',	DBG_LINE	},	/* prefix trace lines with line# */
1092 	{ '$',	DBG_PID		},	/* prefix trace lines with sh pid */
1093 	{ '^',	DBG_NEST	},	/* show shell nesting level */
1094 
1095 			/* alpha options only */
1096 	{ '_',	DBG_PARSE | DBG_EVAL | DBG_EXPAND | DBG_JOBS |
1097 		    DBG_PROCS | DBG_REDIR | DBG_CMDS | DBG_ERRS |
1098 		    DBG_WAIT | DBG_TRAP | DBG_VARS | DBG_MEM |
1099 		    DBG_INPUT | DBG_OUTPUT | DBG_ARITH | DBG_HISTORY },
1100 
1101    /*   { '*',	DBG_ALLVERBOSE	}, 	   is handled in the code */
1102 
1103 	{ '#',	DBG_U0 | DBG_U1 | DBG_U2 },
1104 
1105 	{ 0,	0		}
1106 };
1107 
1108 void
1109 set_debug(const char * flags, int on)
1110 {
1111 	char f;
1112 	struct debug_flag *df;
1113 	int verbose;
1114 
1115 	while ((f = *flags++) != '\0') {
1116 		verbose = 0;
1117 		if (is_upper(f)) {
1118 			verbose = 1;
1119 			f += 'a' - 'A';
1120 		}
1121 		if (f == '*')
1122 			f = '_', verbose = 1;
1123 		if (f == '+') {
1124 			if (*flags == '+')
1125 				flags++, verbose=1;
1126 		}
1127 
1128 		/*
1129 		 * Note: turning on any debug option also enables DBG_ALWAYS
1130 		 * turning on any verbose option also enables DBG_VERBOSE
1131 		 * Once enabled, those flags cannot be disabled.
1132 		 * (tracing can still be turned off with "set +o debug")
1133 		 */
1134 		for (df = debug_flags; df->label != '\0'; df++) {
1135 			if (f == '+' || df->label == f) {
1136 				if (on) {
1137 					DFlags |= DBG_ALWAYS | df->flag;
1138 					if (verbose)
1139 					    DFlags |= DBG_VERBOSE |
1140 						(df->flag << DBG_VBOSE_SHIFT);
1141 				} else {
1142 					DFlags &= ~(df->flag<<DBG_VBOSE_SHIFT);
1143 					if (!verbose)
1144 						DFlags &= ~df->flag;
1145 				}
1146 			}
1147 		}
1148 	}
1149 }
1150 
1151 
1152 int
1153 debugcmd(int argc, char **argv)
1154 {
1155 	if (argc == 1) {
1156 		struct debug_flag *df;
1157 
1158 		out1fmt("Debug: %sabled.  Flags: ", debug ? "en" : "dis");
1159 		for (df = debug_flags; df->label != '\0'; df++) {
1160 			if (df->flag & (df->flag - 1))
1161 				continue;
1162 			if (is_alpha(df->label) &&
1163 			    (df->flag << DBG_VBOSE_SHIFT) & DFlags)
1164 				out1c(df->label - ('a' - 'A'));
1165 			else if (df->flag & DFlags)
1166 				out1c(df->label);
1167 		}
1168 		out1c('\n');
1169 		return 0;
1170 	}
1171 
1172 	while (*++argv) {
1173 		if (**argv == '-')
1174 			set_debug(*argv + 1, 1);
1175 		else if (**argv == '+')
1176 			set_debug(*argv + 1, 0);
1177 		else
1178 			return 1;
1179 	}
1180 	return 0;
1181 }
1182 
1183 #endif /* DEBUG */
1184