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