xref: /original-bsd/bin/ps/keyword.c (revision 731b2947)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)keyword.c	8.5 (Berkeley) 04/02/94";
10 #endif /* not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 #include <sys/proc.h>
16 
17 #include <err.h>
18 #include <errno.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "ps.h"
25 
26 #ifdef P_PPWAIT
27 #define NEWVM
28 #endif
29 
30 #ifdef NEWVM
31 #include <sys/ucred.h>
32 #include <sys/sysctl.h>
33 #endif
34 
35 static VAR *findvar __P((char *));
36 static int  vcmp __P((const void *, const void *));
37 
38 #ifdef NOTINUSE
39 int	utime(), stime(), ixrss(), idrss(), isrss();
40 	{{"utime"}, "UTIME", USER, utime, 4},
41 	{{"stime"}, "STIME", USER, stime, 4},
42 	{{"ixrss"}, "IXRSS", USER, ixrss, 4},
43 	{{"idrss"}, "IDRSS", USER, idrss, 4},
44 	{{"isrss"}, "ISRSS", USER, isrss, 4},
45 #endif
46 
47 /* Compute offset in common structures. */
48 #define	POFF(x)	offsetof(struct proc, x)
49 #define	EOFF(x)	offsetof(struct eproc, x)
50 #define	UOFF(x)	offsetof(struct usave, x)
51 #define	ROFF(x)	offsetof(struct rusage, x)
52 
53 #define	UIDFMT	"u"
54 #define	UIDLEN	5
55 #define	PIDFMT	"d"
56 #define	PIDLEN	5
57 #define	USERLEN	8
58 
59 VAR var[] = {
60 #ifdef NEWVM
61 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
62 	{"%mem", "%MEM", NULL, 0, pmem, 4},
63 	{"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
64 	{"acflg", "", "acflag"},
65 	{"blocked", "", "sigmask"},
66 	{"caught", "", "sigcatch"},
67 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
68 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), ULONG, "d"},
69 	{"cputime", "", "time"},
70 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
71 	{"flags", "", "f"},
72 	{"ignored", "", "sigignore"},
73 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
74 	{"inblock", "", "inblk"},
75 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
76 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
77 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
78 	{"lim", "LIM", NULL, 0, maxrss, 5},
79 	{"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
80 	{"logname", "", "login"},
81 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
82 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
83 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
84 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
85 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
86 	{"ni", "", "nice"},
87 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
88 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
89 	{"nsignals", "", "nsigs"},
90 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
91 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
92 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
93 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
94 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
95 	{"oublock", "", "oublk"},
96 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
97 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
98 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
99 	{"pcpu", "", "%cpu"},
100 	{"pending", "", "sig"},
101 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
102 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
103 	{"pmem", "", "%mem"},
104 	{"ppid", "PPID", NULL, 0, evar, PIDLEN, EOFF(e_ppid), LONG, PIDFMT},
105 	{"pri", "PRI", NULL, 0, pri, 3},
106 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
107 	{"rgid", "RGID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_rgid),
108 		ULONG, UIDFMT},
109 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
110 	{"rss", "RSS", NULL, 0, p_rssize, 4},
111 	{"rssize", "", "rsz"},
112 	{"rsz", "RSZ", NULL, 0, rssize, 4},
113 	{"ruid", "RUID", NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_ruid),
114 		ULONG, UIDFMT},
115 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
116 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
117 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_siglist), LONG, "x"},
118 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
119 	{"sigignore", "IGNORED",
120 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
121 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
122 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
123 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
124 	{"stat", "", "state"},
125 	{"state", "STAT", NULL, 0, state, 4},
126 	{"svgid", "SVGID",
127 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svgid), ULONG, UIDFMT},
128 	{"svuid", "SVUID",
129 		NULL, 0, evar, UIDLEN, EOFF(e_pcred.p_svuid), ULONG, UIDFMT},
130 	{"tdev", "TDEV", NULL, 0, tdev, 4},
131 	{"time", "TIME", NULL, USER, cputime, 9},
132 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
133 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
134 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
135 	{"tt", "TT", NULL, LJUST, tname, 3},
136 	{"tty", "TTY", NULL, LJUST, longtname, 8},
137 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
138 	{"uid", "UID", NULL, 0, evar, UIDLEN, EOFF(e_ucred.cr_uid),
139 		ULONG, UIDFMT},
140 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
141 	{"user", "USER", NULL, LJUST, uname, USERLEN},
142 	{"usrpri", "", "upr"},
143 	{"vsize", "", "vsz"},
144 	{"vsz", "VSZ", NULL, 0, vsize, 5},
145 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
146 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
147 #else
148 	{"%cpu", "%CPU", NULL, 0, pcpu, 4},
149 	{"%mem", "%MEM", NULL, 0, pmem, 4},
150 	{"acflag", "ACFLG", NULL, USER, uvar, 3, UOFF(u_acflag), SHORT, "x"},
151 	{"acflg", "", "acflag"},
152 	{"blocked", "", "sigmask"},
153 	{"caught", "", "sigcatch"},
154 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
155 	{"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_cpu), ULONG, "d"},
156 	{"cputime", "", "time"},
157 	{"f", "F", NULL, 0, pvar, 7, POFF(p_flag), LONG, "x"},
158 	{"flags", "", "f"},
159 	{"ignored", "", "sigignore"},
160 	{"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
161 	{"inblock", "", "inblk"},
162 	{"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
163 	{"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), LONG, "x"},
164 	{"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), LONG, "x"},
165 	{"lim", "LIM", NULL, 0, maxrss, 5},
166 	{"logname", "LOGNAME", NULL, LJUST, logname, MAXLOGNAME},
167 	{"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
168 	{"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
169 	{"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
170 	{"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
171 	{"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
172 	{"ni", "", "nice"},
173 	{"nice", "NI", NULL, 0, pvar, 2, POFF(p_nice), CHAR, "d"},
174 	{"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
175 	{"nsignals", "", "nsigs"},
176 	{"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
177 	{"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
178 	{"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
179 	{"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
180 	{"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
181 	{"oublock", "", "oublk"},
182 	{"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
183 	{"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
184 	{"pagein", "PAGEIN", NULL, USER, pagein, 6},
185 	{"pcpu", "", "%cpu"},
186 	{"pending", "", "sig"},
187 	{"pgid", "PGID", NULL, 0, evar, PIDLEN, EOFF(e_pgid), ULONG, PIDFMT},
188 	{"pid", "PID", NULL, 0, pvar, PIDLEN, POFF(p_pid), LONG, PIDFMT},
189 	{"pmem", "", "%mem"},
190 	{"poip", "POIP", NULL, 0, pvar, 4, POFF(p_poip), SHORT, "d"},
191 	{"ppid", "PPID", NULL, 0, pvar, PIDLEN, POFF(p_ppid), LONG, PIDFMT},
192 	{"pri", "PRI", NULL, 0, pri, 3},
193 	{"re", "RE", NULL, 0, pvar, 3, POFF(p_swtime), ULONG, "d"},
194 	{"rgid", "RGID", NULL, 0, pvar, UIDLEN, POFF(p_rgid), USHORT, UIDFMT},
195 	{"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_rlink), KPTR, "x"},
196 	{"rss", "RSS", NULL, 0, p_rssize, 4},
197 	{"rssize", "", "rsz"},
198 	{"rsz", "RSZ", NULL, 0, rssize, 4},
199 	{"ruid", "RUID", NULL, 0, pvar, UIDLEN, POFF(p_ruid), USHORT, UIDFMT},
200 	{"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
201 	{"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
202 	{"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_sig), LONG, "x"},
203 	{"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), LONG, "x"},
204 	{"sigignore", "IGNORED",
205 		NULL, 0, pvar, 8, POFF(p_sigignore), LONG, "x"},
206 	{"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), LONG, "x"},
207 	{"sl", "SL", NULL, 0, pvar, 3, POFF(p_slptime), ULONG, "d"},
208 	{"start", "STARTED", NULL, LJUST|USER, started, 8},
209 	{"stat", "", "state"},
210 	{"state", "STAT", NULL, 0, state, 4},
211 	{"svgid", "SVGID",
212 		NULL, 0, pvar, UIDLEN, POFF(p_svgid), USHORT, UIDFMT},
213 	{"svuid", "SVUID",
214 		NULL, 0, pvar, UIDLEN, POFF(p_svuid), USHORT, UIDFMT},
215 	{"tdev", "TDEV", NULL, 0, tdev, 4},
216 	{"time", "TIME", NULL, USER, cputime, 9},
217 	{"tpgid", "TPGID", NULL, 0, evar, 4, EOFF(e_tpgid), ULONG, PIDFMT},
218 	{"trs", "TRS", NULL, 0, trss, 3},
219 	{"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
220 	{"tsiz", "TSIZ", NULL, 0, tsize, 4},
221 	{"tt", "TT", NULL, LJUST, tname, 3},
222 	{"tty", "TTY", NULL, LJUST, longtname, 8},
223 	{"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
224 	{"uid", "UID", NULL, 0, pvar, UIDLEN, POFF(p_uid),USHORT, UIDFMT},
225 	{"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), CHAR, "d"},
226 	{"uprocp", "UPROCP", NULL, USER, uvar, 6, UOFF(u_procp), KPTR, "x"},
227 	{"user", "USER", NULL, LJUST, uname, USERLEN},
228 	{"usrpri", "", "upr"},
229 	{"vsize", "", "vsz"},
230 	{"vsz", "VSZ", NULL, 0, vsize, 5},
231 	{"wchan", "WCHAN", NULL, LJUST, wchan, 6},
232 	{"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
233 #endif
234 	{""},
235 };
236 
237 void
showkey()238 showkey()
239 {
240 	VAR *v;
241 	int i;
242 	char *p, *sep;
243 
244 	i = 0;
245 	sep = "";
246 	for (v = var; *(p = v->name); ++v) {
247 		int len = strlen(p);
248 		if (termwidth && (i += len + 1) > termwidth) {
249 			i = len;
250 			sep = "\n";
251 		}
252 		(void) printf("%s%s", sep, p);
253 		sep = " ";
254 	}
255 	(void) printf("\n");
256 }
257 
258 void
parsefmt(p)259 parsefmt(p)
260 	char *p;
261 {
262 	static struct varent *vtail;
263 
264 #define	FMTSEP	" \t,\n"
265 	while (p && *p) {
266 		char *cp;
267 		VAR *v;
268 		struct varent *vent;
269 
270 		while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
271 			/* void */;
272 		if (!(v = findvar(cp)))
273 			continue;
274 		if ((vent = malloc(sizeof(struct varent))) == NULL)
275 			err(1, NULL);
276 		vent->var = v;
277 		vent->next = NULL;
278 		if (vhead == NULL)
279 			vhead = vtail = vent;
280 		else {
281 			vtail->next = vent;
282 			vtail = vent;
283 		}
284 	}
285 	if (!vhead)
286 		errx(1, "no valid keywords");
287 }
288 
289 static VAR *
findvar(p)290 findvar(p)
291 	char *p;
292 {
293 	VAR *v, key;
294 	char *hp;
295 	int vcmp();
296 
297 	key.name = p;
298 
299 	hp = strchr(p, '=');
300 	if (hp)
301 		*hp++ = '\0';
302 
303 	key.name = p;
304 	v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
305 
306 	if (v && v->alias) {
307 		if (hp) {
308 			warnx("%s: illegal keyword specification", p);
309 			eval = 1;
310 		}
311 		parsefmt(v->alias);
312 		return ((VAR *)NULL);
313 	}
314 	if (!v) {
315 		warnx("%s: keyword not found", p);
316 		eval = 1;
317 	}
318 	if (hp)
319 		v->header = hp;
320 	return (v);
321 }
322 
323 static int
vcmp(a,b)324 vcmp(a, b)
325         const void *a, *b;
326 {
327         return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
328 }
329