]>
git.cameronkatri.com Git - cgit.git/blob - filter.c
1 /* filter.c: filter framework functions
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include <sys/types.h>
24 static ssize_t (*libc_write
)(int fd
, const void *buf
, size_t count
);
25 static ssize_t (*filter_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
) = NULL
;
26 static struct cgit_filter
*current_write_filter
= NULL
;
28 static inline void reap_filter(struct cgit_filter
*filter
)
30 if (filter
&& filter
->cleanup
)
31 filter
->cleanup(filter
);
34 void cgit_cleanup_filters(void)
37 reap_filter(ctx
.cfg
.about_filter
);
38 reap_filter(ctx
.cfg
.commit_filter
);
39 reap_filter(ctx
.cfg
.source_filter
);
40 reap_filter(ctx
.cfg
.email_filter
);
41 for (i
= 0; i
< cgit_repolist
.count
; ++i
) {
42 reap_filter(cgit_repolist
.repos
[i
].about_filter
);
43 reap_filter(cgit_repolist
.repos
[i
].commit_filter
);
44 reap_filter(cgit_repolist
.repos
[i
].source_filter
);
45 reap_filter(cgit_repolist
.repos
[i
].email_filter
);
49 void cgit_init_filters(void)
51 libc_write
= dlsym(RTLD_NEXT
, "write");
53 die("Could not locate libc's write function");
56 ssize_t
write(int fd
, const void *buf
, size_t count
)
58 if (fd
!= STDOUT_FILENO
|| !filter_write
)
59 return libc_write(fd
, buf
, count
);
60 return filter_write(current_write_filter
, buf
, count
);
63 static inline void hook_write(struct cgit_filter
*filter
, ssize_t (*new_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
))
65 /* We want to avoid buggy nested patterns. */
66 assert(filter_write
== NULL
);
67 assert(current_write_filter
== NULL
);
68 current_write_filter
= filter
;
69 filter_write
= new_write
;
72 static inline void unhook_write()
74 assert(filter_write
!= NULL
);
75 assert(current_write_filter
!= NULL
);
77 current_write_filter
= NULL
;
80 static int open_exec_filter(struct cgit_filter
*base
, va_list ap
)
82 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
85 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
86 filter
->argv
[i
+ 1] = va_arg(ap
, char *);
88 filter
->old_stdout
= chk_positive(dup(STDOUT_FILENO
),
89 "Unable to duplicate STDOUT");
90 chk_zero(pipe(filter
->pipe_fh
), "Unable to create pipe to subprocess");
91 filter
->pid
= chk_non_negative(fork(), "Unable to create subprocess");
92 if (filter
->pid
== 0) {
93 close(filter
->pipe_fh
[1]);
94 chk_non_negative(dup2(filter
->pipe_fh
[0], STDIN_FILENO
),
95 "Unable to use pipe as STDIN");
96 execvp(filter
->cmd
, filter
->argv
);
97 die_errno("Unable to exec subprocess %s", filter
->cmd
);
99 close(filter
->pipe_fh
[0]);
100 chk_non_negative(dup2(filter
->pipe_fh
[1], STDOUT_FILENO
),
101 "Unable to use pipe as STDOUT");
102 close(filter
->pipe_fh
[1]);
106 static int close_exec_filter(struct cgit_filter
*base
)
108 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
111 chk_non_negative(dup2(filter
->old_stdout
, STDOUT_FILENO
),
112 "Unable to restore STDOUT");
113 close(filter
->old_stdout
);
116 waitpid(filter
->pid
, &exit_status
, 0);
117 if (WIFEXITED(exit_status
) && !WEXITSTATUS(exit_status
))
119 die("Subprocess %s exited abnormally", filter
->cmd
);
122 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
123 filter
->argv
[i
+ 1] = NULL
;
128 static void fprintf_exec_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
130 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
131 fprintf(f
, "%sexec:%s\n", prefix
, filter
->cmd
);
134 static void cleanup_exec_filter(struct cgit_filter
*base
)
136 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
147 static struct cgit_filter
*new_exec_filter(const char *cmd
, int argument_count
)
149 struct cgit_exec_filter
*f
;
152 f
= xmalloc(sizeof(*f
));
153 /* We leave argv for now and assign it below. */
154 cgit_exec_filter_init(f
, xstrdup(cmd
), NULL
);
155 f
->base
.argument_count
= argument_count
;
156 args_size
= (2 + argument_count
) * sizeof(char *);
157 f
->argv
= xmalloc(args_size
);
158 memset(f
->argv
, 0, args_size
);
163 void cgit_exec_filter_init(struct cgit_exec_filter
*filter
, char *cmd
, char **argv
)
165 memset(filter
, 0, sizeof(*filter
));
166 filter
->base
.open
= open_exec_filter
;
167 filter
->base
.close
= close_exec_filter
;
168 filter
->base
.fprintf
= fprintf_exec_filter
;
169 filter
->base
.cleanup
= cleanup_exec_filter
;
172 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
173 filter
->base
.argument_count
= 0;
178 struct cgit_filter base
;
180 lua_State
*lua_state
;
183 static void error_lua_filter(struct lua_filter
*filter
)
185 die("Lua error in %s: %s", filter
->script_file
, lua_tostring(filter
->lua_state
, -1));
186 lua_pop(filter
->lua_state
, 1);
189 static ssize_t
write_lua_filter(struct cgit_filter
*base
, const void *buf
, size_t count
)
191 struct lua_filter
*filter
= (struct lua_filter
*)base
;
193 lua_getglobal(filter
->lua_state
, "filter_write");
194 lua_pushlstring(filter
->lua_state
, buf
, count
);
195 if (lua_pcall(filter
->lua_state
, 1, 0, 0)) {
196 error_lua_filter(filter
);
203 static inline int hook_lua_filter(lua_State
*lua_state
, void (*fn
)(const char *txt
))
206 ssize_t (*save_filter_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
);
207 struct cgit_filter
*save_filter
;
209 str
= lua_tostring(lua_state
, 1);
213 save_filter_write
= filter_write
;
214 save_filter
= current_write_filter
;
217 hook_write(save_filter
, save_filter_write
);
222 static int html_lua_filter(lua_State
*lua_state
)
224 return hook_lua_filter(lua_state
, html
);
227 static int html_txt_lua_filter(lua_State
*lua_state
)
229 return hook_lua_filter(lua_state
, html_txt
);
232 static int html_attr_lua_filter(lua_State
*lua_state
)
234 return hook_lua_filter(lua_state
, html_attr
);
237 static int html_url_path_lua_filter(lua_State
*lua_state
)
239 return hook_lua_filter(lua_state
, html_url_path
);
242 static int html_url_arg_lua_filter(lua_State
*lua_state
)
244 return hook_lua_filter(lua_state
, html_url_arg
);
247 static void cleanup_lua_filter(struct cgit_filter
*base
)
249 struct lua_filter
*filter
= (struct lua_filter
*)base
;
251 if (!filter
->lua_state
)
254 lua_close(filter
->lua_state
);
255 filter
->lua_state
= NULL
;
256 if (filter
->script_file
) {
257 free(filter
->script_file
);
258 filter
->script_file
= NULL
;
262 static int init_lua_filter(struct lua_filter
*filter
)
264 if (filter
->lua_state
)
267 if (!(filter
->lua_state
= luaL_newstate()))
270 luaL_openlibs(filter
->lua_state
);
272 lua_pushcfunction(filter
->lua_state
, html_lua_filter
);
273 lua_setglobal(filter
->lua_state
, "html");
274 lua_pushcfunction(filter
->lua_state
, html_txt_lua_filter
);
275 lua_setglobal(filter
->lua_state
, "html_txt");
276 lua_pushcfunction(filter
->lua_state
, html_attr_lua_filter
);
277 lua_setglobal(filter
->lua_state
, "html_attr");
278 lua_pushcfunction(filter
->lua_state
, html_url_path_lua_filter
);
279 lua_setglobal(filter
->lua_state
, "html_url_path");
280 lua_pushcfunction(filter
->lua_state
, html_url_arg_lua_filter
);
281 lua_setglobal(filter
->lua_state
, "html_url_arg");
283 if (luaL_dofile(filter
->lua_state
, filter
->script_file
)) {
284 error_lua_filter(filter
);
285 lua_close(filter
->lua_state
);
286 filter
->lua_state
= NULL
;
292 static int open_lua_filter(struct cgit_filter
*base
, va_list ap
)
294 struct lua_filter
*filter
= (struct lua_filter
*)base
;
297 if (init_lua_filter(filter
))
300 hook_write(base
, write_lua_filter
);
302 lua_getglobal(filter
->lua_state
, "filter_open");
303 for (i
= 0; i
< filter
->base
.argument_count
; ++i
)
304 lua_pushstring(filter
->lua_state
, va_arg(ap
, char *));
305 if (lua_pcall(filter
->lua_state
, filter
->base
.argument_count
, 0, 0)) {
306 error_lua_filter(filter
);
312 static int close_lua_filter(struct cgit_filter
*base
)
314 struct lua_filter
*filter
= (struct lua_filter
*)base
;
317 lua_getglobal(filter
->lua_state
, "filter_close");
318 if (lua_pcall(filter
->lua_state
, 0, 0, 0)) {
319 error_lua_filter(filter
);
326 static void fprintf_lua_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
328 struct lua_filter
*filter
= (struct lua_filter
*)base
;
329 fprintf(f
, "%slua:%s\n", prefix
, filter
->script_file
);
333 static struct cgit_filter
*new_lua_filter(const char *cmd
, int argument_count
)
335 struct lua_filter
*filter
;
337 filter
= xmalloc(sizeof(*filter
));
338 memset(filter
, 0, sizeof(*filter
));
339 filter
->base
.open
= open_lua_filter
;
340 filter
->base
.close
= close_lua_filter
;
341 filter
->base
.fprintf
= fprintf_lua_filter
;
342 filter
->base
.cleanup
= cleanup_lua_filter
;
343 filter
->base
.argument_count
= argument_count
;
344 filter
->script_file
= xstrdup(cmd
);
346 return &filter
->base
;
352 int cgit_open_filter(struct cgit_filter
*filter
, ...)
358 va_start(ap
, filter
);
359 result
= filter
->open(filter
, ap
);
364 int cgit_close_filter(struct cgit_filter
*filter
)
368 return filter
->close(filter
);
371 void cgit_fprintf_filter(struct cgit_filter
*filter
, FILE *f
, const char *prefix
)
373 filter
->fprintf(filter
, f
, prefix
);
378 static const struct {
380 struct cgit_filter
*(*ctor
)(const char *cmd
, int argument_count
);
382 { "exec", new_exec_filter
},
384 { "lua", new_lua_filter
},
388 struct cgit_filter
*cgit_new_filter(const char *cmd
, filter_type filtertype
)
398 colon
= strchr(cmd
, ':');
401 * In case we're running on Windows, don't allow a single letter before
407 switch (filtertype
) {
423 /* If no prefix is given, exec filter is the default. */
425 return new_exec_filter(cmd
, argument_count
);
427 for (i
= 0; i
< ARRAY_SIZE(filter_specs
); i
++) {
428 if (len
== strlen(filter_specs
[i
].prefix
) &&
429 !strncmp(filter_specs
[i
].prefix
, cmd
, len
))
430 return filter_specs
[i
].ctor(colon
+ 1, argument_count
);
433 die("Invalid filter type: %.*s", (int) len
, cmd
);