]>
git.cameronkatri.com Git - apple_cmds.git/blob - adv_cmds/ps/keyword.c
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid
[] = "@(#)keyword.c 8.5 (Berkeley) 4/2/94";
34 static const char rcsid
[] =
35 "$FreeBSD: keyword.c,v 1.23 1999/01/26 02:38:09 julian Exp $";
39 #include <sys/param.h>
41 #include <sys/resource.h>
43 #include <sys/sysctl.h>
54 VAR
*findvar(char *, int, char **header
);
55 static int vcmp(const void *, const void *);
57 /* Compute offset in common structures. */
58 #define POFF(x) offsetof(struct extern_proc, x)
59 #define EOFF(x) offsetof(struct eproc, x)
60 #define UOFF(x) offsetof(struct usave, x)
61 #define ROFF(x) offsetof(struct rusage, x)
63 #define EMULLEN 13 /* enough for "FreeBSD ELF32" */
78 /* PLEASE KEEP THE TABLE BELOW SORTED ALPHABETICALLY!!! */
80 /* 4133537: 5 characters to accomodate 100% or more */
81 {"%cpu", "%CPU", NULL
, 0, pcpu
, NULL
, 5, 0, CHAR
, NULL
, 0},
82 {"%mem", "%MEM", NULL
, 0, pmem
, NULL
, 4},
84 NULL
, 0, pvar
, NULL
, 3, POFF(p_acflag
), USHORT
, "x"},
85 {"acflg", "", "acflag"},
86 {"args", "ARGS", NULL
, COMM
|LJUST
|USER
|DSIZ
, args
, s_args
, 64},
87 {"blocked", "", "sigmask"},
88 {"caught", "", "sigcatch"},
89 {"comm", "COMM", NULL
, COMM
|LJUST
|USER
|DSIZ
, just_command
, s_just_command
, 16},
90 {"command", "COMMAND", NULL
, COMM
|LJUST
|USER
|DSIZ
, command
, s_command
, 16},
91 {"cpu", "CPU", NULL
, 0, pvar
, NULL
, 3, POFF(p_estcpu
), UINT
, "d"},
92 {"cputime", "", "time"},
93 {"etime", "ELAPSED", NULL
, USER
|DSIZ
, p_etime
, s_etime
, 20},
94 /* 5861775: Make F column 8 characters. */
95 {"f", "F", NULL
, 0, pvar
, NULL
, 8, POFF(p_flag
), INT
, "x"},
97 {"gid", "GID", NULL
, 0, evar
, NULL
, UIDLEN
, EOFF(e_ucred
.cr_gid
),
99 {"group", "GROUP", "gid"},
100 {"ignored", "", "sigignore"},
102 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_inblock
), LONG
, "ld"},
103 {"inblock", "", "inblk"},
104 {"jobc", "JOBC", NULL
, 0, evar
, NULL
, 4, EOFF(e_jobc
), SHORT
, "d"},
106 NULL
, 0, pvar
, NULL
, 8, POFF(p_traceflag
), INT
, "x"},
107 {"ktracep", "KTRACEP",
108 NULL
, 0, pvar
, NULL
, 8, POFF(p_tracep
), LONG
, "lx"},
109 {"lim", "LIM", NULL
, 0, maxrss
, NULL
, 5},
110 {"login", "LOGIN", NULL
, LJUST
, logname
, NULL
, MAXLOGNAME
-1},
111 {"logname", "", "login"},
112 {"lstart", "STARTED", NULL
, LJUST
|USER
, lstarted
, NULL
, 28},
114 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_majflt
), LONG
, "ld"},
116 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_minflt
), LONG
, "ld"},
118 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_msgrcv
), LONG
, "ld"},
120 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_msgsnd
), LONG
, "ld"},
122 {"nice", "NI", NULL
, 0, pvar
, NULL
, 2, POFF(p_nice
), CHAR
, "d"},
124 NULL
, USER
, rvar
, NULL
, 5, ROFF(ru_nivcsw
), LONG
, "ld"},
125 {"nsignals", "", "nsigs"},
127 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_nsignals
), LONG
, "ld"},
129 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_nswap
), LONG
, "ld"},
131 NULL
, USER
, rvar
, NULL
, 5, ROFF(ru_nvcsw
), LONG
, "ld"},
132 {"nwchan", "WCHAN", NULL
, 0, pvar
, NULL
, 6, POFF(p_wchan
), KPTR
, "lx"},
134 NULL
, USER
, rvar
, NULL
, 4, ROFF(ru_oublock
), LONG
, "ld"},
135 {"oublock", "", "oublk"},
136 {"p_ru", "P_RU", NULL
, 0, pvar
, NULL
, 6, POFF(p_ru
), KPTR
, "lx"},
137 {"paddr", "PADDR", NULL
, 0, evar
, NULL
, sizeof(void *) * 2, EOFF(e_paddr
), KPTR
, "lx"},
138 {"pagein", "PAGEIN", NULL
, USER
, pagein
, NULL
, 6},
139 {"pcpu", "", "%cpu"},
140 {"pending", "", "sig"},
142 NULL
, 0, evar
, NULL
, PIDLEN
, EOFF(e_pgid
), UINT
, PIDFMT
},
143 {"pid", "PID", NULL
, 0, pvar
, NULL
, PIDLEN
, POFF(p_pid
), UINT
, PIDFMT
},
144 {"pmem", "", "%mem"},
146 NULL
, 0, evar
, NULL
, PIDLEN
, EOFF(e_ppid
), UINT
, PIDFMT
},
147 {"pri", "PRI", NULL
, 0, pri
, NULL
, 3},
148 {"pstime", "", "stime"},
149 {"putime", "", "utime"},
150 {"re", "RE", NULL
, 0, pvar
, NULL
, 3, POFF(p_swtime
), UINT
, "d"},
151 {"rgid", "RGID", NULL
, 0, evar
, NULL
, UIDLEN
, EOFF(e_pcred
.p_rgid
),
153 {"rgroup", "RGROUP", "rgid"},
154 {"rss", "RSS", NULL
, 0, p_rssize
, NULL
, 6},
156 {"rtprio", "RTPRIO", NULL
, 0, rtprior
, NULL
, 7, POFF(p_rtprio
)},
158 {"ruid", "RUID", NULL
, 0, evar
, NULL
, UIDLEN
, EOFF(e_pcred
.p_ruid
),
160 {"ruser", "RUSER", NULL
, LJUST
|DSIZ
, runame
, s_runame
, USERLEN
},
161 {"sess", "SESS", NULL
, 0, evar
, NULL
, 6, EOFF(e_sess
), KPTR
, "lx"},
162 {"sig", "PENDING", NULL
, 0, pvar
, NULL
, 8, POFF(p_siglist
), INT
, "x"},
164 {"sigcatch", "CAUGHT",
165 NULL
, 0, evar
, NULL
, 8, EOFF(e_procsig
.ps_sigcatch
), UINT
, "x"},
166 {"sigignore", "IGNORED",
167 NULL
, 0, evar
, NULL
, 8, EOFF(e_procsig
.ps_sigignore
), UINT
, "x"},
169 {"sigmask", "BLOCKED",
170 NULL
, 0, pvar
, NULL
, 8, POFF(p_sigmask
), UINT
, "x"},
171 {"sl", "SL", NULL
, 0, pvar
, NULL
, 3, POFF(p_slptime
), UINT
, "d"},
172 {"start", "STARTED", NULL
, LJUST
|USER
, started
, NULL
, 7},
173 {"stat", "", "state"},
174 {"state", "STAT", NULL
, 0, state
, NULL
, 4},
175 {"stime", "STIME", NULL
, USER
, pstime
, NULL
, 9},
176 {"svgid", "SVGID", NULL
, 0,
177 evar
, NULL
, UIDLEN
, EOFF(e_pcred
.p_svgid
), UINT
, UIDFMT
},
178 {"svuid", "SVUID", NULL
, 0,
179 evar
, NULL
, UIDLEN
, EOFF(e_pcred
.p_svuid
), UINT
, UIDFMT
},
180 {"tdev", "TDEV", NULL
, 0, tdev
, NULL
, 4},
181 {"time", "TIME", NULL
, USER
, cputime
, NULL
, 9},
183 NULL
, 0, evar
, NULL
, 4, EOFF(e_tpgid
), UINT
, PIDFMT
},
184 {"tsess", "TSESS", NULL
, 0, evar
, NULL
, 6, EOFF(e_tsess
), KPTR
, "lx"},
185 {"tsiz", "TSIZ", NULL
, 0, tsize
, NULL
, 8},
186 {"tt", "TT ", NULL
, 0, tname
, NULL
, 5},
187 {"tty", "TTY", NULL
, LJUST
, longtname
, NULL
, 8},
188 {"ucomm", "UCOMM", NULL
, LJUST
, ucomm
, NULL
, MAXCOMLEN
},
189 {"uid", "UID", NULL
, 0, evar
, NULL
, UIDLEN
, EOFF(e_ucred
.cr_uid
),
191 {"upr", "UPR", NULL
, 0, pvar
, NULL
, 3, POFF(p_usrpri
), CHAR
, "d"},
192 {"user", "USER", NULL
, LJUST
|DSIZ
, uname
, s_uname
, USERLEN
},
193 {"usrpri", "", "upr"},
194 {"utime", "UTIME", NULL
, USER
, putime
, NULL
, 9},
195 {"vsize", "", "vsz"},
196 {"vsz", "VSZ", NULL
, 0, vsize
, NULL
, 8},
197 {"wchan", "WCHAN", NULL
, LJUST
, wchan
, NULL
, 6},
198 {"wq", "WQ", NULL
, 0, wq
, NULL
, 4, 0, CHAR
, NULL
, 0},
199 {"wqb", "WQB", NULL
, 0, wq
, NULL
, 4, 0, CHAR
, NULL
, 0},
200 {"wql", "WQL", NULL
, 0, wq
, NULL
, 3, 0, CHAR
, NULL
, 0},
201 {"wqr", "WQR", NULL
, 0, wq
, NULL
, 4, 0, CHAR
, NULL
, 0},
202 {"xstat", "XSTAT", NULL
, 0, pvar
, NULL
, 4, POFF(p_xstat
), USHORT
, "x"},
215 for (v
= var
; *(p
= v
->name
); ++v
) {
217 if (termwidth
&& (i
+= len
+ 1) > termwidth
) {
221 (void) printf("%s%s", sep
, p
);
228 parsefmt(const char *p
, int user
)
230 char *tempstr
, *tempstr1
;
232 #define FMTSEP " \t,\n"
233 tempstr1
= tempstr
= strdup(p
);
234 while (tempstr
&& *tempstr
) {
241 * If an item contains an equals sign, it specifies a column
242 * header, may contain embedded separator characters and
243 * is always the last item.
245 if (tempstr
[strcspn(tempstr
, "="FMTSEP
)] != '=')
246 #endif /* !__APPLE__ */
247 while ((cp
= strsep(&tempstr
, FMTSEP
)) != NULL
&&
255 #endif /* !__APPLE__ */
256 if (cp
== NULL
|| !(v
= findvar(cp
, user
, &hp
)))
260 * If the user is NOT adding this field manually,
261 * get on with our lives if this VAR is already
262 * represented in the list.
264 vent
= find_varentry(v
);
268 if ((vent
= malloc(sizeof(struct varent
))) == NULL
)
269 errx(1, "malloc failed");
270 vent
->header
= v
->header
;
276 vent
->var
= malloc(sizeof(*vent
->var
));
277 if (vent
->var
== NULL
)
278 errx(1, "malloc failed");
279 memcpy(vent
->var
, v
, sizeof(*vent
->var
));
280 STAILQ_INSERT_TAIL(&varlist
, vent
, next_ve
);
283 if (STAILQ_EMPTY(&varlist
)) {
284 warnx("no valid keywords; valid keywords:");
291 findvar(char *p
, int user
, char **header
)
302 v
= bsearch(&key
, var
, sizeof(var
)/sizeof(VAR
) - 1, sizeof(VAR
), vcmp
);
306 * If the user specified an alternate-header for this
307 * (aliased) format-name, then we need to copy that
308 * alternate-header when making the recursive call to
312 parsefmt(v
->alias
, user
);
315 * XXX - This processing will not be correct for
316 * any alias which expands into a list of format
317 * keywords. Presently there are no aliases
320 rflen
= strlen(v
->alias
) + strlen(hp
) + 2;
321 realfmt
= malloc(rflen
);
322 snprintf(realfmt
, rflen
, "%s=%s", v
->alias
, hp
);
323 parsefmt(realfmt
, user
);
325 return ((VAR
*)NULL
);
328 warnx("%s: keyword not found", p
);
337 vcmp(const void *a
, const void *b
)
339 return (strcmp(((const VAR
*)a
)->name
, ((const VAR
*)b
)->name
));