Changed to PCRE for processing configuration file parameters.

This commit is contained in:
Markus Makela
2015-08-31 11:33:10 +03:00
parent e0f95de606
commit 9d9b7bccfc
2 changed files with 75 additions and 8 deletions

View File

@ -73,7 +73,10 @@
#include <netinet/in.h>
#include <string.h>
#include <sys/utsname.h>
#include <pcre.h>
/** According to the PCRE manual, this should be a multiple of 3 */
#define MAXSCALE_PCRE_BUFSZ 24
/** Defined in log_manager.cc */
extern int lm_enabled_logfiles_bitmask;
@ -125,6 +128,57 @@ char *ptr;
return str;
}
/**
* Remove extra commas and whitespace from a string. This string is interpreted
* as a list of string values separated by commas.
* @param strptr String to clean
* @return pointer to a new string or NULL if an error occurred
*/
char* config_clean_string_list(char* str)
{
char *tmp;
if((tmp = malloc(sizeof(char)*(strlen(str) + 1))) != NULL)
{
char *ptr;
int match[MAXSCALE_PCRE_BUFSZ];
pcre* re;
const char *re_err;
int err_offset,rval;
tmp[0] = '\0';
if((re = pcre_compile("\\s*+([^,]*[^,\\s])",0,&re_err,&err_offset,NULL)) == NULL)
{
skygw_log_write(LE,"[%s] Error: Regular expression compilation failed at %d: %s",
__FUNCTION__,err_offset,re_err);
free(tmp);
return NULL;
}
ptr = str;
while((rval = pcre_exec(re,NULL,ptr,strlen(ptr),0,0,(int*)&match,MAXSCALE_PCRE_BUFSZ)) > 1)
{
const char* substr;
pcre_get_substring(ptr,(int*)&match,rval,1,&substr);
if(strlen(tmp) > 0)
strcat(tmp,",");
strcat(tmp,substr);
pcre_free_substring(substr);
ptr = &ptr[match[1]];
}
pcre_free(re);
}
else
{
skygw_log_write(LE,"[%s] Error: Memory allocation failed.",__FUNCTION__);
}
return tmp;
}
/**
* Config item handler for the ini file reader
*
@ -174,7 +228,7 @@ CONFIG_PARAMETER *param, *p1;
{
if (!strcmp(p1->name, name))
{
char* tmp;
char *tmp;
int paramlen = strlen(p1->value) + strlen(value) + 2;
if((tmp = realloc(p1->value,sizeof(char) * (paramlen))) == NULL)
@ -182,13 +236,15 @@ CONFIG_PARAMETER *param, *p1;
skygw_log_write(LE,"[%s] Error: Memory allocation failed.",__FUNCTION__);
return 0;
}
/** This is a parameter with no comma or separate definition of
* an already existing parameter. Add a comma to it and concatenate
* the values. */
if(p1->value[strlen(p1->value)-1] != ',')
strcat(tmp,",");
strcat(tmp,value);
p1->value = tmp;
if((p1->value = config_clean_string_list(tmp)) == NULL)
{
p1->value = tmp;
skygw_log_write(LE,"[%s] Error: Cleaning configuration parameter failed.",__FUNCTION__);
return 0;
}
free(tmp);
return 1;
}
p1 = p1->next;