email-gravatar: do not scale icons up
[cgit.git] / configfile.c
blob833f158e1a76469bbfdc9a1e5aac7dc0947a603f
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 <ctype.h>
10 #include <stdio.h>
11 #include "configfile.h"
13 static int next_char(FILE *f)
15 int c = fgetc(f);
16 if (c == '\r') {
17 c = fgetc(f);
18 if (c != '\n') {
19 ungetc(c, f);
20 c = '\r';
23 return c;
26 static void skip_line(FILE *f)
28 int c;
30 while ((c = next_char(f)) && c != '\n' && c != EOF)
34 static int read_config_line(FILE *f, struct strbuf *name, struct strbuf *value)
36 int c = next_char(f);
38 strbuf_reset(name);
39 strbuf_reset(value);
41 /* Skip comments and preceding spaces. */
42 for(;;) {
43 if (c == '#' || c == ';')
44 skip_line(f);
45 else if (!isspace(c))
46 break;
47 c = next_char(f);
50 /* Read variable name. */
51 while (c != '=') {
52 if (c == '\n' || c == EOF)
53 return 0;
54 strbuf_addch(name, c);
55 c = next_char(f);
58 /* Read variable value. */
59 c = next_char(f);
60 while (c != '\n' && c != EOF) {
61 strbuf_addch(value, c);
62 c = next_char(f);
65 return 1;
68 int parse_configfile(const char *filename, configfile_value_fn fn)
70 static int nesting;
71 struct strbuf name = STRBUF_INIT;
72 struct strbuf value = STRBUF_INIT;
73 FILE *f;
75 /* cancel deeply nested include-commands */
76 if (nesting > 8)
77 return -1;
78 if (!(f = fopen(filename, "r")))
79 return -1;
80 nesting++;
81 while (read_config_line(f, &name, &value))
82 fn(name.buf, value.buf);
83 nesting--;
84 fclose(f);
85 strbuf_release(&name);
86 strbuf_release(&value);
87 return 0;