getoptify and simply the code even more.

This commit is contained in:
Roy Marples 2007-08-09 09:53:47 +00:00
parent 874bee1338
commit 9072ce748f

View File

@ -8,6 +8,7 @@
#define APPLET "rc-update" #define APPLET "rc-update"
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <limits.h> #include <limits.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@ -23,45 +24,17 @@
static char *applet = NULL; static char *applet = NULL;
static void usage (void )
{
printf (
"usage: %s -a|add script runlevel1 [runlevel2 ...]\n"
" %s -d|del script [runlevel1 ...]\n"
" %s -s|show [-v|--verbose] [runlevel1 ...]\n"
" %s -h|help\n"
"\n"
"examples:\n"
" # %s add net.eth0 default\n"
" Adds the net.eth0 script (in /etc/init.d) to the `default' runlevel.\n"
"\n"
" # %s del sysklogd\n"
" Deletes the sysklogd script from all runlevels. The original script\n"
" is not deleted, just any symlinks to the script in /etc/runlevels/*.\n"
"\n"
" # %s del net.eth2 default wumpus\n"
" Delete the net.eth2 script from the default and wumpus runlevels.\n"
" All other runlevels are unaffected. Again, the net.eth2 script\n"
" residing in /etc/init.d is not deleted, just any symlinks in\n"
" /etc/runlevels/default and /etc/runlevels/wumpus.\n"
"\n"
" # %s show\n"
" Show all enabled scripts and list at which runlevels they will\n"
" execute. Run with --verbose to see all available scripts.\n",
applet, applet, applet, applet, applet, applet, applet, applet);
}
static bool add (const char *runlevel, const char *service) static bool add (const char *runlevel, const char *service)
{ {
bool retval = false; bool retval = false;
if (! rc_service_exists (service)) if (! rc_service_exists (service))
eerror ("service `%s' does not exist", service); eerror ("%s: service `%s' does not exist", applet, service);
else if (! rc_runlevel_exists (runlevel)) else if (! rc_runlevel_exists (runlevel))
eerror ("runlevel `%s' does not exist", runlevel); eerror ("%s: runlevel `%s' does not exist", applet, runlevel);
else if (rc_service_in_runlevel (service, runlevel)) { else if (rc_service_in_runlevel (service, runlevel)) {
ewarn ("%s already installed in runlevel `%s'; skipping", ewarn ("%s: %s already installed in runlevel `%s'; skipping",
service, runlevel); applet, service, runlevel);
retval = true; retval = true;
} else if (rc_service_add (runlevel, service)) { } else if (rc_service_add (runlevel, service)) {
einfo ("%s added to runlevel %s", service, runlevel); einfo ("%s added to runlevel %s", service, runlevel);
@ -85,42 +58,21 @@ static bool delete (const char *runlevel, const char *service)
eerror ("%s: failed to remove service `%s' from runlevel `%s': %s", eerror ("%s: failed to remove service `%s' from runlevel `%s': %s",
applet, service, runlevel, strerror (errno)); applet, service, runlevel, strerror (errno));
} else if (! rc_service_exists (service)) } else if (! rc_service_exists (service))
eerror ("service `%s' does not exist", service); eerror ("%s: service `%s' does not exist", applet, service);
else if (! rc_runlevel_exists (runlevel)) else if (! rc_runlevel_exists (runlevel))
eerror ("runlevel `%s' does not exist", runlevel); eerror ("%s: runlevel `%s' does not exist", applet, runlevel);
else else
retval = true; retval = true;
return (retval); return (retval);
} }
int rc_update (int argc, char **argv) static void show (char **runlevels, bool verbose)
{ {
char *service;
char **services = rc_services_in_runlevel (NULL);
char *runlevel;
int i; int i;
int j; int j;
char *service;
char **runlevels = NULL;
char *runlevel;
bool doadd;
int retval;
applet = argv[0];
if (argc < 2 ||
strcmp (argv[1], "show") == 0 ||
strcmp (argv[1], "-s") == 0)
{
bool verbose = false;
char **services = rc_services_in_runlevel (NULL);
for (i = 2; i < argc; i++) {
if (strcmp (argv[i], "--verbose") == 0 ||
strcmp (argv[i], "-v") == 0)
verbose = true;
else
runlevels = rc_strlist_add (runlevels, argv[i]);
}
if (! runlevels)
runlevels = rc_get_runlevels ();
STRLIST_FOREACH (services, service, i) { STRLIST_FOREACH (services, service, i) {
char **in = NULL; char **in = NULL;
@ -148,51 +100,126 @@ int rc_update (int argc, char **argv)
rc_strlist_free (in); rc_strlist_free (in);
} }
rc_strlist_free (runlevels);
rc_strlist_free (services); rc_strlist_free (services);
return (EXIT_SUCCESS); }
} else if (argc > 1 &&
(strcmp (argv[1], "help") == 0 || #include "_usage.h"
strcmp (argv[1], "--help") == 0 || #define getoptstring "adsv" getoptstring_COMMON
strcmp (argv[1], "-h") == 0)) static struct option longopts[] = {
{ "add", 0, NULL, 'a'},
{ "delete", 0, NULL, 'd'},
{ "show", 0, NULL, 's'},
{ "verbose", 0, NULL, 'v'},
longopts_COMMON
{ NULL, 0, NULL, 0}
};
#include "_usage.c"
#define DOADD (1 << 0)
#define DODELETE (1 << 1)
#define DOSHOW (1 << 2)
int rc_update (int argc, char **argv)
{
int i;
char *service = NULL;
char **runlevels = NULL;
char *runlevel;
int action = 0;
bool verbose = false;
int opt;
int retval = EXIT_FAILURE;
applet = argv[0];
while ((opt = getopt_long (argc, argv, getoptstring,
longopts, (int *) 0)) != -1)
{ {
usage (); switch (opt) {
return (EXIT_SUCCESS); case 'a':
action |= DOADD;
break;
case 'd':
action |= DODELETE;
break;
case 's':
action |= DOSHOW;
break;
case 'v':
verbose = true;
break;
case_RC_COMMON_GETOPT
}
} }
if (geteuid () != 0) if ((action & DOSHOW && action != DOSHOW) ||
eerrorx ("%s: must be root to add or delete services from runlevels", (action & DOADD && action != DOADD) ||
applet); (action & DODELETE && action != DODELETE))
eerrorx ("%s: cannot mix commands", applet);
if (! (service = argv[2])) /* We need to be backwards compatible */
eerrorx ("%s: no service specified", applet); if (! action) {
if (optind < argc) {
if (strcmp (argv[1], "add") == 0 || if (strcmp (argv[optind], "add") == 0)
strcmp (argv[1], "-a") == 0) action = DOADD;
doadd = true; else if (strcmp (argv[optind], "delete") == 0)
else if (strcmp (argv[1], "delete") == 0 || action = DODELETE;
strcmp (argv[1], "del") == 0 || else if (strcmp (argv[optind], "show") == 0)
strcmp (argv[1], "-d") == 0) action = DOSHOW;
doadd = false; if (action)
optind++;
else else
eerrorx ("%s: unknown command `%s'", applet, argv[1]); eerrorx ("%s: invalid command `%s'", applet, argv[optind]);
}
if (! action && opt)
action = DOSHOW;
}
for (i = 3; i < argc; i++) if (optind >= argc) {
runlevels = rc_strlist_add (runlevels, argv[i]); if (! action & DOSHOW)
eerrorx ("%s: no service specified", applet);
} else {
service = argv[optind];
optind++;
while (optind < argc)
if (rc_runlevel_exists (argv[optind]))
runlevels = rc_strlist_add (runlevels, argv[optind++]);
else {
rc_strlist_free (runlevels);
eerrorx ("%s: `%s' is not a valid runlevel", applet, argv[optind]);
}
}
if (action & DOSHOW) {
if (service)
runlevels = rc_strlist_add (runlevels, service);
if (! runlevels)
runlevels = rc_get_runlevels ();
show (runlevels, verbose);
retval = EXIT_SUCCESS;
} else {
if (! service)
eerror ("%s: no service specified", applet);
else if (! rc_service_exists (service))
eerror ("%s: service `%s' does not exist", applet, service);
else {
retval = EXIT_SUCCESS;
if (! runlevels) if (! runlevels)
runlevels = rc_strlist_add (runlevels, rc_get_runlevel ()); runlevels = rc_strlist_add (runlevels, rc_get_runlevel ());
retval = EXIT_SUCCESS;
STRLIST_FOREACH (runlevels, runlevel, i) { STRLIST_FOREACH (runlevels, runlevel, i) {
if (doadd) { if (action & DOADD) {
if (! add (runlevel, service)) if (! add (runlevel, service))
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} else { } else if (action & DODELETE) {
if (! delete (runlevel, service)) if (! delete (runlevel, service))
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
} }
} }
}
}
rc_strlist_free (runlevels); rc_strlist_free (runlevels);