]>
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)
18 static ssize_t (*libc_write
)(int fd
, const void *buf
, size_t count
);
19 static ssize_t (*filter_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
) = NULL
;
20 static struct cgit_filter
*current_write_filter
= NULL
;
22 static inline void reap_filter(struct cgit_filter
*filter
)
24 if (filter
&& filter
->cleanup
)
25 filter
->cleanup(filter
);
28 void cgit_cleanup_filters(void)
31 reap_filter(ctx
.cfg
.about_filter
);
32 reap_filter(ctx
.cfg
.commit_filter
);
33 reap_filter(ctx
.cfg
.source_filter
);
34 reap_filter(ctx
.cfg
.email_filter
);
35 reap_filter(ctx
.cfg
.owner_filter
);
36 reap_filter(ctx
.cfg
.auth_filter
);
37 for (i
= 0; i
< cgit_repolist
.count
; ++i
) {
38 reap_filter(cgit_repolist
.repos
[i
].about_filter
);
39 reap_filter(cgit_repolist
.repos
[i
].commit_filter
);
40 reap_filter(cgit_repolist
.repos
[i
].source_filter
);
41 reap_filter(cgit_repolist
.repos
[i
].email_filter
);
42 reap_filter(cgit_repolist
.repos
[i
].owner_filter
);
46 void cgit_init_filters(void)
48 libc_write
= dlsym(RTLD_NEXT
, "write");
50 die("Could not locate libc's write function");
53 ssize_t
write(int fd
, const void *buf
, size_t count
)
55 if (fd
!= STDOUT_FILENO
|| !filter_write
)
56 return libc_write(fd
, buf
, count
);
57 return filter_write(current_write_filter
, buf
, count
);
60 static inline void hook_write(struct cgit_filter
*filter
, ssize_t (*new_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
))
62 /* We want to avoid buggy nested patterns. */
63 assert(filter_write
== NULL
);
64 assert(current_write_filter
== NULL
);
65 current_write_filter
= filter
;
66 filter_write
= new_write
;
69 static inline void unhook_write(void)
71 assert(filter_write
!= NULL
);
72 assert(current_write_filter
!= NULL
);
74 current_write_filter
= NULL
;
77 static int open_exec_filter(struct cgit_filter
*base
, va_list ap
)
79 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
82 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
83 filter
->argv
[i
+ 1] = va_arg(ap
, char *);
85 filter
->old_stdout
= chk_positive(dup(STDOUT_FILENO
),
86 "Unable to duplicate STDOUT");
87 chk_zero(pipe(filter
->pipe_fh
), "Unable to create pipe to subprocess");
88 filter
->pid
= chk_non_negative(fork(), "Unable to create subprocess");
89 if (filter
->pid
== 0) {
90 close(filter
->pipe_fh
[1]);
91 chk_non_negative(dup2(filter
->pipe_fh
[0], STDIN_FILENO
),
92 "Unable to use pipe as STDIN");
93 execvp(filter
->cmd
, filter
->argv
);
94 die_errno("Unable to exec subprocess %s", filter
->cmd
);
96 close(filter
->pipe_fh
[0]);
97 chk_non_negative(dup2(filter
->pipe_fh
[1], STDOUT_FILENO
),
98 "Unable to use pipe as STDOUT");
99 close(filter
->pipe_fh
[1]);
103 static int close_exec_filter(struct cgit_filter
*base
)
105 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
106 int i
, exit_status
= 0;
108 chk_non_negative(dup2(filter
->old_stdout
, STDOUT_FILENO
),
109 "Unable to restore STDOUT");
110 close(filter
->old_stdout
);
113 waitpid(filter
->pid
, &exit_status
, 0);
114 if (WIFEXITED(exit_status
))
116 die("Subprocess %s exited abnormally", filter
->cmd
);
119 for (i
= 0; i
< filter
->base
.argument_count
; i
++)
120 filter
->argv
[i
+ 1] = NULL
;
121 return WEXITSTATUS(exit_status
);
125 static void fprintf_exec_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
127 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
128 fprintf(f
, "%sexec:%s\n", prefix
, filter
->cmd
);
131 static void cleanup_exec_filter(struct cgit_filter
*base
)
133 struct cgit_exec_filter
*filter
= (struct cgit_exec_filter
*)base
;
144 static struct cgit_filter
*new_exec_filter(const char *cmd
, int argument_count
)
146 struct cgit_exec_filter
*f
;
149 f
= xmalloc(sizeof(*f
));
150 /* We leave argv for now and assign it below. */
151 cgit_exec_filter_init(f
, xstrdup(cmd
), NULL
);
152 f
->base
.argument_count
= argument_count
;
153 args_size
= (2 + argument_count
) * sizeof(char *);
154 f
->argv
= xmalloc(args_size
);
155 memset(f
->argv
, 0, args_size
);
160 void cgit_exec_filter_init(struct cgit_exec_filter
*filter
, char *cmd
, char **argv
)
162 memset(filter
, 0, sizeof(*filter
));
163 filter
->base
.open
= open_exec_filter
;
164 filter
->base
.close
= close_exec_filter
;
165 filter
->base
.fprintf
= fprintf_exec_filter
;
166 filter
->base
.cleanup
= cleanup_exec_filter
;
169 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
170 filter
->base
.argument_count
= 0;
175 struct cgit_filter base
;
177 lua_State
*lua_state
;
180 static void error_lua_filter(struct lua_filter
*filter
)
182 die("Lua error in %s: %s", filter
->script_file
, lua_tostring(filter
->lua_state
, -1));
183 lua_pop(filter
->lua_state
, 1);
186 static ssize_t
write_lua_filter(struct cgit_filter
*base
, const void *buf
, size_t count
)
188 struct lua_filter
*filter
= (struct lua_filter
*)base
;
190 lua_getglobal(filter
->lua_state
, "filter_write");
191 lua_pushlstring(filter
->lua_state
, buf
, count
);
192 if (lua_pcall(filter
->lua_state
, 1, 0, 0)) {
193 error_lua_filter(filter
);
200 static inline int hook_lua_filter(lua_State
*lua_state
, void (*fn
)(const char *txt
))
203 ssize_t (*save_filter_write
)(struct cgit_filter
*base
, const void *buf
, size_t count
);
204 struct cgit_filter
*save_filter
;
206 str
= lua_tostring(lua_state
, 1);
210 save_filter_write
= filter_write
;
211 save_filter
= current_write_filter
;
214 hook_write(save_filter
, save_filter_write
);
219 static int html_lua_filter(lua_State
*lua_state
)
221 return hook_lua_filter(lua_state
, html
);
224 static int html_txt_lua_filter(lua_State
*lua_state
)
226 return hook_lua_filter(lua_state
, html_txt
);
229 static int html_attr_lua_filter(lua_State
*lua_state
)
231 return hook_lua_filter(lua_state
, html_attr
);
234 static int html_url_path_lua_filter(lua_State
*lua_state
)
236 return hook_lua_filter(lua_state
, html_url_path
);
239 static int html_url_arg_lua_filter(lua_State
*lua_state
)
241 return hook_lua_filter(lua_state
, html_url_arg
);
244 static int html_include_lua_filter(lua_State
*lua_state
)
246 return hook_lua_filter(lua_state
, (void (*)(const char *))html_include
);
249 static void cleanup_lua_filter(struct cgit_filter
*base
)
251 struct lua_filter
*filter
= (struct lua_filter
*)base
;
253 if (!filter
->lua_state
)
256 lua_close(filter
->lua_state
);
257 filter
->lua_state
= NULL
;
258 if (filter
->script_file
) {
259 free(filter
->script_file
);
260 filter
->script_file
= NULL
;
264 static int init_lua_filter(struct lua_filter
*filter
)
266 if (filter
->lua_state
)
269 if (!(filter
->lua_state
= luaL_newstate()))
272 luaL_openlibs(filter
->lua_state
);
274 lua_pushcfunction(filter
->lua_state
, html_lua_filter
);
275 lua_setglobal(filter
->lua_state
, "html");
276 lua_pushcfunction(filter
->lua_state
, html_txt_lua_filter
);
277 lua_setglobal(filter
->lua_state
, "html_txt");
278 lua_pushcfunction(filter
->lua_state
, html_attr_lua_filter
);
279 lua_setglobal(filter
->lua_state
, "html_attr");
280 lua_pushcfunction(filter
->lua_state
, html_url_path_lua_filter
);
281 lua_setglobal(filter
->lua_state
, "html_url_path");
282 lua_pushcfunction(filter
->lua_state
, html_url_arg_lua_filter
);
283 lua_setglobal(filter
->lua_state
, "html_url_arg");
284 lua_pushcfunction(filter
->lua_state
, html_include_lua_filter
);
285 lua_setglobal(filter
->lua_state
, "html_include");
287 if (luaL_dofile(filter
->lua_state
, filter
->script_file
)) {
288 error_lua_filter(filter
);
289 lua_close(filter
->lua_state
);
290 filter
->lua_state
= NULL
;
296 static int open_lua_filter(struct cgit_filter
*base
, va_list ap
)
298 struct lua_filter
*filter
= (struct lua_filter
*)base
;
301 if (init_lua_filter(filter
))
304 hook_write(base
, write_lua_filter
);
306 lua_getglobal(filter
->lua_state
, "filter_open");
307 for (i
= 0; i
< filter
->base
.argument_count
; ++i
)
308 lua_pushstring(filter
->lua_state
, va_arg(ap
, char *));
309 if (lua_pcall(filter
->lua_state
, filter
->base
.argument_count
, 0, 0)) {
310 error_lua_filter(filter
);
316 static int close_lua_filter(struct cgit_filter
*base
)
318 struct lua_filter
*filter
= (struct lua_filter
*)base
;
321 lua_getglobal(filter
->lua_state
, "filter_close");
322 if (lua_pcall(filter
->lua_state
, 0, 1, 0)) {
323 error_lua_filter(filter
);
326 ret
= lua_tonumber(filter
->lua_state
, -1);
327 lua_pop(filter
->lua_state
, 1);
334 static void fprintf_lua_filter(struct cgit_filter
*base
, FILE *f
, const char *prefix
)
336 struct lua_filter
*filter
= (struct lua_filter
*)base
;
337 fprintf(f
, "%slua:%s\n", prefix
, filter
->script_file
);
341 static struct cgit_filter
*new_lua_filter(const char *cmd
, int argument_count
)
343 struct lua_filter
*filter
;
345 filter
= xmalloc(sizeof(*filter
));
346 memset(filter
, 0, sizeof(*filter
));
347 filter
->base
.open
= open_lua_filter
;
348 filter
->base
.close
= close_lua_filter
;
349 filter
->base
.fprintf
= fprintf_lua_filter
;
350 filter
->base
.cleanup
= cleanup_lua_filter
;
351 filter
->base
.argument_count
= argument_count
;
352 filter
->script_file
= xstrdup(cmd
);
354 return &filter
->base
;
360 int cgit_open_filter(struct cgit_filter
*filter
, ...)
366 va_start(ap
, filter
);
367 result
= filter
->open(filter
, ap
);
372 int cgit_close_filter(struct cgit_filter
*filter
)
376 return filter
->close(filter
);
379 void cgit_fprintf_filter(struct cgit_filter
*filter
, FILE *f
, const char *prefix
)
381 filter
->fprintf(filter
, f
, prefix
);
386 static const struct {
388 struct cgit_filter
*(*ctor
)(const char *cmd
, int argument_count
);
390 { "exec", new_exec_filter
},
392 { "lua", new_lua_filter
},
396 struct cgit_filter
*cgit_new_filter(const char *cmd
, filter_type filtertype
)
406 colon
= strchr(cmd
, ':');
409 * In case we're running on Windows, don't allow a single letter before
415 switch (filtertype
) {
439 /* If no prefix is given, exec filter is the default. */
441 return new_exec_filter(cmd
, argument_count
);
443 for (i
= 0; i
< ARRAY_SIZE(filter_specs
); i
++) {
444 if (len
== strlen(filter_specs
[i
].prefix
) &&
445 !strncmp(filter_specs
[i
].prefix
, cmd
, len
))
446 return filter_specs
[i
].ctor(colon
+ 1, argument_count
);
449 die("Invalid filter type: %.*s", (int) len
, cmd
);