lua_action.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <stdlib.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. #include <konf/buf.h>
  5. #include <lub/string.h>
  6. #include "private.h"
  7. static int exec_action(lua_State *L, const char *script)
  8. {
  9. int res = 0;
  10. if ((res = luaL_loadstring(L, script))) {
  11. l_print_error(L, __func__, "load", res);
  12. } else if ((res = lua_pcall(L, 0, 0, 0))) {
  13. l_print_error(L, __func__, "exec", res);
  14. }
  15. lua_gc(L, LUA_GCCOLLECT, 0);
  16. return res;
  17. }
  18. CLISH_PLUGIN_SYM(clish_plugin_lua_action)
  19. {
  20. clish_shell_t *shell = clish_context__get_shell(clish_context);
  21. lua_State *L = clish_shell__get_udata(shell, LUA_UDATA);
  22. konf_buf_t *buf;
  23. pid_t childpid;
  24. int res = 0, fd[2];
  25. if (!script) /* Nothing to do */
  26. return (0);
  27. if (!out) /* Handle trivial case */
  28. return exec_action(L, script);
  29. if (pipe(fd) == -1) {
  30. perror("pipe");
  31. return -1;
  32. }
  33. if ((childpid = fork()) == -1) {
  34. perror("fork");
  35. return -1;
  36. }
  37. if (childpid == 0) { /* Child */
  38. dup2(fd[1], 1);
  39. close(fd[0]);
  40. close(fd[1]);
  41. exit(exec_action(L, script));
  42. } else { /* Parent */
  43. close(fd[1]);
  44. buf = konf_buf_new(fd[0]);
  45. while(konf_buf_read(buf) > 0);
  46. *out = konf_buf__dup_line(buf);
  47. konf_buf_delete(buf);
  48. close(fd[0]);
  49. }
  50. return WEXITSTATUS(res);
  51. }