lua_action.c 729 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include <lub/string.h>
  2. #include "private.h"
  3. CLISH_PLUGIN_SYM(clish_plugin_lua_action)
  4. {
  5. clish_context_t *context = (clish_context_t *) clish_context;
  6. lua_State *L = clish_shell__get_udata(context->shell, LUA_UDATA);
  7. int res = 0, result = -1, retnum = 0;
  8. if (!script) /* Nothing to do */
  9. return (0);
  10. if (out) {
  11. *out = NULL;
  12. retnum = 1;
  13. }
  14. if ((res = luaL_loadstring(L, script))) {
  15. l_print_error(L, __func__, "load", res);
  16. } else if ((res = lua_pcall(L, 0, retnum, 0))) {
  17. l_print_error(L, __func__, "exec", res);
  18. } else {
  19. if (retnum) {
  20. if (lua_isstring(L, -1))
  21. *out = lub_string_dup(lua_tostring(L, -1));
  22. lua_pop(L, 1);
  23. }
  24. result = 0;
  25. }
  26. lua_gc(L, LUA_GCCOLLECT, 0);
  27. return (result);
  28. }