1 /* config.c: parsing of config files
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 int next_char(FILE *f)
24 void skip_line(FILE *f)
28 while((c=next_char(f)) && c!='\n' && c!=EOF)
32 int read_config_line(FILE *f, char *line, const char **value, int bufsize)
34 int i = 0, isname = 0;
39 if (!isname && (c=='#' || c==';')) {
43 if (!isname && isspace(c))
46 if (c=='=' && !*value) {
49 } else if (c=='\n' && !isname) {
52 } else if (c=='\n' || c==EOF) {
65 int cgit_read_config(const char *filename, configfn fn)
70 FILE *f = fopen(filename, "r");
75 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
82 int cgit_parse_query(char *txt, configfn fn)
84 char *t, *value = NULL, c;
89 t = txt = xstrdup(txt);
91 while((c=*t) != '\0') {
108 char *substr(const char *head, const char *tail)
112 buf = xmalloc(tail - head + 1);
113 strncpy(buf, head, tail - head);
114 buf[tail - head] = '\0';
118 struct commitinfo *cgit_parse_commit(struct commit *commit)
120 struct commitinfo *ret;
121 char *p = commit->buffer, *t = commit->buffer;
123 ret = xmalloc(sizeof(*ret));
124 ret->commit = commit;
126 if (strncmp(p, "tree ", 5))
127 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
129 p += 46; // "tree " + hex[40] + "\n"
131 while (!strncmp(p, "parent ", 7))
132 p += 48; // "parent " + hex[40] + "\n"
134 if (!strncmp(p, "author ", 7)) {
136 t = strchr(p, '<') - 1;
137 ret->author = substr(p, t);
139 t = strchr(t, '>') + 1;
140 ret->author_email = substr(p, t);
141 ret->author_date = atol(++t);
142 p = strchr(t, '\n') + 1;
145 if (!strncmp(p, "committer ", 9)) {
147 t = strchr(p, '<') - 1;
148 ret->committer = substr(p, t);
150 t = strchr(t, '>') + 1;
151 ret->committer_email = substr(p, t);
152 ret->committer_date = atol(++t);
153 p = strchr(t, '\n') + 1;
157 p = strchr(p, '\n') + 1;
160 ret->subject = substr(p, t);
164 p = strchr(p, '\n') + 1;