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