Commit 1eb79272 authored by frtabu's avatar frtabu

config module enhancement, next step

parent 79b85864
...@@ -275,7 +275,7 @@ configmodule_interface_t *load_configmodule(int argc, ...@@ -275,7 +275,7 @@ configmodule_interface_t *load_configmodule(int argc,
/* argv[0] is the exec name, always Ok */ /* argv[0] is the exec name, always Ok */
cfgptr->argv_info[0] |= CONFIG_CMDLINEOPT_PROCESSED; cfgptr->argv_info[0] |= CONFIG_CMDLINEOPT_PROCESSED;
/* a file with config parameters as defined after all processing will be created */ /* When reuested _(_--OW or rtflag is 5), a file with config parameters, as defined after all processing, will be created */
if (OWoptIdx >= 0) { if (OWoptIdx >= 0) {
cfgptr->argv_info[OWoptIdx] |= CONFIG_CMDLINEOPT_PROCESSED; cfgptr->argv_info[OWoptIdx] |= CONFIG_CMDLINEOPT_PROCESSED;
cfgptr->rtflags |= CONFIG_SAVERUNCFG ; cfgptr->rtflags |= CONFIG_SAVERUNCFG ;
...@@ -322,7 +322,10 @@ configmodule_interface_t *load_configmodule(int argc, ...@@ -322,7 +322,10 @@ configmodule_interface_t *load_configmodule(int argc,
if (i == 0) { if (i == 0) {
printf("[CONFIG] config module %s loaded\n",cfgmode); printf("[CONFIG] config module %s loaded\n",cfgmode);
Config_Params[CONFIGPARAM_DEBUGFLAGS_IDX].uptr=&(cfgptr->rtflags); int idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_DEBUGFLAGS);
Config_Params[idx].uptr=&(cfgptr->rtflags);
idx = config_paramidx_fromname(Config_Params, CONFIG_PARAMLENGTH(Config_Params), CONFIGP_TMPDIR);
Config_Params[idx].strptr=&(cfgptr->tmpdir);
config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME ); config_get(Config_Params,CONFIG_PARAMLENGTH(Config_Params), CONFIG_SECTIONNAME );
} else { } else {
fprintf(stderr,"[CONFIG] %s %d config module \"%s\" couldn't be loaded\n", __FILE__, __LINE__,cfgmode); fprintf(stderr,"[CONFIG] %s %d config module \"%s\" couldn't be loaded\n", __FILE__, __LINE__,cfgmode);
......
...@@ -80,6 +80,7 @@ typedef struct configmodule_interface { ...@@ -80,6 +80,7 @@ typedef struct configmodule_interface {
uint32_t rtflags; uint32_t rtflags;
char *ptrs[CONFIG_MAX_ALLOCATEDPTRS]; char *ptrs[CONFIG_MAX_ALLOCATEDPTRS];
bool ptrsAllocated[CONFIG_MAX_ALLOCATEDPTRS]; bool ptrsAllocated[CONFIG_MAX_ALLOCATEDPTRS];
char *tmpdir;
} configmodule_interface_t; } configmodule_interface_t;
#ifdef CONFIG_LOADCONFIG_MAIN #ifdef CONFIG_LOADCONFIG_MAIN
...@@ -92,16 +93,19 @@ static char config_helpstr [] = "\n lte-softmodem -O [config mode]<:dbgl[debugfl ...@@ -92,16 +93,19 @@ static char config_helpstr [] = "\n lte-softmodem -O [config mode]<:dbgl[debugfl
incp parameter can be used to define the include path used for config files (@include directive)\n \ incp parameter can be used to define the include path used for config files (@include directive)\n \
defaults is set to the path of the main config file.\n"; defaults is set to the path of the main config file.\n";
#define CONFIG_SECTIONNAME "config" #define CONFIG_HELP_TMPDIR "<Repository to create temporary file>"
#define CONFIGPARAM_DEBUGFLAGS_IDX 0
#define CONFIG_SECTIONNAME "config"
#define CONFIGP_DEBUGFLAGS "debugflags"
#define CONFIGP_TMPDIR "tmpdir"
static paramdef_t Config_Params[] = { static paramdef_t Config_Params[] = {
/*-----------------------------------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------------*/
/* config parameters for config module */ /* config parameters for config module */
/* optname helpstr paramflags XXXptr defXXXval type numelt */ /* optname helpstr paramflags XXXptr defXXXval type numelt */
/*-----------------------------------------------------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------------------------------------------------------*/
{"debugflags", config_helpstr, 0, uptr:NULL, defintval:0, TYPE_MASK, 0}, {CONFIGP_DEBUGFLAGS, config_helpstr, 0, uptr:NULL, defintval:0, TYPE_MASK, 0},
{CONFIGP_TMPDIR, CONFIG_HELP_TMPDIR,PARAMFLAG_NOFREE, strptr:NULL, defstrval:"/tmp" ,TYPE_STRING, 0},
}; };
#else #else
......
...@@ -91,14 +91,30 @@ int read_intarray(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpat ...@@ -91,14 +91,30 @@ int read_intarray(paramdef_t *cfgoptions,config_setting_t *setting, char *cfgpat
int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_setting_t * asetting) { int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_setting_t * asetting) {
int status; int status;
int errors=0; int errors=0;
int notused=0;
#define LIBCONFIG_NOTUSED_PARAMS "Not used? (NULL value ptr)"
for(int i=0; i<numoptions; i++) { for(int i=0; i<numoptions; i++) {
status=CONFIG_FALSE; status=CONFIG_FALSE;
config_setting_t * psetting;
switch(cfgoptions[i].type) { switch(cfgoptions[i].type) {
case TYPE_STRING: case TYPE_STRING:
//status=config_setting_set_string (asetting, *cfgoptions[i].strptr; psetting =config_setting_add(asetting,cfgoptions[i].optname,CONFIG_TYPE_STRING);
if (psetting!= NULL) {
char *value;
if (cfgoptions[i].strptr == NULL) {
notused++;
value=LIBCONFIG_NOTUSED_PARAMS;
} else {
if (cfgoptions[i].numelt == 0 ) {
value = *(cfgoptions[i].strptr);
} else {
value = (char *)(cfgoptions[i].strptr);
}
}
status=config_setting_set_string(psetting, value);
}
break; break;
case TYPE_STRINGLIST: case TYPE_STRINGLIST:
break; break;
...@@ -109,12 +125,16 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se ...@@ -109,12 +125,16 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
case TYPE_UINT32: case TYPE_UINT32:
case TYPE_INT32: case TYPE_INT32:
case TYPE_MASK: case TYPE_MASK:
status=config_setting_set_int(asetting, (int)*(cfgoptions[i].iptr) ); psetting =config_setting_add(asetting,cfgoptions[i].optname,CONFIG_TYPE_INT);
if (psetting!= NULL)
status=config_setting_set_int(psetting, (int)*(cfgoptions[i].iptr) );
break; break;
case TYPE_UINT64: case TYPE_UINT64:
case TYPE_INT64: case TYPE_INT64:
status=config_setting_set_int64(asetting, (int64_t)*(cfgoptions[i].i64ptr) ); psetting =config_setting_add(asetting,cfgoptions[i].optname,CONFIG_TYPE_INT64);
if (psetting!= NULL)
status=config_setting_set_int64(psetting, (int64_t)*(cfgoptions[i].i64ptr) );
break; break;
case TYPE_UINTARRAY: case TYPE_UINTARRAY:
...@@ -123,7 +143,9 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se ...@@ -123,7 +143,9 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
break; break;
case TYPE_DOUBLE: case TYPE_DOUBLE:
status=config_setting_set_float(asetting, (double)*(cfgoptions[i].dblptr) ); psetting =config_setting_add(asetting,cfgoptions[i].optname,CONFIG_TYPE_FLOAT);
if (psetting!= NULL)
status=config_setting_set_float(psetting, (double)*(cfgoptions[i].dblptr) );
break; break;
case TYPE_IPV4ADDR: case TYPE_IPV4ADDR:
...@@ -142,8 +164,11 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se ...@@ -142,8 +164,11 @@ int config_libconfig_setparams(paramdef_t *cfgoptions, int numoptions, config_se
fprintf(stderr,"[LIBCONFIG] Error creating setting %i: %s type %i\n",i,cfgoptions[i].optname ,cfgoptions[i].type); fprintf(stderr,"[LIBCONFIG] Error creating setting %i: %s type %i\n",i,cfgoptions[i].optname ,cfgoptions[i].type);
} }
} }
if (notused > 0)
printf_params("[LIBCONFIG], %i settings with NULL value pointer\n",notused);
if (errors ==0) { if (errors ==0) {
print_params("[LIBCONFIG], %i settings set successfully ",numoptions); printf_params("[LIBCONFIG], %i settings set successfully ",numoptions);
} else { } else {
fprintf(stderr,"[LIBCONFIG] ...%i/%i settings creation errors \n",errors,numoptions); fprintf(stderr,"[LIBCONFIG] ...%i/%i settings creation errors \n",errors,numoptions);
} }
...@@ -161,29 +186,39 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix ) ...@@ -161,29 +186,39 @@ int config_libconfig_set(paramdef_t *cfgoptions, int numoptions, char *prefix )
prefix_elem1 = strtok_r(prefixbck, ".", &tokctx1); prefix_elem1 = strtok_r(prefixbck, ".", &tokctx1);
} }
int n1=0; int n1=0;
/* parse the prefix , possibly creating groups, lists and list elements */
while (prefix_elem1 != NULL) { while (prefix_elem1 != NULL) {
n1+=strlen(prefix_elem1)+1; n1+=strlen(prefix_elem1)+1;
char *prefix_elem2 = ( n1<strlen(prefix) ) ? prefix_elem1+n1 : ""; char *prefix_elem2 = ( n1<strlen(prefix) ) ? prefix_elem1+n1 : "";
/* prefix (the path to the parameter name) may contain groups and elements from a list, which are specified with [] */
if (prefix_elem2[0] != '[') { if (prefix_elem2[0] != '[') { // not a list
config_setting_t *tmpset = config_setting_lookup(asetting,prefix_elem1); config_setting_t *tmpset = config_setting_lookup(asetting,prefix_elem1);
if (tmpset == NULL) if (tmpset == NULL)
asetting = config_setting_add (asetting, prefix_elem1, CONFIG_TYPE_GROUP); asetting = config_setting_add (asetting, prefix_elem1, CONFIG_TYPE_GROUP);
else else
asetting = tmpset; asetting = tmpset;
} else { } else { // a list
listidx=(int)strtol(prefix_elem2,NULL,10); listidx=(int)strtol(prefix_elem2,NULL,10);
config_setting_t *tmpset = config_setting_lookup(asetting,prefix_elem1); config_setting_t *tmpset = config_setting_lookup(asetting,prefix_elem1);
if (tmpset == NULL) if (tmpset == NULL)
asetting = config_setting_add (asetting, prefix_elem1, CONFIG_TYPE_LIST); asetting = config_setting_add (asetting, prefix_elem1, CONFIG_TYPE_LIST);
else else
asetting = tmpset; asetting = tmpset;
// tmpset = if (asetting != NULL) {
tmpset = config_setting_get_elem (asetting, listidx);
if (tmpset == NULL)
asetting = config_setting_add (asetting, NULL, CONFIG_TYPE_GROUP);
else
asetting = tmpset;
}
prefix_elem1 = strtok_r(NULL, ".", &tokctx1); //skip the [x] elements we already took care of it
if (prefix_elem1 == NULL) {
fprintf(stderr, "[LICONFIG] Error parsing %s__\n",prefix);
break;
}
} }
if (asetting == NULL) { if (asetting == NULL) {
fprintf(stderr,"[LIBCONFIG] Error creating setting %s %s %i\n",prefix_elem1,prefix_elem2,listidx); fprintf(stderr,"[LIBCONFIG] Error creating setting %s %s %i\n",prefix_elem1,prefix_elem2,listidx);
break;
} }
prefix_elem1 = strtok_r(NULL, ".", &tokctx1); prefix_elem1 = strtok_r(NULL, ".", &tokctx1);
} }
...@@ -498,10 +533,11 @@ int config_libconfig_init(char *cfgP[], int numP) { ...@@ -498,10 +533,11 @@ int config_libconfig_init(char *cfgP[], int numP) {
void config_libconfig_end(void ) { void config_libconfig_end(void ) {
config_destroy(&(libconfig_privdata.cfg)); config_destroy(&(libconfig_privdata.cfg));
if( cfgptr->rtflags & CONFIG_SAVERUNCFG ) { if( cfgptr->rtflags & CONFIG_SAVERUNCFG ) {
char *fname=strdup(libconfig_privdata.configfile);
char *newcfgf = malloc( strlen( libconfig_privdata.configfile +20)); char *newcfgf = malloc( strlen( libconfig_privdata.configfile +20));
time_t t = time(NULL); time_t t = time(NULL);
struct tm tm = *localtime(&t); struct tm tm = *localtime(&t);
sprintf (newcfgf, "%s-run%d_%02d_%02d_%02d%02d",libconfig_privdata.configfile, sprintf (newcfgf, "%s/%s-run%d_%02d_%02d_%02d%02d",cfgptr->tmpdir,basename(fname),
tm.tm_year + 1900,tm.tm_mon + 1,tm.tm_mday, tm.tm_hour, tm.tm_min); tm.tm_year + 1900,tm.tm_mon + 1,tm.tm_mday, tm.tm_hour, tm.tm_min);
if ( config_write_file (&(libconfig_privdata.runtcfg) , newcfgf) != CONFIG_TRUE) { if ( config_write_file (&(libconfig_privdata.runtcfg) , newcfgf) != CONFIG_TRUE) {
fprintf(stderr,"[LIBCONFIG] %s %d file %s - line %d: %s\n",__FILE__, __LINE__, fprintf(stderr,"[LIBCONFIG] %s %d file %s - line %d: %s\n",__FILE__, __LINE__,
...@@ -511,6 +547,7 @@ void config_libconfig_end(void ) { ...@@ -511,6 +547,7 @@ void config_libconfig_end(void ) {
printf("[LIBCONFIG] file %s created successfully\n", newcfgf); printf("[LIBCONFIG] file %s created successfully\n", newcfgf);
} }
config_destroy(&(libconfig_privdata.runtcfg)); config_destroy(&(libconfig_privdata.runtcfg));
free(fname);
free(newcfgf); free(newcfgf);
} }
if ( libconfig_privdata.configfile != NULL ) { if ( libconfig_privdata.configfile != NULL ) {
......
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