keyword.c revision 1.16 1 /* $NetBSD: keyword.c,v 1.16 1998/02/06 04:47:32 mrg 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.16 1998/02/06 04:47:32 mrg 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 <stddef.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58
59 #include "ps.h"
60
61 static VAR *findvar __P((char *));
62 static int vcmp __P((const void *, const void *));
63
64 #ifdef NOTINUSE
65 int utime(), stime(), ixrss(), idrss(), isrss();
66 {{"utime"}, "UTIME", USER, utime, 4},
67 {{"stime"}, "STIME", USER, stime, 4},
68 {{"ixrss"}, "IXRSS", USER, ixrss, 4},
69 {{"idrss"}, "IDRSS", USER, idrss, 4},
70 {{"isrss"}, "ISRSS", USER, isrss, 4},
71 #endif
72
73 /* Compute offset in common structures. */
74 #define POFF(x) offsetof(struct proc, x)
75 #define EOFF(x) offsetof(struct eproc, x)
76 #define UOFF(x) offsetof(struct usave, x)
77 #define ROFF(x) offsetof(struct rusage, x)
78
79 #define UIDFMT "u"
80 #define UIDLEN 5
81 #define UID(n1, n2, fn, off) \
82 { n1, n2, NULL, 0, fn, UIDLEN, off, UINT32, UIDFMT }
83 #define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
84
85 #define PIDFMT "d"
86 #define PIDLEN 5
87 #define PID(n1, n2, fn, off) \
88 { n1, n2, NULL, 0, fn, PIDLEN, off, INT32, PIDFMT }
89
90 #define USERLEN 8
91
92 VAR var[] = {
93 {"%cpu", "%CPU", NULL, 0, pcpu, 4},
94 {"%mem", "%MEM", NULL, 0, pmem, 4},
95 {"acflag", "ACFLG", NULL, 0, pvar, 3, POFF(p_acflag), USHORT, "x"},
96 {"acflg", "", "acflag"},
97 {"blocked", "", "sigmask"},
98 {"caught", "", "sigcatch"},
99 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
100 {"cpu", "CPU", NULL, 0, pvar, 3, POFF(p_estcpu), UINT, "d"},
101 {"cputime", "", "time"},
102 {"f", "F", NULL, 0, pvar, 7, POFF(p_flag), INT, "x"},
103 {"flags", "", "f"},
104 {"holdcnt", "HOLDCNT", NULL, 0, pvar, 8, POFF(p_holdcnt), INT, "d"},
105 {"ignored", "", "sigignore"},
106 {"inblk", "INBLK", NULL, USER, rvar, 4, ROFF(ru_inblock), LONG, "d"},
107 {"inblock", "", "inblk"},
108 {"jobc", "JOBC", NULL, 0, evar, 4, EOFF(e_jobc), SHORT, "d"},
109 {"ktrace", "KTRACE", NULL, 0, pvar, 8, POFF(p_traceflag), INT, "x"},
110 /* XXX */
111 {"ktracep", "KTRACEP", NULL, 0, pvar, 8, POFF(p_tracep), KPTR, "x"},
112 {"lim", "LIM", NULL, 0, maxrss, 5},
113 {"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
114 {"logname", "", "login"},
115 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
116 {"majflt", "MAJFLT", NULL, USER, rvar, 4, ROFF(ru_majflt), LONG, "d"},
117 {"minflt", "MINFLT", NULL, USER, rvar, 4, ROFF(ru_minflt), LONG, "d"},
118 {"msgrcv", "MSGRCV", NULL, USER, rvar, 4, ROFF(ru_msgrcv), LONG, "d"},
119 {"msgsnd", "MSGSND", NULL, USER, rvar, 4, ROFF(ru_msgsnd), LONG, "d"},
120 {"ni", "", "nice"},
121 {"nice", "NI", NULL, 0, pnice, 2},
122 {"nivcsw", "NIVCSW", NULL, USER, rvar, 5, ROFF(ru_nivcsw), LONG, "d"},
123 {"nsignals", "", "nsigs"},
124 {"nsigs", "NSIGS", NULL, USER, rvar, 4, ROFF(ru_nsignals), LONG, "d"},
125 {"nswap", "NSWAP", NULL, USER, rvar, 4, ROFF(ru_nswap), LONG, "d"},
126 {"nvcsw", "NVCSW", NULL, USER, rvar, 5, ROFF(ru_nvcsw), LONG, "d"},
127 /* XXX */
128 {"nwchan", "WCHAN", NULL, 0, pvar, 6, POFF(p_wchan), KPTR, "x"},
129 {"oublk", "OUBLK", NULL, USER, rvar, 4, ROFF(ru_oublock), LONG, "d"},
130 {"oublock", "", "oublk"},
131 /* XXX */
132 {"p_ru", "P_RU", NULL, 0, pvar, 6, POFF(p_ru), KPTR, "x"},
133 /* XXX */
134 {"paddr", "PADDR", NULL, 0, evar, 6, EOFF(e_paddr), KPTR, "x"},
135 {"pagein", "PAGEIN", NULL, USER, pagein, 6},
136 {"pcpu", "", "%cpu"},
137 {"pending", "", "sig"},
138 PID("pgid", "PGID", evar, EOFF(e_pgid)),
139 PID("pid", "PID", pvar, POFF(p_pid)),
140 {"pmem", "", "%mem"},
141 PID("ppid", "PPID", evar, EOFF(e_ppid)),
142 {"pri", "PRI", NULL, 0, pri, 3},
143 {"re", "RE", NULL, INF127, pvar, 3, POFF(p_swtime), UINT, "d"},
144 GID("rgid", "RGID", evar, EOFF(e_pcred.p_rgid)),
145 /* XXX */
146 {"rlink", "RLINK", NULL, 0, pvar, 8, POFF(p_back), KPTR, "x"},
147 {"rss", "RSS", NULL, 0, p_rssize, 4},
148 {"rssize", "", "rsz"},
149 {"rsz", "RSZ", NULL, 0, rssize, 4},
150 UID("ruid", "RUID", evar, EOFF(e_pcred.p_ruid)),
151 {"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
152 {"sess", "SESS", NULL, 0, evar, 6, EOFF(e_sess), KPTR, "x"},
153 {"sig", "PENDING", NULL, 0, pvar, 8, POFF(p_siglist), INT, "x"},
154 {"sigcatch", "CAUGHT", NULL, 0, pvar, 8, POFF(p_sigcatch), UINT, "x"},
155 {"sigignore", "IGNORED",
156 NULL, 0, pvar, 8, POFF(p_sigignore), UINT, "x"},
157 {"sigmask", "BLOCKED", NULL, 0, pvar, 8, POFF(p_sigmask), UINT, "x"},
158 {"sl", "SL", NULL, INF127, pvar, 3, POFF(p_slptime), UINT, "d"},
159 {"start", "STARTED", NULL, LJUST|USER, started, 8},
160 {"stat", "", "state"},
161 {"state", "STAT", NULL, 0, state, 4},
162 GID("svgid", "SVGID", evar, EOFF(e_pcred.p_svgid)),
163 UID("svuid", "SVUID", evar, EOFF(e_pcred.p_svuid)),
164 {"tdev", "TDEV", NULL, 0, tdev, 4},
165 {"time", "TIME", NULL, USER, cputime, 9},
166 PID("tpgid", "TGPID", evar, EOFF(e_tpgid)),
167 {"tsess", "TSESS", NULL, 0, evar, 6, EOFF(e_tsess), KPTR, "x"},
168 {"tsiz", "TSIZ", NULL, 0, tsize, 4},
169 {"tt", "TT", NULL, LJUST, tname, 3},
170 {"tty", "TTY", NULL, LJUST, longtname, 8},
171 {"ucomm", "UCOMM", NULL, LJUST, ucomm, MAXCOMLEN},
172 UID("uid", "UID", evar, EOFF(e_ucred.cr_uid)),
173 {"upr", "UPR", NULL, 0, pvar, 3, POFF(p_usrpri), UCHAR, "d"},
174 {"user", "USER", NULL, LJUST, uname, USERLEN},
175 {"usrpri", "", "upr"},
176 {"vsize", "", "vsz"},
177 {"vsz", "VSZ", NULL, 0, vsize, 5},
178 {"wchan", "WCHAN", NULL, LJUST, wchan, 6},
179 {"xstat", "XSTAT", NULL, 0, pvar, 4, POFF(p_xstat), USHORT, "x"},
180 {""},
181 };
182
183 void
184 showkey()
185 {
186 VAR *v;
187 int i;
188 char *p, *sep;
189
190 i = 0;
191 sep = "";
192 for (v = var; *(p = v->name); ++v) {
193 int len = strlen(p);
194 if (termwidth && (i += len + 1) > termwidth) {
195 i = len;
196 sep = "\n";
197 }
198 (void) printf("%s%s", sep, p);
199 sep = " ";
200 }
201 (void) printf("\n");
202 }
203
204 void
205 parsefmt(p)
206 char *p;
207 {
208 static struct varent *vtail;
209
210 #define FMTSEP " \t,\n"
211 while (p && *p) {
212 char *cp;
213 VAR *v;
214 struct varent *vent;
215
216 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
217 /* void */;
218 if (!(v = findvar(cp)))
219 continue;
220 if ((vent = malloc(sizeof(struct varent))) == NULL)
221 err(1, "%s", "");
222 vent->var = v;
223 vent->next = NULL;
224 if (vhead == NULL)
225 vhead = vtail = vent;
226 else {
227 vtail->next = vent;
228 vtail = vent;
229 }
230 }
231 if (!vhead)
232 errx(1, "no valid keywords");
233 }
234
235 static VAR *
236 findvar(p)
237 char *p;
238 {
239 VAR *v, key;
240 char *hp;
241
242 key.name = p;
243
244 hp = strchr(p, '=');
245 if (hp)
246 *hp++ = '\0';
247
248 key.name = p;
249 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
250
251 if (v && v->alias) {
252 if (hp) {
253 warnx("%s: illegal keyword specification", p);
254 eval = 1;
255 }
256 parsefmt(v->alias);
257 return ((VAR *)NULL);
258 }
259 if (!v) {
260 warnx("%s: keyword not found", p);
261 eval = 1;
262 return ((VAR *)NULL);
263 }
264 if (hp)
265 v->header = hp;
266 return (v);
267 }
268
269 static int
270 vcmp(a, b)
271 const void *a, *b;
272 {
273 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
274 }
275