2007-09-01 20:54:39 +04:00
|
|
|
/* $NetBSD: keyword.c,v 1.51 2007/09/01 16:54:39 yamt 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.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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
|
2007-09-01 20:54:39 +04:00
|
|
|
__RCSID("$NetBSD: keyword.c,v 1.51 2007/09/01 16:54:39 yamt 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>
|
2003-01-18 13:52:16 +03:00
|
|
|
#include <sys/lwp.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#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>
|
1999-12-03 05:26:36 +03:00
|
|
|
#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>
|
1999-04-16 17:34:32 +04:00
|
|
|
#include <signal.h>
|
1994-05-09 07:31:07 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "ps.h"
|
|
|
|
|
2003-03-06 12:04:21 +03:00
|
|
|
static VAR *findvar(const char *);
|
|
|
|
static int vcmp(const void *, const void *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2003-03-06 12:04:21 +03:00
|
|
|
#if 0 /* kernel doesn't calculate these */
|
|
|
|
PUVAR("idrss", "IDRSS", 0, p_uru_idrss, UINT64, PRIu64),
|
|
|
|
PUVAR("isrss", "ISRSS", 0, p_uru_isrss, UINT64, PRId64),
|
|
|
|
PUVAR("ixrss", "IXRSS", 0, p_uru_ixrss, UINT64, PRId64),
|
|
|
|
PUVAR("maxrss", "MAXRSS", 0, p_uru_maxrss, UINT64, PRIu64),
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Compute offset in common structures. */
|
2000-05-26 07:04:28 +04:00
|
|
|
#define POFF(x) offsetof(struct kinfo_proc2, x)
|
2003-01-18 13:52:16 +03:00
|
|
|
#define LOFF(x) offsetof(struct kinfo_lwp, x)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#define UIDFMT "u"
|
2006-10-16 04:31:47 +04:00
|
|
|
#define UID(n1, n2, of) \
|
|
|
|
{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
|
|
|
|
.off = POFF(of), .type = UINT32, .fmt = UIDFMT }
|
2003-03-06 12:04:21 +03:00
|
|
|
#define GID(n1, n2, off) UID(n1, n2, off)
|
1995-09-30 00:58:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#define PIDFMT "d"
|
2006-10-16 04:31:47 +04:00
|
|
|
#define PID(n1, n2, of) \
|
|
|
|
{ .name = n1, .header = n2, .flag = 0, .oproc = pvar, \
|
|
|
|
.off = POFF(of), .type = INT32, .fmt = PIDFMT }
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-10-16 04:31:47 +04:00
|
|
|
#define LVAR(n1, n2, fl, of, ty, fm) \
|
|
|
|
{ .name = n1, .header = n2, .flag = (fl) | LWP, .oproc = pvar, \
|
|
|
|
.off = LOFF(of), .type = ty, .fmt = fm }
|
|
|
|
#define PVAR(n1, n2, fl, of, ty, fm) \
|
|
|
|
{ .name = n1, .header = n2, .flag = (fl) | 0, .oproc = pvar, \
|
|
|
|
.off = POFF(of), .type = ty, .fmt = fm }
|
|
|
|
#define PUVAR(n1, n2, fl, of, ty, fm) \
|
|
|
|
{ .name = n1, .header = n2, .flag = (fl) | UAREA, .oproc = pvar, \
|
|
|
|
.off = POFF(of), .type = ty, .fmt = fm }
|
|
|
|
#define VAR3(n1, n2, fl) \
|
|
|
|
{ .name = n1, .header = n2, .flag = fl }
|
|
|
|
#define VAR4(n1, n2, fl, op) \
|
|
|
|
{ .name = n1, .header = n2, .flag = fl, .oproc = op, }
|
|
|
|
#define VAR6(n1, n2, fl, op, of, ty) \
|
|
|
|
{ .name = n1, .header = n2, .flag = fl, .oproc = op, \
|
|
|
|
.off = of, .type = ty }
|
2003-03-06 12:04:21 +03:00
|
|
|
|
|
|
|
/* NB: table must be sorted, in vi use:
|
|
|
|
* :/^VAR/,/end_sort/! sort -t\" +1
|
|
|
|
* breaking long lines just makes the sort harder
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
*
|
|
|
|
* We support all the fields required by P1003.1-2004 (SUSv3), with
|
|
|
|
* the correct default headers, except for the "tty" field, where the
|
|
|
|
* standard says the header should be "TT", but we have "TTY".
|
2003-03-06 12:04:21 +03:00
|
|
|
*/
|
1993-03-21 12:45:37 +03:00
|
|
|
VAR var[] = {
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("%cpu", "%CPU", 0, pcpu, 0, PCPU),
|
|
|
|
VAR6("%mem", "%MEM", 0, pmem, POFF(p_vm_rssize), INT32),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("acflag", "ACFLG", 0, p_acflag, USHORT, "x"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("acflg", "acflag", ALIAS),
|
|
|
|
VAR3("args", "command", ALIAS),
|
|
|
|
VAR3("blocked", "sigmask", ALIAS),
|
|
|
|
VAR3("caught", "sigcatch", ALIAS),
|
|
|
|
VAR4("comm", "COMMAND", COMM|ARGV0|LJUST, command),
|
|
|
|
VAR4("command", "COMMAND", COMM|LJUST, command),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("cpu", "CPU", 0, p_estcpu, UINT, "u"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("cputime", "time", ALIAS),
|
|
|
|
VAR6("ctime", "CTIME", 0, putimeval, POFF(p_uctime_sec), TIMEVAL),
|
2003-03-06 12:04:21 +03:00
|
|
|
GID("egid", "EGID", p_gid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("egroup", "EGROUP", LJUST, gname),
|
2006-10-30 01:32:53 +03:00
|
|
|
VAR4("emul", "EMUL", LJUST, emul),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("etime", "ELAPSED", 0, elapsed, POFF(p_ustart_sec), TIMEVAL),
|
2003-03-06 12:04:21 +03:00
|
|
|
UID("euid", "EUID", p_uid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("euser", "EUSER", LJUST, uname),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("f", "F", 0, p_flag, INT, "x"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("flags", "f", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
GID("gid", "GID", p_gid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("group", "GROUP", LJUST, gname),
|
|
|
|
VAR4("groupnames", "GROUPNAMES", LJUST, groupnames),
|
|
|
|
VAR4("groups", "GROUPS", LJUST, groups),
|
2003-03-06 12:04:21 +03:00
|
|
|
LVAR("holdcnt", "HOLDCNT", 0, l_holdcnt, INT, "d"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("ignored", "sigignore", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
PUVAR("inblk", "INBLK", 0, p_uru_inblock, UINT64, PRIu64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("inblock", "inblk", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("jobc", "JOBC", 0, p_jobc, SHORT, "d"),
|
|
|
|
PVAR("ktrace", "KTRACE", 0, p_traceflag, INT, "x"),
|
|
|
|
/*XXX*/ PVAR("ktracep", "KTRACEP", 0, p_tracep, KPTR, PRIx64),
|
2004-08-23 20:31:10 +04:00
|
|
|
LVAR("laddr", "LADDR", 0, l_laddr, KPTR, PRIx64),
|
2004-07-28 11:48:22 +04:00
|
|
|
LVAR("lid", "LID", 0, l_lid, INT32, "d"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("lim", "LIM", 0, maxrss),
|
2007-09-01 20:54:39 +04:00
|
|
|
VAR4("lname", "LNAME", LJUST|LWP, lname),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("login", "LOGIN", LJUST, logname),
|
|
|
|
VAR3("logname", "login", ALIAS),
|
|
|
|
VAR6("lstart", "STARTED", LJUST, lstarted, POFF(p_ustart_sec), UINT32),
|
|
|
|
VAR4("lstate", "STAT", LJUST|LWP, lstate),
|
2003-03-06 12:04:21 +03:00
|
|
|
PUVAR("majflt", "MAJFLT", 0, p_uru_majflt, UINT64, PRIu64),
|
|
|
|
PUVAR("minflt", "MINFLT", 0, p_uru_minflt, UINT64, PRIu64),
|
|
|
|
PUVAR("msgrcv", "MSGRCV", 0, p_uru_msgrcv, UINT64, PRIu64),
|
|
|
|
PUVAR("msgsnd", "MSGSND", 0, p_uru_msgsnd, UINT64, PRIu64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("ni", "nice", ALIAS),
|
|
|
|
VAR6("nice", "NI", 0, pnice, POFF(p_nice), UCHAR),
|
2003-03-06 12:04:21 +03:00
|
|
|
PUVAR("nivcsw", "NIVCSW", 0, p_uru_nivcsw, UINT64, PRIu64),
|
|
|
|
PVAR("nlwp", "NLWP", 0, p_nlwps, UINT64, PRId64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("nsignals", "nsigs", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
PUVAR("nsigs", "NSIGS", 0, p_uru_nsignals, UINT64, PRIu64),
|
|
|
|
PUVAR("nswap", "NSWAP", 0, p_uru_nswap, UINT64, PRIu64),
|
|
|
|
PUVAR("nvcsw", "NVCSW", 0, p_uru_nvcsw, UINT64, PRIu64),
|
|
|
|
/*XXX*/ LVAR("nwchan", "WCHAN", 0, l_wchan, KPTR, PRIx64),
|
|
|
|
PUVAR("oublk", "OUBLK", 0, p_uru_oublock, UINT64, PRIu64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("oublock", "oublk", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
/*XXX*/ PVAR("p_ru", "P_RU", 0, p_ru, KPTR, PRIx64),
|
|
|
|
/*XXX*/ PVAR("paddr", "PADDR", 0, p_paddr, KPTR, PRIx64),
|
|
|
|
PUVAR("pagein", "PAGEIN", 0, p_uru_majflt, UINT64, PRIu64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("pcpu", "%cpu", ALIAS),
|
|
|
|
VAR3("pending", "sig", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
PID("pgid", "PGID", p__pgid),
|
|
|
|
PID("pid", "PID", p_pid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR3("pmem", "%mem", ALIAS),
|
2003-03-06 12:04:21 +03:00
|
|
|
PID("ppid", "PPID", p_ppid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("pri", "PRI", LWP, pri),
|
2003-03-06 12:04:21 +03:00
|
|
|
LVAR("re", "RE", INF127, l_swtime, UINT, "u"),
|
|
|
|
GID("rgid", "RGID", p_rgid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("rgroup", "RGROUP", LJUST, rgname),
|
2003-03-06 12:04:21 +03:00
|
|
|
/*XXX*/ LVAR("rlink", "RLINK", 0, l_back, KPTR, PRIx64),
|
|
|
|
PVAR("rlwp", "RLWP", 0, p_nrlwps, UINT64, PRId64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("rss", "RSS", 0, p_rssize, POFF(p_vm_rssize), INT32),
|
|
|
|
VAR3("rssize", "rsz", ALIAS),
|
|
|
|
VAR6("rsz", "RSZ", 0, rssize, POFF(p_vm_rssize), INT32),
|
2003-03-06 12:04:21 +03:00
|
|
|
UID("ruid", "RUID", p_ruid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("ruser", "RUSER", LJUST, runame),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("sess", "SESS", 0, p_sess, KPTR24, PRIx64),
|
|
|
|
PID("sid", "SID", p_sid),
|
|
|
|
PVAR("sig", "PENDING", 0, p_siglist, SIGLIST, "s"),
|
|
|
|
PVAR("sigcatch", "CAUGHT", 0, p_sigcatch, SIGLIST, "s"),
|
|
|
|
PVAR("sigignore", "IGNORED", 0, p_sigignore, SIGLIST, "s"),
|
|
|
|
PVAR("sigmask", "BLOCKED", 0, p_sigmask, SIGLIST, "s"),
|
|
|
|
LVAR("sl", "SL", INF127, l_slptime, UINT, "u"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("start", "STARTED", 0, started, POFF(p_ustart_sec), UINT32),
|
|
|
|
VAR3("stat", "state", ALIAS),
|
|
|
|
VAR4("state", "STAT", LJUST, state),
|
|
|
|
VAR6("stime", "STIME", 0, putimeval, POFF(p_ustime_sec), TIMEVAL),
|
2003-03-06 12:04:21 +03:00
|
|
|
GID("svgid", "SVGID", p_svgid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("svgroup", "SVGROUP", LJUST, svgname),
|
2003-03-06 12:04:21 +03:00
|
|
|
UID("svuid", "SVUID", p_svuid),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("svuser", "SVUSER", LJUST, svuname),
|
2003-03-08 11:04:24 +03:00
|
|
|
/* "tdev" is UINT32, but we do this for sorting purposes */
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("tdev", "TDEV", 0, tdev, POFF(p_tdev), INT32),
|
|
|
|
VAR6("time", "TIME", 0, cputime, 0, CPUTIME),
|
2003-12-16 17:26:46 +03:00
|
|
|
PID("tpgid", "TPGID", p_tpgid),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("tsess", "TSESS", 0, p_tsess, KPTR, PRIx64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR6("tsiz", "TSIZ", 0, tsize, POFF(p_vm_tsize), INT32),
|
|
|
|
VAR6("tt", "TTY", LJUST, tname, POFF(p_tdev), INT32),
|
|
|
|
VAR6("tty", "TTY", LJUST, longtname, POFF(p_tdev), INT32),
|
2005-11-05 22:27:24 +03:00
|
|
|
LVAR("uaddr", "UADDR", 0, l_addr, KPTR, PRIx64),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("ucomm", "UCOMM", LJUST, ucomm),
|
2003-03-06 12:04:21 +03:00
|
|
|
UID("uid", "UID", p_uid),
|
|
|
|
LVAR("upr", "UPR", 0, l_usrpri, UCHAR, "u"),
|
2006-10-16 04:31:47 +04:00
|
|
|
VAR4("user", "USER", LJUST, uname),
|
|
|
|
VAR3("usrpri", "upr", ALIAS),
|
|
|
|
VAR6("utime", "UTIME", 0, putimeval, POFF(p_uutime_sec), TIMEVAL),
|
|
|
|
VAR3("vsize", "vsz", ALIAS),
|
|
|
|
VAR6("vsz", "VSZ", 0, vsize, 0, VSIZE),
|
|
|
|
VAR4("wchan", "WCHAN", LJUST|LWP, wchan),
|
2003-03-06 12:04:21 +03:00
|
|
|
PVAR("xstat", "XSTAT", 0, p_xstat, USHORT, "x"),
|
|
|
|
/* "zzzz" end_sort */
|
2006-10-16 04:31:47 +04:00
|
|
|
{ .name = "" },
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
1994-05-09 07:31:07 +04:00
|
|
|
void
|
2003-03-06 12:04:21 +03:00
|
|
|
showkey(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-09 07:31:07 +04:00
|
|
|
VAR *v;
|
|
|
|
int i;
|
2005-06-26 23:10:48 +04:00
|
|
|
const char *p;
|
|
|
|
const char *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";
|
|
|
|
}
|
2004-03-27 15:44:08 +03:00
|
|
|
(void)printf("%s%s", sep, p);
|
1993-03-21 12:45:37 +03:00
|
|
|
sep = " ";
|
|
|
|
}
|
2004-03-27 15:44:08 +03:00
|
|
|
(void)printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
/*
|
|
|
|
* Parse the string pp, and insert or append entries to the list
|
|
|
|
* referenced by listptr. If pos in non-null and *pos is non-null, then
|
|
|
|
* *pos specifies where to insert (instead of appending). If pos is
|
|
|
|
* non-null, then a new value is returned through *pos referring to the
|
|
|
|
* last item inserted.
|
|
|
|
*/
|
2003-03-06 12:04:21 +03:00
|
|
|
static void
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
parsevarlist(const char *pp, struct varlist *listptr, struct varent **pos)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
char *p, *sp, *equalsp;
|
|
|
|
|
|
|
|
/* dup to avoid zapping arguments. We will free sp later. */
|
|
|
|
p = sp = strdup(pp);
|
2003-03-06 12:04:21 +03:00
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
/*
|
|
|
|
* Everything after the first '=' is part of a custom header.
|
|
|
|
* Temporarily replace it with '\0' to simplify other code.
|
|
|
|
*/
|
|
|
|
equalsp = strchr(p, '=');
|
|
|
|
if (equalsp)
|
|
|
|
*equalsp = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#define FMTSEP " \t,\n"
|
|
|
|
while (p && *p) {
|
1994-05-09 07:31:07 +04:00
|
|
|
char *cp;
|
|
|
|
VAR *v;
|
|
|
|
struct varent *vent;
|
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
/*
|
|
|
|
* skip separators before the first keyword, and
|
|
|
|
* look for the separator after the keyword.
|
|
|
|
*/
|
|
|
|
for (cp = p; *cp != '\0'; cp++) {
|
|
|
|
p = strpbrk(cp, FMTSEP);
|
|
|
|
if (p != cp)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*cp == '\0')
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* Now cp points to the start of a keyword,
|
|
|
|
* and p is NULL or points past the end of the keyword.
|
|
|
|
*
|
|
|
|
* Terminate the keyword with '\0', or reinstate the
|
|
|
|
* '=' that was removed earlier, if appropriate.
|
|
|
|
*/
|
|
|
|
if (p) {
|
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
} else if (equalsp) {
|
|
|
|
*equalsp = '=';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If findvar() likes the keyword or keyword=header,
|
|
|
|
* add it to our list. If findvar() doesn't like it,
|
|
|
|
* it will print a warning, so we ignore it.
|
|
|
|
*/
|
|
|
|
if ((v = findvar(cp)) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
if ((vent = malloc(sizeof(struct varent))) == NULL)
|
1999-11-09 18:06:30 +03:00
|
|
|
err(1, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
vent->var = v;
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
if (pos && *pos)
|
|
|
|
SIMPLEQ_INSERT_AFTER(listptr, *pos, vent, next);
|
|
|
|
else {
|
|
|
|
SIMPLEQ_INSERT_TAIL(listptr, vent, next);
|
|
|
|
}
|
|
|
|
if (pos)
|
|
|
|
*pos = vent;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
free(sp);
|
|
|
|
if (SIMPLEQ_EMPTY(listptr))
|
1994-05-09 07:31:07 +04:00
|
|
|
errx(1, "no valid keywords");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2003-03-06 12:04:21 +03:00
|
|
|
void
|
|
|
|
parsefmt(const char *p)
|
|
|
|
{
|
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
parsevarlist(p, &displaylist, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parsefmt_insert(const char *p, struct varent **pos)
|
|
|
|
{
|
|
|
|
|
|
|
|
parsevarlist(p, &displaylist, pos);
|
2003-03-06 12:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parsesort(const char *p)
|
|
|
|
{
|
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
parsevarlist(p, &sortlist, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search through a list for an entry with a specified name. */
|
|
|
|
struct varent *
|
|
|
|
varlist_find(struct varlist *list, const char *name)
|
|
|
|
{
|
|
|
|
struct varent *vent;
|
|
|
|
|
|
|
|
SIMPLEQ_FOREACH(vent, list, next) {
|
|
|
|
if (strcmp(vent->var->name, name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return vent;
|
2003-03-06 12:04:21 +03:00
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
static VAR *
|
2003-03-06 12:04:21 +03:00
|
|
|
findvar(const char *p)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2003-03-06 12:04:21 +03:00
|
|
|
VAR *v;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *hp;
|
|
|
|
|
1994-05-09 07:31:07 +04:00
|
|
|
hp = strchr(p, '=');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (hp)
|
|
|
|
*hp++ = '\0';
|
|
|
|
|
2003-03-06 12:04:21 +03:00
|
|
|
v = bsearch(p, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
|
|
|
|
if (v && v->flag & ALIAS)
|
|
|
|
v = findvar(v->header);
|
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;
|
2003-03-06 12:04:21 +03:00
|
|
|
return NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-03-06 12:04:21 +03:00
|
|
|
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
if (v && hp) {
|
|
|
|
/*
|
|
|
|
* Override the header.
|
|
|
|
*
|
|
|
|
* We need to copy the entry first, and override the
|
|
|
|
* header in the copy, because the same field might be
|
|
|
|
* used multiple times with different headers. We also
|
|
|
|
* need to strdup the header.
|
|
|
|
*/
|
|
|
|
struct var *newvar;
|
|
|
|
char *newheader;
|
|
|
|
|
|
|
|
if ((newvar = malloc(sizeof(struct var))) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
if ((newheader = strdup(hp)) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
memcpy(newvar, v, sizeof(struct var));
|
|
|
|
newvar->header = newheader;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* According to P1003.1-2004, if the header text is null,
|
|
|
|
* such as -o user=, the field width will be at least as
|
|
|
|
* wide as the default header text.
|
|
|
|
*/
|
|
|
|
if (*hp == '\0')
|
2006-10-02 22:43:13 +04:00
|
|
|
newvar->width = strlen(v->header);
|
|
|
|
|
|
|
|
v = newvar;
|
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.
* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.
* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.
* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)
* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".
* Convert from home-grown linked lists to SIMPLEQ lists.
Discussed in tech-userlevel.
2006-10-02 21:54:35 +04:00
|
|
|
}
|
2003-03-06 12:04:21 +03:00
|
|
|
return v;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-05-09 07:31:07 +04:00
|
|
|
static int
|
2003-03-06 12:04:21 +03:00
|
|
|
vcmp(const void *a, const void *b)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2005-06-26 23:10:48 +04:00
|
|
|
return strcmp(a, ((const VAR *)b)->name);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|