make version option common

This reworks the code for the version option so that it is part of the
parser loop and is a common option to all applets.
This commit is contained in:
William Hubbs 2011-02-12 19:15:29 -06:00 committed by William Hubbs
parent bae0a693a9
commit 73d1a8698e
3 changed files with 27 additions and 17 deletions

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2007-2008 Roy Marples <roy@marples.name> * Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
* All rights reserved * All rights reserved
*
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@ -24,6 +24,8 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#include "version.h"
#if lint #if lint
# define _noreturn # define _noreturn
#endif #endif
@ -33,6 +35,22 @@
# define _noreturn # define _noreturn
#endif #endif
_noreturn static void
show_version(void)
{
const char *bootlevel = NULL;
printf("%s (OpenRC", applet);
if ((bootlevel = rc_sys()))
printf(" [%s]", bootlevel);
printf(") %s", VERSION);
#ifdef BRANDING
printf(" (%s)", BRANDING);
#endif
printf("\n");
exit(EXIT_SUCCESS);
}
_noreturn static void _noreturn static void
usage(int exit_status) usage(int exit_status)
{ {

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) 2007-2008 Roy Marples <roy@marples.name> * Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
* All rights reserved * All rights reserved
*
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
@ -24,11 +24,12 @@
* SUCH DAMAGE. * SUCH DAMAGE.
*/ */
#define getoptstring_COMMON "Chqv" #define getoptstring_COMMON "ChqVv"
#define longopts_COMMON \ #define longopts_COMMON \
{ "help", 0, NULL, 'h'}, \ { "help", 0, NULL, 'h'}, \
{ "nocolor", 0, NULL, 'C'}, \ { "nocolor", 0, NULL, 'C'}, \
{ "version", 0, NULL, 'V'}, \
{ "verbose", 0, NULL, 'v'}, \ { "verbose", 0, NULL, 'v'}, \
{ "quiet", 0, NULL, 'q'}, \ { "quiet", 0, NULL, 'q'}, \
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
@ -36,11 +37,13 @@
#define longopts_help_COMMON \ #define longopts_help_COMMON \
"Display this help output", \ "Display this help output", \
"Disable color output", \ "Disable color output", \
"Display software version", \
"Run verbosely", \ "Run verbosely", \
"Run quietly" "Run quietly"
#define case_RC_COMMON_getopt_case_C setenv ("EINFO_COLOR", "NO", 1); #define case_RC_COMMON_getopt_case_C setenv ("EINFO_COLOR", "NO", 1);
#define case_RC_COMMON_getopt_case_h usage (EXIT_SUCCESS); #define case_RC_COMMON_getopt_case_h usage (EXIT_SUCCESS);
#define case_RC_COMMON_getopt_case_V if (argc == 2) show_version();
#define case_RC_COMMON_getopt_case_v setenv ("EINFO_VERBOSE", "YES", 1); #define case_RC_COMMON_getopt_case_v setenv ("EINFO_VERBOSE", "YES", 1);
#define case_RC_COMMON_getopt_case_q setenv ("EINFO_QUIET", "YES", 1); #define case_RC_COMMON_getopt_case_q setenv ("EINFO_QUIET", "YES", 1);
#define case_RC_COMMON_getopt_default usage (EXIT_FAILURE); #define case_RC_COMMON_getopt_default usage (EXIT_FAILURE);
@ -48,6 +51,7 @@
#define case_RC_COMMON_GETOPT \ #define case_RC_COMMON_GETOPT \
case 'C': case_RC_COMMON_getopt_case_C; break; \ case 'C': case_RC_COMMON_getopt_case_C; break; \
case 'h': case_RC_COMMON_getopt_case_h; break; \ case 'h': case_RC_COMMON_getopt_case_h; break; \
case 'V': case_RC_COMMON_getopt_case_V; break; \
case 'v': case_RC_COMMON_getopt_case_v; break; \ case 'v': case_RC_COMMON_getopt_case_v; break; \
case 'q': case_RC_COMMON_getopt_case_q; break; \ case 'q': case_RC_COMMON_getopt_case_q; break; \
default: case_RC_COMMON_getopt_default; break; default: case_RC_COMMON_getopt_default; break;

View File

@ -837,18 +837,6 @@ main(int argc, char **argv)
if (!applet) if (!applet)
eerrorx("arguments required"); eerrorx("arguments required");
if (argc > 1 && (strcmp(argv[1], "--version") == 0)) {
printf("%s (OpenRC", applet);
if ((bootlevel = rc_sys()))
printf(" [%s]", bootlevel);
printf(") " VERSION
#ifdef BRANDING
" (" BRANDING ")"
#endif
"\n");
exit(EXIT_SUCCESS);
}
/* Run our built in applets. If we ran one, we don't return. */ /* Run our built in applets. If we ran one, we don't return. */
run_applets(argc, argv); run_applets(argc, argv);