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