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