Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 534 lines (474 sloc) 12.506 kB
a1a7999 @hjemli Add head comment to shared.c
hjemli authored
1 /* 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
44923f8 @hjemli Move global variables + callback functions into shared.c
hjemli authored
9 #include "cgit.h"
14f2892 @fhuberts cgit_open_filter: hand down repo configuration to script
fhuberts authored
10 #include <stdio.h>
44923f8 @hjemli Move global variables + callback functions into shared.c
hjemli authored
11
d1f3bbe @hjemli Move cgit_repo into cgit_context
hjemli authored
12 struct cgit_repolist cgit_repolist;
d14d77f @hjemli Introduce struct cgit_context
hjemli authored
13 struct cgit_context ctx;
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
14
ab2ab95 @hjemli Add support for snapshots
hjemli authored
15 int chk_zero(int result, char *msg)
16 {
17 if (result != 0)
18 die("%s: %s", msg, strerror(errno));
19 return result;
20 }
21
22 int chk_positive(int result, char *msg)
23 {
24 if (result <= 0)
25 die("%s: %s", msg, strerror(errno));
26 return result;
27 }
28
127f43d @hacker added a chk_non_negative check
hacker authored
29 int chk_non_negative(int result, char *msg)
30 {
31 if (result < 0)
32 die("%s: %s",msg, strerror(errno));
33 return result;
34 }
35
163037e @hjemli Move non-generic functions from shared.c to cgit.c
hjemli authored
36 struct cgit_repo *cgit_add_repo(const char *url)
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
37 {
d1f3bbe @hjemli Move cgit_repo into cgit_context
hjemli authored
38 struct cgit_repo *ret;
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
39
40 if (++cgit_repolist.count > cgit_repolist.length) {
41 if (cgit_repolist.length == 0)
42 cgit_repolist.length = 8;
43 else
44 cgit_repolist.length *= 2;
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
45 cgit_repolist.repos = xrealloc(cgit_repolist.repos,
46 cgit_repolist.length *
d1f3bbe @hjemli Move cgit_repo into cgit_context
hjemli authored
47 sizeof(struct cgit_repo));
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
48 }
49
50 ret = &cgit_repolist.repos[cgit_repolist.count-1];
42e7a16 @hjemli shared.c: initialize cgit_repo structs properly
hjemli authored
51 memset(ret, 0, sizeof(struct cgit_repo));
4e40d85 @hjemli Use trim_end() to remove trailing slashes
hjemli authored
52 ret->url = trim_end(url, '/');
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
53 ret->name = ret->url;
54 ret->path = NULL;
7b34664 Default repo description to "[no description]"
Evan Martin authored
55 ret->desc = "[no description]";
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
56 ret->owner = NULL;
e7af002 @hjemli Introduce 'section' as canonical spelling for 'repo.group'
hjemli authored
57 ret->section = ctx.cfg.section;
b228d4f @hjemli Add all config variables into struct cgit_context
hjemli authored
58 ret->snapshots = ctx.cfg.snapshots;
9a8d39c @jherland ui-log: Implement support for commit graphs
jherland authored
59 ret->enable_commit_graph = ctx.cfg.enable_commit_graph;
b228d4f @hjemli Add all config variables into struct cgit_context
hjemli authored
60 ret->enable_log_filecount = ctx.cfg.enable_log_filecount;
61 ret->enable_log_linecount = ctx.cfg.enable_log_linecount;
41934a3 @hjemli Add support for remote branches
hjemli authored
62 ret->enable_remote_branches = ctx.cfg.enable_remote_branches;
581a0c2 @hjemli Optionally generate verbose parent links
hjemli authored
63 ret->enable_subject_links = ctx.cfg.enable_subject_links;
fb2f3f6 @hjemli ui-stats: replace 'enable-stats' setting with 'max-stats'
hjemli authored
64 ret->max_stats = ctx.cfg.max_stats;
b228d4f @hjemli Add all config variables into struct cgit_context
hjemli authored
65 ret->module_link = ctx.cfg.module_link;
515edb0 @hjemli Add support for "readme" option
hjemli authored
66 ret->readme = ctx.cfg.readme;
8813170 @hjemli ui-repolist: implement lazy caching of repo->mtime
hjemli authored
67 ret->mtime = -1;
537c05f @hjemli Add 'about-filter' and 'repo.about-filter' options
hjemli authored
68 ret->about_filter = ctx.cfg.about_filter;
e976df2 @hjemli Add support for repo.commit-filter and repo.source-filter
hjemli authored
69 ret->commit_filter = ctx.cfg.commit_filter;
70 ret->source_filter = ctx.cfg.source_filter;
a1429db @hjemli cgit.c: add 'clone-url' setting with support for macro expansion
hjemli authored
71 ret->clone_url = ctx.cfg.clone_url;
6857bec @hjemli ui-tree.c: add support for path-selected submodule links
hjemli authored
72 ret->submodules.strdup_strings = 1;
fe1d128 @kaos Added "visible-for" config option.
authored
73 ret->visible_for = ctx.cfg.visible_for;
ce1c733 @hjemli Read repo-info from /etc/cgitrc
hjemli authored
74 return ret;
75 }
76
d1f3bbe @hjemli Move cgit_repo into cgit_context
hjemli authored
77 struct cgit_repo *cgit_get_repoinfo(const char *url)
305414d @hjemli Move cgit_get_repoinfo into shared.c
hjemli authored
78 {
79 int i;
d1f3bbe @hjemli Move cgit_repo into cgit_context
hjemli authored
80 struct cgit_repo *repo;
305414d @hjemli Move cgit_get_repoinfo into shared.c
hjemli authored
81
82 for (i=0; i<cgit_repolist.count; i++) {
83 repo = &cgit_repolist.repos[i];
84 if (!strcmp(repo->url, url))
7b52e0e @kaos Added options to control repo visibility.
authored
85 if (is_visible(repo))
86 return repo;
305414d @hjemli Move cgit_get_repoinfo into shared.c
hjemli authored
87 }
88 return NULL;
89 }
90
aaa24bd @hjemli Add cgit_free_commitinfo() and use where needed
hjemli authored
91 void *cgit_free_commitinfo(struct commitinfo *info)
92 {
93 free(info->author);
94 free(info->author_email);
95 free(info->committer);
96 free(info->committer_email);
97 free(info->subject);
3845e17 @jothan Add commit->msg_encoding, allocate msg dynamicly.
jothan authored
98 free(info->msg);
99 free(info->msg_encoding);
aaa24bd @hjemli Add cgit_free_commitinfo() and use where needed
hjemli authored
100 free(info);
101 return NULL;
102 }
52e605c Handle %xx encoding in querystring
Lars Hjemli authored
103
382805e @hjemli Add trim_end() and use it to remove trailing slashes from repo paths
hjemli authored
104 char *trim_end(const char *str, char c)
105 {
106 int len;
107
108 if (str == NULL)
109 return NULL;
dc1a8ea @hjemli shared.c: do not modify const memory
hjemli authored
110 len = strlen(str);
111 while(len > 0 && str[len - 1] == c)
382805e @hjemli Add trim_end() and use it to remove trailing slashes from repo paths
hjemli authored
112 len--;
113 if (len == 0)
114 return NULL;
dc1a8ea @hjemli shared.c: do not modify const memory
hjemli authored
115 return xstrndup(str, len);
382805e @hjemli Add trim_end() and use it to remove trailing slashes from repo paths
hjemli authored
116 }
117
0c8e184 @hjemli Change the cgit layout
hjemli authored
118 char *strlpart(char *txt, int maxlen)
119 {
120 char *result;
121
122 if (!txt)
123 return txt;
124
125 if (strlen(txt) <= maxlen)
126 return txt;
127 result = xmalloc(maxlen + 1);
128 memcpy(result, txt, maxlen - 3);
129 result[maxlen-1] = result[maxlen-2] = result[maxlen-3] = '.';
130 result[maxlen] = '\0';
131 return result;
132 }
133
134 char *strrpart(char *txt, int maxlen)
135 {
136 char *result;
137
138 if (!txt)
139 return txt;
140
141 if (strlen(txt) <= maxlen)
142 return txt;
143 result = xmalloc(maxlen + 1);
144 memcpy(result + 3, txt + strlen(txt) - maxlen + 4, maxlen - 3);
145 result[0] = result[1] = result[2] = '.';
146 return result;
147 }
148
e397ff7 @hjemli Add functions and types for ref lists
hjemli authored
149 void cgit_add_ref(struct reflist *list, struct refinfo *ref)
150 {
151 size_t size;
152
153 if (list->count >= list->alloc) {
154 list->alloc += (list->alloc ? list->alloc : 4);
155 size = list->alloc * sizeof(struct refinfo *);
156 list->refs = xrealloc(list->refs, size);
157 }
158 list->refs[list->count++] = ref;
159 }
160
161 struct refinfo *cgit_mk_refinfo(const char *refname, const unsigned char *sha1)
162 {
163 struct refinfo *ref;
164
165 ref = xmalloc(sizeof (struct refinfo));
166 ref->refname = xstrdup(refname);
167 ref->object = parse_object(sha1);
168 switch (ref->object->type) {
169 case OBJ_TAG:
170 ref->tag = cgit_parse_tag((struct tag *)ref->object);
171 break;
172 case OBJ_COMMIT:
173 ref->commit = cgit_parse_commit((struct commit *)ref->object);
174 break;
175 }
176 return ref;
177 }
178
179 int cgit_refs_cb(const char *refname, const unsigned char *sha1, int flags,
180 void *cb_data)
181 {
182 struct reflist *list = (struct reflist *)cb_data;
183 struct refinfo *info = cgit_mk_refinfo(refname, sha1);
184
185 if (info)
186 cgit_add_ref(list, info);
187 return 0;
188 }
189
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
190 void cgit_diff_tree_cb(struct diff_queue_struct *q,
191 struct diff_options *options, void *data)
192 {
193 int i;
194
195 for (i = 0; i < q->nr; i++) {
196 if (q->queue[i]->status == 'U')
197 continue;
198 ((filepair_fn)data)(q->queue[i]);
199 }
200 }
201
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
202 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
203 {
204 enum object_type type;
205
206 if (is_null_sha1(sha1)) {
207 file->ptr = (char *)"";
208 file->size = 0;
209 } else {
0835ffe @hjemli Remove a few compiler warnings
hjemli authored
210 file->ptr = read_sha1_file(sha1, &type,
211 (unsigned long *)&file->size);
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
212 }
213 return 1;
214 }
215
216 /*
217 * Receive diff-buffers from xdiff and concatenate them as
218 * needed across multiple callbacks.
219 *
220 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
221 * ripped from git and modified to use globals instead of
222 * a special callback-struct.
223 */
224 char *diffbuf = NULL;
225 int buflen = 0;
226
227 int filediff_cb(void *priv, mmbuffer_t *mb, int nbuf)
228 {
229 int i;
230
231 for (i = 0; i < nbuf; i++) {
232 if (mb[i].ptr[mb[i].size-1] != '\n') {
233 /* Incomplete line */
234 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
235 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
236 buflen += mb[i].size;
237 continue;
238 }
239
240 /* we have a complete line */
241 if (!diffbuf) {
242 ((linediff_fn)priv)(mb[i].ptr, mb[i].size);
243 continue;
244 }
245 diffbuf = xrealloc(diffbuf, buflen + mb[i].size);
246 memcpy(diffbuf + buflen, mb[i].ptr, mb[i].size);
247 ((linediff_fn)priv)(diffbuf, buflen + mb[i].size);
248 free(diffbuf);
249 diffbuf = NULL;
250 buflen = 0;
251 }
252 if (diffbuf) {
253 ((linediff_fn)priv)(diffbuf, buflen);
254 free(diffbuf);
255 diffbuf = NULL;
256 buflen = 0;
257 }
258 return 0;
259 }
260
261 int cgit_diff_files(const unsigned char *old_sha1,
c495cf0 @hjemli Handle binary files in diffs
hjemli authored
262 const unsigned char *new_sha1, unsigned long *old_size,
6180e61 @jherland Add URL parameter 'context' for changing the number of context lines …
jherland authored
263 unsigned long *new_size, int *binary, int context,
2cc8b99 @jherland Add URL parameter 'ignorews' for optionally ignoring whitespace in diffs
jherland authored
264 int ignorews, linediff_fn fn)
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
265 {
266 mmfile_t file1, file2;
267 xpparam_t diff_params;
268 xdemitconf_t emit_params;
269 xdemitcb_t emit_cb;
270
271 if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))
272 return 1;
273
c495cf0 @hjemli Handle binary files in diffs
hjemli authored
274 *old_size = file1.size;
275 *new_size = file2.size;
276
481ce5e @hjemli shared.c: avoid SEGFAULT when checking for binary buffers
hjemli authored
277 if ((file1.ptr && buffer_is_binary(file1.ptr, file1.size)) ||
278 (file2.ptr && buffer_is_binary(file2.ptr, file2.size))) {
c495cf0 @hjemli Handle binary files in diffs
hjemli authored
279 *binary = 1;
ce761fd @hjemli shared.c: avoid memory leak during diff
hjemli authored
280 if (file1.size)
281 free(file1.ptr);
282 if (file2.size)
283 free(file2.ptr);
c495cf0 @hjemli Handle binary files in diffs
hjemli authored
284 return 0;
285 }
286
0edf760 @hjemli shared.c: future-proof usage of git diff-structures
hjemli authored
287 memset(&diff_params, 0, sizeof(diff_params));
288 memset(&emit_params, 0, sizeof(emit_params));
289 memset(&emit_cb, 0, sizeof(emit_cb));
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
290 diff_params.flags = XDF_NEED_MINIMAL;
2cc8b99 @jherland Add URL parameter 'ignorews' for optionally ignoring whitespace in diffs
jherland authored
291 if (ignorews)
292 diff_params.flags |= XDF_IGNORE_WHITESPACE;
6180e61 @jherland Add URL parameter 'context' for changing the number of context lines …
jherland authored
293 emit_params.ctxlen = context > 0 ? context : 3;
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
294 emit_params.flags = XDL_EMIT_FUNCNAMES;
295 emit_cb.outf = filediff_cb;
296 emit_cb.priv = fn;
297 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
ce761fd @hjemli shared.c: avoid memory leak during diff
hjemli authored
298 if (file1.size)
299 free(file1.ptr);
300 if (file2.size)
301 free(file2.ptr);
c4ef667 @hjemli Add standard interface for file diff functions
hjemli authored
302 return 0;
303 }
304
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
305 void cgit_diff_tree(const unsigned char *old_sha1,
306 const unsigned char *new_sha1,
2cc8b99 @jherland Add URL parameter 'ignorews' for optionally ignoring whitespace in diffs
jherland authored
307 filepair_fn fn, const char *prefix, int ignorews)
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
308 {
309 struct diff_options opt;
f527a57 @hjemli Add prefix parameter to cgit_diff_tree()
hjemli authored
310 int prefixlen;
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
311
312 diff_setup(&opt);
313 opt.output_format = DIFF_FORMAT_CALLBACK;
314 opt.detect_rename = 1;
b228d4f @hjemli Add all config variables into struct cgit_context
hjemli authored
315 opt.rename_limit = ctx.cfg.renamelimit;
776200b @hjemli Use GIT-1.5.4.rc4
hjemli authored
316 DIFF_OPT_SET(&opt, RECURSIVE);
2cc8b99 @jherland Add URL parameter 'ignorews' for optionally ignoring whitespace in diffs
jherland authored
317 if (ignorews)
318 DIFF_XDL_SET(&opt, IGNORE_WHITESPACE);
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
319 opt.format_callback = cgit_diff_tree_cb;
320 opt.format_callback_data = fn;
f527a57 @hjemli Add prefix parameter to cgit_diff_tree()
hjemli authored
321 if (prefix) {
322 opt.nr_paths = 1;
323 opt.paths = &prefix;
324 prefixlen = strlen(prefix);
325 opt.pathlens = &prefixlen;
326 }
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
327 diff_setup_done(&opt);
328
4a0be58 @hjemli Add cgit_diff_link()
hjemli authored
329 if (old_sha1 && !is_null_sha1(old_sha1))
c905971 Remove unused variable from cgit_diff_tree().
Lukas Fleischer authored
330 diff_tree_sha1(old_sha1, new_sha1, "", &opt);
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
331 else
c905971 Remove unused variable from cgit_diff_tree().
Lukas Fleischer authored
332 diff_root_tree_sha1(new_sha1, "", &opt);
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
333 diffcore_std(&opt);
334 diff_flush(&opt);
335 }
336
1415f3f @jherland ui-log: Fix filecount/linecount when path limit is in effect
jherland authored
337 void cgit_diff_commit(struct commit *commit, filepair_fn fn, const char *prefix)
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
338 {
339 unsigned char *old_sha1 = NULL;
340
341 if (commit->parents)
342 old_sha1 = commit->parents->item->object.sha1;
1415f3f @jherland ui-log: Fix filecount/linecount when path limit is in effect
jherland authored
343 cgit_diff_tree(old_sha1, commit->object.sha1, fn, prefix,
2cc8b99 @jherland Add URL parameter 'ignorews' for optionally ignoring whitespace in diffs
jherland authored
344 ctx.qry.ignorews);
1b49de3 @hjemli Add shared diff-handling functions
hjemli authored
345 }
f34478c @hjemli Refactor snapshot support
hjemli authored
346
347 int cgit_parse_snapshots_mask(const char *str)
348 {
349 const struct cgit_snapshot_format *f;
350 static const char *delim = " \t,:/|;";
351 int tl, sl, rv = 0;
352
353 /* favor legacy setting */
354 if(atoi(str))
355 return 1;
356 for(;;) {
357 str += strspn(str,delim);
358 tl = strcspn(str,delim);
359 if (!tl)
360 break;
361 for (f = cgit_snapshot_formats; f->suffix; f++) {
362 sl = strlen(f->suffix);
363 if((tl == sl && !strncmp(f->suffix, str, tl)) ||
364 (tl == sl-1 && !strncmp(f->suffix+1, str, tl-1))) {
365 rv |= f->bit;
366 break;
367 }
368 }
369 str += tl;
370 }
371 return rv;
372 }
d6f6072 @hjemli Add generic filter/plugin infrastructure
hjemli authored
373
14f2892 @fhuberts cgit_open_filter: hand down repo configuration to script
fhuberts authored
374 typedef struct {
375 char * name;
376 char * value;
377 } cgit_env_var;
378
3ec6b30 @hjemli cgit.c: always setup cgit repo environment variables
hjemli authored
379 void cgit_prepare_repo_env(struct cgit_repo * repo)
380 {
14f2892 @fhuberts cgit_open_filter: hand down repo configuration to script
fhuberts authored
381 cgit_env_var env_vars[] = {
382 { .name = "CGIT_REPO_URL", .value = repo->url },
383 { .name = "CGIT_REPO_NAME", .value = repo->name },
384 { .name = "CGIT_REPO_PATH", .value = repo->path },
385 { .name = "CGIT_REPO_OWNER", .value = repo->owner },
386 { .name = "CGIT_REPO_DEFBRANCH", .value = repo->defbranch },
387 { .name = "CGIT_REPO_SECTION", .value = repo->section },
388 { .name = "CGIT_REPO_CLONE_URL", .value = repo->clone_url }
389 };
390 int env_var_count = ARRAY_SIZE(env_vars);
c2b58ed @hjemli shared.c: use execvp() to execute filter commands
hjemli authored
391 cgit_env_var *p, *q;
392 static char *warn = "cgit warning: failed to set env: %s=%s\n";
393
394 p = env_vars;
395 q = p + env_var_count;
396 for (; p < q; p++)
d96d2c9 shared.c: Only setenv() if value is non-null
Lukas Fleischer authored
397 if (p->value && setenv(p->name, p->value, 1))
c2b58ed @hjemli shared.c: use execvp() to execute filter commands
hjemli authored
398 fprintf(stderr, warn, p->name, p->value);
14f2892 @fhuberts cgit_open_filter: hand down repo configuration to script
fhuberts authored
399 }
400
3ec6b30 @hjemli cgit.c: always setup cgit repo environment variables
hjemli authored
401 int cgit_open_filter(struct cgit_filter *filter)
d6f6072 @hjemli Add generic filter/plugin infrastructure
hjemli authored
402 {
403
404 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
405 "Unable to duplicate STDOUT");
406 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
407 filter->pid = chk_non_negative(fork(), "Unable to create subprocess");
408 if (filter->pid == 0) {
409 close(filter->pipe_fh[1]);
410 chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO),
411 "Unable to use pipe as STDIN");
412 execvp(filter->cmd, filter->argv);
413 die("Unable to exec subprocess %s: %s (%d)", filter->cmd,
414 strerror(errno), errno);
415 }
416 close(filter->pipe_fh[0]);
417 chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO),
418 "Unable to use pipe as STDOUT");
419 close(filter->pipe_fh[1]);
420 return 0;
421 }
422
423 int cgit_close_filter(struct cgit_filter *filter)
424 {
425 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
426 "Unable to restore STDOUT");
427 close(filter->old_stdout);
428 if (filter->pid < 0)
429 return 0;
430 waitpid(filter->pid, &filter->exitstatus, 0);
431 if (WIFEXITED(filter->exitstatus) && !WEXITSTATUS(filter->exitstatus))
432 return 0;
433 die("Subprocess %s exited abnormally", filter->cmd);
434 }
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
435
436 /* Read the content of the specified file into a newly allocated buffer,
437 * zeroterminate the buffer and return 0 on success, errno otherwise.
438 */
439 int readfile(const char *path, char **buf, size_t *size)
440 {
21f67e7 @hjemli shared.c: return original errno
hjemli authored
441 int fd, e;
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
442 struct stat st;
443
444 fd = open(path, O_RDONLY);
445 if (fd == -1)
446 return errno;
8cfe489 @rys Close fd on error in readfile()
rys authored
447 if (fstat(fd, &st)) {
21f67e7 @hjemli shared.c: return original errno
hjemli authored
448 e = errno;
8cfe489 @rys Close fd on error in readfile()
rys authored
449 close(fd);
21f67e7 @hjemli shared.c: return original errno
hjemli authored
450 return e;
8cfe489 @rys Close fd on error in readfile()
rys authored
451 }
452 if (!S_ISREG(st.st_mode)) {
453 close(fd);
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
454 return EISDIR;
8cfe489 @rys Close fd on error in readfile()
rys authored
455 }
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
456 *buf = xmalloc(st.st_size + 1);
457 *size = read_in_full(fd, *buf, st.st_size);
21f67e7 @hjemli shared.c: return original errno
hjemli authored
458 e = errno;
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
459 (*buf)[*size] = '\0';
8cfe489 @rys Close fd on error in readfile()
rys authored
460 close(fd);
21f67e7 @hjemli shared.c: return original errno
hjemli authored
461 return (*size == st.st_size ? 0 : e);
e16f178 @hjemli Add and use a common readfile() function
hjemli authored
462 }
ba56a37 @hjemli Add support for environment variable expansion
hjemli authored
463
464 int is_token_char(char c)
465 {
466 return isalnum(c) || c == '_';
467 }
468
469 /* Replace name with getenv(name), return pointer to zero-terminating char
470 */
471 char *expand_macro(char *name, int maxlength)
472 {
473 char *value;
474 int len;
475
476 len = 0;
477 value = getenv(name);
478 if (value) {
479 len = strlen(value);
480 if (len > maxlength)
481 len = maxlength;
482 strncpy(name, value, len);
483 }
484 return name + len;
485 }
486
487 #define EXPBUFSIZE (1024 * 8)
488
489 /* Replace all tokens prefixed by '$' in the specified text with the
490 * value of the named environment variable.
491 * NB: the return value is a static buffer, i.e. it must be strdup'd
492 * by the caller.
493 */
494 char *expand_macros(const char *txt)
495 {
496 static char result[EXPBUFSIZE];
497 char *p, *start;
498 int len;
499
500 p = result;
501 start = NULL;
502 while (p < result + EXPBUFSIZE - 1 && txt && *txt) {
503 *p = *txt;
504 if (start) {
505 if (!is_token_char(*txt)) {
506 if (p - start > 0) {
507 *p = '\0';
508 len = result + EXPBUFSIZE - start - 1;
509 p = expand_macro(start, len) - 1;
510 }
511 start = NULL;
512 txt--;
513 }
514 p++;
515 txt++;
516 continue;
517 }
518 if (*txt == '$') {
519 start = p;
520 txt++;
521 continue;
522 }
523 p++;
524 txt++;
525 }
526 *p = '\0';
527 if (start && p - start > 0) {
528 len = result + EXPBUFSIZE - start - 1;
529 p = expand_macro(start, len);
530 *p = '\0';
531 }
532 return result;
533 }
Something went wrong with that request. Please try again.