aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--servicecmd/disable.c7
-rw-r--r--servicecmd/enable.c7
-rw-r--r--servicecmd/servicecmd.c17
-rw-r--r--servicecmd/servicecmd.h15
4 files changed, 36 insertions, 10 deletions
diff --git a/servicecmd/disable.c b/servicecmd/disable.c
index 1901381..8a6f9f3 100644
--- a/servicecmd/disable.c
+++ b/servicecmd/disable.c
@@ -31,18 +31,15 @@ static int cmd_disable(int argc, char **argv)
char *linkname, *ptr;
struct stat sb;
- if (argc < 2 || argc > 3) {
- fputs("Wrong number of arguments for `disable'.\n"
- "Try `service help disable' for more information.\n",
- stderr);
+ if (check_arguments(argv[0], argc, 2, 3))
return EXIT_FAILURE;
- }
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
;
if (*ptr != '\0') {
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
+ tell_read_help(argv[0]);
return EXIT_FAILURE;
}
diff --git a/servicecmd/enable.c b/servicecmd/enable.c
index 73ea01e..5d4195e 100644
--- a/servicecmd/enable.c
+++ b/servicecmd/enable.c
@@ -31,18 +31,15 @@ static int cmd_enable(int argc, char **argv)
int ret = EXIT_FAILURE;
struct stat sb;
- if (argc < 2 || argc > 3) {
- fputs("Wrong number of arguments for `enable'.\n"
- "Try `service help enable' for more information.\n",
- stderr);
+ if (check_arguments(argv[0], argc, 2, 3))
return EXIT_FAILURE;
- }
for (ptr = argv[1]; isalnum(*ptr) || *ptr == '_'; ++ptr)
;
if (*ptr != '\0') {
fprintf(stderr, "Invalid service name '%s'\n", argv[1]);
+ tell_read_help(argv[0]);
return EXIT_FAILURE;
}
diff --git a/servicecmd/servicecmd.c b/servicecmd/servicecmd.c
index ff37740..84e6a32 100644
--- a/servicecmd/servicecmd.c
+++ b/servicecmd/servicecmd.c
@@ -55,6 +55,23 @@ void usage(int status)
exit(status);
}
+void tell_read_help(const char *cmd)
+{
+ fprintf(stderr, "Try `%s help %s' for more information.\n",
+ __progname, cmd);
+}
+
+int check_arguments(const char *cmd, int argc, int minc, int maxc)
+{
+ if (argc >= minc && argc <= maxc)
+ return 0;
+
+ fprintf(stderr, "Too %s arguments for `%s'\n",
+ argc > maxc ? "many" : "few", cmd);
+ tell_read_help(cmd);
+ return -1;
+}
+
int main(int argc, char **argv)
{
command_t *cmd;
diff --git a/servicecmd/servicecmd.h b/servicecmd/servicecmd.h
index 27204d0..b839799 100644
--- a/servicecmd/servicecmd.h
+++ b/servicecmd/servicecmd.h
@@ -61,6 +61,21 @@ extern command_t *commands;
void usage(int status) NORETURN;
/*
+ Write a message to stderr that advises the user how to consult the
+ help text for a specific command.
+*/
+void tell_read_help(const char *cmd);
+
+/*
+ Check if the argument count is within specified bounds (minc and maxc
+ inclusive). If it is, return 0.
+
+ If it isn't, complain about a wrong number of arguments for a
+ command (cmd), tell the user to consult the help text and return -1.
+*/
+int check_arguments(const char *cmd, int argc, int minc, int maxc);
+
+/*
To implement a new command, add a global, static instance of a
command_t (or derived) structure to a C file and pass it to this
macro to have it automatically registered on program startup.