Skip to content

Commit bd6e1f2

Browse files
committed
pgrep, pkill: Move regex to settings
Also move construction of Settings into its own function.
1 parent 7f610a2 commit bd6e1f2

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

src/uu/pgrep/src/pgrep.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub mod process;
99
use clap::{arg, crate_version, Arg, ArgAction, ArgGroup, ArgMatches, Command};
1010
use process::{walk_process, ProcessInformation, Teletype};
1111
use regex::Regex;
12-
use std::{collections::HashSet, sync::OnceLock};
1312
#[cfg(unix)]
1413
use uucore::{display::Quotable, signals::signal_by_name_or_value};
1514
use uucore::{
@@ -20,9 +19,9 @@ use uucore::{
2019
const ABOUT: &str = help_about!("pgrep.md");
2120
const USAGE: &str = help_usage!("pgrep.md");
2221

23-
static REGEX: OnceLock<Regex> = OnceLock::new();
24-
2522
struct Settings {
23+
regex: Regex,
24+
2625
exact: bool,
2726
full: bool,
2827
ignore_case: bool,
@@ -35,28 +34,12 @@ struct Settings {
3534
terminal: Option<HashSet<Teletype>>,
3635
}
3736

38-
/// # Conceptual model of `pgrep`
39-
///
40-
/// At first, `pgrep` command will check the patterns is legal.
41-
/// In this stage, `pgrep` will construct regex if `--exact` argument was passed.
42-
///
43-
/// Then, `pgrep` will collect all *matched* pids, and filtering them.
44-
/// In this stage `pgrep` command will collect all the pids and its information from __/proc/__
45-
/// file system. At the same time, `pgrep` will construct filters from command
46-
/// line arguments to filter the collected pids. Note that the "-o" and "-n" flag filters works
47-
/// if them enabled and based on general collecting result.
48-
///
49-
/// Last, `pgrep` will construct output format from arguments, and print the processed result.
50-
#[uucore::main]
51-
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
52-
let matches = uu_app().try_get_matches_from(args)?;
53-
54-
let pattern = try_get_pattern_from(&matches)?;
55-
REGEX
56-
.set(Regex::new(&pattern).map_err(|e| USimpleError::new(2, e.to_string()))?)
57-
.unwrap();
37+
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
38+
let pattern = try_get_pattern_from(matches)?;
39+
let regex = Regex::new(&pattern).map_err(|e| USimpleError::new(2, e.to_string()))?;
5840

5941
let settings = Settings {
42+
regex,
6043
exact: matches.get_flag("exact"),
6144
full: matches.get_flag("full"),
6245
ignore_case: matches.get_flag("ignore-case"),
@@ -88,6 +71,25 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
8871
"no matching criteria specified\nTry `pgrep --help' for more information.",
8972
));
9073
}
74+
Ok(settings)
75+
}
76+
77+
/// # Conceptual model of `pgrep`
78+
///
79+
/// At first, `pgrep` command will check the patterns is legal.
80+
/// In this stage, `pgrep` will construct regex if `--exact` argument was passed.
81+
///
82+
/// Then, `pgrep` will collect all *matched* pids, and filtering them.
83+
/// In this stage `pgrep` command will collect all the pids and its information from __/proc/__
84+
/// file system. At the same time, `pgrep` will construct filters from command
85+
/// line arguments to filter the collected pids. Note that the "-o" and "-n" flag filters works
86+
/// if them enabled and based on general collecting result.
87+
///
88+
/// Last, `pgrep` will construct output format from arguments, and print the processed result.
89+
#[uucore::main]
90+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
91+
let matches = uu_app().try_get_matches_from(args)?;
92+
let settings = get_match_settings(&matches)?;
9193

9294
// Parse signal
9395
#[cfg(unix)]
@@ -214,7 +216,7 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
214216
&pid.proc_stat()[..15]
215217
};
216218

217-
REGEX.get().unwrap().is_match(want)
219+
settings.regex.is_match(want)
218220
};
219221

220222
let tty_matched = match &settings.terminal {

src/uu/pkill/src/pkill.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use nix::{
1111
unistd::Pid,
1212
};
1313
use regex::Regex;
14+
use std::collections::HashSet;
1415
#[cfg(unix)]
1516
use std::io::Error;
16-
use std::{collections::HashSet, sync::OnceLock};
1717
use uu_pgrep::process::{walk_process, ProcessInformation, Teletype};
1818
#[cfg(unix)]
1919
use uucore::{
@@ -30,9 +30,9 @@ use uucore::{
3030
const ABOUT: &str = help_about!("pkill.md");
3131
const USAGE: &str = help_usage!("pkill.md");
3232

33-
static REGEX: OnceLock<Regex> = OnceLock::new();
34-
3533
struct Settings {
34+
regex: Regex,
35+
3636
exact: bool,
3737
full: bool,
3838
ignore_case: bool,
@@ -45,23 +45,12 @@ struct Settings {
4545
terminal: Option<HashSet<Teletype>>,
4646
}
4747

48-
#[uucore::main]
49-
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
50-
#[cfg(unix)]
51-
let mut args = args.collect_ignore();
52-
#[cfg(target_os = "windows")]
53-
let args = args.collect_ignore();
54-
#[cfg(unix)]
55-
handle_obsolete(&mut args);
56-
57-
let matches = uu_app().try_get_matches_from(&args)?;
58-
59-
let pattern = try_get_pattern_from(&matches)?;
60-
REGEX
61-
.set(Regex::new(&pattern).map_err(|e| USimpleError::new(2, e.to_string()))?)
62-
.unwrap();
48+
fn get_match_settings(matches: &ArgMatches) -> UResult<Settings> {
49+
let pattern = try_get_pattern_from(matches)?;
50+
let regex = Regex::new(&pattern).map_err(|e| USimpleError::new(2, e.to_string()))?;
6351

6452
let settings = Settings {
53+
regex,
6554
exact: matches.get_flag("exact"),
6655
full: matches.get_flag("full"),
6756
ignore_case: matches.get_flag("ignore-case"),
@@ -93,6 +82,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
9382
"no matching criteria specified\nTry `pkill --help' for more information.",
9483
));
9584
}
85+
Ok(settings)
86+
}
87+
88+
#[uucore::main]
89+
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
90+
#[cfg(unix)]
91+
let mut args = args.collect_ignore();
92+
#[cfg(target_os = "windows")]
93+
let args = args.collect_ignore();
94+
#[cfg(unix)]
95+
handle_obsolete(&mut args);
96+
97+
let matches = uu_app().try_get_matches_from(&args)?;
98+
let settings = get_match_settings(&matches)?;
9699

97100
// Parse signal
98101
#[cfg(unix)]
@@ -211,7 +214,7 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
211214
&pid.proc_stat()[..15]
212215
};
213216

214-
REGEX.get().unwrap().is_match(want)
217+
settings.regex.is_match(want)
215218
};
216219

217220
let tty_matched = match &settings.terminal {

0 commit comments

Comments
 (0)