ui-shared: add homepage to tabs
[cgit.git] / configfile.c
blob5b0d880cb7d2d1da755b2f4bc621a6c460ef4df0
1 /* configfile.c: parsing of config files
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include <git-compat-util.h>
10 #include "configfile.h"
12 static int next_char(FILE *f)
14 int c = fgetc(f);
15 if (c == '\r') {
16 c = fgetc(f);
17 if (c != '\n') {
18 ungetc(c, f);
19 c = '\r';
22 return c;
25 static void skip_line(FILE *f)
27 int c;
29 while ((c = next_char(f)) && c != '\n' && c != EOF)
33 static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
35 int c = next_char(f);
37 strbuf_reset(name);
38 strbuf_reset(value);
40 /* Skip comments and preceding spaces. */
41 for(;;) {
42 if (c == '#' || c == ';')
43 skip_line(f);
44 else if (!isspace(c))
45 break;
46 c = next_char(f);
49 /* Read variable name. */
50 while (c != '=') {
51 if (c == '\n' || c == EOF)
52 return 0;
53 strbuf_addch(name, c);
54 c = next_char(f);
57 /* Read variable value. */
58 c = next_char(f);
59 while (c != '\n' && c != EOF) {
60 strbuf_addch(value, c);
61 c = next_char(f);
64 return 1;
67 int parse_configfile(const char *filename, configfile_value_fn fn)
69 static int nesting;
70 struct strbuf name = STRBUF_INIT;
71 struct strbuf value = STRBUF_INIT;
72 FILE *f;
74 /* cancel deeply nested include-commands */
75 if (nesting > 8)
76 return -1;
77 if (!(f = fopen(filename, "r")))
78 return -1;
79 nesting++;
80 while (read_config_line(f, &name, &value))
81 fn(name.buf, value.buf);
82 nesting--;
83 fclose(f);
84 strbuf_release(&name);
85 strbuf_release(&value);
86 return 0;