diff options
author | Mathieu Lirzin <mthl@gnu.org> | 2017-04-25 16:32:49 +0200 |
---|---|---|
committer | Mathieu Lirzin <mthl@gnu.org> | 2017-09-28 18:12:27 +0200 |
commit | d01195784352b29fe13d0676a2f50d60371d007f (patch) | |
tree | c7897c43468210847e454c550ff3ef53d8d4a61a /src/mcron.c | |
parent | c01106387f6d0866ef6fd4801473984cc642e60b (diff) | |
download | mcron-d01195784352b29fe13d0676a2f50d60371d007f.tar.gz mcron-d01195784352b29fe13d0676a2f50d60371d007f.tar.bz2 mcron-d01195784352b29fe13d0676a2f50d60371d007f.zip |
mcron: Handle command line arguments in C with argp
'argp' is a convenient and maintainable way to parse command line arguments.
Guile doesn't offer an equivalent of this, so the command line handling has
been moved to C.
* src/mcron.c (parse_args, parse_opt): New functions.
(inner_main): Call 'parse_args'.
* src/mcron/scripts/mcron.scm (show-help, %options): Delete.
(main): Remove command line handling.
Diffstat (limited to 'src/mcron.c')
-rw-r--r-- | src/mcron.c | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/src/mcron.c b/src/mcron.c index 43afc72..5c63c4b 100644 --- a/src/mcron.c +++ b/src/mcron.c @@ -18,10 +18,13 @@ along with GNU Mcron. If not, see <http://www.gnu.org/licenses/>. */ #include "utils.h" +#include <argp.h> #include <libguile.h> /* Forward declarations. */ static void inner_main (void *closure, int argc, char *argv[]); +static SCM parse_args (int argc, char *argv[]); +static error_t parse_opt (int key, char *arg, struct argp_state *state); int main (int argc, char *argv[]) @@ -44,6 +47,83 @@ main (int argc, char *argv[]) static void inner_main (void *closure, int argc, char *argv[]) { + SCM config = parse_args (argc, argv); scm_set_current_module (scm_c_resolve_module ("mcron scripts mcron")); - scm_call_0 (scm_variable_ref (scm_c_lookup ("main"))); + scm_call_1 (scm_variable_ref (scm_c_lookup ("main")), config); +} + +/* Handle command line arguments. */ +static SCM +parse_args (int argc, char *argv[]) +{ + static struct argp_option options[] = { + {"schedule", 's', "N", 0, + "Display the next N jobs that will be run"}, + {"daemon", 'd', 0, 0, + "Run as a daemon process"}, + {"stdin", 'i', "FORMAT", 0, + "Format of data passed as standard input or file arguments (default guile)"}, + {0, 0, 0, 0, 0} + }; + + static struct argp argp = { + .options = options, + .parser = parse_opt, + .args_doc = "[FILE...]", + .doc = "Run an mcron process according to the specifications in the " + "FILE... (`-' for standard input), or use all the files in " + "~/.config/cron (or the deprecated ~/.cron) with .guile or " + ".vixie extensions." + }; + + SCM config = SCM_EOL; + argp_program_version = PACKAGE_STRING; + argp_program_bug_address = PACKAGE_BUGREPORT; + argp_parse (&argp, argc, argv, 0, NULL, &config); + + return config; +} + +static error_t +parse_opt (int key, char *arg, struct argp_state *state) +{ + SCM *config = state->input; + + switch (key) + { + case 's': + *config = scm_assq_set_x (*config, scm_from_utf8_symbol ("schedule"), + scm_from_int (atoi (arg))); + break; + case 'd': + *config = scm_assq_set_x (*config, scm_from_utf8_symbol ("daemon"), + SCM_BOOL_T); + break; + case 'i': + if (strncmp (arg, "vixie", 6) == 0) + *config = scm_assq_set_x (*config, scm_from_utf8_symbol ("vixie"), + SCM_BOOL_T); + break; + case ARGP_KEY_NO_ARGS: + *config = scm_assq_set_x (*config, scm_from_utf8_symbol ("files"), + SCM_EOL); + break; + case ARGP_KEY_ARGS: + { + SCM lst = SCM_EOL; + int filec = state->argc - state->next; + char **filev = state->argv + state->next; + + for (int i = filec - 1; i >= 0; i--) + lst = scm_cons (scm_from_locale_string (filev[i]), lst); + + *config = scm_assq_set_x (*config, scm_from_utf8_symbol ("files"), + lst); + break; + } + case ARGP_KEY_ARG: + default: + return ARGP_ERR_UNKNOWN; + } + return 0; } |