diff --git a/server/src/main.c b/server/src/main.c index befd304..a099589 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -1,67 +1,25 @@ // main.c #include -#include -#include -#include #include -#include - #include "error.h" -#include "daemon.h" - -struct options_t { - bool daemonise; -}; - - -static enum error_t parse_arguments( - struct options_t *options, - int argc, char **argv -) { - static struct option const long_options[] = { - { "daemon", no_argument, 0, 'd' }, - }; - - while (true) { - int option_index = 0; - - int c = getopt_long(argc, argv, "d", long_options, &option_index); - if (c == -1) break; - - switch (c) { - case 'd': options->daemonise = true; break; - default: return ERR_INPUT; - } - } - - return ERR_OK; -} - +#include "opts.h" int main(int argc, char **argv) { enum error_t err = ERR_OK; struct options_t options = { false }; - err = parse_arguments(&options, argc, argv); + err = opts_parse(&options, argc, argv); if (err) goto handle_error; - if (options.daemonise) { - err = daemonise(); - if (err) goto handle_error; + err = opts_init(&options); + if (err) goto handle_error; - openlog(NULL, LOG_PID, LOG_DAEMON); - syslog(LOG_NOTICE, "Daemon initialised"); - } - - goto handle_exit; - // Is this the best way to do this? I didn't want to duplicate - // closing the syslog + opts_free(&options); + return ERR_OK; handle_error: printf("ERROR: %s\n", ERROR_STRS[err]); -handle_exit: - if (options.daemonise) closelog(); return err; } diff --git a/server/src/opts.c b/server/src/opts.c new file mode 100644 index 0000000..af37844 --- /dev/null +++ b/server/src/opts.c @@ -0,0 +1,53 @@ +// opts.c + +#include +#include +#include + +#include + +#include "opts.h" +#include "daemon.h" + +enum error_t opts_parse(struct options_t *options, int argc, char **argv) { + struct option const long_options[] = { + { "daemon", no_argument, 0, 'd' }, + }; + + while (true) { + int option_index = 0; + + int c = getopt_long(argc, argv, "d", long_options, &option_index); + if (c == -1) break; + + switch (c) { + case 'd': options->daemonise = true; break; + default: return ERR_INPUT; + } + } + + return ERR_OK; +} + + +enum error_t opts_init(struct options_t const *options) { + assert(options != NULL); + enum error_t err = ERR_OK; + + if (options->daemonise) { + err = daemonise(); + if (err) return err; + + openlog(NULL, LOG_PID, LOG_DAEMON); + syslog(LOG_NOTICE, "Daemon initialised :-)"); + } + + return ERR_OK; +} + + +void opts_free(struct options_t const *options) { + assert(options != NULL); + + if (options->daemonise) closelog(); +} diff --git a/server/src/opts.h b/server/src/opts.h new file mode 100644 index 0000000..b6b6712 --- /dev/null +++ b/server/src/opts.h @@ -0,0 +1,16 @@ +#ifndef OPTS_H +#define OPTS_H + +#include + +#include "error.h" + +struct options_t { + bool daemonise; +}; + +enum error_t opts_parse(struct options_t *, int, char **); +enum error_t opts_init(struct options_t const *); +void opts_free(struct options_t const *); + +#endif