clean up server/main by moving cli option handling to separate files

This commit is contained in:
snit 2024-10-05 01:55:42 -05:00
parent 2db264c027
commit 867bbb1c37
3 changed files with 75 additions and 48 deletions

View File

@ -1,67 +1,25 @@
// main.c // main.c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/syslog.h>
#include <unistd.h> #include <unistd.h>
#include <getopt.h>
#include "error.h" #include "error.h"
#include "daemon.h" #include "opts.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;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
enum error_t err = ERR_OK; enum error_t err = ERR_OK;
struct options_t options = { false }; struct options_t options = { false };
err = parse_arguments(&options, argc, argv); err = opts_parse(&options, argc, argv);
if (err) goto handle_error; if (err) goto handle_error;
if (options.daemonise) { err = opts_init(&options);
err = daemonise();
if (err) goto handle_error; if (err) goto handle_error;
openlog(NULL, LOG_PID, LOG_DAEMON); opts_free(&options);
syslog(LOG_NOTICE, "Daemon initialised"); return ERR_OK;
}
goto handle_exit;
// Is this the best way to do this? I didn't want to duplicate
// closing the syslog
handle_error: handle_error:
printf("ERROR: %s\n", ERROR_STRS[err]); printf("ERROR: %s\n", ERROR_STRS[err]);
handle_exit:
if (options.daemonise) closelog();
return err; return err;
} }

53
server/src/opts.c Normal file
View File

@ -0,0 +1,53 @@
// opts.c
#include <stdlib.h>
#include <assert.h>
#include <sys/syslog.h>
#include <getopt.h>
#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();
}

16
server/src/opts.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef OPTS_H
#define OPTS_H
#include <stdbool.h>
#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