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