Commit 094aaaae by Carlos Martín Nieto

config: store the section name separately

The section and variable names use different rules, so store them as
two different variables internally.

This will simplify the configuration-writing code as well later on,
but even with parsing, the code is simpler.

Take this opportunity to add a variable to the list directly when
parsing instead of passing through config_set.
parent 0130d818
...@@ -42,6 +42,7 @@ static void cvar_free(git_cvar *var) ...@@ -42,6 +42,7 @@ static void cvar_free(git_cvar *var)
if (var == NULL) if (var == NULL)
return; return;
free(var->section);
free(var->name); free(var->name);
free(var->value); free(var->value);
free(var); free(var);
...@@ -59,13 +60,15 @@ static void cvar_list_free(git_cvar_list *list) ...@@ -59,13 +60,15 @@ static void cvar_list_free(git_cvar_list *list)
} }
/* /*
* The order is important. The first parameter is the name we want to * Compare two strings according to the git section-subsection
* match against, and the second one is what we're looking for * rules. The order of the strings is important because local is
* assumed to have the internal format (only the section name and with
* case information) and input the normalized one (only dots, no case
* information).
*/ */
static int cvar_section_match(const char *local, const char *input) static int cvar_match_section(const char *local, const char *input)
{ {
char *input_dot = strrchr(input, '.'); char *first_dot, *last_dot;
char *local_last_dot = strrchr(local, '.');
char *local_sp = strchr(local, ' '); char *local_sp = strchr(local, ' ');
int comparison_len; int comparison_len;
...@@ -74,45 +77,48 @@ static int cvar_section_match(const char *local, const char *input) ...@@ -74,45 +77,48 @@ static int cvar_section_match(const char *local, const char *input)
* just do a case-insensitive compare. * just do a case-insensitive compare.
*/ */
if (local_sp == NULL) if (local_sp == NULL)
return !strncasecmp(local, input, local_last_dot - local); return !strncasecmp(local, input, strlen(local));
/* Anything before the space in local is case-insensitive */ /*
* From here onwards, there is a space diving the section and the
* subsection. Anything before the space in local is
* case-insensitive.
*/
if (strncasecmp(local, input, local_sp - local)) if (strncasecmp(local, input, local_sp - local))
return 0; return 0;
/* /*
* We compare starting from the first character after the * We compare starting from the first character after the
* quotation marks, which is two characters beyond the space. For * quotation marks, which is two characters beyond the space. For
* the input, we start one character beyond the first dot. * the input, we start one character beyond the dot. If the names
* have different lengths, then we can fail early, as we know they
* can't be the same.
* The length is given by the length between the quotation marks. * The length is given by the length between the quotation marks.
*
* this "that".var
* ^ ^
* a b
*
* where a is (local_sp + 2) and b is local_last_dot. The comparison
* length is given by b - 1 - a.
*/ */
input_dot = strchr(input, '.');
comparison_len = local_last_dot - 1 - (local_sp + 2); first_dot = strchr(input, '.');
return !strncmp(local_sp + 2, input_dot + 1, comparison_len); last_dot = strrchr(input, '.');
comparison_len = strlen(local_sp + 2) - 1;
if (last_dot == first_dot || last_dot - first_dot - 1 != comparison_len)
return 0;
return !strncmp(local_sp + 2, first_dot + 1, comparison_len);
} }
static int cvar_name_match(const char *local, const char *input) static int cvar_match_name(const git_cvar *var, const char *str)
{ {
char *input_dot = strrchr(input, '.'); const char *name_start;
char *local_dot = strrchr(local, '.');
/* if (!cvar_match_section(var->section, str)) {
* First try to match the section name return 0;
*/ }
if (!cvar_section_match(local, input)) /* Early exit if the lengths are different */
name_start = strrchr(str, '.') + 1;
if (strlen(var->name) != strlen(name_start))
return 0; return 0;
/* return !strcasecmp(var->name, name_start);
* Anything after the last (possibly only) dot is case-insensitive
*/
return !strcasecmp(input_dot, local_dot);
} }
static git_cvar *cvar_list_find(git_cvar_list *list, const char *name) static git_cvar *cvar_list_find(git_cvar_list *list, const char *name)
...@@ -120,7 +126,7 @@ static git_cvar *cvar_list_find(git_cvar_list *list, const char *name) ...@@ -120,7 +126,7 @@ static git_cvar *cvar_list_find(git_cvar_list *list, const char *name)
git_cvar *iter; git_cvar *iter;
CVAR_LIST_FOREACH (list, iter) { CVAR_LIST_FOREACH (list, iter) {
if (cvar_name_match(iter->name, name)) if (cvar_match_name(iter, name))
return iter; return iter;
} }
...@@ -264,6 +270,7 @@ static int config_set(git_config *cfg, const char *name, const char *value) ...@@ -264,6 +270,7 @@ static int config_set(git_config *cfg, const char *name, const char *value)
git_cvar *var = NULL; git_cvar *var = NULL;
git_cvar *existing = NULL; git_cvar *existing = NULL;
int error = GIT_SUCCESS; int error = GIT_SUCCESS;
const char *last_dot;
/* /*
* If it already exists, we just need to update its value. * If it already exists, we just need to update its value.
...@@ -290,7 +297,19 @@ static int config_set(git_config *cfg, const char *name, const char *value) ...@@ -290,7 +297,19 @@ static int config_set(git_config *cfg, const char *name, const char *value)
memset(var, 0x0, sizeof(git_cvar)); memset(var, 0x0, sizeof(git_cvar));
var->name = git__strdup(name); last_dot = strrchr(name, '.');
if (last_dot == NULL) {
error = GIT_ERROR;
goto out;
}
var->section = git__strndup(name, last_dot - name);
if (var->section == NULL) {
error = GIT_ENOMEM;
goto out;
}
var->name = git__strdup(last_dot + 1);
if (var->name == NULL) { if (var->name == NULL) {
error = GIT_ENOMEM; error = GIT_ENOMEM;
goto out; goto out;
...@@ -639,36 +658,6 @@ static inline int config_keychar(int c) ...@@ -639,36 +658,6 @@ static inline int config_keychar(int c)
return isalnum(c) || c == '-'; return isalnum(c) || c == '-';
} }
/*
* Returns $section.$name, using only name_len chars from the name,
* which is useful so we don't have to copy the variable name
* twice. The name of the variable is set to lowercase.
* Don't forget to free the buffer.
*/
static char *build_varname(const char *section, const char *name)
{
char *varname;
int section_len, ret;
int name_len;
size_t total_len;
name_len = strlen(name);
section_len = strlen(section);
total_len = section_len + name_len + 2;
varname = malloc(total_len);
if(varname == NULL)
return NULL;
ret = snprintf(varname, total_len, "%s.%s", section, name);
if (ret >= 0) { /* lowercase from the last dot onwards */
char *dot = strrchr(varname, '.');
if (dot != NULL)
git__strtolower(dot);
}
return varname;
}
static int parse_section_header_ext(const char *line, const char *base_name, char **section_name) static int parse_section_header_ext(const char *line, const char *base_name, char **section_name)
{ {
int buf_len, total_len, pos, rpos; int buf_len, total_len, pos, rpos;
...@@ -901,7 +890,7 @@ static int config_parse(git_config *cfg_file) ...@@ -901,7 +890,7 @@ static int config_parse(git_config *cfg_file)
char *current_section = NULL; char *current_section = NULL;
char *var_name; char *var_name;
char *var_value; char *var_value;
char *full_name; git_cvar *var;
/* Initialise the reading position */ /* Initialise the reading position */
cfg_file->reader.read_ptr = cfg_file->reader.buffer.data; cfg_file->reader.read_ptr = cfg_file->reader.buffer.data;
...@@ -933,18 +922,26 @@ static int config_parse(git_config *cfg_file) ...@@ -933,18 +922,26 @@ static int config_parse(git_config *cfg_file)
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
break; break;
full_name = build_varname(current_section, var_name); var = malloc(sizeof(git_cvar));
if (full_name == NULL) { if (var == NULL) {
error = GIT_ENOMEM; error = GIT_ENOMEM;
free(var_name);
free(var_value);
break; break;
} }
config_set(cfg_file, full_name, var_value); memset(var, 0x0, sizeof(git_cvar));
free(var_name);
free(var_value); var->section = git__strdup(current_section);
free(full_name); if (var->section == NULL) {
error = GIT_ENOMEM;
free(var);
break;
}
var->name = var_name;
var->value = var_value;
git__strtolower(var->name);
CVAR_LIST_APPEND(&cfg_file->var_list, var);
break; break;
} }
......
...@@ -23,6 +23,7 @@ struct git_config { ...@@ -23,6 +23,7 @@ struct git_config {
struct git_cvar { struct git_cvar {
git_cvar *next; git_cvar *next;
char *section;
char *name; char *name;
char *value; char *value;
}; };
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#define git__calloc calloc #define git__calloc calloc
#define git__realloc realloc #define git__realloc realloc
#define git__strdup strdup #define git__strdup strdup
#define git__strndup strndup
extern int git__fmt(char *, size_t, const char *, ...) extern int git__fmt(char *, size_t, const char *, ...)
GIT_FORMAT_PRINTF(3, 4); GIT_FORMAT_PRINTF(3, 4);
......
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