123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <konf/buf.h>
- #include <lub/string.h>
- #include "private.h"
- static int exec_action(lua_State *L, const char *script)
- {
- int res = 0;
- if ((res = luaL_loadstring(L, script))) {
- l_print_error(L, __func__, "load", res);
- } else if ((res = lua_pcall(L, 0, 0, 0))) {
- l_print_error(L, __func__, "exec", res);
- }
- lua_gc(L, LUA_GCCOLLECT, 0);
- return res;
- }
- CLISH_PLUGIN_SYM(clish_plugin_lua_action)
- {
- clish_context_t *context = (clish_context_t *) clish_context;
- lua_State *L = clish_shell__get_udata(context->shell, LUA_UDATA);
- konf_buf_t *buf;
- pid_t childpid;
- int res = 0, origstdout = -1, fd[2];
- if (!script) /* Nothing to do */
- return (0);
- if (!out) /* Handle trivial case */
- return exec_action(L, script);
- pipe(fd);
- if ((childpid = fork()) == -1) {
- perror("fork");
- return -1;
- }
- if (childpid == 0) { /* Child */
- dup2(fd[1], 1);
- close(fd[0]);
- close(fd[1]);
- exit(exec_action(L, script));
- } else { /* Parent */
- close(fd[1]);
- buf = konf_buf_new(fd[0]);
- while(konf_buf_read(buf) > 0);
- *out = konf_buf__dup_line(buf);
- konf_buf_delete(buf);
- close(fd[0]);
- }
- return WEXITSTATUS(res);
- }
|