about summary refs log tree commit diff stats
path: root/configfile.c
diff options
context:
space:
mode:
authorLars Hjemli2008-04-08 21:29:21 +0200
committerLars Hjemli2008-04-08 21:29:21 +0200
commit23296ad648c0e2a9e3cf40a3de322b10ad25cce3 (patch)
tree136493d8228b0ff4971feb06b0e8aee296367b00 /configfile.c
parentUse GIT-1.5.5-rc2 (diff)
parentReset ctx.repo to NULL when the config parser is finished (diff)
downloadcgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.tar.gz
cgit-23296ad648c0e2a9e3cf40a3de322b10ad25cce3.zip
Merge branch 'lh/cleanup'
* lh/cleanup: (21 commits)
  Reset ctx.repo to NULL when the config parser is finished
  Move cgit_parse_query() from parsing.c to html.c as http_parse_querystring()
  Move function for configfile parsing into configfile.[ch]
  Add cache.h
  Remove global and obsolete cgit_cmd
  Makefile: copy the QUIET constructs from the Makefile in git.git
  Move cgit_version from shared.c to cgit.c
  Makefile: autobuild dependency rules
  Initial Makefile cleanup
  Move non-generic functions from shared.c to cgit.c
  Add ui-shared.h
  Add separate header-files for each page/view
  Refactor snapshot support
  Add command dispatcher
  Remove obsolete cacheitem parameter to ui-functions
  Add struct cgit_page to cgit_context
  Introduce html.h
  Improve initialization of git directory
  Move cgit_repo into cgit_context
  Add all config variables into struct cgit_context
  ...
Diffstat (limited to 'configfile.c')
-rw-r--r--configfile.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..4908058 --- /dev/null +++ b/configfile.c
@@ -0,0 +1,87 @@
1/* configfile.c: parsing of config files
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include <ctype.h>
10#include <stdio.h>
11#include "configfile.h"
12
13int next_char(FILE *f)
14{
15 int c = fgetc(f);
16 if (c=='\r') {
17 c = fgetc(f);
18 if (c!='\n') {
19 ungetc(c, f);
20 c = '\r';
21 }
22 }
23 return c;
24}
25
26void skip_line(FILE *f)
27{
28 int c;
29
30 while((c=next_char(f)) && c!='\n' && c!=EOF)
31 ;
32}
33
34int read_config_line(FILE *f, char *line, const char **value, int bufsize)
35{
36 int i = 0, isname = 0;
37
38 *value = NULL;
39 while(i<bufsize-1) {
40 int c = next_char(f);
41 if (!isname && (c=='#' || c==';')) {
42 skip_line(f);
43 continue;
44 }
45 if (!isname && isspace(c))
46 continue;
47
48 if (c=='=' && !*value) {
49 line[i] = 0;
50 *value = &line[i+1];
51 } else if (c=='\n' && !isname) {
52 i = 0;
53 continue;
54 } else if (c=='\n' || c==EOF) {
55 line[i] = 0;
56 break;
57 } else {
58 line[i]=c;
59 }
60 isname = 1;
61 i++;
62 }
63 line[i+1] = 0;
64 return i;
65}
66
67int parse_configfile(const char *filename, configfile_value_fn fn)
68{
69 static int nesting;
70 int len;
71 char line[256];
72 const char *value;
73 FILE *f;
74
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((len = read_config_line(f, line, &value, sizeof(line))) > 0)
82 fn(line, value);
83 nesting--;
84 fclose(f);
85 return 0;
86}
87