|
|
4b6aa8 |
From 2b6424dec2233ef944b6947bbf2350daf222adae Mon Sep 17 00:00:00 2001
|
|
|
4b6aa8 |
From: Ernestas Kulik <ekulik@redhat.com>
|
|
|
4b6aa8 |
Date: Tue, 28 May 2019 15:51:05 +0200
|
|
|
4b6aa8 |
Subject: [PATCH] cli: Unpack command-line argument parsing logic
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
Currently, checking for invalid command line needlessly involves
|
|
|
4b6aa8 |
convoluted bitwise operations, which can be simplified drastically.
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
First, argc is reduced by optind, which points to the next argument to
|
|
|
4b6aa8 |
be processed. If everything goes well, argc should be 1, since the only
|
|
|
4b6aa8 |
remaining argument to be processed is the problem directory. If that
|
|
|
4b6aa8 |
does not hold, we want to bail. Another point at which we want to bail
|
|
|
4b6aa8 |
is when an option is passed that operates on the positional argument
|
|
|
4b6aa8 |
(anything but -L, which just lists available events). Checking for that
|
|
|
4b6aa8 |
involves ANDing the current option mask with the mask of all such
|
|
|
4b6aa8 |
options. The result is NOTed for comparison, since argc is 0 in such
|
|
|
4b6aa8 |
cases.
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
Signed-off-by: Ernestas Kulik <ekulik@redhat.com>
|
|
|
4b6aa8 |
---
|
|
|
4b6aa8 |
src/cli/cli.c | 17 ++++++++---------
|
|
|
4b6aa8 |
1 file changed, 8 insertions(+), 9 deletions(-)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
diff --git a/src/cli/cli.c b/src/cli/cli.c
|
|
|
4b6aa8 |
index a467bf6..67ce7dd 100644
|
|
|
4b6aa8 |
--- a/src/cli/cli.c
|
|
|
4b6aa8 |
+++ b/src/cli/cli.c
|
|
|
4b6aa8 |
@@ -39,6 +39,9 @@ static char *steal_directory_if_needed(char *dump_dir_name)
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
int main(int argc, char** argv)
|
|
|
4b6aa8 |
{
|
|
|
4b6aa8 |
+ bool runaway_arguments;
|
|
|
4b6aa8 |
+ bool missing_positional_argument;
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
abrt_init(argv);
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
setlocale(LC_ALL, "");
|
|
|
4b6aa8 |
@@ -108,16 +111,12 @@ int main(int argc, char** argv)
|
|
|
4b6aa8 |
argv += optind;
|
|
|
4b6aa8 |
argc -= optind;
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
+ runaway_arguments = argc > 1;
|
|
|
4b6aa8 |
+ missing_positional_argument = (opts & OPTMASK_need_arg) && (argc == 0);
|
|
|
4b6aa8 |
+
|
|
|
4b6aa8 |
/* Check for bad usage */
|
|
|
4b6aa8 |
- if (argc > 1 /* more than one arg? */
|
|
|
4b6aa8 |
- ||
|
|
|
4b6aa8 |
- /* dont_need_arg == have_arg? bad in both cases:
|
|
|
4b6aa8 |
- * TRUE == TRUE (dont need arg but have) or
|
|
|
4b6aa8 |
- * FALSE == FALSE (need arg but havent).
|
|
|
4b6aa8 |
- * OPT_list_events is an exception, it can be used in both cases.
|
|
|
4b6aa8 |
- */
|
|
|
4b6aa8 |
- ((!(opts & OPTMASK_need_arg) == argc) && (op != OPT_list_events))
|
|
|
4b6aa8 |
- ) {
|
|
|
4b6aa8 |
+ if (runaway_arguments || (missing_positional_argument && op != OPT_list_events))
|
|
|
4b6aa8 |
+ {
|
|
|
4b6aa8 |
show_usage_and_die(program_usage_string, program_options);
|
|
|
4b6aa8 |
}
|
|
|
4b6aa8 |
|
|
|
4b6aa8 |
--
|
|
|
4b6aa8 |
2.21.0
|
|
|
4b6aa8 |
|