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