2006-12-10 21:41:14 +00:00
|
|
|
/* config.c: parsing of config files
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Lars Hjemli
|
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
2006-12-09 14:18:17 +00:00
|
|
|
#include "cgit.h"
|
|
|
|
|
|
|
|
int next_char(FILE *f)
|
|
|
|
{
|
|
|
|
int c = fgetc(f);
|
|
|
|
if (c=='\r') {
|
|
|
|
c = fgetc(f);
|
|
|
|
if (c!='\n') {
|
|
|
|
ungetc(c, f);
|
|
|
|
c = '\r';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip_line(FILE *f)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while((c=next_char(f)) && c!='\n' && c!=EOF)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
int read_config_line(FILE *f, char *line, const char **value, int bufsize)
|
|
|
|
{
|
|
|
|
int i = 0, isname = 0;
|
|
|
|
|
|
|
|
*value = NULL;
|
|
|
|
while(i<bufsize-1) {
|
|
|
|
int c = next_char(f);
|
|
|
|
if (!isname && (c=='#' || c==';')) {
|
|
|
|
skip_line(f);
|
|
|
|
continue;
|
|
|
|
}
|
2006-12-10 21:31:36 +00:00
|
|
|
if (!isname && isspace(c))
|
2006-12-09 14:18:17 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c=='=' && !*value) {
|
|
|
|
line[i] = 0;
|
|
|
|
*value = &line[i+1];
|
|
|
|
} else if (c=='\n' && !isname) {
|
|
|
|
i = 0;
|
|
|
|
continue;
|
|
|
|
} else if (c=='\n' || c==EOF) {
|
|
|
|
line[i] = 0;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
line[i]=c;
|
|
|
|
}
|
|
|
|
isname = 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
line[i+1] = 0;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cgit_read_config(const char *filename, configfn fn)
|
|
|
|
{
|
2007-05-14 21:40:33 +00:00
|
|
|
static int nesting;
|
|
|
|
int len;
|
2006-12-09 14:18:17 +00:00
|
|
|
char line[256];
|
|
|
|
const char *value;
|
2007-05-14 21:40:33 +00:00
|
|
|
FILE *f;
|
2006-12-09 14:18:17 +00:00
|
|
|
|
2007-05-15 21:28:40 +00:00
|
|
|
/* cancel deeply nested include-commands */
|
|
|
|
if (nesting > 8)
|
2007-05-14 21:40:33 +00:00
|
|
|
return -1;
|
|
|
|
if (!(f = fopen(filename, "r")))
|
2006-12-09 14:18:17 +00:00
|
|
|
return -1;
|
2007-05-15 21:28:40 +00:00
|
|
|
nesting++;
|
2006-12-10 21:31:36 +00:00
|
|
|
while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
|
2006-12-09 14:18:17 +00:00
|
|
|
(*fn)(line, value);
|
2007-05-15 21:28:40 +00:00
|
|
|
nesting--;
|
2006-12-09 14:18:17 +00:00
|
|
|
fclose(f);
|
2007-05-14 21:40:33 +00:00
|
|
|
return 0;
|
2006-12-09 14:18:17 +00:00
|
|
|
}
|
|
|
|
|
2007-01-04 15:53:03 +00:00
|
|
|
char *convert_query_hexchar(char *txt)
|
|
|
|
{
|
|
|
|
int d1, d2;
|
|
|
|
if (strlen(txt) < 3) {
|
|
|
|
*txt = '\0';
|
|
|
|
return txt-1;
|
|
|
|
}
|
|
|
|
d1 = hextoint(*(txt+1));
|
|
|
|
d2 = hextoint(*(txt+2));
|
|
|
|
if (d1<0 || d2<0) {
|
|
|
|
strcpy(txt, txt+3);
|
|
|
|
return txt-1;
|
|
|
|
} else {
|
|
|
|
*txt = d1 * 16 + d2;
|
|
|
|
strcpy(txt+1, txt+3);
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-11 15:11:40 +00:00
|
|
|
int cgit_parse_query(char *txt, configfn fn)
|
|
|
|
{
|
|
|
|
char *t, *value = NULL, c;
|
|
|
|
|
|
|
|
if (!txt)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
t = txt = xstrdup(txt);
|
2007-05-15 21:28:40 +00:00
|
|
|
|
2006-12-11 15:11:40 +00:00
|
|
|
while((c=*t) != '\0') {
|
|
|
|
if (c=='=') {
|
|
|
|
*t = '\0';
|
|
|
|
value = t+1;
|
2006-12-28 01:51:46 +00:00
|
|
|
} else if (c=='+') {
|
|
|
|
*t = ' ';
|
2007-01-04 15:53:03 +00:00
|
|
|
} else if (c=='%') {
|
|
|
|
t = convert_query_hexchar(t);
|
2006-12-11 15:11:40 +00:00
|
|
|
} else if (c=='&') {
|
|
|
|
*t = '\0';
|
|
|
|
(*fn)(txt, value);
|
|
|
|
txt = t+1;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
if (t!=txt)
|
|
|
|
(*fn)(txt, value);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2007-05-18 01:00:54 +00:00
|
|
|
/*
|
|
|
|
* url syntax: [repo ['/' cmd [ '/' path]]]
|
|
|
|
* repo: any valid repo url, may contain '/'
|
|
|
|
* cmd: log | commit | diff | tree | view | blob | snapshot
|
|
|
|
* path: any valid path, may contain '/'
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void cgit_parse_url(const char *url)
|
|
|
|
{
|
|
|
|
char *cmd, *p;
|
|
|
|
|
|
|
|
cgit_repo = NULL;
|
|
|
|
if (!url || url[0] == '\0')
|
|
|
|
return;
|
|
|
|
|
|
|
|
cgit_repo = cgit_get_repoinfo(url);
|
|
|
|
if (cgit_repo) {
|
|
|
|
cgit_query_repo = cgit_repo->url;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = strchr(url, '/');
|
|
|
|
while (!cgit_repo && cmd) {
|
|
|
|
cmd[0] = '\0';
|
|
|
|
cgit_repo = cgit_get_repoinfo(url);
|
|
|
|
if (cgit_repo == NULL) {
|
|
|
|
cmd[0] = '/';
|
|
|
|
cmd = strchr(cmd + 1, '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cgit_query_repo = cgit_repo->url;
|
|
|
|
p = strchr(cmd + 1, '/');
|
|
|
|
if (p) {
|
|
|
|
p[0] = '\0';
|
2007-05-18 11:06:45 +00:00
|
|
|
if (p[1])
|
2007-06-26 16:04:31 +00:00
|
|
|
cgit_query_path = trim_end(p + 1, '/');
|
2007-05-18 01:00:54 +00:00
|
|
|
}
|
|
|
|
cgit_cmd = cgit_get_cmd_index(cmd + 1);
|
|
|
|
cgit_query_page = xstrdup(cmd + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
char *substr(const char *head, const char *tail)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = xmalloc(tail - head + 1);
|
|
|
|
strncpy(buf, head, tail - head);
|
|
|
|
buf[tail - head] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct commitinfo *cgit_parse_commit(struct commit *commit)
|
|
|
|
{
|
|
|
|
struct commitinfo *ret;
|
|
|
|
char *p = commit->buffer, *t = commit->buffer;
|
|
|
|
|
|
|
|
ret = xmalloc(sizeof(*ret));
|
|
|
|
ret->commit = commit;
|
2007-01-16 17:41:01 +00:00
|
|
|
ret->author = NULL;
|
|
|
|
ret->author_email = NULL;
|
|
|
|
ret->committer = NULL;
|
|
|
|
ret->committer_email = NULL;
|
|
|
|
ret->subject = NULL;
|
|
|
|
ret->msg = NULL;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2007-05-26 01:27:49 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return ret;
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
if (strncmp(p, "tree ", 5))
|
|
|
|
die("Bad commit: %s", sha1_to_hex(commit->object.sha1));
|
|
|
|
else
|
|
|
|
p += 46; // "tree " + hex[40] + "\n"
|
|
|
|
|
|
|
|
while (!strncmp(p, "parent ", 7))
|
|
|
|
p += 48; // "parent " + hex[40] + "\n"
|
|
|
|
|
|
|
|
if (!strncmp(p, "author ", 7)) {
|
|
|
|
p += 7;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->author = substr(p, t);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->author_email = substr(p, t);
|
|
|
|
ret->author_date = atol(++t);
|
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(p, "committer ", 9)) {
|
|
|
|
p += 9;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->committer = substr(p, t);
|
2006-12-16 13:25:41 +00:00
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->committer_email = substr(p, t);
|
|
|
|
ret->committer_date = atol(++t);
|
|
|
|
p = strchr(t, '\n') + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
|
|
|
|
t = strchr(p, '\n');
|
2007-05-26 00:19:38 +00:00
|
|
|
if (t) {
|
|
|
|
if (*t == '\0')
|
|
|
|
ret->subject = strdup("** empty **");
|
|
|
|
else
|
|
|
|
ret->subject = substr(p, t);
|
2007-01-16 17:41:01 +00:00
|
|
|
p = t + 1;
|
2006-12-15 17:17:36 +00:00
|
|
|
|
2007-01-16 17:41:01 +00:00
|
|
|
while (*p == '\n')
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
ret->msg = p;
|
2007-05-26 00:19:38 +00:00
|
|
|
} else
|
|
|
|
ret->subject = substr(p, p+strlen(p));
|
|
|
|
|
2006-12-15 17:17:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-01-17 00:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct taginfo *cgit_parse_tag(struct tag *tag)
|
|
|
|
{
|
|
|
|
void *data;
|
2007-05-08 20:40:59 +00:00
|
|
|
enum object_type type;
|
2007-01-17 00:09:51 +00:00
|
|
|
unsigned long size;
|
|
|
|
char *p, *t;
|
|
|
|
struct taginfo *ret;
|
|
|
|
|
2007-05-08 20:40:59 +00:00
|
|
|
data = read_sha1_file(tag->object.sha1, &type, &size);
|
|
|
|
if (!data || type != OBJ_TAG) {
|
2007-01-17 00:09:51 +00:00
|
|
|
free(data);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-05-15 21:28:40 +00:00
|
|
|
|
2007-01-17 00:09:51 +00:00
|
|
|
ret = xmalloc(sizeof(*ret));
|
|
|
|
ret->tagger = NULL;
|
|
|
|
ret->tagger_email = NULL;
|
|
|
|
ret->tagger_date = 0;
|
|
|
|
ret->msg = NULL;
|
|
|
|
|
|
|
|
p = data;
|
|
|
|
|
2007-02-03 15:11:41 +00:00
|
|
|
while (p && *p) {
|
2007-01-17 00:09:51 +00:00
|
|
|
if (*p == '\n')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!strncmp(p, "tagger ", 7)) {
|
|
|
|
p += 7;
|
|
|
|
t = strchr(p, '<') - 1;
|
|
|
|
ret->tagger = substr(p, t);
|
|
|
|
p = t;
|
|
|
|
t = strchr(t, '>') + 1;
|
|
|
|
ret->tagger_email = substr(p, t);
|
|
|
|
ret->tagger_date = atol(++t);
|
|
|
|
}
|
|
|
|
p = strchr(p, '\n') + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (p && (*p == '\n'))
|
|
|
|
p = strchr(p, '\n') + 1;
|
2007-02-03 15:11:41 +00:00
|
|
|
if (p && *p)
|
2007-01-17 00:09:51 +00:00
|
|
|
ret->msg = xstrdup(p);
|
|
|
|
free(data);
|
|
|
|
return ret;
|
|
|
|
}
|