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
#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;
for (i = 1; i < argc; i++) {
if (strcmp (argv[i], "--fstype-regex") == 0 && (i + 1 < argc)) {
i++;
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, argv[i],
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);
}
continue;
}
break;
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) {
i++;
case 'N':
if (node_regex)
free (node_regex);
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)
{
regerror (result, node_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
break;
if (strcmp (argv[i], "--skip-regex") == 0 && (i + 1 < argc)) {
i++;
case 'R':
if (skip_regex)
free (skip_regex);
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)
{
regerror (result, skip_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer);
}
continue;
}
break;
if (strcmp (argv[i], "--fstype") == 0) {
case 'f':
list_fstype = true;
continue;
}
list_nodes = false;
break;
if (strcmp (argv[i], "--node") == 0) {
case 'n':
list_nodes = true;
continue;
}
if (strcmp (argv[i], "--reverse") == 0) {
list_fstype = false;
break;
case 'r':
reverse = true;
continue;
break;
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);

View File

@ -63,10 +63,10 @@ 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'},
{"all", 0, NULL, 'a'},
{"list", 0, NULL, 'l'},
{"servicelist", 0, NULL, 's'},
{"unused", 0, NULL, 'u'},
{NULL, 0, NULL, 0}
};

View File

@ -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,36 +925,29 @@ 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)
while ((c = getopt_long (argc, argv, "dhCDNqv",
longopts, (int *) 0)) != -1)
switch (c) {
case 'd':
setenv ("RC_DEBUG", "yes", 1);
else if (strcmp (argv[i], "--help") == 0) {
break;
case 'h':
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)
case 'C':
setenv ("RC_NOCOLOR", "yes", 1);
else if (strcmp (argv[i], "--nodeps") == 0)
break;
case 'D':
deps = false;
else if (strcmp (argv[i], "--quiet") == 0)
break;
case 'q':
setenv ("RC_QUIET", "yes", 1);
else if (strcmp (argv[i], "--verbose") == 0)
break;
case 'v':
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);
break;
default:
exit (EXIT_FAILURE);
}
@ -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 ();