Improved CLI experience
This commit is contained in:
39
main.c
39
main.c
@@ -19,6 +19,7 @@
|
|||||||
static bool _to_work = true;
|
static bool _to_work = true;
|
||||||
static int _sig_fd = -1;
|
static int _sig_fd = -1;
|
||||||
static int _server_fd = -1;
|
static int _server_fd = -1;
|
||||||
|
static bool _is_server = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Private API
|
* 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("A") ? true : false;
|
||||||
required_args_present &= cli_arg_get("P") ? true : false;
|
required_args_present &= cli_arg_get("P") ? true : false;
|
||||||
required_args_present &= cli_arg_get("E") ? 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.
|
// Initialize signals.
|
||||||
@@ -167,6 +185,7 @@ static void on_fd_event(int fd, uint32_t events) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
// Initialization
|
||||||
if (!parse_cli_args(argc, argv)) {
|
if (!parse_cli_args(argc, argv)) {
|
||||||
print_usage_text();
|
print_usage_text();
|
||||||
return 1;
|
return 1;
|
||||||
@@ -178,12 +197,14 @@ int main(int argc, char **argv) {
|
|||||||
loop_deinit();
|
loop_deinit();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
if (!server_init()) {
|
if (_is_server) {
|
||||||
signals_deinit();
|
if (!server_init()) {
|
||||||
loop_deinit();
|
signals_deinit();
|
||||||
return 1;
|
loop_deinit();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Loop
|
||||||
while (_to_work) {
|
while (_to_work) {
|
||||||
if (!loop_wait()) {
|
if (!loop_wait()) {
|
||||||
fprintf(stderr, "[!] loop_wait returns false, stopping...\n");
|
fprintf(stderr, "[!] loop_wait returns false, stopping...\n");
|
||||||
@@ -191,8 +212,10 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
fprintf(stderr, "--- loop ---\n");
|
fprintf(stderr, "--- loop ---\n");
|
||||||
}
|
}
|
||||||
|
// Termination
|
||||||
server_close();
|
if (_is_server) {
|
||||||
|
server_close();
|
||||||
|
}
|
||||||
signals_deinit();
|
signals_deinit();
|
||||||
loop_deinit();
|
loop_deinit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
9
utils.c
9
utils.c
@@ -22,16 +22,21 @@ typedef struct {
|
|||||||
*/
|
*/
|
||||||
static cli_arg_t _cli_args[MAX_CLI_ARGS];
|
static cli_arg_t _cli_args[MAX_CLI_ARGS];
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Public data
|
||||||
|
*/
|
||||||
|
int flag_verbose = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Public API
|
* Public API
|
||||||
*/
|
*/
|
||||||
void print_usage_text() {
|
void print_usage_text() {
|
||||||
printf("ipoim -M <mode> -A <address> -P <port> -C <carrier> ...\n");
|
printf("ipoim -M <mode> -A <address> -P <port> -C <carrier> ...\n");
|
||||||
printf(" --- MANDATORY ARGUMENTS ---\n");
|
printf(" --- MANDATORY ARGUMENTS ---\n");
|
||||||
printf(" -M <mode> - operation mode ('provider' or 'last-mile')\n");
|
printf(" -M <mode> - operation mode ('server' or 'client')\n");
|
||||||
printf(" -A <address> - IPv4 to listen on/connect to\n");
|
printf(" -A <address> - IPv4 to listen on/connect to\n");
|
||||||
printf(" -P <port> - port to listen on/connect to\n");
|
printf(" -P <port> - port to listen on/connect to\n");
|
||||||
printf(" -E <carrier> - extension to use, one of:\n");
|
printf(" -E <extension> - extension to use, one of:\n");
|
||||||
printf(" * pipe - use STDIN and STDOUT\n");
|
printf(" * pipe - use STDIN and STDOUT\n");
|
||||||
printf(" --- OPTIONAL ARGUMENTS ---\n");
|
printf(" --- OPTIONAL ARGUMENTS ---\n");
|
||||||
printf(" +V - be verbose\n");
|
printf(" +V - be verbose\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user