Improved CLI experience
This commit is contained in:
29
main.c
29
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 (_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");
|
||||
}
|
||||
|
||||
// Termination
|
||||
if (_is_server) {
|
||||
server_close();
|
||||
}
|
||||
signals_deinit();
|
||||
loop_deinit();
|
||||
return 0;
|
||||
|
||||
9
utils.c
9
utils.c
@@ -22,16 +22,21 @@ typedef struct {
|
||||
*/
|
||||
static cli_arg_t _cli_args[MAX_CLI_ARGS];
|
||||
|
||||
/*
|
||||
* Public data
|
||||
*/
|
||||
int flag_verbose = 0;
|
||||
|
||||
/*
|
||||
* Public API
|
||||
*/
|
||||
void print_usage_text() {
|
||||
printf("ipoim -M <mode> -A <address> -P <port> -C <carrier> ...\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(" -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(" --- OPTIONAL ARGUMENTS ---\n");
|
||||
printf(" +V - be verbose\n");
|
||||
|
||||
Reference in New Issue
Block a user