BSD 4_4_Lite2 release
[unix-history] / usr / src / bin / sh / show.c
index cd83791..4319809 100644 (file)
@@ -1,45 +1,87 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
  *
  *
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)show.c     5.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)show.c     8.3 (Berkeley) 5/4/95";
 #endif /* not lint */
 
 #include <stdio.h>
 #endif /* not lint */
 
 #include <stdio.h>
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
 #include "shell.h"
 #include "parser.h"
 #include "nodes.h"
 #include "mystring.h"
 #include "shell.h"
 #include "parser.h"
 #include "nodes.h"
 #include "mystring.h"
+#include "show.h"
 
 
 #ifdef DEBUG
 
 
 #ifdef DEBUG
-static shtree(), shcmd(), sharg(), indent();
+static void shtree __P((union node *, int, char *, FILE*));
+static void shcmd __P((union node *, FILE *));
+static void sharg __P((union node *, FILE *));
+static void indent __P((int, char *, FILE *));
+static void trstring __P((char *));
 
 
 
 
+void
 showtree(n)
        union node *n;
 showtree(n)
        union node *n;
-       {
+{
        trputs("showtree called\n");
        shtree(n, 1, NULL, stdout);
 }
 
 
        trputs("showtree called\n");
        shtree(n, 1, NULL, stdout);
 }
 
 
-static
+static void
 shtree(n, ind, pfx, fp)
        union node *n;
 shtree(n, ind, pfx, fp)
        union node *n;
+       int ind;
        char *pfx;
        FILE *fp;
        char *pfx;
        FILE *fp;
-       {
+{
        struct nodelist *lp;
        char *s;
 
        struct nodelist *lp;
        char *s;
 
+       if (n == NULL)
+               return;
+
        indent(ind, pfx, fp);
        switch(n->type) {
        case NSEMI:
        indent(ind, pfx, fp);
        switch(n->type) {
        case NSEMI:
@@ -82,11 +124,11 @@ binop:
 
 
 
 
 
 
-static
+static void
 shcmd(cmd, fp)
        union node *cmd;
        FILE *fp;
 shcmd(cmd, fp)
        union node *cmd;
        FILE *fp;
-       {
+{
        union node *np;
        int first;
        char *s;
        union node *np;
        int first;
        char *s;
@@ -108,6 +150,7 @@ shcmd(cmd, fp)
                        case NTOFD:     s = ">&"; dftfd = 1; break;
                        case NFROM:     s = "<";  dftfd = 0; break;
                        case NFROMFD:   s = "<&"; dftfd = 0; break;
                        case NTOFD:     s = ">&"; dftfd = 1; break;
                        case NFROM:     s = "<";  dftfd = 0; break;
                        case NFROMFD:   s = "<&"; dftfd = 0; break;
+                       default:        s = "*error*"; dftfd = 0; break;
                }
                if (np->nfile.fd != dftfd)
                        fprintf(fp, "%d", np->nfile.fd);
                }
                if (np->nfile.fd != dftfd)
                        fprintf(fp, "%d", np->nfile.fd);
@@ -123,7 +166,7 @@ shcmd(cmd, fp)
 
 
 
 
 
 
-static
+static void
 sharg(arg, fp)
        union node *arg;
        FILE *fp;
 sharg(arg, fp)
        union node *arg;
        FILE *fp;
@@ -147,10 +190,15 @@ sharg(arg, fp)
                        putc('$', fp);
                        putc('{', fp);
                        subtype = *++p;
                        putc('$', fp);
                        putc('{', fp);
                        subtype = *++p;
+                       if (subtype == VSLENGTH)
+                               putc('#', fp);
+
                        while (*p != '=')
                                putc(*p++, fp);
                        while (*p != '=')
                                putc(*p++, fp);
+
                        if (subtype & VSNUL)
                                putc(':', fp);
                        if (subtype & VSNUL)
                                putc(':', fp);
+
                        switch (subtype & VSTYPE) {
                        case VSNORMAL:
                                putc('}', fp);
                        switch (subtype & VSTYPE) {
                        case VSNORMAL:
                                putc('}', fp);
@@ -167,6 +215,22 @@ sharg(arg, fp)
                        case VSASSIGN:
                                putc('=', fp);
                                break;
                        case VSASSIGN:
                                putc('=', fp);
                                break;
+                       case VSTRIMLEFT:
+                               putc('#', fp);
+                               break;
+                       case VSTRIMLEFTMAX:
+                               putc('#', fp);
+                               putc('#', fp);
+                               break;
+                       case VSTRIMRIGHT:
+                               putc('%', fp);
+                               break;
+                       case VSTRIMRIGHTMAX:
+                               putc('%', fp);
+                               putc('%', fp);
+                               break;
+                       case VSLENGTH:
+                               break;
                        default:
                                printf("<subtype %d>", subtype);
                        }
                        default:
                                printf("<subtype %d>", subtype);
                        }
@@ -189,11 +253,12 @@ sharg(arg, fp)
 }
 
 
 }
 
 
-static
+static void
 indent(amount, pfx, fp)
 indent(amount, pfx, fp)
+       int amount;
        char *pfx;
        FILE *fp;
        char *pfx;
        FILE *fp;
-       {
+{
        int i;
 
        for (i = 0 ; i < amount ; i++) {
        int i;
 
        for (i = 0 ; i < amount ; i++) {
@@ -220,7 +285,10 @@ int debug = 0;
 #endif
 
 
 #endif
 
 
-trputc(c) {
+void
+trputc(c) 
+       int c;
+{
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
@@ -230,23 +298,37 @@ trputc(c) {
 #endif
 }
 
 #endif
 }
 
-
-trace(fmt, a1, a2, a3, a4, a5, a6, a7, a8)
-       char *fmt;
-       {
+void
+#if __STDC__
+trace(const char *fmt, ...)
+#else
+trace(va_alist)
+       va_dcl
+#endif
+{
 #ifdef DEBUG
 #ifdef DEBUG
-       if (tracefile == NULL)
-               return;
-       fprintf(tracefile, fmt, a1, a2, a3, a4, a5, a6, a7, a8);
-       if (strchr(fmt, '\n'))
-               fflush(tracefile);
+       va_list va;
+#if __STDC__
+       va_start(va, fmt);
+#else
+       char *fmt;
+       va_start(va);
+       fmt = va_arg(va, char *);
+#endif
+       if (tracefile != NULL) {
+               (void) vfprintf(tracefile, fmt, va);
+               if (strchr(fmt, '\n'))
+                       (void) fflush(tracefile);
+       }
+       va_end(va);
 #endif
 }
 
 
 #endif
 }
 
 
+void
 trputs(s)
        char *s;
 trputs(s)
        char *s;
-       {
+{
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
@@ -257,9 +339,10 @@ trputs(s)
 }
 
 
 }
 
 
+static void
 trstring(s)
        char *s;
 trstring(s)
        char *s;
-       {
+{
        register char *p;
        char c;
 
        register char *p;
        char c;
 
@@ -299,9 +382,10 @@ backslash:   putc('\\', tracefile);
 }
 
 
 }
 
 
+void
 trargs(ap)
        char **ap;
 trargs(ap)
        char **ap;
-       {
+{
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
 #ifdef DEBUG
        if (tracefile == NULL)
                return;
@@ -317,23 +401,32 @@ trargs(ap)
 }
 
 
 }
 
 
+void
 opentrace() {
        char s[100];
 opentrace() {
        char s[100];
-       char *p;
        char *getenv();
        char *getenv();
+#ifdef O_APPEND
        int flags;
        int flags;
+#endif
 
 #ifdef DEBUG
        if (!debug)
                return;
 
 #ifdef DEBUG
        if (!debug)
                return;
-       if ((p = getenv("HOME")) == NULL) {
-               if (getuid() == 0)
-                       p = "/";
-               else
-                       p = "/tmp";
+#ifdef not_this_way
+       {
+               char *p;
+               if ((p = getenv("HOME")) == NULL) {
+                       if (geteuid() == 0)
+                               p = "/";
+                       else
+                               p = "/tmp";
+               }
+               scopy(p, s);
+               strcat(s, "/trace");
        }
        }
-       scopy(p, s);
-       strcat(s, "/trace");
+#else
+       scopy("./trace", s);
+#endif /* not_this_way */
        if ((tracefile = fopen(s, "a")) == NULL) {
                fprintf(stderr, "Can't open %s\n", s);
                return;
        if ((tracefile = fopen(s, "a")) == NULL) {
                fprintf(stderr, "Can't open %s\n", s);
                return;
@@ -344,5 +437,5 @@ opentrace() {
 #endif
        fputs("\nTracing started.\n", tracefile);
        fflush(tracefile);
 #endif
        fputs("\nTracing started.\n", tracefile);
        fflush(tracefile);
-#endif
+#endif /* DEBUG */
 }
 }