Module Name:    src
Committed By:   christos
Date:           Tue Sep 14 17:09:18 UTC 2021

Modified Files:
        src/bin/ps: Makefile keyword.c print.c ps.1 ps.h

Log Message:
Provide symbolic printing of some keywords by capitalizing them.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/bin/ps/Makefile src/bin/ps/ps.h
cvs rdiff -u -r1.57 -r1.58 src/bin/ps/keyword.c
cvs rdiff -u -r1.135 -r1.136 src/bin/ps/print.c
cvs rdiff -u -r1.112 -r1.113 src/bin/ps/ps.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/ps/Makefile
diff -u src/bin/ps/Makefile:1.30 src/bin/ps/Makefile:1.31
--- src/bin/ps/Makefile:1.30	Tue Apr  6 00:49:41 2021
+++ src/bin/ps/Makefile	Tue Sep 14 13:09:18 2021
@@ -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
 
Index: src/bin/ps/ps.h
diff -u src/bin/ps/ps.h:1.30 src/bin/ps/ps.h:1.31
--- src/bin/ps/ps.h:1.30	Fri Sep 10 18:11:03 2021
+++ src/bin/ps/ps.h	Tue Sep 14 13:09:18 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: ps.h,v 1.30 2021/09/10 22:11:03 rillig Exp $	*/
+/*	$NetBSD: ps.h,v 1.31 2021/09/14 17:09:18 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993
@@ -44,7 +44,7 @@ enum type {
 	UNSPECIFIED,
 	CHAR, UCHAR, SHORT, USHORT, INT, UINT, LONG, ULONG,
 	KPTR, KPTR24, INT32, UINT32, SIGLIST, INT64, UINT64,
-	TIMEVAL, CPUTIME, PCPU, VSIZE
+	TIMEVAL, CPUTIME, PCPU, VSIZE, PROCFLAG, PROCACFLAG
 };
 
 /* Variables. */
@@ -73,6 +73,7 @@ typedef struct var {
 #define	LWP	0x10		/* dispatch to kinfo_lwp routine */
 #define	UAREA	0x20		/* need to check p_uvalid */
 #define	ALIAS	0x40		/* entry is alias for 'header' */
+#define	ALTPR	0x80		/* use alternate printing method */
 	u_int	flag;
 				/* output routine */
 	void	(*oproc)(struct pinfo *pi, struct varent *, enum mode);

Index: src/bin/ps/keyword.c
diff -u src/bin/ps/keyword.c:1.57 src/bin/ps/keyword.c:1.58
--- src/bin/ps/keyword.c:1.57	Tue Aug  6 14:07:51 2019
+++ src/bin/ps/keyword.c	Tue Sep 14 13:09:18 2021
@@ -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 @@ __RCSID("$NetBSD: keyword.c,v 1.57 2019/
 #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 @@ static int  vcmp(const void *, const voi
 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 @@ VAR var[] = {
 	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 @@ findvar(const char *p)
 {
 	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 @@ findvar(const char *p)
 		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 @@ findvar(const char *p)
 		 * 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 @@ findvar(const char *p)
 		 */
 		if (*hp == '\0')
 			newvar->width = strlen(v->header);
-
-		v = newvar;
 	}
+
+	if (*p != *pp)
+		newvar->flag |= ALTPR|LJUST;
+
 	return v;
 }
 

Index: src/bin/ps/print.c
diff -u src/bin/ps/print.c:1.135 src/bin/ps/print.c:1.136
--- src/bin/ps/print.c:1.135	Sat Apr 17 04:35:33 2021
+++ src/bin/ps/print.c	Tue Sep 14 13:09:18 2021
@@ -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 @@ __RCSID("$NetBSD: print.c,v 1.135 2021/0
 #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 @@ tsize(struct pinfo *pi, VARENT *ve, enum
 	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 @@ printval(void *bp, VAR *v, enum mode mod
 			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 @@ printval(void *bp, VAR *v, enum mode mod
 			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 @@ printval(void *bp, VAR *v, enum mode mod
 	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 @@ printval(void *bp, VAR *v, enum mode mod
 	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];
-
-			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);
-#undef SIGSETSIZE
-		}
-		return;
 	case INT64:
 		(void)printf(ofmt, width, GET(int64_t));
 		return;
 	case UINT64:
 		(void)printf(ofmt, width, CHK_INF127(GET(u_int64_t)));
 		return;
+	case SIGLIST:
+		printsig(v, (const sigset_t *)(void *)bp, mode);
+		return;
 	default:
 		errx(EXIT_FAILURE, "unknown type %d", v->type);
 	}

Index: src/bin/ps/ps.1
diff -u src/bin/ps/ps.1:1.112 src/bin/ps/ps.1:1.113
--- src/bin/ps/ps.1:1.112	Fri Jun  4 04:16:14 2021
+++ src/bin/ps/ps.1	Tue Sep 14 13:09:18 2021
@@ -1,4 +1,4 @@
-.\"	$NetBSD: ps.1,v 1.112 2021/06/04 08:16:14 wiz Exp $
+.\"	$NetBSD: ps.1,v 1.113 2021/09/14 17:09:18 christos Exp $
 .\"
 .\" Copyright (c) 1980, 1990, 1991, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)ps.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd August 6, 2019
+.Dd September 14, 2021
 .Dt PS 1
 .Os
 .Sh NAME
@@ -205,6 +205,9 @@ options.
 If all the keywords to be displayed have customised headers,
 and all the customised headers are entirely empty,
 then the header line is not printed at all.
+.Pp
+If the keyword is capitalized, then an alternate (symbolic) form of it
+is printed, if available.
 .It Fl p Ar pid
 Display information associated with the specified process ID.
 .It Fl r

Reply via email to