blob: 919a99ef6629549fd597aa8154f78737306f1d03 [file] [log] [blame]
Lars Hjemlia1a79992006-12-16 01:14:01 +01001/* shared.c: global vars + some callback functions
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemli44923f82006-12-11 17:25:41 +01009#include "cgit.h"
Ferry Huberts14f28922011-03-23 11:57:43 +010010#include <stdio.h>
Lars Hjemli44923f82006-12-11 17:25:41 +010011
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010012struct cgit_repolist cgit_repolist;
Lars Hjemlid14d77f2008-02-16 11:53:40 +010013struct cgit_context ctx;
Lars Hjemlice1c7332007-02-03 15:02:55 +010014
Lars Hjemliab2ab952007-02-08 13:53:13 +010015int chk_zero(int result, char *msg)
16{
17 if (result != 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010018 die_errno("%s", msg);
Lars Hjemliab2ab952007-02-08 13:53:13 +010019 return result;
20}
21
22int chk_positive(int result, char *msg)
23{
24 if (result <= 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010025 die_errno("%s", msg);
Lars Hjemliab2ab952007-02-08 13:53:13 +010026 return result;
27}
28
Michael Krelin127f43d2007-07-20 20:56:43 +020029int chk_non_negative(int result, char *msg)
30{
Lukas Fleischer53bc7472013-03-03 16:04:29 +010031 if (result < 0)
John Keeping1e9f1ee2013-05-18 16:21:36 +010032 die_errno("%s", msg);
Michael Krelin127f43d2007-07-20 20:56:43 +020033 return result;
34}
35
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040036char *cgit_default_repo_desc = "[no description]";
Lars Hjemli163037e2008-03-24 17:26:08 +010037struct cgit_repo *cgit_add_repo(const char *url)
Lars Hjemlice1c7332007-02-03 15:02:55 +010038{
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010039 struct cgit_repo *ret;
Lars Hjemlice1c7332007-02-03 15:02:55 +010040
41 if (++cgit_repolist.count > cgit_repolist.length) {
42 if (cgit_repolist.length == 0)
43 cgit_repolist.length = 8;
44 else
45 cgit_repolist.length *= 2;
Lars Hjemli1b49de32007-05-13 11:24:23 +020046 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
47 cgit_repolist.length *
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010048 sizeof(struct cgit_repo));
Lars Hjemlice1c7332007-02-03 15:02:55 +010049 }
50
51 ret = &cgit_repolist.repos[cgit_repolist.count-1];
Lars Hjemli42e7a162009-08-24 10:14:02 +020052 memset(ret, 0, sizeof(struct cgit_repo));
Lars Hjemli4e40d852007-09-20 00:56:53 +020053 ret->url = trim_end(url, '/');
Lars Hjemlice1c7332007-02-03 15:02:55 +010054 ret->name = ret->url;
55 ret->path = NULL;
Jason A. Donenfeld521e10c2012-10-09 06:56:14 -040056 ret->desc = cgit_default_repo_desc;
Lars Hjemlice1c7332007-02-03 15:02:55 +010057 ret->owner = NULL;
Lars Hjemlie7af0022009-08-23 22:58:39 +020058 ret->section = ctx.cfg.section;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010059 ret->snapshots = ctx.cfg.snapshots;
Johan Herland9a8d39c2010-11-15 18:39:50 +010060 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010061 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
62 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
Lars Hjemli41934a32009-11-07 19:10:58 +010063 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
Lars Hjemli581a0c22010-02-27 13:12:55 +010064 ret->enable_subject_links = ctx.cfg.enable_subject_links;
Lars Hjemlifb2f3f62008-12-07 13:17:21 +010065 ret->max_stats = ctx.cfg.max_stats;
Jason A. Donenfeld389cc172013-04-08 16:57:12 +020066 ret->branch_sort = ctx.cfg.branch_sort;
Tobias Bieniek792f8132012-10-13 16:10:30 +020067 ret->commit_sort = ctx.cfg.commit_sort;
Lars Hjemlib228d4f2008-02-16 13:07:13 +010068 ret->module_link = ctx.cfg.module_link;
Lars Hjemli515edb02010-08-21 15:08:01 +020069 ret->readme = ctx.cfg.readme;
Lars Hjemli88131702008-11-29 16:46:37 +010070 ret->mtime = -1;
Lars Hjemli537c05f2009-08-09 13:27:21 +020071 ret->about_filter = ctx.cfg.about_filter;
Lars Hjemlie976df22009-08-09 13:22:00 +020072 ret->commit_filter = ctx.cfg.commit_filter;
73 ret->source_filter = ctx.cfg.source_filter;
Lars Hjemlia1429db2011-06-06 20:49:13 +000074 ret->clone_url = ctx.cfg.clone_url;
Lars Hjemli6857bec2011-06-15 10:04:13 +020075 ret->submodules.strdup_strings = 1;
Lars Hjemlice1c7332007-02-03 15:02:55 +010076 return ret;
77}
78
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010079struct cgit_repo *cgit_get_repoinfo(const char *url)
Lars Hjemli305414d2007-05-18 00:47:47 +020080{
81 int i;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010082 struct cgit_repo *repo;
Lars Hjemli305414d2007-05-18 00:47:47 +020083
Lukas Fleischer53bc7472013-03-03 16:04:29 +010084 for (i = 0; i < cgit_repolist.count; i++) {
Lars Hjemli305414d2007-05-18 00:47:47 +020085 repo = &cgit_repolist.repos[i];
86 if (!strcmp(repo->url, url))
87 return repo;
88 }
89 return NULL;
90}
91
Lars Hjemliaaa24bd2006-12-16 14:58:20 +010092void *cgit_free_commitinfo(struct commitinfo *info)
93{
94 free(info->author);
95 free(info->author_email);
96 free(info->committer);
97 free(info->committer_email);
98 free(info->subject);
Jonathan Bastien-Filiatrault3845e172007-10-26 18:09:06 -040099 free(info->msg);
100 free(info->msg_encoding);
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100101 free(info);
102 return NULL;
103}
Lars Hjemli52e605c2007-01-04 16:53:03 +0100104
Lars Hjemli382805e2007-06-26 18:04:31 +0200105char *trim_end(const char *str, char c)
106{
107 int len;
Lars Hjemli382805e2007-06-26 18:04:31 +0200108
109 if (str == NULL)
110 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200111 len = strlen(str);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500112 while (len > 0 && str[len - 1] == c)
Lars Hjemli382805e2007-06-26 18:04:31 +0200113 len--;
Lars Hjemli382805e2007-06-26 18:04:31 +0200114 if (len == 0)
115 return NULL;
Lars Hjemlidc1a8ea2011-05-22 12:45:32 +0200116 return xstrndup(str, len);
Lars Hjemli382805e2007-06-26 18:04:31 +0200117}
118
John Keepingb1f17f12013-04-01 19:03:34 +0100119char *ensure_end(const char *str, char c)
120{
121 size_t len = strlen(str);
122 char *result;
123
124 if (len && str[len - 1] == c)
125 return xstrndup(str, len);
126
127 result = xmalloc(len + 2);
128 memcpy(result, str, len);
129 result[len] = '/';
130 result[len + 1] = '\0';
131 return result;
132}
133
John Keepingd2e20e32013-04-07 14:03:47 +0100134void strbuf_ensure_end(struct strbuf *sb, char c)
135{
136 if (!sb->len || sb->buf[sb->len - 1] != c)
137 strbuf_addch(sb, c);
138}
139
Lars Hjemli0c8e1842007-10-30 10:47:38 +0100140char *strlpart(char *txt, int maxlen)
141{
142 char *result;
143
144 if (!txt)
145 return txt;
146
147 if (strlen(txt) <= maxlen)
148 return txt;
149 result = xmalloc(maxlen + 1);
150 memcpy(result, txt, maxlen - 3);
151 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
152 result[maxlen] = '\0';
153 return result;
154}
155
156char *strrpart(char *txt, int maxlen)
157{
158 char *result;
159
160 if (!txt)
161 return txt;
162
163 if (strlen(txt) <= maxlen)
164 return txt;
165 result = xmalloc(maxlen + 1);
166 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
167 result[0] = result[1] = result[2] = '.';
168 return result;
169}
170
Lars Hjemlie397ff72007-10-25 09:30:06 +0200171void cgit_add_ref(struct reflist *list, struct refinfo *ref)
172{
173 size_t size;
174
175 if (list->count >= list->alloc) {
176 list->alloc += (list->alloc ? list->alloc : 4);
177 size = list->alloc * sizeof(struct refinfo *);
178 list->refs = xrealloc(list->refs, size);
179 }
180 list->refs[list->count++] = ref;
181}
182
Lukas Fleischerbafab422013-03-04 08:52:33 +0100183static struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
Lars Hjemlie397ff72007-10-25 09:30:06 +0200184{
185 struct refinfo *ref;
186
187 ref = xmalloc(sizeof (struct refinfo));
188 ref->refname = xstrdup(refname);
189 ref->object = parse_object(sha1);
190 switch (ref->object->type) {
191 case OBJ_TAG:
192 ref->tag = cgit_parse_tag((struct tag *)ref->object);
193 break;
194 case OBJ_COMMIT:
195 ref->commit = cgit_parse_commit((struct commit *)ref->object);
196 break;
197 }
198 return ref;
199}
200
Lukas Fleischer1268afe2013-03-04 13:25:33 +0100201static void cgit_free_taginfo(struct taginfo *tag)
202{
203 if (tag->tagger)
204 free(tag->tagger);
205 if (tag->tagger_email)
206 free(tag->tagger_email);
207 if (tag->msg)
208 free(tag->msg);
209 free(tag);
210}
211
212static void cgit_free_refinfo(struct refinfo *ref)
213{
214 if (ref->refname)
215 free((char *)ref->refname);
216 switch (ref->object->type) {
217 case OBJ_TAG:
218 cgit_free_taginfo(ref->tag);
219 break;
220 case OBJ_COMMIT:
221 cgit_free_commitinfo(ref->commit);
222 break;
223 }
224 free(ref);
225}
226
227void cgit_free_reflist_inner(struct reflist *list)
228{
229 int i;
230
231 for (i = 0; i < list->count; i++) {
232 cgit_free_refinfo(list->refs[i]);
233 }
234 free(list->refs);
235}
236
Lars Hjemlie397ff72007-10-25 09:30:06 +0200237int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
238 void *cb_data)
239{
240 struct reflist *list = (struct reflist *)cb_data;
241 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
242
243 if (info)
244 cgit_add_ref(list, info);
245 return 0;
246}
247
Lukas Fleischerbafab422013-03-04 08:52:33 +0100248static void cgit_diff_tree_cb(struct diff_queue_struct *q,
249 struct diff_options *options, void *data)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200250{
251 int i;
252
253 for (i = 0; i < q->nr; i++) {
254 if (q->queue[i]->status == 'U')
255 continue;
256 ((filepair_fn)data)(q->queue[i]);
257 }
258}
259
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200260static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
261{
262 enum object_type type;
263
264 if (is_null_sha1(sha1)) {
265 file->ptr = (char *)"";
266 file->size = 0;
267 } else {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100268 file->ptr = read_sha1_file(sha1, &type,
Lars Hjemli0835ffe2007-09-20 00:21:47 +0200269 (unsigned long *)&file->size);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200270 }
271 return 1;
272}
273
274/*
275 * Receive diff-buffers from xdiff and concatenate them as
276 * needed across multiple callbacks.
277 *
278 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
279 * ripped from git and modified to use globals instead of
280 * a special callback-struct.
281 */
282char *diffbuf = NULL;
283int buflen = 0;
284
Lukas Fleischerbafab422013-03-04 08:52:33 +0100285static int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200286{
287 int i;
288
289 for (i = 0; i < nbuf; i++) {
290 if (mb[i].ptr[mb[i].size-1] != '\n') {
291 /* Incomplete line */
292 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
293 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
294 buflen += mb[i].size;
295 continue;
296 }
297
298 /* we have a complete line */
299 if (!diffbuf) {
300 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
301 continue;
302 }
303 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
304 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
305 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
306 free(diffbuf);
307 diffbuf = NULL;
308 buflen = 0;
309 }
310 if (diffbuf) {
311 ((linediff_fn)priv)(diffbuf, buflen);
312 free(diffbuf);
313 diffbuf = NULL;
314 buflen = 0;
315 }
316 return 0;
317}
318
319int cgit_diff_files(const unsigned char *old_sha1,
Lars Hjemlic495cf02009-01-31 10:40:40 +0100320 const unsigned char *new_sha1, unsigned long *old_size,
Johan Herland6180e612010-06-10 20:15:27 +0200321 unsigned long *new_size, int *binary, int context,
Johan Herland2cc8b992010-06-24 17:52:57 +0200322 int ignorews, linediff_fn fn)
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200323{
324 mmfile_t file1, file2;
325 xpparam_t diff_params;
326 xdemitconf_t emit_params;
327 xdemitcb_t emit_cb;
328
329 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
330 return 1;
331
Lars Hjemlic495cf02009-01-31 10:40:40 +0100332 *old_size = file1.size;
333 *new_size = file2.size;
334
Lars Hjemli481ce5e2009-02-01 19:29:24 +0100335 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
336 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
Lars Hjemlic495cf02009-01-31 10:40:40 +0100337 *binary = 1;
Lars Hjemlice761fd2010-04-08 00:48:36 +0200338 if (file1.size)
339 free(file1.ptr);
340 if (file2.size)
341 free(file2.ptr);
Lars Hjemlic495cf02009-01-31 10:40:40 +0100342 return 0;
343 }
344
Lars Hjemli0edf7602008-12-26 11:02:02 +0100345 memset(&diff_params, 0, sizeof(diff_params));
346 memset(&emit_params, 0, sizeof(emit_params));
347 memset(&emit_cb, 0, sizeof(emit_cb));
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200348 diff_params.flags = XDF_NEED_MINIMAL;
Johan Herland2cc8b992010-06-24 17:52:57 +0200349 if (ignorews)
350 diff_params.flags |= XDF_IGNORE_WHITESPACE;
Johan Herland6180e612010-06-10 20:15:27 +0200351 emit_params.ctxlen = context > 0 ? context : 3;
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200352 emit_params.flags = XDL_EMIT_FUNCNAMES;
353 emit_cb.outf = filediff_cb;
354 emit_cb.priv = fn;
355 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
Lars Hjemlice761fd2010-04-08 00:48:36 +0200356 if (file1.size)
357 free(file1.ptr);
358 if (file2.size)
359 free(file2.ptr);
Lars Hjemlic4ef6672007-05-13 14:21:19 +0200360 return 0;
361}
362
Lars Hjemli1b49de32007-05-13 11:24:23 +0200363void cgit_diff_tree(const unsigned char *old_sha1,
364 const unsigned char *new_sha1,
Johan Herland2cc8b992010-06-24 17:52:57 +0200365 filepair_fn fn, const char *prefix, int ignorews)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200366{
367 struct diff_options opt;
John Keepingbfc14d02013-03-02 12:32:10 +0000368 struct pathspec_item item;
Lars Hjemli1b49de32007-05-13 11:24:23 +0200369
370 diff_setup(&opt);
371 opt.output_format = DIFF_FORMAT_CALLBACK;
372 opt.detect_rename = 1;
Lars Hjemlib228d4f2008-02-16 13:07:13 +0100373 opt.rename_limit = ctx.cfg.renamelimit;
Lars Hjemli776200b2008-01-13 19:16:23 +0100374 DIFF_OPT_SET(&opt, RECURSIVE);
Johan Herland2cc8b992010-06-24 17:52:57 +0200375 if (ignorews)
376 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200377 opt.format_callback = cgit_diff_tree_cb;
378 opt.format_callback_data = fn;
Lars Hjemlif527a572007-10-01 11:42:19 +0200379 if (prefix) {
John Keepingbfc14d02013-03-02 12:32:10 +0000380 item.match = prefix;
381 item.len = strlen(prefix);
382 opt.pathspec.nr = 1;
383 opt.pathspec.items = &item;
Lars Hjemlif527a572007-10-01 11:42:19 +0200384 }
Lars Hjemli1b49de32007-05-13 11:24:23 +0200385 diff_setup_done(&opt);
386
Lars Hjemli4a0be582007-06-17 18:12:03 +0200387 if (old_sha1 && !is_null_sha1(old_sha1))
Lukas Fleischerc9059712011-03-30 19:17:58 +0200388 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200389 else
Lukas Fleischerc9059712011-03-30 19:17:58 +0200390 diff_root_tree_sha1(new_sha1, "", &opt);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200391 diffcore_std(&opt);
392 diff_flush(&opt);
393}
394
Johan Herland1415f3f2010-09-30 20:15:14 +0200395void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
Lars Hjemli1b49de32007-05-13 11:24:23 +0200396{
397 unsigned char *old_sha1 = NULL;
398
399 if (commit->parents)
400 old_sha1 = commit->parents->item->object.sha1;
Johan Herland1415f3f2010-09-30 20:15:14 +0200401 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
Johan Herland2cc8b992010-06-24 17:52:57 +0200402 ctx.qry.ignorews);
Lars Hjemli1b49de32007-05-13 11:24:23 +0200403}
Lars Hjemlif34478c2008-03-24 16:00:27 +0100404
405int cgit_parse_snapshots_mask(const char *str)
406{
407 const struct cgit_snapshot_format *f;
408 static const char *delim = " \t,:/|;";
409 int tl, sl, rv = 0;
410
411 /* favor legacy setting */
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500412 if (atoi(str))
Lars Hjemlif34478c2008-03-24 16:00:27 +0100413 return 1;
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500414 for (;;) {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100415 str += strspn(str, delim);
416 tl = strcspn(str, delim);
Lars Hjemlif34478c2008-03-24 16:00:27 +0100417 if (!tl)
418 break;
419 for (f = cgit_snapshot_formats; f->suffix; f++) {
420 sl = strlen(f->suffix);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500421 if ((tl == sl && !strncmp(f->suffix, str, tl)) ||
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100422 (tl == sl - 1 && !strncmp(f->suffix + 1, str, tl - 1))) {
Lars Hjemlif34478c2008-03-24 16:00:27 +0100423 rv |= f->bit;
424 break;
425 }
426 }
427 str += tl;
428 }
429 return rv;
430}
Lars Hjemlid6f60722009-07-31 17:38:38 +0200431
Ferry Huberts14f28922011-03-23 11:57:43 +0100432typedef struct {
433 char * name;
434 char * value;
435} cgit_env_var;
436
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000437void cgit_prepare_repo_env(struct cgit_repo * repo)
438{
Ferry Huberts14f28922011-03-23 11:57:43 +0100439 cgit_env_var env_vars[] = {
440 { .name = "CGIT_REPO_URL", .value = repo->url },
441 { .name = "CGIT_REPO_NAME", .value = repo->name },
442 { .name = "CGIT_REPO_PATH", .value = repo->path },
443 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
444 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
445 { .name = "CGIT_REPO_SECTION", .value = repo->section },
446 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
447 };
448 int env_var_count = ARRAY_SIZE(env_vars);
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100449 cgit_env_var *p, *q;
450 static char *warn = "cgit warning: failed to set env: %s=%s\n";
Ferry Huberts14f28922011-03-23 11:57:43 +0100451
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100452 p = env_vars;
453 q = p + env_var_count;
454 for (; p < q; p++)
Lukas Fleischerd96d2c92011-09-14 11:52:43 +0200455 if (p->value && setenv(p->name, p->value, 1))
Lars Hjemlic2b58ed2011-03-26 11:22:35 +0100456 fprintf(stderr, warn, p->name, p->value);
Ferry Huberts14f28922011-03-23 11:57:43 +0100457}
458
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000459int cgit_open_filter(struct cgit_filter *filter)
Lars Hjemlid6f60722009-07-31 17:38:38 +0200460{
461
462 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
463 "Unable to duplicate STDOUT");
464 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
465 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
466 if (filter->pid == 0) {
467 close(filter->pipe_fh[1]);
468 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
469 "Unable to use pipe as STDIN");
470 execvp(filter->cmd, filter->argv);
John Keeping1e9f1ee2013-05-18 16:21:36 +0100471 die_errno("Unable to exec subprocess %s", filter->cmd);
Lars Hjemlid6f60722009-07-31 17:38:38 +0200472 }
473 close(filter->pipe_fh[0]);
474 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
475 "Unable to use pipe as STDOUT");
476 close(filter->pipe_fh[1]);
477 return 0;
478}
479
480int cgit_close_filter(struct cgit_filter *filter)
481{
482 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
483 "Unable to restore STDOUT");
484 close(filter->old_stdout);
485 if (filter->pid < 0)
486 return 0;
487 waitpid(filter->pid, &filter->exitstatus, 0);
488 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
489 return 0;
490 die("Subprocess %s exited abnormally", filter->cmd);
491}
Lars Hjemlie16f1782009-08-18 17:17:41 +0200492
493/* Read the content of the specified file into a newly allocated buffer,
494 * zeroterminate the buffer and return 0 on success, errno otherwise.
495 */
496int readfile(const char *path, char **buf, size_t *size)
497{
Lars Hjemli21f67e72009-11-07 18:08:30 +0100498 int fd, e;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200499 struct stat st;
500
501 fd = open(path, O_RDONLY);
502 if (fd == -1)
503 return errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100504 if (fstat(fd, &st)) {
Lars Hjemli21f67e72009-11-07 18:08:30 +0100505 e = errno;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100506 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100507 return e;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100508 }
509 if (!S_ISREG(st.st_mode)) {
510 close(fd);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200511 return EISDIR;
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100512 }
Lars Hjemlie16f1782009-08-18 17:17:41 +0200513 *buf = xmalloc(st.st_size + 1);
514 *size = read_in_full(fd, *buf, st.st_size);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100515 e = errno;
Lars Hjemlie16f1782009-08-18 17:17:41 +0200516 (*buf)[*size] = '\0';
Rys Sommefeldt8cfe4892009-11-07 15:24:45 +0100517 close(fd);
Lars Hjemli21f67e72009-11-07 18:08:30 +0100518 return (*size == st.st_size ? 0 : e);
Lars Hjemlie16f1782009-08-18 17:17:41 +0200519}
Lars Hjemliba56a372010-03-22 00:09:43 +0100520
Lukas Fleischerbafab422013-03-04 08:52:33 +0100521static int is_token_char(char c)
Lars Hjemliba56a372010-03-22 00:09:43 +0100522{
523 return isalnum(c) || c == '_';
524}
525
526/* Replace name with getenv(name), return pointer to zero-terminating char
527 */
Lukas Fleischerbafab422013-03-04 08:52:33 +0100528static char *expand_macro(char *name, int maxlength)
Lars Hjemliba56a372010-03-22 00:09:43 +0100529{
530 char *value;
531 int len;
532
533 len = 0;
534 value = getenv(name);
535 if (value) {
536 len = strlen(value);
537 if (len > maxlength)
538 len = maxlength;
539 strncpy(name, value, len);
540 }
541 return name + len;
542}
543
544#define EXPBUFSIZE (1024 * 8)
545
546/* Replace all tokens prefixed by '$' in the specified text with the
547 * value of the named environment variable.
548 * NB: the return value is a static buffer, i.e. it must be strdup'd
549 * by the caller.
550 */
551char *expand_macros(const char *txt)
552{
553 static char result[EXPBUFSIZE];
554 char *p, *start;
555 int len;
556
557 p = result;
558 start = NULL;
559 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
560 *p = *txt;
561 if (start) {
562 if (!is_token_char(*txt)) {
563 if (p - start > 0) {
564 *p = '\0';
565 len = result + EXPBUFSIZE - start - 1;
566 p = expand_macro(start, len) - 1;
567 }
568 start = NULL;
569 txt--;
570 }
571 p++;
572 txt++;
573 continue;
574 }
575 if (*txt == '$') {
576 start = p;
577 txt++;
578 continue;
579 }
580 p++;
581 txt++;
582 }
583 *p = '\0';
584 if (start && p - start > 0) {
585 len = result + EXPBUFSIZE - start - 1;
586 p = expand_macro(start, len);
587 *p = '\0';
588 }
589 return result;
590}