Commit 6421c49a by Carlos Martín Nieto

Fix variable name normalization

When I changed it over to use different strings for the variable and
the name, cvar_name_normalize was left behind. Fix this and rename to
cvar_normalize_name to reflect the incompatible change.

Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
parent f44cbec4
...@@ -197,36 +197,49 @@ static cvar_t *cvar_list_find(cvar_t_list *list, const char *name) ...@@ -197,36 +197,49 @@ static cvar_t *cvar_list_find(cvar_t_list *list, const char *name)
return NULL; return NULL;
} }
static int cvar_name_normalize(const char *input, char **output) static int cvar_normalize_name(cvar_t *var, char **output)
{ {
char *input_sp = strchr(input, ' '); char *section_sp = strchr(var->section, ' ');
char *quote, *str; char *quote, *name;
int i; int len, ret;
/* We need to make a copy anyway */ /*
str = git__strdup(input); * The final string is going to be at most one char longer than
if (str == NULL) * the input
*/
len = strlen(var->section) + strlen(var->name) + 1;
name = git__malloc(len + 1);
if (name == NULL)
return GIT_ENOMEM; return GIT_ENOMEM;
*output = str; /* If there aren't any spaces in the section, it's easy */
if (section_sp == NULL) {
ret = snprintf(name, len + 1, "%s.%s", var->section, var->name);
if (ret < 0)
return git__throw(GIT_EOSERR, "Failed to normalize name. OS err: %s", strerror(errno));
/* If there aren't any spaces, we don't need to do anything */ *output = name;
if (input_sp == NULL)
return GIT_SUCCESS; return GIT_SUCCESS;
}
/* /*
* If there are spaces, we replace the space by a dot, move the * If there are spaces, we replace the space by a dot, move
* variable name so that the dot before it replaces the last * section name so it overwrites the first quotation mark and
* quotation mark and repeat so that the first quotation mark * replace the last quotation mark by a dot. We then append the
* disappears. * variable name.
*/ */
str[input_sp - input] = '.'; strcpy(name, var->section);
section_sp = strchr(name, ' ');
for (i = 0; i < 2; ++i) { *section_sp = '.';
quote = strrchr(str, '"'); /* Remove first quote */
memmove(quote, quote + 1, strlen(quote)); quote = strchr(name, '"');
} memmove(quote, quote+1, strlen(quote+1));
/* Remove second quote */
quote = strchr(name, '"');
*quote = '.';
strcpy(quote+1, var->name);
*output = name;
return GIT_SUCCESS; return GIT_SUCCESS;
} }
...@@ -276,7 +289,7 @@ static int file_foreach(git_config_backend *backend, int (*fn)(const char *, voi ...@@ -276,7 +289,7 @@ static int file_foreach(git_config_backend *backend, int (*fn)(const char *, voi
file_backend *b = (file_backend *)backend; file_backend *b = (file_backend *)backend;
CVAR_LIST_FOREACH(&b->var_list, var) { CVAR_LIST_FOREACH(&b->var_list, var) {
ret = cvar_name_normalize(var->name, &normalized); ret = cvar_normalize_name(var, &normalized);
if (ret < GIT_SUCCESS) if (ret < GIT_SUCCESS)
return ret; return ret;
......
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