summaryrefslogtreecommitdiff
path: root/src/config_file.c
diff options
context:
space:
mode:
authorVicent Martí <tanoku@gmail.com>2011-05-19 14:18:57 -0700
committerVicent Martí <tanoku@gmail.com>2011-05-19 14:18:57 -0700
commit274f2c213fdb2402b586827e6e6e27b2e055101c (patch)
treedb8c6b14f74d9518ad7694b6bc9c67f26d7e6c5d /src/config_file.c
parent44d16d6fb2987d4025714214139b22fc1a2ccfe1 (diff)
parent6421c49a3726b4132aafcc780016f5a984832caf (diff)
downloadlibgit2-274f2c213fdb2402b586827e6e6e27b2e055101c.tar.gz
Merge pull request #193 from carlosmn/config
A couple of config improvements
Diffstat (limited to 'src/config_file.c')
-rw-r--r--src/config_file.c57
1 files changed, 35 insertions, 22 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 75319a6a3..40829eb49 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -197,36 +197,49 @@ static cvar_t *cvar_list_find(cvar_t_list *list, const char *name)
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 *quote, *str;
- int i;
+ char *section_sp = strchr(var->section, ' ');
+ char *quote, *name;
+ int len, ret;
- /* We need to make a copy anyway */
- str = git__strdup(input);
- if (str == NULL)
+ /*
+ * The final string is going to be at most one char longer than
+ * the input
+ */
+ len = strlen(var->section) + strlen(var->name) + 1;
+ name = git__malloc(len + 1);
+ if (name == NULL)
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 */
- if (input_sp == NULL)
+ *output = name;
return GIT_SUCCESS;
+ }
/*
- * If there are spaces, we replace the space by a dot, move the
- * variable name so that the dot before it replaces the last
- * quotation mark and repeat so that the first quotation mark
- * disappears.
+ * If there are spaces, we replace the space by a dot, move
+ * section name so it overwrites the first quotation mark and
+ * replace the last quotation mark by a dot. We then append the
+ * variable name.
*/
- str[input_sp - input] = '.';
-
- for (i = 0; i < 2; ++i) {
- quote = strrchr(str, '"');
- memmove(quote, quote + 1, strlen(quote));
- }
-
+ strcpy(name, var->section);
+ section_sp = strchr(name, ' ');
+ *section_sp = '.';
+ /* Remove first 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;
}
@@ -276,7 +289,7 @@ static int file_foreach(git_config_backend *backend, int (*fn)(const char *, voi
file_backend *b = (file_backend *)backend;
CVAR_LIST_FOREACH(&b->var_list, var) {
- ret = cvar_name_normalize(var->name, &normalized);
+ ret = cvar_normalize_name(var, &normalized);
if (ret < GIT_SUCCESS)
return ret;