summaryrefslogtreecommitdiffabout
authorLars Hjemli <hjemli@gmail.com>2010-03-21 23:09:43 (UTC)
committer Lars Hjemli <hjemli@gmail.com>2010-03-21 23:09:43 (UTC)
commitba56a37e863d0f90e3e953047486905cfb959925 (patch) (unidiff)
tree965e2c917f472264de35125eec35c361984eba4f
parentff3a3b4e2b7463bb8cb370bdda393e8b3526fcb9 (diff)
downloadcgit-ba56a37e863d0f90e3e953047486905cfb959925.zip
cgit-ba56a37e863d0f90e3e953047486905cfb959925.tar.gz
cgit-ba56a37e863d0f90e3e953047486905cfb959925.tar.bz2
Add support for environment variable expansion
This patch teaches cgit to expand environment variables in certain cgitrc option values (cache_root, scan-path, include) plus when finding the location of cgitrc itself. One use case for this feature is virtual hosting - e.g. by setting $CGIT_CONFIG='/etc/cgitrc/$HTTP_HOST' in httpd.conf, all virtual hosts automatically gets their own cgitrc. Signed-off-by: Lars Hjemli <hjemli@gmail.com>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--cgit.c10
-rw-r--r--cgit.h2
-rw-r--r--shared.c71
3 files changed, 78 insertions, 5 deletions
diff --git a/cgit.c b/cgit.c
index 9305d0a..37154e3 100644
--- a/cgit.c
+++ b/cgit.c
@@ -55,248 +55,248 @@ void repo_config(struct cgit_repo *repo, const char *name, const char *value)
55 else if (!strcmp(name, "defbranch")) 55 else if (!strcmp(name, "defbranch"))
56 repo->defbranch = xstrdup(value); 56 repo->defbranch = xstrdup(value);
57 else if (!strcmp(name, "snapshots")) 57 else if (!strcmp(name, "snapshots"))
58 repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value); 58 repo->snapshots = ctx.cfg.snapshots & cgit_parse_snapshots_mask(value);
59 else if (!strcmp(name, "enable-log-filecount")) 59 else if (!strcmp(name, "enable-log-filecount"))
60 repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value); 60 repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);
61 else if (!strcmp(name, "enable-log-linecount")) 61 else if (!strcmp(name, "enable-log-linecount"))
62 repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); 62 repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value);
63 else if (!strcmp(name, "enable-remote-branches")) 63 else if (!strcmp(name, "enable-remote-branches"))
64 repo->enable_remote_branches = atoi(value); 64 repo->enable_remote_branches = atoi(value);
65 else if (!strcmp(name, "max-stats")) 65 else if (!strcmp(name, "max-stats"))
66 repo->max_stats = cgit_find_stats_period(value, NULL); 66 repo->max_stats = cgit_find_stats_period(value, NULL);
67 else if (!strcmp(name, "module-link")) 67 else if (!strcmp(name, "module-link"))
68 repo->module_link= xstrdup(value); 68 repo->module_link= xstrdup(value);
69 else if (!strcmp(name, "section")) 69 else if (!strcmp(name, "section"))
70 repo->section = xstrdup(value); 70 repo->section = xstrdup(value);
71 else if (!strcmp(name, "readme") && value != NULL) { 71 else if (!strcmp(name, "readme") && value != NULL) {
72 if (*value == '/') 72 if (*value == '/')
73 repo->readme = xstrdup(value); 73 repo->readme = xstrdup(value);
74 else 74 else
75 repo->readme = xstrdup(fmt("%s/%s", repo->path, value)); 75 repo->readme = xstrdup(fmt("%s/%s", repo->path, value));
76 } else if (ctx.cfg.enable_filter_overrides) { 76 } else if (ctx.cfg.enable_filter_overrides) {
77 if (!strcmp(name, "about-filter")) 77 if (!strcmp(name, "about-filter"))
78 repo->about_filter = new_filter(value, 0); 78 repo->about_filter = new_filter(value, 0);
79 else if (!strcmp(name, "commit-filter")) 79 else if (!strcmp(name, "commit-filter"))
80 repo->commit_filter = new_filter(value, 0); 80 repo->commit_filter = new_filter(value, 0);
81 else if (!strcmp(name, "source-filter")) 81 else if (!strcmp(name, "source-filter"))
82 repo->source_filter = new_filter(value, 1); 82 repo->source_filter = new_filter(value, 1);
83 } 83 }
84} 84}
85 85
86void config_cb(const char *name, const char *value) 86void config_cb(const char *name, const char *value)
87{ 87{
88 if (!strcmp(name, "section") || !strcmp(name, "repo.group")) 88 if (!strcmp(name, "section") || !strcmp(name, "repo.group"))
89 ctx.cfg.section = xstrdup(value); 89 ctx.cfg.section = xstrdup(value);
90 else if (!strcmp(name, "repo.url")) 90 else if (!strcmp(name, "repo.url"))
91 ctx.repo = cgit_add_repo(value); 91 ctx.repo = cgit_add_repo(value);
92 else if (ctx.repo && !strcmp(name, "repo.path")) 92 else if (ctx.repo && !strcmp(name, "repo.path"))
93 ctx.repo->path = trim_end(value, '/'); 93 ctx.repo->path = trim_end(value, '/');
94 else if (ctx.repo && !prefixcmp(name, "repo.")) 94 else if (ctx.repo && !prefixcmp(name, "repo."))
95 repo_config(ctx.repo, name + 5, value); 95 repo_config(ctx.repo, name + 5, value);
96 else if (!strcmp(name, "root-title")) 96 else if (!strcmp(name, "root-title"))
97 ctx.cfg.root_title = xstrdup(value); 97 ctx.cfg.root_title = xstrdup(value);
98 else if (!strcmp(name, "root-desc")) 98 else if (!strcmp(name, "root-desc"))
99 ctx.cfg.root_desc = xstrdup(value); 99 ctx.cfg.root_desc = xstrdup(value);
100 else if (!strcmp(name, "root-readme")) 100 else if (!strcmp(name, "root-readme"))
101 ctx.cfg.root_readme = xstrdup(value); 101 ctx.cfg.root_readme = xstrdup(value);
102 else if (!strcmp(name, "css")) 102 else if (!strcmp(name, "css"))
103 ctx.cfg.css = xstrdup(value); 103 ctx.cfg.css = xstrdup(value);
104 else if (!strcmp(name, "favicon")) 104 else if (!strcmp(name, "favicon"))
105 ctx.cfg.favicon = xstrdup(value); 105 ctx.cfg.favicon = xstrdup(value);
106 else if (!strcmp(name, "footer")) 106 else if (!strcmp(name, "footer"))
107 ctx.cfg.footer = xstrdup(value); 107 ctx.cfg.footer = xstrdup(value);
108 else if (!strcmp(name, "head-include")) 108 else if (!strcmp(name, "head-include"))
109 ctx.cfg.head_include = xstrdup(value); 109 ctx.cfg.head_include = xstrdup(value);
110 else if (!strcmp(name, "header")) 110 else if (!strcmp(name, "header"))
111 ctx.cfg.header = xstrdup(value); 111 ctx.cfg.header = xstrdup(value);
112 else if (!strcmp(name, "logo")) 112 else if (!strcmp(name, "logo"))
113 ctx.cfg.logo = xstrdup(value); 113 ctx.cfg.logo = xstrdup(value);
114 else if (!strcmp(name, "index-header")) 114 else if (!strcmp(name, "index-header"))
115 ctx.cfg.index_header = xstrdup(value); 115 ctx.cfg.index_header = xstrdup(value);
116 else if (!strcmp(name, "index-info")) 116 else if (!strcmp(name, "index-info"))
117 ctx.cfg.index_info = xstrdup(value); 117 ctx.cfg.index_info = xstrdup(value);
118 else if (!strcmp(name, "logo-link")) 118 else if (!strcmp(name, "logo-link"))
119 ctx.cfg.logo_link = xstrdup(value); 119 ctx.cfg.logo_link = xstrdup(value);
120 else if (!strcmp(name, "module-link")) 120 else if (!strcmp(name, "module-link"))
121 ctx.cfg.module_link = xstrdup(value); 121 ctx.cfg.module_link = xstrdup(value);
122 else if (!strcmp(name, "virtual-root")) { 122 else if (!strcmp(name, "virtual-root")) {
123 ctx.cfg.virtual_root = trim_end(value, '/'); 123 ctx.cfg.virtual_root = trim_end(value, '/');
124 if (!ctx.cfg.virtual_root && (!strcmp(value, "/"))) 124 if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
125 ctx.cfg.virtual_root = ""; 125 ctx.cfg.virtual_root = "";
126 } else if (!strcmp(name, "nocache")) 126 } else if (!strcmp(name, "nocache"))
127 ctx.cfg.nocache = atoi(value); 127 ctx.cfg.nocache = atoi(value);
128 else if (!strcmp(name, "noplainemail")) 128 else if (!strcmp(name, "noplainemail"))
129 ctx.cfg.noplainemail = atoi(value); 129 ctx.cfg.noplainemail = atoi(value);
130 else if (!strcmp(name, "noheader")) 130 else if (!strcmp(name, "noheader"))
131 ctx.cfg.noheader = atoi(value); 131 ctx.cfg.noheader = atoi(value);
132 else if (!strcmp(name, "snapshots")) 132 else if (!strcmp(name, "snapshots"))
133 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value); 133 ctx.cfg.snapshots = cgit_parse_snapshots_mask(value);
134 else if (!strcmp(name, "enable-filter-overrides")) 134 else if (!strcmp(name, "enable-filter-overrides"))
135 ctx.cfg.enable_filter_overrides = atoi(value); 135 ctx.cfg.enable_filter_overrides = atoi(value);
136 else if (!strcmp(name, "enable-index-links")) 136 else if (!strcmp(name, "enable-index-links"))
137 ctx.cfg.enable_index_links = atoi(value); 137 ctx.cfg.enable_index_links = atoi(value);
138 else if (!strcmp(name, "enable-log-filecount")) 138 else if (!strcmp(name, "enable-log-filecount"))
139 ctx.cfg.enable_log_filecount = atoi(value); 139 ctx.cfg.enable_log_filecount = atoi(value);
140 else if (!strcmp(name, "enable-log-linecount")) 140 else if (!strcmp(name, "enable-log-linecount"))
141 ctx.cfg.enable_log_linecount = atoi(value); 141 ctx.cfg.enable_log_linecount = atoi(value);
142 else if (!strcmp(name, "enable-remote-branches")) 142 else if (!strcmp(name, "enable-remote-branches"))
143 ctx.cfg.enable_remote_branches = atoi(value); 143 ctx.cfg.enable_remote_branches = atoi(value);
144 else if (!strcmp(name, "enable-tree-linenumbers")) 144 else if (!strcmp(name, "enable-tree-linenumbers"))
145 ctx.cfg.enable_tree_linenumbers = atoi(value); 145 ctx.cfg.enable_tree_linenumbers = atoi(value);
146 else if (!strcmp(name, "max-stats")) 146 else if (!strcmp(name, "max-stats"))
147 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL); 147 ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);
148 else if (!strcmp(name, "cache-size")) 148 else if (!strcmp(name, "cache-size"))
149 ctx.cfg.cache_size = atoi(value); 149 ctx.cfg.cache_size = atoi(value);
150 else if (!strcmp(name, "cache-root")) 150 else if (!strcmp(name, "cache-root"))
151 ctx.cfg.cache_root = xstrdup(value); 151 ctx.cfg.cache_root = xstrdup(expand_macros(value));
152 else if (!strcmp(name, "cache-root-ttl")) 152 else if (!strcmp(name, "cache-root-ttl"))
153 ctx.cfg.cache_root_ttl = atoi(value); 153 ctx.cfg.cache_root_ttl = atoi(value);
154 else if (!strcmp(name, "cache-repo-ttl")) 154 else if (!strcmp(name, "cache-repo-ttl"))
155 ctx.cfg.cache_repo_ttl = atoi(value); 155 ctx.cfg.cache_repo_ttl = atoi(value);
156 else if (!strcmp(name, "cache-scanrc-ttl")) 156 else if (!strcmp(name, "cache-scanrc-ttl"))
157 ctx.cfg.cache_scanrc_ttl = atoi(value); 157 ctx.cfg.cache_scanrc_ttl = atoi(value);
158 else if (!strcmp(name, "cache-static-ttl")) 158 else if (!strcmp(name, "cache-static-ttl"))
159 ctx.cfg.cache_static_ttl = atoi(value); 159 ctx.cfg.cache_static_ttl = atoi(value);
160 else if (!strcmp(name, "cache-dynamic-ttl")) 160 else if (!strcmp(name, "cache-dynamic-ttl"))
161 ctx.cfg.cache_dynamic_ttl = atoi(value); 161 ctx.cfg.cache_dynamic_ttl = atoi(value);
162 else if (!strcmp(name, "about-filter")) 162 else if (!strcmp(name, "about-filter"))
163 ctx.cfg.about_filter = new_filter(value, 0); 163 ctx.cfg.about_filter = new_filter(value, 0);
164 else if (!strcmp(name, "commit-filter")) 164 else if (!strcmp(name, "commit-filter"))
165 ctx.cfg.commit_filter = new_filter(value, 0); 165 ctx.cfg.commit_filter = new_filter(value, 0);
166 else if (!strcmp(name, "embedded")) 166 else if (!strcmp(name, "embedded"))
167 ctx.cfg.embedded = atoi(value); 167 ctx.cfg.embedded = atoi(value);
168 else if (!strcmp(name, "max-message-length")) 168 else if (!strcmp(name, "max-message-length"))
169 ctx.cfg.max_msg_len = atoi(value); 169 ctx.cfg.max_msg_len = atoi(value);
170 else if (!strcmp(name, "max-repodesc-length")) 170 else if (!strcmp(name, "max-repodesc-length"))
171 ctx.cfg.max_repodesc_len = atoi(value); 171 ctx.cfg.max_repodesc_len = atoi(value);
172 else if (!strcmp(name, "max-blob-size")) 172 else if (!strcmp(name, "max-blob-size"))
173 ctx.cfg.max_blob_size = atoi(value); 173 ctx.cfg.max_blob_size = atoi(value);
174 else if (!strcmp(name, "max-repo-count")) 174 else if (!strcmp(name, "max-repo-count"))
175 ctx.cfg.max_repo_count = atoi(value); 175 ctx.cfg.max_repo_count = atoi(value);
176 else if (!strcmp(name, "max-commit-count")) 176 else if (!strcmp(name, "max-commit-count"))
177 ctx.cfg.max_commit_count = atoi(value); 177 ctx.cfg.max_commit_count = atoi(value);
178 else if (!strcmp(name, "scan-path")) 178 else if (!strcmp(name, "scan-path"))
179 if (!ctx.cfg.nocache && ctx.cfg.cache_size) 179 if (!ctx.cfg.nocache && ctx.cfg.cache_size)
180 process_cached_repolist(value); 180 process_cached_repolist(expand_macros(value));
181 else 181 else
182 scan_tree(value, repo_config); 182 scan_tree(expand_macros(value), repo_config);
183 else if (!strcmp(name, "source-filter")) 183 else if (!strcmp(name, "source-filter"))
184 ctx.cfg.source_filter = new_filter(value, 1); 184 ctx.cfg.source_filter = new_filter(value, 1);
185 else if (!strcmp(name, "summary-log")) 185 else if (!strcmp(name, "summary-log"))
186 ctx.cfg.summary_log = atoi(value); 186 ctx.cfg.summary_log = atoi(value);
187 else if (!strcmp(name, "summary-branches")) 187 else if (!strcmp(name, "summary-branches"))
188 ctx.cfg.summary_branches = atoi(value); 188 ctx.cfg.summary_branches = atoi(value);
189 else if (!strcmp(name, "summary-tags")) 189 else if (!strcmp(name, "summary-tags"))
190 ctx.cfg.summary_tags = atoi(value); 190 ctx.cfg.summary_tags = atoi(value);
191 else if (!strcmp(name, "side-by-side-diffs")) 191 else if (!strcmp(name, "side-by-side-diffs"))
192 ctx.cfg.ssdiff = atoi(value); 192 ctx.cfg.ssdiff = atoi(value);
193 else if (!strcmp(name, "agefile")) 193 else if (!strcmp(name, "agefile"))
194 ctx.cfg.agefile = xstrdup(value); 194 ctx.cfg.agefile = xstrdup(value);
195 else if (!strcmp(name, "renamelimit")) 195 else if (!strcmp(name, "renamelimit"))
196 ctx.cfg.renamelimit = atoi(value); 196 ctx.cfg.renamelimit = atoi(value);
197 else if (!strcmp(name, "robots")) 197 else if (!strcmp(name, "robots"))
198 ctx.cfg.robots = xstrdup(value); 198 ctx.cfg.robots = xstrdup(value);
199 else if (!strcmp(name, "clone-prefix")) 199 else if (!strcmp(name, "clone-prefix"))
200 ctx.cfg.clone_prefix = xstrdup(value); 200 ctx.cfg.clone_prefix = xstrdup(value);
201 else if (!strcmp(name, "local-time")) 201 else if (!strcmp(name, "local-time"))
202 ctx.cfg.local_time = atoi(value); 202 ctx.cfg.local_time = atoi(value);
203 else if (!prefixcmp(name, "mimetype.")) 203 else if (!prefixcmp(name, "mimetype."))
204 add_mimetype(name + 9, value); 204 add_mimetype(name + 9, value);
205 else if (!strcmp(name, "include")) 205 else if (!strcmp(name, "include"))
206 parse_configfile(value, config_cb); 206 parse_configfile(expand_macros(value), config_cb);
207} 207}
208 208
209static void querystring_cb(const char *name, const char *value) 209static void querystring_cb(const char *name, const char *value)
210{ 210{
211 if (!value) 211 if (!value)
212 value = ""; 212 value = "";
213 213
214 if (!strcmp(name,"r")) { 214 if (!strcmp(name,"r")) {
215 ctx.qry.repo = xstrdup(value); 215 ctx.qry.repo = xstrdup(value);
216 ctx.repo = cgit_get_repoinfo(value); 216 ctx.repo = cgit_get_repoinfo(value);
217 } else if (!strcmp(name, "p")) { 217 } else if (!strcmp(name, "p")) {
218 ctx.qry.page = xstrdup(value); 218 ctx.qry.page = xstrdup(value);
219 } else if (!strcmp(name, "url")) { 219 } else if (!strcmp(name, "url")) {
220 if (*value == '/') 220 if (*value == '/')
221 value++; 221 value++;
222 ctx.qry.url = xstrdup(value); 222 ctx.qry.url = xstrdup(value);
223 cgit_parse_url(value); 223 cgit_parse_url(value);
224 } else if (!strcmp(name, "qt")) { 224 } else if (!strcmp(name, "qt")) {
225 ctx.qry.grep = xstrdup(value); 225 ctx.qry.grep = xstrdup(value);
226 } else if (!strcmp(name, "q")) { 226 } else if (!strcmp(name, "q")) {
227 ctx.qry.search = xstrdup(value); 227 ctx.qry.search = xstrdup(value);
228 } else if (!strcmp(name, "h")) { 228 } else if (!strcmp(name, "h")) {
229 ctx.qry.head = xstrdup(value); 229 ctx.qry.head = xstrdup(value);
230 ctx.qry.has_symref = 1; 230 ctx.qry.has_symref = 1;
231 } else if (!strcmp(name, "id")) { 231 } else if (!strcmp(name, "id")) {
232 ctx.qry.sha1 = xstrdup(value); 232 ctx.qry.sha1 = xstrdup(value);
233 ctx.qry.has_sha1 = 1; 233 ctx.qry.has_sha1 = 1;
234 } else if (!strcmp(name, "id2")) { 234 } else if (!strcmp(name, "id2")) {
235 ctx.qry.sha2 = xstrdup(value); 235 ctx.qry.sha2 = xstrdup(value);
236 ctx.qry.has_sha1 = 1; 236 ctx.qry.has_sha1 = 1;
237 } else if (!strcmp(name, "ofs")) { 237 } else if (!strcmp(name, "ofs")) {
238 ctx.qry.ofs = atoi(value); 238 ctx.qry.ofs = atoi(value);
239 } else if (!strcmp(name, "path")) { 239 } else if (!strcmp(name, "path")) {
240 ctx.qry.path = trim_end(value, '/'); 240 ctx.qry.path = trim_end(value, '/');
241 } else if (!strcmp(name, "name")) { 241 } else if (!strcmp(name, "name")) {
242 ctx.qry.name = xstrdup(value); 242 ctx.qry.name = xstrdup(value);
243 } else if (!strcmp(name, "mimetype")) { 243 } else if (!strcmp(name, "mimetype")) {
244 ctx.qry.mimetype = xstrdup(value); 244 ctx.qry.mimetype = xstrdup(value);
245 } else if (!strcmp(name, "s")){ 245 } else if (!strcmp(name, "s")){
246 ctx.qry.sort = xstrdup(value); 246 ctx.qry.sort = xstrdup(value);
247 } else if (!strcmp(name, "showmsg")) { 247 } else if (!strcmp(name, "showmsg")) {
248 ctx.qry.showmsg = atoi(value); 248 ctx.qry.showmsg = atoi(value);
249 } else if (!strcmp(name, "period")) { 249 } else if (!strcmp(name, "period")) {
250 ctx.qry.period = xstrdup(value); 250 ctx.qry.period = xstrdup(value);
251 } else if (!strcmp(name, "ss")) { 251 } else if (!strcmp(name, "ss")) {
252 ctx.qry.ssdiff = atoi(value); 252 ctx.qry.ssdiff = atoi(value);
253 } 253 }
254} 254}
255 255
256char *xstrdupn(const char *str) 256char *xstrdupn(const char *str)
257{ 257{
258 return (str ? xstrdup(str) : NULL); 258 return (str ? xstrdup(str) : NULL);
259} 259}
260 260
261static void prepare_context(struct cgit_context *ctx) 261static void prepare_context(struct cgit_context *ctx)
262{ 262{
263 memset(ctx, 0, sizeof(*ctx)); 263 memset(ctx, 0, sizeof(*ctx));
264 ctx->cfg.agefile = "info/web/last-modified"; 264 ctx->cfg.agefile = "info/web/last-modified";
265 ctx->cfg.nocache = 0; 265 ctx->cfg.nocache = 0;
266 ctx->cfg.cache_size = 0; 266 ctx->cfg.cache_size = 0;
267 ctx->cfg.cache_dynamic_ttl = 5; 267 ctx->cfg.cache_dynamic_ttl = 5;
268 ctx->cfg.cache_max_create_time = 5; 268 ctx->cfg.cache_max_create_time = 5;
269 ctx->cfg.cache_repo_ttl = 5; 269 ctx->cfg.cache_repo_ttl = 5;
270 ctx->cfg.cache_root = CGIT_CACHE_ROOT; 270 ctx->cfg.cache_root = CGIT_CACHE_ROOT;
271 ctx->cfg.cache_root_ttl = 5; 271 ctx->cfg.cache_root_ttl = 5;
272 ctx->cfg.cache_scanrc_ttl = 15; 272 ctx->cfg.cache_scanrc_ttl = 15;
273 ctx->cfg.cache_static_ttl = -1; 273 ctx->cfg.cache_static_ttl = -1;
274 ctx->cfg.css = "/cgit.css"; 274 ctx->cfg.css = "/cgit.css";
275 ctx->cfg.logo = "/cgit.png"; 275 ctx->cfg.logo = "/cgit.png";
276 ctx->cfg.local_time = 0; 276 ctx->cfg.local_time = 0;
277 ctx->cfg.enable_tree_linenumbers = 1; 277 ctx->cfg.enable_tree_linenumbers = 1;
278 ctx->cfg.max_repo_count = 50; 278 ctx->cfg.max_repo_count = 50;
279 ctx->cfg.max_commit_count = 50; 279 ctx->cfg.max_commit_count = 50;
280 ctx->cfg.max_lock_attempts = 5; 280 ctx->cfg.max_lock_attempts = 5;
281 ctx->cfg.max_msg_len = 80; 281 ctx->cfg.max_msg_len = 80;
282 ctx->cfg.max_repodesc_len = 80; 282 ctx->cfg.max_repodesc_len = 80;
283 ctx->cfg.max_blob_size = 0; 283 ctx->cfg.max_blob_size = 0;
284 ctx->cfg.max_stats = 0; 284 ctx->cfg.max_stats = 0;
285 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s"; 285 ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";
286 ctx->cfg.renamelimit = -1; 286 ctx->cfg.renamelimit = -1;
287 ctx->cfg.robots = "index, nofollow"; 287 ctx->cfg.robots = "index, nofollow";
288 ctx->cfg.root_title = "Git repository browser"; 288 ctx->cfg.root_title = "Git repository browser";
289 ctx->cfg.root_desc = "a fast webinterface for the git dscm"; 289 ctx->cfg.root_desc = "a fast webinterface for the git dscm";
290 ctx->cfg.script_name = CGIT_SCRIPT_NAME; 290 ctx->cfg.script_name = CGIT_SCRIPT_NAME;
291 ctx->cfg.section = ""; 291 ctx->cfg.section = "";
292 ctx->cfg.summary_branches = 10; 292 ctx->cfg.summary_branches = 10;
293 ctx->cfg.summary_log = 10; 293 ctx->cfg.summary_log = 10;
294 ctx->cfg.summary_tags = 10; 294 ctx->cfg.summary_tags = 10;
295 ctx->cfg.ssdiff = 0; 295 ctx->cfg.ssdiff = 0;
296 ctx->env.cgit_config = xstrdupn(getenv("CGIT_CONFIG")); 296 ctx->env.cgit_config = xstrdupn(getenv("CGIT_CONFIG"));
297 ctx->env.http_host = xstrdupn(getenv("HTTP_HOST")); 297 ctx->env.http_host = xstrdupn(getenv("HTTP_HOST"));
298 ctx->env.https = xstrdupn(getenv("HTTPS")); 298 ctx->env.https = xstrdupn(getenv("HTTPS"));
299 ctx->env.no_http = xstrdupn(getenv("NO_HTTP")); 299 ctx->env.no_http = xstrdupn(getenv("NO_HTTP"));
300 ctx->env.path_info = xstrdupn(getenv("PATH_INFO")); 300 ctx->env.path_info = xstrdupn(getenv("PATH_INFO"));
301 ctx->env.query_string = xstrdupn(getenv("QUERY_STRING")); 301 ctx->env.query_string = xstrdupn(getenv("QUERY_STRING"));
302 ctx->env.request_method = xstrdupn(getenv("REQUEST_METHOD")); 302 ctx->env.request_method = xstrdupn(getenv("REQUEST_METHOD"));
@@ -595,138 +595,138 @@ static void process_cached_repolist(const char *path)
595 * in a child-process to avoid latency for the current request. 595 * in a child-process to avoid latency for the current request.
596 */ 596 */
597 if (fork()) 597 if (fork())
598 return; 598 return;
599 599
600 exit(generate_cached_repolist(path, cached_rc)); 600 exit(generate_cached_repolist(path, cached_rc));
601} 601}
602 602
603static void cgit_parse_args(int argc, const char **argv) 603static void cgit_parse_args(int argc, const char **argv)
604{ 604{
605 int i; 605 int i;
606 int scan = 0; 606 int scan = 0;
607 607
608 for (i = 1; i < argc; i++) { 608 for (i = 1; i < argc; i++) {
609 if (!strncmp(argv[i], "--cache=", 8)) { 609 if (!strncmp(argv[i], "--cache=", 8)) {
610 ctx.cfg.cache_root = xstrdup(argv[i]+8); 610 ctx.cfg.cache_root = xstrdup(argv[i]+8);
611 } 611 }
612 if (!strcmp(argv[i], "--nocache")) { 612 if (!strcmp(argv[i], "--nocache")) {
613 ctx.cfg.nocache = 1; 613 ctx.cfg.nocache = 1;
614 } 614 }
615 if (!strcmp(argv[i], "--nohttp")) { 615 if (!strcmp(argv[i], "--nohttp")) {
616 ctx.env.no_http = "1"; 616 ctx.env.no_http = "1";
617 } 617 }
618 if (!strncmp(argv[i], "--query=", 8)) { 618 if (!strncmp(argv[i], "--query=", 8)) {
619 ctx.qry.raw = xstrdup(argv[i]+8); 619 ctx.qry.raw = xstrdup(argv[i]+8);
620 } 620 }
621 if (!strncmp(argv[i], "--repo=", 7)) { 621 if (!strncmp(argv[i], "--repo=", 7)) {
622 ctx.qry.repo = xstrdup(argv[i]+7); 622 ctx.qry.repo = xstrdup(argv[i]+7);
623 } 623 }
624 if (!strncmp(argv[i], "--page=", 7)) { 624 if (!strncmp(argv[i], "--page=", 7)) {
625 ctx.qry.page = xstrdup(argv[i]+7); 625 ctx.qry.page = xstrdup(argv[i]+7);
626 } 626 }
627 if (!strncmp(argv[i], "--head=", 7)) { 627 if (!strncmp(argv[i], "--head=", 7)) {
628 ctx.qry.head = xstrdup(argv[i]+7); 628 ctx.qry.head = xstrdup(argv[i]+7);
629 ctx.qry.has_symref = 1; 629 ctx.qry.has_symref = 1;
630 } 630 }
631 if (!strncmp(argv[i], "--sha1=", 7)) { 631 if (!strncmp(argv[i], "--sha1=", 7)) {
632 ctx.qry.sha1 = xstrdup(argv[i]+7); 632 ctx.qry.sha1 = xstrdup(argv[i]+7);
633 ctx.qry.has_sha1 = 1; 633 ctx.qry.has_sha1 = 1;
634 } 634 }
635 if (!strncmp(argv[i], "--ofs=", 6)) { 635 if (!strncmp(argv[i], "--ofs=", 6)) {
636 ctx.qry.ofs = atoi(argv[i]+6); 636 ctx.qry.ofs = atoi(argv[i]+6);
637 } 637 }
638 if (!strncmp(argv[i], "--scan-tree=", 12) || 638 if (!strncmp(argv[i], "--scan-tree=", 12) ||
639 !strncmp(argv[i], "--scan-path=", 12)) { 639 !strncmp(argv[i], "--scan-path=", 12)) {
640 /* HACK: the global snapshot bitmask defines the 640 /* HACK: the global snapshot bitmask defines the
641 * set of allowed snapshot formats, but the config 641 * set of allowed snapshot formats, but the config
642 * file hasn't been parsed yet so the mask is 642 * file hasn't been parsed yet so the mask is
643 * currently 0. By setting all bits high before 643 * currently 0. By setting all bits high before
644 * scanning we make sure that any in-repo cgitrc 644 * scanning we make sure that any in-repo cgitrc
645 * snapshot setting is respected by scan_tree(). 645 * snapshot setting is respected by scan_tree().
646 * BTW: we assume that there'll never be more than 646 * BTW: we assume that there'll never be more than
647 * 255 different snapshot formats supported by cgit... 647 * 255 different snapshot formats supported by cgit...
648 */ 648 */
649 ctx.cfg.snapshots = 0xFF; 649 ctx.cfg.snapshots = 0xFF;
650 scan++; 650 scan++;
651 scan_tree(argv[i] + 12, repo_config); 651 scan_tree(argv[i] + 12, repo_config);
652 } 652 }
653 } 653 }
654 if (scan) { 654 if (scan) {
655 qsort(cgit_repolist.repos, cgit_repolist.count, 655 qsort(cgit_repolist.repos, cgit_repolist.count,
656 sizeof(struct cgit_repo), cmp_repos); 656 sizeof(struct cgit_repo), cmp_repos);
657 print_repolist(stdout, &cgit_repolist, 0); 657 print_repolist(stdout, &cgit_repolist, 0);
658 exit(0); 658 exit(0);
659 } 659 }
660} 660}
661 661
662static int calc_ttl() 662static int calc_ttl()
663{ 663{
664 if (!ctx.repo) 664 if (!ctx.repo)
665 return ctx.cfg.cache_root_ttl; 665 return ctx.cfg.cache_root_ttl;
666 666
667 if (!ctx.qry.page) 667 if (!ctx.qry.page)
668 return ctx.cfg.cache_repo_ttl; 668 return ctx.cfg.cache_repo_ttl;
669 669
670 if (ctx.qry.has_symref) 670 if (ctx.qry.has_symref)
671 return ctx.cfg.cache_dynamic_ttl; 671 return ctx.cfg.cache_dynamic_ttl;
672 672
673 if (ctx.qry.has_sha1) 673 if (ctx.qry.has_sha1)
674 return ctx.cfg.cache_static_ttl; 674 return ctx.cfg.cache_static_ttl;
675 675
676 return ctx.cfg.cache_repo_ttl; 676 return ctx.cfg.cache_repo_ttl;
677} 677}
678 678
679int main(int argc, const char **argv) 679int main(int argc, const char **argv)
680{ 680{
681 const char *path; 681 const char *path;
682 char *qry; 682 char *qry;
683 int err, ttl; 683 int err, ttl;
684 684
685 prepare_context(&ctx); 685 prepare_context(&ctx);
686 cgit_repolist.length = 0; 686 cgit_repolist.length = 0;
687 cgit_repolist.count = 0; 687 cgit_repolist.count = 0;
688 cgit_repolist.repos = NULL; 688 cgit_repolist.repos = NULL;
689 689
690 cgit_parse_args(argc, argv); 690 cgit_parse_args(argc, argv);
691 parse_configfile(ctx.env.cgit_config, config_cb); 691 parse_configfile(expand_macros(ctx.env.cgit_config), config_cb);
692 ctx.repo = NULL; 692 ctx.repo = NULL;
693 http_parse_querystring(ctx.qry.raw, querystring_cb); 693 http_parse_querystring(ctx.qry.raw, querystring_cb);
694 694
695 /* If virtual-root isn't specified in cgitrc, lets pretend 695 /* If virtual-root isn't specified in cgitrc, lets pretend
696 * that virtual-root equals SCRIPT_NAME. 696 * that virtual-root equals SCRIPT_NAME.
697 */ 697 */
698 if (!ctx.cfg.virtual_root) 698 if (!ctx.cfg.virtual_root)
699 ctx.cfg.virtual_root = ctx.cfg.script_name; 699 ctx.cfg.virtual_root = ctx.cfg.script_name;
700 700
701 /* If no url parameter is specified on the querystring, lets 701 /* If no url parameter is specified on the querystring, lets
702 * use PATH_INFO as url. This allows cgit to work with virtual 702 * use PATH_INFO as url. This allows cgit to work with virtual
703 * urls without the need for rewriterules in the webserver (as 703 * urls without the need for rewriterules in the webserver (as
704 * long as PATH_INFO is included in the cache lookup key). 704 * long as PATH_INFO is included in the cache lookup key).
705 */ 705 */
706 path = ctx.env.path_info; 706 path = ctx.env.path_info;
707 if (!ctx.qry.url && path) { 707 if (!ctx.qry.url && path) {
708 if (path[0] == '/') 708 if (path[0] == '/')
709 path++; 709 path++;
710 ctx.qry.url = xstrdup(path); 710 ctx.qry.url = xstrdup(path);
711 if (ctx.qry.raw) { 711 if (ctx.qry.raw) {
712 qry = ctx.qry.raw; 712 qry = ctx.qry.raw;
713 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); 713 ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry));
714 free(qry); 714 free(qry);
715 } else 715 } else
716 ctx.qry.raw = xstrdup(ctx.qry.url); 716 ctx.qry.raw = xstrdup(ctx.qry.url);
717 cgit_parse_url(ctx.qry.url); 717 cgit_parse_url(ctx.qry.url);
718 } 718 }
719 719
720 ttl = calc_ttl(); 720 ttl = calc_ttl();
721 ctx.page.expires += ttl*60; 721 ctx.page.expires += ttl*60;
722 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) 722 if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD"))
723 ctx.cfg.nocache = 1; 723 ctx.cfg.nocache = 1;
724 if (ctx.cfg.nocache) 724 if (ctx.cfg.nocache)
725 ctx.cfg.cache_size = 0; 725 ctx.cfg.cache_size = 0;
726 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, 726 err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root,
727 ctx.qry.raw, ttl, process_request, &ctx); 727 ctx.qry.raw, ttl, process_request, &ctx);
728 if (err) 728 if (err)
729 cgit_print_error(fmt("Error processing page: %s (%d)", 729 cgit_print_error(fmt("Error processing page: %s (%d)",
730 strerror(err), err)); 730 strerror(err), err));
731 return err; 731 return err;
732} 732}
diff --git a/cgit.h b/cgit.h
index cd4af72..568e0c0 100644
--- a/cgit.h
+++ b/cgit.h
@@ -203,97 +203,99 @@ struct cgit_config {
203 struct cgit_filter *about_filter; 203 struct cgit_filter *about_filter;
204 struct cgit_filter *commit_filter; 204 struct cgit_filter *commit_filter;
205 struct cgit_filter *source_filter; 205 struct cgit_filter *source_filter;
206}; 206};
207 207
208struct cgit_page { 208struct cgit_page {
209 time_t modified; 209 time_t modified;
210 time_t expires; 210 time_t expires;
211 size_t size; 211 size_t size;
212 char *mimetype; 212 char *mimetype;
213 char *charset; 213 char *charset;
214 char *filename; 214 char *filename;
215 char *etag; 215 char *etag;
216 char *title; 216 char *title;
217 int status; 217 int status;
218 char *statusmsg; 218 char *statusmsg;
219}; 219};
220 220
221struct cgit_environment { 221struct cgit_environment {
222 char *cgit_config; 222 char *cgit_config;
223 char *http_host; 223 char *http_host;
224 char *https; 224 char *https;
225 char *no_http; 225 char *no_http;
226 char *path_info; 226 char *path_info;
227 char *query_string; 227 char *query_string;
228 char *request_method; 228 char *request_method;
229 char *script_name; 229 char *script_name;
230 char *server_name; 230 char *server_name;
231 char *server_port; 231 char *server_port;
232}; 232};
233 233
234struct cgit_context { 234struct cgit_context {
235 struct cgit_environment env; 235 struct cgit_environment env;
236 struct cgit_query qry; 236 struct cgit_query qry;
237 struct cgit_config cfg; 237 struct cgit_config cfg;
238 struct cgit_repo *repo; 238 struct cgit_repo *repo;
239 struct cgit_page page; 239 struct cgit_page page;
240}; 240};
241 241
242struct cgit_snapshot_format { 242struct cgit_snapshot_format {
243 const char *suffix; 243 const char *suffix;
244 const char *mimetype; 244 const char *mimetype;
245 write_archive_fn_t write_func; 245 write_archive_fn_t write_func;
246 int bit; 246 int bit;
247}; 247};
248 248
249extern const char *cgit_version; 249extern const char *cgit_version;
250 250
251extern struct cgit_repolist cgit_repolist; 251extern struct cgit_repolist cgit_repolist;
252extern struct cgit_context ctx; 252extern struct cgit_context ctx;
253extern const struct cgit_snapshot_format cgit_snapshot_formats[]; 253extern const struct cgit_snapshot_format cgit_snapshot_formats[];
254 254
255extern struct cgit_repo *cgit_add_repo(const char *url); 255extern struct cgit_repo *cgit_add_repo(const char *url);
256extern struct cgit_repo *cgit_get_repoinfo(const char *url); 256extern struct cgit_repo *cgit_get_repoinfo(const char *url);
257extern void cgit_repo_config_cb(const char *name, const char *value); 257extern void cgit_repo_config_cb(const char *name, const char *value);
258 258
259extern int chk_zero(int result, char *msg); 259extern int chk_zero(int result, char *msg);
260extern int chk_positive(int result, char *msg); 260extern int chk_positive(int result, char *msg);
261extern int chk_non_negative(int result, char *msg); 261extern int chk_non_negative(int result, char *msg);
262 262
263extern char *trim_end(const char *str, char c); 263extern char *trim_end(const char *str, char c);
264extern char *strlpart(char *txt, int maxlen); 264extern char *strlpart(char *txt, int maxlen);
265extern char *strrpart(char *txt, int maxlen); 265extern char *strrpart(char *txt, int maxlen);
266 266
267extern void cgit_add_ref(struct reflist *list, struct refinfo *ref); 267extern void cgit_add_ref(struct reflist *list, struct refinfo *ref);
268extern int cgit_refs_cb(const char *refname, const unsigned char *sha1, 268extern int cgit_refs_cb(const char *refname, const unsigned char *sha1,
269 int flags, void *cb_data); 269 int flags, void *cb_data);
270 270
271extern void *cgit_free_commitinfo(struct commitinfo *info); 271extern void *cgit_free_commitinfo(struct commitinfo *info);
272 272
273extern int cgit_diff_files(const unsigned char *old_sha1, 273extern int cgit_diff_files(const unsigned char *old_sha1,
274 const unsigned char *new_sha1, 274 const unsigned char *new_sha1,
275 unsigned long *old_size, unsigned long *new_size, 275 unsigned long *old_size, unsigned long *new_size,
276 int *binary, linediff_fn fn); 276 int *binary, linediff_fn fn);
277 277
278extern void cgit_diff_tree(const unsigned char *old_sha1, 278extern void cgit_diff_tree(const unsigned char *old_sha1,
279 const unsigned char *new_sha1, 279 const unsigned char *new_sha1,
280 filepair_fn fn, const char *prefix); 280 filepair_fn fn, const char *prefix);
281 281
282extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); 282extern void cgit_diff_commit(struct commit *commit, filepair_fn fn);
283 283
284extern char *fmt(const char *format,...); 284extern char *fmt(const char *format,...);
285 285
286extern struct commitinfo *cgit_parse_commit(struct commit *commit); 286extern struct commitinfo *cgit_parse_commit(struct commit *commit);
287extern struct taginfo *cgit_parse_tag(struct tag *tag); 287extern struct taginfo *cgit_parse_tag(struct tag *tag);
288extern void cgit_parse_url(const char *url); 288extern void cgit_parse_url(const char *url);
289 289
290extern const char *cgit_repobasename(const char *reponame); 290extern const char *cgit_repobasename(const char *reponame);
291 291
292extern int cgit_parse_snapshots_mask(const char *str); 292extern int cgit_parse_snapshots_mask(const char *str);
293 293
294extern int cgit_open_filter(struct cgit_filter *filter); 294extern int cgit_open_filter(struct cgit_filter *filter);
295extern int cgit_close_filter(struct cgit_filter *filter); 295extern int cgit_close_filter(struct cgit_filter *filter);
296 296
297extern int readfile(const char *path, char **buf, size_t *size); 297extern int readfile(const char *path, char **buf, size_t *size);
298 298
299extern char *expand_macros(const char *txt);
300
299#endif /* CGIT_H */ 301#endif /* CGIT_H */
diff --git a/shared.c b/shared.c
index 5f46793..76d26dd 100644
--- a/shared.c
+++ b/shared.c
@@ -330,96 +330,167 @@ void cgit_diff_commit(struct commit *commit, filepair_fn fn)
330 unsigned char *old_sha1 = NULL; 330 unsigned char *old_sha1 = NULL;
331 331
332 if (commit->parents) 332 if (commit->parents)
333 old_sha1 = commit->parents->item->object.sha1; 333 old_sha1 = commit->parents->item->object.sha1;
334 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL); 334 cgit_diff_tree(old_sha1, commit->object.sha1, fn, NULL);
335} 335}
336 336
337int cgit_parse_snapshots_mask(const char *str) 337int cgit_parse_snapshots_mask(const char *str)
338{ 338{
339 const struct cgit_snapshot_format *f; 339 const struct cgit_snapshot_format *f;
340 static const char *delim = " \t,:/|;"; 340 static const char *delim = " \t,:/|;";
341 int tl, sl, rv = 0; 341 int tl, sl, rv = 0;
342 342
343 /* favor legacy setting */ 343 /* favor legacy setting */
344 if(atoi(str)) 344 if(atoi(str))
345 return 1; 345 return 1;
346 for(;;) { 346 for(;;) {
347 str += strspn(str,delim); 347 str += strspn(str,delim);
348 tl = strcspn(str,delim); 348 tl = strcspn(str,delim);
349 if (!tl) 349 if (!tl)
350 break; 350 break;
351 for (f = cgit_snapshot_formats; f->suffix; f++) { 351 for (f = cgit_snapshot_formats; f->suffix; f++) {
352 sl = strlen(f->suffix); 352 sl = strlen(f->suffix);
353 if((tl == sl && !strncmp(f->suffix, str, tl)) || 353 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
354 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) { 354 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
355 rv |= f->bit; 355 rv |= f->bit;
356 break; 356 break;
357 } 357 }
358 } 358 }
359 str += tl; 359 str += tl;
360 } 360 }
361 return rv; 361 return rv;
362} 362}
363 363
364int cgit_open_filter(struct cgit_filter *filter) 364int cgit_open_filter(struct cgit_filter *filter)
365{ 365{
366 366
367 filter->old_stdout = chk_positive(dup(STDOUT_FILENO), 367 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
368 "Unable to duplicate STDOUT"); 368 "Unable to duplicate STDOUT");
369 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); 369 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
370 filter->pid = chk_non_negative(fork(), "Unable to create subprocess"); 370 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
371 if (filter->pid == 0) { 371 if (filter->pid == 0) {
372 close(filter->pipe_fh[1]); 372 close(filter->pipe_fh[1]);
373 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), 373 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
374 "Unable to use pipe as STDIN"); 374 "Unable to use pipe as STDIN");
375 execvp(filter->cmd, filter->argv); 375 execvp(filter->cmd, filter->argv);
376 die("Unable to exec subprocess %s: %s (%d)", filter->cmd, 376 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
377 strerror(errno), errno); 377 strerror(errno), errno);
378 } 378 }
379 close(filter->pipe_fh[0]); 379 close(filter->pipe_fh[0]);
380 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO), 380 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
381 "Unable to use pipe as STDOUT"); 381 "Unable to use pipe as STDOUT");
382 close(filter->pipe_fh[1]); 382 close(filter->pipe_fh[1]);
383 return 0; 383 return 0;
384} 384}
385 385
386int cgit_close_filter(struct cgit_filter *filter) 386int cgit_close_filter(struct cgit_filter *filter)
387{ 387{
388 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), 388 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
389 "Unable to restore STDOUT"); 389 "Unable to restore STDOUT");
390 close(filter->old_stdout); 390 close(filter->old_stdout);
391 if (filter->pid < 0) 391 if (filter->pid < 0)
392 return 0; 392 return 0;
393 waitpid(filter->pid, &filter->exitstatus, 0); 393 waitpid(filter->pid, &filter->exitstatus, 0);
394 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus)) 394 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
395 return 0; 395 return 0;
396 die("Subprocess %s exited abnormally", filter->cmd); 396 die("Subprocess %s exited abnormally", filter->cmd);
397} 397}
398 398
399/* Read the content of the specified file into a newly allocated buffer, 399/* Read the content of the specified file into a newly allocated buffer,
400 * zeroterminate the buffer and return 0 on success, errno otherwise. 400 * zeroterminate the buffer and return 0 on success, errno otherwise.
401 */ 401 */
402int readfile(const char *path, char **buf, size_t *size) 402int readfile(const char *path, char **buf, size_t *size)
403{ 403{
404 int fd, e; 404 int fd, e;
405 struct stat st; 405 struct stat st;
406 406
407 fd = open(path, O_RDONLY); 407 fd = open(path, O_RDONLY);
408 if (fd == -1) 408 if (fd == -1)
409 return errno; 409 return errno;
410 if (fstat(fd, &st)) { 410 if (fstat(fd, &st)) {
411 e = errno; 411 e = errno;
412 close(fd); 412 close(fd);
413 return e; 413 return e;
414 } 414 }
415 if (!S_ISREG(st.st_mode)) { 415 if (!S_ISREG(st.st_mode)) {
416 close(fd); 416 close(fd);
417 return EISDIR; 417 return EISDIR;
418 } 418 }
419 *buf = xmalloc(st.st_size + 1); 419 *buf = xmalloc(st.st_size + 1);
420 *size = read_in_full(fd, *buf, st.st_size); 420 *size = read_in_full(fd, *buf, st.st_size);
421 e = errno; 421 e = errno;
422 (*buf)[*size] = '\0'; 422 (*buf)[*size] = '\0';
423 close(fd); 423 close(fd);
424 return (*size == st.st_size ? 0 : e); 424 return (*size == st.st_size ? 0 : e);
425} 425}
426
427int is_token_char(char c)
428{
429 return isalnum(c) || c == '_';
430}
431
432/* Replace name with getenv(name), return pointer to zero-terminating char
433 */
434char *expand_macro(char *name, int maxlength)
435{
436 char *value;
437 int len;
438
439 len = 0;
440 value = getenv(name);
441 if (value) {
442 len = strlen(value);
443 if (len > maxlength)
444 len = maxlength;
445 strncpy(name, value, len);
446 }
447 return name + len;
448}
449
450#define EXPBUFSIZE (1024 * 8)
451
452/* Replace all tokens prefixed by '$' in the specified text with the
453 * value of the named environment variable.
454 * NB: the return value is a static buffer, i.e. it must be strdup'd
455 * by the caller.
456 */
457char *expand_macros(const char *txt)
458{
459 static char result[EXPBUFSIZE];
460 char *p, *start;
461 int len;
462
463 p = result;
464 start = NULL;
465 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
466 *p = *txt;
467 if (start) {
468 if (!is_token_char(*txt)) {
469 if (p - start > 0) {
470 *p = '\0';
471 len = result + EXPBUFSIZE - start - 1;
472 p = expand_macro(start, len) - 1;
473 }
474 start = NULL;
475 txt--;
476 }
477 p++;
478 txt++;
479 continue;
480 }
481 if (*txt == '$') {
482 start = p;
483 txt++;
484 continue;
485 }
486 p++;
487 txt++;
488 }
489 *p = '\0';
490 if (start && p - start > 0) {
491 len = result + EXPBUFSIZE - start - 1;
492 p = expand_macro(start, len);
493 *p = '\0';
494 }
495 return result;
496}