Improved CLI experience
This commit is contained in:
39
main.c
39
main.c
@@ -19,6 +19,7 @@
|
||||
static bool _to_work = true;
|
||||
static int _sig_fd = -1;
|
||||
static int _server_fd = -1;
|
||||
static bool _is_server = false;
|
||||
|
||||
/*
|
||||
* Private API
|
||||
@@ -68,7 +69,24 @@ bool parse_cli_args(int argc, char** argv) {
|
||||
required_args_present &= cli_arg_get("A") ? true : false;
|
||||
required_args_present &= cli_arg_get("P") ? true : false;
|
||||
required_args_present &= cli_arg_get("E") ? true : false;
|
||||
return required_args_present;
|
||||
if (!required_args_present) {
|
||||
fprintf(stderr, "[!] Some required arguments are missing\n");
|
||||
return false;
|
||||
}
|
||||
const char *mode = cli_arg_get("M");
|
||||
if (strcmp(mode, "server") && strcmp(mode, "client")) {
|
||||
fprintf(stderr, "[!] Unknown mode '%s'\n", mode);
|
||||
return false;
|
||||
}
|
||||
_is_server = !strcmp(mode, "server");
|
||||
flag_verbose = cli_arg_get("V") ? 1 : 0;
|
||||
if (flag_verbose) {
|
||||
fprintf(stdout, "[I] Being verbose\n");
|
||||
fprintf(stdout, "[I] - Mode ------ %s\n", cli_arg_get("M"));
|
||||
fprintf(stdout, "[I] - Address --- %s:%d\n", cli_arg_get("A"), atoi(cli_arg_get("P")));
|
||||
fprintf(stdout, "[I] - Extension - %s\n", cli_arg_get("E"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Initialize signals.
|
||||
@@ -167,6 +185,7 @@ static void on_fd_event(int fd, uint32_t events) {
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
// Initialization
|
||||
if (!parse_cli_args(argc, argv)) {
|
||||
print_usage_text();
|
||||
return 1;
|
||||
@@ -178,12 +197,14 @@ int main(int argc, char **argv) {
|
||||
loop_deinit();
|
||||
return 1;
|
||||
}
|
||||
if (!server_init()) {
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 1;
|
||||
if (_is_server) {
|
||||
if (!server_init()) {
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop
|
||||
while (_to_work) {
|
||||
if (!loop_wait()) {
|
||||
fprintf(stderr, "[!] loop_wait returns false, stopping...\n");
|
||||
@@ -191,8 +212,10 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
fprintf(stderr, "--- loop ---\n");
|
||||
}
|
||||
|
||||
server_close();
|
||||
// Termination
|
||||
if (_is_server) {
|
||||
server_close();
|
||||
}
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user