Merge from vendor branch BSDTAR:
[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.12 2004/11/07 19:29:27 liamfoy Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <sys/sysctl.h>
42 #include <sys/ucred.h>
43 #include <sys/user.h>
44
45 #include <err.h>
46 #include <errno.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <utmp.h>
52
53 #include "ps.h"
54
55 static VAR *findvar (char *);
56 static int  vcmp (const void *, const void *);
57
58 #ifdef NOTINUSE
59 int     utime(), stime(), ixrss(), idrss(), isrss();
60         {{"utime"}, "UTIME", USER, utime, NULL, 4},
61         {{"stime"}, "STIME", USER, stime, NULL, 4},
62         {{"ixrss"}, "IXRSS", USER, ixrss, NULL, 4},
63         {{"idrss"}, "IDRSS", USER, idrss, NULL, 4},
64         {{"isrss"}, "ISRSS", USER, isrss, NULL, 4},
65 #endif
66
67 /* Compute offset in common structures. */
68 #define POFF(x) offsetof(struct proc, x)
69 #define TOFF(x) offsetof(struct thread, x)
70 #define EOFF(x) offsetof(struct eproc, x)
71 #define UOFF(x) offsetof(struct usave, x)
72 #define ROFF(x) offsetof(struct rusage, x)
73
74 #define UIDFMT  "u"
75 #define UIDLEN  5
76 #define PIDFMT  "d"
77 #define PIDLEN  5
78 #define USERLEN UT_NAMESIZE
79
80 VAR var[] = {
81         {"%cpu", "%CPU", NULL, 0, pcpu, NULL, 4},
82         {"%mem", "%MEM", NULL, 0, pmem, NULL, 4},
83         {"acflag", "ACFLG",
84                 NULL, 0, pvar, NULL, 3, POFF(p_acflag), USHORT, "x"},
85         {"acflg", "", "acflag"},
86         {"blocked", "", "sigmask"},
87         {"caught", "", "sigcatch"},
88         {"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16},
89         {"cpu", "CPU", NULL, 0, pvar, NULL, 3, POFF(p_estcpu), UINT, "d"},
90         {"lastcpu", "C", NULL, 0, pvar, NULL, 3, EOFF(e_cpuid), UINT, "d"},
91         {"cputime", "", "time"},
92         {"f", "F", NULL, 0, pvar, NULL, 7, POFF(p_flag), INT, "x"},
93         {"flags", "", "f"},
94         {"iac", "IAC", 
95                 NULL, 0, pvar, NULL, 4, POFF(p_interactive), CHAR, PIDFMT},
96         {"ignored", "", "sigignore"},
97         {"inblk", "INBLK",
98                 NULL, USER, rvar, NULL, 4, ROFF(ru_inblock), LONG, "ld"},
99         {"inblock", "", "inblk"},
100         {"jobc", "JOBC", NULL, 0, evar, NULL, 4, EOFF(e_jobc), SHORT, "d"},
101         {"ktrace", "KTRACE",
102                 NULL, 0, pvar, NULL, 8, POFF(p_traceflag), INT, "x"},
103         {"ktracep", "KTRACEP",
104                 NULL, 0, pvar, NULL, 8, POFF(p_tracep), LONG, "lx"},
105         {"lim", "LIM", NULL, 0, maxrss, NULL, 5},
106         {"login", "LOGIN", NULL, LJUST, logname, NULL, MAXLOGNAME-1},
107         {"logname", "", "login"},
108         {"lstart", "STARTED", NULL, LJUST|USER, lstarted, NULL, 28},
109         {"majflt", "MAJFLT",
110                 NULL, USER, rvar, NULL, 4, ROFF(ru_majflt), LONG, "ld"},
111         {"minflt", "MINFLT",
112                 NULL, USER, rvar, NULL, 4, ROFF(ru_minflt), LONG, "ld"},
113         {"msgrcv", "MSGRCV",
114                 NULL, USER, rvar, NULL, 4, ROFF(ru_msgrcv), LONG, "ld"},
115         {"msgsnd", "MSGSND",
116                 NULL, USER, rvar, NULL, 4, ROFF(ru_msgsnd), LONG, "ld"},
117         {"ni", "", "nice"},
118         {"nice", "NI", NULL, 0, pnice, NULL, 3 },
119         {"nivcsw", "NIVCSW",
120                 NULL, USER, rvar, NULL, 5, ROFF(ru_nivcsw), LONG, "ld"},
121         {"nsignals", "", "nsigs"},
122         {"nsigs", "NSIGS",
123                 NULL, USER, rvar, NULL, 4, ROFF(ru_nsignals), LONG, "ld"},
124         {"nswap", "NSWAP",
125                 NULL, USER, rvar, NULL, 4, ROFF(ru_nswap), LONG, "ld"},
126         {"nvcsw", "NVCSW",
127                 NULL, USER, rvar, NULL, 5, ROFF(ru_nvcsw), LONG, "ld"},
128         {"nwchan", "WCHAN", NULL, 0, tvar, NULL, 8, TOFF(td_wchan), KPTR, "lx"},
129         {"oublk", "OUBLK",
130                 NULL, USER, rvar, NULL, 4, ROFF(ru_oublock), LONG, "ld"},
131         {"oublock", "", "oublk"},
132         {"p_ru", "P_RU", NULL, 0, pvar, NULL, 6, POFF(p_ru), KPTR, "lx"},
133         {"paddr", "PADDR", NULL, 0, evar, NULL, 8, EOFF(e_paddr), KPTR, "lx"},
134         {"pagein", "PAGEIN", NULL, USER, pagein, NULL, 6},
135         {"pcpu", "", "%cpu"},
136         {"pending", "", "sig"},
137         {"pgid", "PGID",
138                 NULL, 0, evar, NULL, PIDLEN, EOFF(e_pgid), UINT, PIDFMT},
139         {"pid", "PID", NULL, 0, pvar, NULL, PIDLEN, POFF(p_pid), UINT, PIDFMT},
140         {"pmem", "", "%mem"},
141         {"ppid", "PPID",
142                 NULL, 0, evar, NULL, PIDLEN, EOFF(e_ppid), UINT, PIDFMT},
143         {"pri", "PRI", NULL, 0, pri, NULL, 3},
144         {"re", "RE", NULL, 0, pvar, NULL, 3, POFF(p_swtime), UINT, "d"},
145         {"rgid", "RGID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_rgid),
146                 UINT, UIDFMT},
147         {"rlink", "RLINK",
148                 NULL, 0, pvar, NULL, 8, POFF(p_procq.tqe_prev), KPTR, "lx"},
149         {"rss", "RSS", NULL, 0, p_rssize, NULL, 4},
150         {"rssize", "", "rsz"},
151         {"rsz", "RSZ", NULL, 0, rssize, NULL, 4},
152         {"rtprio", "RTPRIO", NULL, 0, rtprior, NULL, 7, POFF(p_rtprio)},
153         {"ruid", "RUID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_ruid),
154                 UINT, UIDFMT},
155         {"ruser", "RUSER", NULL, LJUST|DSIZ, runame, s_runame, USERLEN},
156         {"sess", "SESS", NULL, 0, evar, NULL, 6, EOFF(e_sess), KPTR, "lx"},
157         {"sig", "PENDING", NULL, 0, pvar, NULL, 8, POFF(p_siglist), INT, "x"},
158         {"sigcatch", "CAUGHT",
159                 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigcatch), UINT, "x"},
160         {"sigignore", "IGNORED",
161                 NULL, 0, evar, NULL, 8, EOFF(e_procsig.ps_sigignore), UINT, "x"},
162         {"sigmask", "BLOCKED",
163                 NULL, 0, pvar, NULL, 8, POFF(p_sigmask), UINT, "x"},
164         {"sl", "SL", NULL, 0, pvar, NULL, 3, POFF(p_slptime), UINT, "d"},
165         {"start", "STARTED", NULL, LJUST|USER, started, NULL, 7},
166         {"stat", "", "state"},
167         {"state", "STAT", NULL, 0, state, NULL, 4},
168         {"svgid", "SVGID", NULL, 0,
169                 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svgid), UINT, UIDFMT},
170         {"svuid", "SVUID", NULL, 0,
171                 evar, NULL, UIDLEN, EOFF(e_ucred.cr_svuid), UINT, UIDFMT},
172         {"tdev", "TDEV", NULL, 0, tdev, NULL, 4},
173         {"time", "TIME", NULL, USER, cputime, NULL, 9},
174         {"tpgid", "TPGID",
175                 NULL, 0, evar, NULL, 4, EOFF(e_tpgid), UINT, PIDFMT},
176         {"tsess", "TSESS", NULL, 0, evar, NULL, 6, EOFF(e_tsess), KPTR, "lx"},
177         {"tsiz", "TSIZ", NULL, 0, tsize, NULL, 4},
178         {"tt", "TT ", NULL, 0, tname, NULL, 4},
179         {"tty", "TTY", NULL, LJUST, longtname, NULL, 8},
180         {"ucomm", "UCOMM", NULL, LJUST, ucomm, NULL, MAXCOMLEN},
181         {"uid", "UID", NULL, 0, evar, NULL, UIDLEN, EOFF(e_ucred.cr_uid),
182                 UINT, UIDFMT},
183         {"user", "USER", NULL, LJUST|DSIZ, uname, s_uname, USERLEN},
184         {"vsize", "", "vsz"},
185         {"vsz", "VSZ", NULL, 0, vsize, NULL, 5},
186         {"wchan", "WCHAN", NULL, LJUST, wchan, NULL, 6},
187         {"xstat", "XSTAT", NULL, 0, pvar, NULL, 4, POFF(p_xstat), USHORT, "x"},
188         {""},
189 };
190
191 void
192 showkey(void)
193 {
194         VAR *v;
195         int i;
196         const char *p, *sep;
197
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";
205                 }
206                 printf("%s%s", sep, p);
207                 sep = " ";
208         }
209         printf("\n");
210 }
211
212 void
213 parsefmt(const char *fmt)
214 {
215         static struct varent *vtail;
216         char *p, *op;
217
218         op = p = strdup(fmt);
219
220         if (p == NULL)
221                 errx(1, "Not enough memory");
222
223 #define FMTSEP  " \t,\n"
224         while (p && *p) {
225                 char *cp;
226                 VAR *v;
227                 struct varent *vent;
228
229                 while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
230                         /* void */;
231                 if (cp == NULL || !(v = findvar(cp)))
232                         continue;
233                 if ((vent = malloc(sizeof(struct varent))) == NULL)
234                         err(1, NULL);
235                 vent->var = v;
236                 vent->next = NULL;
237                 if (vhead == NULL)
238                         vhead = vtail = vent;
239                 else {
240                         vtail->next = vent;
241                         vtail = vent;
242                 }
243         }
244         if (!vhead) {
245                 warnx("no valid keywords; valid keywords:");
246                 showkey();
247                 exit(1);
248         }
249
250         free(op);
251 }
252
253 static VAR *
254 findvar(char *p)
255 {
256         VAR *v, key;
257         char *hp;
258
259         hp = strchr(p, '=');
260         if (hp)
261                 *hp++ = '\0';
262
263         key.name = p;
264         v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
265
266         if (v && v->alias) {
267                 if (hp) {
268                         warnx("%s: illegal keyword specification", p);
269                         eval = 1;
270                 }
271                 parsefmt(v->alias);
272                 return ((VAR *)NULL);
273         }
274         if (!v) {
275                 warnx("%s: keyword not found", p);
276                 eval = 1;
277         } else if (hp)
278                 v->header = hp;
279         return (v);
280 }
281
282 static int
283 vcmp(const void *a, const void *b)
284 {
285         return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
286 }