From 9d50d4cb258ad0e63ca85467817bccf9765d8326 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Tue, 17 Apr 2007 12:44:32 +0000 Subject: [PATCH] start unifying help handling --- src/_usage.c | 26 ++++++++++++++++++++++++++ src/fstabinfo.c | 31 +++++++++++++++++++------------ src/mountinfo.c | 19 ++++--------------- src/rc-status.c | 31 +++++++++++++++++-------------- src/runscript.c | 28 ++++++++++++++++------------ 5 files changed, 82 insertions(+), 53 deletions(-) create mode 100644 src/_usage.c diff --git a/src/_usage.c b/src/_usage.c new file mode 100644 index 00000000..89258185 --- /dev/null +++ b/src/_usage.c @@ -0,0 +1,26 @@ +/* + * @file _usage.c + * @brief standardize help/usage output across all our programs + * @internal + * + * Copyright 2007 Gentoo Foundation + * Released under the GPLv2 + */ + +#ifndef APPLET +# error you forgot to define APPLET +#endif + +static void usage (int exit_status) +{ + int i; + printf ("Usage: " APPLET " [options]\n\n"); + printf ("Options: [" getoptstring "]\n"); + for (i = 0; longopts[i].name; ++i) + printf (" -%c, --%s\n", longopts[i].val, longopts[i].name); + exit (exit_status); +} + +#define case_RC_COMMON_GETOPT \ + case 'h': usage (EXIT_SUCCESS); \ + default: usage (EXIT_FAILURE); diff --git a/src/fstabinfo.c b/src/fstabinfo.c index 19071238..ab437e08 100644 --- a/src/fstabinfo.c +++ b/src/fstabinfo.c @@ -5,6 +5,8 @@ Copyright 2007 Gentoo Foundation */ +#define APPLET "fstabinfo" + #include #include #include @@ -54,6 +56,17 @@ static struct mntent *getmntfile (FILE *fp, const char *file) } #endif +#define getoptstring "f:m:o:p:h" +static struct option longopts[] = { + { "fstype", 1, NULL, 'f'}, + { "mountcmd", 1, NULL, 'm'}, + { "opts", 1, NULL, 'o'}, + { "passno", 1, NULL, 'p'}, + { "help", 0, NULL, 'h'}, + { NULL, 0, NULL, 0} +}; +#include "_usage.c" + int main (int argc, char **argv) { #ifdef HAVE_GETMNTENT @@ -67,15 +80,7 @@ int main (int argc, char **argv) int n = 0; char c; - static struct option longopts[] = { - { "fstype", 1, NULL, 'f'}, - { "mountcmd", 1, NULL, 'm'}, - { "opts", 1, NULL, 'o'}, - { "passno", 1, NULL, 'p'}, - { NULL, 0, NULL, 0} - }; - - while ((c = getopt_long (argc, argv, "f:m:o:p:", + while ((c = getopt_long (argc, argv, getoptstring, longopts, (int *) 0)) != -1) { #ifdef HAVE_GETMNTENT @@ -132,10 +137,13 @@ int main (int argc, char **argv) } break; } - + + case 'h': + END_ENT; + usage (EXIT_SUCCESS); default: END_ENT; - exit (EXIT_FAILURE); + usage (EXIT_FAILURE); } END_ENT; @@ -146,4 +154,3 @@ int main (int argc, char **argv) exit (result); } - diff --git a/src/mountinfo.c b/src/mountinfo.c index 85d4ecc3..4699fcd5 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -5,6 +5,8 @@ Copyright 2007 Gentoo Foundation */ +#define APPLET "mountinfo" + #include #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) #include @@ -139,16 +141,7 @@ static struct option longopts[] = { { "help", 0, NULL, 'h'}, { NULL, 0, NULL, 0} }; - -static void usage (int exit_status) -{ - int i; - printf ("Usage: mountinfo [options]\n\n"); - printf ("Options:\n"); - for (i = 0; longopts[i].name; ++i) - printf (" -%c, --%s\n", longopts[i].val, longopts[i].name); - exit (exit_status); -} +#include "_usage.c" int main (int argc, char **argv) { @@ -219,11 +212,7 @@ int main (int argc, char **argv) reverse = true; break; - case 'h': - usage (EXIT_SUCCESS); - - default: - usage (EXIT_FAILURE); + case_RC_COMMON_GETOPT } while (optind < argc) { diff --git a/src/rc-status.c b/src/rc-status.c index 2cf4ed0c..bf472c1d 100644 --- a/src/rc-status.c +++ b/src/rc-status.c @@ -5,6 +5,8 @@ Released under the GPLv2 */ +#define APPLET "rc-status" + #include #include #include @@ -51,9 +53,20 @@ static void print_service (char *service) ebracket (cols, color, status); } +#define getoptstring "alsuh" +const struct option longopts[] = { + {"all", 0, NULL, 'a'}, + {"list", 0, NULL, 'l'}, + {"servicelist", 0, NULL, 's'}, + {"unused", 0, NULL, 'u'}, + {"help", 0, NULL, 'h'}, + {NULL, 0, NULL, 0} +}; +#include "_usage.c" + int main (int argc, char **argv) { - char **levels = NULL; + char **levels = NULL; char **services = NULL; char *level; char *service; @@ -62,15 +75,7 @@ int main (int argc, char **argv) int i; int j; - const struct option longopts[] = { - {"all", 0, NULL, 'a'}, - {"list", 0, NULL, 'l'}, - {"servicelist", 0, NULL, 's'}, - {"unused", 0, NULL, 'u'}, - {NULL, 0, NULL, 0} - }; - - while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1) + while ((c = getopt_long(argc, argv, getoptstring, longopts, &option_index)) != -1) switch (c) { case 'a': levels = rc_get_runlevels (); @@ -103,10 +108,8 @@ int main (int argc, char **argv) rc_strlist_free (levels); rc_strlist_free (services); exit (EXIT_SUCCESS); - case '?': - exit (EXIT_FAILURE); - default: - exit (EXIT_FAILURE); + + case_RC_COMMON_GETOPT } while (optind < argc) diff --git a/src/runscript.c b/src/runscript.c index 98b26889..6f372270 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -6,6 +6,8 @@ * Distributed under the terms of the GNU General Public License v2 */ +#define APPLET "runscript" + #include #include #include @@ -808,6 +810,19 @@ static void svc_restart (const char *service, bool deps) } } +#define getoptstring "dCDNqvh" +static struct option longopts[] = { + { "debug", 0, NULL, 'd'}, + { "nocolor", 0, NULL, 'C'}, + { "nocolour", 0, NULL, 'C'}, + { "nodeps", 0, NULL, 'D'}, + { "quiet", 0, NULL, 'q'}, + { "verbose", 0, NULL, 'v'}, + { "help", 0, NULL, 'h'}, + { NULL, 0, NULL, 0} +}; +#include "_usage.c" + int main (int argc, char **argv) { const char *service = argv[1]; @@ -818,17 +833,6 @@ int main (int argc, char **argv) int retval; char c; - static struct option longopts[] = { - { "debug", 0, NULL, 'd'}, - { "help", 0, NULL, 'h'}, - { "nocolor", 0, NULL, 'C'}, - { "nocolour", 0, NULL, 'C'}, - { "nodeps", 0, NULL, 'D'}, - { "quiet", 0, NULL, 'q'}, - { "verbose", 0, NULL, 'v'}, - { NULL, 0, NULL, 0} - }; - /* Show help if insufficient args */ if (argc < 3) { execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); @@ -922,7 +926,7 @@ int main (int argc, char **argv) argv++; /* Right then, parse any options there may be */ - while ((c = getopt_long (argc, argv, "dhCDNqv", + while ((c = getopt_long (argc, argv, getoptstring, longopts, (int *) 0)) != -1) switch (c) { case 'd':