sync
[bitrig.git] / bin / ps / keyword.c
blob21eac632377467357b52735362be50b3de22ca1c
1 /* $OpenBSD: keyword.c,v 1.37 2012/04/11 17:33:26 guenther Exp $ */
2 /* $NetBSD: keyword.c,v 1.12.6.1 1996/05/30 21:25:13 cgd Exp $ */
4 /*-
5 * Copyright (c) 1990, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/resource.h>
36 #include <sys/proc.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #include "ps.h"
47 #include <sys/ucred.h>
48 #include <sys/sysctl.h>
50 int needheader;
52 static VAR *findvar(char *);
53 static int vcmp(const void *, const void *);
55 #ifdef NOTINUSE
56 int utime(), stime(), ixrss(), idrss(), isrss();
57 {{"utime"}, "UTIME", USER, utime, 4},
58 {{"stime"}, "STIME", USER, stime, 4},
59 {{"ixrss"}, "IXRSS", USER, ixrss, 4},
60 {{"idrss"}, "IDRSS", USER, idrss, 4},
61 {{"isrss"}, "ISRSS", USER, isrss, 4},
62 #endif
64 /* Compute offset in common structures. */
65 #define POFF(x) offsetof(struct kinfo_proc, x)
67 #define UIDFMT "u"
68 #define UIDLEN 5
69 #define UID(n1, n2, fn, off) \
70 { n1, n2, NULL, 0, fn, UIDLEN, 0, off, UINT32, UIDFMT }
71 #define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
73 #define PIDFMT "d"
74 #define PIDLEN 5
75 #define PID(n1, n2, fn, off) \
76 { n1, n2, NULL, 0, fn, PIDLEN, 0, off, INT32, PIDFMT }
78 #define TIDFMT "d"
79 #define TIDLEN 7
80 #define TID(n1, n2, fn, off) \
81 { n1, n2, NULL, 0, fn, TIDLEN, 0, off, INT32, TIDFMT }
83 #define USERLEN 8
84 #define CWDLEN 40
86 /* Bit types must match their respective entries in struct kinfo_proc */
87 VAR var[] = {
88 {"%cpu", "%CPU", NULL, NLIST, pcpu, 4},
89 {"%mem", "%MEM", NULL, NLIST, pmem, 4},
90 {"acflag", "ACFLG", NULL, 0, pvar, 3, 0, POFF(p_acflag), UINT16, "x"},
91 {"acflg", "", "acflag"},
92 {"args", "", "command"},
93 {"blocked", "", "sigmask"},
94 {"caught", "", "sigcatch"},
95 {"comm", "", "ucomm"},
96 {"command", "COMMAND", NULL, COMM|LJUST|USER, command, 16},
97 {"cpu", "CPU", NULL, 0, pvar, 3, 0, POFF(p_estcpu), UINT32, "d"},
98 {"cpuid", "CPUID", NULL, 0, pvar, 8, 0, POFF(p_cpuid), UINT64, "lld"},
99 {"cputime", "", "time"},
100 {"cwd", "CWD", NULL, LJUST, curwd, CWDLEN},
101 {"dsiz", "DSIZ", NULL, 0, dsize, 4},
102 {"emul", "EMUL", NULL, LJUST, emulname, KI_EMULNAMELEN - 1},
103 {"etime", "", "start"},
104 {"f", "F", NULL, 0, pvar, 7, 0, POFF(p_flag), INT32, "x"},
105 {"flags", "", "f"},
106 GID("gid", "GID", pvar, POFF(p_gid)),
107 {"group", "GROUP", NULL, LJUST, gname, USERLEN},
108 {"ignored", "", "sigignore"},
109 {"inblk", "INBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_inblock), UINT64, "lld"},
110 {"inblock", "", "inblk"},
111 {"jobc", "JOBC", NULL, 0, pvar, 4, 0, POFF(p_jobc), INT16, "d"},
112 {"ktrace", "KTRACE", NULL, 0, pvar, 8, 0, POFF(p_traceflag), INT32, "x"},
113 /* XXX */
114 {"ktracep", "KTRACEP", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tracep), UINT64, "llx"},
115 {"lim", "LIM", NULL, 0, maxrss, 5},
116 {"login", "LOGIN", NULL, LJUST, logname, MAXLOGNAME},
117 {"logname", "", "login"},
118 {"lstart", "STARTED", NULL, LJUST|USER, lstarted, 28},
119 {"majflt", "MAJFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_majflt), UINT64, "lld"},
120 {"minflt", "MINFLT", NULL, USER, pvar, 4, 0, POFF(p_uru_minflt), UINT64, "lld"},
121 {"maxrss", "MAXRSS", NULL, USER, pvar, 4, 0, POFF(p_uru_maxrss), UINT64, "lld"},
122 {"msgrcv", "MSGRCV", NULL, USER, pvar, 4, 0, POFF(p_uru_msgrcv), UINT64, "lld"},
123 {"msgsnd", "MSGSND", NULL, USER, pvar, 4, 0, POFF(p_uru_msgsnd), UINT64, "lld"},
124 {"ni", "", "nice"},
125 {"nice", "NI", NULL, 0, pnice, 3},
126 {"nivcsw", "NIVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nivcsw), UINT64, "lld"},
127 {"nsignals", "", "nsigs"},
128 {"nsigs", "NSIGS", NULL, USER, pvar, 4, 0, POFF(p_uru_nsignals), UINT64, "lld"},
129 {"nswap", "NSWAP", NULL, USER, pvar, 4, 0, POFF(p_uru_nswap), UINT64, "lld"},
130 {"nvcsw", "NVCSW", NULL, USER, pvar, 5, 0, POFF(p_uru_nvcsw), UINT64, "lld"},
131 /* XXX */
132 {"nwchan", "WCHAN", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_wchan), UINT64, "llx"},
133 {"oublk", "OUBLK", NULL, USER, pvar, 4, 0, POFF(p_uru_oublock), UINT64, "lld"},
134 {"oublock", "", "oublk"},
135 /* XXX */
136 {"p_ru", "P_RU", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_ru), UINT64, "llx"},
137 /* XXX */
138 {"paddr", "PADDR", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_paddr), UINT64, "llx"},
139 {"pagein", "PAGEIN", NULL, USER, pagein, 6},
140 {"pcpu", "", "%cpu"},
141 {"pending", "", "sig"},
142 PID("pgid", "PGID", pvar, POFF(p__pgid)),
143 PID("pid", "PID", pvar, POFF(p_pid)),
144 {"pmem", "", "%mem"},
145 PID("ppid", "PPID", pvar, POFF(p_ppid)),
146 {"pri", "PRI", NULL, 0, pri, 3},
147 {"re", "RE", NULL, INF127, pvar, 3, 0, POFF(p_swtime), UINT32, "u"},
148 GID("rgid", "RGID", pvar, POFF(p_rgid)),
149 /* XXX */
150 {"rgroup", "RGROUP", NULL, LJUST, rgname, USERLEN},
151 {"rlink", "RLINK", NULL, 0, pvar, 8, 0, POFF(p_back), UINT64, "llx"},
152 {"rss", "RSS", NULL, 0, p_rssize, 5},
153 {"rssize", "", "rsz"},
154 {"rsz", "RSZ", NULL, 0, rssize, 4},
155 {"rtable", "RTABLE", NULL, 0, pvar, 0, 0, POFF(p_rtableid), INT32, "d"},
156 UID("ruid", "RUID", pvar, POFF(p_ruid)),
157 {"ruser", "RUSER", NULL, LJUST, runame, USERLEN},
158 {"sess", "SESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_sess), UINT64, "llx"},
159 {"sig", "PENDING", NULL, 0, pvar, 8, 0, POFF(p_siglist), INT32, "x"},
160 {"sigcatch", "CAUGHT", NULL, 0, pvar, 8, 0, POFF(p_sigcatch), UINT32, "x"},
161 {"sigignore", "IGNORED",
162 NULL, 0, pvar, 8, 0, POFF(p_sigignore), UINT32, "x"},
163 {"sigmask", "BLOCKED", NULL, 0, pvar, 8, 0, POFF(p_sigmask), UINT32, "x"},
164 {"sl", "SL", NULL, INF127, pvar, 3, 0, POFF(p_slptime), UINT32, "u"},
165 {"ssiz", "SSIZ", NULL, 0, ssize, 4},
166 {"start", "STARTED", NULL, LJUST|USER, started, 8},
167 {"stat", "", "state"},
168 {"state", "STAT", NULL, LJUST|NLIST, state, 5},
169 GID("svgid", "SVGID", pvar, POFF(p_svgid)),
170 UID("svuid", "SVUID", pvar, POFF(p_svuid)),
171 {"tdev", "TDEV", NULL, 0, tdev, 4},
172 TID("tid", "TID", pvar, POFF(p_tid)),
173 {"time", "TIME", NULL, USER, cputime, 9},
174 PID("tpgid", "TPGID", pvar, POFF(p_tpgid)),
175 {"tsess", "TSESS", NULL, 0, pvar, PTRWIDTH, 0, POFF(p_tsess), UINT64, "llx"},
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", pvar, POFF(p_uid)),
181 {"upr", "UPR", NULL, 0, pvar, 3, 0, POFF(p_usrpri), UINT8, "d"},
182 {"user", "USER", NULL, LJUST, euname, USERLEN},
183 {"usrpri", "", "upr"},
184 {"vsize", "", "vsz"},
185 {"vsz", "VSZ", NULL, 0, vsize, 5},
186 {"wchan", "WCHAN", NULL, LJUST, wchan, KI_WMESGLEN - 1},
187 {"xstat", "XSTAT", NULL, 0, pvar, 4, 0, POFF(p_xstat), UINT16, "x"},
188 {""},
191 void
192 showkey(void)
194 VAR *v;
195 int i;
196 char *p, *sep;
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";
206 (void) printf("%s%s", sep, p);
207 sep = " ";
209 (void) printf("\n");
212 void
213 parsefmt(char *p)
215 static struct varent *vtail;
217 #define FMTSEP " \t,\n"
218 while (p && *p) {
219 char *cp;
220 VAR *v;
221 struct varent *vent;
223 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
224 /* void */;
225 if (!cp)
226 break;
227 if (!(v = findvar(cp)) || v->parsed == 1)
228 continue;
229 v->parsed = 1;
230 if ((vent = malloc(sizeof(struct varent))) == NULL)
231 err(1, NULL);
232 vent->var = v;
233 vent->next = NULL;
234 if (vhead == NULL)
235 vhead = vtail = vent;
236 else {
237 vtail->next = vent;
238 vtail = vent;
240 needheader |= v->header[0] != '\0';
242 if (!vhead)
243 errx(1, "no valid keywords");
246 static VAR *
247 findvar(char *p)
249 VAR *v, key;
250 char *hp;
251 int vcmp(const void *, const void *);
253 key.name = p;
255 hp = strchr(p, '=');
256 if (hp)
257 *hp++ = '\0';
259 key.name = p;
260 v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
262 if (v && v->alias) {
263 if (hp) {
264 warnx("%s: illegal keyword specification", p);
265 eval = 1;
267 parsefmt(v->alias);
268 return (NULL);
270 if (!v) {
271 warnx("%s: keyword not found", p);
272 eval = 1;
273 return (NULL);
275 if (hp)
276 v->header = hp;
277 return (v);
280 static int
281 vcmp(const void *a, const void *b)
283 return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));