aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile75
-rw-r--r--cache.c21
-rw-r--r--cache.h23
-rw-r--r--cgit.c391
-rw-r--r--cgit.h237
-rw-r--r--cmd.c112
-rw-r--r--cmd.h15
-rw-r--r--configfile.c87
-rw-r--r--configfile.h8
-rw-r--r--html.c95
-rw-r--r--html.h20
-rw-r--r--parsing.c146
-rw-r--r--shared.c257
-rw-r--r--ui-blob.c16
-rw-r--r--ui-blob.h6
-rw-r--r--ui-commit.c26
-rw-r--r--ui-commit.h6
-rw-r--r--ui-diff.c9
-rw-r--r--ui-diff.h7
-rw-r--r--ui-log.c32
-rw-r--r--ui-log.h7
-rw-r--r--ui-patch.c10
-rw-r--r--ui-patch.h6
-rw-r--r--ui-refs.c175
-rw-r--r--ui-refs.h8
-rw-r--r--ui-repolist.c54
-rw-r--r--ui-repolist.h6
-rw-r--r--ui-shared.c237
-rw-r--r--ui-shared.h36
-rw-r--r--ui-snapshot.c123
-rw-r--r--ui-snapshot.h8
-rw-r--r--ui-summary.c189
-rw-r--r--ui-summary.h6
-rw-r--r--ui-tag.c3
-rw-r--r--ui-tag.h6
-rw-r--r--ui-tree.c26
-rw-r--r--ui-tree.h6
38 files changed, 1379 insertions, 1117 deletions
diff --git a/.gitignore b/.gitignore
index aa36ff7..1e016e5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ cgit
3cgit.conf 3cgit.conf
4VERSION 4VERSION
5*.o 5*.o
6*.d
diff --git a/Makefile b/Makefile
index 8bf47f2..931c60e 100644
--- a/Makefile
+++ b/Makefile
@@ -12,13 +12,62 @@ GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2
12# 12#
13-include cgit.conf 13-include cgit.conf
14 14
15#
16# Define a way to invoke make in subdirs quietly, shamelessly ripped
17# from git.git
18#
19QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
20QUIET_SUBDIR1 =
15 21
16EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto 22ifneq ($(findstring $(MAKEFLAGS),w),w)
17OBJECTS = shared.o cache.o parsing.o html.o ui-shared.o ui-repolist.o \ 23PRINT_DIR = --no-print-directory
18 ui-summary.o ui-log.o ui-tree.o ui-commit.o ui-diff.o \ 24else # "make -w"
19 ui-snapshot.o ui-blob.o ui-tag.o ui-refs.o ui-patch.o 25NO_SUBDIR = :
26endif
27
28ifndef V
29 QUIET_CC = @echo ' ' CC $@;
30 QUIET_MM = @echo ' ' MM $@;
31 QUIET_SUBDIR0 = +@subdir=
32 QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \
33 $(MAKE) $(PRINT_DIR) -C $$subdir
34endif
35
36#
37# Define a pattern rule for automatic dependency building
38#
39%.d: %.c
40 $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@
41
42#
43# Define a pattern rule for silent object building
44#
45%.o: %.c
46 $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $<
20 47
21 48
49EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
50OBJECTS =
51OBJECTS += cache.o
52OBJECTS += cgit.o
53OBJECTS += cmd.o
54OBJECTS += configfile.o
55OBJECTS += html.o
56OBJECTS += parsing.o
57OBJECTS += shared.o
58OBJECTS += ui-blob.o
59OBJECTS += ui-commit.o
60OBJECTS += ui-diff.o
61OBJECTS += ui-log.o
62OBJECTS += ui-patch.o
63OBJECTS += ui-refs.o
64OBJECTS += ui-repolist.o
65OBJECTS += ui-shared.o
66OBJECTS += ui-snapshot.o
67OBJECTS += ui-summary.o
68OBJECTS += ui-tag.o
69OBJECTS += ui-tree.o
70
22ifdef NEEDS_LIBICONV 71ifdef NEEDS_LIBICONV
23 EXTLIBS += -liconv 72 EXTLIBS += -liconv
24endif 73endif
@@ -41,21 +90,25 @@ CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
41CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' 90CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
42 91
43 92
44cgit: cgit.c $(OBJECTS) 93cgit: $(OBJECTS)
45 $(CC) $(CFLAGS) cgit.c -o cgit $(OBJECTS) $(EXTLIBS) 94 $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
95
96$(OBJECTS): git/xdiff/lib.a git/libgit.a
97
98cgit.o: VERSION
46 99
47$(OBJECTS): cgit.h git/xdiff/lib.a git/libgit.a VERSION 100-include $(OBJECTS:.o=.d)
48 101
49git/xdiff/lib.a: | git 102git/xdiff/lib.a: | git
50 103
51git/libgit.a: | git 104git/libgit.a: | git
52 105
53git: 106git:
54 cd git && $(MAKE) xdiff/lib.a 107 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) xdiff/lib.a
55 cd git && $(MAKE) libgit.a 108 $(QUIET_SUBDIR0)git $(QUIET_SUBDIR1) libgit.a
56 109
57test: all 110test: all
58 $(MAKE) -C tests 111 $(QUIET_SUBDIR0)tests $(QUIET_SUBDIR1) all
59 112
60install: all 113install: all
61 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH) 114 mkdir -p $(DESTDIR)$(CGIT_SCRIPT_PATH)
@@ -69,7 +122,7 @@ uninstall:
69 rm -f $(CGIT_SCRIPT_PATH)/cgit.png 122 rm -f $(CGIT_SCRIPT_PATH)/cgit.png
70 123
71clean: 124clean:
72 rm -f cgit VERSION *.o 125 rm -f cgit VERSION *.o *.d
73 cd git && $(MAKE) clean 126 cd git && $(MAKE) clean
74 127
75distclean: clean 128distclean: clean
diff --git a/cache.c b/cache.c
index 372e38d..89f7ecd 100644
--- a/cache.c
+++ b/cache.c
@@ -7,6 +7,7 @@
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cache.h"
10 11
11const int NOLOCK = -1; 12const int NOLOCK = -1;
12 13
@@ -44,23 +45,23 @@ int cache_create_dirs()
44{ 45{
45 char *path; 46 char *path;
46 47
47 path = fmt("%s", cgit_cache_root); 48 path = fmt("%s", ctx.cfg.cache_root);
48 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 49 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
49 return 0; 50 return 0;
50 51
51 if (!cgit_repo) 52 if (!ctx.repo)
52 return 0; 53 return 0;
53 54
54 path = fmt("%s/%s", cgit_cache_root, 55 path = fmt("%s/%s", ctx.cfg.cache_root,
55 cache_safe_filename(cgit_repo->url)); 56 cache_safe_filename(ctx.repo->url));
56 57
57 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 58 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
58 return 0; 59 return 0;
59 60
60 if (cgit_query_page) { 61 if (ctx.qry.page) {
61 path = fmt("%s/%s/%s", cgit_cache_root, 62 path = fmt("%s/%s/%s", ctx.cfg.cache_root,
62 cache_safe_filename(cgit_repo->url), 63 cache_safe_filename(ctx.repo->url),
63 cgit_query_page); 64 ctx.qry.page);
64 if (mkdir(path, S_IRWXU) && errno!=EEXIST) 65 if (mkdir(path, S_IRWXU) && errno!=EEXIST)
65 return 0; 66 return 0;
66 } 67 }
@@ -74,7 +75,7 @@ int cache_refill_overdue(const char *lockfile)
74 if (stat(lockfile, &st)) 75 if (stat(lockfile, &st))
75 return 0; 76 return 0;
76 else 77 else
77 return (time(NULL) - st.st_mtime > cgit_cache_max_create_time); 78 return (time(NULL) - st.st_mtime > ctx.cfg.cache_max_create_time);
78} 79}
79 80
80int cache_lock(struct cacheitem *item) 81int cache_lock(struct cacheitem *item)
@@ -83,7 +84,7 @@ int cache_lock(struct cacheitem *item)
83 char *lockfile = xstrdup(fmt("%s.lock", item->name)); 84 char *lockfile = xstrdup(fmt("%s.lock", item->name));
84 85
85 top: 86 top:
86 if (++i > cgit_max_lock_attempts) 87 if (++i > ctx.cfg.max_lock_attempts)
87 die("cache_lock: unable to lock %s: %s", 88 die("cache_lock: unable to lock %s: %s",
88 item->name, strerror(errno)); 89 item->name, strerror(errno));
89 90
diff --git a/cache.h b/cache.h
new file mode 100644
index 0000000..4dcbea3
--- /dev/null
+++ b/cache.h
@@ -0,0 +1,23 @@
1/*
2 * Since git has it's own cache.h which we include,
3 * lets test on CGIT_CACHE_H to avoid confusion
4 */
5
6#ifndef CGIT_CACHE_H
7#define CGIT_CACHE_H
8
9struct cacheitem {
10 char *name;
11 struct stat st;
12 int ttl;
13 int fd;
14};
15
16extern char *cache_safe_filename(const char *unsafe);
17extern int cache_lock(struct cacheitem *item);
18extern int cache_unlock(struct cacheitem *item);
19extern int cache_cancel_lock(struct cacheitem *item);
20extern int cache_exist(struct cacheitem *item);
21extern int cache_expired(struct cacheitem *item);
22
23#endif /* CGIT_CACHE_H */
diff --git a/cgit.c b/cgit.c
index e8acc03..6ec763f 100644
--- a/cgit.c
+++ b/cgit.c
@@ -7,40 +7,199 @@
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cache.h"
11#include "cmd.h"
12#include "configfile.h"
13#include "html.h"
14#include "ui-shared.h"
15
16const char *cgit_version = CGIT_VERSION;
17
18void config_cb(const char *name, const char *value)
19{
20 if (!strcmp(name, "root-title"))
21 ctx.cfg.root_title = xstrdup(value);
22 else if (!strcmp(name, "css"))
23 ctx.cfg.css = xstrdup(value);
24 else if (!strcmp(name, "logo"))
25 ctx.cfg.logo = xstrdup(value);
26 else if (!strcmp(name, "index-header"))
27 ctx.cfg.index_header = xstrdup(value);
28 else if (!strcmp(name, "index-info"))
29 ctx.cfg.index_info = xstrdup(value);
30 else if (!strcmp(name, "logo-link"))
31 ctx.cfg.logo_link = xstrdup(value);
32 else if (!strcmp(name, "module-link"))
33 ctx.cfg.module_link = xstrdup(value);
34 else if (!strcmp(name, "virtual-root")) {
35 ctx.cfg.virtual_root = trim_end(value, '/');
36 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
37 ctx.cfg.virtual_root = "";
38 } else if (!strcmp(name, "nocache"))
39 ctx.cfg.nocache = atoi(value);
40 else if (!strcmp(name, "snapshots"))
41 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
42 else if (!strcmp(name, "enable-index-links"))
43 ctx.cfg.enable_index_links = atoi(value);
44 else if (!strcmp(name, "enable-log-filecount"))
45 ctx.cfg.enable_log_filecount = atoi(value);
46 else if (!strcmp(name, "enable-log-linecount"))
47 ctx.cfg.enable_log_linecount = atoi(value);
48 else if (!strcmp(name, "cache-root"))
49 ctx.cfg.cache_root = xstrdup(value);
50 else if (!strcmp(name, "cache-root-ttl"))
51 ctx.cfg.cache_root_ttl = atoi(value);
52 else if (!strcmp(name, "cache-repo-ttl"))
53 ctx.cfg.cache_repo_ttl = atoi(value);
54 else if (!strcmp(name, "cache-static-ttl"))
55 ctx.cfg.cache_static_ttl = atoi(value);
56 else if (!strcmp(name, "cache-dynamic-ttl"))
57 ctx.cfg.cache_dynamic_ttl = atoi(value);
58 else if (!strcmp(name, "max-message-length"))
59 ctx.cfg.max_msg_len = atoi(value);
60 else if (!strcmp(name, "max-repodesc-length"))
61 ctx.cfg.max_repodesc_len = atoi(value);
62 else if (!strcmp(name, "max-commit-count"))
63 ctx.cfg.max_commit_count = atoi(value);
64 else if (!strcmp(name, "summary-log"))
65 ctx.cfg.summary_log = atoi(value);
66 else if (!strcmp(name, "summary-branches"))
67 ctx.cfg.summary_branches = atoi(value);
68 else if (!strcmp(name, "summary-tags"))
69 ctx.cfg.summary_tags = atoi(value);
70 else if (!strcmp(name, "agefile"))
71 ctx.cfg.agefile = xstrdup(value);
72 else if (!strcmp(name, "renamelimit"))
73 ctx.cfg.renamelimit = atoi(value);
74 else if (!strcmp(name, "robots"))
75 ctx.cfg.robots = xstrdup(value);
76 else if (!strcmp(name, "clone-prefix"))
77 ctx.cfg.clone_prefix = xstrdup(value);
78 else if (!strcmp(name, "repo.group"))
79 ctx.cfg.repo_group = xstrdup(value);
80 else if (!strcmp(name, "repo.url"))
81 ctx.repo = cgit_add_repo(value);
82 else if (!strcmp(name, "repo.name"))
83 ctx.repo->name = xstrdup(value);
84 else if (ctx.repo && !strcmp(name, "repo.path"))
85 ctx.repo->path = trim_end(value, '/');
86 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
87 ctx.repo->clone_url = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.desc"))
89 ctx.repo->desc = xstrdup(value);
90 else if (ctx.repo && !strcmp(name, "repo.owner"))
91 ctx.repo->owner = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
93 ctx.repo->defbranch = xstrdup(value);
94 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
95 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
96 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
97 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
98 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
99 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
100 else if (ctx.repo && !strcmp(name, "repo.module-link"))
101 ctx.repo->module_link= xstrdup(value);
102 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
103 if (*value == '/')
104 ctx.repo->readme = xstrdup(value);
105 else
106 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
107 } else if (!strcmp(name, "include"))
108 parse_configfile(value, config_cb);
109}
110
111static void querystring_cb(const char *name, const char *value)
112{
113 if (!strcmp(name,"r")) {
114 ctx.qry.repo = xstrdup(value);
115 ctx.repo = cgit_get_repoinfo(value);
116 } else if (!strcmp(name, "p")) {
117 ctx.qry.page = xstrdup(value);
118 } else if (!strcmp(name, "url")) {
119 cgit_parse_url(value);
120 } else if (!strcmp(name, "qt")) {
121 ctx.qry.grep = xstrdup(value);
122 } else if (!strcmp(name, "q")) {
123 ctx.qry.search = xstrdup(value);
124 } else if (!strcmp(name, "h")) {
125 ctx.qry.head = xstrdup(value);
126 ctx.qry.has_symref = 1;
127 } else if (!strcmp(name, "id")) {
128 ctx.qry.sha1 = xstrdup(value);
129 ctx.qry.has_sha1 = 1;
130 } else if (!strcmp(name, "id2")) {
131 ctx.qry.sha2 = xstrdup(value);
132 ctx.qry.has_sha1 = 1;
133 } else if (!strcmp(name, "ofs")) {
134 ctx.qry.ofs = atoi(value);
135 } else if (!strcmp(name, "path")) {
136 ctx.qry.path = trim_end(value, '/');
137 } else if (!strcmp(name, "name")) {
138 ctx.qry.name = xstrdup(value);
139 }
140}
141
142static void prepare_context(struct cgit_context *ctx)
143{
144 memset(ctx, 0, sizeof(ctx));
145 ctx->cfg.agefile = "info/web/last-modified";
146 ctx->cfg.cache_dynamic_ttl = 5;
147 ctx->cfg.cache_max_create_time = 5;
148 ctx->cfg.cache_repo_ttl = 5;
149 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
150 ctx->cfg.cache_root_ttl = 5;
151 ctx->cfg.cache_static_ttl = -1;
152 ctx->cfg.css = "/cgit.css";
153 ctx->cfg.logo = "/git-logo.png";
154 ctx->cfg.max_commit_count = 50;
155 ctx->cfg.max_lock_attempts = 5;
156 ctx->cfg.max_msg_len = 60;
157 ctx->cfg.max_repodesc_len = 60;
158 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
159 ctx->cfg.renamelimit = -1;
160 ctx->cfg.robots = "index, nofollow";
161 ctx->cfg.root_title = "Git repository browser";
162 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
163 ctx->page.mimetype = "text/html";
164 ctx->page.charset = PAGE_ENCODING;
165 ctx->page.filename = NULL;
166}
10 167
11static int cgit_prepare_cache(struct cacheitem *item) 168static int cgit_prepare_cache(struct cacheitem *item)
12{ 169{
13 if (!cgit_repo && cgit_query_repo) { 170 if (!ctx.repo && ctx.qry.repo) {
14 char *title = fmt("%s - %s", cgit_root_title, "Bad request"); 171 ctx.page.title = fmt("%s - %s", ctx.cfg.root_title,
15 cgit_print_docstart(title, item); 172 "Bad request");
16 cgit_print_pageheader(title, 0); 173 cgit_print_http_headers(&ctx);
17 cgit_print_error(fmt("Unknown repo: %s", cgit_query_repo)); 174 cgit_print_docstart(&ctx);
175 cgit_print_pageheader(&ctx);
176 cgit_print_error(fmt("Unknown repo: %s", ctx.qry.repo));
18 cgit_print_docend(); 177 cgit_print_docend();
19 return 0; 178 return 0;
20 } 179 }
21 180
22 if (!cgit_repo) { 181 if (!ctx.repo) {
23 item->name = xstrdup(fmt("%s/index.html", cgit_cache_root)); 182 item->name = xstrdup(fmt("%s/index.html", ctx.cfg.cache_root));
24 item->ttl = cgit_cache_root_ttl; 183 item->ttl = ctx.cfg.cache_root_ttl;
25 return 1; 184 return 1;
26 } 185 }
27 186
28 if (!cgit_cmd) { 187 if (!ctx.qry.page) {
29 item->name = xstrdup(fmt("%s/%s/index.%s.html", cgit_cache_root, 188 item->name = xstrdup(fmt("%s/%s/index.%s.html", ctx.cfg.cache_root,
30 cache_safe_filename(cgit_repo->url), 189 cache_safe_filename(ctx.repo->url),
31 cache_safe_filename(cgit_querystring))); 190 cache_safe_filename(ctx.qry.raw)));
32 item->ttl = cgit_cache_repo_ttl; 191 item->ttl = ctx.cfg.cache_repo_ttl;
33 } else { 192 } else {
34 item->name = xstrdup(fmt("%s/%s/%s/%s.html", cgit_cache_root, 193 item->name = xstrdup(fmt("%s/%s/%s/%s.html", ctx.cfg.cache_root,
35 cache_safe_filename(cgit_repo->url), 194 cache_safe_filename(ctx.repo->url),
36 cgit_query_page, 195 ctx.qry.page,
37 cache_safe_filename(cgit_querystring))); 196 cache_safe_filename(ctx.qry.raw)));
38 if (cgit_query_has_symref) 197 if (ctx.qry.has_symref)
39 item->ttl = cgit_cache_dynamic_ttl; 198 item->ttl = ctx.cfg.cache_dynamic_ttl;
40 else if (cgit_query_has_sha1) 199 else if (ctx.qry.has_sha1)
41 item->ttl = cgit_cache_static_ttl; 200 item->ttl = ctx.cfg.cache_static_ttl;
42 else 201 else
43 item->ttl = cgit_cache_repo_ttl; 202 item->ttl = ctx.cfg.cache_repo_ttl;
44 } 203 }
45 return 1; 204 return 1;
46} 205}
@@ -64,7 +223,7 @@ int find_current_ref(const char *refname, const unsigned char *sha1,
64 return info->match; 223 return info->match;
65} 224}
66 225
67char *find_default_branch(struct repoinfo *repo) 226char *find_default_branch(struct cgit_repo *repo)
68{ 227{
69 struct refmatch info; 228 struct refmatch info;
70 229
@@ -78,113 +237,98 @@ char *find_default_branch(struct repoinfo *repo)
78 return info.first_ref; 237 return info.first_ref;
79} 238}
80 239
81static void cgit_print_repo_page(struct cacheitem *item) 240static int prepare_repo_cmd(struct cgit_context *ctx)
82{ 241{
83 char *title, *tmp; 242 char *tmp;
84 int show_search;
85 unsigned char sha1[20]; 243 unsigned char sha1[20];
86 244 int nongit = 0;
87 if (chdir(cgit_repo->path)) { 245
88 title = fmt("%s - %s", cgit_root_title, "Bad request"); 246 setenv("GIT_DIR", ctx->repo->path, 1);
89 cgit_print_docstart(title, item); 247 setup_git_directory_gently(&nongit);
90 cgit_print_pageheader(title, 0); 248 if (nongit) {
91 cgit_print_error(fmt("Unable to scan repository: %s", 249 ctx->page.title = fmt("%s - %s", ctx->cfg.root_title,
92 strerror(errno))); 250 "config error");
251 tmp = fmt("Not a git repository: '%s'", ctx->repo->path);
252 ctx->repo = NULL;
253 cgit_print_http_headers(ctx);
254 cgit_print_docstart(ctx);
255 cgit_print_pageheader(ctx);
256 cgit_print_error(tmp);
93 cgit_print_docend(); 257 cgit_print_docend();
94 return; 258 return 1;
95 } 259 }
260 ctx->page.title = fmt("%s - %s", ctx->repo->name, ctx->repo->desc);
96 261
97 title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); 262 if (!ctx->qry.head) {
98 show_search = 0; 263 ctx->qry.head = xstrdup(find_default_branch(ctx->repo));
99 setenv("GIT_DIR", cgit_repo->path, 1); 264 ctx->repo->defbranch = ctx->qry.head;
100
101 if (!cgit_query_head) {
102 cgit_query_head = xstrdup(find_default_branch(cgit_repo));
103 cgit_repo->defbranch = cgit_query_head;
104 } 265 }
105 266
106 if (!cgit_query_head) { 267 if (!ctx->qry.head) {
107 cgit_print_docstart(title, item); 268 cgit_print_http_headers(ctx);
108 cgit_print_pageheader(title, 0); 269 cgit_print_docstart(ctx);
270 cgit_print_pageheader(ctx);
109 cgit_print_error("Repository seems to be empty"); 271 cgit_print_error("Repository seems to be empty");
110 cgit_print_docend(); 272 cgit_print_docend();
111 return; 273 return 1;
112 } 274 }
113 275
114 if (get_sha1(cgit_query_head, sha1)) { 276 if (get_sha1(ctx->qry.head, sha1)) {
115 tmp = xstrdup(cgit_query_head); 277 tmp = xstrdup(ctx->qry.head);
116 cgit_query_head = cgit_repo->defbranch; 278 ctx->qry.head = ctx->repo->defbranch;
117 cgit_print_docstart(title, item); 279 cgit_print_http_headers(ctx);
118 cgit_print_pageheader(title, 0); 280 cgit_print_docstart(ctx);
281 cgit_print_pageheader(ctx);
119 cgit_print_error(fmt("Invalid branch: %s", tmp)); 282 cgit_print_error(fmt("Invalid branch: %s", tmp));
120 cgit_print_docend(); 283 cgit_print_docend();
121 return; 284 return 1;
122 } 285 }
286 return 0;
287}
123 288
124 if ((cgit_cmd == CMD_SNAPSHOT) && cgit_repo->snapshots) { 289static void process_request(struct cgit_context *ctx)
125 cgit_print_snapshot(item, cgit_query_head, cgit_query_sha1, 290{
126 cgit_repobasename(cgit_repo->url), 291 struct cgit_cmd *cmd;
127 cgit_query_path, 292
128 cgit_repo->snapshots ); 293 cmd = cgit_get_cmd(ctx);
294 if (!cmd) {
295 ctx->page.title = "cgit error";
296 ctx->repo = NULL;
297 cgit_print_http_headers(ctx);
298 cgit_print_docstart(ctx);
299 cgit_print_pageheader(ctx);
300 cgit_print_error("Invalid request");
301 cgit_print_docend();
129 return; 302 return;
130 } 303 }
131 304
132 if (cgit_cmd == CMD_PATCH) { 305 if (cmd->want_repo && prepare_repo_cmd(ctx))
133 cgit_print_patch(cgit_query_sha1, item);
134 return; 306 return;
135 }
136 307
137 if (cgit_cmd == CMD_BLOB) { 308 if (cmd->want_layout) {
138 cgit_print_blob(item, cgit_query_sha1, cgit_query_path); 309 cgit_print_http_headers(ctx);
139 return; 310 cgit_print_docstart(ctx);
311 cgit_print_pageheader(ctx);
140 } 312 }
141 313
142 show_search = (cgit_cmd == CMD_LOG); 314 cmd->fn(ctx);
143 cgit_print_docstart(title, item); 315
144 if (!cgit_cmd) { 316 if (cmd->want_layout)
145 cgit_print_pageheader("summary", show_search);
146 cgit_print_summary();
147 cgit_print_docend(); 317 cgit_print_docend();
148 return; 318}
149 }
150 319
151 cgit_print_pageheader(cgit_query_page, show_search); 320static long ttl_seconds(long ttl)
152 321{
153 switch(cgit_cmd) { 322 if (ttl<0)
154 case CMD_LOG: 323 return 60 * 60 * 24 * 365;
155 cgit_print_log(cgit_query_sha1, cgit_query_ofs, 324 else
156 cgit_max_commit_count, cgit_query_grep, cgit_query_search, 325 return ttl * 60;
157 cgit_query_path, 1);
158 break;
159 case CMD_TREE:
160 cgit_print_tree(cgit_query_sha1, cgit_query_path);
161 break;
162 case CMD_COMMIT:
163 cgit_print_commit(cgit_query_sha1);
164 break;
165 case CMD_REFS:
166 cgit_print_refs();
167 break;
168 case CMD_TAG:
169 cgit_print_tag(cgit_query_sha1);
170 break;
171 case CMD_DIFF:
172 cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path);
173 break;
174 default:
175 cgit_print_error("Invalid request");
176 }
177 cgit_print_docend();
178} 326}
179 327
180static void cgit_fill_cache(struct cacheitem *item, int use_cache) 328static void cgit_fill_cache(struct cacheitem *item, int use_cache)
181{ 329{
182 static char buf[PATH_MAX];
183 int stdout2; 330 int stdout2;
184 331
185 getcwd(buf, sizeof(buf));
186 item->st.st_mtime = time(NULL);
187
188 if (use_cache) { 332 if (use_cache) {
189 stdout2 = chk_positive(dup(STDOUT_FILENO), 333 stdout2 = chk_positive(dup(STDOUT_FILENO),
190 "Preserving STDOUT"); 334 "Preserving STDOUT");
@@ -192,10 +336,9 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
192 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); 336 chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)");
193 } 337 }
194 338
195 if (cgit_repo) 339 ctx.page.modified = time(NULL);
196 cgit_print_repo_page(item); 340 ctx.page.expires = ctx.page.modified + ttl_seconds(item->ttl);
197 else 341 process_request(&ctx);
198 cgit_print_repolist(item);
199 342
200 if (use_cache) { 343 if (use_cache) {
201 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); 344 chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT");
@@ -203,8 +346,6 @@ static void cgit_fill_cache(struct cacheitem *item, int use_cache)
203 "Restoring original STDOUT"); 346 "Restoring original STDOUT");
204 chk_zero(close(stdout2), "Closing temporary STDOUT"); 347 chk_zero(close(stdout2), "Closing temporary STDOUT");
205 } 348 }
206
207 chdir(buf);
208} 349}
209 350
210static void cgit_check_cache(struct cacheitem *item) 351static void cgit_check_cache(struct cacheitem *item)
@@ -212,7 +353,7 @@ static void cgit_check_cache(struct cacheitem *item)
212 int i = 0; 353 int i = 0;
213 354
214 top: 355 top:
215 if (++i > cgit_max_lock_attempts) { 356 if (++i > ctx.cfg.max_lock_attempts) {
216 die("cgit_refresh_cache: unable to lock %s: %s", 357 die("cgit_refresh_cache: unable to lock %s: %s",
217 item->name, strerror(errno)); 358 item->name, strerror(errno));
218 } 359 }
@@ -258,30 +399,30 @@ static void cgit_parse_args(int argc, const char **argv)
258 399
259 for (i = 1; i < argc; i++) { 400 for (i = 1; i < argc; i++) {
260 if (!strncmp(argv[i], "--cache=", 8)) { 401 if (!strncmp(argv[i], "--cache=", 8)) {
261 cgit_cache_root = xstrdup(argv[i]+8); 402 ctx.cfg.cache_root = xstrdup(argv[i]+8);
262 } 403 }
263 if (!strcmp(argv[i], "--nocache")) { 404 if (!strcmp(argv[i], "--nocache")) {
264 cgit_nocache = 1; 405 ctx.cfg.nocache = 1;
265 } 406 }
266 if (!strncmp(argv[i], "--query=", 8)) { 407 if (!strncmp(argv[i], "--query=", 8)) {
267 cgit_querystring = xstrdup(argv[i]+8); 408 ctx.qry.raw = xstrdup(argv[i]+8);
268 } 409 }
269 if (!strncmp(argv[i], "--repo=", 7)) { 410 if (!strncmp(argv[i], "--repo=", 7)) {
270 cgit_query_repo = xstrdup(argv[i]+7); 411 ctx.qry.repo = xstrdup(argv[i]+7);
271 } 412 }
272 if (!strncmp(argv[i], "--page=", 7)) { 413 if (!strncmp(argv[i], "--page=", 7)) {
273 cgit_query_page = xstrdup(argv[i]+7); 414 ctx.qry.page = xstrdup(argv[i]+7);
274 } 415 }
275 if (!strncmp(argv[i], "--head=", 7)) { 416 if (!strncmp(argv[i], "--head=", 7)) {
276 cgit_query_head = xstrdup(argv[i]+7); 417 ctx.qry.head = xstrdup(argv[i]+7);
277 cgit_query_has_symref = 1; 418 ctx.qry.has_symref = 1;
278 } 419 }
279 if (!strncmp(argv[i], "--sha1=", 7)) { 420 if (!strncmp(argv[i], "--sha1=", 7)) {
280 cgit_query_sha1 = xstrdup(argv[i]+7); 421 ctx.qry.sha1 = xstrdup(argv[i]+7);
281 cgit_query_has_sha1 = 1; 422 ctx.qry.has_sha1 = 1;
282 } 423 }
283 if (!strncmp(argv[i], "--ofs=", 6)) { 424 if (!strncmp(argv[i], "--ofs=", 6)) {
284 cgit_query_ofs = atoi(argv[i]+6); 425 ctx.qry.ofs = atoi(argv[i]+6);
285 } 426 }
286 } 427 }
287} 428}
@@ -291,24 +432,24 @@ int main(int argc, const char **argv)
291 struct cacheitem item; 432 struct cacheitem item;
292 const char *cgit_config_env = getenv("CGIT_CONFIG"); 433 const char *cgit_config_env = getenv("CGIT_CONFIG");
293 434
294 htmlfd = STDOUT_FILENO; 435 prepare_context(&ctx);
295 item.st.st_mtime = time(NULL); 436 item.st.st_mtime = time(NULL);
296 cgit_repolist.length = 0; 437 cgit_repolist.length = 0;
297 cgit_repolist.count = 0; 438 cgit_repolist.count = 0;
298 cgit_repolist.repos = NULL; 439 cgit_repolist.repos = NULL;
299 440
300 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 441 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
301 cgit_global_config_cb); 442 config_cb);
302 cgit_repo = NULL; 443 ctx.repo = NULL;
303 if (getenv("SCRIPT_NAME")) 444 if (getenv("SCRIPT_NAME"))
304 cgit_script_name = xstrdup(getenv("SCRIPT_NAME")); 445 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
305 if (getenv("QUERY_STRING")) 446 if (getenv("QUERY_STRING"))
306 cgit_querystring = xstrdup(getenv("QUERY_STRING")); 447 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
307 cgit_parse_args(argc, argv); 448 cgit_parse_args(argc, argv);
308 cgit_parse_query(cgit_querystring, cgit_querystring_cb); 449 http_parse_querystring(ctx.qry.raw, querystring_cb);
309 if (!cgit_prepare_cache(&item)) 450 if (!cgit_prepare_cache(&item))
310 return 0; 451 return 0;
311 if (cgit_nocache) { 452 if (ctx.cfg.nocache) {
312 cgit_fill_cache(&item, 0); 453 cgit_fill_cache(&item, 0);
313 } else { 454 } else {
314 cgit_check_cache(&item); 455 cgit_check_cache(&item);
diff --git a/cgit.h b/cgit.h
index 66c40b9..ee8c716 100644
--- a/cgit.h
+++ b/cgit.h
@@ -20,19 +20,6 @@
20 20
21 21
22/* 22/*
23 * The valid cgit repo-commands
24 */
25#define CMD_LOG 1
26#define CMD_COMMIT 2
27#define CMD_DIFF 3
28#define CMD_TREE 4
29#define CMD_BLOB 5
30#define CMD_SNAPSHOT 6
31#define CMD_TAG 7
32#define CMD_REFS 8
33#define CMD_PATCH 9
34
35/*
36 * Dateformats used on misc. pages 23 * Dateformats used on misc. pages
37 */ 24 */
38#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S" 25#define FMT_LONGDATE "%Y-%m-%d %H:%M:%S"
@@ -59,14 +46,7 @@ typedef void (*configfn)(const char *name, const char *value);
59typedef void (*filepair_fn)(struct diff_filepair *pair); 46typedef void (*filepair_fn)(struct diff_filepair *pair);
60typedef void (*linediff_fn)(char *line, int len); 47typedef void (*linediff_fn)(char *line, int len);
61 48
62struct cacheitem { 49struct cgit_repo {
63 char *name;
64 struct stat st;
65 int ttl;
66 int fd;
67};
68
69struct repoinfo {
70 char *url; 50 char *url;
71 char *name; 51 char *name;
72 char *path; 52 char *path;
@@ -82,10 +62,10 @@ struct repoinfo {
82 int enable_log_linecount; 62 int enable_log_linecount;
83}; 63};
84 64
85struct repolist { 65struct cgit_repolist {
86 int length; 66 int length;
87 int count; 67 int count;
88 struct repoinfo *repos; 68 struct cgit_repo *repos;
89}; 69};
90 70
91struct commitinfo { 71struct commitinfo {
@@ -123,74 +103,94 @@ struct reflist {
123 int count; 103 int count;
124}; 104};
125 105
106struct cgit_query {
107 int has_symref;
108 int has_sha1;
109 char *raw;
110 char *repo;
111 char *page;
112 char *search;
113 char *grep;
114 char *head;
115 char *sha1;
116 char *sha2;
117 char *path;
118 char *name;
119 int ofs;
120};
121
122struct cgit_config {
123 char *agefile;
124 char *cache_root;
125 char *clone_prefix;
126 char *css;
127 char *index_header;
128 char *index_info;
129 char *logo;
130 char *logo_link;
131 char *module_link;
132 char *repo_group;
133 char *robots;
134 char *root_title;
135 char *script_name;
136 char *virtual_root;
137 int cache_dynamic_ttl;
138 int cache_max_create_time;
139 int cache_repo_ttl;
140 int cache_root_ttl;
141 int cache_static_ttl;
142 int enable_index_links;
143 int enable_log_filecount;
144 int enable_log_linecount;
145 int max_commit_count;
146 int max_lock_attempts;
147 int max_msg_len;
148 int max_repodesc_len;
149 int nocache;
150 int renamelimit;
151 int snapshots;
152 int summary_branches;
153 int summary_log;
154 int summary_tags;
155};
156
157struct cgit_page {
158 time_t modified;
159 time_t expires;
160 char *mimetype;
161 char *charset;
162 char *filename;
163 char *title;
164};
165
166struct cgit_context {
167 struct cgit_query qry;
168 struct cgit_config cfg;
169 struct cgit_repo *repo;
170 struct cgit_page page;
171};
172
173struct cgit_snapshot_format {
174 const char *suffix;
175 const char *mimetype;
176 write_archive_fn_t write_func;
177 int bit;
178};
179
126extern const char *cgit_version; 180extern const char *cgit_version;
127 181
128extern struct repolist cgit_repolist; 182extern struct cgit_repolist cgit_repolist;
129extern struct repoinfo *cgit_repo; 183extern struct cgit_context ctx;
130extern int cgit_cmd; 184extern const struct cgit_snapshot_format cgit_snapshot_formats[];
131 185
132extern char *cgit_root_title; 186extern struct cgit_repo *cgit_add_repo(const char *url);
133extern char *cgit_css; 187extern struct cgit_repo *cgit_get_repoinfo(const char *url);
134extern char *cgit_logo;
135extern char *cgit_index_header;
136extern char *cgit_index_info;
137extern char *cgit_logo_link;
138extern char *cgit_module_link;
139extern char *cgit_agefile;
140extern char *cgit_virtual_root;
141extern char *cgit_script_name;
142extern char *cgit_cache_root;
143extern char *cgit_repo_group;
144extern char *cgit_robots;
145extern char *cgit_clone_prefix;
146
147extern int cgit_nocache;
148extern int cgit_snapshots;
149extern int cgit_enable_index_links;
150extern int cgit_enable_log_filecount;
151extern int cgit_enable_log_linecount;
152extern int cgit_max_lock_attempts;
153extern int cgit_cache_root_ttl;
154extern int cgit_cache_repo_ttl;
155extern int cgit_cache_dynamic_ttl;
156extern int cgit_cache_static_ttl;
157extern int cgit_cache_max_create_time;
158extern int cgit_summary_log;
159extern int cgit_summary_tags;
160extern int cgit_summary_branches;
161
162extern int cgit_max_msg_len;
163extern int cgit_max_repodesc_len;
164extern int cgit_max_commit_count;
165
166extern int cgit_query_has_symref;
167extern int cgit_query_has_sha1;
168
169extern char *cgit_querystring;
170extern char *cgit_query_repo;
171extern char *cgit_query_page;
172extern char *cgit_query_search;
173extern char *cgit_query_grep;
174extern char *cgit_query_head;
175extern char *cgit_query_sha1;
176extern char *cgit_query_sha2;
177extern char *cgit_query_path;
178extern char *cgit_query_name;
179extern int cgit_query_ofs;
180
181extern int htmlfd;
182
183extern int cgit_get_cmd_index(const char *cmd);
184extern struct repoinfo *cgit_get_repoinfo(const char *url);
185extern void cgit_global_config_cb(const char *name, const char *value);
186extern void cgit_repo_config_cb(const char *name, const char *value); 188extern void cgit_repo_config_cb(const char *name, const char *value);
187extern void cgit_querystring_cb(const char *name, const char *value);
188 189
189extern int chk_zero(int result, char *msg); 190extern int chk_zero(int result, char *msg);
190extern int chk_positive(int result, char *msg); 191extern int chk_positive(int result, char *msg);
191extern int chk_non_negative(int result, char *msg); 192extern int chk_non_negative(int result, char *msg);
192 193
193extern int hextoint(char c);
194extern char *trim_end(const char *str, char c); 194extern char *trim_end(const char *str, char c);
195extern char *strlpart(char *txt, int maxlen); 195extern char *strlpart(char *txt, int maxlen);
196extern char *strrpart(char *txt, int maxlen); 196extern char *strrpart(char *txt, int maxlen);
@@ -213,83 +213,12 @@ extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
213 213
214extern char *fmt(const char *format,...); 214extern char *fmt(const char *format,...);
215 215
216extern void html(const char *txt);
217extern void htmlf(const char *format,...);
218extern void html_txt(char *txt);
219extern void html_ntxt(int len, char *txt);
220extern void html_attr(char *txt);
221extern void html_hidden(char *name, char *value);
222extern void html_option(char *value, char *text, char *selected_value);
223extern void html_link_open(char *url, char *title, char *class);
224extern void html_link_close(void);
225extern void html_filemode(unsigned short mode);
226extern int html_include(const char *filename);
227
228extern int cgit_read_config(const char *filename, configfn fn);
229extern int cgit_parse_query(char *txt, configfn fn);
230extern struct commitinfo *cgit_parse_commit(struct commit *commit); 216extern struct commitinfo *cgit_parse_commit(struct commit *commit);
231extern struct taginfo *cgit_parse_tag(struct tag *tag); 217extern struct taginfo *cgit_parse_tag(struct tag *tag);
232extern void cgit_parse_url(const char *url); 218extern void cgit_parse_url(const char *url);
233 219
234extern char *cache_safe_filename(const char *unsafe);
235extern int cache_lock(struct cacheitem *item);
236extern int cache_unlock(struct cacheitem *item);
237extern int cache_cancel_lock(struct cacheitem *item);
238extern int cache_exist(struct cacheitem *item);
239extern int cache_expired(struct cacheitem *item);
240
241extern char *cgit_repourl(const char *reponame);
242extern char *cgit_fileurl(const char *reponame, const char *pagename,
243 const char *filename, const char *query);
244extern char *cgit_pageurl(const char *reponame, const char *pagename,
245 const char *query);
246
247extern const char *cgit_repobasename(const char *reponame); 220extern const char *cgit_repobasename(const char *reponame);
248 221
249extern void cgit_tree_link(char *name, char *title, char *class, char *head,
250 char *rev, char *path);
251extern void cgit_log_link(char *name, char *title, char *class, char *head,
252 char *rev, char *path, int ofs, char *grep,
253 char *pattern);
254extern void cgit_commit_link(char *name, char *title, char *class, char *head,
255 char *rev);
256extern void cgit_refs_link(char *name, char *title, char *class, char *head,
257 char *rev, char *path);
258extern void cgit_snapshot_link(char *name, char *title, char *class,
259 char *head, char *rev, char *archivename);
260extern void cgit_diff_link(char *name, char *title, char *class, char *head,
261 char *new_rev, char *old_rev, char *path);
262
263extern void cgit_object_link(struct object *obj);
264
265extern void cgit_print_error(char *msg);
266extern void cgit_print_date(time_t secs, char *format);
267extern void cgit_print_age(time_t t, time_t max_relative, char *format);
268extern void cgit_print_docstart(char *title, struct cacheitem *item);
269extern void cgit_print_docend();
270extern void cgit_print_pageheader(char *title, int show_search);
271extern void cgit_print_snapshot_start(const char *mimetype,
272 const char *filename,
273 struct cacheitem *item);
274extern void cgit_print_branches(int maxcount);
275extern void cgit_print_tags(int maxcount);
276
277extern void cgit_print_repolist(struct cacheitem *item);
278extern void cgit_print_summary();
279extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep,
280 char *pattern, char *path, int pager);
281extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path);
282extern void cgit_print_tree(const char *rev, char *path);
283extern void cgit_print_commit(char *hex);
284extern void cgit_print_refs();
285extern void cgit_print_tag(char *revname);
286extern void cgit_print_diff(const char *new_hex, const char *old_hex, const char *prefix);
287extern void cgit_print_patch(char *hex, struct cacheitem *item);
288extern void cgit_print_snapshot(struct cacheitem *item, const char *head,
289 const char *hex, const char *prefix,
290 const char *filename, int snapshot);
291extern void cgit_print_snapshot_links(const char *repo, const char *head,
292 const char *hex, int snapshots);
293extern int cgit_parse_snapshots_mask(const char *str); 222extern int cgit_parse_snapshots_mask(const char *str);
294 223
295#endif /* CGIT_H */ 224#endif /* CGIT_H */
diff --git a/cmd.c b/cmd.c
new file mode 100644
index 0000000..e0eacbe
--- /dev/null
+++ b/cmd.c
@@ -0,0 +1,112 @@
1/* cmd.c: the cgit command dispatcher
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
10#include "cmd.h"
11#include "ui-blob.h"
12#include "ui-commit.h"
13#include "ui-diff.h"
14#include "ui-log.h"
15#include "ui-patch.h"
16#include "ui-refs.h"
17#include "ui-repolist.h"
18#include "ui-snapshot.h"
19#include "ui-summary.h"
20#include "ui-tag.h"
21#include "ui-tree.h"
22
23static void blob_fn(struct cgit_context *ctx)
24{
25 cgit_print_blob(ctx->qry.sha1, ctx->qry.path);
26}
27
28static void commit_fn(struct cgit_context *ctx)
29{
30 cgit_print_commit(ctx->qry.sha1);
31}
32
33static void diff_fn(struct cgit_context *ctx)
34{
35 cgit_print_diff(ctx->qry.sha1, ctx->qry.sha2, ctx->qry.path);
36}
37
38static void repolist_fn(struct cgit_context *ctx)
39{
40 cgit_print_repolist();
41}
42
43static void log_fn(struct cgit_context *ctx)
44{
45 cgit_print_log(ctx->qry.sha1, ctx->qry.ofs, ctx->cfg.max_commit_count,
46 ctx->qry.grep, ctx->qry.search, ctx->qry.path, 1);
47}
48
49static void patch_fn(struct cgit_context *ctx)
50{
51 cgit_print_patch(ctx->qry.sha1);
52}
53
54static void refs_fn(struct cgit_context *ctx)
55{
56 cgit_print_refs();
57}
58
59static void snapshot_fn(struct cgit_context *ctx)
60{
61 cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1,
62 cgit_repobasename(ctx->repo->url), ctx->qry.path,
63 ctx->repo->snapshots);
64}
65
66static void summary_fn(struct cgit_context *ctx)
67{
68 cgit_print_summary();
69}
70
71static void tag_fn(struct cgit_context *ctx)
72{
73 cgit_print_tag(ctx->qry.sha1);
74}
75
76static void tree_fn(struct cgit_context *ctx)
77{
78 cgit_print_tree(ctx->qry.sha1, ctx->qry.path);
79}
80
81#define def_cmd(name, want_repo, want_layout) \
82 {#name, name##_fn, want_repo, want_layout}
83
84struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)
85{
86 static struct cgit_cmd cmds[] = {
87 def_cmd(blob, 1, 0),
88 def_cmd(commit, 1, 1),
89 def_cmd(diff, 1, 1),
90 def_cmd(log, 1, 1),
91 def_cmd(patch, 1, 0),
92 def_cmd(refs, 1, 1),
93 def_cmd(repolist, 0, 0),
94 def_cmd(snapshot, 1, 0),
95 def_cmd(summary, 1, 1),
96 def_cmd(tag, 1, 1),
97 def_cmd(tree, 1, 1),
98 };
99 int i;
100
101 if (ctx->qry.page == NULL) {
102 if (ctx->repo)
103 ctx->qry.page = "summary";
104 else
105 ctx->qry.page = "repolist";
106 }
107
108 for(i = 0; i < sizeof(cmds)/sizeof(*cmds); i++)
109 if (!strcmp(ctx->qry.page, cmds[i].name))
110 return &cmds[i];
111 return NULL;
112}
diff --git a/cmd.h b/cmd.h
new file mode 100644
index 0000000..ec9e691
--- /dev/null
+++ b/cmd.h
@@ -0,0 +1,15 @@
1#ifndef CMD_H
2#define CMD_H
3
4typedef void (*cgit_cmd_fn)(struct cgit_context *ctx);
5
6struct cgit_cmd {
7 const char *name;
8 cgit_cmd_fn fn;
9 unsigned int want_repo:1,
10 want_layout:1;
11};
12
13extern struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx);
14
15#endif /* CMD_H */
diff --git a/configfile.c b/configfile.c
new file mode 100644
index 0000000..4908058
--- /dev/null
+++ b/configfile.c
@@ -0,0 +1,87 @@
1/* configfile.c: parsing of config files
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include <ctype.h>
10#include <stdio.h>
11#include "configfile.h"
12
13int next_char(FILE *f)
14{
15 int c = fgetc(f);
16 if (c=='\r') {
17 c = fgetc(f);
18 if (c!='\n') {
19 ungetc(c, f);
20 c = '\r';
21 }
22 }
23 return c;
24}
25
26void skip_line(FILE *f)
27{
28 int c;
29
30 while((c=next_char(f)) && c!='\n' && c!=EOF)
31 ;
32}
33
34int read_config_line(FILE *f, char *line, const char **value, int bufsize)
35{
36 int i = 0, isname = 0;
37
38 *value = NULL;
39 while(i<bufsize-1) {
40 int c = next_char(f);
41 if (!isname && (c=='#' || c==';')) {
42 skip_line(f);
43 continue;
44 }
45 if (!isname && isspace(c))
46 continue;
47
48 if (c=='=' && !*value) {
49 line[i] = 0;
50 *value = &line[i+1];
51 } else if (c=='\n' && !isname) {
52 i = 0;
53 continue;
54 } else if (c=='\n' || c==EOF) {
55 line[i] = 0;
56 break;
57 } else {
58 line[i]=c;
59 }
60 isname = 1;
61 i++;
62 }
63 line[i+1] = 0;