]> git.cameronkatri.com Git - cgit.git/blob - filter.c
ui-tree: use "sane" isgraph()
[cgit.git] / 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 <dlfcn.h>
12 #ifndef NO_LUA
13 #include <lua.h>
14 #include <lualib.h>
15 #include <lauxlib.h>
16 #endif
17
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;
21
22 static inline void reap_filter(struct cgit_filter *filter)
23 {
24 if (filter && filter->cleanup)
25 filter->cleanup(filter);
26 }
27
28 void cgit_cleanup_filters(void)
29 {
30 int i;
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);
43 }
44 }
45
46 void cgit_init_filters(void)
47 {
48 libc_write = dlsym(RTLD_NEXT, "write");
49 if (!libc_write)
50 die("Could not locate libc's write function");
51 }
52
53 ssize_t write(int fd, const void *buf, size_t count)
54 {
55 if (fd != STDOUT_FILENO || !filter_write)
56 return libc_write(fd, buf, count);
57 return filter_write(current_write_filter, buf, count);
58 }
59
60 static inline void hook_write(struct cgit_filter *filter, ssize_t (*new_write)(struct cgit_filter *base, const void *buf, size_t count))
61 {
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;
67 }
68
69 static inline void unhook_write(void)
70 {
71 assert(filter_write != NULL);
72 assert(current_write_filter != NULL);
73 filter_write = NULL;
74 current_write_filter = NULL;
75 }
76
77 static int open_exec_filter(struct cgit_filter *base, va_list ap)
78 {
79 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
80 int i;
81
82 for (i = 0; i < filter->base.argument_count; i++)
83 filter->argv[i + 1] = va_arg(ap, char *);
84
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);
95 }
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]);
100 return 0;
101 }
102
103 static int close_exec_filter(struct cgit_filter *base)
104 {
105 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
106 int i, exit_status = 0;
107
108 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
109 "Unable to restore STDOUT");
110 close(filter->old_stdout);
111 if (filter->pid < 0)
112 goto done;
113 waitpid(filter->pid, &exit_status, 0);
114 if (WIFEXITED(exit_status))
115 goto done;
116 die("Subprocess %s exited abnormally", filter->cmd);
117
118 done:
119 for (i = 0; i < filter->base.argument_count; i++)
120 filter->argv[i + 1] = NULL;
121 return WEXITSTATUS(exit_status);
122
123 }
124
125 static void fprintf_exec_filter(struct cgit_filter *base, FILE *f, const char *prefix)
126 {
127 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
128 fprintf(f, "%sexec:%s\n", prefix, filter->cmd);
129 }
130
131 static void cleanup_exec_filter(struct cgit_filter *base)
132 {
133 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
134 if (filter->argv) {
135 free(filter->argv);
136 filter->argv = NULL;
137 }
138 if (filter->cmd) {
139 free(filter->cmd);
140 filter->cmd = NULL;
141 }
142 }
143
144 static struct cgit_filter *new_exec_filter(const char *cmd, int argument_count)
145 {
146 struct cgit_exec_filter *f;
147 int args_size = 0;
148
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);
156 f->argv[0] = f->cmd;
157 return &f->base;
158 }
159
160 void cgit_exec_filter_init(struct cgit_exec_filter *filter, char *cmd, char **argv)
161 {
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;
167 filter->cmd = cmd;
168 filter->argv = argv;
169 /* The argument count for open_filter is zero by default, unless called from new_filter, above. */
170 filter->base.argument_count = 0;
171 }
172
173 #ifndef NO_LUA
174 struct lua_filter {
175 struct cgit_filter base;
176 char *script_file;
177 lua_State *lua_state;
178 };
179
180 static void error_lua_filter(struct lua_filter *filter)
181 {
182 die("Lua error in %s: %s", filter->script_file, lua_tostring(filter->lua_state, -1));
183 lua_pop(filter->lua_state, 1);
184 }
185
186 static ssize_t write_lua_filter(struct cgit_filter *base, const void *buf, size_t count)
187 {
188 struct lua_filter *filter = (struct lua_filter *)base;
189
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);
194 errno = EIO;
195 return -1;
196 }
197 return count;
198 }
199
200 static inline int hook_lua_filter(lua_State *lua_state, void (*fn)(const char *txt))
201 {
202 const char *str;
203 ssize_t (*save_filter_write)(struct cgit_filter *base, const void *buf, size_t count);
204 struct cgit_filter *save_filter;
205
206 str = lua_tostring(lua_state, 1);
207 if (!str)
208 return 0;
209
210 save_filter_write = filter_write;
211 save_filter = current_write_filter;
212 unhook_write();
213 fn(str);
214 hook_write(save_filter, save_filter_write);
215
216 return 0;
217 }
218
219 static int html_lua_filter(lua_State *lua_state)
220 {
221 return hook_lua_filter(lua_state, html);
222 }
223
224 static int html_txt_lua_filter(lua_State *lua_state)
225 {
226 return hook_lua_filter(lua_state, html_txt);
227 }
228
229 static int html_attr_lua_filter(lua_State *lua_state)
230 {
231 return hook_lua_filter(lua_state, html_attr);
232 }
233
234 static int html_url_path_lua_filter(lua_State *lua_state)
235 {
236 return hook_lua_filter(lua_state, html_url_path);
237 }
238
239 static int html_url_arg_lua_filter(lua_State *lua_state)
240 {
241 return hook_lua_filter(lua_state, html_url_arg);
242 }
243
244 static int html_include_lua_filter(lua_State *lua_state)
245 {
246 return hook_lua_filter(lua_state, (void (*)(const char *))html_include);
247 }
248
249 static void cleanup_lua_filter(struct cgit_filter *base)
250 {
251 struct lua_filter *filter = (struct lua_filter *)base;
252
253 if (!filter->lua_state)
254 return;
255
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;
261 }
262 }
263
264 static int init_lua_filter(struct lua_filter *filter)
265 {
266 if (filter->lua_state)
267 return 0;
268
269 if (!(filter->lua_state = luaL_newstate()))
270 return 1;
271
272 luaL_openlibs(filter->lua_state);
273
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");
286
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;
291 return 1;
292 }
293 return 0;
294 }
295
296 static int open_lua_filter(struct cgit_filter *base, va_list ap)
297 {
298 struct lua_filter *filter = (struct lua_filter *)base;
299 int i;
300
301 if (init_lua_filter(filter))
302 return 1;
303
304 hook_write(base, write_lua_filter);
305
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);
311 return 1;
312 }
313 return 0;
314 }
315
316 static int close_lua_filter(struct cgit_filter *base)
317 {
318 struct lua_filter *filter = (struct lua_filter *)base;
319 int ret = 0;
320
321 lua_getglobal(filter->lua_state, "filter_close");
322 if (lua_pcall(filter->lua_state, 0, 1, 0)) {
323 error_lua_filter(filter);
324 ret = -1;
325 } else {
326 ret = lua_tonumber(filter->lua_state, -1);
327 lua_pop(filter->lua_state, 1);
328 }
329
330 unhook_write();
331 return ret;
332 }
333
334 static void fprintf_lua_filter(struct cgit_filter *base, FILE *f, const char *prefix)
335 {
336 struct lua_filter *filter = (struct lua_filter *)base;
337 fprintf(f, "%slua:%s\n", prefix, filter->script_file);
338 }
339
340
341 static struct cgit_filter *new_lua_filter(const char *cmd, int argument_count)
342 {
343 struct lua_filter *filter;
344
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);
353
354 return &filter->base;
355 }
356
357 #endif
358
359
360 int cgit_open_filter(struct cgit_filter *filter, ...)
361 {
362 int result;
363 va_list ap;
364 if (!filter)
365 return 0;
366 va_start(ap, filter);
367 result = filter->open(filter, ap);
368 va_end(ap);
369 return result;
370 }
371
372 int cgit_close_filter(struct cgit_filter *filter)
373 {
374 if (!filter)
375 return 0;
376 return filter->close(filter);
377 }
378
379 void cgit_fprintf_filter(struct cgit_filter *filter, FILE *f, const char *prefix)
380 {
381 filter->fprintf(filter, f, prefix);
382 }
383
384
385
386 static const struct {
387 const char *prefix;
388 struct cgit_filter *(*ctor)(const char *cmd, int argument_count);
389 } filter_specs[] = {
390 { "exec", new_exec_filter },
391 #ifndef NO_LUA
392 { "lua", new_lua_filter },
393 #endif
394 };
395
396 struct cgit_filter *cgit_new_filter(const char *cmd, filter_type filtertype)
397 {
398 char *colon;
399 int i;
400 size_t len;
401 int argument_count;
402
403 if (!cmd || !cmd[0])
404 return NULL;
405
406 colon = strchr(cmd, ':');
407 len = colon - cmd;
408 /*
409 * In case we're running on Windows, don't allow a single letter before
410 * the colon.
411 */
412 if (len == 1)
413 colon = NULL;
414
415 switch (filtertype) {
416 case AUTH:
417 argument_count = 12;
418 break;
419
420 case EMAIL:
421 argument_count = 2;
422 break;
423
424 case OWNER:
425 argument_count = 0;
426 break;
427
428 case SOURCE:
429 case ABOUT:
430 argument_count = 1;
431 break;
432
433 case COMMIT:
434 default:
435 argument_count = 0;
436 break;
437 }
438
439 /* If no prefix is given, exec filter is the default. */
440 if (!colon)
441 return new_exec_filter(cmd, argument_count);
442
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);
447 }
448
449 die("Invalid filter type: %.*s", (int) len, cmd);
450 }