Added argument parser and set all logs to STDERR
This commit is contained in:
75
utils.c
75
utils.c
@@ -0,0 +1,75 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
* Macros
|
||||
*/
|
||||
#define MAX_CLI_ARGS (20)
|
||||
|
||||
/*
|
||||
* Type definitions
|
||||
*/
|
||||
typedef struct {
|
||||
const char *key;
|
||||
char *value;
|
||||
} cli_arg_t;
|
||||
|
||||
/*
|
||||
* Data
|
||||
*/
|
||||
static cli_arg_t _cli_args[MAX_CLI_ARGS];
|
||||
|
||||
/*
|
||||
* 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(" -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(" * pipe - use STDIN and STDOUT\n");
|
||||
printf(" --- OPTIONAL ARGUMENTS ---\n");
|
||||
printf(" +V - be verbose\n");
|
||||
}
|
||||
|
||||
bool cli_arg_set(const char *key, char *value) {
|
||||
int index_to_use = -1;
|
||||
for (int i = 0; i < MAX_CLI_ARGS; ++i) {
|
||||
// empty entry
|
||||
if (_cli_args[i].key == NULL) {
|
||||
// update index only if no place is found yet
|
||||
if (index_to_use == -1)
|
||||
index_to_use = i;
|
||||
}
|
||||
// entry with the same name
|
||||
else if (!strcmp(_cli_args[i].key, key)) {
|
||||
index_to_use = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// no space for the argument
|
||||
if (index_to_use == -1) {
|
||||
return false;
|
||||
}
|
||||
_cli_args[index_to_use].key = key;
|
||||
_cli_args[index_to_use].value = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
const char *cli_arg_get(const char *key) {
|
||||
for (int i = 0; i < MAX_CLI_ARGS; ++i) {
|
||||
if (_cli_args[i].key == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (strcmp(_cli_args[i].key, key)) {
|
||||
continue;
|
||||
}
|
||||
return _cli_args[i].value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user