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);
136 * url syntax: [repo ['/' cmd [ '/' path]]]
137 * repo: any valid repo url, may contain '/'
138 * cmd: log | commit | diff | tree | view | blob | snapshot
139 * path: any valid path, may contain '/'
142 void cgit_parse_url(const char *url)
147 if (!url || url[0] == '\0')
150 cgit_repo = cgit_get_repoinfo(url);
152 cgit_query_repo = cgit_repo->url;
156 cmd = strchr(url, '/');
157 while (!cgit_repo && cmd) {
159 cgit_repo = cgit_get_repoinfo(url);
160 if (cgit_repo == NULL) {
162 cmd = strchr(cmd + 1, '/');
166 cgit_query_repo = cgit_repo->url;
167 p = strchr(cmd + 1, '/');
171 cgit_query_path = trim_end(p + 1, '/');
173 cgit_cmd = cgit_get_cmd_index(cmd + 1);
174 cgit_query_page = xstrdup(cmd + 1);
179 char *substr(const char *head, const char *tail)
183 buf = xmalloc(tail - head + 1);
184 strncpy(buf, head, tail - head);
185 buf[tail - head] = '\0';
189 struct commitinfo *cgit_parse_commit(struct commit *commit)
191 struct commitinfo *ret;
192 char *p = commit->buffer, *t = commit->buffer;
194 ret = xmalloc(sizeof(*ret));
195 ret->commit = commit;
197 ret->author_email = NULL;
198 ret->committer = NULL;
199 ret->committer_email = NULL;
202 ret->msg_encoding = NULL;
207 if (strncmp(p, "tree ", 5))
208 die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
210 p += 46; // "tree " + hex[40] + "\n"
212 while (!strncmp(p, "parent ", 7))
213 p += 48; // "parent " + hex[40] + "\n"
215 if (!strncmp(p, "author ", 7)) {
217 t = strchr(p, '<') - 1;
218 ret->author = substr(p, t);
220 t = strchr(t, '>') + 1;
221 ret->author_email = substr(p, t);
222 ret->author_date = atol(++t);
223 p = strchr(t, '\n') + 1;
226 if (!strncmp(p, "committer ", 9)) {
228 t = strchr(p, '<') - 1;
229 ret->committer = substr(p, t);
231 t = strchr(t, '>') + 1;
232 ret->committer_email = substr(p, t);
233 ret->committer_date = atol(++t);
234 p = strchr(t, '\n') + 1;
237 if (!strncmp(p, "encoding ", 9)) {
239 t = strchr(p, '\n') + 1;
240 ret->msg_encoding = substr(p, t);
243 ret->msg_encoding = xstrdup(PAGE_ENCODING);
245 while (*p && (*p != '\n'))
246 p = strchr(p, '\n') + 1; // skip unknown header fields
249 p = strchr(p, '\n') + 1;
254 ret->subject = "** empty **";
256 ret->subject = substr(p, t);
260 p = strchr(p, '\n') + 1;
261 ret->msg = xstrdup(p);
263 ret->subject = substr(p, p+strlen(p));
265 if(strcmp(ret->msg_encoding, PAGE_ENCODING)) {
266 t = reencode_string(ret->subject, PAGE_ENCODING,
273 t = reencode_string(ret->msg, PAGE_ENCODING,
285 struct taginfo *cgit_parse_tag(struct tag *tag)
288 enum object_type type;
293 data = read_sha1_file(tag->object.sha1, &type, &size);
294 if (!data || type != OBJ_TAG) {
299 ret = xmalloc(sizeof(*ret));
301 ret->tagger_email = NULL;
302 ret->tagger_date = 0;
311 if (!strncmp(p, "tagger ", 7)) {
313 t = strchr(p, '<') - 1;
314 ret->tagger = substr(p, t);
316 t = strchr(t, '>') + 1;
317 ret->tagger_email = substr(p, t);
318 ret->tagger_date = atol(++t);
320 p = strchr(p, '\n') + 1;
323 while (p && *p && (*p != '\n'))
324 p = strchr(p, '\n') + 1; // skip unknown tag fields
326 while (p && (*p == '\n'))
327 p = strchr(p, '\n') + 1;
329 ret->msg = xstrdup(p);