Commit 5a579d96 authored by Lucas Russo's avatar Lucas Russo

src/{apps,dev_mngr}: add getopt for command-line parsing

parent cd4a8bdc
This diff is collapsed.
...@@ -13,95 +13,135 @@ static devio_err_e _spawn_platform_smios (devio_t *devio, devio_type_e devio_typ ...@@ -13,95 +13,135 @@ static devio_err_e _spawn_platform_smios (devio_t *devio, devio_type_e devio_typ
uint32_t smio_inst_id); uint32_t smio_inst_id);
static devio_err_e _spawn_be_platform_smios (devio_t *devio); static devio_err_e _spawn_be_platform_smios (devio_t *devio);
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"brokerendp", required_argument, NULL, 'b'},
{"daemon", no_argument, NULL, 'd'},
{"daemonworkdir", required_argument, NULL, 'w'},
{"verbose", no_argument, NULL, 'v'},
{"deviotype", required_argument, NULL, 'n'},
{"devicetype", required_argument, NULL, 't'},
{"deviceentry", required_argument, NULL, 'e'},
{"deviceid", required_argument, NULL, 'i'},
{"logprefix", required_argument, NULL, 'l'},
{NULL, 0, NULL, 0}
};
static const char* shortopt = "hb:dw:vn:t:e:i:l:";
void print_help (char *program_name) void print_help (char *program_name)
{ {
printf( "EBPM Config Device I/O\n" fprintf (stdout, "EBPM Device Config I/O\n"
"Usage: %s [options]\n" "Usage: %s [options]\n"
"\t-h This help message\n" "Version %s\n, Build by: %s, %s\n"
"\t-d Daemon mode.\n" "\n"
"\t-v Verbose output\n" " -h --help Display this usage information\n"
"\t-n <devio_type = [be|fe]> Devio type\n" " -b --brokerendp <Broker endpoint> Broker endpoint\n"
"\t-t <device_type = [eth|pcie]> Device type\n" " -d --daemon Run as system daemon.\n"
"\t-e <dev_entry = [ip_addr|/dev entry]> Device entry\n" " -w --daemonworkdir <Work Directory> Daemon working directory.\n"
"\t-i <dev_id> Device ID\n" " -v --verbose Verbose output\n"
"\t-l <log_filename> Log filename\n" " -n --deviotype <[be|fe]> Devio type\n"
"\t-b <broker_endpoint> Broker endpoint\n", program_name); " -t --devicetype <[eth|pcie]> Device type\n"
" -e --deviceentry <[ip_addr|/dev entry]>\n"
" Device entry\n"
" -i --deviceid <Device ID> Device ID\n"
" -l --logfilename <Log filename> Log filename\n",
program_name,
revision_get_build_version (),
revision_get_build_user_name (), revision_get_build_date ());
} }
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
int verbose = 0; int verbose = 0;
int daemonize = 0; int devio_daemonize = 0;
char *devio_work_dir = NULL;
char *devio_type_str = NULL; char *devio_type_str = NULL;
char *dev_type = NULL; char *dev_type = NULL;
char *dev_entry = NULL; char *dev_entry = NULL;
char *dev_id_str = NULL; char *dev_id_str = NULL;
char *broker_endp = NULL; char *broker_endp = NULL;
char *log_file_name = NULL; char *log_filename = NULL;
char **str_p = NULL; int opt;
int i;
while ((opt = getopt_long (argc, argv, shortopt, long_options, NULL)) != -1) {
/* Get the user selected options */
switch (opt) {
/* Display Help */
case 'h':
print_help (argv [0]);
exit (1);
break;
if (argc < 5) { case 'b':
print_help (argv[0]); DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set broker_endp parameter\n");
exit (1); broker_endp = strdup (optarg);
} break;
/* FIXME: This is rather buggy! */ case 'd':
/* Simple handling of command-line options. This should be done DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_TRACE, "[ebpm_cfg] Will set daemon parameter\n");
* with getopt, for instance*/ devio_daemonize = 1;
for (i = 1; i < argc; i++) break;
{
if (streq (argv[i], "-v")) { case 'w':
verbose = 1; DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set devio_work_dir parameter\n");
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Verbose mode set\n"); devio_work_dir = strdup (optarg);
} break;
else if (streq (argv[i], "-d")) {
daemonize = 1; case 'v':
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Demonize mode set\n"); DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_TRACE, "[ebpm_cfg] Will set verbose parameter\n");
} verbose = 1;
else if (streq (argv[i], "-n")) { break;
str_p = &devio_type_str;
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set devio_type parameter\n"); case 'n':
} DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set devio_type parameter\n");
else if (streq (argv[i], "-t")) { devio_type_str = strdup (optarg);
str_p = &dev_type; break;
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_type parameter\n");
} case 't':
else if (streq (argv[i], "-e")) { DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_type parameter\n");
str_p = &dev_entry; dev_type = strdup (optarg);
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_entry parameter\n"); break;
}
else if (streq (argv[i], "-i")) { case 'e':
str_p = &dev_id_str; DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_type parameter\n");
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_id_str parameter\n"); dev_type = strdup (optarg);
} break;
else if (streq (argv[i], "-b")) {
str_p = &broker_endp; case 'i':
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set broker_endp parameter\n"); DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set dev_id_str parameter\n");
} dev_id_str = strdup (optarg);
else if (streq (argv[i], "-l")) { break;
str_p = &log_file_name;
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set log filename\n"); case 'l':
} DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Will set log filename\n");
else if (streq (argv[i], "-h")) { log_filename = strdup (optarg);
print_help (argv[0]); break;
exit (1);
} case '?':
/* Fallout for options with parameters */ DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[ebpm_cfg] Option not recognized or missing argument\n");
else { print_help (argv [0]);
if (str_p) { exit (1);
*str_p = strdup (argv[i]); break;
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_TRACE, "[ebpm_cfg] Parameter set to \"%s\"\n", *str_p);
} default:
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[ebpm_cfg] Could not parse options\n");
print_help (argv [0]);
exit (1);
} }
} }
/* Daemonize dev_io */ /* Daemonize dev_io */
if (daemonize != 0) { if (devio_daemonize != 0) {
int rc = daemon(0, 0); if (devio_work_dir == NULL) {
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_FATAL, "[ebpm_cfg] Daemon working directory not specified\n");
goto err_exit;
}
int rc = zsys_daemonize (devio_work_dir);
if (rc != 0) { if (rc != 0) {
perror ("[ebpm_cfg] daemon"); DBE_DEBUG (DBG_DEV_IO | DBG_LVL_FATAL, "[ebpm_cfg] Fail to daemonize\n");
goto err_exit; goto err_exit;
} }
} }
...@@ -163,11 +203,9 @@ int main (int argc, char *argv[]) ...@@ -163,11 +203,9 @@ int main (int argc, char *argv[])
dev_id); dev_id);
/* We don't need it anymore */ /* We don't need it anymore */
str_p = &dev_type; free (dev_type);
free (*str_p);
dev_type = NULL; dev_type = NULL;
str_p = &dev_id_str; free (dev_id_str);
free (*str_p);
dev_id_str = NULL; dev_id_str = NULL;
/* Initilialize dev_io */ /* Initilialize dev_io */
...@@ -177,7 +215,7 @@ int main (int argc, char *argv[]) ...@@ -177,7 +215,7 @@ int main (int argc, char *argv[])
snprintf (devio_service_str, DEVIO_SERVICE_LEN-1, "BPM%u:DEVIO_CFG", dev_id); snprintf (devio_service_str, DEVIO_SERVICE_LEN-1, "BPM%u:DEVIO_CFG", dev_id);
devio_service_str [DEVIO_SERVICE_LEN-1] = '\0'; /* Just in case ... */ devio_service_str [DEVIO_SERVICE_LEN-1] = '\0'; /* Just in case ... */
devio_t *devio = devio_new (devio_service_str, dev_id, dev_entry, llio_type, devio_t *devio = devio_new (devio_service_str, dev_id, dev_entry, llio_type,
broker_endp, verbose, log_file_name); broker_endp, verbose, log_filename);
if (devio == NULL) { if (devio == NULL) {
DBE_DEBUG (DBG_DEV_IO | DBG_LVL_FATAL, "[ebpm_cfg] devio_new error!\n"); DBE_DEBUG (DBG_DEV_IO | DBG_LVL_FATAL, "[ebpm_cfg] devio_new error!\n");
...@@ -185,11 +223,9 @@ int main (int argc, char *argv[]) ...@@ -185,11 +223,9 @@ int main (int argc, char *argv[])
} }
/* We don't need it anymore */ /* We don't need it anymore */
str_p = &dev_entry; free (dev_entry);
free (*str_p);
dev_entry = NULL; dev_entry = NULL;
str_p = &broker_endp; free (broker_endp);
free (*str_p);
broker_endp = NULL; broker_endp = NULL;
devio_err_e err = _spawn_platform_smios (devio, devio_type, 0); devio_err_e err = _spawn_platform_smios (devio, devio_type, 0);
...@@ -216,18 +252,14 @@ int main (int argc, char *argv[]) ...@@ -216,18 +252,14 @@ int main (int argc, char *argv[])
err_devio: err_devio:
devio_destroy (&devio); devio_destroy (&devio);
err_exit: err_exit:
str_p = &log_file_name; free (log_filename);
free (*str_p); free (broker_endp);
str_p = &broker_endp; free (dev_id_str);
free (*str_p); free (dev_entry);
str_p = &dev_id_str; free (dev_type);
free (*str_p); free (devio_work_dir);
str_p = &dev_entry; free (devio_type_str);
free (*str_p); DBE_DEBUG (DBG_DEV_IO | DBG_LVL_INFO, "[ebpm_cfg] Exiting ...\n");
str_p = &dev_type;
free (*str_p);
str_p = &devio_type_str;
free (*str_p);
return 0; return 0;
} }
......
...@@ -36,45 +36,68 @@ ...@@ -36,45 +36,68 @@
#define DFLT_LOG_DIR "stdout" #define DFLT_LOG_DIR "stdout"
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"cfgfile", required_argument, NULL, 'f'},
{NULL, 0, NULL, 0}
};
static const char* shortopt = "hf:";
void print_help (char *program_name) void print_help (char *program_name)
{ {
printf( "Usage: %s [options]\n" fprintf (stdout, "EBPM Device Manager\n"
"\t-f Configuration file\n" "Usage: %s [options]\n"
"\t-h This help message\n" "Version %s\n, Build by: %s, %s\n"
"\n"
" -h --help Display this usage information\n"
" -f --cfgfile <Configuration File> Specify configuration file\n"
"\n"
"\n\t Most of the options resides at the bpm_sw configuration file,\n" "\n\t Most of the options resides at the bpm_sw configuration file,\n"
"typically located in /etc/bpm_sw", program_name); "typically located in /etc/bpm_sw", program_name,
revision_get_build_version (),
revision_get_build_user_name (), revision_get_build_date ());
} }
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
char *cfg_file = NULL; char *cfg_file = NULL;
char **str_p = NULL; int opt;
int i;
while ((opt = getopt_long (argc, argv, shortopt, long_options, NULL)) != -1) {
if (argc < 3) { /* Get the user selected options */
print_help (argv[0]); switch (opt) {
exit (1); /* Display Help */
case 'h':
print_help (argv [0]);
exit (1);
break;
case 'f':
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_TRACE, "[dev_mngr] Will set cfg_file parameter\n");
cfg_file = strdup (optarg);
break;
case '?':
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[dev_mngr] Option not recognized or missing argument\n");
print_help (argv [0]);
exit (1);
break;
default:
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[dev_mngr] Could not parse options\n");
print_help (argv [0]);
exit (1);
}
} }
/* Simple handling of command-line options. This should be done /* Check command-line parse options */
* with getopt, for instance*/ if (cfg_file == NULL) {
for (i = 1; i < argc; i++) DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[dev_mngr] Config file was "
{ "not specified\n");
if (streq (argv[i], "-f")) { print_help (argv [0]);
str_p = &cfg_file; goto err_parse_cfg;
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_TRACE, "[dev_mngr] Will set cfg_file parameter\n");
}
else if (streq(argv[i], "-h")) {
print_help (argv [0]);
exit (1);
}
/* Fallout for options with parameters */
else {
if (str_p) {
*str_p = strdup (argv[i]);
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_TRACE, "[dev_mngr] Parameter set to \"%s\"\n", *str_p);
}
}
} }
zhashx_t *dmngr_hints = zhashx_new (); zhashx_t *dmngr_hints = zhashx_new ();
...@@ -354,9 +377,9 @@ err_daemonize: ...@@ -354,9 +377,9 @@ err_daemonize:
err_cfg_exit: err_cfg_exit:
zhashx_destroy (&dmngr_hints); zhashx_destroy (&dmngr_hints);
err_dmngr_hints_alloc: err_dmngr_hints_alloc:
str_p = &cfg_file; free (cfg_file);
free (*str_p); err_parse_cfg:
DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_FATAL, "[dev_mngr] Exiting ...\n"); DBE_DEBUG (DBG_DEV_MNGR | DBG_LVL_INFO, "[dev_mngr] Exiting ...\n");
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment