Skip to content

Commit 2e3fe79

Browse files
Merge pull request #492 from dezgeg/ps-fields
ps: Implement bunch of field selection flags
2 parents ba2da94 + 55ccd9b commit 2e3fe79

File tree

4 files changed

+259
-7
lines changed

4 files changed

+259
-7
lines changed

src/uu/ps/src/mapping.rs

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,84 @@ pub(crate) fn default_codes() -> Vec<String> {
2626
["pid", "tname", "time", "ucmd"].map(Into::into).to_vec()
2727
}
2828

29+
/// Returns the full format codes (for -f flag).
30+
pub(crate) fn full_format_codes() -> Vec<String> {
31+
[
32+
"uid_hack", "pid", "ppid", "c", "stime", "tname", "time", "cmd",
33+
]
34+
.map(Into::into)
35+
.to_vec()
36+
}
37+
38+
/// Returns the extra full format codes (for -F flag).
39+
pub(crate) fn extra_full_format_codes() -> Vec<String> {
40+
[
41+
"uid", "pid", "ppid", "c", "sz", "rss", "psr", "stime", "tname", "time", "ucmd",
42+
]
43+
.map(Into::into)
44+
.to_vec()
45+
}
46+
47+
/// Returns the job format codes (for -j flag).
48+
pub(crate) fn job_format_codes() -> Vec<String> {
49+
["pid", "pgid", "sid", "tname", "time", "ucmd"]
50+
.map(Into::into)
51+
.to_vec()
52+
}
53+
54+
/// Returns the default codes with PSR column (for -P flag).
55+
pub(crate) fn default_with_psr_codes() -> Vec<String> {
56+
["pid", "psr", "tname", "time", "ucmd"]
57+
.map(Into::into)
58+
.to_vec()
59+
}
60+
61+
/// Returns the signal format codes (for -s flag).
62+
pub(crate) fn signal_format_codes() -> Vec<String> {
63+
[
64+
"uid", "pid", "pending", "blocked", "ignored", "caught", "stat", "tname", "time", "command",
65+
]
66+
.map(Into::into)
67+
.to_vec()
68+
}
69+
70+
/// Returns the user format codes (for -u flag).
71+
pub(crate) fn user_format_codes() -> Vec<String> {
72+
[
73+
"user", "pid", "%cpu", "%mem", "vsz", "rss", "tname", "stat", "bsdstart", "time", "command",
74+
]
75+
.map(Into::into)
76+
.to_vec()
77+
}
78+
79+
/// Returns the virtual memory format codes (for -v flag).
80+
pub(crate) fn vm_format_codes() -> Vec<String> {
81+
[
82+
"pid", "tname", "stat", "time", "maj_flt", "trs", "drs", "rss", "%mem", "command",
83+
]
84+
.map(Into::into)
85+
.to_vec()
86+
}
87+
2988
/// Collect mapping from argument
3089
pub(crate) fn default_mapping() -> HashMap<String, String> {
3190
let mut mapping = HashMap::new();
3291
let mut append = |code: &str, header: &str| mapping.insert(code.into(), header.into());
3392

34-
// Those mapping generated from manpage
93+
// This list is mainly generated from both `ps L` output and manpage,
94+
// but some are also apparently undocumented.
3595
append("%cpu", "%CPU");
3696
append("%mem", "%MEM");
97+
append("_left", "LLLLLLLL");
98+
append("_left2", "L2L2L2L2");
99+
append("_right", "RRRRRRRR");
100+
append("_right2", "R2R2R2R2");
101+
append("_unlimited", "U");
102+
append("_unlimited2", "U2");
37103
append("ag_id", "AGID");
38104
append("ag_nice", "AGNI");
39105
append("args", "COMMAND");
106+
append("atime", "TIME");
40107
append("blocked", "BLOCKED");
41108
append("bsdstart", "START");
42109
append("bsdtime", "TIME");
@@ -50,42 +117,63 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
50117
append("cmd", "CMD");
51118
append("comm", "COMMAND");
52119
append("command", "COMMAND");
120+
append("context", "CONTEXT");
53121
append("cp", "CP");
122+
append("cpuid", "CPUID");
54123
append("cputime", "TIME");
55124
append("cputimes", "TIME");
56125
append("cuc", "%CUC");
57126
append("cuu", "%CUU");
127+
append("docker", "DOCKER");
58128
append("drs", "DRS");
129+
append("dsiz", "DSIZ");
59130
append("egid", "EGID");
60131
append("egroup", "EGROUP");
61132
append("eip", "EIP");
133+
append("environ", "ENVIRONM");
62134
append("esp", "ESP");
63135
append("etime", "ELAPSED");
64136
append("etimes", "ELAPSED");
65137
append("euid", "EUID");
66138
append("euser", "EUSER");
67139
append("exe", "EXE");
68140
append("f", "F");
141+
append("fds", "FDS");
69142
append("fgid", "FGID");
70143
append("fgroup", "FGROUP");
71144
append("flag", "F");
72145
append("flags", "F");
73146
append("fname", "COMMAND");
147+
append("fsgid", "FSGID");
148+
append("fsgroup", "FSGROUP");
149+
append("fsuid", "FSUID");
150+
append("fsuser", "FSUSER");
74151
append("fuid", "FUID");
75152
append("fuser", "FUSER");
76153
append("gid", "GID");
77154
append("group", "GROUP");
155+
append("htprv", "HTPRV");
156+
append("htshr", "HTSHR");
78157
append("ignored", "IGNORED");
158+
append("intpri", "PRI");
79159
append("ipcns", "IPCNS");
80160
append("label", "LABEL");
81-
append("lstart", "STARTED");
161+
append("lastcpu", "C");
162+
append("lim", "LIM");
163+
append("longtname", "TTY");
82164
append("lsession", "SESSION");
165+
append("lstart", "STARTED");
83166
append("luid", "LUID");
84167
append("lwp", "LWP");
85168
append("lxc", "LXC");
169+
append("m_drs", "DRS");
170+
append("m_size", "SIZE");
171+
append("m_trs", "TRS");
86172
append("machine", "MACHINE");
87-
append("maj_flt", "MAJFLT");
88-
append("min_flt", "MINFLT");
173+
append("maj_flt", "MAJFL");
174+
append("majflt", "MAJFLT");
175+
append("min_flt", "MINFL");
176+
append("minflt", "MINFLT");
89177
append("mntns", "MNTNS");
90178
append("netns", "NETNS");
91179
append("ni", "NI");
@@ -95,7 +183,11 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
95183
append("nwchan", "WCHAN");
96184
append("oom", "OOM");
97185
append("oomadj", "OOMADJ");
186+
append("opri", "PRI");
98187
append("ouid", "OWNER");
188+
append("pagein", "PAGEIN");
189+
append("pcap", "PCAP");
190+
append("pcaps", "PCAPS");
99191
append("pcpu", "%CPU");
100192
append("pending", "PENDING");
101193
append("pgid", "PGID");
@@ -106,6 +198,11 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
106198
append("policy", "POL");
107199
append("ppid", "PPID");
108200
append("pri", "PRI");
201+
append("pri_api", "API");
202+
append("pri_bar", "BAR");
203+
append("pri_baz", "BAZ");
204+
append("pri_foo", "FOO");
205+
append("priority", "PRI");
109206
append("psr", "PSR");
110207
append("pss", "PSS");
111208
append("rbytes", "RBYTES");
@@ -123,11 +220,17 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
123220
append("sched", "SCH");
124221
append("seat", "SEAT");
125222
append("sess", "SESS");
223+
append("session", "SESS");
126224
append("sgi_p", "P");
225+
append("sgi_rss", "RSS");
127226
append("sgid", "SGID");
128227
append("sgroup", "SGROUP");
129228
append("sid", "SID");
130229
append("sig", "PENDING");
230+
append("sig_block", "BLOCKED");
231+
append("sig_catch", "CATCHED");
232+
append("sig_ignore", "IGNORED");
233+
append("sig_pend", "SIGNAL");
131234
append("sigcatch", "CAUGHT");
132235
append("sigignore", "IGNORED");
133236
append("sigmask", "BLOCKED");
@@ -136,6 +239,7 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
136239
append("spid", "SPID");
137240
append("stackp", "STACKP");
138241
append("start", "STARTED");
242+
append("start_stack", "STACKP");
139243
append("start_time", "START");
140244
append("stat", "STAT");
141245
append("state", "S");
@@ -145,7 +249,9 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
145249
append("supgrp", "SUPGRP");
146250
append("suser", "SUSER");
147251
append("svgid", "SVGID");
252+
append("svgroup", "SVGROUP");
148253
append("svuid", "SVUID");
254+
append("svuser", "SVUSER");
149255
append("sz", "SZ");
150256
append("tgid", "TGID");
151257
append("thcount", "THCNT");
@@ -156,16 +262,23 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
156262
append("tname", "TTY");
157263
append("tpgid", "TPGID");
158264
append("trs", "TRS");
265+
append("trss", "TRSS");
266+
append("tsig", "PENDING");
267+
append("tsiz", "TSIZ");
159268
append("tt", "TT");
160269
append("tty", "TT");
270+
append("tty4", "TTY");
271+
append("tty8", "TTY");
161272
append("ucmd", "CMD");
162273
append("ucomm", "COMMAND");
163274
append("uid", "UID");
275+
append("uid_hack", "UID");
164276
append("uname", "USER");
165277
append("unit", "UNIT");
166278
append("user", "USER");
167279
append("userns", "USERNS");
168280
append("uss", "USS");
281+
append("util", "C");
169282
append("utsns", "UTSNS");
170283
append("uunit", "UUNIT");
171284
append("vsize", "VSZ");
@@ -174,7 +287,9 @@ pub(crate) fn default_mapping() -> HashMap<String, String> {
174287
append("wcbytes", "WCBYTES");
175288
append("wchan", "WCHAN");
176289
append("wchars", "WCHARS");
290+
append("wname", "WCHAN");
177291
append("wops", "WOPS");
292+
append("zone", "ZONE");
178293

179294
mapping
180295
}

src/uu/ps/src/picker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn collect_pickers(
3131
"uid" | "euid" => pickers.push(helper(euid)),
3232
"ruid" => pickers.push(helper(ruid)),
3333
"suid" => pickers.push(helper(suid)),
34-
"user" | "euser" => pickers.push(helper(euser)),
34+
"uid_hack" | "user" | "euser" => pickers.push(helper(euser)),
3535
"ruser" => pickers.push(helper(ruser)),
3636
"suser" => pickers.push(helper(suser)),
3737
"pgid" => pickers.push(helper(pgid)),

src/uu/ps/src/ps.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ mod sorting;
1111

1212
use clap::crate_version;
1313
use clap::{Arg, ArgAction, ArgMatches, Command};
14-
use mapping::{collect_code_mapping, default_codes, default_mapping};
14+
use mapping::{
15+
collect_code_mapping, default_codes, default_mapping, default_with_psr_codes,
16+
extra_full_format_codes, full_format_codes, job_format_codes, signal_format_codes,
17+
user_format_codes, vm_format_codes,
18+
};
1519
use parser::{parser, OptionalKeyValue};
1620
use prettytable::{format::consts::FORMAT_CLEAN, Row, Table};
1721
use std::{cell::RefCell, rc::Rc};
@@ -47,7 +51,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4751
};
4852

4953
// Collect codes with order
50-
let codes = if arg_formats.is_empty() {
54+
let codes = if matches.get_flag("f") {
55+
full_format_codes()
56+
} else if matches.get_flag("F") {
57+
extra_full_format_codes()
58+
} else if matches.get_flag("j") {
59+
job_format_codes()
60+
} else if matches.get_flag("P") {
61+
default_with_psr_codes()
62+
} else if matches.get_flag("s") {
63+
signal_format_codes()
64+
} else if matches.get_flag("u") {
65+
user_format_codes()
66+
} else if matches.get_flag("v") {
67+
vm_format_codes()
68+
} else if arg_formats.is_empty() {
5169
default_codes()
5270
} else {
5371
arg_formats.iter().map(|it| it.key().to_owned()).collect()
@@ -164,6 +182,49 @@ pub fn uu_app() -> Command {
164182
// .help("processes without controlling ttys")
165183
// .allow_hyphen_values(true),
166184
])
185+
.arg(
186+
Arg::new("f")
187+
.short('f')
188+
.action(ArgAction::SetTrue)
189+
.help("full format listing"),
190+
)
191+
.arg(
192+
Arg::new("F")
193+
.short('F')
194+
.action(ArgAction::SetTrue)
195+
.help("extra full format listing"),
196+
)
197+
.arg(
198+
Arg::new("j")
199+
.short('j')
200+
.action(ArgAction::SetTrue)
201+
.help("job format"),
202+
)
203+
.arg(
204+
Arg::new("P")
205+
.short('P')
206+
.action(ArgAction::SetTrue)
207+
.help("add psr column"),
208+
)
209+
.arg(
210+
Arg::new("s")
211+
.short('s')
212+
.action(ArgAction::SetTrue)
213+
.help("signal format"),
214+
)
215+
// TODO: this can also be used with argument to filter by uid
216+
.arg(
217+
Arg::new("u")
218+
.short('u')
219+
.action(ArgAction::SetTrue)
220+
.help("user format"),
221+
)
222+
.arg(
223+
Arg::new("v")
224+
.short('v')
225+
.action(ArgAction::SetTrue)
226+
.help("virtual memory format"),
227+
)
167228
.arg(
168229
Arg::new("format")
169230
.short('o')

0 commit comments

Comments
 (0)