From 8c5ac1f77d5342b133f08da70f2faf347d61eeb9 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 7 Jun 2014 14:36:37 -0400 Subject: [PATCH] proper commandline args --- src/main.c | 117 +++++++++++++++++++++++++++++++------------- src/settings.c | 4 +- src/toxic_strings.c | 2 +- 3 files changed, 87 insertions(+), 36 deletions(-) diff --git a/src/main.c b/src/main.c index 854e397..02e9cf3 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -79,7 +80,12 @@ ToxAv *av; char *DATA_FILE = NULL; ToxWindow *prompt = NULL; -static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ +struct arg_opts { + int load_data_file; + int use_ipv4; + char config_path[MAX_STR_SIZE]; + char nodes_path[MAX_STR_SIZE]; +} arg_opts; struct _Winthread Winthread; struct user_settings *user_settings = NULL; @@ -147,6 +153,9 @@ static Tox *init_tox(int ipv4) m = tox_new(0); } + if (ipv4) + fprintf(stderr, "Forcing IPv4 connection\n"); + if (m == NULL) return NULL; @@ -262,7 +271,12 @@ int init_connection(Tox *m) */ if (!srvlist_loaded) { srvlist_loaded = true; - int res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); + int res; + + if (!arg_opts.nodes_path[0]) + res = nodelist_load(PACKAGE_DATADIR "/DHTnodes"); + else + res = nodelist_load(arg_opts.nodes_path); if (linecnt < 1) return res; @@ -332,7 +346,7 @@ static void load_friendlist(Tox *m) */ int store_data(Tox *m, char *path) { - if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ + if (arg_opts.load_data_file == 0) /*If file loading/saving is disabled*/ return 0; if (path == NULL) @@ -370,7 +384,7 @@ int store_data(Tox *m, char *path) static void load_data(Tox *m, char *path) { - if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ + if (arg_opts.load_data_file == 0) /*If file loading/saving is disabled*/ return; FILE *fd; @@ -459,36 +473,76 @@ void *thread_winref(void *data) } } +void print_usage(void) +{ + fprintf(stderr, "Usage: Print usage here\n"); +} + +void set_default_opts(void) +{ + arg_opts.use_ipv4 = 0; + arg_opts.load_data_file = 1; +} + +void parse_args(int argc, char *argv[]) +{ + set_default_opts(); + + const char *opts_str = "4xf:c:n:"; + int opt, indexptr; + + while (true) { + static struct option long_opts[] = { + {"file", required_argument, 0, 'f'}, + {"nodata", no_argument, 0, 'x'}, + {"ipv4", no_argument, 0, '4'}, + {"config", required_argument, 0, 'c'}, + {"nodes", required_argument, 0, 'n'}, + }; + + opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr); + + if (opt == -1) + break; + + switch (opt) { + case 'f': + DATA_FILE = strdup(optarg); + break; + + case 'x': + arg_opts.load_data_file = 0; + break; + + case '4': + arg_opts.use_ipv4 = 1; + break; + + case 'c': + snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg); + break; + + case 'n': + snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); + break; + + default: + print_usage(); + exit(EXIT_FAILURE); + } + } +} + int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); int config_err = 0; - f_loadfromfile = 1; - int f_flag = 0; - int i = 0; - int f_use_ipv4 = 0; + parse_args(argc, argv); /* Make sure all written files are read/writeable only by the current user. */ umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); - for (i = 0; i < argc; ++i) { - if (argv[i] == NULL) - break; - else if (argv[i][0] == '-') { - if (argv[i][1] == 'f') { - if (argv[i + 1] != NULL) - DATA_FILE = strdup(argv[i + 1]); - else - f_flag = -1; - } else if (argv[i][1] == 'n') { - f_loadfromfile = 0; - } else if (argv[i][1] == '4') { - f_use_ipv4 = 1; - } - } - } - config_err = create_user_config_dir(user_config_dir); if (DATA_FILE == NULL ) { @@ -521,9 +575,11 @@ int main(int argc, char *argv[]) } memset(user_settings, 0, sizeof(struct user_settings)); - int settings_err = settings_load(user_settings, NULL); - Tox *m = init_tox(f_use_ipv4); + char *p = arg_opts.config_path[0] ? arg_opts.config_path : NULL; + int settings_err = settings_load(user_settings, p); + + Tox *m = init_tox(arg_opts.use_ipv4); init_term(); if (m == NULL) { @@ -532,7 +588,7 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (f_loadfromfile) + if (arg_opts.load_data_file) load_data(m, DATA_FILE); prompt = init_windows(m); @@ -568,11 +624,6 @@ int main(int argc, char *argv[]) #endif /* _SUPPORT_AUDIO */ - if (f_flag == -1) { - msg = "You passed '-f' without giving an argument. Defaulting to 'data' for a keyfile..."; - line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); - } - if (config_err) { msg = "Unable to determine configuration directory. Defaulting to 'data' for a keyfile..."; line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); diff --git a/src/settings.c b/src/settings.c index add9e47..6efbc3e 100644 --- a/src/settings.c +++ b/src/settings.c @@ -178,8 +178,8 @@ int settings_load(struct user_settings *s, char *path) if (line[0] == '#' || !line[0]) continue; - const char *key = strtok(line, ":"); - const char *val = strtok(NULL, ";"); + char *key = strtok(line, ":"); + char *val = strtok(NULL, ";"); if (key == NULL || val == NULL) continue; diff --git a/src/toxic_strings.c b/src/toxic_strings.c index 9cbc152..4f7f9ae 100644 --- a/src/toxic_strings.c +++ b/src/toxic_strings.c @@ -128,7 +128,7 @@ void rm_trailing_spaces_buf(ChatContext *ctx) } ctx->len = i + 1; - ctx->pos = MIN(ctx->pos, i + 1); + ctx->pos = MIN(ctx->pos, ctx->len); ctx->line[ctx->len] = L'\0'; }