@@ -9,7 +9,6 @@ pub mod process;
99use clap:: { arg, crate_version, Arg , ArgAction , ArgGroup , ArgMatches , Command } ;
1010use process:: { walk_process, ProcessInformation , Teletype } ;
1111use regex:: Regex ;
12- use std:: { collections:: HashSet , sync:: OnceLock } ;
1312#[ cfg( unix) ]
1413use uucore:: { display:: Quotable , signals:: signal_by_name_or_value} ;
1514use uucore:: {
@@ -20,9 +19,9 @@ use uucore::{
2019const ABOUT : & str = help_about ! ( "pgrep.md" ) ;
2120const USAGE : & str = help_usage ! ( "pgrep.md" ) ;
2221
23- static REGEX : OnceLock < Regex > = OnceLock :: new ( ) ;
24-
2522struct 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\n Try `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 {
0 commit comments