]>
git.cameronkatri.com Git - cgit.git/blob - filter.c
5ee4e71db3e6b239142fe612d6fb8fafa1f49a80
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
;
109 int i
, exit_status
= 0;
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
))
119 die("Subprocess %s exited abnormally", filter
->cmd
);
122 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
123 filter
->argv
[i
+ 1] = NULL
;
124 return WEXITSTATUS(exit_status
);
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 int html_include_lua_filter(lua_State
*lua_state
)
249 return hook_lua_filter(lua_state
, (void (*)(const char *))html_include
);
252 static void cleanup_lua_filter(struct cgit_filter
*base
)
254 struct lua_filter
*filter
= (struct lua_filter
*)base
;
256 if (!filter
->lua_state
)
259 lua_close(filter
->lua_state
);
260 filter
->lua_state
= NULL
;
261 if (filter
->script_file
) {
262 free(filter
->script_file
);
263 filter
->script_file
= NULL
;
267 static int init_lua_filter(struct lua_filter
*filter
)
269 if (filter
->lua_state
)
272 if (!(filter
->lua_state
= luaL_newstate()))
275 luaL_openlibs(filter
->lua_state
);
277 lua_pushcfunction(filter
->lua_state
, html_lua_filter
);
278 lua_setglobal(filter
->lua_state
, "html");
279 lua_pushcfunction(filter
->lua_state
, html_txt_lua_filter
);
280 lua_setglobal(filter
->lua_state
, "html_txt");
281 lua_pushcfunction(filter
->lua_state
, html_attr_lua_filter
);
282 lua_setglobal(filter
->lua_state
, "html_attr");
283 lua_pushcfunction(filter
->lua_state
, html_url_path_lua_filter
);
284 lua_setglobal(filter
->lua_state
, "html_url_path");
285 lua_pushcfunction(filter
->lua_state
, html_url_arg_lua_filter
);
286 lua_setglobal(filter
->lua_state
, "html_url_arg");
287 lua_pushcfunction(filter
->lua_state
, html_include_lua_filter
);
288 lua_setglobal(filter
->lua_state
, "html_include");
290 if (luaL_dofile(filter
->lua_state
, filter
->script_file
)) {
291 error_lua_filter(filter
);
292 lua_close(filter
->lua_state
);
293 filter
->lua_state
= NULL
;
299 static int open_lua_filter(struct cgit_filter
*base
, va_list ap
)
301 struct lua_filter
*filter
= (struct lua_filter
*)base
;
304 if (init_lua_filter(filter
))
307 hook_write(base
, write_lua_filter
);
309 lua_getglobal(filter
->lua_state
, "filter_open");
310 for (i
= 0; i
< filter
->base
.argument_count
; ++i
)
311 lua_pushstring(filter
->lua_state
, va_arg(ap
, char *));
312 if (lua_pcall(filter
->lua_state
, filter
->base
.argument_count
, 0, 0)) {
313 error_lua_filter(filter
);
319 static int close_lua_filter(struct cgit_filter
*base
)
321 struct lua_filter
*filter
= (struct lua_filter
*)base
;
324 lua_getglobal(filter
->lua_state
, "filter_close");
325 if (lua_pcall(filter
->lua_state
, 0, 1, 0)) {
326 error_lua_filter(filter
);
329 ret
= lua_tonumber(filter
->lua_state
, -1);
330 lua_pop(filter
->lua_state
, 1);
337 static void fprintf_lua_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
339 struct lua_filter
*filter
= (struct lua_filter
*)base
;
340 fprintf(f
, "%slua:%s\n", prefix
, filter
->script_file
);
344 static struct cgit_filter
*new_lua_filter(const char *cmd
, int argument_count
)
346 struct lua_filter
*filter
;
348 filter
= xmalloc(sizeof(*filter
));
349 memset(filter
, 0, sizeof(*filter
));
350 filter
->base
.open
= open_lua_filter
;
351 filter
->base
.close
= close_lua_filter
;
352 filter
->base
.fprintf
= fprintf_lua_filter
;
353 filter
->base
.cleanup
= cleanup_lua_filter
;
354 filter
->base
.argument_count
= argument_count
;
355 filter
->script_file
= xstrdup(cmd
);
357 return &filter
->base
;
363 int cgit_open_filter(struct cgit_filter
*filter
, ...)
369 va_start(ap
, filter
);
370 result
= filter
->open(filter
, ap
);
375 int cgit_close_filter(struct cgit_filter
*filter
)
379 return filter
->close(filter
);
382 void cgit_fprintf_filter(struct cgit_filter
*filter
, FILE *f
, const char *prefix
)
384 filter
->fprintf(filter
, f
, prefix
);
389 static const struct {
391 struct cgit_filter
*(*ctor
)(const char *cmd
, int argument_count
);
393 { "exec", new_exec_filter
},
395 { "lua", new_lua_filter
},
399 struct cgit_filter
*cgit_new_filter(const char *cmd
, filter_type filtertype
)
409 colon
= strchr(cmd
, ':');
412 * In case we're running on Windows, don't allow a single letter before
418 switch (filtertype
) {
438 /* If no prefix is given, exec filter is the default. */
440 return new_exec_filter(cmd
, argument_count
);
442 for (i
= 0; i
< ARRAY_SIZE(filter_specs
); i
++) {
443 if (len
== strlen(filter_specs
[i
].prefix
) &&
444 !strncmp(filter_specs
[i
].prefix
, cmd
, len
))
445 return filter_specs
[i
].ctor(colon
+ 1, argument_count
);
448 die("Invalid filter type: %.*s", (int) len
, cmd
);