Home | History | Annotate | Line # | Download | only in ps
      1  1.60  christos /*	$NetBSD: keyword.c,v 1.60 2021/09/15 13:16:57 christos Exp $	*/
      2   1.8       cgd 
      3   1.1       cgd /*-
      4   1.7       cgd  * Copyright (c) 1990, 1993, 1994
      5   1.7       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.35       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.15  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.8       cgd #if 0
     35   1.7       cgd static char sccsid[] = "@(#)keyword.c	8.5 (Berkeley) 4/2/94";
     36   1.8       cgd #else
     37  1.60  christos __RCSID("$NetBSD: keyword.c,v 1.60 2021/09/15 13:16:57 christos Exp $");
     38   1.8       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/param.h>
     42   1.1       cgd #include <sys/time.h>
     43  1.29   thorpej #include <sys/lwp.h>
     44  1.16       mrg #include <sys/proc.h>
     45   1.1       cgd #include <sys/resource.h>
     46  1.16       mrg #include <sys/sysctl.h>
     47  1.16       mrg #include <sys/ucred.h>
     48   1.7       cgd 
     49   1.7       cgd #include <err.h>
     50   1.1       cgd #include <errno.h>
     51  1.21    simonb #include <kvm.h>
     52  1.58  christos #include <ctype.h>
     53   1.7       cgd #include <stddef.h>
     54   1.1       cgd #include <stdio.h>
     55   1.1       cgd #include <stdlib.h>
     56   1.1       cgd #include <string.h>
     57  1.18  christos #include <signal.h>
     58  1.59  christos #include <util.h>
     59   1.7       cgd 
     60   1.1       cgd #include "ps.h"
     61   1.1       cgd 
     62  1.32       dsl static VAR *findvar(const char *);
     63  1.32       dsl static int  vcmp(const void *, const void *);
     64   1.1       cgd 
     65   1.1       cgd /* Compute offset in common structures. */
     66  1.23    simonb #define	POFF(x)	offsetof(struct kinfo_proc2, x)
     67  1.29   thorpej #define	LOFF(x)	offsetof(struct kinfo_lwp, x)
     68   1.1       cgd 
     69   1.1       cgd #define	UIDFMT	"u"
     70  1.49  christos #define	UID(n1, n2, of) \
     71  1.49  christos 	{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
     72  1.49  christos 	  .off = POFF(of), .type = UINT32, .fmt = UIDFMT }
     73  1.32       dsl #define	GID(n1, n2, off)	UID(n1, n2, off)
     74  1.12       cgd 
     75   1.1       cgd #define	PIDFMT	"d"
     76  1.49  christos #define	PID(n1, n2, of) \
     77  1.49  christos 	{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
     78  1.49  christos 	  .off = POFF(of), .type = INT32, .fmt = PIDFMT }
     79  1.49  christos 
     80  1.49  christos #define	LVAR(n1, n2, fl, of, ty, fm) \
     81  1.49  christos 	{ .name = n1, .header = n2, .flag = (fl) | LWP, .oproc = pvar, \
     82  1.49  christos 	  .off = LOFF(of), .type = ty, .fmt = fm }
     83  1.49  christos #define	PVAR(n1, n2, fl, of, ty, fm) \
     84  1.49  christos 	{ .name = n1, .header = n2, .flag = (fl) | 0, .oproc = pvar, \
     85  1.49  christos 	  .off = POFF(of), .type = ty, .fmt = fm }
     86  1.49  christos #define	PUVAR(n1, n2, fl, of, ty, fm) \
     87  1.49  christos 	{ .name = n1, .header = n2, .flag = (fl) | UAREA, .oproc = pvar, \
     88  1.49  christos 	  .off = POFF(of), .type = ty, .fmt = fm }
     89  1.49  christos #define VAR3(n1, n2, fl) \
     90  1.49  christos 	{ .name = n1, .header = n2, .flag = fl }
     91  1.49  christos #define VAR4(n1, n2, fl, op) \
     92  1.49  christos 	{ .name = n1, .header = n2, .flag = fl, .oproc = op, }
     93  1.49  christos #define VAR6(n1, n2, fl, op, of, ty) \
     94  1.49  christos 	{ .name = n1, .header = n2, .flag = fl, .oproc = op, \
     95  1.49  christos 	  .off = of, .type = ty }
     96  1.32       dsl 
     97  1.32       dsl /* NB: table must be sorted, in vi use:
     98  1.32       dsl  *	:/^VAR/,/end_sort/! sort -t\" +1
     99  1.32       dsl  * breaking long lines just makes the sort harder
    100  1.47       apb  *
    101  1.47       apb  * We support all the fields required by P1003.1-2004 (SUSv3), with
    102  1.47       apb  * the correct default headers, except for the "tty" field, where the
    103  1.47       apb  * standard says the header should be "TT", but we have "TTY".
    104  1.32       dsl  */
    105   1.1       cgd VAR var[] = {
    106  1.49  christos 	VAR6("%cpu", "%CPU", 0, pcpu, 0, PCPU),
    107  1.49  christos 	VAR6("%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32),
    108  1.58  christos 	PVAR("acflag", "ACFLG", 0, p_acflag, PROCACFLAG, "x"),
    109  1.49  christos 	VAR3("acflg", "acflag", ALIAS),
    110  1.49  christos 	VAR3("args", "command", ALIAS),
    111  1.49  christos 	VAR3("blocked", "sigmask", ALIAS),
    112  1.49  christos 	VAR3("caught", "sigcatch", ALIAS),
    113  1.49  christos 	VAR4("comm", "COMMAND", COMM|ARGV0|LJUST, command),
    114  1.49  christos 	VAR4("command", "COMMAND", COMM|LJUST, command),
    115  1.32       dsl 	PVAR("cpu", "CPU", 0, p_estcpu, UINT, "u"),
    116  1.52  christos 	VAR4("cpuid", "CPUID", LWP, cpuid),
    117  1.49  christos 	VAR3("cputime", "time", ALIAS),
    118  1.49  christos 	VAR6("ctime", "CTIME", 0, putimeval, POFF(p_uctime_sec), TIMEVAL),
    119  1.32       dsl 	GID("egid", "EGID", p_gid),
    120  1.49  christos 	VAR4("egroup", "EGROUP", LJUST, gname),
    121  1.50  christos 	VAR4("emul", "EMUL", LJUST, emul),
    122  1.49  christos 	VAR6("etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL),
    123  1.32       dsl 	UID("euid", "EUID", p_uid),
    124  1.55     kamil 	VAR4("euser", "EUSER", LJUST, usrname),
    125  1.58  christos 	PVAR("f", "F", 0, p_flag, PROCFLAG, "x"),
    126  1.49  christos 	VAR3("flags", "f", ALIAS),
    127  1.32       dsl 	GID("gid", "GID", p_gid),
    128  1.49  christos 	VAR4("group", "GROUP", LJUST, gname),
    129  1.49  christos 	VAR4("groupnames", "GROUPNAMES", LJUST, groupnames),
    130  1.49  christos 	VAR4("groups", "GROUPS", LJUST, groups),
    131  1.53     rmind 	/* holdcnt: unused, left for compat. */
    132  1.32       dsl 	LVAR("holdcnt", "HOLDCNT", 0, l_holdcnt, INT, "d"),
    133  1.57     kamil 	PUVAR("idrss", "IDRSS", 0, p_uru_idrss, UINT64, PRIu64),
    134  1.49  christos 	VAR3("ignored", "sigignore", ALIAS),
    135  1.32       dsl 	PUVAR("inblk", "INBLK", 0, p_uru_inblock, UINT64, PRIu64),
    136  1.49  christos 	VAR3("inblock", "inblk", ALIAS),
    137  1.57     kamil 	PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
    138  1.57     kamil 	PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
    139  1.32       dsl 	PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
    140  1.59  christos 	PVAR("ktrace", "KTRACE", 0, p_traceflag, KTRACEFLAG, "x"),
    141  1.32       dsl /*XXX*/	PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
    142  1.42   nathanw 	LVAR("laddr", "LADDR", 0, l_laddr, KPTR, PRIx64),
    143  1.41    simonb 	LVAR("lid", "LID", 0, l_lid, INT32, "d"),
    144  1.49  christos 	VAR4("lim", "LIM", 0, maxrss),
    145  1.51      yamt 	VAR4("lname", "LNAME", LJUST|LWP, lname),
    146  1.49  christos 	VAR4("login", "LOGIN", LJUST, logname),
    147  1.49  christos 	VAR3("logname", "login", ALIAS),
    148  1.49  christos 	VAR6("lstart", "STARTED", LJUST, lstarted, POFF(p_ustart_sec), UINT32),
    149  1.49  christos 	VAR4("lstate", "STAT", LJUST|LWP, lstate),
    150  1.54   mlelstv 	VAR6("ltime", "LTIME", LWP, lcputime, 0, CPUTIME),
    151  1.32       dsl 	PUVAR("majflt", "MAJFLT", 0, p_uru_majflt, UINT64, PRIu64),
    152  1.57     kamil 	PUVAR("maxrss", "MAXRSS", 0, p_uru_maxrss, UINT64, PRIu64),
    153  1.32       dsl 	PUVAR("minflt", "MINFLT", 0, p_uru_minflt, UINT64, PRIu64),
    154  1.32       dsl 	PUVAR("msgrcv", "MSGRCV", 0, p_uru_msgrcv, UINT64, PRIu64),
    155  1.32       dsl 	PUVAR("msgsnd", "MSGSND", 0, p_uru_msgsnd, UINT64, PRIu64),
    156  1.49  christos 	VAR3("ni", "nice", ALIAS),
    157  1.49  christos 	VAR6("nice", "NI", 0, pnice, POFF(p_nice), UCHAR),
    158  1.32       dsl 	PUVAR("nivcsw", "NIVCSW", 0, p_uru_nivcsw, UINT64, PRIu64),
    159  1.32       dsl 	PVAR("nlwp", "NLWP", 0, p_nlwps, UINT64, PRId64),
    160  1.49  christos 	VAR3("nsignals", "nsigs", ALIAS),
    161  1.32       dsl 	PUVAR("nsigs", "NSIGS", 0, p_uru_nsignals, UINT64, PRIu64),
    162  1.53     rmind 	/* nswap: unused, left for compat. */
    163  1.32       dsl 	PUVAR("nswap", "NSWAP", 0, p_uru_nswap, UINT64, PRIu64),
    164  1.32       dsl 	PUVAR("nvcsw", "NVCSW", 0, p_uru_nvcsw, UINT64, PRIu64),
    165  1.32       dsl /*XXX*/	LVAR("nwchan", "WCHAN", 0, l_wchan, KPTR, PRIx64),
    166  1.32       dsl 	PUVAR("oublk", "OUBLK", 0, p_uru_oublock, UINT64, PRIu64),
    167  1.49  christos 	VAR3("oublock", "oublk", ALIAS),
    168  1.32       dsl /*XXX*/	PVAR("p_ru", "P_RU", 0, p_ru, KPTR, PRIx64),
    169  1.32       dsl /*XXX*/	PVAR("paddr", "PADDR", 0, p_paddr, KPTR, PRIx64),
    170  1.32       dsl 	PUVAR("pagein", "PAGEIN", 0, p_uru_majflt, UINT64, PRIu64),
    171  1.49  christos 	VAR3("pcpu", "%cpu", ALIAS),
    172  1.49  christos 	VAR3("pending", "sig", ALIAS),
    173  1.32       dsl 	PID("pgid", "PGID", p__pgid),
    174  1.32       dsl 	PID("pid", "PID", p_pid),
    175  1.49  christos 	VAR3("pmem", "%mem", ALIAS),
    176  1.32       dsl 	PID("ppid", "PPID", p_ppid),
    177  1.49  christos 	VAR4("pri", "PRI", LWP, pri),
    178  1.32       dsl 	LVAR("re", "RE", INF127, l_swtime, UINT, "u"),
    179  1.32       dsl 	GID("rgid", "RGID", p_rgid),
    180  1.49  christos 	VAR4("rgroup", "RGROUP", LJUST, rgname),
    181  1.32       dsl /*XXX*/	LVAR("rlink", "RLINK", 0, l_back, KPTR, PRIx64),
    182  1.32       dsl 	PVAR("rlwp", "RLWP", 0, p_nrlwps, UINT64, PRId64),
    183  1.49  christos 	VAR6("rss", "RSS", 0, p_rssize, POFF(p_vm_rssize), INT32),
    184  1.49  christos 	VAR3("rssize", "rsz", ALIAS),
    185  1.49  christos 	VAR6("rsz", "RSZ", 0, rssize, POFF(p_vm_rssize), INT32),
    186  1.32       dsl 	UID("ruid", "RUID", p_ruid),
    187  1.49  christos 	VAR4("ruser", "RUSER", LJUST, runame),
    188  1.32       dsl 	PVAR("sess", "SESS", 0, p_sess, KPTR24, PRIx64),
    189  1.32       dsl 	PID("sid", "SID", p_sid),
    190  1.32       dsl 	PVAR("sig", "PENDING", 0, p_siglist, SIGLIST, "s"),
    191  1.32       dsl 	PVAR("sigcatch", "CAUGHT", 0, p_sigcatch, SIGLIST, "s"),
    192  1.32       dsl 	PVAR("sigignore", "IGNORED", 0, p_sigignore, SIGLIST, "s"),
    193  1.32       dsl 	PVAR("sigmask", "BLOCKED", 0, p_sigmask, SIGLIST, "s"),
    194  1.32       dsl 	LVAR("sl", "SL", INF127, l_slptime, UINT, "u"),
    195  1.49  christos 	VAR6("start", "STARTED", 0, started, POFF(p_ustart_sec), UINT32),
    196  1.49  christos 	VAR3("stat", "state", ALIAS),
    197  1.49  christos 	VAR4("state", "STAT", LJUST, state),
    198  1.49  christos 	VAR6("stime", "STIME", 0, putimeval, POFF(p_ustime_sec), TIMEVAL),
    199  1.32       dsl 	GID("svgid", "SVGID", p_svgid),
    200  1.49  christos 	VAR4("svgroup", "SVGROUP", LJUST, svgname),
    201  1.32       dsl 	UID("svuid", "SVUID", p_svuid),
    202  1.49  christos 	VAR4("svuser", "SVUSER", LJUST, svuname),
    203  1.34       dsl 	/* "tdev" is UINT32, but we do this for sorting purposes */
    204  1.49  christos 	VAR6("tdev", "TDEV", 0, tdev, POFF(p_tdev), INT32),
    205  1.49  christos 	VAR6("time", "TIME", 0, cputime, 0, CPUTIME),
    206  1.36       mjl 	PID("tpgid", "TPGID", p_tpgid),
    207  1.32       dsl 	PVAR("tsess", "TSESS", 0, p_tsess, KPTR, PRIx64),
    208  1.49  christos 	VAR6("tsiz", "TSIZ", 0, tsize, POFF(p_vm_tsize), INT32),
    209  1.49  christos 	VAR6("tt", "TTY", LJUST, tname, POFF(p_tdev), INT32),
    210  1.49  christos 	VAR6("tty", "TTY", LJUST, longtname, POFF(p_tdev), INT32),
    211  1.45       chs 	LVAR("uaddr", "UADDR", 0, l_addr, KPTR, PRIx64),
    212  1.49  christos 	VAR4("ucomm", "UCOMM", LJUST, ucomm),
    213  1.32       dsl 	UID("uid", "UID", p_uid),
    214  1.32       dsl 	LVAR("upr", "UPR", 0, l_usrpri, UCHAR, "u"),
    215  1.55     kamil 	VAR4("user", "USER", LJUST, usrname),
    216  1.49  christos 	VAR3("usrpri", "upr", ALIAS),
    217  1.49  christos 	VAR6("utime", "UTIME", 0, putimeval, POFF(p_uutime_sec), TIMEVAL),
    218  1.49  christos 	VAR3("vsize", "vsz", ALIAS),
    219  1.49  christos 	VAR6("vsz", "VSZ", 0, vsize, 0, VSIZE),
    220  1.49  christos 	VAR4("wchan", "WCHAN", LJUST|LWP, wchan),
    221  1.32       dsl 	PVAR("xstat", "XSTAT", 0, p_xstat, USHORT, "x"),
    222  1.32       dsl /* "zzzz" end_sort */
    223  1.49  christos 	{ .name = "" },
    224   1.1       cgd };
    225   1.1       cgd 
    226   1.7       cgd void
    227  1.32       dsl showkey(void)
    228   1.1       cgd {
    229   1.7       cgd 	VAR *v;
    230   1.7       cgd 	int i;
    231  1.44  christos 	const char *p;
    232  1.44  christos 	const char *sep;
    233   1.1       cgd 
    234   1.1       cgd 	i = 0;
    235   1.1       cgd 	sep = "";
    236   1.1       cgd 	for (v = var; *(p = v->name); ++v) {
    237   1.7       cgd 		int len = strlen(p);
    238   1.1       cgd 		if (termwidth && (i += len + 1) > termwidth) {
    239   1.1       cgd 			i = len;
    240   1.1       cgd 			sep = "\n";
    241   1.1       cgd 		}
    242  1.38    simonb 		(void)printf("%s%s", sep, p);
    243   1.1       cgd 		sep = " ";
    244   1.1       cgd 	}
    245  1.38    simonb 	(void)printf("\n");
    246   1.1       cgd }
    247   1.1       cgd 
    248  1.47       apb /*
    249  1.47       apb  * Parse the string pp, and insert or append entries to the list
    250  1.47       apb  * referenced by listptr.  If pos in non-null and *pos is non-null, then
    251  1.47       apb  * *pos specifies where to insert (instead of appending).  If pos is
    252  1.47       apb  * non-null, then a new value is returned through *pos referring to the
    253  1.47       apb  * last item inserted.
    254  1.47       apb  */
    255  1.32       dsl static void
    256  1.47       apb parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
    257  1.32       dsl {
    258  1.47       apb 	char *p, *sp, *equalsp;
    259  1.47       apb 
    260  1.47       apb 	/* dup to avoid zapping arguments.  We will free sp later. */
    261  1.59  christos 	p = sp = estrdup(pp);
    262  1.32       dsl 
    263  1.47       apb 	/*
    264  1.47       apb 	 * Everything after the first '=' is part of a custom header.
    265  1.47       apb 	 * Temporarily replace it with '\0' to simplify other code.
    266  1.47       apb 	 */
    267  1.47       apb 	equalsp = strchr(p, '=');
    268  1.47       apb 	if (equalsp)
    269  1.47       apb 	    *equalsp = '\0';
    270   1.1       cgd 
    271   1.1       cgd #define	FMTSEP	" \t,\n"
    272   1.1       cgd 	while (p && *p) {
    273   1.7       cgd 		char *cp;
    274   1.7       cgd 		VAR *v;
    275   1.7       cgd 		struct varent *vent;
    276   1.7       cgd 
    277  1.47       apb 		/*
    278  1.47       apb 		 * skip separators before the first keyword, and
    279  1.47       apb 		 * look for the separator after the keyword.
    280  1.47       apb 		 */
    281  1.47       apb 		for (cp = p; *cp != '\0'; cp++) {
    282  1.47       apb 		    p = strpbrk(cp, FMTSEP);
    283  1.47       apb 		    if (p != cp)
    284  1.47       apb 			break;
    285  1.47       apb 		}
    286  1.47       apb 		if (*cp == '\0')
    287  1.47       apb 		    break;
    288  1.47       apb 		/*
    289  1.47       apb 		 * Now cp points to the start of a keyword,
    290  1.47       apb 		 * and p is NULL or points past the end of the keyword.
    291  1.47       apb 		 *
    292  1.47       apb 		 * Terminate the keyword with '\0', or reinstate the
    293  1.47       apb 		 * '=' that was removed earlier, if appropriate.
    294  1.47       apb 		 */
    295  1.47       apb 		if (p) {
    296  1.47       apb 			*p = '\0';
    297  1.47       apb 			p++;
    298  1.47       apb 		} else if (equalsp) {
    299  1.47       apb 			*equalsp = '=';
    300  1.47       apb 		}
    301  1.47       apb 
    302  1.47       apb 		/*
    303  1.47       apb 		 * If findvar() likes the keyword or keyword=header,
    304  1.47       apb 		 * add it to our list.  If findvar() doesn't like it,
    305  1.47       apb 		 * it will print a warning, so we ignore it.
    306  1.47       apb 		 */
    307  1.47       apb 		if ((v = findvar(cp)) == NULL)
    308   1.1       cgd 			continue;
    309  1.59  christos 		vent = emalloc(sizeof(*vent));
    310   1.1       cgd 		vent->var = v;
    311  1.47       apb 		if (pos && *pos)
    312  1.47       apb 		    SIMPLEQ_INSERT_AFTER(listptr, *pos, vent, next);
    313  1.47       apb 		else {
    314  1.47       apb 		    SIMPLEQ_INSERT_TAIL(listptr, vent, next);
    315  1.47       apb 		}
    316  1.47       apb 		if (pos)
    317  1.47       apb 		    *pos = vent;
    318   1.1       cgd 	}
    319  1.47       apb  	free(sp);
    320  1.47       apb 	if (SIMPLEQ_EMPTY(listptr))
    321  1.56  christos 		errx(EXIT_FAILURE, "no valid keywords");
    322   1.1       cgd }
    323   1.1       cgd 
    324  1.32       dsl void
    325  1.32       dsl parsefmt(const char *p)
    326  1.32       dsl {
    327  1.32       dsl 
    328  1.47       apb 	parsevarlist(p, &displaylist, NULL);
    329  1.47       apb }
    330  1.47       apb 
    331  1.47       apb void
    332  1.47       apb parsefmt_insert(const char *p, struct varent **pos)
    333  1.47       apb {
    334  1.47       apb 
    335  1.47       apb 	parsevarlist(p, &displaylist, pos);
    336  1.32       dsl }
    337  1.32       dsl 
    338  1.32       dsl void
    339  1.32       dsl parsesort(const char *p)
    340  1.32       dsl {
    341  1.32       dsl 
    342  1.47       apb 	parsevarlist(p, &sortlist, NULL);
    343  1.47       apb }
    344  1.47       apb 
    345  1.47       apb /* Search through a list for an entry with a specified name. */
    346  1.47       apb struct varent *
    347  1.47       apb varlist_find(struct varlist *list, const char *name)
    348  1.47       apb {
    349  1.47       apb 	struct varent *vent;
    350  1.47       apb 
    351  1.47       apb 	SIMPLEQ_FOREACH(vent, list, next) {
    352  1.47       apb 		if (strcmp(vent->var->name, name) == 0)
    353  1.47       apb 			break;
    354  1.47       apb 	}
    355  1.47       apb 	return vent;
    356  1.32       dsl }
    357  1.32       dsl 
    358   1.1       cgd static VAR *
    359  1.32       dsl findvar(const char *p)
    360   1.1       cgd {
    361  1.32       dsl 	VAR *v;
    362   1.1       cgd 	char *hp;
    363  1.58  christos 	char pp[1024];
    364  1.58  christos 	strlcpy(pp, p, sizeof(pp));
    365   1.1       cgd 
    366  1.58  christos 	hp = strchr(pp, '=');
    367   1.1       cgd 	if (hp)
    368   1.1       cgd 		*hp++ = '\0';
    369  1.58  christos 	for (char *dp = pp; *dp; dp++)
    370  1.58  christos 		*dp = tolower((unsigned char)*dp);
    371   1.1       cgd 
    372  1.59  christos 	v = bsearch(pp, var, __arraycount(var) - 1, sizeof(*var), vcmp);
    373  1.32       dsl 	if (v && v->flag & ALIAS)
    374  1.32       dsl 		v = findvar(v->header);
    375   1.1       cgd 	if (!v) {
    376   1.7       cgd 		warnx("%s: keyword not found", p);
    377   1.1       cgd 		eval = 1;
    378  1.32       dsl 		return NULL;
    379   1.1       cgd 	}
    380  1.32       dsl 
    381  1.58  christos 	if (!hp && *p == *pp)
    382  1.58  christos 		return v;
    383  1.58  christos 
    384  1.59  christos 	struct var *newvar = emalloc(sizeof(*newvar));
    385  1.59  christos 	*newvar = *v;
    386  1.58  christos 
    387  1.58  christos 	if (hp) {
    388  1.47       apb 		/*
    389  1.47       apb 		 * Override the header.
    390  1.47       apb 		 *
    391  1.47       apb 		 * We need to copy the entry first, and override the
    392  1.47       apb 		 * header in the copy, because the same field might be
    393  1.47       apb 		 * used multiple times with different headers.  We also
    394  1.47       apb 		 * need to strdup the header.
    395  1.47       apb 		 */
    396  1.59  christos 		newvar->header = estrdup(hp);
    397  1.47       apb 		/*
    398  1.47       apb 		 * According to P1003.1-2004, if the header text is null,
    399  1.47       apb 		 * such as -o user=, the field width will be at least as
    400  1.47       apb 		 * wide as the default header text.
    401  1.47       apb 		 */
    402  1.47       apb 		if (*hp == '\0')
    403  1.48       apb 			newvar->width = strlen(v->header);
    404  1.58  christos 	}
    405  1.58  christos 
    406  1.58  christos 	if (*p != *pp)
    407  1.58  christos 		newvar->flag |= ALTPR|LJUST;
    408  1.48       apb 
    409  1.60  christos 	return newvar;
    410   1.1       cgd }
    411   1.1       cgd 
    412   1.7       cgd static int
    413  1.32       dsl vcmp(const void *a, const void *b)
    414   1.1       cgd {
    415  1.44  christos         return strcmp(a, ((const VAR *)b)->name);
    416   1.1       cgd }
    417