aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-15 02:07:23 (JST)
committerGravatar Jason A. Donenfeld <Jason@zx2c4.com>2014-01-15 02:09:52 (JST)
commit6ca734da8fb246ad2272826331e0d56428b96fa1 (patch)
tree01abfea3df45135b1114996de29093b6eed7023a
parentce56d89a2662549acd178292450798f5ffcd4bc6 (diff)
downloadcgit-6ca734da8fb246ad2272826331e0d56428b96fa1.zip
cgit-6ca734da8fb246ad2272826331e0d56428b96fa1.tar.gz
filter: allow returning exit code from filter
Filters can now indicate a status back to cgit by means of the exit code for exec, or the return value from close for Lua. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--cgitrc.5.txt3
-rw-r--r--filter.c14
-rw-r--r--filters/email-gravatar.lua1
3 files changed, 12 insertions, 6 deletions
diff --git a/cgitrc.5.txt b/cgitrc.5.txt
index d8f7d0e..170e825 100644
--- a/cgitrc.5.txt
+++ b/cgitrc.5.txt
@@ -590,7 +590,8 @@ specification with the relevant string; available values are:
590 This is called whenever cgit writes data to the webpage. 590 This is called whenever cgit writes data to the webpage.
591 'filter_close()':: 591 'filter_close()'::
592 This is called when the current filtering operation is 592 This is called when the current filtering operation is
593 completed. 593 completed. It must return an integer value. Usually 0
594 indicates success.
594 595
595 Additionally, cgit exposes to the Lua the following built-in functions: 596 Additionally, cgit exposes to the Lua the following built-in functions:
596 597
diff --git a/filter.c b/filter.c
index 4d4acaf..0cce7bb 100644
--- a/filter.c
+++ b/filter.c
@@ -106,7 +106,7 @@ static int open_exec_filter(struct cgit_filter *base, va_list ap)
106static int close_exec_filter(struct cgit_filter *base) 106static int close_exec_filter(struct cgit_filter *base)
107{ 107{
108 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base; 108 struct cgit_exec_filter *filter = (struct cgit_exec_filter *)base;
109 int i, exit_status; 109 int i, exit_status = 0;
110 110
111 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO), 111 chk_non_negative(dup2(filter->old_stdout, STDOUT_FILENO),
112 "Unable to restore STDOUT"); 112 "Unable to restore STDOUT");
@@ -114,14 +114,14 @@ static int close_exec_filter(struct cgit_filter *base)
114 if (filter->pid < 0) 114 if (filter->pid < 0)
115 goto done; 115 goto done;
116 waitpid(filter->pid, &exit_status, 0); 116 waitpid(filter->pid, &exit_status, 0);
117 if (WIFEXITED(exit_status) && !WEXITSTATUS(exit_status)) 117 if (WIFEXITED(exit_status))
118 goto done; 118 goto done;
119 die("Subprocess %s exited abnormally", filter->cmd); 119 die("Subprocess %s exited abnormally", filter->cmd);
120 120
121done: 121done:
122 for (i = 0; i < filter->base.argument_count; i++) 122 for (i = 0; i < filter->base.argument_count; i++)
123 filter->argv[i + 1] = NULL; 123 filter->argv[i + 1] = NULL;
124 return 0; 124 return WEXITSTATUS(exit_status);
125 125
126} 126}
127 127
@@ -315,10 +315,14 @@ static int close_lua_filter(struct cgit_filter *base)
315 int ret = 0; 315 int ret = 0;
316 316
317 lua_getglobal(filter->lua_state, "filter_close"); 317 lua_getglobal(filter->lua_state, "filter_close");
318 if (lua_pcall(filter->lua_state, 0, 0, 0)) { 318 if (lua_pcall(filter->lua_state, 0, 1, 0)) {
319 error_lua_filter(filter); 319 error_lua_filter(filter);
320 ret = 1; 320 ret = -1;
321 } else {
322 ret = lua_tonumber(filter->lua_state, -1);
323 lua_pop(filter->lua_state, 1);
321 } 324 }
325
322 unhook_write(); 326 unhook_write();
323 return ret; 327 return ret;
324} 328}
diff --git a/filters/email-gravatar.lua b/filters/email-gravatar.lua
index 22f0641..c80b63e 100644
--- a/filters/email-gravatar.lua
+++ b/filters/email-gravatar.lua
@@ -16,6 +16,7 @@ end
16 16
17function filter_close() 17function filter_close()
18 html("<img src='//www.gravatar.com/avatar/" .. md5 .. "?s=13&amp;d=retro' style='height:10pt;width:10pt' alt='Gravatar' /> " .. buffer) 18 html("<img src='//www.gravatar.com/avatar/" .. md5 .. "?s=13&amp;d=retro' style='height:10pt;width:10pt' alt='Gravatar' /> " .. buffer)
19 return 0
19end 20end
20 21
21function filter_write(str) 22function filter_write(str)