aboutsummaryrefslogtreecommitdiffstats
path: root/filter.c
diff options
context:
space:
mode:
authorGravatar John Keeping <john@keeping.me.uk>2014-01-13 02:13:50 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-13 04:20:20 (JST)
commit3d8a6507ca542881a5e8b30ad6b7068a9c4fdeea (patch)
treef95c74d3317546606d72b74db3082769515c5e33 /filter.c
parentda218fcd9eb554a8405ca72e84bcc0feb371194f (diff)
downloadcgit-3d8a6507ca542881a5e8b30ad6b7068a9c4fdeea.zip
cgit-3d8a6507ca542881a5e8b30ad6b7068a9c4fdeea.tar.gz
filter: pass extra arguments via cgit_open_filter
This avoids poking into the filter data structure at various points in the code. We rely on the fact that the number of arguments is fixed based on the filter type (set in cgit_new_filter) and that the call sites all know which filter type they're using. Signed-off-by: John Keeping <john@keeping.me.uk>
Diffstat (limited to 'filter.c')
-rw-r--r--filter.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/filter.c b/filter.c
index f4ee9ac..d8c0116 100644
--- a/filter.c
+++ b/filter.c
@@ -13,8 +13,16 @@
13#include <string.h> 13#include <string.h>
14#include <stdlib.h> 14#include <stdlib.h>
15 15
16int cgit_open_filter(struct cgit_filter *filter) 16int cgit_open_filter(struct cgit_filter *filter, ...)
17{ 17{
18 int i;
19 va_list ap;
20
21 va_start(ap, filter);
22 for (i = 0; i < filter->extra_args; i++)
23 filter->argv[i+1] = va_arg(ap, char *);
24 va_end(ap);
25
18 filter->old_stdout = chk_positive(dup(STDOUT_FILENO), 26 filter->old_stdout = chk_positive(dup(STDOUT_FILENO),
19 "Unable to duplicate STDOUT"); 27 "Unable to duplicate STDOUT");
20 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess"); 28 chk_zero(pipe(filter->pipe_fh), "Unable to create pipe to subprocess");
@@ -36,45 +44,50 @@ int cgit_open_filter(struct cgit_filter *filter)
36 44
37int cgit_close_filter(struct cgit_filter *filter) 45int cgit_close_filter(struct cgit_filter *filter)
38{ 46{
39 int exit_status; 47 int i, exit_status;
40 48
41 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), 49 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
42 "Unable to restore STDOUT"); 50 "Unable to restore STDOUT");
43 close(filter->old_stdout); 51 close(filter->old_stdout);
44 if (filter->pid < 0) 52 if (filter->pid < 0)
45 return 0; 53 goto done;
46 waitpid(filter->pid, &exit_status, 0); 54 waitpid(filter->pid, &exit_status, 0);
47 if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) 55 if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status))
48 return 0; 56 goto done;
49 die("Subprocess %s exited abnormally", filter->cmd); 57 die("Subprocess %s exited abnormally", filter->cmd);
58
59done:
60 for (i = 0; i < filter->extra_args; i++)
61 filter->argv[i+1] = NULL;
62 return 0;
63
50} 64}
51 65
52struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype) 66struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
53{ 67{
54 struct cgit_filter *f; 68 struct cgit_filter *f;
55 int args_size = 0; 69 int args_size = 0;
56 int extra_args;
57 70
58 if (!cmd || !cmd[0]) 71 if (!cmd || !cmd[0])
59 return NULL; 72 return NULL;
60 73
74 f = xmalloc(sizeof(struct cgit_filter));
75 memset(f, 0, sizeof(struct cgit_filter));
76
61 switch (filtertype) { 77 switch (filtertype) {
62 case SOURCE: 78 case SOURCE:
63 case ABOUT: 79 case ABOUT:
64 extra_args = 1; 80 f->extra_args = 1;
65 break; 81 break;
66 82
67 case COMMIT: 83 case COMMIT:
68 default: 84 default:
69 extra_args = 0; 85 f->extra_args = 0;
70 break; 86 break;
71 } 87 }
72
73 f = xmalloc(sizeof(struct cgit_filter));
74 memset(f, 0, sizeof(struct cgit_filter));
75 88
76 f->cmd = xstrdup(cmd); 89 f->cmd = xstrdup(cmd);
77 args_size = (2 + extra_args) * sizeof(char *); 90 args_size = (2 + f->extra_args) * sizeof(char *);
78 f->argv = xmalloc(args_size); 91 f->argv = xmalloc(args_size);
79 memset(f->argv, 0, args_size); 92 memset(f->argv, 0, args_size);
80 f->argv[0] = f->cmd; 93 f->argv[0] = f->cmd;