diff options
author | Roy Marples <roy@marples.name> | 2007-04-12 10:08:42 +0000 |
---|---|---|
committer | Roy Marples <roy@marples.name> | 2007-04-12 10:08:42 +0000 |
commit | 2728cef0e1b7f91fe1f23b3f85a50820743b75f2 (patch) | |
tree | 5f056163c0754806e8b5ad677d65fd1be8647c1f /src | |
parent | 33d303f1844d98490bb24071e612bf5845c5eebb (diff) |
Hug getopt_long even though it's not POSIX. start-stop-daemon has always required this and all our supported platforms have it.
Diffstat (limited to 'src')
-rw-r--r-- | src/mountinfo.c | 127 | ||||
-rw-r--r-- | src/rc-status.c | 10 | ||||
-rw-r--r-- | src/runscript.c | 98 |
3 files changed, 125 insertions, 110 deletions
diff --git a/src/mountinfo.c b/src/mountinfo.c index 1e15de4b..50e0716d 100644 --- a/src/mountinfo.c +++ b/src/mountinfo.c @@ -15,6 +15,7 @@ #endif #include <errno.h> +#include <getopt.h> #include <limits.h> #include <regex.h> #include <stdio.h> @@ -26,6 +27,7 @@ #include "rc-misc.h" #include "strlist.h" + #if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, char **mounts, bool list_nodes, bool list_fstype) @@ -34,6 +36,7 @@ static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, int nmnts; int i; char **list = NULL; + char c; if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0) eerrorx ("getmntinfo: %s", strerror (errno)); @@ -140,70 +143,80 @@ int main (int argc, char **argv) bool list_fstype = false; bool reverse = false; char **mounts = NULL; + char c; + + static struct option longopts[] = { + { "fstype-regex", 1, NULL, 'F'}, + { "node-regex", 1, NULL, 'N'}, + { "skip-regex", 1, NULL, 'S'}, + { "fstype", 0, NULL, 'f'}, + { "node", 0, NULL, 'n'}, + { "reverse", 0, NULL, 'r'}, + { NULL, 0, NULL, 0} + }; + + while ((c = getopt_long (argc, argv, "F:N:S;fnr", + longopts, (int *) 0)) != -1) + switch (c) { + case 'F': + if (fstype_regex) + free (fstype_regex); + fstype_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (fstype_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, fstype_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; + + case 'N': + if (node_regex) + free (node_regex); + node_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (node_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, node_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; + + case 'R': + if (skip_regex) + free (skip_regex); + skip_regex = rc_xmalloc (sizeof (regex_t)); + if ((result = regcomp (skip_regex, optarg, + REG_EXTENDED | REG_NOSUB)) != 0) + { + regerror (result, skip_regex, buffer, sizeof (buffer)); + eerrorx ("%s: invalid regex `%s'", argv[0], buffer); + } + break; - for (i = 1; i < argc; i++) { - if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) { - i++; - if (fstype_regex) - free (fstype_regex); - fstype_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (fstype_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, fstype_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } - - if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) { - i++; - if (node_regex) - free (node_regex); - node_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (node_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, node_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } + case 'f': + list_fstype = true; + list_nodes = false; + break; - if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) { - i++; - if (skip_regex) - free (skip_regex); - skip_regex = rc_xmalloc (sizeof (regex_t)); - if ((result = regcomp (skip_regex, argv[i], - REG_EXTENDED | REG_NOSUB)) != 0) - { - regerror (result, skip_regex, buffer, sizeof (buffer)); - eerrorx ("%s: invalid regex `%s'", argv[0], buffer); - } - continue; - } + case 'n': + list_nodes = true; + list_fstype = false; + break; - if (strcmp (argv[i], "--fstype") == 0) { - list_fstype = true; - continue; - } + case 'r': + reverse = true; + break; - if (strcmp (argv[i], "--node") == 0) { - list_nodes = true; - continue; - } - if (strcmp (argv[i], "--reverse") == 0) { - reverse = true; - continue; + default: + exit (EXIT_FAILURE); } - if (argv[i][0] != '/') - eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]); - - mounts = rc_strlist_add (mounts, argv[i]); + while (optind < argc) { + if (argv[optind][0] != '/') + eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]); + mounts = rc_strlist_add (mounts, argv[optind++]); } - nodes = find_mounts (node_regex, fstype_regex, mounts, list_nodes, list_fstype); diff --git a/src/rc-status.c b/src/rc-status.c index 0fcba811..7709e422 100644 --- a/src/rc-status.c +++ b/src/rc-status.c @@ -63,11 +63,11 @@ int main (int argc, char **argv) int j; const struct option longopts[] = { - {"all", no_argument, NULL, 'a'}, - {"list", no_argument, NULL, 'l'}, - {"servicelist", no_argument, NULL, 's'}, - {"unused", no_argument, NULL, 'u'}, - {NULL, 0, NULL, 0} + {"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) diff --git a/src/runscript.c b/src/runscript.c index 8f479771..24512e25 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -12,6 +12,7 @@ #include <sys/wait.h> #include <dlfcn.h> #include <errno.h> +#include <getopt.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> @@ -815,7 +816,17 @@ int main (int argc, char **argv) bool doneone = false; char pid[16]; int retval; - bool ifstarted = false; + 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'}, + { "verbose", 0, NULL, 'v'}, + { NULL, 0, NULL, 0} + }; applet = strdup (basename (service)); atexit (cleanup); @@ -914,38 +925,31 @@ int main (int argc, char **argv) #endif /* Right then, parse any options there may be */ - for (i = 2; i < argc; i++) { - if (strlen (argv[i]) < 2 || argv[i][0] != '-' || argv[i][1] != '-') - continue; - - if (strcmp (argv[i], "--debug") == 0) - setenv ("RC_DEBUG", "yes", 1); - else if (strcmp (argv[i], "--help") == 0) { - execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); - eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", - applet, strerror (errno)); - } else if (strcmp (argv[i],"--ifstarted") == 0) - ifstarted = true; - else if (strcmp (argv[i], "--nocolour") == 0 || - strcmp (argv[i], "--nocolor") == 0) - setenv ("RC_NOCOLOR", "yes", 1); - else if (strcmp (argv[i], "--nodeps") == 0) - deps = false; - else if (strcmp (argv[i], "--quiet") == 0) - setenv ("RC_QUIET", "yes", 1); - else if (strcmp (argv[i], "--verbose") == 0) - setenv ("RC_VERBOSE", "yes", 1); - else if (strcmp (argv[i], "--version") == 0) - printf ("version me\n"); - else - eerror ("%s: unknown option `%s'", applet, argv[i]); - } - - if (ifstarted && ! rc_service_state (applet, rc_service_started)) { - if (! rc_is_env("RC_QUIET", "yes")) - eerror ("ERROR: %s is not started", applet); - exit (EXIT_FAILURE); - } + while ((c = getopt_long (argc, argv, "dhCDNqv", + longopts, (int *) 0)) != -1) + switch (c) { + case 'd': + setenv ("RC_DEBUG", "yes", 1); + break; + case 'h': + execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); + eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", + applet, strerror (errno)); + case 'C': + setenv ("RC_NOCOLOR", "yes", 1); + break; + case 'D': + deps = false; + break; + case 'q': + setenv ("RC_QUIET", "yes", 1); + break; + case 'v': + setenv ("RC_VERBOSE", "yes", 1); + break; + default: + exit (EXIT_FAILURE); + } if (rc_is_env ("IN_HOTPLUG", "1")) { if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet)) @@ -964,37 +968,35 @@ int main (int argc, char **argv) /* Now run each option */ retval = EXIT_SUCCESS; - for (i = 2; i < argc; i++) { + while (optind < argc) { + optarg = argv[optind++]; + /* Abort on a sighup here */ if (sighup) exit (EXIT_FAILURE); - if (strlen (argv[i]) < 2 || - (argv[i][0] == '-' && argv[i][1] == '-')) - continue; - /* Export the command we're running. This is important as we stamp on the restart function now but some start/stop routines still need to behave differently if restarting. */ unsetenv ("RC_CMD"); - setenv ("RC_CMD", argv[i], 1); + setenv ("RC_CMD", optarg, 1); doneone = true; - if (strcmp (argv[i], "conditionalrestart") == 0 || - strcmp (argv[i], "condrestart") == 0) + if (strcmp (optarg, "conditionalrestart") == 0 || + strcmp (optarg, "condrestart") == 0) { if (rc_service_state (service, rc_service_started)) svc_restart (service, deps); } - else if (strcmp (argv[i], "restart") == 0) + else if (strcmp (optarg, "restart") == 0) svc_restart (service, deps); - else if (strcmp (argv[i], "start") == 0) + else if (strcmp (optarg, "start") == 0) svc_start (service, deps); - else if (strcmp (argv[i], "status") == 0) { + else if (strcmp (optarg, "status") == 0) { rc_service_state_t r = svc_status (service); retval = (int) r; - } else if (strcmp (argv[i], "stop") == 0) { + } else if (strcmp (optarg, "stop") == 0) { if (in_background) get_started_services (); @@ -1014,16 +1016,16 @@ int main (int argc, char **argv) if (rc_service_state (svc, rc_service_stopped)) rc_schedule_start_service (service, svc); } - } else if (strcmp (argv[i], "zap") == 0) { + } else if (strcmp (optarg, "zap") == 0) { einfo ("Manually resetting %s to stopped state", applet); rc_mark_service (applet, rc_service_stopped); uncoldplug (applet); - } else if (strcmp (argv[i], "help") == 0) { + } else if (strcmp (optarg, "help") == 0) { execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL); eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", applet, strerror (errno)); }else - svc_exec (service, argv[i], NULL); + svc_exec (service, optarg, NULL); /* Flush our buffered output if any */ eflush (); |