ps(1): Consider references to keywords as immutable

Building on the previous commit that moved the 'width' field from VAR
(structure representing the meaning of a keyword) to VARENT (structure
representing an actual column in a display), it is now possible to
declare the field 'var' of VARENT as a constant reference.

At this point, it could be possible to constify the var[] array
containing all the recognized keywords.  However, we do not do it as
a subsequent commit will change the way alias keywords are resolved,
causing their fields to be modified to reflect the final values to use
after alias resolution.

In parsefmt(), forget about allocating a new VAR for the selected
keyword to be pointed by the corresponding column (VARENT), and instead
just keep a pointer to that structure in var[].

MFC after:      3 days
Sponsored by:   The FreeBSD Foundation
Differential Revision:  https://reviews.freebsd.org/D49611

(cherry picked from commit 1dbb1cca9b)
This commit is contained in:
Olivier Certner 2025-02-28 22:07:14 +01:00
parent b1722013af
commit 21d9f3d9f0
No known key found for this signature in database
GPG key ID: 8CA13040971E2627
4 changed files with 8 additions and 11 deletions

View file

@ -305,10 +305,7 @@ parsefmt(const char *p, struct velisthead *const var_list,
vent->header = hp;
}
vent->width = strlen(vent->header);
vent->var = malloc(sizeof(*vent->var));
if (vent->var == NULL)
xo_errx(1, "malloc failed");
memcpy(vent->var, v, sizeof(*vent->var));
vent->var = v;
STAILQ_INSERT_TAIL(var_list, vent, next_ve);
}
free(tempstr1);

View file

@ -73,7 +73,7 @@ static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94";
void
printheader(void)
{
VAR *v;
const VAR *v;
struct varent *vent;
STAILQ_FOREACH(vent, &varlist, next_ve)
@ -745,7 +745,7 @@ priorityr(KINFO *k, VARENT *ve __unused)
* structures.
*/
static char *
printval(void *bp, VAR *v)
printval(void *bp, const VAR *v)
{
static char ofmt[32] = "%";
const char *fcp;
@ -796,7 +796,7 @@ printval(void *bp, VAR *v)
char *
kvar(KINFO *k, VARENT *ve)
{
VAR *v;
const VAR *v;
v = ve->var;
return (printval((char *)((char *)k->ki_p + v->off), v));
@ -805,7 +805,7 @@ kvar(KINFO *k, VARENT *ve)
char *
rvar(KINFO *k, VARENT *ve)
{
VAR *v;
const VAR *v;
v = ve->var;
if (!k->ki_valid)

View file

@ -1217,7 +1217,7 @@ static void
scanvars(void)
{
struct varent *vent;
VAR *v;
const VAR *v;
STAILQ_FOREACH(vent, &varlist, next_ve) {
v = vent->var;
@ -1232,7 +1232,7 @@ static void
format_output(KINFO *ki)
{
struct varent *vent;
VAR *v;
const VAR *v;
KINFO_STR *ks;
char *str;
u_int len;

View file

@ -59,7 +59,7 @@ typedef struct kinfo {
typedef struct varent {
STAILQ_ENTRY(varent) next_ve;
const char *header;
struct var *var;
const struct var *var;
u_int width;
} VARENT;
STAILQ_HEAD(velisthead, varent);