From 743f9bc29f74df7f0cf2071937c5968c2476ab64 Mon Sep 17 00:00:00 2001 From: dhfelix Date: Fri, 25 Jan 2019 21:09:13 -0600 Subject: [PATCH] Add getopt to handle argc and argv - Needs more tweaks --- src/main.c | 92 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 4951ff98..e3556902 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "common.h" @@ -12,6 +13,17 @@ #include "object/tal.h" #include "rsync/rsync.h" +struct rpki_config { + /* tal file path*/ + char *tal; + /* Local repository path */ + char *local_repository; + /* Disable rsync downloads */ + bool disable_rsync; + /* Shuffle uris in tal */ + bool shuffle_uris; +}; + /** * Registers the RPKI-specific OIDs in the SSL library. * LibreSSL needs it; not sure about OpenSSL. @@ -100,38 +112,90 @@ end: return error; } +static int +handle_args(int argc, char **argv, struct rpki_config *config) +{ + int opt, error = 0; + + static struct option long_options[] = { + {"tal", no_argument, NULL, 't'}, + {"local_repository", required_argument, NULL, 'l'}, + {"disable_rsync", no_argument, 0, 'r'}, + {"shuffle_uris", no_argument, 0, 's'}, + {0,0,0,} + }; + + config->disable_rsync = false; + config->shuffle_uris = false; + config->local_repository = NULL; + config->tal = NULL; + + while ((opt = getopt_long(argc, argv, "t:l:rs", long_options, NULL)) + != -1) { + switch (opt) { + case 't' : + config->tal = optarg; + break; + case 'l' : + config->local_repository = optarg; + break; + case 'r': + config->disable_rsync = true; + break; + case 's': + config->shuffle_uris = true; + break; + default: + return pr_err("some usage hints.");/* TODO */ + } + } + + if (config->tal == NULL) { + fprintf(stderr, "Missing flag --tal \n"); + error = -EINVAL; + } + if(config->local_repository == NULL) { + fprintf(stderr, "Missing flag --local_repository \n"); + error = -EINVAL; + } + + pr_debug("TAL file : %s", config->tal); + pr_debug("Local repository : %s", config->local_repository); + pr_debug("Disable rsync : %s", config->disable_rsync + ? "true" : "false"); + pr_debug("shuffle uris : %s", config->shuffle_uris + ? "true" : "false"); + + return error; +} + int main(int argc, char **argv) { + struct rpki_config config; struct tal *tal; int error; - bool is_rsync_active = true; - bool shuffle_uris = false; + error = handle_args(argc, argv, &config); + if (error) + return error; print_stack_trace_on_segfault(); - if (argc < 3) - return pr_err("Repository path as first argument and TAL file as second argument, please."); - if (argc >= 4) - is_rsync_active = false; - if (argc >= 5) - shuffle_uris = true; /* TODO lol fix this */ - - error = rsync_init(is_rsync_active); + error = rsync_init(!config.disable_rsync); if (error) return error; add_rpki_oids(); thvar_init(); fnstack_store(); - fnstack_push(argv[2]); + fnstack_push(config.tal); - repository = argv[1]; + repository = config.local_repository; repository_len = strlen(repository); - error = tal_load(argv[2], &tal); + error = tal_load(config.tal, &tal); if (!error) { - if (shuffle_uris) + if (config.shuffle_uris) tal_shuffle_uris(tal); error = foreach_uri(tal, handle_tal_uri); error = (error >= 0) ? 0 : error; -- 2.47.3