2008-03-28 00:09:11 +01:00
|
|
|
/* configfile.c: parsing of config files
|
|
|
|
*
|
2014-01-08 15:10:49 +01:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-03-28 00:09:11 +01:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "configfile.h"
|
|
|
|
|
2013-03-04 08:52:33 +01:00
|
|
|
static int next_char(FILE *f)
|
2008-03-28 00:09:11 +01:00
|
|
|
{
|
|
|
|
int c = fgetc(f);
|
2013-03-03 16:04:29 +01:00
|
|
|
if (c == '\r') {
|
2008-03-28 00:09:11 +01:00
|
|
|
c = fgetc(f);
|
2013-03-03 16:04:29 +01:00
|
|
|
if (c != '\n') {
|
2008-03-28 00:09:11 +01:00
|
|
|
ungetc(c, f);
|
|
|
|
c = '\r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-03-04 08:52:33 +01:00
|
|
|
static void skip_line(FILE *f)
|
2008-03-28 00:09:11 +01:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2013-03-04 05:21:33 +01:00
|
|
|
while ((c = next_char(f)) && c != '\n' && c != EOF)
|
2008-03-28 00:09:11 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2013-06-04 16:47:53 +02:00
|
|
|
static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
|
2008-03-28 00:09:11 +01:00
|
|
|
{
|
2013-06-04 16:47:53 +02:00
|
|
|
int c = next_char(f);
|
2008-03-28 00:09:11 +01:00
|
|
|
|
2013-06-04 16:47:53 +02:00
|
|
|
strbuf_reset(name);
|
|
|
|
strbuf_reset(value);
|
2008-03-28 00:09:11 +01:00
|
|
|
|
2013-06-04 16:47:53 +02:00
|
|
|
/* Skip comments and preceding spaces. */
|
|
|
|
for(;;) {
|
|
|
|
if (c == '#' || c == ';')
|
|
|
|
skip_line(f);
|
|
|
|
else if (!isspace(c))
|
2008-03-28 00:09:11 +01:00
|
|
|
break;
|
2013-06-04 16:47:53 +02:00
|
|
|
c = next_char(f);
|
2008-03-28 00:09:11 +01:00
|
|
|
}
|
2013-06-04 16:47:53 +02:00
|
|
|
|
|
|
|
/* 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;
|
2008-03-28 00:09:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int parse_configfile(const char *filename, configfile_value_fn fn)
|
|
|
|
{
|
|
|
|
static int nesting;
|
2013-06-04 16:47:53 +02:00
|
|
|
struct strbuf name = STRBUF_INIT;
|
|
|
|
struct strbuf value = STRBUF_INIT;
|
2008-03-28 00:09:11 +01:00
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
/* cancel deeply nested include-commands */
|
|
|
|
if (nesting > 8)
|
|
|
|
return -1;
|
|
|
|
if (!(f = fopen(filename, "r")))
|
|
|
|
return -1;
|
|
|
|
nesting++;
|
2013-06-04 16:47:53 +02:00
|
|
|
while (read_config_line(f, &name, &value))
|
|
|
|
fn(name.buf, value.buf);
|
2008-03-28 00:09:11 +01:00
|
|
|
nesting--;
|
|
|
|
fclose(f);
|
2013-06-04 16:47:53 +02:00
|
|
|
strbuf_release(&name);
|
|
|
|
strbuf_release(&value);
|
2008-03-28 00:09:11 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|