diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2014-01-10 13:19:05 (JST) |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2014-01-11 01:45:43 (JST) |
commit | 3eae406934b98ce78eff3a92cb410475d71095a3 (patch) | |
tree | 8545d754ee0b4d970a3299479087847bc4c33b5e | |
parent | b67ea0c0222d5b7eb4f65413047138e72055d8c5 (diff) | |
download | cgit-3eae406934b98ce78eff3a92cb410475d71095a3.zip cgit-3eae406934b98ce78eff3a92cb410475d71095a3.tar.gz |
filter: split filter functions into their own file
A first step for more interesting things.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | cgit.c | 42 | ||||
-rw-r--r-- | cgit.h | 1 | ||||
-rw-r--r-- | cgit.mk | 1 | ||||
-rw-r--r-- | filter.c | 82 | ||||
-rw-r--r-- | shared.c | 35 |
5 files changed, 90 insertions, 71 deletions
@@ -27,36 +27,6 @@ static void add_mimetype(const char *name, const char *value) | |||
27 | item->util = xstrdup(value); | 27 | item->util = xstrdup(value); |
28 | } | 28 | } |
29 | 29 | ||
30 | static struct cgit_filter *new_filter(const char *cmd, filter_type filtertype) | ||
31 | { | ||
32 | struct cgit_filter *f; | ||
33 | int args_size = 0; | ||
34 | int extra_args; | ||
35 | |||
36 | if (!cmd || !cmd[0]) | ||
37 | return NULL; | ||
38 | |||
39 | switch (filtertype) { | ||
40 | case SOURCE: | ||
41 | case ABOUT: | ||
42 | extra_args = 1; | ||
43 | break; | ||
44 | |||
45 | case COMMIT: | ||
46 | default: | ||
47 | extra_args = 0; | ||
48 | break; | ||
49 | } | ||
50 | |||
51 | f = xmalloc(sizeof(struct cgit_filter)); | ||
52 | f->cmd = xstrdup(cmd); | ||
53 | args_size = (2 + extra_args) * sizeof(char *); | ||
54 | f->argv = xmalloc(args_size); | ||
55 | memset(f->argv, 0, args_size); | ||
56 | f->argv[0] = f->cmd; | ||
57 | return f; | ||
58 | } | ||
59 | |||
60 | static void process_cached_repolist(const char *path); | 30 | static void process_cached_repolist(const char *path); |
61 | 31 | ||
62 | static void repo_config(struct cgit_repo *repo, const char *name, const char *value) | 32 | static void repo_config(struct cgit_repo *repo, const char *name, const char *value) |
@@ -114,11 +84,11 @@ static void repo_config(struct cgit_repo *repo, const char *name, const char *va | |||
114 | repo->logo_link = xstrdup(value); | 84 | repo->logo_link = xstrdup(value); |
115 | else if (ctx.cfg.enable_filter_overrides) { | 85 | else if (ctx.cfg.enable_filter_overrides) { |
116 | if (!strcmp(name, "about-filter")) | 86 | if (!strcmp(name, "about-filter")) |
117 | repo->about_filter = new_filter(value, ABOUT); | 87 | repo->about_filter = cgit_new_filter(value, ABOUT); |
118 | else if (!strcmp(name, "commit-filter")) | 88 | else if (!strcmp(name, "commit-filter")) |
119 | repo->commit_filter = new_filter(value, COMMIT); | 89 | repo->commit_filter = cgit_new_filter(value, COMMIT); |
120 | else if (!strcmp(name, "source-filter")) | 90 | else if (!strcmp(name, "source-filter")) |
121 | repo->source_filter = new_filter(value, SOURCE); | 91 | repo->source_filter = cgit_new_filter(value, SOURCE); |
122 | } | 92 | } |
123 | } | 93 | } |
124 | 94 | ||
@@ -215,9 +185,9 @@ static void config_cb(const char *name, const char *value) | |||
215 | else if (!strcmp(name, "case-sensitive-sort")) | 185 | else if (!strcmp(name, "case-sensitive-sort")) |
216 | ctx.cfg.case_sensitive_sort = atoi(value); | 186 | ctx.cfg.case_sensitive_sort = atoi(value); |
217 | else if (!strcmp(name, "about-filter")) | 187 | else if (!strcmp(name, "about-filter")) |
218 | ctx.cfg.about_filter = new_filter(value, ABOUT); | 188 | ctx.cfg.about_filter = cgit_new_filter(value, ABOUT); |
219 | else if (!strcmp(name, "commit-filter")) | 189 | else if (!strcmp(name, "commit-filter")) |
220 | ctx.cfg.commit_filter = new_filter(value, COMMIT); | 190 | ctx.cfg.commit_filter = cgit_new_filter(value, COMMIT); |
221 | else if (!strcmp(name, "embedded")) | 191 | else if (!strcmp(name, "embedded")) |
222 | ctx.cfg.embedded = atoi(value); | 192 | ctx.cfg.embedded = atoi(value); |
223 | else if (!strcmp(name, "max-atom-items")) | 193 | else if (!strcmp(name, "max-atom-items")) |
@@ -251,7 +221,7 @@ static void config_cb(const char *name, const char *value) | |||
251 | else if (!strcmp(name, "section-sort")) | 221 | else if (!strcmp(name, "section-sort")) |
252 | ctx.cfg.section_sort = atoi(value); | 222 | ctx.cfg.section_sort = atoi(value); |
253 | else if (!strcmp(name, "source-filter")) | 223 | else if (!strcmp(name, "source-filter")) |
254 | ctx.cfg.source_filter = new_filter(value, SOURCE); | 224 | ctx.cfg.source_filter = cgit_new_filter(value, SOURCE); |
255 | else if (!strcmp(name, "summary-log")) | 225 | else if (!strcmp(name, "summary-log")) |
256 | ctx.cfg.summary_log = atoi(value); | 226 | ctx.cfg.summary_log = atoi(value); |
257 | else if (!strcmp(name, "summary-branches")) | 227 | else if (!strcmp(name, "summary-branches")) |
@@ -344,6 +344,7 @@ extern int cgit_parse_snapshots_mask(const char *str); | |||
344 | 344 | ||
345 | extern int cgit_open_filter(struct cgit_filter *filter); | 345 | extern int cgit_open_filter(struct cgit_filter *filter); |
346 | extern int cgit_close_filter(struct cgit_filter *filter); | 346 | extern int cgit_close_filter(struct cgit_filter *filter); |
347 | extern struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype); | ||
347 | 348 | ||
348 | extern void cgit_prepare_repo_env(struct cgit_repo * repo); | 349 | extern void cgit_prepare_repo_env(struct cgit_repo * repo); |
349 | 350 | ||
@@ -29,6 +29,7 @@ CGIT_OBJ_NAMES += cgit.o | |||
29 | CGIT_OBJ_NAMES += cache.o | 29 | CGIT_OBJ_NAMES += cache.o |
30 | CGIT_OBJ_NAMES += cmd.o | 30 | CGIT_OBJ_NAMES += cmd.o |
31 | CGIT_OBJ_NAMES += configfile.o | 31 | CGIT_OBJ_NAMES += configfile.o |
32 | CGIT_OBJ_NAMES += filter.o | ||
32 | CGIT_OBJ_NAMES += html.o | 33 | CGIT_OBJ_NAMES += html.o |
33 | CGIT_OBJ_NAMES += parsing.o | 34 | CGIT_OBJ_NAMES += parsing.o |
34 | CGIT_OBJ_NAMES += scan-tree.o | 35 | CGIT_OBJ_NAMES += scan-tree.o |
diff --git a/filter.c b/filter.c new file mode 100644 index 0000000..f4ee9ac --- /dev/null +++ b/filter.c | |||
@@ -0,0 +1,82 @@ | |||
1 | /* filter.c: filter framework functions | ||
2 | * | ||
3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> | ||
4 | * | ||
5 | * Licensed under GNU General Public License v2 | ||
6 | * (see COPYING for full license text) | ||
7 | */ | ||
8 | |||
9 | #include "cgit.h" | ||
10 | #include <sys/types.h> | ||
11 | #include <sys/wait.h> | ||
12 | #include <unistd.h> | ||
13 | #include <string.h> | ||
14 | #include <stdlib.h> | ||
15 | |||
16 | int cgit_open_filter(struct cgit_filter *filter) | ||
17 | { | ||
18 | filter->old_stdout = chk_positive(dup(STDOUT_FILENO), | ||
19 | "Unable to duplicate STDOUT"); | ||
20 | chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); | ||
21 | filter->pid = chk_non_negative(fork(), "Unable to create subprocess"); | ||
22 | if (filter->pid == 0) { | ||
23 | close(filter->pipe_fh[1]); | ||
24 | chk_non_negative(dup2(filter->pipe_fh[0], STDIN_FILENO), | ||
25 | "Unable to use pipe as STDIN"); | ||
26 | execvp(filter->cmd, filter->argv); | ||
27 | die_errno("Unable to exec subprocess %s", filter->cmd); | ||
28 | } | ||
29 | close(filter->pipe_fh[0]); | ||
30 | chk_non_negative(dup2(filter->pipe_fh[1], STDOUT_FILENO), | ||
31 | "Unable to use pipe as STDOUT"); | ||
32 | close(filter->pipe_fh[1]); | ||
33 | return 0; | ||
34 | } | ||
35 | |||
36 | |||
37 | int cgit_close_filter(struct cgit_filter *filter) | ||
38 | { | ||
39 | int exit_status; | ||
40 | |||
41 | chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), | ||
42 | "Unable to restore STDOUT"); | ||
43 | close(filter->old_stdout); | ||
44 | if (filter->pid < 0) | ||
45 | return 0; | ||
46 | waitpid(filter->pid, &exit_status, 0); | ||
47 | if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) | ||
48 | return 0; | ||
49 | die("Subprocess %s exited abnormally", filter->cmd); | ||
50 | } | ||
51 | |||
52 | struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype) | ||
53 | { | ||
54 | struct cgit_filter *f; | ||
55 | int args_size = 0; | ||
56 | int extra_args; | ||
57 | |||
58 | if (!cmd || !cmd[0]) | ||
59 | return NULL; | ||
60 | |||
61 | switch (filtertype) { | ||
62 | case SOURCE: | ||
63 | case ABOUT: | ||
64 | extra_args = 1; | ||
65 | break; | ||
66 | |||
67 | case COMMIT: | ||
68 | default: | ||
69 | extra_args = 0; | ||
70 | break; | ||
71 | } | ||
72 | |||
73 | f = xmalloc(sizeof(struct cgit_filter)); | ||
74 | memset(f, 0, sizeof(struct cgit_filter)); | ||
75 | |||
76 | f->cmd = xstrdup(cmd); | ||
77 | args_size = (2 + extra_args) * sizeof(char *); | ||
78 | f->argv = xmalloc(args_size); | ||
79 | memset(f->argv, 0, args_size); | ||
80 | f->argv[0] = f->cmd; | ||
81 | return f; | ||
82 | } | ||
@@ -457,41 +457,6 @@ void cgit_prepare_repo_env(struct cgit_repo * repo) | |||
457 | fprintf(stderr, warn, p->name, p->value); | 457 | fprintf(stderr, warn, p->name, p->value); |
458 | } | 458 | } |
459 | 459 | ||
460 | int cgit_open_filter(struct cgit_filter *filter) | ||
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); | ||
471 | die_errno("Unable to exec subprocess %s", filter->cmd); | ||
472 | } | ||
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 | |||
480 | int cgit_close_filter(struct cgit_filter *filter) | ||
481 | { | ||
482 | int exit_status; | ||
483 | |||
484 | chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), | ||
485 | "Unable to restore STDOUT"); | ||
486 | close(filter->old_stdout); | ||
487 | if (filter->pid < 0) | ||
488 | return 0; | ||
489 | waitpid(filter->pid, &exit_status, 0); | ||
490 | if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) | ||
491 | return 0; | ||
492 | die("Subprocess %s exited abnormally", filter->cmd); | ||
493 | } | ||
494 | |||
495 | /* Read the content of the specified file into a newly allocated buffer, | 460 | /* Read the content of the specified file into a newly allocated buffer, |
496 | * zeroterminate the buffer and return 0 on success, errno otherwise. | 461 | * zeroterminate the buffer and return 0 on success, errno otherwise. |
497 | */ | 462 | */ |