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