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