Source-Changes-HG archive

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

[src/trunk]: src/bin/ps Provide symbolic printing of some keywords by capital...



details:   https://anonhg.NetBSD.org/src/rev/2b05cdf2d46a
branches:  trunk
changeset: 1023550:2b05cdf2d46a
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Sep 14 17:09:18 2021 +0000

description:
Provide symbolic printing of some keywords by capitalizing them.

diffstat:

 bin/ps/Makefile  |    6 +-
 bin/ps/keyword.c |   41 ++++++++++++------
 bin/ps/print.c   |  119 ++++++++++++++++++++++++++++++++++++++++++++----------
 bin/ps/ps.1      |    7 ++-
 bin/ps/ps.h      |    5 +-
 5 files changed, 133 insertions(+), 45 deletions(-)

diffs (truncated from 381 to 300 lines):

diff -r fa2ee01bb145 -r 2b05cdf2d46a bin/ps/Makefile
--- a/bin/ps/Makefile   Tue Sep 14 15:04:09 2021 +0000
+++ b/bin/ps/Makefile   Tue Sep 14 17:09:18 2021 +0000
@@ -1,10 +1,10 @@
-#      $NetBSD: Makefile,v 1.30 2021/04/06 04:49:41 simonb Exp $
+#      $NetBSD: Makefile,v 1.31 2021/09/14 17:09:18 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/2/93
 
 PROG=          ps
 SRCS=          fmt.c keyword.c nlist.c print.c ps.c
-DPADD=         ${LIBM} ${LIBKVM}
-LDADD=         -lm -lkvm
+DPADD=         ${LIBUTIL} ${LIBM} ${LIBKVM}
+LDADD=         -lutil -lm -lkvm
 
 COPTS.print.c = -Wno-format-nonliteral -Wno-format-y2k
 
diff -r fa2ee01bb145 -r 2b05cdf2d46a bin/ps/keyword.c
--- a/bin/ps/keyword.c  Tue Sep 14 15:04:09 2021 +0000
+++ b/bin/ps/keyword.c  Tue Sep 14 17:09:18 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: keyword.c,v 1.57 2019/08/06 18:07:51 kamil Exp $       */
+/*     $NetBSD: keyword.c,v 1.58 2021/09/14 17:09:18 christos Exp $    */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)keyword.c  8.5 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: keyword.c,v 1.57 2019/08/06 18:07:51 kamil Exp $");
+__RCSID("$NetBSD: keyword.c,v 1.58 2021/09/14 17:09:18 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -49,6 +49,7 @@
 #include <err.h>
 #include <errno.h>
 #include <kvm.h>
+#include <ctype.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -103,7 +104,7 @@
 VAR var[] = {
        VAR6("%cpu", "%CPU", 0, pcpu, 0, PCPU),
        VAR6("%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32),
-       PVAR("acflag", "ACFLG", 0, p_acflag, USHORT, "x"),
+       PVAR("acflag", "ACFLG", 0, p_acflag, PROCACFLAG, "x"),
        VAR3("acflg", "acflag", ALIAS),
        VAR3("args", "command", ALIAS),
        VAR3("blocked", "sigmask", ALIAS),
@@ -120,7 +121,7 @@
        VAR6("etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL),
        UID("euid", "EUID", p_uid),
        VAR4("euser", "EUSER", LJUST, usrname),
-       PVAR("f", "F", 0, p_flag, INT, "x"),
+       PVAR("f", "F", 0, p_flag, PROCFLAG, "x"),
        VAR3("flags", "f", ALIAS),
        GID("gid", "GID", p_gid),
        VAR4("group", "GROUP", LJUST, gname),
@@ -359,12 +360,16 @@
 {
        VAR *v;
        char *hp;
+       char pp[1024];
+       strlcpy(pp, p, sizeof(pp));
 
-       hp = strchr(p, '=');
+       hp = strchr(pp, '=');
        if (hp)
                *hp++ = '\0';
+       for (char *dp = pp; *dp; dp++)
+               *dp = tolower((unsigned char)*dp);
 
-       v = bsearch(p, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
+       v = bsearch(pp, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
        if (v && v->flag & ALIAS)
                v = findvar(v->header);
        if (!v) {
@@ -373,7 +378,17 @@
                return NULL;
        }
 
-       if (v && hp) {
+       if (!hp && *p == *pp)
+               return v;
+
+       struct var *newvar;
+
+       if ((newvar = malloc(sizeof(*newvar))) == NULL)
+               err(EXIT_FAILURE, NULL);
+       memcpy(newvar, v, sizeof(*newvar));
+       v = newvar;
+
+       if (hp) {
                /*
                 * Override the header.
                 *
@@ -382,16 +397,10 @@
                 * used multiple times with different headers.  We also
                 * need to strdup the header.
                 */
-               struct var *newvar;
                char *newheader;
-
-               if ((newvar = malloc(sizeof(struct var))) == NULL)
-                       err(EXIT_FAILURE, NULL);
                if ((newheader = strdup(hp)) == NULL)
                        err(EXIT_FAILURE, NULL);
-               memcpy(newvar, v, sizeof(struct var));
                newvar->header = newheader;
-
                /*
                 * According to P1003.1-2004, if the header text is null,
                 * such as -o user=, the field width will be at least as
@@ -399,9 +408,11 @@
                 */
                if (*hp == '\0')
                        newvar->width = strlen(v->header);
+       }
 
-               v = newvar;
-       }
+       if (*p != *pp)
+               newvar->flag |= ALTPR|LJUST;
+
        return v;
 }
 
diff -r fa2ee01bb145 -r 2b05cdf2d46a bin/ps/print.c
--- a/bin/ps/print.c    Tue Sep 14 15:04:09 2021 +0000
+++ b/bin/ps/print.c    Tue Sep 14 17:09:18 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: print.c,v 1.135 2021/04/17 08:35:33 maya Exp $ */
+/*     $NetBSD: print.c,v 1.136 2021/09/14 17:09:18 christos Exp $     */
 
 /*
  * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
@@ -63,18 +63,20 @@
 #if 0
 static char sccsid[] = "@(#)print.c    8.6 (Berkeley) 4/16/94";
 #else
-__RCSID("$NetBSD: print.c,v 1.135 2021/04/17 08:35:33 maya Exp $");
+__RCSID("$NetBSD: print.c,v 1.136 2021/09/14 17:09:18 christos Exp $");
 #endif
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <sys/sysctl.h>
 #include <sys/lwp.h>
 #include <sys/proc.h>
 #include <sys/stat.h>
 #include <sys/ucred.h>
 #include <sys/sysctl.h>
+#include <sys/acct.h>
 
 #include <err.h>
 #include <grp.h>
@@ -87,8 +89,10 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <util.h>
 #include <tzfile.h>
 #include <unistd.h>
+#include <signal.h>
 
 #include "ps.h"
 
@@ -1145,6 +1149,73 @@
        intprintorsetwidth(v, pgtok(k->p_vm_tsize), mode);
 }
 
+static void
+printsig(VAR *v, const sigset_t *s, enum mode mode)
+{
+#define        SIGSETSIZE      __arraycount(s->__bits)
+       if ((v->flag & ALTPR) == 0) {
+               char buf[SIGSETSIZE * 8 + 1];
+               size_t i;
+
+               for (i = 0; i < SIGSETSIZE; i++)
+                       (void)snprintf(&buf[i * 8], 9, "%.8x",
+                           s->__bits[(SIGSETSIZE - 1) - i]);
+
+               /* Skip leading zeroes */
+               for (i = 0; buf[i] == '0'; i++)
+                       continue;
+
+               if (buf[i] == '\0')
+                       i--;
+               strprintorsetwidth(v, buf + i, mode);
+       } else {
+               size_t maxlen = 1024, len = 0;
+               char *buf = malloc(maxlen);
+               if (buf == NULL)
+                       err(EXIT_FAILURE, NULL);
+               *buf = '\0';
+               for (size_t i = 0; i < SIGSETSIZE; i++) {
+                       uint32_t m = s->__bits[i];
+                       for (uint32_t j = 0; j < 32; j++) {
+                               if ((m & (1 << j)) == 0)
+                                       continue;
+                               const char *n = signalname(j + 1);
+                               size_t sn = strlen(n);
+                               if (len)
+                                       sn++;
+                               if (len + sn >= maxlen) {
+                                       maxlen += 1024;
+                                       buf = realloc(buf, maxlen);
+                                       if (buf == NULL)
+                                               err(EXIT_FAILURE, NULL);
+                               }
+                               snprintf(buf + len, sn + 1, "%s%s",
+                                   len == 0 ? "" : ",", n);
+                               len += sn;
+                       }
+               }
+               strprintorsetwidth(v, buf, mode);
+               free(buf);
+#undef SIGSETSIZE
+       }
+}
+
+static void
+printflag(VAR *v, int flag, enum mode mode)
+{
+       char buf[1024];
+       snprintb(buf, sizeof(buf), __SYSCTL_PROC_FLAG_BITS, flag);
+       strprintorsetwidth(v, buf, mode);
+}
+
+static void
+printacflag(VAR *v, int flag, enum mode mode)
+{
+       char buf[1024];
+       snprintb(buf, sizeof(buf), __ACCT_FLAG_BITS, flag);
+       strprintorsetwidth(v, buf, mode);
+}
+
 /*
  * Generic output routines.  Print fields from various prototype
  * structures.
@@ -1188,6 +1259,10 @@
                        val = GET(short);
                        vok = VSIGN;
                        break;
+               case PROCACFLAG:
+                       if (v->flag & ALTPR)
+                               break;
+                       /*FALLTHROUGH*/
                case USHORT:
                        uval = CHK_INF127(GET(u_short));
                        vok = VUNSIGN;
@@ -1196,6 +1271,10 @@
                        val = GET(int32_t);
                        vok = VSIGN;
                        break;
+               case PROCFLAG:
+                       if (v->flag & ALTPR)
+                               break;
+                       /*FALLTHROUGH*/
                case INT:
                        val = GET(int);
                        vok = VSIGN;
@@ -1286,9 +1365,21 @@
        case SHORT:
                (void)printf(ofmt, width, GET(short));
                return;
+       case PROCACFLAG:
+               if (v->flag & ALTPR) {
+                       printacflag(v, CHK_INF127(GET(u_short)), mode);
+                       return;
+               }
+               /*FALLTHROUGH*/
        case USHORT:
                (void)printf(ofmt, width, CHK_INF127(GET(u_short)));
                return;
+       case PROCFLAG:
+               if (v->flag & ALTPR) {
+                       printflag(v, GET(int), mode);
+                       return;
+               }
+               /*FALLTHROUGH*/
        case INT:
                (void)printf(ofmt, width, GET(int));
                return;
@@ -1313,33 +1404,15 @@
        case UINT32:
                (void)printf(ofmt, width, CHK_INF127(GET(u_int32_t)));
                return;
-       case SIGLIST:
-               {
-                       sigset_t *s = (sigset_t *)(void *)bp;
-                       size_t i;
-#define        SIGSETSIZE      (sizeof(s->__bits) / sizeof(s->__bits[0]))
-                       char buf[SIGSETSIZE * 8 + 1];



Home | Main Index | Thread Index | Old Index