NetBSD/bin/ps/keyword.c

273 lines
8.4 KiB
C
Raw Normal View History

/* $NetBSD: keyword.c,v 1.26 2001/01/08 13:20:29 itojun Exp $ */
1995-03-21 12:01:59 +03:00
1993-03-21 12:45:37 +03:00
/*-
1994-05-09 07:31:07 +04:00
* Copyright (c) 1990, 1993, 1994
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-07-21 00:37:53 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1995-03-21 12:01:59 +03:00
#if 0
1994-05-09 07:31:07 +04:00
static char sccsid[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
1995-03-21 12:01:59 +03:00
#else
__RCSID("$NetBSD: keyword.c,v 1.26 2001/01/08 13:20:29 itojun Exp $");
1995-03-21 12:01:59 +03:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
#include <sys/time.h>
#include <sys/proc.h>
1998-02-06 07:47:30 +03:00
#include <sys/resource.h>
#include <sys/sysctl.h>
#include <sys/ucred.h>
1994-05-09 07:31:07 +04:00
#include <err.h>
1993-03-21 12:45:37 +03:00
#include <errno.h>
#include <kvm.h>
1994-05-09 07:31:07 +04:00
#include <stddef.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
1994-05-09 07:31:07 +04:00
1993-03-21 12:45:37 +03:00
#include "ps.h"
1994-05-09 07:31:07 +04:00
static VAR *findvar __P((char *));
static int vcmp __P((const void *, const void *));
1993-03-21 12:45:37 +03:00
#ifdef NOTINUSE
{"stime", "STIME", NULL, 0, cputime}, /* XXX add stime, utime ... */
{"utime", "UTIME", NULL, 0, cputime}, /* ... display to cputime() */
{"idrss", "IDRSS", NULL, 0, pvar, 0, POFF(p_uru_idrss), ULONG, "d"},
{"isrss", "ISRSS", NULL, 0, pvar, 0, POFF(p_uru_isrss), ULONG, "d"},
{"ixrss", "IXRSS", NULL, 0, pvar, 0, POFF(p_uru_ixrss), ULONG, "d"},
1993-03-21 12:45:37 +03:00
#endif
/* Compute offset in common structures. */
#define POFF(x) offsetof(struct kinfo_proc2, x)
1993-03-21 12:45:37 +03:00
#define UIDFMT "u"
#define UID(n1, n2, fn, off) \
{ n1, n2, NULL, 0, fn, 0, off, UINT32, UIDFMT }
#define GID(n1, n2, fn, off) UID(n1, n2, fn, off)
1993-03-21 12:45:37 +03:00
#define PIDFMT "d"
#define PID(n1, n2, fn, off) \
{ n1, n2, NULL, 0, fn, 0, off, INT32, PIDFMT }
1993-03-21 12:45:37 +03:00
VAR var[] = {
{"%cpu", "%CPU", NULL, 0, pcpu},
{"%mem", "%MEM", NULL, 0, pmem},
{"acflag", "ACFLG", NULL, 0, pvar, 0, POFF(p_acflag), USHORT, "x"},
1993-03-21 12:45:37 +03:00
{"acflg", "", "acflag"},
{"blocked", "", "sigmask"},
{"caught", "", "sigcatch"},
{"command", "COMMAND", NULL, COMM|LJUST, command},
{"cpu", "CPU", NULL, 0, pvar, 0, POFF(p_estcpu), UINT, "d"},
1993-03-21 12:45:37 +03:00
{"cputime", "", "time"},
{"f", "F", NULL, 0, pvar, 0, POFF(p_flag), INT, "x"},
1993-03-21 12:45:37 +03:00
{"flags", "", "f"},
{"holdcnt", "HOLDCNT", NULL, 0, pvar, 0, POFF(p_holdcnt), INT, "d"},
1993-03-21 12:45:37 +03:00
{"ignored", "", "sigignore"},
{"inblk", "INBLK", NULL, 0, pvar, 0, POFF(p_uru_inblock), ULONG, "d"},
1993-03-21 12:45:37 +03:00
{"inblock", "", "inblk"},
{"jobc", "JOBC", NULL, 0, pvar, 0, POFF(p_jobc), SHORT, "d"},
{"ktrace", "KTRACE", NULL, 0, pvar, 0, POFF(p_traceflag), INT, "x"},
/* XXX */
{"ktracep", "KTRACEP", NULL, 0, pvar, 0, POFF(p_tracep), KPTR, "llx"},
{"lim", "LIM", NULL, 0, maxrss},
{"login", "LOGIN", NULL, LJUST, logname},
1993-03-21 12:45:37 +03:00
{"logname", "", "login"},
{"lstart", "STARTED", NULL, LJUST, lstarted},
{"majflt", "MAJFLT", NULL, 0, pvar, 0, POFF(p_uru_majflt), ULONG, "d"},
{"minflt", "MINFLT", NULL, 0, pvar, 0, POFF(p_uru_minflt), ULONG, "d"},
{"msgrcv", "MSGRCV", NULL, 0, pvar, 0, POFF(p_uru_msgrcv), ULONG, "d"},
{"msgsnd", "MSGSND", NULL, 0, pvar, 0, POFF(p_uru_msgsnd), ULONG, "d"},
1993-03-21 12:45:37 +03:00
{"ni", "", "nice"},
{"nice", "NI", NULL, 0, pnice},
{"nivcsw", "NIVCSW", NULL, 0, pvar, 0, POFF(p_uru_nivcsw), ULONG, "d"},
1993-03-21 12:45:37 +03:00
{"nsignals", "", "nsigs"},
{"nsigs", "NSIGS", NULL, 0, pvar, 0, POFF(p_uru_nsignals), ULONG, "d"},
{"nswap", "NSWAP", NULL, 0, pvar, 0, POFF(p_uru_nswap), ULONG, "d"},
{"nvcsw", "NVCSW", NULL, 0, pvar, 0, POFF(p_uru_nvcsw), ULONG, "d"},
/* XXX */
{"nwchan", "WCHAN", NULL, 0, pvar, 0, POFF(p_wchan), KPTR, "llx"},
{"oublk", "OUBLK", NULL, 0, pvar, 0, POFF(p_uru_oublock), ULONG, "d"},
1993-03-21 12:45:37 +03:00
{"oublock", "", "oublk"},
/* XXX */
{"p_ru", "P_RU", NULL, 0, pvar, 0, POFF(p_ru), KPTR, "llx"},
/* XXX */
{"paddr", "PADDR", NULL, 0, pvar, 0, POFF(p_paddr), KPTR, "llx"},
{"pagein", "PAGEIN", NULL, 0, pagein},
1993-03-21 12:45:37 +03:00
{"pcpu", "", "%cpu"},
{"pending", "", "sig"},
PID("pgid", "PGID", pvar, POFF(p__pgid)),
PID("pid", "PID", pvar, POFF(p_pid)),
1993-03-21 12:45:37 +03:00
{"pmem", "", "%mem"},
PID("ppid", "PPID", pvar, POFF(p_ppid)),
{"pri", "PRI", NULL, 0, pri},
{"re", "RE", NULL, INF127, pvar, 0, POFF(p_swtime), UINT, "d"},
GID("rgid", "RGID", pvar, POFF(p_rgid)),
/* XXX */
{"rlink", "RLINK", NULL, 0, pvar, 0, POFF(p_back), KPTR, "llx"},
{"rss", "RSS", NULL, 0, p_rssize},
1993-03-21 12:45:37 +03:00
{"rssize", "", "rsz"},
{"rsz", "RSZ", NULL, 0, rssize},
UID("ruid", "RUID", pvar, POFF(p_ruid)),
{"ruser", "RUSER", NULL, LJUST, runame},
{"sess", "SESS", NULL, 0, pvar, 0, POFF(p_sess), KPTR24, "llx"},
PID("sid", "SID", pvar, POFF(p_sid)),
{"sig", "PENDING",
NULL, 0, pvar, 0, POFF(p_siglist), SIGLIST, "s"},
{"sigcatch", "CAUGHT",
NULL, 0, pvar, 0, POFF(p_sigcatch), SIGLIST, "s"},
1993-03-21 12:45:37 +03:00
{"sigignore", "IGNORED",
NULL, 0, pvar, 0, POFF(p_sigignore), SIGLIST, "s"},
{"sigmask", "BLOCKED",
NULL, 0, pvar, 0, POFF(p_sigmask), SIGLIST, "s"},
{"sl", "SL", NULL, INF127, pvar, 0, POFF(p_slptime), UINT, "d"},
{"start", "STARTED", NULL, 0, started},
1993-03-21 12:45:37 +03:00
{"stat", "", "state"},
{"state", "STAT", NULL, LJUST, state},
GID("svgid", "SVGID", pvar, POFF(p_gid)),
UID("svuid", "SVUID", pvar, POFF(p_uid)),
{"tdev", "TDEV", NULL, 0, tdev},
{"time", "TIME", NULL, 0, cputime},
PID("tpgid", "TGPID", pvar, POFF(p_tpgid)),
{"tsess", "TSESS", NULL, 0, pvar, 0, POFF(p_tsess), KPTR, "llx"},
{"tsiz", "TSIZ", NULL, 0, tsize},
{"tt", "TT", NULL, LJUST, tname},
{"tty", "TTY", NULL, LJUST, longtname},
{"ucomm", "UCOMM", NULL, LJUST, ucomm},
UID("uid", "UID", pvar, POFF(p_uid)),
{"upr", "UPR", NULL, 0, pvar, 0, POFF(p_usrpri), UCHAR, "d"},
{"user", "USER", NULL, LJUST, uname},
1993-03-21 12:45:37 +03:00
{"usrpri", "", "upr"},
{"vsize", "", "vsz"},
{"vsz", "VSZ", NULL, 0, vsize},
{"wchan", "WCHAN", NULL, LJUST, wchan},
{"xstat", "XSTAT", NULL, 0, pvar, 0, POFF(p_xstat), USHORT, "x"},
1993-03-21 12:45:37 +03:00
{""},
};
1994-05-09 07:31:07 +04:00
void
1993-03-21 12:45:37 +03:00
showkey()
{
1994-05-09 07:31:07 +04:00
VAR *v;
int i;
char *p, *sep;
1993-03-21 12:45:37 +03:00
i = 0;
sep = "";
for (v = var; *(p = v->name); ++v) {
1994-05-09 07:31:07 +04:00
int len = strlen(p);
1993-03-21 12:45:37 +03:00
if (termwidth && (i += len + 1) > termwidth) {
i = len;
sep = "\n";
}
(void) printf("%s%s", sep, p);
sep = " ";
}
(void) printf("\n");
}
1994-05-09 07:31:07 +04:00
void
1993-03-21 12:45:37 +03:00
parsefmt(p)
char *p;
{
static struct varent *vtail;
#define FMTSEP " \t,\n"
while (p && *p) {
1994-05-09 07:31:07 +04:00
char *cp;
VAR *v;
struct varent *vent;
1993-03-21 12:45:37 +03:00
while ((cp = strsep(&p, FMTSEP)) != NULL && *cp == '\0')
/* void */;
if (cp == NULL || !(v = findvar(cp)))
1993-03-21 12:45:37 +03:00
continue;
if ((vent = malloc(sizeof(struct varent))) == NULL)
err(1, NULL);
1993-03-21 12:45:37 +03:00
vent->var = v;
vent->next = NULL;
if (vhead == NULL)
vhead = vtail = vent;
else {
vtail->next = vent;
vtail = vent;
}
}
if (!vhead)
1994-05-09 07:31:07 +04:00
errx(1, "no valid keywords");
1993-03-21 12:45:37 +03:00
}
static VAR *
findvar(p)
char *p;
{
VAR *v, key;
char *hp;
key.name = p;
1994-05-09 07:31:07 +04:00
hp = strchr(p, '=');
1993-03-21 12:45:37 +03:00
if (hp)
*hp++ = '\0';
key.name = p;
1994-05-09 07:31:07 +04:00
v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
1993-03-21 12:45:37 +03:00
if (v && v->alias) {
if (hp) {
1994-05-09 07:31:07 +04:00
warnx("%s: illegal keyword specification", p);
1993-03-21 12:45:37 +03:00
eval = 1;
}
parsefmt(v->alias);
1994-05-09 07:31:07 +04:00
return ((VAR *)NULL);
1993-03-21 12:45:37 +03:00
}
if (!v) {
1994-05-09 07:31:07 +04:00
warnx("%s: keyword not found", p);
1993-03-21 12:45:37 +03:00
eval = 1;
return ((VAR *)NULL);
1993-03-21 12:45:37 +03:00
}
if (hp)
v->header = hp;
1994-05-09 07:31:07 +04:00
return (v);
1993-03-21 12:45:37 +03:00
}
1994-05-09 07:31:07 +04:00
static int
1993-03-21 12:45:37 +03:00
vcmp(a, b)
1994-05-09 07:31:07 +04:00
const void *a, *b;
1993-03-21 12:45:37 +03:00
{
1994-05-09 07:31:07 +04:00
return (strcmp(((VAR *)a)->name, ((VAR *)b)->name));
1993-03-21 12:45:37 +03:00
}