Skip to content

Commit a2d49d6

Browse files
Merge pull request #323 from dezgeg/pgrep_pkill_unification3
pidwait: Use common process_matcher (+ move a pidwait feature there)
2 parents 22c25b0 + b633a46 commit a2d49d6

File tree

6 files changed

+108
-321
lines changed

6 files changed

+108
-321
lines changed

src/uu/pgrep/src/pgrep.rs

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
pub mod process;
88
pub mod process_matcher;
99

10-
use clap::{arg, crate_version, Arg, ArgAction, ArgGroup, Command};
10+
use clap::{arg, crate_version, Command};
1111
use uucore::{error::UResult, format_usage, help_about, help_usage};
1212

1313
const ABOUT: &str = help_about!("pgrep.md");
@@ -76,61 +76,16 @@ pub fn uu_app() -> Command {
7676
.about(ABOUT)
7777
.override_usage(format_usage(USAGE))
7878
.args_override_self(true)
79-
.group(ArgGroup::new("oldest_newest").args(["oldest", "newest", "inverse"]))
8079
.args([
8180
arg!(-d --delimiter <string> "specify output delimiter")
8281
.default_value("\n")
8382
.hide_default_value(true),
8483
arg!(-l --"list-name" "list PID and process name"),
8584
arg!(-a --"list-full" "list PID and full command line"),
86-
arg!(-H --"require-handler" "match only if signal handler is present"),
87-
arg!(-v --inverse "negates the matching"),
8885
// arg!(-w --lightweight "list all TID"),
89-
arg!(-c --count "count of matching processes"),
90-
arg!(-f --full "use full process name to match"),
91-
// arg!(-g --pgroup <PGID> ... "match listed process group IDs")
92-
// .value_delimiter(',')
93-
// .value_parser(clap::value_parser!(u64)),
94-
// arg!(-G --group <GID> ... "match real group IDs")
95-
// .value_delimiter(',')
96-
// .value_parser(clap::value_parser!(u64)),
97-
arg!(-i --"ignore-case" "match case insensitively"),
98-
arg!(-n --newest "select most recently started"),
99-
arg!(-o --oldest "select least recently started"),
100-
arg!(-O --older <seconds> "select where older than seconds")
101-
.value_parser(clap::value_parser!(u64)),
102-
arg!(-P --parent <PPID> "match only child processes of the given parent")
103-
.value_delimiter(',')
104-
.value_parser(clap::value_parser!(u64)),
105-
// arg!(-s --session <SID> "match session IDs")
106-
// .value_delimiter(',')
107-
// .value_parser(clap::value_parser!(u64)),
108-
arg!(--signal <sig> "signal to send (either number or name)")
109-
.default_value("SIGTERM"),
110-
arg!(-t --terminal <tty> "match by controlling terminal")
111-
.value_delimiter(','),
112-
// arg!(-u --euid <ID> ... "match by effective IDs")
113-
// .value_delimiter(',')
114-
// .value_parser(clap::value_parser!(u64)),
115-
// arg!(-U --uid <ID> ... "match by real IDs")
116-
// .value_delimiter(',')
117-
// .value_parser(clap::value_parser!(u64)),
118-
arg!(-x --exact "match exactly with the command name"),
119-
// arg!(-F --pidfile <file> "read PIDs from file"),
120-
// arg!(-L --logpidfile "fail if PID file is not locked"),
121-
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
122-
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
123-
// arg!(--cgroup <grp> "match by cgroup v2 names")
124-
// .value_delimiter(','),
125-
// arg!( --ns <PID> "match the processes that belong to the same namespace as <pid>"),
126-
// arg!( --nslist <ns> ... "list which namespaces will be considered for the --ns option.")
127-
// .value_delimiter(',')
128-
// .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]),
12986
])
130-
.arg(
131-
Arg::new("pattern")
132-
.help("Name of the program to find the PID of")
133-
.action(ArgAction::Append)
134-
.index(1),
135-
)
87+
.args(process_matcher::clap_args(
88+
"Name of the program to find the PID of",
89+
true,
90+
))
13691
}

src/uu/pgrep/src/process_matcher.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use std::collections::HashSet;
99

10-
use clap::ArgMatches;
10+
use clap::{arg, Arg, ArgAction, ArgMatches};
1111
use regex::Regex;
1212
use uucore::error::{UResult, USimpleError};
1313
#[cfg(unix)]
@@ -78,6 +78,12 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
7878
));
7979
}
8080

81+
if !settings.full && pattern.len() >= 15 {
82+
let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\
83+
Try `{} -f' option to match against the complete command line.", uucore::util_name());
84+
return Err(USimpleError::new(1, msg));
85+
}
86+
8187
Ok(settings)
8288
}
8389

@@ -248,3 +254,60 @@ fn parse_signal_value(signal_name: &str) -> UResult<usize> {
248254
signal_by_name_or_value(signal_name)
249255
.ok_or_else(|| USimpleError::new(1, format!("Unknown signal {}", signal_name.quote())))
250256
}
257+
258+
#[allow(clippy::cognitive_complexity)]
259+
pub fn clap_args(pattern_help: &'static str, enable_v_flag: bool) -> Vec<Arg> {
260+
vec![
261+
if enable_v_flag {
262+
arg!(-v --inverse "negates the matching").group("oldest_newest_inverse")
263+
} else {
264+
arg!(--inverse "negates the matching").group("oldest_newest_inverse")
265+
},
266+
arg!(-H --"require-handler" "match only if signal handler is present"),
267+
arg!(-c --count "count of matching processes"),
268+
arg!(-f --full "use full process name to match"),
269+
// arg!(-g --pgroup <PGID> "match listed process group IDs")
270+
// .value_delimiter(',')
271+
// .value_parser(clap::value_parser!(u64)),
272+
// arg!(-G --group <GID> "match real group IDs")
273+
// .value_delimiter(',')
274+
// .value_parser(clap::value_parser!(u64)),
275+
arg!(-i --"ignore-case" "match case insensitively"),
276+
arg!(-n --newest "select most recently started")
277+
.group("oldest_newest_inverse"),
278+
arg!(-o --oldest "select least recently started")
279+
.group("oldest_newest_inverse"),
280+
arg!(-O --older <seconds> "select where older than seconds")
281+
.value_parser(clap::value_parser!(u64)),
282+
arg!(-P --parent <PPID> "match only child processes of the given parent")
283+
.value_delimiter(',')
284+
.value_parser(clap::value_parser!(u64)),
285+
// arg!(-s --session <SID> "match session IDs")
286+
// .value_delimiter(',')
287+
// .value_parser(clap::value_parser!(u64)),
288+
arg!(--signal <sig> "signal to send (either number or name)")
289+
.default_value("SIGTERM"),
290+
arg!(-t --terminal <tty> "match by controlling terminal").value_delimiter(','),
291+
// arg!(-u --euid <ID> "match by effective IDs")
292+
// .value_delimiter(',')
293+
// .value_parser(clap::value_parser!(u64)),
294+
// arg!(-U --uid <ID> "match by real IDs")
295+
// .value_delimiter(',')
296+
// .value_parser(clap::value_parser!(u64)),
297+
arg!(-x --exact "match exactly with the command name"),
298+
// arg!(-F --pidfile <file> "read PIDs from file"),
299+
// arg!(-L --logpidfile "fail if PID file is not locked"),
300+
arg!(-r --runstates <state> "match runstates [D,S,Z,...]"),
301+
// arg!(-A --"ignore-ancestors" "exclude our ancestors from results"),
302+
// arg!(--cgroup <grp> "match by cgroup v2 names")
303+
// .value_delimiter(','),
304+
// arg!(--ns <PID> "match the processes that belong to the same namespace as <pid>"),
305+
// arg!(--nslist <ns> "list which namespaces will be considered for the --ns option.")
306+
// .value_delimiter(',')
307+
// .value_parser(["ipc", "mnt", "net", "pid", "user", "uts"]),
308+
Arg::new("pattern")
309+
.help(pattern_help)
310+
.action(ArgAction::Append)
311+
.index(1),
312+
]
313+
}

0 commit comments

Comments
 (0)