]>
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 for (i
= 0; i
< cgit_repolist
.count
; ++i
) {
41 reap_filter(cgit_repolist
.repos
[i
].about_filter
);
42 reap_filter(cgit_repolist
.repos
[i
].commit_filter
);
43 reap_filter(cgit_repolist
.repos
[i
].source_filter
);
47 void cgit_init_filters(void)
49 libc_write
= dlsym(RTLD_NEXT
, "write");
51 die("Could not locate libc's write function");
54 ssize_t
write(int fd
, const void *buf
, size_t count
)
56 if (fd
!= STDOUT_FILENO
|| !filter_write
)
57 return libc_write(fd
, buf
, count
);
58 return filter_write(current_write_filter
, buf
, count
);
61 static inline void hook_write(struct cgit_filter
*filter
, ssize_t (*new_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
))
63 /* We want to avoid buggy nested patterns. */
64 assert(filter_write
== NULL
);
65 assert(current_write_filter
== NULL
);
66 current_write_filter
= filter
;
67 filter_write
= new_write
;
70 static inline void unhook_write()
72 assert(filter_write
!= NULL
);
73 assert(current_write_filter
!= NULL
);
75 current_write_filter
= NULL
;
78 static int open_exec_filter(struct cgit_filter
*base
, va_list ap
)
80 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*) base
;
83 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
84 filter
->argv
[i
+1] = va_arg(ap
, char *);
86 filter
->old_stdout
= chk_positive(dup(STDOUT_FILENO
),
87 "Unable to duplicate STDOUT");
88 chk_zero(pipe(filter
->pipe_fh
), "Unable to create pipe to subprocess");
89 filter
->pid
= chk_non_negative(fork(), "Unable to create subprocess");
90 if (filter
->pid
== 0) {
91 close(filter
->pipe_fh
[1]);
92 chk_non_negative(dup2(filter
->pipe_fh
[0], STDIN_FILENO
),
93 "Unable to use pipe as STDIN");
94 execvp(filter
->cmd
, filter
->argv
);
95 die_errno("Unable to exec subprocess %s", filter
->cmd
);
97 close(filter
->pipe_fh
[0]);
98 chk_non_negative(dup2(filter
->pipe_fh
[1], STDOUT_FILENO
),
99 "Unable to use pipe as STDOUT");
100 close(filter
->pipe_fh
[1]);
104 static int close_exec_filter(struct cgit_filter
*base
)
106 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*) base
;
109 chk_non_negative(dup2(filter
->old_stdout
, STDOUT_FILENO
),
110 "Unable to restore STDOUT");
111 close(filter
->old_stdout
);
114 waitpid(filter
->pid
, &exit_status
, 0);
115 if (WIFEXITED(exit_status
) && !WEXITSTATUS(exit_status
))
117 die("Subprocess %s exited abnormally", filter
->cmd
);
120 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
121 filter
->argv
[i
+1] = NULL
;
126 static void fprintf_exec_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
128 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*) base
;
129 fprintf(f
, "%sexec:%s\n", prefix
, filter
->cmd
);
132 static void cleanup_exec_filter(struct cgit_filter
*base
)
134 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*) base
;
145 static struct cgit_filter
*new_exec_filter(const char *cmd
, int argument_count
)
147 struct cgit_exec_filter
*f
;
150 f
= xmalloc(sizeof(*f
));
151 /* We leave argv for now and assign it below. */
152 cgit_exec_filter_init(f
, xstrdup(cmd
), NULL
);
153 f
->base
.argument_count
= argument_count
;
154 args_size
= (2 + argument_count
) * sizeof(char *);
155 f
->argv
= xmalloc(args_size
);
156 memset(f
->argv
, 0, args_size
);
161 void cgit_exec_filter_init(struct cgit_exec_filter
*filter
, char *cmd
, char **argv
)
163 memset(filter
, 0, sizeof(*filter
));
164 filter
->base
.open
= open_exec_filter
;
165 filter
->base
.close
= close_exec_filter
;
166 filter
->base
.fprintf
= fprintf_exec_filter
;
167 filter
->base
.cleanup
= cleanup_exec_filter
;
170 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
171 filter
->base
.argument_count
= 0;
176 struct cgit_filter base
;
178 lua_State
*lua_state
;
181 static void error_lua_filter(struct lua_filter
*filter
)
183 die("Lua error in %s: %s", filter
->script_file
, lua_tostring(filter
->lua_state
, -1));
184 lua_pop(filter
->lua_state
, 1);
187 static ssize_t
write_lua_filter(struct cgit_filter
*base
, const void *buf
, size_t count
)
189 struct lua_filter
*filter
= (struct lua_filter
*) base
;
191 lua_getglobal(filter
->lua_state
, "filter_write");
192 lua_pushlstring(filter
->lua_state
, buf
, count
);
193 if (lua_pcall(filter
->lua_state
, 1, 0, 0)) {
194 error_lua_filter(filter
);
201 static inline int hook_lua_filter(lua_State
*lua_state
, void (*fn
)(const char *txt
))
204 ssize_t (*save_filter_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
);
205 struct cgit_filter
*save_filter
;
207 str
= lua_tostring(lua_state
, 1);
211 save_filter_write
= filter_write
;
212 save_filter
= current_write_filter
;
215 hook_write(save_filter
, save_filter_write
);
220 static int html_lua_filter(lua_State
*lua_state
)
222 return hook_lua_filter(lua_state
, html
);
225 static int html_txt_lua_filter(lua_State
*lua_state
)
227 return hook_lua_filter(lua_state
, html_txt
);
230 static int html_attr_lua_filter(lua_State
*lua_state
)
232 return hook_lua_filter(lua_state
, html_attr
);
235 static int html_url_path_lua_filter(lua_State
*lua_state
)
237 return hook_lua_filter(lua_state
, html_url_path
);
240 static int html_url_arg_lua_filter(lua_State
*lua_state
)
242 return hook_lua_filter(lua_state
, html_url_arg
);
245 static void cleanup_lua_filter(struct cgit_filter
*base
)
247 struct lua_filter
*filter
= (struct lua_filter
*) base
;
249 if (!filter
->lua_state
)
252 lua_close(filter
->lua_state
);
253 filter
->lua_state
= NULL
;
254 if (filter
->script_file
) {
255 free(filter
->script_file
);
256 filter
->script_file
= NULL
;
260 static int init_lua_filter(struct lua_filter
*filter
)
262 if (filter
->lua_state
)
265 if (!(filter
->lua_state
= luaL_newstate()))
268 luaL_openlibs(filter
->lua_state
);
270 lua_pushcfunction(filter
->lua_state
, html_lua_filter
);
271 lua_setglobal(filter
->lua_state
, "html");
272 lua_pushcfunction(filter
->lua_state
, html_txt_lua_filter
);
273 lua_setglobal(filter
->lua_state
, "html_txt");
274 lua_pushcfunction(filter
->lua_state
, html_attr_lua_filter
);
275 lua_setglobal(filter
->lua_state
, "html_attr");
276 lua_pushcfunction(filter
->lua_state
, html_url_path_lua_filter
);
277 lua_setglobal(filter
->lua_state
, "html_url_path");
278 lua_pushcfunction(filter
->lua_state
, html_url_arg_lua_filter
);
279 lua_setglobal(filter
->lua_state
, "html_url_arg");
281 if (luaL_dofile(filter
->lua_state
, filter
->script_file
)) {
282 error_lua_filter(filter
);
283 lua_close(filter
->lua_state
);
284 filter
->lua_state
= NULL
;
290 static int open_lua_filter(struct cgit_filter
*base
, va_list ap
)
292 struct lua_filter
*filter
= (struct lua_filter
*) base
;
295 if (init_lua_filter(filter
))
298 hook_write(base
, write_lua_filter
);
300 lua_getglobal(filter
->lua_state
, "filter_open");
301 for (i
= 0; i
< filter
->base
.argument_count
; ++i
)
302 lua_pushstring(filter
->lua_state
, va_arg(ap
, char *));
303 if (lua_pcall(filter
->lua_state
, filter
->base
.argument_count
, 0, 0)) {
304 error_lua_filter(filter
);
310 static int close_lua_filter(struct cgit_filter
*base
)
312 struct lua_filter
*filter
= (struct lua_filter
*) base
;
315 lua_getglobal(filter
->lua_state
, "filter_close");
316 if (lua_pcall(filter
->lua_state
, 0, 0, 0)) {
317 error_lua_filter(filter
);
324 static void fprintf_lua_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
326 struct lua_filter
*filter
= (struct lua_filter
*) base
;
327 fprintf(f
, "%slua:%s\n", prefix
, filter
->script_file
);
331 static struct cgit_filter
*new_lua_filter(const char *cmd
, int argument_count
)
333 struct lua_filter
*filter
;
335 filter
= xmalloc(sizeof(*filter
));
336 memset(filter
, 0, sizeof(*filter
));
337 filter
->base
.open
= open_lua_filter
;
338 filter
->base
.close
= close_lua_filter
;
339 filter
->base
.fprintf
= fprintf_lua_filter
;
340 filter
->base
.cleanup
= cleanup_lua_filter
;
341 filter
->base
.argument_count
= argument_count
;
342 filter
->script_file
= xstrdup(cmd
);
344 return &filter
->base
;
350 int cgit_open_filter(struct cgit_filter
*filter
, ...)
356 va_start(ap
, filter
);
357 result
= filter
->open(filter
, ap
);
362 int cgit_close_filter(struct cgit_filter
*filter
)
366 return filter
->close(filter
);
369 void cgit_fprintf_filter(struct cgit_filter
*filter
, FILE *f
, const char *prefix
)
371 filter
->fprintf(filter
, f
, prefix
);
376 static const struct {
378 struct cgit_filter
*(*ctor
)(const char *cmd
, int argument_count
);
380 { "exec", new_exec_filter
},
382 { "lua", new_lua_filter
},
386 struct cgit_filter
*cgit_new_filter(const char *cmd
, filter_type filtertype
)
396 colon
= strchr(cmd
, ':');
399 * In case we're running on Windows, don't allow a single letter before
405 switch (filtertype
) {
417 /* If no prefix is given, exec filter is the default. */
419 return new_exec_filter(cmd
, argument_count
);
421 for (i
= 0; i
< ARRAY_SIZE(filter_specs
); i
++) {
422 if (len
== strlen(filter_specs
[i
].prefix
) &&
423 !strncmp(filter_specs
[i
].prefix
, cmd
, len
))
424 return filter_specs
[i
].ctor(colon
+ 1, argument_count
);
427 die("Invalid filter type: %.*s", (int) len
, cmd
);