summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-02-20 07:36:40 +0100
committerSébastien Helleu <flashcode@flashtux.org>2023-03-16 20:42:18 +0100
commit7b8e5b36c0e894df40642b3ce993767844edc16b (patch)
tree602c750d37ccbc01259852ec46dac8aa22f99bc2 /src
parent31bf962bba5c8b81a59f4173405e48a2f3887c1e (diff)
downloadweechat-7b8e5b36c0e894df40642b3ce993767844edc16b.zip
core: simplify function config_file_read_internal
Diffstat (limited to 'src')
-rw-r--r--src/core/wee-config-file.c336
1 files changed, 173 insertions, 163 deletions
diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c
index 045f5c73e..8b4d42104 100644
--- a/src/core/wee-config-file.c
+++ b/src/core/wee-config-file.c
@@ -2808,11 +2808,11 @@ config_file_write (struct t_config_file *config_file)
int
config_file_read_internal (struct t_config_file *config_file, int reload)
{
- int filename_length, line_number, rc, undefined_value;
- char *filename;
+ int filename_length, line_number, rc, length;
+ char *filename, *section, *option, *value;
struct t_config_section *ptr_section;
struct t_config_option *ptr_option;
- char line[16384], *ptr_line, *ptr_line2, *pos, *pos2, *ptr_option_name;
+ char line[16384], *ptr_line, *ptr_line2, *pos, *pos2;
if (!config_file)
return WEECHAT_CONFIG_READ_FILE_NOT_FOUND;
@@ -2862,192 +2862,202 @@ config_file_read_internal (struct t_config_file *config_file, int reload)
line_number = 0;
while (!feof (config_file->file))
{
- ptr_line = fgets (line, sizeof (line) - 1, config_file->file);
line_number++;
- if (ptr_line)
+
+ ptr_line = fgets (line, sizeof (line) - 1, config_file->file);
+ if (!ptr_line)
+ continue;
+
+ /* encode line to internal charset */
+ ptr_line2 = string_iconv_to_internal (NULL, ptr_line);
+ if (ptr_line2)
{
- /* encode line to internal charset */
- ptr_line2 = string_iconv_to_internal (NULL, ptr_line);
- if (ptr_line2)
- snprintf (line, sizeof (line), "%s", ptr_line2);
+ snprintf (line, sizeof (line), "%s", ptr_line2);
+ free (ptr_line2);
+ }
+
+ /* skip spaces */
+ while (ptr_line[0] == ' ')
+ {
+ ptr_line++;
+ }
+
+ /* remove CR/LF */
+ pos = strchr (ptr_line, '\r');
+ if (pos)
+ pos[0] = '\0';
+ pos = strchr (ptr_line, '\n');
+ if (pos)
+ pos[0] = '\0';
+
+ /* ignore empty line or comment */
+ if (!ptr_line[0] || (ptr_line[0] == '#'))
+ continue;
- /* skip spaces */
- while (ptr_line[0] == ' ')
+ /* beginning of section */
+ if ((ptr_line[0] == '[') && !strchr (ptr_line, '='))
+ {
+ pos = strchr (ptr_line, ']');
+ if (!pos)
{
- ptr_line++;
+ gui_chat_printf (NULL,
+ _("%sWarning: %s, line %d: invalid "
+ "syntax, missing \"]\""),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ filename, line_number);
}
-
- /* not a comment and not an empty line */
- if ((ptr_line[0] != '#') && (ptr_line[0] != '\r')
- && (ptr_line[0] != '\n'))
+ else
{
- /* beginning of section */
- if ((ptr_line[0] == '[') && !strchr (ptr_line, '='))
+ section = string_strndup (ptr_line + 1, pos - ptr_line - 1);
+ if (section)
{
- pos = strchr (line, ']');
- if (!pos)
+ ptr_section = config_file_search_section (config_file,
+ section);
+ if (!ptr_section)
{
gui_chat_printf (NULL,
- _("%sWarning: %s, line %d: invalid "
- "syntax, missing \"]\""),
+ _("%sWarning: %s, line %d: unknown "
+ "section identifier (\"%s\")"),
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- filename, line_number);
- }
- else
- {
- pos[0] = '\0';
- pos = ptr_line + 1;
- ptr_section = config_file_search_section (config_file,
- pos);
- if (!ptr_section)
- {
- gui_chat_printf (NULL,
- _("%sWarning: %s, line %d: unknown "
- "section identifier "
- "(\"%s\")"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- filename, line_number, pos);
- }
+ filename, line_number, section);
}
+ free (section);
}
- else
- {
- undefined_value = 1;
-
- /* remove CR/LF */
- pos = strchr (line, '\r');
- if (pos != NULL)
- pos[0] = '\0';
- pos = strchr (line, '\n');
- if (pos != NULL)
- pos[0] = '\0';
-
- pos = strstr (line, " =");
- if (pos)
- {
- pos[0] = '\0';
- pos += 2;
+ }
+ continue;
+ }
- /* remove spaces before '=' */
- pos2 = pos - 3;
- while ((pos2 > line) && (pos2[0] == ' '))
- {
- pos2[0] = '\0';
- pos2--;
- }
+ /* option */
+ option = NULL;
+ value = NULL;
- /* skip spaces after '=' */
- while (pos[0] && (pos[0] == ' '))
- {
- pos++;
- }
+ /* skip escape char */
+ if (ptr_line[0] == '\\')
+ ptr_line++;
- if (pos[0]
- && strcmp (pos, WEECHAT_CONFIG_OPTION_NULL) != 0)
- {
- undefined_value = 0;
- /* remove simple or double quotes and spaces at the end */
- if (strlen (pos) > 1)
- {
- pos2 = pos + strlen (pos) - 1;
- while ((pos2 > pos) && (pos2[0] == ' '))
- {
- pos2[0] = '\0';
- pos2--;
- }
- pos2 = pos + strlen (pos) - 1;
- if (((pos[0] == '\'') &&
- (pos2[0] == '\'')) ||
- ((pos[0] == '"') &&
- (pos2[0] == '"')))
- {
- pos2[0] = '\0';
- pos++;
- }
- }
- }
+ pos = strstr (ptr_line, " =");
+ if (pos)
+ {
+ /* skip spaces before '=' */
+ pos2 = pos - 1;
+ while ((pos2 > ptr_line) && (pos2[0] == ' '))
+ {
+ pos2--;
+ }
+ option = string_strndup (ptr_line, pos2 + 1 - ptr_line);
+ /* skip spaces after '=' */
+ pos += 2;
+ while (pos[0] == ' ')
+ {
+ pos++;
+ }
+ if (strcmp (pos, WEECHAT_CONFIG_OPTION_NULL) != 0)
+ {
+ length = strlen (pos);
+ if (length > 1)
+ {
+ /* remove simple or double quotes and spaces at the end */
+ pos2 = pos + length - 1;
+ while ((pos2 > pos) && (pos2[0] == ' '))
+ {
+ pos2--;
}
-
- ptr_option_name = (line[0] == '\\') ? line + 1 : line;
-
- if (ptr_section && ptr_section->callback_read)
+ if (((pos[0] == '\'') && (pos2[0] == '\''))
+ || ((pos[0] == '"') && (pos2[0] == '"')))
{
- ptr_option = NULL;
- rc = (ptr_section->callback_read)
- (ptr_section->callback_read_pointer,
- ptr_section->callback_read_data,
- config_file,
- ptr_section,
- ptr_option_name,
- (undefined_value) ? NULL : pos);
+ value = string_strndup (pos + 1, pos2 - pos - 1);
}
else
{
- rc = WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND;
- ptr_option = config_file_search_option (config_file,
- ptr_section,
- ptr_option_name);
- if (ptr_option)
- {
- rc = config_file_option_set (ptr_option,
- (undefined_value) ?
- NULL : pos,
- 1);
- ptr_option->loaded = 1;
- }
- else
- {
- if (ptr_section
- && ptr_section->callback_create_option)
- {
- rc = (int) (ptr_section->callback_create_option) (
- ptr_section->callback_create_option_pointer,
- ptr_section->callback_create_option_data,
- config_file,
- ptr_section,
- ptr_option_name,
- (undefined_value) ? NULL : pos);
- }
- }
+ value = string_strndup (pos, pos2 + 1 - pos);
}
+ }
+ else
+ {
+ value = strdup (pos);
+ }
+ }
+ }
+ else
+ {
+ option = strdup (ptr_line);
+ }
- switch (rc)
- {
- case WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND:
- if (ptr_section)
- gui_chat_printf (NULL,
- _("%sWarning: %s, line %d: "
- "unknown option for section "
- "\"%s\": %s"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- filename, line_number,
- ptr_section->name,
- ptr_line2);
- else
- gui_chat_printf (NULL,
- _("%sWarning: %s, line %d: "
- "option outside section: "
- "%s"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- filename, line_number,
- ptr_line2);
- break;
- case WEECHAT_CONFIG_OPTION_SET_ERROR:
- gui_chat_printf (NULL,
- _("%sWarning: %s, line %d: "
- "invalid value for option: "
- "%s"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- filename, line_number,
- ptr_line2);
- break;
- }
+ if (ptr_section && ptr_section->callback_read)
+ {
+ ptr_option = NULL;
+ rc = (ptr_section->callback_read)
+ (ptr_section->callback_read_pointer,
+ ptr_section->callback_read_data,
+ config_file,
+ ptr_section,
+ option,
+ value);
+ }
+ else
+ {
+ rc = WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND;
+ ptr_option = config_file_search_option (config_file,
+ ptr_section,
+ option);
+ if (ptr_option)
+ {
+ rc = config_file_option_set (ptr_option, value, 1);
+ ptr_option->loaded = 1;
+ }
+ else
+ {
+ if (ptr_section
+ && ptr_section->callback_create_option)
+ {
+ rc = (int) (ptr_section->callback_create_option) (
+ ptr_section->callback_create_option_pointer,
+ ptr_section->callback_create_option_data,
+ config_file,
+ ptr_section,
+ option,
+ value);
}
}
+ }
- if (ptr_line2)
- free (ptr_line2);
+ switch (rc)
+ {
+ case WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND:
+ if (ptr_section)
+ {
+ gui_chat_printf (NULL,
+ _("%sWarning: %s, line %d: "
+ "unknown option for section \"%s\": %s"),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ filename, line_number,
+ ptr_section->name,
+ line);
+ }
+ else
+ {
+ gui_chat_printf (NULL,
+ _("%sWarning: %s, line %d: "
+ "option outside section: %s"),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ filename, line_number,
+ line);
+ }
+ break;
+ case WEECHAT_CONFIG_OPTION_SET_ERROR:
+ gui_chat_printf (NULL,
+ _("%sWarning: %s, line %d: "
+ "invalid value for option: %s"),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ filename, line_number,
+ line);
+ break;
}
+
+ if (option)
+ free (option);
+ if (value)
+ free (value);
}
fclose (config_file->file);