Source-Changes-HG archive

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

[src/trunk]: src/bin Reverse a decision made when the printsignals() routines...



details:   https://anonhg.NetBSD.org/src/rev/4baa8c132108
branches:  trunk
changeset: 446627:4baa8c132108
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Dec 12 20:22:43 2018 +0000

description:
Reverse a decision made when the printsignals() routines from
kill and sh were merged so that the shell (for trap -l) and
kill (for kill -l) can use the same routine, and site that function
in the shell, rather than in kill (use the code that is in kill as
the basis for that routine).   This allows access to sh internals,
and in particular to the posix option, so the builtin kill can
operate in posix mode where the standard requires just a single
character (space of newline) between successive signal names (and
we prefer nicely aligned columns instead)..

In a SMALL shell, use the ancient sh printsignals routine instead,
it is smaller (and very much dumber).

/bin/kill still uses the routine that is in its source, and is
not posix compliant.   A task for some other day...

diffstat:

 bin/kill/kill.c |   6 +++-
 bin/sh/sh.1     |   9 +++++-
 bin/sh/trap.c   |  67 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 74 insertions(+), 8 deletions(-)

diffs (164 lines):

diff -r bde932ae241c -r 4baa8c132108 bin/kill/kill.c
--- a/bin/kill/kill.c   Wed Dec 12 19:05:32 2018 +0000
+++ b/bin/kill/kill.c   Wed Dec 12 20:22:43 2018 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: kill.c,v 1.29 2018/10/28 18:26:52 kre Exp $ */
+/* $NetBSD: kill.c,v 1.30 2018/12/12 20:22:43 kre Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)kill.c     8.4 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: kill.c,v 1.29 2018/10/28 18:26:52 kre Exp $");
+__RCSID("$NetBSD: kill.c,v 1.30 2018/12/12 20:22:43 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -254,6 +254,7 @@
        /* NOTREACHED */
 }
 
+#ifndef SHELL
 /*
  * Print the names of all the signals (neatly) to fp
  * "len" gives the number of chars already printed to
@@ -302,6 +303,7 @@
        if (len != 0)
                fprintf(fp, "\n");
 }
+#endif
 
 static void
 usage(void)
diff -r bde932ae241c -r 4baa8c132108 bin/sh/sh.1
--- a/bin/sh/sh.1       Wed Dec 12 19:05:32 2018 +0000
+++ b/bin/sh/sh.1       Wed Dec 12 20:22:43 2018 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sh.1,v 1.215 2018/12/12 12:56:17 kre Exp $
+.\"    $NetBSD: sh.1,v 1.216 2018/12/12 20:22:43 kre Exp $
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
 .\"
@@ -530,11 +530,16 @@
 other than in assignment statements
 .Dq ( no
 in posix mode),
+the format of the output of the
+.Ic kill Fl l
+command, where posix mode causes the names of the signals
+be separated by either a single space or newline, and where otherwise
+sufficient spaces are inserted to generate nice looking columns,
 and whether the shell treats
 an empty brace-list compound statement as a syntax error
 (expected by POSIX) or permits it.
 Such statements
-.Dq "{ }"
+.Dq "{\ }"
 can be useful when defining dummy functions.
 Lastly, in posix mode, only one
 .Dq \&!
diff -r bde932ae241c -r 4baa8c132108 bin/sh/trap.c
--- a/bin/sh/trap.c     Wed Dec 12 19:05:32 2018 +0000
+++ b/bin/sh/trap.c     Wed Dec 12 20:22:43 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: trap.c,v 1.49 2018/12/05 22:25:38 kre Exp $    */
+/*     $NetBSD: trap.c,v 1.50 2018/12/12 20:22:43 kre Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)trap.c     8.5 (Berkeley) 6/5/95";
 #else
-__RCSID("$NetBSD: trap.c,v 1.49 2018/12/05 22:25:38 kre Exp $");
+__RCSID("$NetBSD: trap.c,v 1.50 2018/12/12 20:22:43 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -46,7 +46,11 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <errno.h>
+#include <termios.h>
 
+#undef CEOF    /* from <termios.h> but concflicts with sh use */
+
+#include <sys/ioctl.h>
 #include <sys/resource.h>
 
 #include "shell.h"
@@ -135,7 +139,7 @@
        return nbuf;
 }
 
-#if 0          /* Share the version of this in src/bin/kill/kill.c */
+#ifdef SMALL
 /*
  * Print a list of valid signal names
  */
@@ -154,7 +158,62 @@
                        outc(' ', out);
        }
 }
-#endif
+#else /* !SMALL */
+/*
+ * Print the names of all the signals (neatly) to fp
+ * "len" gives the number of chars already printed to
+ * the current output line (in kill.c, always 0)
+ */
+void
+printsignals(struct output *out, int len)
+{
+       int sig;
+       int nl, pad;
+       const char *name;
+       int termwidth = 80;
+
+       if ((name = bltinlookup("COLUMNS", 1)) != NULL)
+               termwidth = (int)strtol(name, NULL, 10);
+       else if (isatty(1)) {
+               struct winsize win;
+
+               if (ioctl(1, TIOCGWINSZ, &win) == 0 && win.ws_col > 0)
+                       termwidth = win.ws_col;
+       }
+
+       if (posix)
+               pad = 1;
+       else
+               pad = (len | 7) + 1 - len;
+
+       for (sig = 0; (sig = signalnext(sig)) != 0; ) {
+               name = signalname(sig);
+               if (name == NULL)
+                       continue;
+
+               nl = strlen(name);
+
+               if (len > 0 && nl + len + pad >= termwidth) {
+                       outc('\n', out);
+                       len = 0;
+                       pad = 0;
+               } else if (pad > 0 && len != 0)
+                       outfmt(out, "%*s", pad, "");
+               else
+                       pad = 0;
+
+               len += nl + pad;
+               if (!posix)
+                       pad = (nl | 7) + 1 - nl;
+               else
+                       pad = 1;
+
+               outstr(name, out);
+       }
+       if (len != 0)
+               outc('\n', out);
+}
+#endif /* SMALL */
 
 /*
  * The trap builtin.



Home | Main Index | Thread Index | Old Index