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