pgrep: Add -x option to only select exact matches

This commit is contained in:
Tim Ledbetter 2023-05-30 17:51:30 +01:00 committed by Andreas Kling
parent 821bf5e071
commit f5da6d61b4
Notes: sideshowbarker 2024-07-17 21:26:19 +09:00
2 changed files with 10 additions and 2 deletions

View file

@ -5,7 +5,7 @@ pgrep - look up processes based on name
## Synopsis ## Synopsis
```sh ```sh
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--uid uid-list] [--invert-match] <process-name> $ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--uid uid-list] [--invert-match] [--exact] <process-name>
``` ```
## Options ## Options
@ -15,7 +15,7 @@ $ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--uid uid-list]
* `-i`, `--ignore-case`: Make matches case-insensitive * `-i`, `--ignore-case`: Make matches case-insensitive
* `-l`, `--list-name`: List the process name in addition to its pid * `-l`, `--list-name`: List the process name in addition to its pid
* `-U uid-list`, `--uid uid-list`: Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used * `-U uid-list`, `--uid uid-list`: Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used
* `-x`, `--exact`: Select only processes whose names match the given pattern exactly
* `-v`, `--invert-match`: Select non-matching lines * `-v`, `--invert-match`: Select non-matching lines
## Arguments ## Arguments

View file

@ -27,6 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
bool case_insensitive = false; bool case_insensitive = false;
bool list_process_name = false; bool list_process_name = false;
bool invert_match = false; bool invert_match = false;
bool exact_match = false;
HashTable<uid_t> uids_to_filter_by; HashTable<uid_t> uids_to_filter_by;
StringView pattern; StringView pattern;
@ -60,6 +61,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
}, },
}); });
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v'); args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
args_parser.add_option(exact_match, "Select only processes whose names match the given pattern exactly", "exact", 'x');
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name"); args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
args_parser.parse(args); args_parser.parse(args);
@ -67,6 +69,12 @@ ErrorOr<int> serenity_main(Main::Arguments args)
if (case_insensitive) if (case_insensitive)
options |= PosixFlags::Insensitive; options |= PosixFlags::Insensitive;
StringBuilder exact_pattern_builder;
if (exact_match) {
exact_pattern_builder.appendff("^({})$", pattern);
pattern = exact_pattern_builder.string_view();
}
Regex<PosixExtended> re(pattern, options); Regex<PosixExtended> re(pattern, options);
if (re.parser_result.error != regex::Error::NoError) { if (re.parser_result.error != regex::Error::NoError) {
return 1; return 1;