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