Commit 55732deb authored by Raphael Defosseux's avatar Raphael Defosseux

Astyling the common files

Signed-off-by: Raphael Defosseux's avatarRaphael Defosseux <raphael.defosseux@eurecom.fr>
parent df5fe4ff
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
*/ */
/*! \file common/config/cmdline/config_libconfig.c /*! \file common/config/cmdline/config_libconfig.c
* \brief configuration module, command line parsing implementation * \brief configuration module, command line parsing implementation
* \author Francois TABURET * \author Francois TABURET
* \date 2017 * \date 2017
* \version 0.1 * \version 0.1
...@@ -39,223 +39,244 @@ ...@@ -39,223 +39,244 @@
#include "config_userapi.h" #include "config_userapi.h"
int parse_stringlist(paramdef_t *cfgoptions, char *val) int parse_stringlist(paramdef_t *cfgoptions, char *val) {
{ char *atoken;
char *atoken; char *tokctx;
char *tokctx; char *tmpval=strdup(val);
char *tmpval=strdup(val); int numelt=0;
int numelt=0; cfgoptions->numelt=0;
atoken=strtok_r(tmpval, ",",&tokctx);
cfgoptions->numelt=0;
while(atoken != NULL) {
atoken=strtok_r(tmpval, ",",&tokctx); numelt++ ;
while(atoken != NULL) { atoken=strtok_r(NULL, ",",&tokctx);
numelt++ ; }
atoken=strtok_r(NULL, ",",&tokctx);
} free(tmpval);
free(tmpval); config_check_valptr(cfgoptions,(char **)&(cfgoptions->strlistptr), sizeof(char *) * numelt);
config_check_valptr(cfgoptions,(char **)&(cfgoptions->strlistptr), sizeof(char *) * numelt); cfgoptions->numelt=numelt;
cfgoptions->numelt=numelt; atoken=strtok_r(val, ",",&tokctx);
atoken=strtok_r(val, ",",&tokctx); for( int i=0; i<cfgoptions->numelt && atoken != NULL ; i++) {
for( int i=0; i<cfgoptions->numelt && atoken != NULL ; i++) { config_check_valptr(cfgoptions,&(cfgoptions->strlistptr[i]),strlen(atoken)+1);
config_check_valptr(cfgoptions,&(cfgoptions->strlistptr[i]),strlen(atoken)+1); sprintf(cfgoptions->strlistptr[i],"%s",atoken);
sprintf(cfgoptions->strlistptr[i],"%s",atoken); printf_params("[LIBCONFIG] %s[%i]: %s\n", cfgoptions->optname,i,cfgoptions->strlistptr[i]);
printf_params("[LIBCONFIG] %s[%i]: %s\n", cfgoptions->optname,i,cfgoptions->strlistptr[i]); atoken=strtok_r(NULL, ",",&tokctx);
atoken=strtok_r(NULL, ",",&tokctx); }
}
return (cfgoptions->numelt > 0); return (cfgoptions->numelt > 0);
} }
int processoption(paramdef_t *cfgoptions, char *value) int processoption(paramdef_t *cfgoptions, char *value) {
{ char *tmpval = value;
char *tmpval = value; int optisset=0;
int optisset=0; char defbool[2]="1";
char defbool[2]="1";
if ( value == NULL) {
if( (cfgoptions->paramflags &PARAMFLAG_BOOL) == 0 ) { /* not a boolean, argument required */
if ( value == NULL) { fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname);
if( (cfgoptions->paramflags &PARAMFLAG_BOOL) == 0 ) { /* not a boolean, argument required */ return 0;
fprintf(stderr,"[CONFIG] command line, option %s requires an argument\n",cfgoptions->optname); } else { /* boolean value option without argument, set value to true*/
return 0; tmpval = defbool;
} else { /* boolean value option without argument, set value to true*/ }
tmpval = defbool; }
}
} switch(cfgoptions->type) {
switch(cfgoptions->type) case TYPE_STRING:
{ if (cfgoptions->numelt == 0 ) {
case TYPE_STRING: config_check_valptr(cfgoptions, cfgoptions->strptr, strlen(tmpval)+1);
if (cfgoptions->numelt == 0 ) { sprintf(*(cfgoptions->strptr), "%s",tmpval);
config_check_valptr(cfgoptions, cfgoptions->strptr, strlen(tmpval)+1); } else {
sprintf(*(cfgoptions->strptr), "%s",tmpval); sprintf( (char *)(cfgoptions->strptr), "%s",tmpval);
} else { }
sprintf( (char *)(cfgoptions->strptr), "%s",tmpval);
} printf_cmdl("[CONFIG] %s set to %s from command line\n", cfgoptions->optname, tmpval);
printf_cmdl("[CONFIG] %s set to %s from command line\n", cfgoptions->optname, tmpval); optisset=1;
optisset=1; break;
break;
case TYPE_STRINGLIST:
case TYPE_STRINGLIST: optisset=parse_stringlist(cfgoptions,tmpval);
optisset=parse_stringlist(cfgoptions,tmpval); break;
break;
case TYPE_UINT32: case TYPE_UINT32:
case TYPE_INT32: case TYPE_INT32:
case TYPE_UINT16: case TYPE_UINT16:
case TYPE_INT16: case TYPE_INT16:
case TYPE_UINT8: case TYPE_UINT8:
case TYPE_INT8: case TYPE_INT8:
config_check_valptr(cfgoptions, (char **)&(cfgoptions->iptr),sizeof(int32_t)); config_check_valptr(cfgoptions, (char **)&(cfgoptions->iptr),sizeof(int32_t));
config_assign_int(cfgoptions,cfgoptions->optname,(int32_t)strtol(tmpval,NULL,0)); config_assign_int(cfgoptions,cfgoptions->optname,(int32_t)strtol(tmpval,NULL,0));
optisset=1; optisset=1;
break; break;
case TYPE_UINT64:
case TYPE_INT64: case TYPE_UINT64:
config_check_valptr(cfgoptions, (char **)&(cfgoptions->i64ptr),sizeof(uint64_t)); case TYPE_INT64:
*(cfgoptions->i64ptr)=strtoll(tmpval,NULL,0); config_check_valptr(cfgoptions, (char **)&(cfgoptions->i64ptr),sizeof(uint64_t));
printf_cmdl("[CONFIG] %s set to %lli from command line\n", cfgoptions->optname, (long long)*(cfgoptions->i64ptr)); *(cfgoptions->i64ptr)=strtoll(tmpval,NULL,0);
optisset=1; printf_cmdl("[CONFIG] %s set to %lli from command line\n", cfgoptions->optname, (long long)*(cfgoptions->i64ptr));
break; optisset=1;
case TYPE_UINTARRAY: break;
case TYPE_INTARRAY:
case TYPE_UINTARRAY:
break; case TYPE_INTARRAY:
case TYPE_DOUBLE: break;
config_check_valptr(cfgoptions, (char **)&(cfgoptions->dblptr),sizeof(double));
*(cfgoptions->dblptr) = strtof(tmpval,NULL); case TYPE_DOUBLE:
printf_cmdl("[CONFIG] %s set to %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr)); config_check_valptr(cfgoptions, (char **)&(cfgoptions->dblptr),sizeof(double));
optisset=1; *(cfgoptions->dblptr) = strtof(tmpval,NULL);
break; printf_cmdl("[CONFIG] %s set to %lf from command line\n", cfgoptions->optname, *(cfgoptions->dblptr));
optisset=1;
case TYPE_IPV4ADDR: break;
break; case TYPE_IPV4ADDR:
break;
default:
fprintf(stderr,"[CONFIG] command line, %s type %i not supported\n",cfgoptions->optname, cfgoptions->type); default:
break; fprintf(stderr,"[CONFIG] command line, %s type %i not supported\n",cfgoptions->optname, cfgoptions->type);
} /* switch on param type */ break;
if (optisset == 1) { } /* switch on param type */
cfgoptions->paramflags = cfgoptions->paramflags | PARAMFLAG_PARAMSET;
} if (optisset == 1) {
return optisset; cfgoptions->paramflags = cfgoptions->paramflags | PARAMFLAG_PARAMSET;
}
return optisset;
} }
int config_check_cmdlineopt(char *prefix) int config_check_cmdlineopt(char *prefix) {
{ int unknowndetected=0;
int unknowndetected=0; char testprefix[CONFIG_MAXOPTLENGTH]="";
char testprefix[CONFIG_MAXOPTLENGTH]=""; int finalcheck = 0;
int finalcheck = 0;
if (prefix != NULL) {
if (prefix != NULL) { if (strcmp(prefix,CONFIG_CHECKALLSECTIONS) == 0)
if (strcmp(prefix,CONFIG_CHECKALLSECTIONS) == 0) finalcheck = 1;
finalcheck = 1; else if (strlen(prefix) > 0) {
else if (strlen(prefix) > 0) { sprintf(testprefix,"--%s.",prefix);
sprintf(testprefix,"--%s.",prefix);
}
}
for (int i=1; i<config_get_if()->argc ; i++) {
if ( !finalcheck && strstr(config_get_if()->argv[i],testprefix) == NULL ) continue;
if ( !finalcheck && testprefix[0]==0 && index(config_get_if()->argv[i],'.') != NULL) continue;
if ( !finalcheck && config_get_if()->argv[i][0] == '-' && isdigit(config_get_if()->argv[i][1])) continue;
if ( (config_get_if()->argv_info[i] & CONFIG_CMDLINEOPT_PROCESSED) == 0 ) {
fprintf(stderr,"[CONFIG] unknown option: %s\n",
config_get_if()->argv[i] );
unknowndetected++;
}
} }
}
for (int i=1; i<config_get_if()->argc ; i++) {
if ( !finalcheck && strstr(config_get_if()->argv[i],testprefix) == NULL ) continue;
if ( !finalcheck && testprefix[0]==0 && index(config_get_if()->argv[i],'.') != NULL) continue;
if ( !finalcheck && config_get_if()->argv[i][0] == '-' && isdigit(config_get_if()->argv[i][1])) continue;
if ( (config_get_if()->argv_info[i] & CONFIG_CMDLINEOPT_PROCESSED) == 0 ) {
fprintf(stderr,"[CONFIG] unknown option: %s\n",
config_get_if()->argv[i] );
unknowndetected++;
}
}
printf_cmdl("[CONFIG] %i unknown option(s) in command line starting with %s (section %s)\n", printf_cmdl("[CONFIG] %i unknown option(s) in command line starting with %s (section %s)\n",
unknowndetected,testprefix,((prefix==NULL)?"":prefix)); unknowndetected,testprefix,((prefix==NULL)?"":prefix));
return unknowndetected; return unknowndetected;
} /* parse_cmdline*/ } /* parse_cmdline*/
int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix) int config_process_cmdline(paramdef_t *cfgoptions,int numoptions, char *prefix) {
{
int c = config_get_if()->argc; int c = config_get_if()->argc;
int i,j; int i,j;
char *pp; char *pp;
char cfgpath[CONFIG_MAXOPTLENGTH]; char cfgpath[CONFIG_MAXOPTLENGTH];
j = 0; j = 0;
i = 0; i = 0;
while (c > 0 ) {
char *oneargv = strdup(config_get_if()->argv[i]); /* we use strtok_r which modifies its string paramater, and we don't want argv to be modified */ while (c > 0 ) {
/* first check help options, either --help, -h or --help_<section> */ char *oneargv = strdup(config_get_if()->argv[i]); /* we use strtok_r which modifies its string paramater, and we don't want argv to be modified */
if (strncmp(oneargv, "-h",2) == 0 || strncmp(oneargv, "--help",6) == 0 ) {
char *tokctx; /* first check help options, either --help, -h or --help_<section> */
pp=strtok_r(oneargv, "_",&tokctx); if (strncmp(oneargv, "-h",2) == 0 || strncmp(oneargv, "--help",6) == 0 ) {
char *tokctx;
pp=strtok_r(oneargv, "_",&tokctx);
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (pp == NULL || strcasecmp(pp,config_get_if()->argv[i] ) == 0 ) {
if( prefix == NULL) {
config_printhelp(cfgoptions,numoptions);
if ( ! ( CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP)))
exit_fun("[CONFIG] Exiting after displaying help\n");
}
} else {
pp=strtok_r(NULL, " ",&tokctx);
if ( prefix != NULL && pp != NULL && strncasecmp(prefix,pp,strlen(pp)) == 0 ) {
printf ("Help for %s section:\n",prefix);
config_printhelp(cfgoptions,numoptions);
if ( ! (CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP))) {
fprintf(stderr,"[CONFIG] %s %i section %s:", __FILE__, __LINE__, prefix);
exit_fun(" Exiting after displaying help\n");
}
}
}
}
/* now, check for non help options */
if (oneargv[0] == '-') {
for(int n=0; n<numoptions; n++) {
if ( ( cfgoptions[n].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
continue;
}
if (prefix != NULL) {
sprintf(cfgpath,"%s.%s",prefix,cfgoptions[n].optname);
} else {
sprintf(cfgpath,"%s",cfgoptions[n].optname);
}
if ( ((strlen(oneargv) == 2) && (strcmp(oneargv + 1,cfgpath) == 0)) || /* short option, one "-" */
((strlen(oneargv) > 2) && (strcmp(oneargv + 2,cfgpath ) == 0 )) ) {
char *valptr=NULL;
int ret;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (c > 0) {
pp = config_get_if()->argv[i+1];
if (pp != NULL ) {
ret = strlen(pp);
if (ret > 0 ) {
if (pp[0] != '-')
valptr=pp;
else if ( ret > 1 && pp[0] == '-' && isdigit(pp[1]) )
valptr=pp;
}
}
}
j += processoption(&(cfgoptions[n]), valptr);
if ( valptr != NULL ) {
i++;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED; config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (pp == NULL || strcasecmp(pp,config_get_if()->argv[i] ) == 0 ) { c--;
if( prefix == NULL) { }
config_printhelp(cfgoptions,numoptions);
if ( ! ( CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP))) break;
exit_fun("[CONFIG] Exiting after displaying help\n");
}
} else {
pp=strtok_r(NULL, " ",&tokctx);
if ( prefix != NULL && pp != NULL && strncasecmp(prefix,pp,strlen(pp)) == 0 ) {
printf ("Help for %s section:\n",prefix);
config_printhelp(cfgoptions,numoptions);
if ( ! (CONFIG_ISFLAGSET(CONFIG_NOEXITONHELP))) {
fprintf(stderr,"[CONFIG] %s %i section %s:", __FILE__, __LINE__, prefix);
exit_fun(" Exiting after displaying help\n");
}
}
}
} }
} /* for n... */
} /* if (oneargv[0] == '-') */
free(oneargv);
i++;
c--;
} /* fin du while */
/* now, check for non help options */
if (oneargv[0] == '-') {
for(int n=0;n<numoptions;n++) {
if ( ( cfgoptions[n].paramflags & PARAMFLAG_DISABLECMDLINE) != 0) {
continue;
}
if (prefix != NULL) {
sprintf(cfgpath,"%s.%s",prefix,cfgoptions[n].optname);
} else {
sprintf(cfgpath,"%s",cfgoptions[n].optname);
}
if ( ((strlen(oneargv) == 2) && (strcmp(oneargv + 1,cfgpath) == 0)) || /* short option, one "-" */
((strlen(oneargv) > 2) && (strcmp(oneargv + 2,cfgpath ) == 0 )) ) {
char *valptr=NULL;
int ret;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
if (c > 0) {
pp = config_get_if()->argv[i+1];
if (pp != NULL ) {
ret = strlen(pp);
if (ret > 0 ) {
if (pp[0] != '-')
valptr=pp;
else if ( ret > 1 && pp[0] == '-' && isdigit(pp[1]) )
valptr=pp;
}
}
}
j += processoption(&(cfgoptions[n]), valptr);
if ( valptr != NULL ) {
i++;
config_get_if()->argv_info[i] |= CONFIG_CMDLINEOPT_PROCESSED;
c--;
}
break;
}
} /* for n... */
} /* if (oneargv[0] == '-') */
free(oneargv);
i++;
c--;
} /* fin du while */
printf_cmdl("[CONFIG] %s %i options set from command line\n",((prefix == NULL) ? "(root)":prefix),j); printf_cmdl("[CONFIG] %s %i options set from command line\n",((prefix == NULL) ? "(root)":prefix),j);
if ( !(CONFIG_ISFLAGSET( CONFIG_NOCHECKUNKOPT )) ) { if ( !(CONFIG_ISFLAGSET( CONFIG_NOCHECKUNKOPT )) ) {
i=config_check_cmdlineopt(prefix); i=config_check_cmdlineopt(prefix);
if (i > 0) {
fprintf(stderr,"[CONFIG] %i unknown options for section %s detected in command line\n", if (i > 0) {
i,((prefix==NULL)?"\"root section\"":prefix)); fprintf(stderr,"[CONFIG] %i unknown options for section %s detected in command line\n",
exit_fun(" Exiting after detecting errors in command line \n"); i,((prefix==NULL)?"\"root section\"":prefix));
} exit_fun(" Exiting after detecting errors in command line \n");
}
} }
return j;
return j;
} /* parse_cmdline*/ } /* parse_cmdline*/
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
*/ */
/*! \file common/config/config_load_configmodule.c /*! \file common/config/config_load_configmodule.c
* \brief configuration module, load the shared library implementing the configuration module * \brief configuration module, load the shared library implementing the configuration module
* \author Francois TABURET * \author Francois TABURET
* \date 2017 * \date 2017
* \version 0.1 * \version 0.1
...@@ -42,293 +42,314 @@ ...@@ -42,293 +42,314 @@
#include "config_userapi.h" #include "config_userapi.h"
#define CONFIG_SHAREDLIBFORMAT "libparams_%s.so" #define CONFIG_SHAREDLIBFORMAT "libparams_%s.so"
int load_config_sharedlib(configmodule_interface_t *cfgptr) int load_config_sharedlib(configmodule_interface_t *cfgptr) {
{ void *lib_handle;
void *lib_handle; char fname[128];
char fname[128]; char libname[FILENAME_MAX];
char libname[FILENAME_MAX]; int st;
int st; st=0;
sprintf(libname,CONFIG_SHAREDLIBFORMAT,cfgptr->cfgmode);
lib_handle = dlopen(libname,RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
if (!lib_handle) {
fprintf(stderr,"[CONFIG] %s %d Error calling dlopen(%s): %s\n",__FILE__, __LINE__, libname,dlerror());
st = -1;
} else {
sprintf (fname,"config_%s_init",cfgptr->cfgmode);
cfgptr->init = dlsym(lib_handle,fname);
if (cfgptr->init == NULL ) {
printf("[CONFIG] %s %d no function %s for config mode %s\n",
__FILE__, __LINE__,fname, cfgptr->cfgmode);
} else {
st=cfgptr->init(cfgptr->cfgP,cfgptr->num_cfgP);
printf("[CONFIG] function %s returned %i\n",
fname, st);
}
st=0; sprintf (fname,"config_%s_get",cfgptr->cfgmode);
sprintf(libname,CONFIG_SHAREDLIBFORMAT,cfgptr->cfgmode); cfgptr->get = dlsym(lib_handle,fname);
lib_handle = dlopen(libname,RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE); if (cfgptr->get == NULL ) {
if (!lib_handle) { printf("[CONFIG] %s %d no function %s for config mode %s\n",
fprintf(stderr,"[CONFIG] %s %d Error calling dlopen(%s): %s\n",__FILE__, __LINE__, libname,dlerror()); __FILE__, __LINE__,fname, cfgptr->cfgmode);
st = -1; st = -1;
} else { }
sprintf (fname,"config_%s_init",cfgptr->cfgmode);
cfgptr->init = dlsym(lib_handle,fname);
if (cfgptr->init == NULL ) {
printf("[CONFIG] %s %d no function %s for config mode %s\n",
__FILE__, __LINE__,fname, cfgptr->cfgmode);
} else {
st=cfgptr->init(cfgptr->cfgP,cfgptr->num_cfgP);
printf("[CONFIG] function %s returned %i\n",
fname, st);
}
sprintf (fname,"config_%s_get",cfgptr->cfgmode); sprintf (fname,"config_%s_getlist",cfgptr->cfgmode);
cfgptr->get = dlsym(lib_handle,fname); cfgptr->getlist = dlsym(lib_handle,fname);
if (cfgptr->get == NULL ) {
printf("[CONFIG] %s %d no function %s for config mode %s\n", if (cfgptr->getlist == NULL ) {
__FILE__, __LINE__,fname, cfgptr->cfgmode); printf("[CONFIG] %s %d no function %s for config mode %s\n",
st = -1; __FILE__, __LINE__,fname, cfgptr->cfgmode);
} st = -1;
}
sprintf (fname,"config_%s_getlist",cfgptr->cfgmode);
cfgptr->getlist = dlsym(lib_handle,fname);
if (cfgptr->getlist == NULL ) {
printf("[CONFIG] %s %d no function %s for config mode %s\n",
__FILE__, __LINE__,fname, cfgptr->cfgmode);
st = -1;
}
sprintf (fname,"config_%s_end",cfgptr->cfgmode); sprintf (fname,"config_%s_end",cfgptr->cfgmode);
cfgptr->end = dlsym(lib_handle,fname); cfgptr->end = dlsym(lib_handle,fname);
if (cfgptr->getlist == NULL ) {
printf("[CONFIG] %s %d no function %s for config mode %s\n", if (cfgptr->getlist == NULL ) {
__FILE__, __LINE__,fname, cfgptr->cfgmode); printf("[CONFIG] %s %d no function %s for config mode %s\n",
} __FILE__, __LINE__,fname, cfgptr->cfgmode);
} }
}
return st;
return st;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* from here: interface implementtion of the configuration module */ /* from here: interface implementtion of the configuration module */
int nooptfunc(void) { int nooptfunc(void) {
return 0; return 0;
}; };
int config_cmdlineonly_getlist(paramlist_def_t *ParamList, int config_cmdlineonly_getlist(paramlist_def_t *ParamList,
paramdef_t *params, int numparams, char *prefix) paramdef_t *params, int numparams, char *prefix) {
{ ParamList->numelt = 0;
ParamList->numelt = 0; return 0;
return 0;