xref: /freebsd/bin/sh/show.c (revision 6c48b6cf75960ede12a39f91a6c87ad709c6c47e)
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.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #include <stdio.h>
46afb033d5SSteve Price #ifdef __STDC__
47aa9caaf6SPeter Wemm #include <stdarg.h>
48aa9caaf6SPeter Wemm #else
49aa9caaf6SPeter Wemm #include <varargs.h>
50aa9caaf6SPeter Wemm #endif
51aa9caaf6SPeter Wemm 
524b88c807SRodney W. Grimes #include "shell.h"
534b88c807SRodney W. Grimes #include "parser.h"
544b88c807SRodney W. Grimes #include "nodes.h"
554b88c807SRodney W. Grimes #include "mystring.h"
56aa9caaf6SPeter Wemm #include "show.h"
574b88c807SRodney W. Grimes 
584b88c807SRodney W. Grimes 
594b88c807SRodney W. Grimes #ifdef DEBUG
60aa9caaf6SPeter Wemm static void shtree __P((union node *, int, char *, FILE*));
61aa9caaf6SPeter Wemm static void shcmd __P((union node *, FILE *));
62aa9caaf6SPeter Wemm static void sharg __P((union node *, FILE *));
63aa9caaf6SPeter Wemm static void indent __P((int, char *, FILE *));
64aa9caaf6SPeter Wemm static void trstring __P((char *));
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes 
67aa9caaf6SPeter Wemm void
684b88c807SRodney W. Grimes showtree(n)
694b88c807SRodney W. Grimes 	union node *n;
704b88c807SRodney W. Grimes {
714b88c807SRodney W. Grimes 	trputs("showtree called\n");
724b88c807SRodney W. Grimes 	shtree(n, 1, NULL, stdout);
734b88c807SRodney W. Grimes }
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes 
76aa9caaf6SPeter Wemm static void
774b88c807SRodney W. Grimes shtree(n, ind, pfx, fp)
784b88c807SRodney W. Grimes 	union node *n;
79aa9caaf6SPeter Wemm 	int ind;
804b88c807SRodney W. Grimes 	char *pfx;
814b88c807SRodney W. Grimes 	FILE *fp;
824b88c807SRodney W. Grimes {
834b88c807SRodney W. Grimes 	struct nodelist *lp;
844b88c807SRodney W. Grimes 	char *s;
854b88c807SRodney W. Grimes 
86aa9caaf6SPeter Wemm 	if (n == NULL)
87aa9caaf6SPeter Wemm 		return;
88aa9caaf6SPeter Wemm 
894b88c807SRodney W. Grimes 	indent(ind, pfx, fp);
904b88c807SRodney W. Grimes 	switch(n->type) {
914b88c807SRodney W. Grimes 	case NSEMI:
924b88c807SRodney W. Grimes 		s = "; ";
934b88c807SRodney W. Grimes 		goto binop;
944b88c807SRodney W. Grimes 	case NAND:
954b88c807SRodney W. Grimes 		s = " && ";
964b88c807SRodney W. Grimes 		goto binop;
974b88c807SRodney W. Grimes 	case NOR:
984b88c807SRodney W. Grimes 		s = " || ";
994b88c807SRodney W. Grimes binop:
1004b88c807SRodney W. Grimes 		shtree(n->nbinary.ch1, ind, NULL, fp);
1014b88c807SRodney W. Grimes 	   /*    if (ind < 0) */
1024b88c807SRodney W. Grimes 			fputs(s, fp);
1034b88c807SRodney W. Grimes 		shtree(n->nbinary.ch2, ind, NULL, fp);
1044b88c807SRodney W. Grimes 		break;
1054b88c807SRodney W. Grimes 	case NCMD:
1064b88c807SRodney W. Grimes 		shcmd(n, fp);
1074b88c807SRodney W. Grimes 		if (ind >= 0)
1084b88c807SRodney W. Grimes 			putc('\n', fp);
1094b88c807SRodney W. Grimes 		break;
1104b88c807SRodney W. Grimes 	case NPIPE:
1114b88c807SRodney W. Grimes 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1124b88c807SRodney W. Grimes 			shcmd(lp->n, fp);
1134b88c807SRodney W. Grimes 			if (lp->next)
1144b88c807SRodney W. Grimes 				fputs(" | ", fp);
1154b88c807SRodney W. Grimes 		}
1164b88c807SRodney W. Grimes 		if (n->npipe.backgnd)
1174b88c807SRodney W. Grimes 			fputs(" &", fp);
1184b88c807SRodney W. Grimes 		if (ind >= 0)
1194b88c807SRodney W. Grimes 			putc('\n', fp);
1204b88c807SRodney W. Grimes 		break;
1214b88c807SRodney W. Grimes 	default:
1224b88c807SRodney W. Grimes 		fprintf(fp, "<node type %d>", n->type);
1234b88c807SRodney W. Grimes 		if (ind >= 0)
1244b88c807SRodney W. Grimes 			putc('\n', fp);
1254b88c807SRodney W. Grimes 		break;
1264b88c807SRodney W. Grimes 	}
1274b88c807SRodney W. Grimes }
1284b88c807SRodney W. Grimes 
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes 
131aa9caaf6SPeter Wemm static void
1324b88c807SRodney W. Grimes shcmd(cmd, fp)
1334b88c807SRodney W. Grimes 	union node *cmd;
1344b88c807SRodney W. Grimes 	FILE *fp;
1354b88c807SRodney W. Grimes {
1364b88c807SRodney W. Grimes 	union node *np;
1374b88c807SRodney W. Grimes 	int first;
1384b88c807SRodney W. Grimes 	char *s;
1394b88c807SRodney W. Grimes 	int dftfd;
1404b88c807SRodney W. Grimes 
1414b88c807SRodney W. Grimes 	first = 1;
1424b88c807SRodney W. Grimes 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
1434b88c807SRodney W. Grimes 		if (! first)
1444b88c807SRodney W. Grimes 			putchar(' ');
1454b88c807SRodney W. Grimes 		sharg(np, fp);
1464b88c807SRodney W. Grimes 		first = 0;
1474b88c807SRodney W. Grimes 	}
1484b88c807SRodney W. Grimes 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
1494b88c807SRodney W. Grimes 		if (! first)
1504b88c807SRodney W. Grimes 			putchar(' ');
1514b88c807SRodney W. Grimes 		switch (np->nfile.type) {
1524b88c807SRodney W. Grimes 			case NTO:	s = ">";  dftfd = 1; break;
1534b88c807SRodney W. Grimes 			case NAPPEND:	s = ">>"; dftfd = 1; break;
1544b88c807SRodney W. Grimes 			case NTOFD:	s = ">&"; dftfd = 1; break;
1554b88c807SRodney W. Grimes 			case NFROM:	s = "<";  dftfd = 0; break;
1564b88c807SRodney W. Grimes 			case NFROMFD:	s = "<&"; dftfd = 0; break;
157aa9caaf6SPeter Wemm 			default:  	s = "*error*"; dftfd = 0; break;
1584b88c807SRodney W. Grimes 		}
1594b88c807SRodney W. Grimes 		if (np->nfile.fd != dftfd)
1604b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->nfile.fd);
1614b88c807SRodney W. Grimes 		fputs(s, fp);
1624b88c807SRodney W. Grimes 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1634b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->ndup.dupfd);
1644b88c807SRodney W. Grimes 		} else {
1654b88c807SRodney W. Grimes 			sharg(np->nfile.fname, fp);
1664b88c807SRodney W. Grimes 		}
1674b88c807SRodney W. Grimes 		first = 0;
1684b88c807SRodney W. Grimes 	}
1694b88c807SRodney W. Grimes }
1704b88c807SRodney W. Grimes 
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes 
173aa9caaf6SPeter Wemm static void
1744b88c807SRodney W. Grimes sharg(arg, fp)
1754b88c807SRodney W. Grimes 	union node *arg;
1764b88c807SRodney W. Grimes 	FILE *fp;
1774b88c807SRodney W. Grimes 	{
1784b88c807SRodney W. Grimes 	char *p;
1794b88c807SRodney W. Grimes 	struct nodelist *bqlist;
1804b88c807SRodney W. Grimes 	int subtype;
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes 	if (arg->type != NARG) {
1834b88c807SRodney W. Grimes 		printf("<node type %d>\n", arg->type);
1844b88c807SRodney W. Grimes 		fflush(stdout);
1854b88c807SRodney W. Grimes 		abort();
1864b88c807SRodney W. Grimes 	}
1874b88c807SRodney W. Grimes 	bqlist = arg->narg.backquote;
1884b88c807SRodney W. Grimes 	for (p = arg->narg.text ; *p ; p++) {
1894b88c807SRodney W. Grimes 		switch (*p) {
1904b88c807SRodney W. Grimes 		case CTLESC:
1914b88c807SRodney W. Grimes 			putc(*++p, fp);
1924b88c807SRodney W. Grimes 			break;
1934b88c807SRodney W. Grimes 		case CTLVAR:
1944b88c807SRodney W. Grimes 			putc('$', fp);
1954b88c807SRodney W. Grimes 			putc('{', fp);
1964b88c807SRodney W. Grimes 			subtype = *++p;
197aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH)
198aa9caaf6SPeter Wemm 				putc('#', fp);
199aa9caaf6SPeter Wemm 
2004b88c807SRodney W. Grimes 			while (*p != '=')
2014b88c807SRodney W. Grimes 				putc(*p++, fp);
202aa9caaf6SPeter Wemm 
2034b88c807SRodney W. Grimes 			if (subtype & VSNUL)
2044b88c807SRodney W. Grimes 				putc(':', fp);
205aa9caaf6SPeter Wemm 
2064b88c807SRodney W. Grimes 			switch (subtype & VSTYPE) {
2074b88c807SRodney W. Grimes 			case VSNORMAL:
2084b88c807SRodney W. Grimes 				putc('}', fp);
2094b88c807SRodney W. Grimes 				break;
2104b88c807SRodney W. Grimes 			case VSMINUS:
2114b88c807SRodney W. Grimes 				putc('-', fp);
2124b88c807SRodney W. Grimes 				break;
2134b88c807SRodney W. Grimes 			case VSPLUS:
2144b88c807SRodney W. Grimes 				putc('+', fp);
2154b88c807SRodney W. Grimes 				break;
2164b88c807SRodney W. Grimes 			case VSQUESTION:
2174b88c807SRodney W. Grimes 				putc('?', fp);
2184b88c807SRodney W. Grimes 				break;
2194b88c807SRodney W. Grimes 			case VSASSIGN:
2204b88c807SRodney W. Grimes 				putc('=', fp);
2214b88c807SRodney W. Grimes 				break;
222aa9caaf6SPeter Wemm 			case VSTRIMLEFT:
223aa9caaf6SPeter Wemm 				putc('#', fp);
224aa9caaf6SPeter Wemm 				break;
225aa9caaf6SPeter Wemm 			case VSTRIMLEFTMAX:
226aa9caaf6SPeter Wemm 				putc('#', fp);
227aa9caaf6SPeter Wemm 				putc('#', fp);
228aa9caaf6SPeter Wemm 				break;
229aa9caaf6SPeter Wemm 			case VSTRIMRIGHT:
230aa9caaf6SPeter Wemm 				putc('%', fp);
231aa9caaf6SPeter Wemm 				break;
232aa9caaf6SPeter Wemm 			case VSTRIMRIGHTMAX:
233aa9caaf6SPeter Wemm 				putc('%', fp);
234aa9caaf6SPeter Wemm 				putc('%', fp);
235aa9caaf6SPeter Wemm 				break;
236aa9caaf6SPeter Wemm 			case VSLENGTH:
237aa9caaf6SPeter Wemm 				break;
2384b88c807SRodney W. Grimes 			default:
2394b88c807SRodney W. Grimes 				printf("<subtype %d>", subtype);
2404b88c807SRodney W. Grimes 			}
2414b88c807SRodney W. Grimes 			break;
2424b88c807SRodney W. Grimes 		case CTLENDVAR:
2434b88c807SRodney W. Grimes 		     putc('}', fp);
2444b88c807SRodney W. Grimes 		     break;
2454b88c807SRodney W. Grimes 		case CTLBACKQ:
2464b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
2474b88c807SRodney W. Grimes 			putc('$', fp);
2484b88c807SRodney W. Grimes 			putc('(', fp);
2494b88c807SRodney W. Grimes 			shtree(bqlist->n, -1, NULL, fp);
2504b88c807SRodney W. Grimes 			putc(')', fp);
2514b88c807SRodney W. Grimes 			break;
2524b88c807SRodney W. Grimes 		default:
2534b88c807SRodney W. Grimes 			putc(*p, fp);
2544b88c807SRodney W. Grimes 			break;
2554b88c807SRodney W. Grimes 		}
2564b88c807SRodney W. Grimes 	}
2574b88c807SRodney W. Grimes }
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes 
260aa9caaf6SPeter Wemm static void
2614b88c807SRodney W. Grimes indent(amount, pfx, fp)
262aa9caaf6SPeter Wemm 	int amount;
2634b88c807SRodney W. Grimes 	char *pfx;
2644b88c807SRodney W. Grimes 	FILE *fp;
2654b88c807SRodney W. Grimes {
2664b88c807SRodney W. Grimes 	int i;
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 	for (i = 0 ; i < amount ; i++) {
2694b88c807SRodney W. Grimes 		if (pfx && i == amount - 1)
2704b88c807SRodney W. Grimes 			fputs(pfx, fp);
2714b88c807SRodney W. Grimes 		putc('\t', fp);
2724b88c807SRodney W. Grimes 	}
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 	if (tracefile == NULL)
2954b88c807SRodney W. Grimes 		return;
2964b88c807SRodney W. Grimes 	putc(c, tracefile);
2974b88c807SRodney W. Grimes 	if (c == '\n')
2984b88c807SRodney W. Grimes 		fflush(tracefile);
2994b88c807SRodney W. Grimes }
3004b88c807SRodney W. Grimes 
301ab0a2172SSteve Price 
302aa9caaf6SPeter Wemm void
303afb033d5SSteve Price #ifdef __STDC__
304ab0a2172SSteve Price trace(const char *fmt, ...)
305aa9caaf6SPeter Wemm #else
306ab0a2172SSteve Price trace(va_alist)
307aa9caaf6SPeter Wemm 	va_dcl
308aa9caaf6SPeter Wemm #endif
3094b88c807SRodney W. Grimes {
310aa9caaf6SPeter Wemm 	va_list va;
311afb033d5SSteve Price #ifdef __STDC__
312aa9caaf6SPeter Wemm 	va_start(va, fmt);
313aa9caaf6SPeter Wemm #else
314aa9caaf6SPeter Wemm 	char *fmt;
315aa9caaf6SPeter Wemm 	va_start(va);
316aa9caaf6SPeter Wemm 	fmt = va_arg(va, char *);
317aa9caaf6SPeter Wemm #endif
318aa9caaf6SPeter Wemm 	if (tracefile != NULL) {
319aa9caaf6SPeter Wemm 		(void) vfprintf(tracefile, fmt, va);
3204b88c807SRodney W. Grimes 		if (strchr(fmt, '\n'))
321aa9caaf6SPeter Wemm 			(void) fflush(tracefile);
322aa9caaf6SPeter Wemm 	}
323aa9caaf6SPeter Wemm 	va_end(va);
3244b88c807SRodney W. Grimes }
3254b88c807SRodney W. Grimes 
3264b88c807SRodney W. Grimes 
327aa9caaf6SPeter Wemm void
3284b88c807SRodney W. Grimes trputs(s)
3294b88c807SRodney W. Grimes 	char *s;
3304b88c807SRodney W. Grimes {
3314b88c807SRodney W. Grimes 	if (tracefile == NULL)
3324b88c807SRodney W. Grimes 		return;
3334b88c807SRodney W. Grimes 	fputs(s, tracefile);
3344b88c807SRodney W. Grimes 	if (strchr(s, '\n'))
3354b88c807SRodney W. Grimes 		fflush(tracefile);
3364b88c807SRodney W. Grimes }
3374b88c807SRodney W. Grimes 
3384b88c807SRodney W. Grimes 
339aa9caaf6SPeter Wemm static void
3404b88c807SRodney W. Grimes trstring(s)
3414b88c807SRodney W. Grimes 	char *s;
3424b88c807SRodney W. Grimes {
343afb033d5SSteve Price 	char *p;
3444b88c807SRodney W. Grimes 	char c;
3454b88c807SRodney W. Grimes 
3464b88c807SRodney W. Grimes 	if (tracefile == NULL)
3474b88c807SRodney W. Grimes 		return;
3484b88c807SRodney W. Grimes 	putc('"', tracefile);
3494b88c807SRodney W. Grimes 	for (p = s ; *p ; p++) {
3504b88c807SRodney W. Grimes 		switch (*p) {
3514b88c807SRodney W. Grimes 		case '\n':  c = 'n';  goto backslash;
3524b88c807SRodney W. Grimes 		case '\t':  c = 't';  goto backslash;
3534b88c807SRodney W. Grimes 		case '\r':  c = 'r';  goto backslash;
3544b88c807SRodney W. Grimes 		case '"':  c = '"';  goto backslash;
3554b88c807SRodney W. Grimes 		case '\\':  c = '\\';  goto backslash;
3564b88c807SRodney W. Grimes 		case CTLESC:  c = 'e';  goto backslash;
3574b88c807SRodney W. Grimes 		case CTLVAR:  c = 'v';  goto backslash;
3584b88c807SRodney W. Grimes 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
3594b88c807SRodney W. Grimes 		case CTLBACKQ:  c = 'q';  goto backslash;
3604b88c807SRodney W. Grimes 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
3614b88c807SRodney W. Grimes backslash:	  putc('\\', tracefile);
3624b88c807SRodney W. Grimes 			putc(c, tracefile);
3634b88c807SRodney W. Grimes 			break;
3644b88c807SRodney W. Grimes 		default:
3654b88c807SRodney W. Grimes 			if (*p >= ' ' && *p <= '~')
3664b88c807SRodney W. Grimes 				putc(*p, tracefile);
3674b88c807SRodney W. Grimes 			else {
3684b88c807SRodney W. Grimes 				putc('\\', tracefile);
3694b88c807SRodney W. Grimes 				putc(*p >> 6 & 03, tracefile);
3704b88c807SRodney W. Grimes 				putc(*p >> 3 & 07, tracefile);
3714b88c807SRodney W. Grimes 				putc(*p & 07, tracefile);
3724b88c807SRodney W. Grimes 			}
3734b88c807SRodney W. Grimes 			break;
3744b88c807SRodney W. Grimes 		}
3754b88c807SRodney W. Grimes 	}
3764b88c807SRodney W. Grimes 	putc('"', tracefile);
3774b88c807SRodney W. Grimes }
3784b88c807SRodney W. Grimes 
3794b88c807SRodney W. Grimes 
380aa9caaf6SPeter Wemm void
3814b88c807SRodney W. Grimes trargs(ap)
3824b88c807SRodney W. Grimes 	char **ap;
3834b88c807SRodney W. Grimes {
3844b88c807SRodney W. Grimes 	if (tracefile == NULL)
3854b88c807SRodney W. Grimes 		return;
3864b88c807SRodney W. Grimes 	while (*ap) {
3874b88c807SRodney W. Grimes 		trstring(*ap++);
3884b88c807SRodney W. Grimes 		if (*ap)
3894b88c807SRodney W. Grimes 			putc(' ', tracefile);
3904b88c807SRodney W. Grimes 		else
3914b88c807SRodney W. Grimes 			putc('\n', tracefile);
3924b88c807SRodney W. Grimes 	}
3934b88c807SRodney W. Grimes 	fflush(tracefile);
3944b88c807SRodney W. Grimes }
3954b88c807SRodney W. Grimes 
3964b88c807SRodney W. Grimes 
397aa9caaf6SPeter Wemm void
3984b88c807SRodney W. Grimes opentrace() {
3994b88c807SRodney W. Grimes 	char s[100];
4004b88c807SRodney W. Grimes 	char *getenv();
401aa9caaf6SPeter Wemm #ifdef O_APPEND
4024b88c807SRodney W. Grimes 	int flags;
403aa9caaf6SPeter Wemm #endif
4044b88c807SRodney W. Grimes 
4054b88c807SRodney W. Grimes 	if (!debug)
4064b88c807SRodney W. Grimes 		return;
4074b88c807SRodney W. Grimes #ifdef not_this_way
408aa9caaf6SPeter Wemm 	{
409aa9caaf6SPeter Wemm 		char *p;
4104b88c807SRodney W. Grimes 		if ((p = getenv("HOME")) == NULL) {
4114b88c807SRodney W. Grimes 			if (geteuid() == 0)
4124b88c807SRodney W. Grimes 				p = "/";
4134b88c807SRodney W. Grimes 			else
4144b88c807SRodney W. Grimes 				p = "/tmp";
4154b88c807SRodney W. Grimes 		}
4164b88c807SRodney W. Grimes 		scopy(p, s);
4174b88c807SRodney W. Grimes 		strcat(s, "/trace");
418aa9caaf6SPeter Wemm 	}
4194b88c807SRodney W. Grimes #else
4204b88c807SRodney W. Grimes 	scopy("./trace", s);
4214b88c807SRodney W. Grimes #endif /* not_this_way */
4224b88c807SRodney W. Grimes 	if ((tracefile = fopen(s, "a")) == NULL) {
4236c48b6cfSMartin Cracauer 		fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
4244b88c807SRodney W. Grimes 		return;
4254b88c807SRodney W. Grimes 	}
4264b88c807SRodney W. Grimes #ifdef O_APPEND
4274b88c807SRodney W. Grimes 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
4284b88c807SRodney W. Grimes 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
4294b88c807SRodney W. Grimes #endif
4304b88c807SRodney W. Grimes 	fputs("\nTracing started.\n", tracefile);
4314b88c807SRodney W. Grimes 	fflush(tracefile);
4324b88c807SRodney W. Grimes }
433ab0a2172SSteve Price #endif /* DEBUG */
434