]> gitweb.ps.run Git - ps-cgit/blob - filter.c
filter: add lua support
[ps-cgit] / filter.c
1 /* filter.c: filter framework functions
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "html.h"
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <dlfcn.h>
17 #include <errno.h>
18 #ifndef NO_LUA
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #endif
23
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;
27
28 static inline void reap_filter(struct cgit_filter *filter)
29 {
30         if (filter && filter->cleanup)
31                 filter->cleanup(filter);
32 }
33
34 void cgit_cleanup_filters(void)
35 {
36         int i;
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);
44         }
45 }
46
47 void cgit_init_filters(void)
48 {
49         libc_write = dlsym(RTLD_NEXT, "write");
50         if (!libc_write)
51                 die("Could not locate libc's write function");
52 }
53
54 ssize_t write(int fd, const void *buf, size_t count)
55 {
56         if (fd != STDOUT_FILENO || !filter_write)
57                 return libc_write(fd, buf, count);
58         return filter_write(current_write_filter, buf, count);
59 }
60
61 static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
62 {
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;
68 }
69
70 static inline void unhook_write()
71 {
72         assert(filter_write != NULL);
73         assert(current_write_filter != NULL);
74         filter_write = NULL;
75         current_write_filter = NULL;
76 }
77
78 static int open_exec_filter(struct cgit_filter *base, va_list ap)
79 {
80         struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
81         int i;
82
83         for (i = 0; i < filter->base.argument_count; i++)
84                 filter->argv[i+1] = va_arg(ap, char *);
85
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);
96         }
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]);
101         return 0;
102 }
103
104 static int close_exec_filter(struct cgit_filter *base)
105 {
106         struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
107         int i, exit_status;
108
109         chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
110                 "Unable to restore STDOUT");
111         close(filter->old_stdout);
112         if (filter->pid < 0)
113                 goto done;
114         waitpid(filter->pid, &exit_status, 0);
115         if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status))
116                 goto done;
117         die("Subprocess %s exited abnormally", filter->cmd);
118
119 done:
120         for (i = 0; i < filter->base.argument_count; i++)
121                 filter->argv[i+1] = NULL;
122         return 0;
123
124 }
125
126 static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
127 {
128         struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
129         fprintf(f, "%sexec:%s\n", prefix, filter->cmd);
130 }
131
132 static void cleanup_exec_filter(struct cgit_filter *base)
133 {
134         struct cgit_exec_filter *filter = (struct cgit_exec_filter *) base;
135         if (filter->argv) {
136                 free(filter->argv);
137                 filter->argv = NULL;
138         }
139         if (filter->cmd) {
140                 free(filter->cmd);
141                 filter->cmd = NULL;
142         }
143 }
144
145 static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count)
146 {
147         struct cgit_exec_filter *f;
148         int args_size = 0;
149
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);
157         f->argv[0] = f->cmd;
158         return &f->base;
159 }
160
161 void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv)
162 {
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;
168         filter->cmd = cmd;
169         filter->argv = argv;
170         /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
171         filter->base.argument_count = 0;
172 }
173
174 #ifndef NO_LUA
175 struct lua_filter {
176         struct cgit_filter base;
177         char *script_file;
178         lua_State *lua_state;
179 };
180
181 static void error_lua_filter(struct lua_filter *filter)
182 {
183         die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1));
184         lua_pop(filter->lua_state, 1);
185 }
186
187 static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count)
188 {
189         struct lua_filter *filter = (struct lua_filter *) base;
190
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);
195                 errno = EIO;
196                 return -1;
197         }
198         return count;
199 }
200
201 static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt))
202 {
203         const char *str;
204         ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count);
205         struct cgit_filter *save_filter;
206
207         str = lua_tostring(lua_state, 1);
208         if (!str)
209                 return 0;
210         
211         save_filter_write = filter_write;
212         save_filter = current_write_filter;
213         unhook_write();
214         fn(str);
215         hook_write(save_filter, save_filter_write);
216
217         return 0;
218 }
219
220 static int html_lua_filter(lua_State *lua_state)
221 {
222         return hook_lua_filter(lua_state, html);
223 }
224
225 static int html_txt_lua_filter(lua_State *lua_state)
226 {
227         return hook_lua_filter(lua_state, html_txt);
228 }
229
230 static int html_attr_lua_filter(lua_State *lua_state)
231 {
232         return hook_lua_filter(lua_state, html_attr);
233 }
234
235 static int html_url_path_lua_filter(lua_State *lua_state)
236 {
237         return hook_lua_filter(lua_state, html_url_path);
238 }
239
240 static int html_url_arg_lua_filter(lua_State *lua_state)
241 {
242         return hook_lua_filter(lua_state, html_url_arg);
243 }
244
245 static void cleanup_lua_filter(struct cgit_filter *base)
246 {
247         struct lua_filter *filter = (struct lua_filter *) base;
248
249         if (!filter->lua_state)
250                 return;
251
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;
257         }
258 }
259
260 static int init_lua_filter(struct lua_filter *filter)
261 {
262         if (filter->lua_state)
263                 return 0;
264
265         if (!(filter->lua_state = luaL_newstate()))
266                 return 1;
267
268         luaL_openlibs(filter->lua_state);
269
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");
280
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;
285                 return 1;
286         }
287         return 0;
288 }
289
290 static int open_lua_filter(struct cgit_filter *base, va_list ap)
291 {
292         struct lua_filter *filter = (struct lua_filter *) base;
293         int i;
294
295         if (init_lua_filter(filter))
296                 return 1;
297
298         hook_write(base, write_lua_filter);
299
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);
305                 return 1;
306         }
307         return 0;
308 }
309
310 static int close_lua_filter(struct cgit_filter *base)
311 {
312         struct lua_filter *filter = (struct lua_filter *) base;
313         int ret = 0;
314
315         lua_getglobal(filter->lua_state, "filter_close");
316         if (lua_pcall(filter->lua_state, 0, 0, 0)) {
317                 error_lua_filter(filter);
318                 ret = 1;
319         }
320         unhook_write();
321         return ret;
322 }
323
324 static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix)
325 {
326         struct lua_filter *filter = (struct lua_filter *) base;
327         fprintf(f, "%slua:%s\n", prefix, filter->script_file);
328 }
329
330
331 static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count)
332 {
333         struct lua_filter *filter;
334
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);
343
344         return &filter->base;
345 }
346
347 #endif
348
349
350 int cgit_open_filter(struct cgit_filter *filter, ...)
351 {
352         int result;
353         va_list ap;
354         va_start(ap, filter);
355         result = filter->open(filter, ap);
356         va_end(ap);
357         return result;
358 }
359
360 int cgit_close_filter(struct cgit_filter *filter)
361 {
362         return filter->close(filter);
363 }
364
365 void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix)
366 {
367         filter->fprintf(filter, f, prefix);
368 }
369
370
371
372 static const struct {
373         const char *prefix;
374         struct cgit_filter *(*ctor)(const char *cmd, int argument_count);
375 } filter_specs[] = {
376         { "exec", new_exec_filter },
377 #ifndef NO_LUA
378         { "lua", new_lua_filter },
379 #endif
380 };
381
382 struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
383 {
384         char *colon;
385         int i;
386         size_t len;
387         int argument_count;
388
389         if (!cmd || !cmd[0])
390                 return NULL;
391
392         colon = strchr(cmd, ':');
393         len = colon - cmd;
394         /*
395          * In case we're running on Windows, don't allow a single letter before
396          * the colon.
397          */
398         if (len == 1)
399                 colon = NULL;
400
401         switch (filtertype) {
402                 case SOURCE:
403                 case ABOUT:
404                         argument_count = 1;
405                         break;
406
407                 case COMMIT:
408                 default:
409                         argument_count = 0;
410                         break;
411         }
412
413         /* If no prefix is given, exec filter is the default. */
414         if (!colon)
415                 return new_exec_filter(cmd, argument_count);
416
417         for (i = 0; i < ARRAY_SIZE(filter_specs); i++) {
418                 if (len == strlen(filter_specs[i].prefix) &&
419                     !strncmp(filter_specs[i].prefix, cmd, len))
420                         return filter_specs[i].ctor(colon + 1, argument_count);
421         }
422
423         die("Invalid filter type: %.*s", (int) len, cmd);
424 }