Improve configuration module error management at init time, Fix command line help option processing, remove shotopt field from parameter structure
This commit is contained in:
@@ -136,7 +136,7 @@ char *cfgpath;
|
||||
} else {
|
||||
sprintf(cfgpath,"%s",cfgoptions[i].optname);
|
||||
}
|
||||
if ( ((strlen(*p) > 1) && (strcmp(*p + 1,cfgoptions[i].shortopt) == 0)) ||
|
||||
if ( ((strlen(*p) == 2) && (strcmp(*p + 1,cfgpath) == 0)) ||
|
||||
((strlen(*p) > 2) && (strcmp(*p + 2,cfgpath ) == 0 )) ) {
|
||||
p++;
|
||||
j =+ processoption(&(cfgoptions[i]), *p);
|
||||
|
||||
@@ -106,16 +106,19 @@ char *modeparams=NULL;
|
||||
char *cfgmode=NULL;
|
||||
char *strtokctx=NULL;
|
||||
char *atoken;
|
||||
|
||||
uint32_t tmpflags=0;
|
||||
int i;
|
||||
|
||||
|
||||
/* first parse the command line to look for the -O option */
|
||||
opterr=0;
|
||||
while ((i = getopt(argc, argv, "O:")) != -1) {
|
||||
while ((i = getopt(argc, argv, "O:h")) != -1) {
|
||||
if ( i == 'O' ) {
|
||||
cfgparam = optarg;
|
||||
}
|
||||
}
|
||||
if ( i == 'h' ) {
|
||||
tmpflags = CONFIG_HELP;
|
||||
}
|
||||
}
|
||||
optind=1;
|
||||
|
||||
@@ -132,20 +135,21 @@ int i;
|
||||
i = sscanf(cfgparam,"%m[^':']:%ms",&cfgmode,&modeparams);
|
||||
if (i< 0) {
|
||||
fprintf(stderr,"[CONFIG] %s, %d, sscanf error parsing config source %s: %s\n", __FILE__, __LINE__,cfgparam, strerror(errno));
|
||||
return NULL;
|
||||
cfgmode=strdup("libconfig");
|
||||
modeparams = strdup("oaisoftmodem.conf");
|
||||
}
|
||||
else if ( i == 1 ) {
|
||||
/* -O argument doesn't contain ":" separator, legacy -O <conf file> option, default cfgmode to libconfig
|
||||
with one parameter, the path to the configuration file */
|
||||
modeparams=cfgmode;
|
||||
cfgmode=strdup("libconfig");
|
||||
tmpflags = tmpflags | CONFIG_LEGACY;/* temporary, legacy mode */
|
||||
}
|
||||
|
||||
cfgptr = malloc(sizeof(configmodule_interface_t));
|
||||
memset(cfgptr,0,sizeof(configmodule_interface_t));
|
||||
/* temporary, legacy mode */
|
||||
if (i==1) cfgptr->rtflags = cfgptr->rtflags | CONFIG_LEGACY;
|
||||
/*--*/
|
||||
|
||||
cfgptr->rtflags = cfgptr->rtflags | tmpflags;
|
||||
cfgptr->argc = argc;
|
||||
cfgptr->argv = argv;
|
||||
cfgptr->cfgmode=strdup(cfgmode);
|
||||
@@ -178,17 +182,18 @@ int i;
|
||||
i=load_config_sharedlib(cfgptr);
|
||||
if (i< 0) {
|
||||
fprintf(stderr,"[CONFIG] %s %d config module %s couldn't be loaded\n", __FILE__, __LINE__,cfgmode);
|
||||
return NULL;
|
||||
cfgptr->rtflags = cfgptr->rtflags | CONFIG_HELP | CONFIG_ABORT;
|
||||
} else {
|
||||
printf("[CONFIG] config module %s loaded\n",cfgmode);
|
||||
Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].uptr=&(cfgptr->rtflags);
|
||||
config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );
|
||||
}
|
||||
|
||||
Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].uptr=&(cfgptr->rtflags);
|
||||
config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );
|
||||
|
||||
|
||||
if (modeparams != NULL) free(modeparams);
|
||||
if (cfgmode != NULL) free(cfgmode);
|
||||
|
||||
if (CONFIG_ISFLAGSET(CONFIG_ABORT)) config_printhelp(Config_Params,CONFIG_PARAMLENGTH(Config_Params));
|
||||
return cfgptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,8 @@
|
||||
#define CONFIG_PRINTPARAMS 1 // print parameters values while processing
|
||||
#define CONFIG_DEBUGPTR 2 // print memory allocation/free debug messages
|
||||
#define CONFIG_DEBUGCMDLINE 4 // print command line processing messages
|
||||
|
||||
#define CONFIG_HELP 8 // print help message
|
||||
#define CONFIG_ABORT 16 // config failed,abort execution
|
||||
/* temporary flag to be able to use legacy config mechanism */
|
||||
#define CONFIG_LEGACY (1 << 10)
|
||||
|
||||
@@ -71,17 +72,21 @@ typedef struct configmodule_interface
|
||||
#ifdef CONFIG_LOADCONFIG_MAIN
|
||||
configmodule_interface_t *cfgptr=NULL;
|
||||
|
||||
static char config_helpstr [] =" \
|
||||
config debugflags: mask, 1->print parameters, 2->print memory allocations debug messages \
|
||||
4->print command line processing debug messages \
|
||||
-O <config mode><:dbg> \
|
||||
debugflags can also be defined in the config_libconfig section of the config file \
|
||||
";
|
||||
static char config_helpstr [] = "\n lte-softmodem -O [config mode]<:dbg[debugflags]> \n \
|
||||
debugflags can also be defined in the config_libconfig section of the config file\n \
|
||||
debugflags: mask, 1->print parameters, 2->print memory allocations debug messages\n \
|
||||
4->print command line processing debug messages\n ";
|
||||
|
||||
#define CONFIG_SECTIONNAME "config"
|
||||
|
||||
#define CONFIGPARAM_DEBUGFLAGS_IDX 0
|
||||
|
||||
|
||||
static paramdef_t Config_Params[] = {
|
||||
{"debugflags", "", config_helpstr, 0, uptr:NULL, defintval:0, TYPE_MASK, 0},
|
||||
/*-----------------------------------------------------------------------------------------------------------------------*/
|
||||
/* config parameters for config module */
|
||||
/* optname helpstr paramflags XXXptr defXXXval type numelt */
|
||||
/*-----------------------------------------------------------------------------------------------------------------------*/
|
||||
{"debugflags", config_helpstr, 0, uptr:NULL, defintval:0, TYPE_MASK, 0},
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
|
||||
|
||||
#define MAX_OPTNAME_SIZE 64
|
||||
#define MAX_SHORTOPT_SIZE 8
|
||||
|
||||
|
||||
|
||||
/* parameter flags definitions */
|
||||
@@ -58,7 +58,6 @@
|
||||
typedef struct paramdef
|
||||
{
|
||||
char optname[MAX_OPTNAME_SIZE]; /* parameter name, can be used as long command line option */
|
||||
char shortopt[MAX_SHORTOPT_SIZE]; /* short command line option */
|
||||
char *helpstr; /* help string */
|
||||
unsigned int paramflags; /* value is a "ored" combination of above PARAMFLAG_XXXX values */
|
||||
union { /* pointer to the parameter value, completed by the config module */
|
||||
|
||||
@@ -112,7 +112,10 @@ void config_printhelp(paramdef_t *params,int numparams)
|
||||
{
|
||||
for (int i=0 ; i<numparams ; i++) {
|
||||
if ( params[i].helpstr != NULL) {
|
||||
printf("%s", params[i].helpstr);
|
||||
printf("%s%s: %s",
|
||||
(strlen(params[i].optname) <= 1) ? "-" : "--",
|
||||
params[i].optname,
|
||||
params[i].helpstr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user