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