xref: /freebsd/bin/sh/show.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: show.c,v 1.2 1994/09/24 02:58:16 davidg Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
434b88c807SRodney W. Grimes #include <stdio.h>
44aa9caaf6SPeter Wemm #if __STDC__
45aa9caaf6SPeter Wemm #include <stdarg.h>
46aa9caaf6SPeter Wemm #else
47aa9caaf6SPeter Wemm #include <varargs.h>
48aa9caaf6SPeter Wemm #endif
49aa9caaf6SPeter Wemm 
504b88c807SRodney W. Grimes #include "shell.h"
514b88c807SRodney W. Grimes #include "parser.h"
524b88c807SRodney W. Grimes #include "nodes.h"
534b88c807SRodney W. Grimes #include "mystring.h"
54aa9caaf6SPeter Wemm #include "show.h"
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes 
574b88c807SRodney W. Grimes #ifdef DEBUG
58aa9caaf6SPeter Wemm static void shtree __P((union node *, int, char *, FILE*));
59aa9caaf6SPeter Wemm static void shcmd __P((union node *, FILE *));
60aa9caaf6SPeter Wemm static void sharg __P((union node *, FILE *));
61aa9caaf6SPeter Wemm static void indent __P((int, char *, FILE *));
62aa9caaf6SPeter Wemm static void trstring __P((char *));
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes 
65aa9caaf6SPeter Wemm void
664b88c807SRodney W. Grimes showtree(n)
674b88c807SRodney W. Grimes 	union node *n;
684b88c807SRodney W. Grimes {
694b88c807SRodney W. Grimes 	trputs("showtree called\n");
704b88c807SRodney W. Grimes 	shtree(n, 1, NULL, stdout);
714b88c807SRodney W. Grimes }
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes 
74aa9caaf6SPeter Wemm static void
754b88c807SRodney W. Grimes shtree(n, ind, pfx, fp)
764b88c807SRodney W. Grimes 	union node *n;
77aa9caaf6SPeter Wemm 	int ind;
784b88c807SRodney W. Grimes 	char *pfx;
794b88c807SRodney W. Grimes 	FILE *fp;
804b88c807SRodney W. Grimes {
814b88c807SRodney W. Grimes 	struct nodelist *lp;
824b88c807SRodney W. Grimes 	char *s;
834b88c807SRodney W. Grimes 
84aa9caaf6SPeter Wemm 	if (n == NULL)
85aa9caaf6SPeter Wemm 		return;
86aa9caaf6SPeter Wemm 
874b88c807SRodney W. Grimes 	indent(ind, pfx, fp);
884b88c807SRodney W. Grimes 	switch(n->type) {
894b88c807SRodney W. Grimes 	case NSEMI:
904b88c807SRodney W. Grimes 		s = "; ";
914b88c807SRodney W. Grimes 		goto binop;
924b88c807SRodney W. Grimes 	case NAND:
934b88c807SRodney W. Grimes 		s = " && ";
944b88c807SRodney W. Grimes 		goto binop;
954b88c807SRodney W. Grimes 	case NOR:
964b88c807SRodney W. Grimes 		s = " || ";
974b88c807SRodney W. Grimes binop:
984b88c807SRodney W. Grimes 		shtree(n->nbinary.ch1, ind, NULL, fp);
994b88c807SRodney W. Grimes 	   /*    if (ind < 0) */
1004b88c807SRodney W. Grimes 			fputs(s, fp);
1014b88c807SRodney W. Grimes 		shtree(n->nbinary.ch2, ind, NULL, fp);
1024b88c807SRodney W. Grimes 		break;
1034b88c807SRodney W. Grimes 	case NCMD:
1044b88c807SRodney W. Grimes 		shcmd(n, fp);
1054b88c807SRodney W. Grimes 		if (ind >= 0)
1064b88c807SRodney W. Grimes 			putc('\n', fp);
1074b88c807SRodney W. Grimes 		break;
1084b88c807SRodney W. Grimes 	case NPIPE:
1094b88c807SRodney W. Grimes 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1104b88c807SRodney W. Grimes 			shcmd(lp->n, fp);
1114b88c807SRodney W. Grimes 			if (lp->next)
1124b88c807SRodney W. Grimes 				fputs(" | ", fp);
1134b88c807SRodney W. Grimes 		}
1144b88c807SRodney W. Grimes 		if (n->npipe.backgnd)
1154b88c807SRodney W. Grimes 			fputs(" &", fp);
1164b88c807SRodney W. Grimes 		if (ind >= 0)
1174b88c807SRodney W. Grimes 			putc('\n', fp);
1184b88c807SRodney W. Grimes 		break;
1194b88c807SRodney W. Grimes 	default:
1204b88c807SRodney W. Grimes 		fprintf(fp, "<node type %d>", n->type);
1214b88c807SRodney W. Grimes 		if (ind >= 0)
1224b88c807SRodney W. Grimes 			putc('\n', fp);
1234b88c807SRodney W. Grimes 		break;
1244b88c807SRodney W. Grimes 	}
1254b88c807SRodney W. Grimes }
1264b88c807SRodney W. Grimes 
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 
129aa9caaf6SPeter Wemm static void
1304b88c807SRodney W. Grimes shcmd(cmd, fp)
1314b88c807SRodney W. Grimes 	union node *cmd;
1324b88c807SRodney W. Grimes 	FILE *fp;
1334b88c807SRodney W. Grimes {
1344b88c807SRodney W. Grimes 	union node *np;
1354b88c807SRodney W. Grimes 	int first;
1364b88c807SRodney W. Grimes 	char *s;
1374b88c807SRodney W. Grimes 	int dftfd;
1384b88c807SRodney W. Grimes 
1394b88c807SRodney W. Grimes 	first = 1;
1404b88c807SRodney W. Grimes 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
1414b88c807SRodney W. Grimes 		if (! first)
1424b88c807SRodney W. Grimes 			putchar(' ');
1434b88c807SRodney W. Grimes 		sharg(np, fp);
1444b88c807SRodney W. Grimes 		first = 0;
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
1474b88c807SRodney W. Grimes 		if (! first)
1484b88c807SRodney W. Grimes 			putchar(' ');
1494b88c807SRodney W. Grimes 		switch (np->nfile.type) {
1504b88c807SRodney W. Grimes 			case NTO:	s = ">";  dftfd = 1; break;
1514b88c807SRodney W. Grimes 			case NAPPEND:	s = ">>"; dftfd = 1; break;
1524b88c807SRodney W. Grimes 			case NTOFD:	s = ">&"; dftfd = 1; break;
1534b88c807SRodney W. Grimes 			case NFROM:	s = "<";  dftfd = 0; break;
1544b88c807SRodney W. Grimes 			case NFROMFD:	s = "<&"; dftfd = 0; break;
155aa9caaf6SPeter Wemm 			default:  	s = "*error*"; dftfd = 0; break;
1564b88c807SRodney W. Grimes 		}
1574b88c807SRodney W. Grimes 		if (np->nfile.fd != dftfd)
1584b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->nfile.fd);
1594b88c807SRodney W. Grimes 		fputs(s, fp);
1604b88c807SRodney W. Grimes 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1614b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->ndup.dupfd);
1624b88c807SRodney W. Grimes 		} else {
1634b88c807SRodney W. Grimes 			sharg(np->nfile.fname, fp);
1644b88c807SRodney W. Grimes 		}
1654b88c807SRodney W. Grimes 		first = 0;
1664b88c807SRodney W. Grimes 	}
1674b88c807SRodney W. Grimes }
1684b88c807SRodney W. Grimes 
1694b88c807SRodney W. Grimes 
1704b88c807SRodney W. Grimes 
171aa9caaf6SPeter Wemm static void
1724b88c807SRodney W. Grimes sharg(arg, fp)
1734b88c807SRodney W. Grimes 	union node *arg;
1744b88c807SRodney W. Grimes 	FILE *fp;
1754b88c807SRodney W. Grimes 	{
1764b88c807SRodney W. Grimes 	char *p;
1774b88c807SRodney W. Grimes 	struct nodelist *bqlist;
1784b88c807SRodney W. Grimes 	int subtype;
1794b88c807SRodney W. Grimes 
1804b88c807SRodney W. Grimes 	if (arg->type != NARG) {
1814b88c807SRodney W. Grimes 		printf("<node type %d>\n", arg->type);
1824b88c807SRodney W. Grimes 		fflush(stdout);
1834b88c807SRodney W. Grimes 		abort();
1844b88c807SRodney W. Grimes 	}
1854b88c807SRodney W. Grimes 	bqlist = arg->narg.backquote;
1864b88c807SRodney W. Grimes 	for (p = arg->narg.text ; *p ; p++) {
1874b88c807SRodney W. Grimes 		switch (*p) {
1884b88c807SRodney W. Grimes 		case CTLESC:
1894b88c807SRodney W. Grimes 			putc(*++p, fp);
1904b88c807SRodney W. Grimes 			break;
1914b88c807SRodney W. Grimes 		case CTLVAR:
1924b88c807SRodney W. Grimes 			putc('$', fp);
1934b88c807SRodney W. Grimes 			putc('{', fp);
1944b88c807SRodney W. Grimes 			subtype = *++p;
195aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH)
196aa9caaf6SPeter Wemm 				putc('#', fp);
197aa9caaf6SPeter Wemm 
1984b88c807SRodney W. Grimes 			while (*p != '=')
1994b88c807SRodney W. Grimes 				putc(*p++, fp);
200aa9caaf6SPeter Wemm 
2014b88c807SRodney W. Grimes 			if (subtype & VSNUL)
2024b88c807SRodney W. Grimes 				putc(':', fp);
203aa9caaf6SPeter Wemm 
2044b88c807SRodney W. Grimes 			switch (subtype & VSTYPE) {
2054b88c807SRodney W. Grimes 			case VSNORMAL:
2064b88c807SRodney W. Grimes 				putc('}', fp);
2074b88c807SRodney W. Grimes 				break;
2084b88c807SRodney W. Grimes 			case VSMINUS:
2094b88c807SRodney W. Grimes 				putc('-', fp);
2104b88c807SRodney W. Grimes 				break;
2114b88c807SRodney W. Grimes 			case VSPLUS:
2124b88c807SRodney W. Grimes 				putc('+', fp);
2134b88c807SRodney W. Grimes 				break;
2144b88c807SRodney W. Grimes 			case VSQUESTION:
2154b88c807SRodney W. Grimes 				putc('?', fp);
2164b88c807SRodney W. Grimes 				break;
2174b88c807SRodney W. Grimes 			case VSASSIGN:
2184b88c807SRodney W. Grimes 				putc('=', fp);
2194b88c807SRodney W. Grimes 				break;
220aa9caaf6SPeter Wemm 			case VSTRIMLEFT:
221aa9caaf6SPeter Wemm 				putc('#', fp);
222aa9caaf6SPeter Wemm 				break;
223aa9caaf6SPeter Wemm 			case VSTRIMLEFTMAX:
224aa9caaf6SPeter Wemm 				putc('#', fp);
225aa9caaf6SPeter Wemm 				putc('#', fp);
226aa9caaf6SPeter Wemm 				break;
227aa9caaf6SPeter Wemm 			case VSTRIMRIGHT:
228aa9caaf6SPeter Wemm 				putc('%', fp);
229aa9caaf6SPeter Wemm 				break;
230aa9caaf6SPeter Wemm 			case VSTRIMRIGHTMAX:
231aa9caaf6SPeter Wemm 				putc('%', fp);
232aa9caaf6SPeter Wemm 				putc('%', fp);
233aa9caaf6SPeter Wemm 				break;
234aa9caaf6SPeter Wemm 			case VSLENGTH:
235aa9caaf6SPeter Wemm 				break;
2364b88c807SRodney W. Grimes 			default:
2374b88c807SRodney W. Grimes 				printf("<subtype %d>", subtype);
2384b88c807SRodney W. Grimes 			}
2394b88c807SRodney W. Grimes 			break;
2404b88c807SRodney W. Grimes 		case CTLENDVAR:
2414b88c807SRodney W. Grimes 		     putc('}', fp);
2424b88c807SRodney W. Grimes 		     break;
2434b88c807SRodney W. Grimes 		case CTLBACKQ:
2444b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
2454b88c807SRodney W. Grimes 			putc('$', fp);
2464b88c807SRodney W. Grimes 			putc('(', fp);
2474b88c807SRodney W. Grimes 			shtree(bqlist->n, -1, NULL, fp);
2484b88c807SRodney W. Grimes 			putc(')', fp);
2494b88c807SRodney W. Grimes 			break;
2504b88c807SRodney W. Grimes 		default:
2514b88c807SRodney W. Grimes 			putc(*p, fp);
2524b88c807SRodney W. Grimes 			break;
2534b88c807SRodney W. Grimes 		}
2544b88c807SRodney W. Grimes 	}
2554b88c807SRodney W. Grimes }
2564b88c807SRodney W. Grimes 
2574b88c807SRodney W. Grimes 
258aa9caaf6SPeter Wemm static void
2594b88c807SRodney W. Grimes indent(amount, pfx, fp)
260aa9caaf6SPeter Wemm 	int amount;
2614b88c807SRodney W. Grimes 	char *pfx;
2624b88c807SRodney W. Grimes 	FILE *fp;
2634b88c807SRodney W. Grimes {
2644b88c807SRodney W. Grimes 	int i;
2654b88c807SRodney W. Grimes 
2664b88c807SRodney W. Grimes 	for (i = 0 ; i < amount ; i++) {
2674b88c807SRodney W. Grimes 		if (pfx && i == amount - 1)
2684b88c807SRodney W. Grimes 			fputs(pfx, fp);
2694b88c807SRodney W. Grimes 		putc('\t', fp);
2704b88c807SRodney W. Grimes 	}
2714b88c807SRodney W. Grimes }
2724b88c807SRodney W. Grimes #endif
2734b88c807SRodney W. Grimes 
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes /*
2774b88c807SRodney W. Grimes  * Debugging stuff.
2784b88c807SRodney W. Grimes  */
2794b88c807SRodney W. Grimes 
2804b88c807SRodney W. Grimes 
2814b88c807SRodney W. Grimes FILE *tracefile;
2824b88c807SRodney W. Grimes 
2834b88c807SRodney W. Grimes #if DEBUG == 2
2844b88c807SRodney W. Grimes int debug = 1;
2854b88c807SRodney W. Grimes #else
2864b88c807SRodney W. Grimes int debug = 0;
2874b88c807SRodney W. Grimes #endif
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes 
290aa9caaf6SPeter Wemm void
291aa9caaf6SPeter Wemm trputc(c)
292aa9caaf6SPeter Wemm 	int c;
293aa9caaf6SPeter Wemm {
2944b88c807SRodney W. Grimes #ifdef DEBUG
2954b88c807SRodney W. Grimes 	if (tracefile == NULL)
2964b88c807SRodney W. Grimes 		return;
2974b88c807SRodney W. Grimes 	putc(c, tracefile);
2984b88c807SRodney W. Grimes 	if (c == '\n')
2994b88c807SRodney W. Grimes 		fflush(tracefile);
3004b88c807SRodney W. Grimes #endif
3014b88c807SRodney W. Grimes }
3024b88c807SRodney W. Grimes 
303aa9caaf6SPeter Wemm void
304aa9caaf6SPeter Wemm #if __STDC__
305aa9caaf6SPeter Wemm shtrace(const char *fmt, ...)
306aa9caaf6SPeter Wemm #else
307aa9caaf6SPeter Wemm shtrace(va_alist)
308aa9caaf6SPeter Wemm 	va_dcl
309aa9caaf6SPeter Wemm #endif
3104b88c807SRodney W. Grimes {
3114b88c807SRodney W. Grimes #ifdef DEBUG
312aa9caaf6SPeter Wemm 	va_list va;
313aa9caaf6SPeter Wemm #if __STDC__
314aa9caaf6SPeter Wemm 	va_start(va, fmt);
315aa9caaf6SPeter Wemm #else
316aa9caaf6SPeter Wemm 	char *fmt;
317aa9caaf6SPeter Wemm 	va_start(va);
318aa9caaf6SPeter Wemm 	fmt = va_arg(va, char *);
319aa9caaf6SPeter Wemm #endif
320aa9caaf6SPeter Wemm 	if (tracefile != NULL) {
321aa9caaf6SPeter Wemm 		(void) vfprintf(tracefile, fmt, va);
3224b88c807SRodney W. Grimes 		if (strchr(fmt, '\n'))
323aa9caaf6SPeter Wemm 			(void) fflush(tracefile);
324aa9caaf6SPeter Wemm 	}
325aa9caaf6SPeter Wemm 	va_end(va);
3264b88c807SRodney W. Grimes #endif
3274b88c807SRodney W. Grimes }
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes 
330aa9caaf6SPeter Wemm void
3314b88c807SRodney W. Grimes trputs(s)
3324b88c807SRodney W. Grimes 	char *s;
3334b88c807SRodney W. Grimes {
3344b88c807SRodney W. Grimes #ifdef DEBUG
3354b88c807SRodney W. Grimes 	if (tracefile == NULL)
3364b88c807SRodney W. Grimes 		return;
3374b88c807SRodney W. Grimes 	fputs(s, tracefile);
3384b88c807SRodney W. Grimes 	if (strchr(s, '\n'))
3394b88c807SRodney W. Grimes 		fflush(tracefile);
3404b88c807SRodney W. Grimes #endif
3414b88c807SRodney W. Grimes }
3424b88c807SRodney W. Grimes 
3434b88c807SRodney W. Grimes 
344aa9caaf6SPeter Wemm static void
3454b88c807SRodney W. Grimes trstring(s)
3464b88c807SRodney W. Grimes 	char *s;
3474b88c807SRodney W. Grimes {
3484b88c807SRodney W. Grimes 	register char *p;
3494b88c807SRodney W. Grimes 	char c;
3504b88c807SRodney W. Grimes 
3514b88c807SRodney W. Grimes #ifdef DEBUG
3524b88c807SRodney W. Grimes 	if (tracefile == NULL)
3534b88c807SRodney W. Grimes 		return;
3544b88c807SRodney W. Grimes 	putc('"', tracefile);
3554b88c807SRodney W. Grimes 	for (p = s ; *p ; p++) {
3564b88c807SRodney W. Grimes 		switch (*p) {
3574b88c807SRodney W. Grimes 		case '\n':  c = 'n';  goto backslash;
3584b88c807SRodney W. Grimes 		case '\t':  c = 't';  goto backslash;
3594b88c807SRodney W. Grimes 		case '\r':  c = 'r';  goto backslash;
3604b88c807SRodney W. Grimes 		case '"':  c = '"';  goto backslash;
3614b88c807SRodney W. Grimes 		case '\\':  c = '\\';  goto backslash;
3624b88c807SRodney W. Grimes 		case CTLESC:  c = 'e';  goto backslash;
3634b88c807SRodney W. Grimes 		case CTLVAR:  c = 'v';  goto backslash;
3644b88c807SRodney W. Grimes 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
3654b88c807SRodney W. Grimes 		case CTLBACKQ:  c = 'q';  goto backslash;
3664b88c807SRodney W. Grimes 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
3674b88c807SRodney W. Grimes backslash:	  putc('\\', tracefile);
3684b88c807SRodney W. Grimes 			putc(c, tracefile);
3694b88c807SRodney W. Grimes 			break;
3704b88c807SRodney W. Grimes 		default:
3714b88c807SRodney W. Grimes 			if (*p >= ' ' && *p <= '~')
3724b88c807SRodney W. Grimes 				putc(*p, tracefile);
3734b88c807SRodney W. Grimes 			else {
3744b88c807SRodney W. Grimes 				putc('\\', tracefile);
3754b88c807SRodney W. Grimes 				putc(*p >> 6 & 03, tracefile);
3764b88c807SRodney W. Grimes 				putc(*p >> 3 & 07, tracefile);
3774b88c807SRodney W. Grimes 				putc(*p & 07, tracefile);
3784b88c807SRodney W. Grimes 			}
3794b88c807SRodney W. Grimes 			break;
3804b88c807SRodney W. Grimes 		}
3814b88c807SRodney W. Grimes 	}
3824b88c807SRodney W. Grimes 	putc('"', tracefile);
3834b88c807SRodney W. Grimes #endif
3844b88c807SRodney W. Grimes }
3854b88c807SRodney W. Grimes 
3864b88c807SRodney W. Grimes 
387aa9caaf6SPeter Wemm void
3884b88c807SRodney W. Grimes trargs(ap)
3894b88c807SRodney W. Grimes 	char **ap;
3904b88c807SRodney W. Grimes {
3914b88c807SRodney W. Grimes #ifdef DEBUG
3924b88c807SRodney W. Grimes 	if (tracefile == NULL)
3934b88c807SRodney W. Grimes 		return;
3944b88c807SRodney W. Grimes 	while (*ap) {
3954b88c807SRodney W. Grimes 		trstring(*ap++);
3964b88c807SRodney W. Grimes 		if (*ap)
3974b88c807SRodney W. Grimes 			putc(' ', tracefile);
3984b88c807SRodney W. Grimes 		else
3994b88c807SRodney W. Grimes 			putc('\n', tracefile);
4004b88c807SRodney W. Grimes 	}
4014b88c807SRodney W. Grimes 	fflush(tracefile);
4024b88c807SRodney W. Grimes #endif
4034b88c807SRodney W. Grimes }
4044b88c807SRodney W. Grimes 
4054b88c807SRodney W. Grimes 
406aa9caaf6SPeter Wemm void
4074b88c807SRodney W. Grimes opentrace() {
4084b88c807SRodney W. Grimes 	char s[100];
4094b88c807SRodney W. Grimes 	char *getenv();
410aa9caaf6SPeter Wemm #ifdef O_APPEND
4114b88c807SRodney W. Grimes 	int flags;
412aa9caaf6SPeter Wemm #endif
4134b88c807SRodney W. Grimes 
4144b88c807SRodney W. Grimes #ifdef DEBUG
4154b88c807SRodney W. Grimes 	if (!debug)
4164b88c807SRodney W. Grimes 		return;
4174b88c807SRodney W. Grimes #ifdef not_this_way
418aa9caaf6SPeter Wemm 	{
419aa9caaf6SPeter Wemm 		char *p;
4204b88c807SRodney W. Grimes 		if ((p = getenv("HOME")) == NULL) {
4214b88c807SRodney W. Grimes 			if (geteuid() == 0)
4224b88c807SRodney W. Grimes 				p = "/";
4234b88c807SRodney W. Grimes 			else
4244b88c807SRodney W. Grimes 				p = "/tmp";
4254b88c807SRodney W. Grimes 		}
4264b88c807SRodney W. Grimes 		scopy(p, s);
4274b88c807SRodney W. Grimes 		strcat(s, "/trace");
428aa9caaf6SPeter Wemm 	}
4294b88c807SRodney W. Grimes #else
4304b88c807SRodney W. Grimes 	scopy("./trace", s);
4314b88c807SRodney W. Grimes #endif /* not_this_way */
4324b88c807SRodney W. Grimes 	if ((tracefile = fopen(s, "a")) == NULL) {
4334b88c807SRodney W. Grimes 		fprintf(stderr, "Can't open %s\n", s);
4344b88c807SRodney W. Grimes 		return;
4354b88c807SRodney W. Grimes 	}
4364b88c807SRodney W. Grimes #ifdef O_APPEND
4374b88c807SRodney W. Grimes 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
4384b88c807SRodney W. Grimes 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
4394b88c807SRodney W. Grimes #endif
4404b88c807SRodney W. Grimes 	fputs("\nTracing started.\n", tracefile);
4414b88c807SRodney W. Grimes 	fflush(tracefile);
4424b88c807SRodney W. Grimes #endif /* DEBUG */
4434b88c807SRodney W. Grimes }
444