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