1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 00:33:02 +01:00

Options parsing code cleanup

This commit is contained in:
jfreegman 2021-11-03 16:23:38 -04:00
parent bcdec5d624
commit 34b7c0a0d8
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -1204,21 +1204,23 @@ static void parse_args(int argc, char *argv[])
}; };
const char *opts_str = "4bdehLotuxvc:f:l:n:r:p:P:T:"; const char *opts_str = "4bdehLotuxvc:f:l:n:r:p:P:T:";
int opt, indexptr; int opt = 0;
long int port = 0; int indexptr = 0;
while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) { while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
switch (opt) { switch (opt) {
case '4': case '4': {
arg_opts.use_ipv4 = 1; arg_opts.use_ipv4 = 1;
break; break;
}
case 'b': case 'b': {
arg_opts.debug = 1; arg_opts.debug = 1;
queue_init_message("stderr enabled"); queue_init_message("stderr enabled");
break; break;
}
case 'c': case 'c': {
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
break; break;
@ -1231,17 +1233,20 @@ static void parse_args(int argc, char *argv[])
} }
break; break;
}
case 'd': case 'd': {
arg_opts.default_locale = 1; arg_opts.default_locale = 1;
queue_init_message("Using default POSIX locale"); queue_init_message("Using default POSIX locale");
break; break;
}
case 'e': case 'e': {
arg_opts.encrypt_data = 1; arg_opts.encrypt_data = 1;
break; break;
}
case 'f': case 'f': {
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
break; break;
@ -1279,8 +1284,9 @@ static void parse_args(int argc, char *argv[])
queue_init_message("Using '%s' data file", DATA_FILE); queue_init_message("Using '%s' data file", DATA_FILE);
break; break;
}
case 'l': case 'l': {
if (optarg) { if (optarg) {
arg_opts.logging = true; arg_opts.logging = true;
@ -1300,13 +1306,15 @@ static void parse_args(int argc, char *argv[])
} }
break; break;
}
case 'L': case 'L': {
arg_opts.disable_local_discovery = 1; arg_opts.disable_local_discovery = 1;
queue_init_message("Local discovery disabled"); queue_init_message("Local discovery disabled");
break; break;
}
case 'n': case 'n': {
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
break; break;
@ -1314,48 +1322,38 @@ static void parse_args(int argc, char *argv[])
snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg); snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
break; break;
}
case 'o': case 'o': {
arg_opts.no_connect = 1; arg_opts.no_connect = 1;
queue_init_message("DHT disabled"); queue_init_message("DHT disabled");
break; break;
case 'p':
if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt);
break;
} }
case 'p': {
arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5; arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5;
snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);
if (++optind > argc || argv[optind - 1][0] == '-') {
exit_toxic_err("Proxy error", FATALERR_PROXY);
} }
port = strtol(argv[optind - 1], NULL, 10); // Intentional fallthrough
if (port <= 0 || port > MAX_PORT_RANGE) { case 'P': {
exit_toxic_err("Proxy error", FATALERR_PROXY);
}
arg_opts.proxy_port = port;
break;
case 'P':
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
arg_opts.proxy_type = TOX_PROXY_TYPE_NONE;
break; break;
} }
if (arg_opts.proxy_type == TOX_PROXY_TYPE_NONE) {
arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP; arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP;
}
snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg); snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);
if (++optind > argc || argv[optind - 1][0] == '-') { if (++optind > argc || argv[optind - 1][0] == '-') {
exit_toxic_err("Proxy error", FATALERR_PROXY); exit_toxic_err("Proxy error", FATALERR_PROXY);
} }
port = strtol(argv[optind - 1], NULL, 10); long int port = strtol(argv[optind - 1], NULL, 10);
if (port <= 0 || port > MAX_PORT_RANGE) { if (port <= 0 || port > MAX_PORT_RANGE) {
exit_toxic_err("Proxy error", FATALERR_PROXY); exit_toxic_err("Proxy error", FATALERR_PROXY);
@ -1363,8 +1361,9 @@ static void parse_args(int argc, char *argv[])
arg_opts.proxy_port = port; arg_opts.proxy_port = port;
break; break;
}
case 'r': case 'r': {
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
break; break;
@ -1377,18 +1376,20 @@ static void parse_args(int argc, char *argv[])
} }
break; break;
}
case 't': case 't': {
arg_opts.force_tcp = 1; arg_opts.force_tcp = 1;
break; break;
}
case 'T': case 'T': {
if (optarg == NULL) { if (optarg == NULL) {
queue_init_message("Invalid argument for option: %d", opt); queue_init_message("Invalid argument for option: %d", opt);
break; break;
} }
port = strtol(optarg, NULL, 10); long int port = strtol(optarg, NULL, 10);
if (port <= 0 || port > MAX_PORT_RANGE) { if (port <= 0 || port > MAX_PORT_RANGE) {
port = MAX_PORT_RANGE; port = MAX_PORT_RANGE;
@ -1396,23 +1397,27 @@ static void parse_args(int argc, char *argv[])
arg_opts.tcp_port = port; arg_opts.tcp_port = port;
break; break;
}
case 'u': case 'u': {
arg_opts.unencrypt_data = 1; arg_opts.unencrypt_data = 1;
break; break;
}
case 'v': case 'v': {
print_version(); print_version();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
}
case 'h': case 'h':
// Intentional fallthrough // Intentional fallthrough
default: default: {
print_usage(); print_usage();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
} }
}
} }
/* Initializes the default config directory and data files used by toxic. /* Initializes the default config directory and data files used by toxic.