summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2008-03-27 23:09:11 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2008-03-27 23:09:11 (UTC)
commit20a33548b9a87a6eb23162ee5d137daa46d78613 (patch) (unidiff)
tree9c4ca364df64dcce640a531c7f515ee48bc99387
parentee4056bd2c902a12dea67874368863fe60ea5a5f (diff)
downloadcgit-20a33548b9a87a6eb23162ee5d137daa46d78613.zip
cgit-20a33548b9a87a6eb23162ee5d137daa46d78613.tar.gz
cgit-20a33548b9a87a6eb23162ee5d137daa46d78613.tar.bz2
Move function for configfile parsing into configfile.[ch]
This is a generic function which wanted its own little object file. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--Makefile1
-rw-r--r--cgit.c5
-rw-r--r--cgit.h1
-rw-r--r--configfile.c87
-rw-r--r--configfile.h8
-rw-r--r--parsing.c75
6 files changed, 99 insertions, 78 deletions
diff --git a/Makefile b/Makefile
index 223765d..355186e 100644
--- a/Makefile
+++ b/Makefile
@@ -6,96 +6,97 @@ CACHE_ROOT = /var/cache/cgit
6SHA1_HEADER = <openssl/sha.h> 6SHA1_HEADER = <openssl/sha.h>
7GIT_VER = 1.5.4.1 7GIT_VER = 1.5.4.1
8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2 8GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2
9 9
10# 10#
11# Let the user override the above settings. 11# Let the user override the above settings.
12# 12#
13-include cgit.conf 13-include cgit.conf
14 14
15# 15#
16# Define a way to invoke make in subdirs quietly, shamelessly ripped 16# Define a way to invoke make in subdirs quietly, shamelessly ripped
17# from git.git 17# from git.git
18# 18#
19QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir 19QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
20QUIET_SUBDIR1 = 20QUIET_SUBDIR1 =
21 21
22ifneq ($(findstring $(MAKEFLAGS),w),w) 22ifneq ($(findstring $(MAKEFLAGS),w),w)
23PRINT_DIR = --no-print-directory 23PRINT_DIR = --no-print-directory
24else # "make -w" 24else # "make -w"
25NO_SUBDIR = : 25NO_SUBDIR = :
26endif 26endif
27 27
28ifndef V 28ifndef V
29 QUIET_CC = @echo ' ' CC $@; 29 QUIET_CC = @echo ' ' CC $@;
30 QUIET_MM = @echo ' ' MM $@; 30 QUIET_MM = @echo ' ' MM $@;
31 QUIET_SUBDIR0 = +@subdir= 31 QUIET_SUBDIR0 = +@subdir=
32 QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \ 32 QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo ' ' SUBDIR $$subdir; \
33 $(MAKE) $(PRINT_DIR) -C $$subdir 33 $(MAKE) $(PRINT_DIR) -C $$subdir
34endif 34endif
35 35
36# 36#
37# Define a pattern rule for automatic dependency building 37# Define a pattern rule for automatic dependency building
38# 38#
39%.d: %.c 39%.d: %.c
40 $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@ 40 $(QUIET_MM)$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@
41 41
42# 42#
43# Define a pattern rule for silent object building 43# Define a pattern rule for silent object building
44# 44#
45%.o: %.c 45%.o: %.c
46 $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $< 46 $(QUIET_CC)$(CC) -o $*.o -c $(CFLAGS) $<
47 47
48 48
49EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto 49EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
50OBJECTS = 50OBJECTS =
51OBJECTS += cache.o 51OBJECTS += cache.o
52OBJECTS += cgit.o 52OBJECTS += cgit.o
53OBJECTS += cmd.o 53OBJECTS += cmd.o
54OBJECTS += configfile.o
54OBJECTS += html.o 55OBJECTS += html.o
55OBJECTS += parsing.o 56OBJECTS += parsing.o
56OBJECTS += shared.o 57OBJECTS += shared.o
57OBJECTS += ui-blob.o 58OBJECTS += ui-blob.o
58OBJECTS += ui-commit.o 59OBJECTS += ui-commit.o
59OBJECTS += ui-diff.o 60OBJECTS += ui-diff.o
60OBJECTS += ui-log.o 61OBJECTS += ui-log.o
61OBJECTS += ui-patch.o 62OBJECTS += ui-patch.o
62OBJECTS += ui-refs.o 63OBJECTS += ui-refs.o
63OBJECTS += ui-repolist.o 64OBJECTS += ui-repolist.o
64OBJECTS += ui-shared.o 65OBJECTS += ui-shared.o
65OBJECTS += ui-snapshot.o 66OBJECTS += ui-snapshot.o
66OBJECTS += ui-summary.o 67OBJECTS += ui-summary.o
67OBJECTS += ui-tag.o 68OBJECTS += ui-tag.o
68OBJECTS += ui-tree.o 69OBJECTS += ui-tree.o
69 70
70ifdef NEEDS_LIBICONV 71ifdef NEEDS_LIBICONV
71 EXTLIBS += -liconv 72 EXTLIBS += -liconv
72endif 73endif
73 74
74 75
75.PHONY: all git test install clean distclean emptycache force-version get-git 76.PHONY: all git test install clean distclean emptycache force-version get-git
76 77
77all: cgit git 78all: cgit git
78 79
79VERSION: force-version 80VERSION: force-version
80 @./gen-version.sh "$(CGIT_VERSION)" 81 @./gen-version.sh "$(CGIT_VERSION)"
81-include VERSION 82-include VERSION
82 83
83 84
84CFLAGS += -g -Wall -Igit 85CFLAGS += -g -Wall -Igit
85CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)' 86CFLAGS += -DSHA1_HEADER='$(SHA1_HEADER)'
86CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"' 87CFLAGS += -DCGIT_VERSION='"$(CGIT_VERSION)"'
87CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"' 88CFLAGS += -DCGIT_CONFIG='"$(CGIT_CONFIG)"'
88CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"' 89CFLAGS += -DCGIT_SCRIPT_NAME='"$(CGIT_SCRIPT_NAME)"'
89CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"' 90CFLAGS += -DCGIT_CACHE_ROOT='"$(CACHE_ROOT)"'
90 91
91 92
92cgit: $(OBJECTS) 93cgit: $(OBJECTS)
93 $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS) 94 $(QUIET_CC)$(CC) $(CFLAGS) -o cgit $(OBJECTS) $(EXTLIBS)
94 95
95$(OBJECTS): git/xdiff/lib.a git/libgit.a 96$(OBJECTS): git/xdiff/lib.a git/libgit.a
96 97
97cgit.o: VERSION 98cgit.o: VERSION
98 99
99-include $(OBJECTS:.o=.d) 100-include $(OBJECTS:.o=.d)
100 101
101git/xdiff/lib.a: | git 102git/xdiff/lib.a: | git
diff --git a/cgit.c b/cgit.c
index 73b1f02..1f46e0d 100644
--- a/cgit.c
+++ b/cgit.c
@@ -1,154 +1,155 @@
1/* cgit.c: cgi for the git scm 1/* cgit.c: cgi for the git scm
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "cache.h" 10#include "cache.h"
11#include "cmd.h" 11#include "cmd.h"
12#include "configfile.h"
12#include "ui-shared.h" 13#include "ui-shared.h"
13 14
14const char *cgit_version = CGIT_VERSION; 15const char *cgit_version = CGIT_VERSION;
15 16
16void config_cb(const char *name, const char *value) 17void config_cb(const char *name, const char *value)
17{ 18{
18 if (!strcmp(name, "root-title")) 19 if (!strcmp(name, "root-title"))
19 ctx.cfg.root_title = xstrdup(value); 20 ctx.cfg.root_title = xstrdup(value);
20 else if (!strcmp(name, "css")) 21 else if (!strcmp(name, "css"))
21 ctx.cfg.css = xstrdup(value); 22 ctx.cfg.css = xstrdup(value);
22 else if (!strcmp(name, "logo")) 23 else if (!strcmp(name, "logo"))
23 ctx.cfg.logo = xstrdup(value); 24 ctx.cfg.logo = xstrdup(value);
24 else if (!strcmp(name, "index-header")) 25 else if (!strcmp(name, "index-header"))
25 ctx.cfg.index_header = xstrdup(value); 26 ctx.cfg.index_header = xstrdup(value);
26 else if (!strcmp(name, "index-info")) 27 else if (!strcmp(name, "index-info"))
27 ctx.cfg.index_info = xstrdup(value); 28 ctx.cfg.index_info = xstrdup(value);
28 else if (!strcmp(name, "logo-link")) 29 else if (!strcmp(name, "logo-link"))
29 ctx.cfg.logo_link = xstrdup(value); 30 ctx.cfg.logo_link = xstrdup(value);
30 else if (!strcmp(name, "module-link")) 31 else if (!strcmp(name, "module-link"))
31 ctx.cfg.module_link = xstrdup(value); 32 ctx.cfg.module_link = xstrdup(value);
32 else if (!strcmp(name, "virtual-root")) { 33 else if (!strcmp(name, "virtual-root")) {
33 ctx.cfg.virtual_root = trim_end(value, '/'); 34 ctx.cfg.virtual_root = trim_end(value, '/');
34 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 35 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
35 ctx.cfg.virtual_root = ""; 36 ctx.cfg.virtual_root = "";
36 } else if (!strcmp(name, "nocache")) 37 } else if (!strcmp(name, "nocache"))
37 ctx.cfg.nocache = atoi(value); 38 ctx.cfg.nocache = atoi(value);
38 else if (!strcmp(name, "snapshots")) 39 else if (!strcmp(name, "snapshots"))
39 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 40 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
40 else if (!strcmp(name, "enable-index-links")) 41 else if (!strcmp(name, "enable-index-links"))
41 ctx.cfg.enable_index_links = atoi(value); 42 ctx.cfg.enable_index_links = atoi(value);
42 else if (!strcmp(name, "enable-log-filecount")) 43 else if (!strcmp(name, "enable-log-filecount"))
43 ctx.cfg.enable_log_filecount = atoi(value); 44 ctx.cfg.enable_log_filecount = atoi(value);
44 else if (!strcmp(name, "enable-log-linecount")) 45 else if (!strcmp(name, "enable-log-linecount"))
45 ctx.cfg.enable_log_linecount = atoi(value); 46 ctx.cfg.enable_log_linecount = atoi(value);
46 else if (!strcmp(name, "cache-root")) 47 else if (!strcmp(name, "cache-root"))
47 ctx.cfg.cache_root = xstrdup(value); 48 ctx.cfg.cache_root = xstrdup(value);
48 else if (!strcmp(name, "cache-root-ttl")) 49 else if (!strcmp(name, "cache-root-ttl"))
49 ctx.cfg.cache_root_ttl = atoi(value); 50 ctx.cfg.cache_root_ttl = atoi(value);
50 else if (!strcmp(name, "cache-repo-ttl")) 51 else if (!strcmp(name, "cache-repo-ttl"))
51 ctx.cfg.cache_repo_ttl = atoi(value); 52 ctx.cfg.cache_repo_ttl = atoi(value);
52 else if (!strcmp(name, "cache-static-ttl")) 53 else if (!strcmp(name, "cache-static-ttl"))
53 ctx.cfg.cache_static_ttl = atoi(value); 54 ctx.cfg.cache_static_ttl = atoi(value);
54 else if (!strcmp(name, "cache-dynamic-ttl")) 55 else if (!strcmp(name, "cache-dynamic-ttl"))
55 ctx.cfg.cache_dynamic_ttl = atoi(value); 56 ctx.cfg.cache_dynamic_ttl = atoi(value);
56 else if (!strcmp(name, "max-message-length")) 57 else if (!strcmp(name, "max-message-length"))
57 ctx.cfg.max_msg_len = atoi(value); 58 ctx.cfg.max_msg_len = atoi(value);
58 else if (!strcmp(name, "max-repodesc-length")) 59 else if (!strcmp(name, "max-repodesc-length"))
59 ctx.cfg.max_repodesc_len = atoi(value); 60 ctx.cfg.max_repodesc_len = atoi(value);
60 else if (!strcmp(name, "max-commit-count")) 61 else if (!strcmp(name, "max-commit-count"))
61 ctx.cfg.max_commit_count = atoi(value); 62 ctx.cfg.max_commit_count = atoi(value);
62 else if (!strcmp(name, "summary-log")) 63 else if (!strcmp(name, "summary-log"))
63 ctx.cfg.summary_log = atoi(value); 64 ctx.cfg.summary_log = atoi(value);
64 else if (!strcmp(name, "summary-branches")) 65 else if (!strcmp(name, "summary-branches"))
65 ctx.cfg.summary_branches = atoi(value); 66 ctx.cfg.summary_branches = atoi(value);
66 else if (!strcmp(name, "summary-tags")) 67 else if (!strcmp(name, "summary-tags"))
67 ctx.cfg.summary_tags = atoi(value); 68 ctx.cfg.summary_tags = atoi(value);
68 else if (!strcmp(name, "agefile")) 69 else if (!strcmp(name, "agefile"))
69 ctx.cfg.agefile = xstrdup(value); 70 ctx.cfg.agefile = xstrdup(value);
70 else if (!strcmp(name, "renamelimit")) 71 else if (!strcmp(name, "renamelimit"))
71 ctx.cfg.renamelimit = atoi(value); 72 ctx.cfg.renamelimit = atoi(value);
72 else if (!strcmp(name, "robots")) 73 else if (!strcmp(name, "robots"))
73 ctx.cfg.robots = xstrdup(value); 74 ctx.cfg.robots = xstrdup(value);
74 else if (!strcmp(name, "clone-prefix")) 75 else if (!strcmp(name, "clone-prefix"))
75 ctx.cfg.clone_prefix = xstrdup(value); 76 ctx.cfg.clone_prefix = xstrdup(value);
76 else if (!strcmp(name, "repo.group")) 77 else if (!strcmp(name, "repo.group"))
77 ctx.cfg.repo_group = xstrdup(value); 78 ctx.cfg.repo_group = xstrdup(value);
78 else if (!strcmp(name, "repo.url")) 79 else if (!strcmp(name, "repo.url"))
79 ctx.repo = cgit_add_repo(value); 80 ctx.repo = cgit_add_repo(value);
80 else if (!strcmp(name, "repo.name")) 81 else if (!strcmp(name, "repo.name"))
81 ctx.repo->name = xstrdup(value); 82 ctx.repo->name = xstrdup(value);
82 else if (ctx.repo && !strcmp(name, "repo.path")) 83 else if (ctx.repo && !strcmp(name, "repo.path"))
83 ctx.repo->path = trim_end(value, '/'); 84 ctx.repo->path = trim_end(value, '/');
84 else if (ctx.repo && !strcmp(name, "repo.clone-url")) 85 else if (ctx.repo && !strcmp(name, "repo.clone-url"))
85 ctx.repo->clone_url = xstrdup(value); 86 ctx.repo->clone_url = xstrdup(value);
86 else if (ctx.repo && !strcmp(name, "repo.desc")) 87 else if (ctx.repo && !strcmp(name, "repo.desc"))
87 ctx.repo->desc = xstrdup(value); 88 ctx.repo->desc = xstrdup(value);
88 else if (ctx.repo && !strcmp(name, "repo.owner")) 89 else if (ctx.repo && !strcmp(name, "repo.owner"))
89 ctx.repo->owner = xstrdup(value); 90 ctx.repo->owner = xstrdup(value);
90 else if (ctx.repo && !strcmp(name, "repo.defbranch")) 91 else if (ctx.repo && !strcmp(name, "repo.defbranch"))
91 ctx.repo->defbranch = xstrdup(value); 92 ctx.repo->defbranch = xstrdup(value);
92 else if (ctx.repo && !strcmp(name, "repo.snapshots")) 93 else if (ctx.repo && !strcmp(name, "repo.snapshots"))
93 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */ 94 ctx.repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); /* XXX: &? */
94 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount")) 95 else if (ctx.repo && !strcmp(name, "repo.enable-log-filecount"))
95 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 96 ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
96 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount")) 97 else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))
97 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 98 ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
98 else if (ctx.repo && !strcmp(name, "repo.module-link")) 99 else if (ctx.repo && !strcmp(name, "repo.module-link"))
99 ctx.repo->module_link= xstrdup(value); 100 ctx.repo->module_link= xstrdup(value);
100 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { 101 else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) {
101 if (*value == '/') 102 if (*value == '/')
102 ctx.repo->readme = xstrdup(value); 103 ctx.repo->readme = xstrdup(value);
103 else 104 else
104 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); 105 ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value));
105 } else if (!strcmp(name, "include")) 106 } else if (!strcmp(name, "include"))
106 cgit_read_config(value, config_cb); 107 parse_configfile(value, config_cb);
107} 108}
108 109
109static void querystring_cb(const char *name, const char *value) 110static void querystring_cb(const char *name, const char *value)
110{ 111{
111 if (!strcmp(name,"r")) { 112 if (!strcmp(name,"r")) {
112 ctx.qry.repo = xstrdup(value); 113 ctx.qry.repo = xstrdup(value);
113 ctx.repo = cgit_get_repoinfo(value); 114 ctx.repo = cgit_get_repoinfo(value);
114 } else if (!strcmp(name, "p")) { 115 } else if (!strcmp(name, "p")) {
115 ctx.qry.page = xstrdup(value); 116 ctx.qry.page = xstrdup(value);
116 } else if (!strcmp(name, "url")) { 117 } else if (!strcmp(name, "url")) {
117 cgit_parse_url(value); 118 cgit_parse_url(value);
118 } else if (!strcmp(name, "qt")) { 119 } else if (!strcmp(name, "qt")) {
119 ctx.qry.grep = xstrdup(value); 120 ctx.qry.grep = xstrdup(value);
120 } else if (!strcmp(name, "q")) { 121 } else if (!strcmp(name, "q")) {
121 ctx.qry.search = xstrdup(value); 122 ctx.qry.search = xstrdup(value);
122 } else if (!strcmp(name, "h")) { 123 } else if (!strcmp(name, "h")) {
123 ctx.qry.head = xstrdup(value); 124 ctx.qry.head = xstrdup(value);
124 ctx.qry.has_symref = 1; 125 ctx.qry.has_symref = 1;
125 } else if (!strcmp(name, "id")) { 126 } else if (!strcmp(name, "id")) {
126 ctx.qry.sha1 = xstrdup(value); 127 ctx.qry.sha1 = xstrdup(value);
127 ctx.qry.has_sha1 = 1; 128 ctx.qry.has_sha1 = 1;
128 } else if (!strcmp(name, "id2")) { 129 } else if (!strcmp(name, "id2")) {
129 ctx.qry.sha2 = xstrdup(value); 130 ctx.qry.sha2 = xstrdup(value);
130 ctx.qry.has_sha1 = 1; 131 ctx.qry.has_sha1 = 1;
131 } else if (!strcmp(name, "ofs")) { 132 } else if (!strcmp(name, "ofs")) {
132 ctx.qry.ofs = atoi(value); 133 ctx.qry.ofs = atoi(value);
133 } else if (!strcmp(name, "path")) { 134 } else if (!strcmp(name, "path")) {
134 ctx.qry.path = trim_end(value, '/'); 135 ctx.qry.path = trim_end(value, '/');
135 } else if (!strcmp(name, "name")) { 136 } else if (!strcmp(name, "name")) {
136 ctx.qry.name = xstrdup(value); 137 ctx.qry.name = xstrdup(value);
137 } 138 }
138} 139}
139 140
140static void prepare_context(struct cgit_context *ctx) 141static void prepare_context(struct cgit_context *ctx)
141{ 142{
142 memset(ctx, 0, sizeof(ctx)); 143 memset(ctx, 0, sizeof(ctx));
143 ctx->cfg.agefile = "info/web/last-modified"; 144 ctx->cfg.agefile = "info/web/last-modified";
144 ctx->cfg.cache_dynamic_ttl = 5; 145 ctx->cfg.cache_dynamic_ttl = 5;
145 ctx->cfg.cache_max_create_time = 5; 146 ctx->cfg.cache_max_create_time = 5;
146 ctx->cfg.cache_repo_ttl = 5; 147 ctx->cfg.cache_repo_ttl = 5;
147 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 148 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
148 ctx->cfg.cache_root_ttl = 5; 149 ctx->cfg.cache_root_ttl = 5;
149 ctx->cfg.cache_static_ttl = -1; 150 ctx->cfg.cache_static_ttl = -1;
150 ctx->cfg.css = "/cgit.css"; 151 ctx->cfg.css = "/cgit.css";
151 ctx->cfg.logo = "/git-logo.png"; 152 ctx->cfg.logo = "/git-logo.png";
152 ctx->cfg.max_commit_count = 50; 153 ctx->cfg.max_commit_count = 50;
153 ctx->cfg.max_lock_attempts = 5; 154 ctx->cfg.max_lock_attempts = 5;
154 ctx->cfg.max_msg_len = 60; 155 ctx->cfg.max_msg_len = 60;
@@ -391,66 +392,66 @@ static void cgit_print_cache(struct cacheitem *item)
391 close(fd); 392 close(fd);
392} 393}
393 394
394static void cgit_parse_args(int argc, const char **argv) 395static void cgit_parse_args(int argc, const char **argv)
395{ 396{
396 int i; 397 int i;
397 398
398 for (i = 1; i < argc; i++) { 399 for (i = 1; i < argc; i++) {
399 if (!strncmp(argv[i], "--cache=", 8)) { 400 if (!strncmp(argv[i], "--cache=", 8)) {
400 ctx.cfg.cache_root = xstrdup(argv[i]+8); 401 ctx.cfg.cache_root = xstrdup(argv[i]+8);
401 } 402 }
402 if (!strcmp(argv[i], "--nocache")) { 403 if (!strcmp(argv[i], "--nocache")) {
403 ctx.cfg.nocache = 1; 404 ctx.cfg.nocache = 1;
404 } 405 }
405 if (!strncmp(argv[i], "--query=", 8)) { 406 if (!strncmp(argv[i], "--query=", 8)) {
406 ctx.qry.raw = xstrdup(argv[i]+8); 407 ctx.qry.raw = xstrdup(argv[i]+8);
407 } 408 }
408 if (!strncmp(argv[i], "--repo=", 7)) { 409 if (!strncmp(argv[i], "--repo=", 7)) {
409 ctx.qry.repo = xstrdup(argv[i]+7); 410 ctx.qry.repo = xstrdup(argv[i]+7);
410 } 411 }
411 if (!strncmp(argv[i], "--page=", 7)) { 412 if (!strncmp(argv[i], "--page=", 7)) {
412 ctx.qry.page = xstrdup(argv[i]+7); 413 ctx.qry.page = xstrdup(argv[i]+7);
413 } 414 }
414 if (!strncmp(argv[i], "--head=", 7)) { 415 if (!strncmp(argv[i], "--head=", 7)) {
415 ctx.qry.head = xstrdup(argv[i]+7); 416 ctx.qry.head = xstrdup(argv[i]+7);
416 ctx.qry.has_symref = 1; 417 ctx.qry.has_symref = 1;
417 } 418 }
418 if (!strncmp(argv[i], "--sha1=", 7)) { 419 if (!strncmp(argv[i], "--sha1=", 7)) {
419 ctx.qry.sha1 = xstrdup(argv[i]+7); 420 ctx.qry.sha1 = xstrdup(argv[i]+7);
420 ctx.qry.has_sha1 = 1; 421 ctx.qry.has_sha1 = 1;
421 } 422 }
422 if (!strncmp(argv[i], "--ofs=", 6)) { 423 if (!strncmp(argv[i], "--ofs=", 6)) {
423 ctx.qry.ofs = atoi(argv[i]+6); 424 ctx.qry.ofs = atoi(argv[i]+6);
424 } 425 }
425 } 426 }
426} 427}
427 428
428int main(int argc, const char **argv) 429int main(int argc, const char **argv)
429{ 430{
430 struct cacheitem item; 431 struct cacheitem item;
431 const char *cgit_config_env = getenv("CGIT_CONFIG"); 432 const char *cgit_config_env = getenv("CGIT_CONFIG");
432 433
433 prepare_context(&ctx); 434 prepare_context(&ctx);
434 item.st.st_mtime = time(NULL); 435 item.st.st_mtime = time(NULL);
435 cgit_repolist.length = 0; 436 cgit_repolist.length = 0;
436 cgit_repolist.count = 0; 437 cgit_repolist.count = 0;
437 cgit_repolist.repos = NULL; 438 cgit_repolist.repos = NULL;
438 439
439 cgit_read_config(cgit_config_env ? cgit_config_env : CGIT_CONFIG, 440 parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG,
440 config_cb); 441 config_cb);
441 if (getenv("SCRIPT_NAME")) 442 if (getenv("SCRIPT_NAME"))
442 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); 443 ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME"));
443 if (getenv("QUERY_STRING")) 444 if (getenv("QUERY_STRING"))
444 ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); 445 ctx.qry.raw = xstrdup(getenv("QUERY_STRING"));
445 cgit_parse_args(argc, argv); 446 cgit_parse_args(argc, argv);
446 cgit_parse_query(ctx.qry.raw, querystring_cb); 447 cgit_parse_query(ctx.qry.raw, querystring_cb);
447 if (!cgit_prepare_cache(&item)) 448 if (!cgit_prepare_cache(&item))
448 return 0; 449 return 0;
449 if (ctx.cfg.nocache) { 450 if (ctx.cfg.nocache) {
450 cgit_fill_cache(&item, 0); 451 cgit_fill_cache(&item, 0);
451 } else { 452 } else {
452 cgit_check_cache(&item); 453 cgit_check_cache(&item);
453 cgit_print_cache(&item); 454 cgit_print_cache(&item);
454 } 455 }
455 return 0; 456 return 0;
456} 457}
diff --git a/cgit.h b/cgit.h
index f600912..91d18f8 100644
--- a/cgit.h
+++ b/cgit.h
@@ -169,59 +169,58 @@ struct cgit_context {
169 struct cgit_repo *repo; 169 struct cgit_repo *repo;
170 struct cgit_page page; 170 struct cgit_page page;
171}; 171};
172 172
173struct cgit_snapshot_format { 173struct cgit_snapshot_format {
174 const char *suffix; 174 const char *suffix;
175 const char *mimetype; 175 const char *mimetype;
176 write_archive_fn_t write_func; 176 write_archive_fn_t write_func;
177 int bit; 177 int bit;
178}; 178};
179 179
180extern const char *cgit_version; 180extern const char *cgit_version;
181 181
182extern struct cgit_repolist cgit_repolist; 182extern struct cgit_repolist cgit_repolist;
183extern struct cgit_context ctx; 183extern struct cgit_context ctx;
184extern const struct cgit_snapshot_format cgit_snapshot_formats[]; 184extern const struct cgit_snapshot_format cgit_snapshot_formats[];
185 185
186extern struct cgit_repo *cgit_add_repo(const char *url); 186extern struct cgit_repo *cgit_add_repo(const char *url);
187extern struct cgit_repo *cgit_get_repoinfo(const char *url); 187extern struct cgit_repo *cgit_get_repoinfo(const char *url);
188extern void cgit_repo_config_cb(const char *name, const char *value); 188extern void cgit_repo_config_cb(const char *name, const char *value);
189 189
190extern int chk_zero(int result, char *msg); 190extern int chk_zero(int result, char *msg);
191extern int chk_positive(int result, char *msg); 191extern int chk_positive(int result, char *msg);
192extern int chk_non_negative(int result, char *msg); 192extern int chk_non_negative(int result, char *msg);
193 193
194extern int hextoint(char c); 194extern int hextoint(char c);
195extern char *trim_end(const char *str, char c); 195extern char *trim_end(const char *str, char c);
196extern char *strlpart(char *txt, int maxlen); 196extern char *strlpart(char *txt, int maxlen);
197extern char *strrpart(char *txt, int maxlen); 197extern char *strrpart(char *txt, int maxlen);
198 198
199extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 199extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
200extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 200extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
201 int flags, void *cb_data); 201 int flags, void *cb_data);
202 202
203extern void *cgit_free_commitinfo(struct commitinfo *info); 203extern void *cgit_free_commitinfo(struct commitinfo *info);
204 204
205extern int cgit_diff_files(const unsigned char *old_sha1, 205extern int cgit_diff_files(const unsigned char *old_sha1,
206 const unsigned char *new_sha1, 206 const unsigned char *new_sha1,
207 linediff_fn fn); 207 linediff_fn fn);
208 208
209extern void cgit_diff_tree(const unsigned char *old_sha1, 209extern void cgit_diff_tree(const unsigned char *old_sha1,
210 const unsigned char *new_sha1, 210 const unsigned char *new_sha1,
211 filepair_fn fn, const char *prefix); 211 filepair_fn fn, const char *prefix);
212 212
213extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 213extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
214 214
215extern char *fmt(const char *format,...); 215extern char *fmt(const char *format,...);
216 216
217extern int cgit_read_config(const char *filename, configfn fn);
218extern int cgit_parse_query(char *txt, configfn fn); 217extern int cgit_parse_query(char *txt, configfn fn);
219extern struct commitinfo *cgit_parse_commit(struct commit *commit); 218extern struct commitinfo *cgit_parse_commit(struct commit *commit);
220extern struct taginfo *cgit_parse_tag(struct tag *tag); 219extern struct taginfo *cgit_parse_tag(struct tag *tag);
221extern void cgit_parse_url(const char *url); 220extern void cgit_parse_url(const char *url);
222 221
223extern const char *cgit_repobasename(const char *reponame); 222extern const char *cgit_repobasename(const char *reponame);
224 223
225extern int cgit_parse_snapshots_mask(const char *str); 224extern int cgit_parse_snapshots_mask(const char *str);
226 225
227#endif /* CGIT_H */ 226#endif /* CGIT_H */
diff --git a/configfile.c b/configfile.c
new file mode 100644
index 0000000..4908058
--- a/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;
64 return i;
65}
66
67int parse_configfile(const char *filename, configfile_value_fn fn)
68{
69 static int nesting;
70 int len;
71 char line[256];
72 const char *value;
73 FILE *f;
74
75 /* cancel deeply nested include-commands */
76 if (nesting > 8)
77 return -1;
78 if (!(f = fopen(filename, "r")))
79 return -1;
80 nesting++;
81 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
82 fn(line, value);
83 nesting--;
84 fclose(f);
85 return 0;
86}
87
diff --git a/configfile.h b/configfile.h
new file mode 100644
index 0000000..04235e5
--- a/dev/null
+++ b/configfile.h
@@ -0,0 +1,8 @@
1#ifndef CONFIGFILE_H
2#define CONFIGFILE_H
3
4typedef void (*configfile_value_fn)(const char *name, const char *value);
5
6extern int parse_configfile(const char *filename, configfile_value_fn fn);
7
8#endif /* CONFIGFILE_H */
diff --git a/parsing.c b/parsing.c
index 8dce488..9a4a7a3 100644
--- a/parsing.c
+++ b/parsing.c
@@ -1,133 +1,58 @@
1/* config.c: parsing of config files 1/* config.c: parsing of config files
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11int next_char(FILE *f)
12{
13 int c = fgetc(f);
14 if (c=='\r') {
15 c = fgetc(f);
16 if (c!='\n') {
17 ungetc(c, f);
18 c = '\r';
19 }
20 }
21 return c;
22}
23
24void skip_line(FILE *f)
25{
26 int c;
27
28 while((c=next_char(f)) && c!='\n' && c!=EOF)
29 ;
30}
31
32int read_config_line(FILE *f, char *line, const char **value, int bufsize)
33{
34 int i = 0, isname = 0;
35
36 *value = NULL;
37 while(i<bufsize-1) {
38 int c = next_char(f);
39 if (!isname && (c=='#' || c==';')) {
40 skip_line(f);
41 continue;
42 }
43 if (!isname && isspace(c))
44 continue;
45
46 if (c=='=' && !*value) {
47 line[i] = 0;
48 *value = &line[i+1];
49 } else if (c=='\n' && !isname) {
50 i = 0;
51 continue;
52 } else if (c=='\n' || c==EOF) {
53 line[i] = 0;
54 break;
55 } else {
56 line[i]=c;
57 }
58 isname = 1;
59 i++;
60 }
61 line[i+1] = 0;
62 return i;
63}
64
65int cgit_read_config(const char *filename, configfn fn)
66{
67 static int nesting;
68 int len;
69 char line[256];
70 const char *value;
71 FILE *f;
72
73 /* cancel deeply nested include-commands */
74 if (nesting > 8)
75 return -1;
76 if (!(f = fopen(filename, "r")))
77 return -1;
78 nesting++;
79 while((len = read_config_line(f, line, &value, sizeof(line))) > 0)
80 (*fn)(line, value);
81 nesting--;
82 fclose(f);
83 return 0;
84}
85
86char *convert_query_hexchar(char *txt) 11char *convert_query_hexchar(char *txt)
87{ 12{
88 int d1, d2; 13 int d1, d2;
89 if (strlen(txt) < 3) { 14 if (strlen(txt) < 3) {
90 *txt = '\0'; 15 *txt = '\0';
91 return txt-1; 16 return txt-1;
92 } 17 }
93 d1 = hextoint(*(txt+1)); 18 d1 = hextoint(*(txt+1));
94 d2 = hextoint(*(txt+2)); 19 d2 = hextoint(*(txt+2));
95 if (d1<0 || d2<0) { 20 if (d1<0 || d2<0) {
96 strcpy(txt, txt+3); 21 strcpy(txt, txt+3);
97 return txt-1; 22 return txt-1;
98 } else { 23 } else {
99 *txt = d1 * 16 + d2; 24 *txt = d1 * 16 + d2;
100 strcpy(txt+1, txt+3); 25 strcpy(txt+1, txt+3);
101 return txt; 26 return txt;
102 } 27 }
103} 28}
104 29
105int cgit_parse_query(char *txt, configfn fn) 30int cgit_parse_query(char *txt, configfn fn)
106{ 31{
107 char *t, *value = NULL, c; 32 char *t, *value = NULL, c;
108 33
109 if (!txt) 34 if (!txt)
110 return 0; 35 return 0;
111 36
112 t = txt = xstrdup(txt); 37 t = txt = xstrdup(txt);
113 38
114 while((c=*t) != '\0') { 39 while((c=*t) != '\0') {
115 if (c=='=') { 40 if (c=='=') {
116 *t = '\0'; 41 *t = '\0';
117 value = t+1; 42 value = t+1;
118 } else if (c=='+') { 43 } else if (c=='+') {
119 *t = ' '; 44 *t = ' ';
120 } else if (c=='%') { 45 } else if (c=='%') {
121 t = convert_query_hexchar(t); 46 t = convert_query_hexchar(t);
122 } else if (c=='&') { 47 } else if (c=='&') {
123 *t = '\0'; 48 *t = '\0';
124 (*fn)(txt, value); 49 (*fn)(txt, value);
125 txt = t+1; 50 txt = t+1;
126 value = NULL; 51 value = NULL;
127 } 52 }
128 t++; 53 t++;
129 } 54 }
130 if (t!=txt) 55 if (t!=txt)
131 (*fn)(txt, value); 56 (*fn)(txt, value);
132 return 0; 57 return 0;
133} 58}