xref: /freebsd/bin/ps/keyword.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	$Id: keyword.c,v 1.6 1995/09/04 01:22:40 peter Exp $
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)keyword.c	8.5 (Berkeley) 4/2/94";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/resource.h>
43 #include <sys/proc.h>
44 
45 #include <err.h>
46 #include <errno.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "ps.h"
53 
54 #ifdef P_PPWAIT
55 #define NEWVM
56 #endif
57 
58 #ifdef NEWVM
59 #include <sys/ucred.h>
60 #include <sys/sysctl.h>
61 #endif
62 
63 static VAR *findvar __P((char *));
64 static int  vcmp __P((const void *, const void *));
65 
66 #ifdef NOTINUSE
67 int	utime(), stime(), ixrss(), idrss(), isrss();
68 	{{"utime"}, "UTIME", USER, utime, 4},
69 	{{"stime"}, "STIME", USER, stime, 4},
70 	{{"ixrss"}, "IXRSS", USER, ixrss, 4},
71 	{{"idrss"}, "IDRSS", USER, idrss, 4},
72 	{{"isrss"}, "ISRSS", USER, isrss, 4},
73 #endif
74 
75 /* Compute offset in common structures. */
76 #define	POFF(x)	offsetof(struct proc, x)
77 #define	EOFF(x)	offsetof(struct eproc, x)
78 #define	UOFF(x)	offsetof(struct usave, x)
79 #define	ROFF(x)	offsetof(struct rusage, x)
80 
81 #define	UIDFMT	"u"
82 #define	UIDLEN	5
83 #define	PIDFMT	"d"
84 #define	PIDLEN	5
85 #define	USERLEN	8
86 
87 VAR var[] = {
88 #ifdef NEWVM
89 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
90 	{"%mem", "%MEM", NULL, 0, pmem, 4},
91 	{"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
92 	{"acflg", "", "acflag"},
93 	{"blocked", "", "sigmask"},
94 	{"caught", "", "sigcatch"},
95 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
96 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), ULONG, "d"},
97 	{"cputime", "", "time"},
98 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
99 	{"flags", "", "f"},
100 	{"ignored", "", "sigignore"},
101 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
102 	{"inblock", "", "inblk"},
103 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
104 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
105 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
106 	{"lim", "LIM", NULL, 0, maxrss, 5},
107 	{"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
108 	{"logname", "", "login"},
109 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
110 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
111 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
112 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
113 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
114 	{"ni", "", "nice"},
115 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
116 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
117 	{"nsignals", "", "nsigs"},
118 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
119 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
120 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
121 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
122 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
123 	{"oublock", "", "oublk"},
124 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
125 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
126 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
127 	{"pcpu", "", "%cpu"},
128 	{"pending", "", "sig"},
129 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
130 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
131 	{"pmem", "", "%mem"},
132 	{"ppid", "PPID", NULL, 0, evar, PIDLEN, EOFF(e_ppid), LONG, PIDFMT},
133 	{"pri", "PRI", NULL, 0, pri, 3},
134 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
135 	{"rgid", "RGID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_rgid),
136 		ULONG, UIDFMT},
137 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
138 	{"rss", "RSS", NULL, 0, p_rssize, 4},
139 	{"rssize", "", "rsz"},
140 	{"rsz", "RSZ", NULL, 0, rssize, 4},
141 	{"rtprio", "RTPRIO", NULL, 0, pvar, 7, POFF(p_rtprio), LONG, "d"},
142 	{"ruid", "RUID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_ruid),
143 		ULONG, UIDFMT},
144 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
145 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
146 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_siglist), LONG, "x"},
147 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
148 	{"sigignore", "IGNORED",
149 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
150 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
151 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
152 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
153 	{"stat", "", "state"},
154 	{"state", "STAT", NULL, 0, state, 4},
155 	{"svgid", "SVGID",
156 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svgid), ULONG, UIDFMT},
157 	{"svuid", "SVUID",
158 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svuid), ULONG, UIDFMT},
159 	{"tdev", "TDEV", NULL, 0, tdev, 4},
160 	{"time", "TIME", NULL, USER, cputime, 9},
161 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
162 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
163 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
164 	{"tt", "TT ", NULL, 0, tname, 4},
165 	{"tty", "TTY", NULL, LJUST, longtname, 8},
166 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
167 	{"uid", "UID", NULL, 0, evar, UIDLEN, EOFF(e_ucred.cr_uid),
168 		ULONG, UIDFMT},
169 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
170 	{"user", "USER", NULL, LJUST, uname, USERLEN},
171 	{"usrpri", "", "upr"},
172 	{"vsize", "", "vsz"},
173 	{"vsz", "VSZ", NULL, 0, vsize, 5},
174 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
175 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
176 #else
177 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
178 	{"%mem", "%MEM", NULL, 0, pmem, 4},
179 	{"acflag", "ACFLG", NULL, USER, uvar, 3, UOFF(u_acflag), SHORT, "x"},
180 	{"acflg", "", "acflag"},
181 	{"blocked", "", "sigmask"},
182 	{"caught", "", "sigcatch"},
183 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
184 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_cpu), ULONG, "d"},
185 	{"cputime", "", "time"},
186 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
187 	{"flags", "", "f"},
188 	{"ignored", "", "sigignore"},
189 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
190 	{"inblock", "", "inblk"},
191 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
192 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
193 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
194 	{"lim", "LIM", NULL, 0, maxrss, 5},
195 	{"logname", "LOGNAME", NULL, LJUST, logname, MAXLOGNAME},
196 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
197 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
198 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
199 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
200 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
201 	{"ni", "", "nice"},
202 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
203 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
204 	{"nsignals", "", "nsigs"},
205 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
206 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
207 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
208 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
209 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
210 	{"oublock", "", "oublk"},
211 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
212 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
213 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
214 	{"pcpu", "", "%cpu"},
215 	{"pending", "", "sig"},
216 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
217 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
218 	{"pmem", "", "%mem"},
219 	{"poip", "POIP", NULL, 0, pvar, 4, POFF(p_poip), SHORT, "d"},
220 	{"ppid", "PPID", NULL, 0, pvar, PIDLEN, POFF(p_ppid), LONG, PIDFMT},
221 	{"pri", "PRI", NULL, 0, pri, 3},
222 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
223 	{"rgid", "RGID", NULL, 0, pvar, UIDLEN, POFF(p_rgid), USHORT, UIDFMT},
224 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_rlink), KPTR, "x"},
225 	{"rss", "RSS", NULL, 0, p_rssize, 4},
226 	{"rssize", "", "rsz"},
227 	{"rsz", "RSZ", NULL, 0, rssize, 4},
228 	{"ruid", "RUID", NULL, 0, pvar, UIDLEN, POFF(p_ruid), USHORT, UIDFMT},
229 	{"rtprio", "RTPRIO", NULL, 0, pvar, 7, POFF(p_rtprio), LONG, "d"},
230 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
231 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
232 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"},
233 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
234 	{"sigignore", "IGNORED",
235 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
236 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
237 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
238 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
239 	{"stat", "", "state"},
240 	{"state", "STAT", NULL, 0, state, 4},
241 	{"svgid", "SVGID",
242 		NULL, 0, pvar, UIDLEN, POFF(p_svgid), USHORT, UIDFMT},
243 	{"svuid", "SVUID",
244 		NULL, 0, pvar, UIDLEN, POFF(p_svuid), USHORT, UIDFMT},
245 	{"tdev", "TDEV", NULL, 0, tdev, 4},
246 	{"time", "TIME", NULL, USER, cputime, 9},
247 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
248 	{"trs", "TRS", NULL, 0, trss, 3},
249 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
250 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
251 	{"tt", "TT", NULL, LJUST, tname, 4},
252 	{"tty", "TTY", NULL, LJUST, longtname, 8},
253 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
254 	{"uid", "UID", NULL, 0, pvar, UIDLEN, POFF(p_uid),USHORT, UIDFMT},
255 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
256 	{"uprocp", "UPROCP", NULL, USER, uvar, 6, UOFF(u_procp), KPTR, "x"},
257 	{"user", "USER", NULL, LJUST, uname, USERLEN},
258 	{"usrpri", "", "upr"},
259 	{"vsize", "", "vsz"},
260 	{"vsz", "VSZ", NULL, 0, vsize, 5},
261 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
262 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
263 #endif
264 	{""},
265 };
266 
267 void
268 showkey()
269 {
270 	VAR *v;
271 	int i;
272 	char *p, *sep;
273 
274 	i = 0;
275 	sep = "";
276 	for (v = var; *(p = v->name); ++v) {
277 		int len = strlen(p);
278 		if (termwidth && (i += len + 1) > termwidth) {
279 			i = len;
280 			sep = "\n";
281 		}
282 		(void) printf("%s%s", sep, p);
283 		sep = " ";
284 	}
285 	(void) printf("\n");
286 }
287 
288 void
289 parsefmt(p)
290 	char *p;
291 {
292 	static struct varent *vtail;
293 
294 #define	FMTSEP	" \t,\n"
295 	while (p && *p) {
296 		char *cp;
297 		VAR *v;
298 		struct varent *vent;
299 
300 		while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
301 			/* void */;
302 		if (!(v = findvar(cp)))
303 			continue;
304 		if ((vent = malloc(sizeof(struct varent))) == NULL)
305 			err(1, NULL);
306 		vent->var = v;
307 		vent->next = NULL;
308 		if (vhead == NULL)
309 			vhead = vtail = vent;
310 		else {
311 			vtail->next = vent;
312 			vtail = vent;
313 		}
314 	}
315 	if (!vhead)
316 		errx(1, "no valid keywords");
317 }
318 
319 static VAR *
320 findvar(p)
321 	char *p;
322 {
323 	VAR *v, key;
324 	char *hp;
325 	int vcmp();
326 
327 	key.name = p;
328 
329 	hp = strchr(p, '=');
330 	if (hp)
331 		*hp++ = '\0';
332 
333 	key.name = p;
334 	v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
335 
336 	if (v && v->alias) {
337 		if (hp) {
338 			warnx("%s: illegal keyword specification", p);
339 			eval = 1;
340 		}
341 		parsefmt(v->alias);
342 		return ((VAR *)NULL);
343 	}
344 	if (!v) {
345 		warnx("%s: keyword not found", p);
346 		eval = 1;
347 	}
348 	if (hp)
349 		v->header = hp;
350 	return (v);
351 }
352 
353 static int
354 vcmp(a, b)
355         const void *a, *b;
356 {
357         return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
358 }
359