xref: /minix3/bin/sh/show.c (revision d90bee97498b3043241050f61aed100786c59df4)
1*d90bee97SLionel Sambuc /*	$NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $	*/
2*d90bee97SLionel Sambuc 
3*d90bee97SLionel Sambuc /*-
4*d90bee97SLionel Sambuc  * Copyright (c) 1991, 1993
5*d90bee97SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
6*d90bee97SLionel Sambuc  *
7*d90bee97SLionel Sambuc  * This code is derived from software contributed to Berkeley by
8*d90bee97SLionel Sambuc  * Kenneth Almquist.
9*d90bee97SLionel Sambuc  *
10*d90bee97SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*d90bee97SLionel Sambuc  * modification, are permitted provided that the following conditions
12*d90bee97SLionel Sambuc  * are met:
13*d90bee97SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*d90bee97SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*d90bee97SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*d90bee97SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*d90bee97SLionel Sambuc  * 3. Neither the name of the University nor the names of its contributors
19*d90bee97SLionel Sambuc  *    may be used to endorse or promote products derived from this software
20*d90bee97SLionel Sambuc  *    without specific prior written permission.
21*d90bee97SLionel Sambuc  *
22*d90bee97SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*d90bee97SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*d90bee97SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*d90bee97SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*d90bee97SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*d90bee97SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*d90bee97SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*d90bee97SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*d90bee97SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*d90bee97SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*d90bee97SLionel Sambuc  * SUCH DAMAGE.
33*d90bee97SLionel Sambuc  */
34*d90bee97SLionel Sambuc 
35*d90bee97SLionel Sambuc #include <sys/cdefs.h>
36*d90bee97SLionel Sambuc #ifndef lint
37*d90bee97SLionel Sambuc #if 0
38*d90bee97SLionel Sambuc static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
39*d90bee97SLionel Sambuc #else
40*d90bee97SLionel Sambuc __RCSID("$NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $");
41*d90bee97SLionel Sambuc #endif
42*d90bee97SLionel Sambuc #endif /* not lint */
43*d90bee97SLionel Sambuc 
44*d90bee97SLionel Sambuc #include <stdio.h>
45*d90bee97SLionel Sambuc #include <stdarg.h>
46*d90bee97SLionel Sambuc #include <stdlib.h>
47*d90bee97SLionel Sambuc #include <unistd.h>
48*d90bee97SLionel Sambuc 
49*d90bee97SLionel Sambuc #include "shell.h"
50*d90bee97SLionel Sambuc #include "parser.h"
51*d90bee97SLionel Sambuc #include "nodes.h"
52*d90bee97SLionel Sambuc #include "mystring.h"
53*d90bee97SLionel Sambuc #include "show.h"
54*d90bee97SLionel Sambuc #include "options.h"
55*d90bee97SLionel Sambuc 
56*d90bee97SLionel Sambuc 
57*d90bee97SLionel Sambuc #ifdef DEBUG
58*d90bee97SLionel Sambuc static void shtree(union node *, int, char *, FILE*);
59*d90bee97SLionel Sambuc static void shcmd(union node *, FILE *);
60*d90bee97SLionel Sambuc static void sharg(union node *, FILE *);
61*d90bee97SLionel Sambuc static void indent(int, char *, FILE *);
62*d90bee97SLionel Sambuc static void trstring(char *);
63*d90bee97SLionel Sambuc 
64*d90bee97SLionel Sambuc 
65*d90bee97SLionel Sambuc void
showtree(union node * n)66*d90bee97SLionel Sambuc showtree(union node *n)
67*d90bee97SLionel Sambuc {
68*d90bee97SLionel Sambuc 	trputs("showtree called\n");
69*d90bee97SLionel Sambuc 	shtree(n, 1, NULL, stdout);
70*d90bee97SLionel Sambuc }
71*d90bee97SLionel Sambuc 
72*d90bee97SLionel Sambuc 
73*d90bee97SLionel Sambuc static void
shtree(union node * n,int ind,char * pfx,FILE * fp)74*d90bee97SLionel Sambuc shtree(union node *n, int ind, char *pfx, FILE *fp)
75*d90bee97SLionel Sambuc {
76*d90bee97SLionel Sambuc 	struct nodelist *lp;
77*d90bee97SLionel Sambuc 	const char *s;
78*d90bee97SLionel Sambuc 
79*d90bee97SLionel Sambuc 	if (n == NULL)
80*d90bee97SLionel Sambuc 		return;
81*d90bee97SLionel Sambuc 
82*d90bee97SLionel Sambuc 	indent(ind, pfx, fp);
83*d90bee97SLionel Sambuc 	switch(n->type) {
84*d90bee97SLionel Sambuc 	case NSEMI:
85*d90bee97SLionel Sambuc 		s = "; ";
86*d90bee97SLionel Sambuc 		goto binop;
87*d90bee97SLionel Sambuc 	case NAND:
88*d90bee97SLionel Sambuc 		s = " && ";
89*d90bee97SLionel Sambuc 		goto binop;
90*d90bee97SLionel Sambuc 	case NOR:
91*d90bee97SLionel Sambuc 		s = " || ";
92*d90bee97SLionel Sambuc binop:
93*d90bee97SLionel Sambuc 		shtree(n->nbinary.ch1, ind, NULL, fp);
94*d90bee97SLionel Sambuc 	   /*    if (ind < 0) */
95*d90bee97SLionel Sambuc 			fputs(s, fp);
96*d90bee97SLionel Sambuc 		shtree(n->nbinary.ch2, ind, NULL, fp);
97*d90bee97SLionel Sambuc 		break;
98*d90bee97SLionel Sambuc 	case NCMD:
99*d90bee97SLionel Sambuc 		shcmd(n, fp);
100*d90bee97SLionel Sambuc 		if (ind >= 0)
101*d90bee97SLionel Sambuc 			putc('\n', fp);
102*d90bee97SLionel Sambuc 		break;
103*d90bee97SLionel Sambuc 	case NPIPE:
104*d90bee97SLionel Sambuc 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
105*d90bee97SLionel Sambuc 			shcmd(lp->n, fp);
106*d90bee97SLionel Sambuc 			if (lp->next)
107*d90bee97SLionel Sambuc 				fputs(" | ", fp);
108*d90bee97SLionel Sambuc 		}
109*d90bee97SLionel Sambuc 		if (n->npipe.backgnd)
110*d90bee97SLionel Sambuc 			fputs(" &", fp);
111*d90bee97SLionel Sambuc 		if (ind >= 0)
112*d90bee97SLionel Sambuc 			putc('\n', fp);
113*d90bee97SLionel Sambuc 		break;
114*d90bee97SLionel Sambuc 	default:
115*d90bee97SLionel Sambuc 		fprintf(fp, "<node type %d>", n->type);
116*d90bee97SLionel Sambuc 		if (ind >= 0)
117*d90bee97SLionel Sambuc 			putc('\n', fp);
118*d90bee97SLionel Sambuc 		break;
119*d90bee97SLionel Sambuc 	}
120*d90bee97SLionel Sambuc }
121*d90bee97SLionel Sambuc 
122*d90bee97SLionel Sambuc 
123*d90bee97SLionel Sambuc 
124*d90bee97SLionel Sambuc static void
shcmd(union node * cmd,FILE * fp)125*d90bee97SLionel Sambuc shcmd(union node *cmd, FILE *fp)
126*d90bee97SLionel Sambuc {
127*d90bee97SLionel Sambuc 	union node *np;
128*d90bee97SLionel Sambuc 	int first;
129*d90bee97SLionel Sambuc 	const char *s;
130*d90bee97SLionel Sambuc 	int dftfd;
131*d90bee97SLionel Sambuc 
132*d90bee97SLionel Sambuc 	first = 1;
133*d90bee97SLionel Sambuc 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
134*d90bee97SLionel Sambuc 		if (! first)
135*d90bee97SLionel Sambuc 			putchar(' ');
136*d90bee97SLionel Sambuc 		sharg(np, fp);
137*d90bee97SLionel Sambuc 		first = 0;
138*d90bee97SLionel Sambuc 	}
139*d90bee97SLionel Sambuc 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
140*d90bee97SLionel Sambuc 		if (! first)
141*d90bee97SLionel Sambuc 			putchar(' ');
142*d90bee97SLionel Sambuc 		switch (np->nfile.type) {
143*d90bee97SLionel Sambuc 			case NTO:	s = ">";  dftfd = 1; break;
144*d90bee97SLionel Sambuc 			case NCLOBBER:	s = ">|"; dftfd = 1; break;
145*d90bee97SLionel Sambuc 			case NAPPEND:	s = ">>"; dftfd = 1; break;
146*d90bee97SLionel Sambuc 			case NTOFD:	s = ">&"; dftfd = 1; break;
147*d90bee97SLionel Sambuc 			case NFROM:	s = "<";  dftfd = 0; break;
148*d90bee97SLionel Sambuc 			case NFROMFD:	s = "<&"; dftfd = 0; break;
149*d90bee97SLionel Sambuc 			case NFROMTO:	s = "<>"; dftfd = 0; break;
150*d90bee97SLionel Sambuc 			default:  	s = "*error*"; dftfd = 0; break;
151*d90bee97SLionel Sambuc 		}
152*d90bee97SLionel Sambuc 		if (np->nfile.fd != dftfd)
153*d90bee97SLionel Sambuc 			fprintf(fp, "%d", np->nfile.fd);
154*d90bee97SLionel Sambuc 		fputs(s, fp);
155*d90bee97SLionel Sambuc 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
156*d90bee97SLionel Sambuc 			fprintf(fp, "%d", np->ndup.dupfd);
157*d90bee97SLionel Sambuc 		} else {
158*d90bee97SLionel Sambuc 			sharg(np->nfile.fname, fp);
159*d90bee97SLionel Sambuc 		}
160*d90bee97SLionel Sambuc 		first = 0;
161*d90bee97SLionel Sambuc 	}
162*d90bee97SLionel Sambuc }
163*d90bee97SLionel Sambuc 
164*d90bee97SLionel Sambuc 
165*d90bee97SLionel Sambuc 
166*d90bee97SLionel Sambuc static void
sharg(union node * arg,FILE * fp)167*d90bee97SLionel Sambuc sharg(union node *arg, FILE *fp)
168*d90bee97SLionel Sambuc {
169*d90bee97SLionel Sambuc 	char *p;
170*d90bee97SLionel Sambuc 	struct nodelist *bqlist;
171*d90bee97SLionel Sambuc 	int subtype;
172*d90bee97SLionel Sambuc 
173*d90bee97SLionel Sambuc 	if (arg->type != NARG) {
174*d90bee97SLionel Sambuc 		printf("<node type %d>\n", arg->type);
175*d90bee97SLionel Sambuc 		abort();
176*d90bee97SLionel Sambuc 	}
177*d90bee97SLionel Sambuc 	bqlist = arg->narg.backquote;
178*d90bee97SLionel Sambuc 	for (p = arg->narg.text ; *p ; p++) {
179*d90bee97SLionel Sambuc 		switch (*p) {
180*d90bee97SLionel Sambuc 		case CTLESC:
181*d90bee97SLionel Sambuc 			putc(*++p, fp);
182*d90bee97SLionel Sambuc 			break;
183*d90bee97SLionel Sambuc 		case CTLVAR:
184*d90bee97SLionel Sambuc 			putc('$', fp);
185*d90bee97SLionel Sambuc 			putc('{', fp);
186*d90bee97SLionel Sambuc 			subtype = *++p;
187*d90bee97SLionel Sambuc 			if (subtype == VSLENGTH)
188*d90bee97SLionel Sambuc 				putc('#', fp);
189*d90bee97SLionel Sambuc 
190*d90bee97SLionel Sambuc 			while (*p != '=')
191*d90bee97SLionel Sambuc 				putc(*p++, fp);
192*d90bee97SLionel Sambuc 
193*d90bee97SLionel Sambuc 			if (subtype & VSNUL)
194*d90bee97SLionel Sambuc 				putc(':', fp);
195*d90bee97SLionel Sambuc 
196*d90bee97SLionel Sambuc 			switch (subtype & VSTYPE) {
197*d90bee97SLionel Sambuc 			case VSNORMAL:
198*d90bee97SLionel Sambuc 				putc('}', fp);
199*d90bee97SLionel Sambuc 				break;
200*d90bee97SLionel Sambuc 			case VSMINUS:
201*d90bee97SLionel Sambuc 				putc('-', fp);
202*d90bee97SLionel Sambuc 				break;
203*d90bee97SLionel Sambuc 			case VSPLUS:
204*d90bee97SLionel Sambuc 				putc('+', fp);
205*d90bee97SLionel Sambuc 				break;
206*d90bee97SLionel Sambuc 			case VSQUESTION:
207*d90bee97SLionel Sambuc 				putc('?', fp);
208*d90bee97SLionel Sambuc 				break;
209*d90bee97SLionel Sambuc 			case VSASSIGN:
210*d90bee97SLionel Sambuc 				putc('=', fp);
211*d90bee97SLionel Sambuc 				break;
212*d90bee97SLionel Sambuc 			case VSTRIMLEFT:
213*d90bee97SLionel Sambuc 				putc('#', fp);
214*d90bee97SLionel Sambuc 				break;
215*d90bee97SLionel Sambuc 			case VSTRIMLEFTMAX:
216*d90bee97SLionel Sambuc 				putc('#', fp);
217*d90bee97SLionel Sambuc 				putc('#', fp);
218*d90bee97SLionel Sambuc 				break;
219*d90bee97SLionel Sambuc 			case VSTRIMRIGHT:
220*d90bee97SLionel Sambuc 				putc('%', fp);
221*d90bee97SLionel Sambuc 				break;
222*d90bee97SLionel Sambuc 			case VSTRIMRIGHTMAX:
223*d90bee97SLionel Sambuc 				putc('%', fp);
224*d90bee97SLionel Sambuc 				putc('%', fp);
225*d90bee97SLionel Sambuc 				break;
226*d90bee97SLionel Sambuc 			case VSLENGTH:
227*d90bee97SLionel Sambuc 				break;
228*d90bee97SLionel Sambuc 			default:
229*d90bee97SLionel Sambuc 				printf("<subtype %d>", subtype);
230*d90bee97SLionel Sambuc 			}
231*d90bee97SLionel Sambuc 			break;
232*d90bee97SLionel Sambuc 		case CTLENDVAR:
233*d90bee97SLionel Sambuc 		     putc('}', fp);
234*d90bee97SLionel Sambuc 		     break;
235*d90bee97SLionel Sambuc 		case CTLBACKQ:
236*d90bee97SLionel Sambuc 		case CTLBACKQ|CTLQUOTE:
237*d90bee97SLionel Sambuc 			putc('$', fp);
238*d90bee97SLionel Sambuc 			putc('(', fp);
239*d90bee97SLionel Sambuc 			shtree(bqlist->n, -1, NULL, fp);
240*d90bee97SLionel Sambuc 			putc(')', fp);
241*d90bee97SLionel Sambuc 			break;
242*d90bee97SLionel Sambuc 		default:
243*d90bee97SLionel Sambuc 			putc(*p, fp);
244*d90bee97SLionel Sambuc 			break;
245*d90bee97SLionel Sambuc 		}
246*d90bee97SLionel Sambuc 	}
247*d90bee97SLionel Sambuc }
248*d90bee97SLionel Sambuc 
249*d90bee97SLionel Sambuc 
250*d90bee97SLionel Sambuc static void
indent(int amount,char * pfx,FILE * fp)251*d90bee97SLionel Sambuc indent(int amount, char *pfx, FILE *fp)
252*d90bee97SLionel Sambuc {
253*d90bee97SLionel Sambuc 	int i;
254*d90bee97SLionel Sambuc 
255*d90bee97SLionel Sambuc 	for (i = 0 ; i < amount ; i++) {
256*d90bee97SLionel Sambuc 		if (pfx && i == amount - 1)
257*d90bee97SLionel Sambuc 			fputs(pfx, fp);
258*d90bee97SLionel Sambuc 		putc('\t', fp);
259*d90bee97SLionel Sambuc 	}
260*d90bee97SLionel Sambuc }
261*d90bee97SLionel Sambuc #endif
262*d90bee97SLionel Sambuc 
263*d90bee97SLionel Sambuc 
264*d90bee97SLionel Sambuc 
265*d90bee97SLionel Sambuc /*
266*d90bee97SLionel Sambuc  * Debugging stuff.
267*d90bee97SLionel Sambuc  */
268*d90bee97SLionel Sambuc 
269*d90bee97SLionel Sambuc 
270*d90bee97SLionel Sambuc FILE *tracefile;
271*d90bee97SLionel Sambuc 
272*d90bee97SLionel Sambuc 
273*d90bee97SLionel Sambuc #ifdef DEBUG
274*d90bee97SLionel Sambuc void
trputc(int c)275*d90bee97SLionel Sambuc trputc(int c)
276*d90bee97SLionel Sambuc {
277*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
278*d90bee97SLionel Sambuc 		return;
279*d90bee97SLionel Sambuc 	putc(c, tracefile);
280*d90bee97SLionel Sambuc }
281*d90bee97SLionel Sambuc #endif
282*d90bee97SLionel Sambuc 
283*d90bee97SLionel Sambuc void
trace(const char * fmt,...)284*d90bee97SLionel Sambuc trace(const char *fmt, ...)
285*d90bee97SLionel Sambuc {
286*d90bee97SLionel Sambuc #ifdef DEBUG
287*d90bee97SLionel Sambuc 	va_list va;
288*d90bee97SLionel Sambuc 
289*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
290*d90bee97SLionel Sambuc 		return;
291*d90bee97SLionel Sambuc 	va_start(va, fmt);
292*d90bee97SLionel Sambuc 	(void) vfprintf(tracefile, fmt, va);
293*d90bee97SLionel Sambuc 	va_end(va);
294*d90bee97SLionel Sambuc #endif
295*d90bee97SLionel Sambuc }
296*d90bee97SLionel Sambuc 
297*d90bee97SLionel Sambuc void
tracev(const char * fmt,va_list va)298*d90bee97SLionel Sambuc tracev(const char *fmt, va_list va)
299*d90bee97SLionel Sambuc {
300*d90bee97SLionel Sambuc #ifdef DEBUG
301*d90bee97SLionel Sambuc 	va_list ap;
302*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
303*d90bee97SLionel Sambuc 		return;
304*d90bee97SLionel Sambuc 	va_copy(ap, va);
305*d90bee97SLionel Sambuc 	(void) vfprintf(tracefile, fmt, ap);
306*d90bee97SLionel Sambuc 	va_end(ap);
307*d90bee97SLionel Sambuc #endif
308*d90bee97SLionel Sambuc }
309*d90bee97SLionel Sambuc 
310*d90bee97SLionel Sambuc 
311*d90bee97SLionel Sambuc #ifdef DEBUG
312*d90bee97SLionel Sambuc void
trputs(const char * s)313*d90bee97SLionel Sambuc trputs(const char *s)
314*d90bee97SLionel Sambuc {
315*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
316*d90bee97SLionel Sambuc 		return;
317*d90bee97SLionel Sambuc 	fputs(s, tracefile);
318*d90bee97SLionel Sambuc }
319*d90bee97SLionel Sambuc 
320*d90bee97SLionel Sambuc 
321*d90bee97SLionel Sambuc static void
trstring(char * s)322*d90bee97SLionel Sambuc trstring(char *s)
323*d90bee97SLionel Sambuc {
324*d90bee97SLionel Sambuc 	char *p;
325*d90bee97SLionel Sambuc 	char c;
326*d90bee97SLionel Sambuc 
327*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
328*d90bee97SLionel Sambuc 		return;
329*d90bee97SLionel Sambuc 	putc('"', tracefile);
330*d90bee97SLionel Sambuc 	for (p = s ; *p ; p++) {
331*d90bee97SLionel Sambuc 		switch (*p) {
332*d90bee97SLionel Sambuc 		case '\n':  c = 'n';  goto backslash;
333*d90bee97SLionel Sambuc 		case '\t':  c = 't';  goto backslash;
334*d90bee97SLionel Sambuc 		case '\r':  c = 'r';  goto backslash;
335*d90bee97SLionel Sambuc 		case '"':  c = '"';  goto backslash;
336*d90bee97SLionel Sambuc 		case '\\':  c = '\\';  goto backslash;
337*d90bee97SLionel Sambuc 		case CTLESC:  c = 'e';  goto backslash;
338*d90bee97SLionel Sambuc 		case CTLVAR:  c = 'v';  goto backslash;
339*d90bee97SLionel Sambuc 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
340*d90bee97SLionel Sambuc 		case CTLBACKQ:  c = 'q';  goto backslash;
341*d90bee97SLionel Sambuc 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
342*d90bee97SLionel Sambuc backslash:	  putc('\\', tracefile);
343*d90bee97SLionel Sambuc 			putc(c, tracefile);
344*d90bee97SLionel Sambuc 			break;
345*d90bee97SLionel Sambuc 		default:
346*d90bee97SLionel Sambuc 			if (*p >= ' ' && *p <= '~')
347*d90bee97SLionel Sambuc 				putc(*p, tracefile);
348*d90bee97SLionel Sambuc 			else {
349*d90bee97SLionel Sambuc 				putc('\\', tracefile);
350*d90bee97SLionel Sambuc 				putc(*p >> 6 & 03, tracefile);
351*d90bee97SLionel Sambuc 				putc(*p >> 3 & 07, tracefile);
352*d90bee97SLionel Sambuc 				putc(*p & 07, tracefile);
353*d90bee97SLionel Sambuc 			}
354*d90bee97SLionel Sambuc 			break;
355*d90bee97SLionel Sambuc 		}
356*d90bee97SLionel Sambuc 	}
357*d90bee97SLionel Sambuc 	putc('"', tracefile);
358*d90bee97SLionel Sambuc }
359*d90bee97SLionel Sambuc #endif
360*d90bee97SLionel Sambuc 
361*d90bee97SLionel Sambuc 
362*d90bee97SLionel Sambuc void
trargs(char ** ap)363*d90bee97SLionel Sambuc trargs(char **ap)
364*d90bee97SLionel Sambuc {
365*d90bee97SLionel Sambuc #ifdef DEBUG
366*d90bee97SLionel Sambuc 	if (debug != 1 || !tracefile)
367*d90bee97SLionel Sambuc 		return;
368*d90bee97SLionel Sambuc 	while (*ap) {
369*d90bee97SLionel Sambuc 		trstring(*ap++);
370*d90bee97SLionel Sambuc 		if (*ap)
371*d90bee97SLionel Sambuc 			putc(' ', tracefile);
372*d90bee97SLionel Sambuc 		else
373*d90bee97SLionel Sambuc 			putc('\n', tracefile);
374*d90bee97SLionel Sambuc 	}
375*d90bee97SLionel Sambuc #endif
376*d90bee97SLionel Sambuc }
377*d90bee97SLionel Sambuc 
378*d90bee97SLionel Sambuc 
379*d90bee97SLionel Sambuc #ifdef DEBUG
380*d90bee97SLionel Sambuc void
opentrace(void)381*d90bee97SLionel Sambuc opentrace(void)
382*d90bee97SLionel Sambuc {
383*d90bee97SLionel Sambuc 	char s[100];
384*d90bee97SLionel Sambuc #ifdef O_APPEND
385*d90bee97SLionel Sambuc 	int flags;
386*d90bee97SLionel Sambuc #endif
387*d90bee97SLionel Sambuc 
388*d90bee97SLionel Sambuc 	if (debug != 1) {
389*d90bee97SLionel Sambuc 		if (tracefile)
390*d90bee97SLionel Sambuc 			fflush(tracefile);
391*d90bee97SLionel Sambuc 		/* leave open because libedit might be using it */
392*d90bee97SLionel Sambuc 		return;
393*d90bee97SLionel Sambuc 	}
394*d90bee97SLionel Sambuc #ifdef not_this_way
395*d90bee97SLionel Sambuc 	{
396*d90bee97SLionel Sambuc 		char *p;
397*d90bee97SLionel Sambuc 		if ((p = getenv("HOME")) == NULL) {
398*d90bee97SLionel Sambuc 			if (geteuid() == 0)
399*d90bee97SLionel Sambuc 				p = "/";
400*d90bee97SLionel Sambuc 			else
401*d90bee97SLionel Sambuc 				p = "/tmp";
402*d90bee97SLionel Sambuc 		}
403*d90bee97SLionel Sambuc 		scopy(p, s);
404*d90bee97SLionel Sambuc 		strcat(s, "/trace");
405*d90bee97SLionel Sambuc 	}
406*d90bee97SLionel Sambuc #else
407*d90bee97SLionel Sambuc 	snprintf(s, sizeof(s), "./trace.%d", (int)getpid());
408*d90bee97SLionel Sambuc #endif /* not_this_way */
409*d90bee97SLionel Sambuc 	if (tracefile) {
410*d90bee97SLionel Sambuc 		if (!freopen(s, "a", tracefile)) {
411*d90bee97SLionel Sambuc 			fprintf(stderr, "Can't re-open %s\n", s);
412*d90bee97SLionel Sambuc 			tracefile = NULL;
413*d90bee97SLionel Sambuc 			debug = 0;
414*d90bee97SLionel Sambuc 			return;
415*d90bee97SLionel Sambuc 		}
416*d90bee97SLionel Sambuc 	} else {
417*d90bee97SLionel Sambuc 		if ((tracefile = fopen(s, "a")) == NULL) {
418*d90bee97SLionel Sambuc 			fprintf(stderr, "Can't open %s\n", s);
419*d90bee97SLionel Sambuc 			debug = 0;
420*d90bee97SLionel Sambuc 			return;
421*d90bee97SLionel Sambuc 		}
422*d90bee97SLionel Sambuc 	}
423*d90bee97SLionel Sambuc #ifdef O_APPEND
424*d90bee97SLionel Sambuc 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
425*d90bee97SLionel Sambuc 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
426*d90bee97SLionel Sambuc #endif
427*d90bee97SLionel Sambuc 	setlinebuf(tracefile);
428*d90bee97SLionel Sambuc 	fputs("\nTracing started.\n", tracefile);
429*d90bee97SLionel Sambuc }
430*d90bee97SLionel Sambuc #endif /* DEBUG */
431