aboutsummaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--cgit.h3
-rw-r--r--filter.c35
-rw-r--r--ui-repolist.c10
-rw-r--r--ui-summary.c13
-rw-r--r--ui-tree.c7
5 files changed, 38 insertions, 30 deletions
diff --git a/cgit.h b/cgit.h
index a72c503..e6e7715 100644
--- a/cgit.h
+++ b/cgit.h
@@ -59,6 +59,7 @@ typedef enum {
59struct cgit_filter { 59struct cgit_filter {
60 char *cmd; 60 char *cmd;
61 char **argv; 61 char **argv;
62 int extra_args;
62 int old_stdout; 63 int old_stdout;
63 int pipe_fh[2]; 64 int pipe_fh[2];
64 int pid; 65 int pid;
@@ -342,7 +343,7 @@ extern const char *cgit_repobasename(const char *reponame);
342 343
343extern int cgit_parse_snapshots_mask(const char *str); 344extern int cgit_parse_snapshots_mask(const char *str);
344 345
345extern int cgit_open_filter(struct cgit_filter *filter); 346extern int cgit_open_filter(struct cgit_filter *filter, ...);
346extern int cgit_close_filter(struct cgit_filter *filter); 347extern int cgit_close_filter(struct cgit_filter *filter);
347extern struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype); 348extern struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype);
348 349
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;
diff --git a/ui-repolist.c b/ui-repolist.c
index d4ee279..f622a01 100644
--- a/ui-repolist.c
+++ b/ui-repolist.c
@@ -331,13 +331,9 @@ void cgit_print_site_readme()
331{ 331{
332 if (!ctx.cfg.root_readme) 332 if (!ctx.cfg.root_readme)
333 return; 333 return;
334 if (ctx.cfg.about_filter) { 334 if (ctx.cfg.about_filter)
335 ctx.cfg.about_filter->argv[1] = ctx.cfg.root_readme; 335 cgit_open_filter(ctx.cfg.about_filter, ctx.cfg.root_readme);
336 cgit_open_filter(ctx.cfg.about_filter);
337 }
338 html_include(ctx.cfg.root_readme); 336 html_include(ctx.cfg.root_readme);
339 if (ctx.cfg.about_filter) { 337 if (ctx.cfg.about_filter)
340 cgit_close_filter(ctx.cfg.about_filter); 338 cgit_close_filter(ctx.cfg.about_filter);
341 ctx.cfg.about_filter->argv[1] = NULL;
342 }
343} 339}
diff --git a/ui-summary.c b/ui-summary.c
index 63a5a75..725f3ab 100644
--- a/ui-summary.c
+++ b/ui-summary.c
@@ -151,18 +151,17 @@ void cgit_print_repo_readme(char *path)
151 * filesystem, while applying the about-filter. 151 * filesystem, while applying the about-filter.
152 */ 152 */
153 html("<div id='summary'>"); 153 html("<div id='summary'>");
154 if (ctx.repo->about_filter) { 154 if (ctx.repo->about_filter)
155 ctx.repo->about_filter->argv[1] = filename; 155 cgit_open_filter(ctx.repo->about_filter, filename);
156 cgit_open_filter(ctx.repo->about_filter); 156
157 }
158 if (ref) 157 if (ref)
159 cgit_print_file(filename, ref, 1); 158 cgit_print_file(filename, ref, 1);
160 else 159 else
161 html_include(filename); 160 html_include(filename);
162 if (ctx.repo->about_filter) { 161
162 if (ctx.repo->about_filter)
163 cgit_close_filter(ctx.repo->about_filter); 163 cgit_close_filter(ctx.repo->about_filter);
164 ctx.repo->about_filter->argv[1] = NULL; 164
165 }
166 html("</div>"); 165 html("</div>");
167 if (free_filename) 166 if (free_filename)
168 free(filename); 167 free(filename);
diff --git a/ui-tree.c b/ui-tree.c
index 5ae3926..e4c3d22 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -45,13 +45,12 @@ static void print_text_buffer(const char *name, char *buf, unsigned long size)
45 } 45 }
46 46
47 if (ctx.repo->source_filter) { 47 if (ctx.repo->source_filter) {
48 char *filter_arg = xstrdup(name);
48 html("<td class='lines'><pre><code>"); 49 html("<td class='lines'><pre><code>");
49 ctx.repo->source_filter->argv[1] = xstrdup(name); 50 cgit_open_filter(ctx.repo->source_filter, filter_arg);
50 cgit_open_filter(ctx.repo->source_filter);
51 html_raw(buf, size); 51 html_raw(buf, size);
52 cgit_close_filter(ctx.repo->source_filter); 52 cgit_close_filter(ctx.repo->source_filter);
53 free(ctx.repo->source_filter->argv[1]); 53 free(filter_arg);
54 ctx.repo->source_filter->argv[1] = NULL;
55 html("</code></pre></td></tr></table>\n"); 54 html("</code></pre></td></tr></table>\n");
56 return; 55 return;
57 } 56 }