git: 07a7557addc0 - main - ps(1): Have parsefmt() take the list of columns to update

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Mon, 28 Apr 2025 12:23:22 UTC
The branch main has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=07a7557addc03acd28aaae55b90419c4ef9d9ad1

commit 07a7557addc03acd28aaae55b90419c4ef9d9ad1
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-02-27 17:15:14 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-04-28 11:56:20 +0000

    ps(1): Have parsefmt() take the list of columns to update
    
    This is in preparation for changing the behavior of the '-O' option.
    
    While here, reformat the definition of 'struct varent', fix formatting
    of that of 'struct var' and expand slightly their herald comments.
    
    More reformatting/commenting in 'ps.h'.
    
    No functional change intended.
    
    Reviewed by:    kib
    MFC after:      3 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D49607
---
 bin/ps/extern.h  |  4 ++--
 bin/ps/keyword.c | 17 +++++++++--------
 bin/ps/ps.c      | 20 ++++++++++----------
 bin/ps/ps.h      | 12 +++++++-----
 4 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/bin/ps/extern.h b/bin/ps/extern.h
index 9d4630adfdd6..48c452aeb844 100644
--- a/bin/ps/extern.h
+++ b/bin/ps/extern.h
@@ -39,7 +39,7 @@ extern int cflag, eval, fscale, nlistread, rawcpu;
 extern unsigned long mempages;
 extern time_t now;
 extern int showthreads, sumrusage, termwidth;
-extern STAILQ_HEAD(velisthead, varent) varlist;
+extern struct velisthead varlist;
 
 __BEGIN_DECLS
 char	 *arguments(KINFO *, VARENT *);
@@ -65,7 +65,7 @@ char	 *lockname(KINFO *, VARENT *);
 char	 *mwchan(KINFO *, VARENT *);
 char	 *nwchan(KINFO *, VARENT *);
 char	 *pagein(KINFO *, VARENT *);
-void	 parsefmt(const char *, int);
+void	 parsefmt(const char *, struct velisthead *, int);
 char	 *pcpu(KINFO *, VARENT *);
 char	 *pmem(KINFO *, VARENT *);
 char	 *pri(KINFO *, VARENT *);
diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c
index 0c8edb06b562..72fe9e183aac 100644
--- a/bin/ps/keyword.c
+++ b/bin/ps/keyword.c
@@ -44,7 +44,7 @@
 
 #include "ps.h"
 
-static VAR *findvar(char *, int, char **header);
+static VAR *findvar(char *, struct velisthead *, int, char **header);
 static int  vcmp(const void *, const void *);
 
 /* Compute offset in common structures. */
@@ -254,7 +254,8 @@ showkey(void)
 }
 
 void
-parsefmt(const char *p, int user)
+parsefmt(const char *p, struct velisthead *const var_list,
+    const int user)
 {
 	char *tempstr, *tempstr1;
 
@@ -278,7 +279,7 @@ parsefmt(const char *p, int user)
 			cp = tempstr;
 			tempstr = NULL;
 		}
-		if (cp == NULL || !(v = findvar(cp, user, &hp)))
+		if (cp == NULL || !(v = findvar(cp, var_list, user, &hp)))
 			continue;
 		if (!user) {
 			/*
@@ -302,10 +303,10 @@ parsefmt(const char *p, int user)
 		if (vent->var == NULL)
 			xo_errx(1, "malloc failed");
 		memcpy(vent->var, v, sizeof(*vent->var));
-		STAILQ_INSERT_TAIL(&varlist, vent, next_ve);
+		STAILQ_INSERT_TAIL(var_list, vent, next_ve);
 	}
 	free(tempstr1);
-	if (STAILQ_EMPTY(&varlist)) {
+	if (STAILQ_EMPTY(var_list)) {
 		xo_warnx("no valid keywords; valid keywords:");
 		showkey();
 		exit(1);
@@ -313,7 +314,7 @@ parsefmt(const char *p, int user)
 }
 
 static VAR *
-findvar(char *p, int user, char **header)
+findvar(char *p, struct velisthead *const var_list, int user, char **header)
 {
 	size_t rflen;
 	VAR *v, key;
@@ -334,7 +335,7 @@ findvar(char *p, int user, char **header)
 		 * process the alias.
 		 */
 		if (hp == NULL)
-			parsefmt(v->alias, user);
+			parsefmt(v->alias, var_list, user);
 		else {
 			/*
 			 * XXX - This processing will not be correct for
@@ -347,7 +348,7 @@ findvar(char *p, int user, char **header)
 			if (realfmt == NULL)
 				xo_errx(1, "malloc failed");
 			snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
-			parsefmt(realfmt, user);
+			parsefmt(realfmt, var_list, user);
 			free(realfmt);
 		}
 		return ((VAR *)NULL);
diff --git a/bin/ps/ps.c b/bin/ps/ps.c
index 15d4d70eebf5..a5ae43b7fad1 100644
--- a/bin/ps/ps.c
+++ b/bin/ps/ps.c
@@ -293,7 +293,7 @@ main(int argc, char *argv[])
 			nselectors++;
 			break;
 		case 'j':
-			parsefmt(jfmt, 0);
+			parsefmt(jfmt, &varlist, 0);
 			_fmt = 1;
 			jfmt[0] = '\0';
 			break;
@@ -301,7 +301,7 @@ main(int argc, char *argv[])
 			showkey();
 			exit(0);
 		case 'l':
-			parsefmt(lfmt, 0);
+			parsefmt(lfmt, &varlist, 0);
 			_fmt = 1;
 			lfmt[0] = '\0';
 			break;
@@ -315,14 +315,14 @@ main(int argc, char *argv[])
 			nlistf = optarg;
 			break;
 		case 'O':
-			parsefmt(o1, 1);
-			parsefmt(optarg, 1);
-			parsefmt(o2, 1);
+			parsefmt(o1, &varlist, 1);
+			parsefmt(optarg, &varlist, 1);
+			parsefmt(o2, &varlist, 1);
 			o1[0] = o2[0] = '\0';
 			_fmt = 1;
 			break;
 		case 'o':
-			parsefmt(optarg, 1);
+			parsefmt(optarg, &varlist, 1);
 			_fmt = 1;
 			break;
 		case 'p':
@@ -384,13 +384,13 @@ main(int argc, char *argv[])
 			nselectors++;
 			break;
 		case 'u':
-			parsefmt(ufmt, 0);
+			parsefmt(ufmt, &varlist, 0);
 			sortby = SORTCPU;
 			_fmt = 1;
 			ufmt[0] = '\0';
 			break;
 		case 'v':
-			parsefmt(vfmt, 0);
+			parsefmt(vfmt, &varlist, 0);
 			sortby = SORTMEM;
 			_fmt = 1;
 			vfmt[0] = '\0';
@@ -420,7 +420,7 @@ main(int argc, char *argv[])
 			xkeep = 1;
 			break;
 		case 'Z':
-			parsefmt(Zfmt, 0);
+			parsefmt(Zfmt, &varlist, 0);
 			Zfmt[0] = '\0';
 			break;
 		case '?':
@@ -454,7 +454,7 @@ main(int argc, char *argv[])
 		xo_errx(1, "%s", errbuf);
 
 	if (!_fmt)
-		parsefmt(dfmt, 0);
+		parsefmt(dfmt, &varlist, 0);
 
 	if (!all && nselectors == 0) {
 		uidlist.l.ptr = malloc(sizeof(uid_t));
diff --git a/bin/ps/ps.h b/bin/ps/ps.h
index 521027427036..c5efb19fc00d 100644
--- a/bin/ps/ps.h
+++ b/bin/ps/ps.h
@@ -53,13 +53,15 @@ typedef struct kinfo {
 	STAILQ_HEAD(, kinfo_str) ki_ks;
 } KINFO;
 
-/* Variables. */
+/* Keywords/variables to be printed. */
 typedef struct varent {
-	STAILQ_ENTRY(varent) next_ve;
-	const char *header;
-	struct var *var;
+	STAILQ_ENTRY(varent)	 next_ve;
+	const char		*header;
+	struct var		*var;
 } VARENT;
+STAILQ_HEAD(velisthead, varent);
 
+/* Structure representing one available keyword. */
 typedef struct var {
 	const char *name;	/* name(s) of variable */
 	const char *header;	/* default header */
@@ -71,7 +73,7 @@ typedef struct var {
 #define	INF127	0x10		/* values >127 displayed as 127 */
 	u_int	flag;
 				/* output routine */
-	char 	*(*oproc)(struct kinfo *, struct varent *);
+	char	*(*oproc)(struct kinfo *, struct varent *);
 	/*
 	 * The following (optional) elements are hooks for passing information
 	 * to the generic output routine pvar (which prints simple elements