Hug getopt_long even though it's not POSIX. start-stop-daemon has always required this and all our supported platforms have it.

This commit is contained in:
Roy Marples 2007-04-12 10:08:42 +00:00
parent 33d303f184
commit 2728cef0e1
3 changed files with 128 additions and 113 deletions

View File

@ -15,6 +15,7 @@
#endif #endif
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <limits.h> #include <limits.h>
#include <regex.h> #include <regex.h>
#include <stdio.h> #include <stdio.h>
@ -26,6 +27,7 @@
#include "rc-misc.h" #include "rc-misc.h"
#include "strlist.h" #include "strlist.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__) #if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex, static char **find_mounts (regex_t *node_regex, regex_t *fstype_regex,
char **mounts, bool list_nodes, bool list_fstype) 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 nmnts;
int i; int i;
char **list = NULL; char **list = NULL;
char c;
if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0) if ((nmnts = getmntinfo (&mnts, MNT_NOWAIT)) == 0)
eerrorx ("getmntinfo: %s", strerror (errno)); eerrorx ("getmntinfo: %s", strerror (errno));
@ -140,70 +143,80 @@ int main (int argc, char **argv)
bool list_fstype = false; bool list_fstype = false;
bool reverse = false; bool reverse = false;
char **mounts = NULL; char **mounts = NULL;
char c;
for (i = 1; i < argc; i++) { static struct option longopts[] = {
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) { { "fstype-regex", 1, NULL, 'F'},
i++; { "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) if (fstype_regex)
free (fstype_regex); free (fstype_regex);
fstype_regex = rc_xmalloc (sizeof (regex_t)); fstype_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (fstype_regex, argv[i], if ((result = regcomp (fstype_regex, optarg,
REG_EXTENDED | REG_NOSUB)) != 0) REG_EXTENDED | REG_NOSUB)) != 0)
{ {
regerror (result, fstype_regex, buffer, sizeof (buffer)); regerror (result, fstype_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer); eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
} }
continue; break;
}
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) { case 'N':
i++;
if (node_regex) if (node_regex)
free (node_regex); free (node_regex);
node_regex = rc_xmalloc (sizeof (regex_t)); node_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (node_regex, argv[i], if ((result = regcomp (node_regex, optarg,
REG_EXTENDED | REG_NOSUB)) != 0) REG_EXTENDED | REG_NOSUB)) != 0)
{ {
regerror (result, node_regex, buffer, sizeof (buffer)); regerror (result, node_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer); eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
} }
continue; break;
}
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) { case 'R':
i++;
if (skip_regex) if (skip_regex)
free (skip_regex); free (skip_regex);
skip_regex = rc_xmalloc (sizeof (regex_t)); skip_regex = rc_xmalloc (sizeof (regex_t));
if ((result = regcomp (skip_regex, argv[i], if ((result = regcomp (skip_regex, optarg,
REG_EXTENDED | REG_NOSUB)) != 0) REG_EXTENDED | REG_NOSUB)) != 0)
{ {
regerror (result, skip_regex, buffer, sizeof (buffer)); regerror (result, skip_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer); eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
} }
continue; break;
}
if (strcmp (argv[i], "--fstype") == 0) { case 'f':
list_fstype = true; list_fstype = true;
continue; list_nodes = false;
} break;
if (strcmp (argv[i], "--node") == 0) { case 'n':
list_nodes = true; list_nodes = true;
continue; list_fstype = false;
} break;
if (strcmp (argv[i], "--reverse") == 0) {
case 'r':
reverse = true; reverse = true;
continue; break;
default:
exit (EXIT_FAILURE);
} }
if (argv[i][0] != '/') while (optind < argc) {
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]); if (argv[optind][0] != '/')
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
mounts = rc_strlist_add (mounts, argv[i]); mounts = rc_strlist_add (mounts, argv[optind++]);
} }
nodes = find_mounts (node_regex, fstype_regex, mounts, nodes = find_mounts (node_regex, fstype_regex, mounts,
list_nodes, list_fstype); list_nodes, list_fstype);

View File

@ -63,10 +63,10 @@ int main (int argc, char **argv)
int j; int j;
const struct option longopts[] = { const struct option longopts[] = {
{"all", no_argument, NULL, 'a'}, {"all", 0, NULL, 'a'},
{"list", no_argument, NULL, 'l'}, {"list", 0, NULL, 'l'},
{"servicelist", no_argument, NULL, 's'}, {"servicelist", 0, NULL, 's'},
{"unused", no_argument, NULL, 'u'}, {"unused", 0, NULL, 'u'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };

View File

@ -12,6 +12,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <errno.h> #include <errno.h>
#include <getopt.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -815,7 +816,17 @@ int main (int argc, char **argv)
bool doneone = false; bool doneone = false;
char pid[16]; char pid[16];
int retval; 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)); applet = strdup (basename (service));
atexit (cleanup); atexit (cleanup);
@ -914,36 +925,29 @@ int main (int argc, char **argv)
#endif #endif
/* Right then, parse any options there may be */ /* Right then, parse any options there may be */
for (i = 2; i < argc; i++) { while ((c = getopt_long (argc, argv, "dhCDNqv",
if (strlen (argv[i]) < 2 || argv[i][0] != '-' || argv[i][1] != '-') longopts, (int *) 0)) != -1)
continue; switch (c) {
case 'd':
if (strcmp (argv[i], "--debug") == 0)
setenv ("RC_DEBUG", "yes", 1); setenv ("RC_DEBUG", "yes", 1);
else if (strcmp (argv[i], "--help") == 0) { break;
case 'h':
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));
} else if (strcmp (argv[i],"--ifstarted") == 0) case 'C':
ifstarted = true;
else if (strcmp (argv[i], "--nocolour") == 0 ||
strcmp (argv[i], "--nocolor") == 0)
setenv ("RC_NOCOLOR", "yes", 1); setenv ("RC_NOCOLOR", "yes", 1);
else if (strcmp (argv[i], "--nodeps") == 0) break;
case 'D':
deps = false; deps = false;
else if (strcmp (argv[i], "--quiet") == 0) break;
case 'q':
setenv ("RC_QUIET", "yes", 1); setenv ("RC_QUIET", "yes", 1);
else if (strcmp (argv[i], "--verbose") == 0) break;
case 'v':
setenv ("RC_VERBOSE", "yes", 1); setenv ("RC_VERBOSE", "yes", 1);
else if (strcmp (argv[i], "--version") == 0) break;
printf ("version me\n"); default:
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); exit (EXIT_FAILURE);
} }
@ -964,37 +968,35 @@ int main (int argc, char **argv)
/* Now run each option */ /* Now run each option */
retval = EXIT_SUCCESS; retval = EXIT_SUCCESS;
for (i = 2; i < argc; i++) { while (optind < argc) {
optarg = argv[optind++];
/* Abort on a sighup here */ /* Abort on a sighup here */
if (sighup) if (sighup)
exit (EXIT_FAILURE); exit (EXIT_FAILURE);
if (strlen (argv[i]) < 2 ||
(argv[i][0] == '-' && argv[i][1] == '-'))
continue;
/* Export the command we're running. /* Export the command we're running.
This is important as we stamp on the restart function now but This is important as we stamp on the restart function now but
some start/stop routines still need to behave differently if some start/stop routines still need to behave differently if
restarting. */ restarting. */
unsetenv ("RC_CMD"); unsetenv ("RC_CMD");
setenv ("RC_CMD", argv[i], 1); setenv ("RC_CMD", optarg, 1);
doneone = true; doneone = true;
if (strcmp (argv[i], "conditionalrestart") == 0 || if (strcmp (optarg, "conditionalrestart") == 0 ||
strcmp (argv[i], "condrestart") == 0) strcmp (optarg, "condrestart") == 0)
{ {
if (rc_service_state (service, rc_service_started)) if (rc_service_state (service, rc_service_started))
svc_restart (service, deps); svc_restart (service, deps);
} }
else if (strcmp (argv[i], "restart") == 0) else if (strcmp (optarg, "restart") == 0)
svc_restart (service, deps); svc_restart (service, deps);
else if (strcmp (argv[i], "start") == 0) else if (strcmp (optarg, "start") == 0)
svc_start (service, deps); 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); rc_service_state_t r = svc_status (service);
retval = (int) r; retval = (int) r;
} else if (strcmp (argv[i], "stop") == 0) { } else if (strcmp (optarg, "stop") == 0) {
if (in_background) if (in_background)
get_started_services (); get_started_services ();
@ -1014,16 +1016,16 @@ int main (int argc, char **argv)
if (rc_service_state (svc, rc_service_stopped)) if (rc_service_state (svc, rc_service_stopped))
rc_schedule_start_service (service, svc); 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); einfo ("Manually resetting %s to stopped state", applet);
rc_mark_service (applet, rc_service_stopped); rc_mark_service (applet, rc_service_stopped);
uncoldplug (applet); uncoldplug (applet);
} else if (strcmp (argv[i], "help") == 0) { } else if (strcmp (optarg, "help") == 0) {
execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL); execl (RCSCRIPT_HELP, RCSCRIPT_HELP, service, "help", (char *) NULL);
eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s", eerrorx ("%s: failed to exec `" RCSCRIPT_HELP "': %s",
applet, strerror (errno)); applet, strerror (errno));
}else }else
svc_exec (service, argv[i], NULL); svc_exec (service, optarg, NULL);
/* Flush our buffered output if any */ /* Flush our buffered output if any */
eflush (); eflush ();