From c203a09e3b1bea5a15a30dae099183291aa43d8e Mon Sep 17 00:00:00 2001 From: Bernhard Voelker Date: Mon, 26 Nov 2018 09:05:37 +0100 Subject: [PATCH] all: detect --help and --version more consistently [FIXME] FIXME: syntax-check, tests. --- src/cksum.c | 11 ++--------- src/dd.c | 19 ++++++++++++++----- src/hostid.c | 11 ++--------- src/hostname.c | 11 ++--------- src/link.c | 11 ++--------- src/logname.c | 11 ++--------- src/nohup.c | 11 ++--------- src/sleep.c | 11 ++--------- src/system.h | 24 ++++++++++++++++++++++++ src/tsort.c | 11 ++--------- src/unlink.c | 11 ++--------- src/uptime.c | 11 ++--------- src/users.c | 11 ++--------- src/whoami.c | 11 ++--------- src/yes.c | 11 ++--------- 15 files changed, 64 insertions(+), 122 deletions(-) diff --git a/src/cksum.c b/src/cksum.c index 372d6b601..d21f70a9a 100644 --- a/src/cksum.c +++ b/src/cksum.c @@ -107,11 +107,6 @@ main (void) # include "die.h" # include "error.h" -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - /* Number of bytes to read at once. */ # define BUFLEN (1 << 16) @@ -294,10 +289,8 @@ main (int argc, char **argv) so that processes running in parallel do not intersperse their output. */ setvbuf (stdout, NULL, _IOLBF, 0); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); have_read_stdin = false; diff --git a/src/dd.c b/src/dd.c index 6aeef520a..c8781dee2 100644 --- a/src/dd.c +++ b/src/dd.c @@ -48,6 +48,8 @@ static struct option const long_options[] = { + {GETOPT_HELP_OPTION_DECL}, + {GETOPT_VERSION_OPTION_DECL}, {NULL, 0, NULL, 0} }; @@ -2396,12 +2398,19 @@ main (int argc, char **argv) page_size = getpagesize (); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version, - usage, AUTHORS, (char const *) NULL); - close_stdout_required = false; + int c; + if ((c = getopt_long (argc, argv, "", long_options, NULL)) != -1) + { + switch (c) + { + case_GETOPT_HELP_CHAR; + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + default: + usage (EXIT_FAILURE); + } + } - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + close_stdout_required = false; /* Initialize translation table to identity translation. */ for (i = 0; i < 256; i++) diff --git a/src/hostid.c b/src/hostid.c index c45328aea..4050aee2f 100644 --- a/src/hostid.c +++ b/src/hostid.c @@ -32,11 +32,6 @@ #define AUTHORS proper_name ("Jim Meyering") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -69,10 +64,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (optind < argc) { diff --git a/src/hostname.c b/src/hostname.c index 292c148ec..c8d51a4c8 100644 --- a/src/hostname.c +++ b/src/hostname.c @@ -33,11 +33,6 @@ #define AUTHORS proper_name ("Jim Meyering") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - #if !defined HAVE_SETHOSTNAME && defined HAVE_SYSINFO && \ defined HAVE_SYS_SYSTEMINFO_H # include @@ -86,10 +81,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (argc == optind + 1) { diff --git a/src/link.c b/src/link.c index 203a11788..7796b6f96 100644 --- a/src/link.c +++ b/src/link.c @@ -36,11 +36,6 @@ #define AUTHORS proper_name ("Michael Stone") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -72,10 +67,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (argc < optind + 2) { diff --git a/src/logname.c b/src/logname.c index 171fe483f..633bc5cdf 100644 --- a/src/logname.c +++ b/src/logname.c @@ -30,11 +30,6 @@ #define AUTHORS proper_name ("FIXME: unknown") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -67,10 +62,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (optind < argc) { diff --git a/src/nohup.c b/src/nohup.c index 4bc7a4a48..f631c50d7 100644 --- a/src/nohup.c +++ b/src/nohup.c @@ -34,11 +34,6 @@ #define AUTHORS proper_name ("Jim Meyering") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - /* Exit statuses. */ enum { @@ -104,10 +99,8 @@ main (int argc, char **argv) initialize_exit_failure (exit_internal_failure); atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "+", long_options, NULL) != -1) - usage (exit_internal_failure); + /* Scan the 1st non-option argument to look for '--help' or '--version'. */ + emit_help_or_version_info (false); if (argc <= optind) { diff --git a/src/sleep.c b/src/sleep.c index 162053d7c..366b3828e 100644 --- a/src/sleep.c +++ b/src/sleep.c @@ -35,11 +35,6 @@ proper_name ("Jim Meyering"), \ proper_name ("Paul Eggert") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -114,10 +109,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (argc == 1) { diff --git a/src/system.h b/src/system.h index f5231d5c4..36ba171f7 100644 --- a/src/system.h +++ b/src/system.h @@ -627,6 +627,30 @@ the VERSION_CONTROL environment variable. Here are the values:\n\ "), stdout); } +/* Unless scanning over all arguments, stop as soon as a + non-option argument is encountered. */ +#define emit_help_or_version_info(Scan_all) \ + do { \ + struct option const long_options[] = \ + { \ + {GETOPT_HELP_OPTION_DECL}, \ + {GETOPT_VERSION_OPTION_DECL}, \ + {NULL, 0, NULL, 0} \ + }; \ + int c; \ + const char *optstring = Scan_all ? "" : "+"; \ + if ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1) \ + { \ + switch (c) \ + { \ + case_GETOPT_HELP_CHAR; \ + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); \ + default: \ + usage (EXIT_FAILURE); \ + } \ + } \ + } while (0) + static inline void emit_ancillary_info (char const *program) { diff --git a/src/tsort.c b/src/tsort.c index 0125c5c50..91861b16a 100644 --- a/src/tsort.c +++ b/src/tsort.c @@ -40,11 +40,6 @@ #define AUTHORS proper_name ("Mark Kettenis") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - /* Token delimiters when reading from a file. */ #define DELIM " \t\n" @@ -556,10 +551,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (1 < argc - optind) { diff --git a/src/unlink.c b/src/unlink.c index a6ae623f9..72540f518 100644 --- a/src/unlink.c +++ b/src/unlink.c @@ -36,11 +36,6 @@ #define AUTHORS proper_name ("Michael Stone") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -71,10 +66,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (argc < optind + 1) { diff --git a/src/uptime.c b/src/uptime.c index 5f0775069..0e3b2ff12 100644 --- a/src/uptime.c +++ b/src/uptime.c @@ -47,11 +47,6 @@ proper_name ("David MacKenzie"), \ proper_name ("Kaveh Ghazi") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - static void print_uptime (size_t n, const STRUCT_UTMP *this) { @@ -239,10 +234,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); switch (argc - optind) { diff --git a/src/users.c b/src/users.c index 3287b7a6d..43e782d8a 100644 --- a/src/users.c +++ b/src/users.c @@ -36,11 +36,6 @@ proper_name ("Joseph Arceneaux"), \ proper_name ("David MacKenzie") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - static int userid_compare (const void *v_a, const void *v_b) { @@ -133,10 +128,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); switch (argc - optind) { diff --git a/src/whoami.c b/src/whoami.c index ac2ac0207..5b7598259 100644 --- a/src/whoami.c +++ b/src/whoami.c @@ -35,11 +35,6 @@ #define AUTHORS proper_name ("Richard Mlynarik") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -75,10 +70,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); if (optind != argc) { diff --git a/src/yes.c b/src/yes.c index 3dd5d2f9d..3cc8182af 100644 --- a/src/yes.c +++ b/src/yes.c @@ -32,11 +32,6 @@ #define AUTHORS proper_name ("David MacKenzie") -static struct option const long_options[] = -{ - {NULL, 0, NULL, 0} -}; - void usage (int status) { @@ -72,10 +67,8 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "+", long_options, NULL) != -1) - usage (EXIT_FAILURE); + /* Scan over all args to look for '--help' or '--version'. */ + emit_help_or_version_info (true); char **operands = argv + optind; char **operand_lim = argv + argc; -- 2.19.1