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 deeply nested include-commands */
76 if (!(f = fopen(filename, "r")))
79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
86 char *convert_query_hexchar(char *txt)
89 if (strlen(txt) < 3) {
93 d1 = hextoint(*(txt+1));
94 d2 = hextoint(*(txt+2));
100 strcpy(txt+1, txt+3);
105 int cgit_parse_query(char *txt, configfn fn)
107 char *t, *value = NULL, c;
112 t = txt = xstrdup(txt);
114 while((c=*t) != '\0') {
121 t = convert_query_hexchar(t);
135 char *substr(const char *head, const char *tail)
139 buf = xmalloc(tail - head + 1);
140 strncpy(buf, head, tail - head);
141 buf[tail - head] = '\0';
145 struct commitinfo *cgit_parse_commit(struct commit *commit)
147 struct commitinfo *ret;
148 char *p = commit->buffer, *t = commit->buffer;
150 ret = xmalloc(sizeof(*ret));
151 ret->commit = commit;
153 ret->author_email = NULL;
154 ret->committer = NULL;
155 ret->committer_email = NULL;
159 if (strncmp(p, "tree ", 5))
160 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
162 p += 46; // "tree " + hex[40] + "\n"
164 while (!strncmp(p, "parent ", 7))
165 p += 48; // "parent " + hex[40] + "\n"
167 if (!strncmp(p, "author ", 7)) {
169 t = strchr(p, '<') - 1;
170 ret->author = substr(p, t);
172 t = strchr(t, '>') + 1;
173 ret->author_email = substr(p, t);
174 ret->author_date = atol(++t);
175 p = strchr(t, '\n') + 1;
178 if (!strncmp(p, "committer ", 9)) {
180 t = strchr(p, '<') - 1;
181 ret->committer = substr(p, t);
183 t = strchr(t, '>') + 1;
184 ret->committer_email = substr(p, t);
185 ret->committer_date = atol(++t);
186 p = strchr(t, '\n') + 1;
190 p = strchr(p, '\n') + 1;
194 ret->subject = substr(p, t);
198 p = strchr(p, '\n') + 1;
205 struct taginfo *cgit_parse_tag(struct tag *tag)
208 enum object_type type;
213 data = read_sha1_file(tag->object.sha1, &type, &size);
214 if (!data || type != OBJ_TAG) {
219 ret = xmalloc(sizeof(*ret));
221 ret->tagger_email = NULL;
222 ret->tagger_date = 0;
231 if (!strncmp(p, "tagger ", 7)) {
233 t = strchr(p, '<') - 1;
234 ret->tagger = substr(p, t);
236 t = strchr(t, '>') + 1;
237 ret->tagger_email = substr(p, t);
238 ret->tagger_date = atol(++t);
240 p = strchr(p, '\n') + 1;
243 while (p && (*p == '\n'))
244 p = strchr(p, '\n') + 1;
246 ret->msg = xstrdup(p);