X-Git-Url: https://git.cameronkatri.com/cgit.git/blobdiff_plain/53bc747d311d18642fa3ad0cc0de34f3899ed1f4..0899eb644fab415e9a3b304f53da9da50aaf91aa:/configfile.c diff --git a/configfile.c b/configfile.c index fe5f9c5..e039109 100644 --- a/configfile.c +++ b/configfile.c @@ -1,16 +1,15 @@ /* configfile.c: parsing of config files * - * Copyright (C) 2008 Lars Hjemli + * Copyright (C) 2006-2014 cgit Development Team * * Licensed under GNU General Public License v2 * (see COPYING for full license text) */ -#include -#include +#include #include "configfile.h" -int next_char(FILE *f) +static int next_char(FILE *f) { int c = fgetc(f); if (c == '\r') { @@ -23,53 +22,55 @@ int next_char(FILE *f) return c; } -void skip_line(FILE *f) +static void skip_line(FILE *f) { int c; - while((c = next_char(f)) && c != '\n' && c != EOF) + while ((c = next_char(f)) && c != '\n' && c != EOF) ; } -int read_config_line(FILE *f, char *line, const char **value, int bufsize) +static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value) { - int i = 0, isname = 0; + int c = next_char(f); - *value = NULL; - while(i < bufsize - 1) { - int c = next_char(f); - if (!isname && (c == '#' || c == ';')) { - skip_line(f); - continue; - } - if (!isname && isspace(c)) - continue; + strbuf_reset(name); + strbuf_reset(value); - if (c == '=' && !*value) { - line[i] = 0; - *value = &line[i + 1]; - } else if (c == '\n' && !isname) { - i = 0; - continue; - } else if (c == '\n' || c == EOF) { - line[i] = 0; + /* Skip comments and preceding spaces. */ + for(;;) { + if (c == EOF) + return 0; + else if (c == '#' || c == ';') + skip_line(f); + else if (!isspace(c)) break; - } else { - line[i] = c; - } - isname = 1; - i++; + c = next_char(f); } - line[i + 1] = 0; - return i; + + /* Read variable name. */ + while (c != '=') { + if (c == '\n' || c == EOF) + return 0; + strbuf_addch(name, c); + c = next_char(f); + } + + /* Read variable value. */ + c = next_char(f); + while (c != '\n' && c != EOF) { + strbuf_addch(value, c); + c = next_char(f); + } + + return 1; } int parse_configfile(const char *filename, configfile_value_fn fn) { static int nesting; - int len; - char line[256]; - const char *value; + struct strbuf name = STRBUF_INIT; + struct strbuf value = STRBUF_INIT; FILE *f; /* cancel deeply nested include-commands */ @@ -78,10 +79,12 @@ int parse_configfile(const char *filename, configfile_value_fn fn) if (!(f = fopen(filename, "r"))) return -1; nesting++; - while((len = read_config_line(f, line, &value, sizeof(line))) > 0) - fn(line, value); + while (read_config_line(f, &name, &value)) + fn(name.buf, value.buf); nesting--; fclose(f); + strbuf_release(&name); + strbuf_release(&value); return 0; }