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