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)
73 /* cancel the reading of yet another configfile after 16 invocations */
76 if (!(f = fopen(filename, "r")))
78 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
84 char *convert_query_hexchar(char *txt)
87 if (strlen(txt) < 3) {
91 d1 = hextoint(*(txt+1));
92 d2 = hextoint(*(txt+2));
103 int cgit_parse_query(char *txt, configfn fn)
105 char *t, *value = NULL, c;
110 t = txt = xstrdup(txt);
112 while((c=*t) != '\0') {
119 t = convert_query_hexchar(t);
133 char *substr(const char *head, const char *tail)
137 buf = xmalloc(tail - head + 1);
138 strncpy(buf, head, tail - head);
139 buf[tail - head] = '\0';
143 struct commitinfo *cgit_parse_commit(struct commit *commit)
145 struct commitinfo *ret;
146 char *p = commit->buffer, *t = commit->buffer;
148 ret = xmalloc(sizeof(*ret));
149 ret->commit = commit;
151 ret->author_email = NULL;
152 ret->committer = NULL;
153 ret->committer_email = NULL;
157 if (strncmp(p, "tree ", 5))
158 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
160 p += 46; // "tree " + hex[40] + "\n"
162 while (!strncmp(p, "parent ", 7))
163 p += 48; // "parent " + hex[40] + "\n"
165 if (!strncmp(p, "author ", 7)) {
167 t = strchr(p, '<') - 1;
168 ret->author = substr(p, t);
170 t = strchr(t, '>') + 1;
171 ret->author_email = substr(p, t);
172 ret->author_date = atol(++t);
173 p = strchr(t, '\n') + 1;
176 if (!strncmp(p, "committer ", 9)) {
178 t = strchr(p, '<') - 1;
179 ret->committer = substr(p, t);
181 t = strchr(t, '>') + 1;
182 ret->committer_email = substr(p, t);
183 ret->committer_date = atol(++t);
184 p = strchr(t, '\n') + 1;
188 p = strchr(p, '\n') + 1;
192 ret->subject = substr(p, t);
196 p = strchr(p, '\n') + 1;
203 struct taginfo *cgit_parse_tag(struct tag *tag)
206 enum object_type type;
211 data = read_sha1_file(tag->object.sha1, &type, &size);
212 if (!data || type != OBJ_TAG) {
217 ret = xmalloc(sizeof(*ret));
219 ret->tagger_email = NULL;
220 ret->tagger_date = 0;
229 if (!strncmp(p, "tagger ", 7)) {
231 t = strchr(p, '<') - 1;
232 ret->tagger = substr(p, t);
234 t = strchr(t, '>') + 1;
235 ret->tagger_email = substr(p, t);
236 ret->tagger_date = atol(++t);
238 p = strchr(p, '\n') + 1;
241 while (p && (*p == '\n'))
242 p = strchr(p, '\n') + 1;
244 ret->msg = xstrdup(p);