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'},
if (fstype_regex) { "skip-regex", 1, NULL, 'S'},
free (fstype_regex); { "fstype", 0, NULL, 'f'},
fstype_regex = rc_xmalloc (sizeof (regex_t)); { "node", 0, NULL, 'n'},
if ((result = regcomp (fstype_regex, argv[i], { "reverse", 0, NULL, 'r'},
REG_EXTENDED | REG_NOSUB)) != 0) { NULL, 0, NULL, 0}
{ };
regerror (result, fstype_regex, buffer, sizeof (buffer));
eerrorx ("%s: invalid regex `%s'", argv[0], buffer); while ((c = getopt_long (argc, argv, "F:N:S;fnr",
} longopts, (int *) 0)) != -1)
continue; 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;
case 'f':
list_fstype = true;
list_nodes = false;
break;
case 'n':
list_nodes = true;
list_fstype = false;
break;
case 'r':
reverse = true;
break;
default:
exit (EXIT_FAILURE);
} }
if (strcmp (argv[i], "--node-regex") == 0 && (i + 1 < argc)) { while (optind < argc) {
i++; if (argv[optind][0] != '/')
if (node_regex) eerrorx ("%s: `%s' is not a mount point", argv[0], argv[optind]);
free (node_regex); mounts = rc_strlist_add (mounts, argv[optind++]);
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;
}
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;
}
if (strcmp (argv[i], "--fstype") == 0) {
list_fstype = true;
continue;
}
if (strcmp (argv[i], "--node") == 0) {
list_nodes = true;
continue;
}
if (strcmp (argv[i], "--reverse") == 0) {
reverse = true;
continue;
}
if (argv[i][0] != '/')
eerrorx ("%s: `%s' is not a mount point", argv[0], argv[i]);
mounts = rc_strlist_add (mounts, argv[i]);
} }
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,11 +63,11 @@ 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}
}; };
while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1) while ((c = getopt_long(argc, argv, "alsu", longopts, &option_index)) != -1)

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,38 +925,31 @@ 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); break;
else if (strcmp (argv[i], "--help") == 0) { 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; setenv ("RC_NOCOLOR", "yes", 1);
else if (strcmp (argv[i], "--nocolour") == 0 || break;
strcmp (argv[i], "--nocolor") == 0) case 'D':
setenv ("RC_NOCOLOR", "yes", 1); deps = false;
else if (strcmp (argv[i], "--nodeps") == 0) break;
deps = false; case 'q':
else if (strcmp (argv[i], "--quiet") == 0) setenv ("RC_QUIET", "yes", 1);
setenv ("RC_QUIET", "yes", 1); break;
else if (strcmp (argv[i], "--verbose") == 0) 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 exit (EXIT_FAILURE);
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);
}
if (rc_is_env ("IN_HOTPLUG", "1")) { if (rc_is_env ("IN_HOTPLUG", "1")) {
if (! rc_is_env ("RC_HOTPLUG", "yes") || ! rc_allow_plug (applet)) 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 */ /* 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 ();