14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
44b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994
54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved.
64b88c807SRodney W. Grimes *
74b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
84b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions
94b88c807SRodney W. Grimes * are met:
104b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
124b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
144b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
164b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software
174b88c807SRodney W. Grimes * without specific prior written permission.
184b88c807SRodney W. Grimes *
194b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b88c807SRodney W. Grimes * SUCH DAMAGE.
304b88c807SRodney W. Grimes */
314b88c807SRodney W. Grimes
324b88c807SRodney W. Grimes #include <sys/param.h>
334b88c807SRodney W. Grimes #include <sys/time.h>
344b88c807SRodney W. Grimes #include <sys/resource.h>
354b88c807SRodney W. Grimes #include <sys/proc.h>
36c1cee2f6SSteve Price #include <sys/sysctl.h>
37c1cee2f6SSteve Price #include <sys/user.h>
384b88c807SRodney W. Grimes
394b88c807SRodney W. Grimes #include <stddef.h>
404b88c807SRodney W. Grimes #include <stdio.h>
414b88c807SRodney W. Grimes #include <stdlib.h>
424b88c807SRodney W. Grimes #include <string.h>
438beb1a2fSMarcel Moolenaar #include <libxo/xo.h>
444b88c807SRodney W. Grimes
454b88c807SRodney W. Grimes #include "ps.h"
464b88c807SRodney W. Grimes
4778b1878aSJuli Mallett static VAR *findvar(char *, int, char **header);
4846251ddeSWarner Losh static int vcmp(const void *, const void *);
494b88c807SRodney W. Grimes
504b88c807SRodney W. Grimes /* Compute offset in common structures. */
511f7d2501SKirk McKusick #define KOFF(x) offsetof(struct kinfo_proc, x)
524b88c807SRodney W. Grimes #define ROFF(x) offsetof(struct rusage, x)
534b88c807SRodney W. Grimes
5476adc1fdSGarance A Drosehn #define LWPFMT "d"
5576adc1fdSGarance A Drosehn #define NLWPFMT "d"
564b88c807SRodney W. Grimes #define UIDFMT "u"
574b88c807SRodney W. Grimes #define PIDFMT "d"
584b88c807SRodney W. Grimes
596f15bc16SMaxim Sobolev /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */
60871e8d8cSMark Murray static VAR var[] = {
618beb1a2fSMarcel Moolenaar {"%cpu", "%CPU", NULL, "percent-cpu", 0, pcpu, 0, CHAR, NULL, 0},
628beb1a2fSMarcel Moolenaar {"%mem", "%MEM", NULL, "percent-memory", 0, pmem, 0, CHAR, NULL, 0},
638beb1a2fSMarcel Moolenaar {"acflag", "ACFLG", NULL, "accounting-flag", 0, kvar, KOFF(ki_acflag),
648beb1a2fSMarcel Moolenaar USHORT, "x", 0},
658beb1a2fSMarcel Moolenaar {"acflg", "", "acflag", NULL, 0, NULL, 0, CHAR, NULL, 0},
668beb1a2fSMarcel Moolenaar {"args", "COMMAND", NULL, "arguments", COMM|LJUST|USER, arguments, 0,
6703334017SJuli Mallett CHAR, NULL, 0},
688beb1a2fSMarcel Moolenaar {"blocked", "", "sigmask", NULL, 0, NULL, 0, CHAR, NULL, 0},
698beb1a2fSMarcel Moolenaar {"caught", "", "sigcatch", NULL, 0, NULL, 0, CHAR, NULL, 0},
708beb1a2fSMarcel Moolenaar {"class", "CLASS", NULL, "login-class", LJUST, loginclass, 0, CHAR,
718beb1a2fSMarcel Moolenaar NULL, 0},
728beb1a2fSMarcel Moolenaar {"comm", "COMMAND", NULL, "command", LJUST, ucomm, 0, CHAR, NULL, 0},
738beb1a2fSMarcel Moolenaar {"command", "COMMAND", NULL, "command", COMM|LJUST|USER, command, 0,
74d9cd71b6SJuli Mallett CHAR, NULL, 0},
758beb1a2fSMarcel Moolenaar {"cow", "COW", NULL, "copy-on-write-faults", 0, kvar, KOFF(ki_cow),
768beb1a2fSMarcel Moolenaar UINT, "u", 0},
774f47f511SPiotr Pawel Stefaniak {"cpu", "C", NULL, "on-cpu", 0, cpunum, 0, CHAR, NULL, 0},
788beb1a2fSMarcel Moolenaar {"cputime", "", "time", NULL, 0, NULL, 0, CHAR, NULL, 0},
798beb1a2fSMarcel Moolenaar {"dsiz", "DSIZ", NULL, "data-size", 0, kvar, KOFF(ki_dsize), PGTOK,
808beb1a2fSMarcel Moolenaar "ld", 0},
818beb1a2fSMarcel Moolenaar {"egid", "", "gid", NULL, 0, NULL, 0, CHAR, NULL, 0},
828beb1a2fSMarcel Moolenaar {"egroup", "", "group", NULL, 0, NULL, 0, CHAR, NULL, 0},
838beb1a2fSMarcel Moolenaar {"emul", "EMUL", NULL, "emulation-envirnment", LJUST, emulname, 0,
848beb1a2fSMarcel Moolenaar CHAR, NULL, 0},
858beb1a2fSMarcel Moolenaar {"etime", "ELAPSED", NULL, "elapsed-time", USER, elapsed, 0, CHAR,
868beb1a2fSMarcel Moolenaar NULL, 0},
878beb1a2fSMarcel Moolenaar {"etimes", "ELAPSED", NULL, "elapsed-times", USER, elapseds, 0, CHAR,
888beb1a2fSMarcel Moolenaar NULL, 0},
898beb1a2fSMarcel Moolenaar {"euid", "", "uid", NULL, 0, NULL, 0, CHAR, NULL, 0},
9045ed6753SConrad Meyer {"f", "F", NULL, "flags", 0, kvar, KOFF(ki_flag), LONG, "lx", 0},
918beb1a2fSMarcel Moolenaar {"f2", "F2", NULL, "flags2", 0, kvar, KOFF(ki_flag2), INT, "08x", 0},
928beb1a2fSMarcel Moolenaar {"fib", "FIB", NULL, "fib", 0, kvar, KOFF(ki_fibnum), INT, "d", 0},
938beb1a2fSMarcel Moolenaar {"flags", "", "f", NULL, 0, NULL, 0, CHAR, NULL, 0},
948beb1a2fSMarcel Moolenaar {"flags2", "", "f2", NULL, 0, NULL, 0, CHAR, NULL, 0},
958beb1a2fSMarcel Moolenaar {"gid", "GID", NULL, "gid", 0, kvar, KOFF(ki_groups), UINT, UIDFMT, 0},
968beb1a2fSMarcel Moolenaar {"group", "GROUP", NULL, "group", LJUST, egroupname, 0, CHAR, NULL, 0},
978beb1a2fSMarcel Moolenaar {"ignored", "", "sigignore", NULL, 0, NULL, 0, CHAR, NULL, 0},
988beb1a2fSMarcel Moolenaar {"inblk", "INBLK", NULL, "read-blocks", USER, rvar, ROFF(ru_inblock),
998beb1a2fSMarcel Moolenaar LONG, "ld", 0},
1008beb1a2fSMarcel Moolenaar {"inblock", "", "inblk", NULL, 0, NULL, 0, CHAR, NULL, 0},
1012f5a9b76SJohn Baldwin {"jail", "JAIL", NULL, "jail-name", LJUST, jailname, 0, CHAR, NULL, 0},
1028beb1a2fSMarcel Moolenaar {"jid", "JID", NULL, "jail-id", 0, kvar, KOFF(ki_jid), INT, "d", 0},
1038beb1a2fSMarcel Moolenaar {"jobc", "JOBC", NULL, "job-control-count", 0, kvar, KOFF(ki_jobc),
1048beb1a2fSMarcel Moolenaar SHORT, "d", 0},
1058beb1a2fSMarcel Moolenaar {"ktrace", "KTRACE", NULL, "ktrace", 0, kvar, KOFF(ki_traceflag), INT,
1068beb1a2fSMarcel Moolenaar "x", 0},
1078beb1a2fSMarcel Moolenaar {"label", "LABEL", NULL, "label", LJUST, label, 0, CHAR, NULL, 0},
1088beb1a2fSMarcel Moolenaar {"lim", "LIM", NULL, "memory-limit", 0, maxrss, 0, CHAR, NULL, 0},
1098beb1a2fSMarcel Moolenaar {"lockname", "LOCK", NULL, "lock-name", LJUST, lockname, 0, CHAR, NULL,
1108beb1a2fSMarcel Moolenaar 0},
1118beb1a2fSMarcel Moolenaar {"login", "LOGIN", NULL, "login-name", LJUST, logname, 0, CHAR, NULL,
1128beb1a2fSMarcel Moolenaar 0},
1138beb1a2fSMarcel Moolenaar {"logname", "", "login", NULL, 0, NULL, 0, CHAR, NULL, 0},
1148beb1a2fSMarcel Moolenaar {"lstart", "STARTED", NULL, "start-time", LJUST|USER, lstarted, 0,
1158beb1a2fSMarcel Moolenaar CHAR, NULL, 0},
116faf5dd87SJohn Baldwin {"lwp", "LWP", NULL, "thread-id", 0, kvar, KOFF(ki_tid), UINT,
1178beb1a2fSMarcel Moolenaar LWPFMT, 0},
1188beb1a2fSMarcel Moolenaar {"majflt", "MAJFLT", NULL, "major-faults", USER, rvar, ROFF(ru_majflt),
1198beb1a2fSMarcel Moolenaar LONG, "ld", 0},
1208beb1a2fSMarcel Moolenaar {"minflt", "MINFLT", NULL, "minor-faults", USER, rvar, ROFF(ru_minflt),
1218beb1a2fSMarcel Moolenaar LONG, "ld", 0},
1228beb1a2fSMarcel Moolenaar {"msgrcv", "MSGRCV", NULL, "received-messages", USER, rvar,
1238beb1a2fSMarcel Moolenaar ROFF(ru_msgrcv), LONG, "ld", 0},
1248beb1a2fSMarcel Moolenaar {"msgsnd", "MSGSND", NULL, "sent-messages", USER, rvar,
1258beb1a2fSMarcel Moolenaar ROFF(ru_msgsnd), LONG, "ld", 0},
1268beb1a2fSMarcel Moolenaar {"mwchan", "MWCHAN", NULL, "wait-channel", LJUST, mwchan, 0, CHAR,
1278beb1a2fSMarcel Moolenaar NULL, 0},
1288beb1a2fSMarcel Moolenaar {"ni", "", "nice", NULL, 0, NULL, 0, CHAR, NULL, 0},
1298beb1a2fSMarcel Moolenaar {"nice", "NI", NULL, "nice", 0, kvar, KOFF(ki_nice), CHAR, "d", 0},
1308beb1a2fSMarcel Moolenaar {"nivcsw", "NIVCSW", NULL, "involuntary-context-switches", USER, rvar,
1318beb1a2fSMarcel Moolenaar ROFF(ru_nivcsw), LONG, "ld", 0},
1328beb1a2fSMarcel Moolenaar {"nlwp", "NLWP", NULL, "threads", 0, kvar, KOFF(ki_numthreads), UINT,
1338beb1a2fSMarcel Moolenaar NLWPFMT, 0},
1348beb1a2fSMarcel Moolenaar {"nsignals", "", "nsigs", NULL, 0, NULL, 0, CHAR, NULL, 0},
1358beb1a2fSMarcel Moolenaar {"nsigs", "NSIGS", NULL, "signals-taken", USER, rvar,
1368beb1a2fSMarcel Moolenaar ROFF(ru_nsignals), LONG, "ld", 0},
1378beb1a2fSMarcel Moolenaar {"nswap", "NSWAP", NULL, "swaps", USER, rvar, ROFF(ru_nswap), LONG,
1388beb1a2fSMarcel Moolenaar "ld", 0},
1398beb1a2fSMarcel Moolenaar {"nvcsw", "NVCSW", NULL, "voluntary-context-switches", USER, rvar,
1408beb1a2fSMarcel Moolenaar ROFF(ru_nvcsw), LONG, "ld", 0},
1418beb1a2fSMarcel Moolenaar {"nwchan", "NWCHAN", NULL, "wait-channel-address", LJUST, nwchan, 0,
1428beb1a2fSMarcel Moolenaar CHAR, NULL, 0},
1438beb1a2fSMarcel Moolenaar {"oublk", "OUBLK", NULL, "written-blocks", USER, rvar,
1448beb1a2fSMarcel Moolenaar ROFF(ru_oublock), LONG, "ld", 0},
1458beb1a2fSMarcel Moolenaar {"oublock", "", "oublk", NULL, 0, NULL, 0, CHAR, NULL, 0},
1468beb1a2fSMarcel Moolenaar {"paddr", "PADDR", NULL, "process-address", 0, kvar, KOFF(ki_paddr),
1478beb1a2fSMarcel Moolenaar KPTR, "lx", 0},
1488beb1a2fSMarcel Moolenaar {"pagein", "PAGEIN", NULL, "pageins", USER, pagein, 0, CHAR, NULL, 0},
1498beb1a2fSMarcel Moolenaar {"pcpu", "", "%cpu", NULL, 0, NULL, 0, CHAR, NULL, 0},
1508beb1a2fSMarcel Moolenaar {"pending", "", "sig", NULL, 0, NULL, 0, CHAR, NULL, 0},
1518beb1a2fSMarcel Moolenaar {"pgid", "PGID", NULL, "process-group", 0, kvar, KOFF(ki_pgid), UINT,
1528beb1a2fSMarcel Moolenaar PIDFMT, 0},
1538beb1a2fSMarcel Moolenaar {"pid", "PID", NULL, "pid", 0, kvar, KOFF(ki_pid), UINT, PIDFMT, 0},
1548beb1a2fSMarcel Moolenaar {"pmem", "", "%mem", NULL, 0, NULL, 0, CHAR, NULL, 0},
1558beb1a2fSMarcel Moolenaar {"ppid", "PPID", NULL, "ppid", 0, kvar, KOFF(ki_ppid), UINT, PIDFMT, 0},
1568beb1a2fSMarcel Moolenaar {"pri", "PRI", NULL, "priority", 0, pri, 0, CHAR, NULL, 0},
1578beb1a2fSMarcel Moolenaar {"re", "RE", NULL, "residency-time", INF127, kvar, KOFF(ki_swtime),
1588beb1a2fSMarcel Moolenaar UINT, "d", 0},
1598beb1a2fSMarcel Moolenaar {"rgid", "RGID", NULL, "real-gid", 0, kvar, KOFF(ki_rgid), UINT,
1608beb1a2fSMarcel Moolenaar UIDFMT, 0},
1618beb1a2fSMarcel Moolenaar {"rgroup", "RGROUP", NULL, "real-group", LJUST, rgroupname, 0, CHAR,
1628beb1a2fSMarcel Moolenaar NULL, 0},
1638beb1a2fSMarcel Moolenaar {"rss", "RSS", NULL, "rss", 0, kvar, KOFF(ki_rssize), PGTOK, "ld", 0},
1648beb1a2fSMarcel Moolenaar {"rtprio", "RTPRIO", NULL, "realtime-priority", 0, priorityr,
1658beb1a2fSMarcel Moolenaar KOFF(ki_pri), CHAR, NULL, 0},
1668beb1a2fSMarcel Moolenaar {"ruid", "RUID", NULL, "real-uid", 0, kvar, KOFF(ki_ruid), UINT,
1678beb1a2fSMarcel Moolenaar UIDFMT, 0},
1688beb1a2fSMarcel Moolenaar {"ruser", "RUSER", NULL, "real-user", LJUST, runame, 0, CHAR, NULL, 0},
1698beb1a2fSMarcel Moolenaar {"sid", "SID", NULL, "sid", 0, kvar, KOFF(ki_sid), UINT, PIDFMT, 0},
1708beb1a2fSMarcel Moolenaar {"sig", "PENDING", NULL, "signals-pending", 0, kvar, KOFF(ki_siglist),
1718beb1a2fSMarcel Moolenaar INT, "x", 0},
1728beb1a2fSMarcel Moolenaar {"sigcatch", "CAUGHT", NULL, "signals-caught", 0, kvar,
1738beb1a2fSMarcel Moolenaar KOFF(ki_sigcatch), UINT, "x", 0},
1748beb1a2fSMarcel Moolenaar {"sigignore", "IGNORED", NULL, "signals-ignored", 0, kvar,
1758beb1a2fSMarcel Moolenaar KOFF(ki_sigignore), UINT, "x", 0},
1768beb1a2fSMarcel Moolenaar {"sigmask", "BLOCKED", NULL, "signal-mask", 0, kvar, KOFF(ki_sigmask),
177871e8d8cSMark Murray UINT, "x", 0},
1788beb1a2fSMarcel Moolenaar {"sl", "SL", NULL, "sleep-time", INF127, kvar, KOFF(ki_slptime), UINT,
1798beb1a2fSMarcel Moolenaar "d", 0},
1808beb1a2fSMarcel Moolenaar {"ssiz", "SSIZ", NULL, "stack-size", 0, kvar, KOFF(ki_ssize), PGTOK,
1818beb1a2fSMarcel Moolenaar "ld", 0},
1828beb1a2fSMarcel Moolenaar {"start", "STARTED", NULL, "start-time", LJUST|USER, started, 0, CHAR,
1838beb1a2fSMarcel Moolenaar NULL, 0},
1848beb1a2fSMarcel Moolenaar {"stat", "", "state", NULL, 0, NULL, 0, CHAR, NULL, 0},
1858beb1a2fSMarcel Moolenaar {"state", "STAT", NULL, "state", LJUST, state, 0, CHAR, NULL, 0},
1868beb1a2fSMarcel Moolenaar {"svgid", "SVGID", NULL, "saved-gid", 0, kvar, KOFF(ki_svgid), UINT,
1878beb1a2fSMarcel Moolenaar UIDFMT, 0},
1888beb1a2fSMarcel Moolenaar {"svuid", "SVUID", NULL, "saved-uid", 0, kvar, KOFF(ki_svuid), UINT,
1898beb1a2fSMarcel Moolenaar UIDFMT, 0},
1908beb1a2fSMarcel Moolenaar {"systime", "SYSTIME", NULL, "system-time", USER, systime, 0, CHAR,
1918beb1a2fSMarcel Moolenaar NULL, 0},
1928beb1a2fSMarcel Moolenaar {"tdaddr", "TDADDR", NULL, "thread-address", 0, kvar, KOFF(ki_tdaddr),
1938beb1a2fSMarcel Moolenaar KPTR, "lx", 0},
1948beb1a2fSMarcel Moolenaar {"tdev", "TDEV", NULL, "terminal-device", 0, tdev, 0, CHAR, NULL, 0},
1954363782bSEdward Tomasz Napierala {"tdnam", "", "tdname", NULL, 0, NULL, 0, CHAR, NULL, 0},
1964363782bSEdward Tomasz Napierala {"tdname", "TDNAME", NULL, "thread-name", LJUST, tdnam, 0, CHAR,
1978beb1a2fSMarcel Moolenaar NULL, 0},
198faf5dd87SJohn Baldwin {"tid", "", "lwp", NULL, 0, NULL, 0, CHAR, NULL, 0},
1998beb1a2fSMarcel Moolenaar {"time", "TIME", NULL, "cpu-time", USER, cputime, 0, CHAR, NULL, 0},
2008beb1a2fSMarcel Moolenaar {"tpgid", "TPGID", NULL, "terminal-process-gid", 0, kvar,
2018beb1a2fSMarcel Moolenaar KOFF(ki_tpgid), UINT, PIDFMT, 0},
2028beb1a2fSMarcel Moolenaar {"tracer", "TRACER", NULL, "tracer", 0, kvar, KOFF(ki_tracer), UINT,
2038beb1a2fSMarcel Moolenaar PIDFMT, 0},
2048beb1a2fSMarcel Moolenaar {"tsid", "TSID", NULL, "terminal-sid", 0, kvar, KOFF(ki_tsid), UINT,
2058beb1a2fSMarcel Moolenaar PIDFMT, 0},
2068beb1a2fSMarcel Moolenaar {"tsiz", "TSIZ", NULL, "text-size", 0, kvar, KOFF(ki_tsize), PGTOK,
2078beb1a2fSMarcel Moolenaar "ld", 0},
2088beb1a2fSMarcel Moolenaar {"tt", "TT ", NULL, "terminal-name", 0, tname, 0, CHAR, NULL, 0},
2098beb1a2fSMarcel Moolenaar {"tty", "TTY", NULL, "tty", LJUST, longtname, 0, CHAR, NULL, 0},
2108beb1a2fSMarcel Moolenaar {"ucomm", "UCOMM", NULL, "accounting-name", LJUST, ucomm, 0, CHAR,
2118beb1a2fSMarcel Moolenaar NULL, 0},
2128beb1a2fSMarcel Moolenaar {"uid", "UID", NULL, "uid", 0, kvar, KOFF(ki_uid), UINT, UIDFMT, 0},
2138beb1a2fSMarcel Moolenaar {"upr", "UPR", NULL, "user-priority", 0, upr, 0, CHAR, NULL, 0},
2148beb1a2fSMarcel Moolenaar {"uprocp", "UPROCP", NULL, "process-address", 0, kvar, KOFF(ki_paddr),
2158beb1a2fSMarcel Moolenaar KPTR, "lx", 0},
2162423585bSAlex Richardson {"user", "USER", NULL, "user", LJUST, username, 0, CHAR, NULL, 0},
2178beb1a2fSMarcel Moolenaar {"usertime", "USERTIME", NULL, "user-time", USER, usertime, 0, CHAR,
2188beb1a2fSMarcel Moolenaar NULL, 0},
2198beb1a2fSMarcel Moolenaar {"usrpri", "", "upr", NULL, 0, NULL, 0, CHAR, NULL, 0},
22053606209SEdward Tomasz Napierala {"vmaddr", "VMADDR", NULL, "vmspace-address", 0, kvar, KOFF(ki_vmspace),
22153606209SEdward Tomasz Napierala KPTR, "lx", 0},
2228beb1a2fSMarcel Moolenaar {"vsize", "", "vsz", NULL, 0, NULL, 0, CHAR, NULL, 0},
2238beb1a2fSMarcel Moolenaar {"vsz", "VSZ", NULL, "virtual-size", 0, vsize, 0, CHAR, NULL, 0},
2248beb1a2fSMarcel Moolenaar {"wchan", "WCHAN", NULL, "wait-channel", LJUST, wchan, 0, CHAR, NULL,
2258beb1a2fSMarcel Moolenaar 0},
2268beb1a2fSMarcel Moolenaar {"xstat", "XSTAT", NULL, "exit-status", 0, kvar, KOFF(ki_xstat),
2278beb1a2fSMarcel Moolenaar USHORT, "x", 0},
2288beb1a2fSMarcel Moolenaar {"", NULL, NULL, NULL, 0, NULL, 0, CHAR, NULL, 0},
2294b88c807SRodney W. Grimes };
2304b88c807SRodney W. Grimes
2314b88c807SRodney W. Grimes void
showkey(void)23246251ddeSWarner Losh showkey(void)
2334b88c807SRodney W. Grimes {
2344b88c807SRodney W. Grimes VAR *v;
2354b88c807SRodney W. Grimes int i;
236871e8d8cSMark Murray const char *p, *sep;
2374b88c807SRodney W. Grimes
2384b88c807SRodney W. Grimes i = 0;
2394b88c807SRodney W. Grimes sep = "";
2408beb1a2fSMarcel Moolenaar xo_open_list("key");
2414b88c807SRodney W. Grimes for (v = var; *(p = v->name); ++v) {
2424b88c807SRodney W. Grimes int len = strlen(p);
2434b88c807SRodney W. Grimes if (termwidth && (i += len + 1) > termwidth) {
2444b88c807SRodney W. Grimes i = len;
2454b88c807SRodney W. Grimes sep = "\n";
2464b88c807SRodney W. Grimes }
247aa8ab146SYuri Pankov xo_emit("{P:/%hs}{l:key/%hs}", sep, p);
2484b88c807SRodney W. Grimes sep = " ";
2494b88c807SRodney W. Grimes }
2508beb1a2fSMarcel Moolenaar xo_emit("\n");
2518beb1a2fSMarcel Moolenaar xo_close_list("key");
252*e17a2944SYan-Hao Wang if (xo_finish() < 0)
253*e17a2944SYan-Hao Wang xo_err(1, "stdout");
2544b88c807SRodney W. Grimes }
2554b88c807SRodney W. Grimes
2564b88c807SRodney W. Grimes void
parsefmt(const char * p,int user)257fde411d5SJuli Mallett parsefmt(const char *p, int user)
2584b88c807SRodney W. Grimes {
259871e8d8cSMark Murray char *tempstr, *tempstr1;
2604b88c807SRodney W. Grimes
2612dda9fe9SJuli Mallett #define FMTSEP " \t,\n"
262871e8d8cSMark Murray tempstr1 = tempstr = strdup(p);
263871e8d8cSMark Murray while (tempstr && *tempstr) {
26478b1878aSJuli Mallett char *cp, *hp;
2654b88c807SRodney W. Grimes VAR *v;
2664b88c807SRodney W. Grimes struct varent *vent;
2674b88c807SRodney W. Grimes
26840fea9c3SJuli Mallett /*
2692dda9fe9SJuli Mallett * If an item contains an equals sign, it specifies a column
2702dda9fe9SJuli Mallett * header, may contain embedded separator characters and
2712dda9fe9SJuli Mallett * is always the last item.
27240fea9c3SJuli Mallett */
2732dda9fe9SJuli Mallett if (tempstr[strcspn(tempstr, "="FMTSEP)] != '=')
2742dda9fe9SJuli Mallett while ((cp = strsep(&tempstr, FMTSEP)) != NULL &&
2752dda9fe9SJuli Mallett *cp == '\0')
2764b88c807SRodney W. Grimes /* void */;
2772dda9fe9SJuli Mallett else {
2782dda9fe9SJuli Mallett cp = tempstr;
2792dda9fe9SJuli Mallett tempstr = NULL;
2802dda9fe9SJuli Mallett }
28178b1878aSJuli Mallett if (cp == NULL || !(v = findvar(cp, user, &hp)))
2824b88c807SRodney W. Grimes continue;
283fde411d5SJuli Mallett if (!user) {
284fde411d5SJuli Mallett /*
285fde411d5SJuli Mallett * If the user is NOT adding this field manually,
286fde411d5SJuli Mallett * get on with our lives if this VAR is already
287fde411d5SJuli Mallett * represented in the list.
288fde411d5SJuli Mallett */
289fde411d5SJuli Mallett vent = find_varentry(v);
290fde411d5SJuli Mallett if (vent != NULL)
291fde411d5SJuli Mallett continue;
292fde411d5SJuli Mallett }
2934b88c807SRodney W. Grimes if ((vent = malloc(sizeof(struct varent))) == NULL)
294*e17a2944SYan-Hao Wang xo_errx(1, "malloc failed");
29578b1878aSJuli Mallett vent->header = v->header;
29678b1878aSJuli Mallett if (hp) {
29778b1878aSJuli Mallett hp = strdup(hp);
29878b1878aSJuli Mallett if (hp)
29978b1878aSJuli Mallett vent->header = hp;
30078b1878aSJuli Mallett }
3016d041cc8SJuli Mallett vent->var = malloc(sizeof(*vent->var));
3026d041cc8SJuli Mallett if (vent->var == NULL)
303*e17a2944SYan-Hao Wang xo_errx(1, "malloc failed");
3046d041cc8SJuli Mallett memcpy(vent->var, v, sizeof(*vent->var));
305bdf8ab46SGarance A Drosehn STAILQ_INSERT_TAIL(&varlist, vent, next_ve);
3064b88c807SRodney W. Grimes }
307871e8d8cSMark Murray free(tempstr1);
308bdf8ab46SGarance A Drosehn if (STAILQ_EMPTY(&varlist)) {
309*e17a2944SYan-Hao Wang xo_warnx("no valid keywords; valid keywords:");
3108dd2eb05SJuli Mallett showkey();
3118dd2eb05SJuli Mallett exit(1);
3128dd2eb05SJuli Mallett }
3134b88c807SRodney W. Grimes }
3144b88c807SRodney W. Grimes
3154b88c807SRodney W. Grimes static VAR *
findvar(char * p,int user,char ** header)31678b1878aSJuli Mallett findvar(char *p, int user, char **header)
3174b88c807SRodney W. Grimes {
318ec716487SGarance A Drosehn size_t rflen;
3194b88c807SRodney W. Grimes VAR *v, key;
320ec716487SGarance A Drosehn char *hp, *realfmt;
3214b88c807SRodney W. Grimes
3224b88c807SRodney W. Grimes hp = strchr(p, '=');
3234b88c807SRodney W. Grimes if (hp)
3244b88c807SRodney W. Grimes *hp++ = '\0';
3254b88c807SRodney W. Grimes
3264b88c807SRodney W. Grimes key.name = p;
3274b88c807SRodney W. Grimes v = bsearch(&key, var, sizeof(var)/sizeof(VAR) - 1, sizeof(VAR), vcmp);
3284b88c807SRodney W. Grimes
3294b88c807SRodney W. Grimes if (v && v->alias) {
330ec716487SGarance A Drosehn /*
3316331f11eSGarance A Drosehn * If the user specified an alternate-header for this
3326331f11eSGarance A Drosehn * (aliased) format-name, then we need to copy that
3336331f11eSGarance A Drosehn * alternate-header when making the recursive call to
3346331f11eSGarance A Drosehn * process the alias.
3356331f11eSGarance A Drosehn */
3366331f11eSGarance A Drosehn if (hp == NULL)
3376331f11eSGarance A Drosehn parsefmt(v->alias, user);
3386331f11eSGarance A Drosehn else {
3396331f11eSGarance A Drosehn /*
3406331f11eSGarance A Drosehn * XXX - This processing will not be correct for
3416331f11eSGarance A Drosehn * any alias which expands into a list of format
3426331f11eSGarance A Drosehn * keywords. Presently there are no aliases
3436331f11eSGarance A Drosehn * which do that.
344ec716487SGarance A Drosehn */
345ec716487SGarance A Drosehn rflen = strlen(v->alias) + strlen(hp) + 2;
346ec716487SGarance A Drosehn realfmt = malloc(rflen);
3474610a811SAttilio Rao if (realfmt == NULL)
348*e17a2944SYan-Hao Wang xo_errx(1, "malloc failed");
34988985aedSGarance A Drosehn snprintf(realfmt, rflen, "%s=%s", v->alias, hp);
350ec716487SGarance A Drosehn parsefmt(realfmt, user);
3515b412ffbSAlexander Leidinger free(realfmt);
3526331f11eSGarance A Drosehn }
3534b88c807SRodney W. Grimes return ((VAR *)NULL);
3544b88c807SRodney W. Grimes }
3554b88c807SRodney W. Grimes if (!v) {
356*e17a2944SYan-Hao Wang xo_warnx("%s: keyword not found", p);
3574b88c807SRodney W. Grimes eval = 1;
35878b1878aSJuli Mallett }
35978b1878aSJuli Mallett if (header)
36078b1878aSJuli Mallett *header = hp;
3614b88c807SRodney W. Grimes return (v);
3624b88c807SRodney W. Grimes }
3634b88c807SRodney W. Grimes
3644b88c807SRodney W. Grimes static int
vcmp(const void * a,const void * b)36546251ddeSWarner Losh vcmp(const void *a, const void *b)
3664b88c807SRodney W. Grimes {
367871e8d8cSMark Murray return (strcmp(((const VAR *)a)->name, ((const VAR *)b)->name));
3684b88c807SRodney W. Grimes }
369