Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/bin/sh Pull up revisions 1.25-1.26 (requested by jonb):



details:   https://anonhg.NetBSD.org/src/rev/dbb0afeefc19
branches:  netbsd-1-5
changeset: 492815:dbb0afeefc19
user:      he <he%NetBSD.org@localhost>
date:      Sat Feb 23 15:53:50 2002 +0000

description:
Pull up revisions 1.25-1.26 (requested by jonb):
  Extend functionality of the trap builtin, which now more closely
  follows POSIX recommendations:
   o accept signal names as well as signal numbers
   o add ``-l'' option which outputs list of valid signals
   o add signal EXIT to list of valid signals
   o an ``-'' in the action part will reset signal to default behaviour
   o changed standard output of ``trap'' to make it suitable as
     subsequent input
  Also various cleanups of redundant declarations and nested externs.

diffstat:

 bin/sh/trap.c |  93 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 83 insertions(+), 10 deletions(-)

diffs (139 lines):

diff -r 297b6a97fcf1 -r dbb0afeefc19 bin/sh/trap.c
--- a/bin/sh/trap.c     Thu Feb 21 00:24:15 2002 +0000
+++ b/bin/sh/trap.c     Sat Feb 23 15:53:50 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: trap.c,v 1.24.4.2 2001/03/18 03:20:11 wulf Exp $       */
+/*     $NetBSD: trap.c,v 1.24.4.3 2002/02/23 15:53:50 he Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "@(#)trap.c     8.5 (Berkeley) 6/5/95";
 #else
-__RCSID("$NetBSD: trap.c,v 1.24.4.2 2001/03/18 03:20:11 wulf Exp $");
+__RCSID("$NetBSD: trap.c,v 1.24.4.3 2002/02/23 15:53:50 he Exp $");
 #endif
 #endif /* not lint */
 
@@ -77,14 +77,59 @@
 #define S_RESET 5              /* temporary - to reset a hard ignored sig */
 
 
-extern char nullstr[1];                /* null string */
-
 char *trap[NSIG+1];            /* trap handler commands */
 MKINIT char sigmode[NSIG];     /* current value of signal */
 char gotsig[NSIG];             /* indicates specified signal received */
 int pendingsigs;                       /* indicates some signal received */
 
 static int getsigaction __P((int, sig_t *));
+static int signame_to_signum __P((const char *));
+void printsignals __P((void));
+
+/*
+ * return the signal number described by `p' (as a number or a name)
+ * or -1 if it isn't one
+ */
+
+static int
+signame_to_signum(p)
+       const char *p;
+{
+       int i;
+
+       if (is_number(p))
+               return number(p);
+
+       if (strcasecmp(p, "exit") == 0 )
+               return 0;
+       
+       if (strncasecmp(p, "sig", 3) == 0)
+               p += 3;
+
+       for (i = 0; i < NSIG; ++i)
+               if (strcasecmp (p, sys_signame[i]) == 0)
+                       return i;
+       return -1;
+}
+
+/*
+ * Print a list of valid signal names
+ */
+void
+printsignals(void)
+{
+       int n;
+
+       out1str("EXIT ");
+
+       for (n = 1; n < NSIG; n++) {
+               out1fmt("%s", sys_signame[n]);
+               if ((n == NSIG/2) ||  n == (NSIG - 1))
+                       out1str("\n");
+               else
+                       out1c(' ');
+       }
+}
 
 /*
  * The trap builtin.
@@ -102,24 +147,52 @@
        if (argc <= 1) {
                for (signo = 0 ; signo <= NSIG ; signo++) {
                        if (trap[signo] != NULL)
-                               out1fmt("%d: %s\n", signo, trap[signo]);
+                               out1fmt("trap -- '%s' %s\n", trap[signo],
+                                   (signo) ? sys_signame[signo] : "EXIT");
                }
                return 0;
        }
        ap = argv + 1;
-       if (is_number(*ap))
-               action = NULL;
-       else
-               action = *ap++;
+
+       action = NULL;
+
+       if (strcmp(*ap, "--") == 0)
+               if (*++ap == NULL)
+                       return 0;
+
+       if (signame_to_signum(*ap) == -1) {
+               if ((*ap)[0] =='-') {
+                       if ((*ap)[1] == NULL)
+                               ap++;
+                       else if ((*ap)[1] == 'l' && (*ap)[2] == NULL) {
+                               printsignals();
+                               return 0;
+                       }
+                       else
+                               error("bad option %s\n", *ap);
+               }
+               else
+                       action = *ap++;
+       }
+
        while (*ap) {
-               if ((signo = number(*ap)) < 0 || signo > NSIG)
+               if (is_number(*ap))
+                       signo = number(*ap);
+               else
+                       signo = signame_to_signum(*ap);
+
+               if (signo < 0 || signo > NSIG)
                        error("%s: bad trap", *ap);
+
                INTOFF;
                if (action)
                        action = savestr(action);
+
                if (trap[signo])
                        ckfree(trap[signo]);
+
                trap[signo] = action;
+
                if (signo != 0)
                        setsignal(signo);
                INTON;



Home | Main Index | Thread Index | Old Index