klish_lua.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #include <locale.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <sys/wait.h>
  8. #include <sys/types.h>
  9. #include <signal.h>
  10. #include <dlfcn.h>
  11. #include <assert.h>
  12. #include <klish/kplugin.h>
  13. #include <klish/kcontext.h>
  14. #include <faux/ini.h>
  15. #include <faux/str.h>
  16. #include <lua.h>
  17. #include <lualib.h>
  18. #include <lauxlib.h>
  19. #define LUA_CONTEXT "klish_context"
  20. #define LUA_AUTORUN_SW "autostart"
  21. #define LUA_BACKTRACE_SW "backtrace"
  22. #define LUA_PACKAGE_PATH_SW "package.path"
  23. const uint8_t kplugin_lua_major = KPLUGIN_MAJOR;
  24. const uint8_t kplugin_lua_minor = KPLUGIN_MINOR;
  25. struct lua_klish_data {
  26. lua_State *L;
  27. kcontext_t *context;
  28. char *package_path_sw;
  29. char *autorun_path_sw;
  30. int backtrace_sw; // show traceback
  31. };
  32. static lua_State *globalL = NULL;
  33. static int luaB_expand_var(lua_State *L);
  34. static const luaL_Reg base_funcs[] = {
  35. {"clish_expand_var", luaB_expand_var},
  36. {NULL, NULL}
  37. };
  38. #if LUA_VERSION_NUM >= 502
  39. static int traceback (lua_State *L)
  40. {
  41. const char *msg = lua_tostring(L, 1);
  42. if (msg)
  43. luaL_traceback(L, L, msg, 1);
  44. else if (!lua_isnoneornil(L, 1)) { // is there an error object?
  45. if (!luaL_callmeta(L, 1, "__tostring")) // try its 'tostring' metamethod
  46. lua_pushliteral(L, "(no error message)");
  47. }
  48. return 1;
  49. }
  50. #else
  51. static int traceback (lua_State *L)
  52. {
  53. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  54. if (!lua_istable(L, -1)) {
  55. lua_pop(L, 1);
  56. return 1;
  57. }
  58. lua_getfield(L, -1, "traceback");
  59. if (!lua_isfunction(L, -1)) {
  60. lua_pop(L, 2);
  61. return 1;
  62. }
  63. lua_pushvalue(L, 1); // pass error message
  64. lua_pushinteger(L, 2); // skip this function and traceback
  65. lua_call(L, 2, 1); // call debug.traceback
  66. return 1;
  67. }
  68. #endif
  69. static int report (lua_State *L, int status)
  70. {
  71. if (status && !lua_isnil(L, -1)) {
  72. const char *msg = lua_tostring(L, -1);
  73. if (msg == NULL)
  74. msg = "(error object is not a string)";
  75. fprintf(stderr,"Error: %s\n", msg);
  76. lua_pop(L, 1);
  77. status = -1;
  78. }
  79. return status;
  80. }
  81. static int docall(struct lua_klish_data *ctx, int narg)
  82. {
  83. int status = 0;
  84. int base = 0;
  85. if (ctx->backtrace_sw) {
  86. base = lua_gettop(ctx->L) - narg; // function index
  87. lua_pushcfunction(ctx->L, traceback); // push traceback function
  88. lua_insert(ctx->L, base); // put it under chunk and args
  89. }
  90. status = lua_pcall(ctx->L, narg, LUA_MULTRET, base);
  91. if (ctx->backtrace_sw)
  92. lua_remove(ctx->L, base); // remove traceback function
  93. // force a complete garbage collection in case of errors
  94. if (status != 0)
  95. lua_gc(ctx->L, LUA_GCCOLLECT, 0);
  96. return status;
  97. }
  98. static int clear(lua_State *L)
  99. {
  100. int N = lua_gettop(L);
  101. lua_pop(L, N);
  102. return 0;
  103. }
  104. static int loadscript(struct lua_klish_data *ctx, const char *path)
  105. {
  106. int status = 0;
  107. status = luaL_loadfile(ctx->L, path);
  108. if (!status) {
  109. status = docall(ctx, 0);
  110. }
  111. status = report(ctx->L, status);
  112. clear(ctx->L);
  113. return status;
  114. }
  115. static int dostring(struct lua_klish_data *ctx, const char *s)
  116. {
  117. int status = luaL_loadstring(ctx->L, s) || docall(ctx, 0);
  118. return report(ctx->L, status);
  119. }
  120. static struct lua_klish_data *lua_context(lua_State *L)
  121. {
  122. struct lua_klish_data *ctx;
  123. lua_getglobal(L, LUA_CONTEXT);
  124. ctx = lua_touserdata(L, -1);
  125. lua_pop(L, 1);
  126. return ctx;
  127. }
  128. static int luaB_expand_var(lua_State *L)
  129. {
  130. // TODO
  131. return 0;
  132. }
  133. static int clish_env(lua_State *L)
  134. {
  135. #if LUA_VERSION_NUM >= 502
  136. lua_pushglobaltable(L);
  137. lua_pushglobaltable(L);
  138. lua_setfield(L, -2, "_G");
  139. // open lib into global table
  140. luaL_setfuncs(L, base_funcs, 0);
  141. #else
  142. luaL_register(L, "_G", base_funcs);
  143. #endif
  144. return 0;
  145. }
  146. static int package_path(struct lua_klish_data *ctx)
  147. {
  148. int rc = 0;
  149. char *str = NULL;
  150. char *path = ctx->package_path_sw;
  151. faux_str_cat(&str, "package.path=\"");
  152. faux_str_cat(&str, path);
  153. faux_str_cat(&str, "\"");
  154. rc = dostring(ctx, str);
  155. clear(ctx->L);
  156. faux_str_free(str);
  157. return rc;
  158. }
  159. static void lstop(lua_State *L, lua_Debug *ar)
  160. {
  161. lua_sethook(L, NULL, 0, 0);
  162. // luaL_error(L, "interrupted!");
  163. ar = ar; // Unused arg
  164. }
  165. static void laction (int i) {
  166. if (!globalL)
  167. return;
  168. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  169. globalL = NULL; // run only once
  170. }
  171. static void locale_set()
  172. {
  173. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  174. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  175. }
  176. static void locale_reset()
  177. {
  178. setlocale(LC_NUMERIC, "");
  179. setlocale(LC_CTYPE, "");
  180. }
  181. static lua_State *lua_init(struct lua_klish_data *ctx)
  182. {
  183. lua_State *L = NULL;
  184. locale_set();
  185. #if LUA_VERSION_NUM >= 502
  186. L = luaL_newstate();
  187. #else
  188. L = lua_open();
  189. #endif
  190. if (!L) {
  191. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  192. locale_reset();
  193. return NULL;
  194. }
  195. ctx->L = L; // lua state
  196. luaL_openlibs(L);
  197. if (ctx->package_path_sw && package_path(ctx)) {
  198. fprintf(stderr, "Error: Failed to define package env.\n");
  199. goto err;
  200. }
  201. if (clish_env(L)) {
  202. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  203. goto err;
  204. }
  205. if (ctx->autorun_path_sw) {
  206. if (loadscript(ctx, ctx->autorun_path_sw)) {
  207. goto err;
  208. }
  209. }
  210. globalL = L;
  211. locale_reset();
  212. return L;
  213. err:
  214. lua_close(L);
  215. locale_reset();
  216. return NULL;
  217. }
  218. static int exec_action(struct lua_klish_data *ctx, const char *script)
  219. {
  220. int rc = 0;
  221. lua_State *L = ctx->L;
  222. assert(L);
  223. globalL = L;
  224. lua_pushlightuserdata(L, ctx);
  225. lua_setglobal(L, LUA_CONTEXT);
  226. locale_set();
  227. rc = dostring(ctx, script);
  228. locale_reset();
  229. fflush(stdout);
  230. fflush(stderr);
  231. clear(L);
  232. return rc;
  233. }
  234. int klish_plugin_lua_action(kcontext_t *context)
  235. {
  236. int status = -1;
  237. const char *script = NULL;
  238. struct sigaction sig_old_int;
  239. struct sigaction sig_old_quit;
  240. struct sigaction sig_new;
  241. sigset_t sig_set;
  242. const kplugin_t *plugin;
  243. struct lua_klish_data *ctx;
  244. assert(context);
  245. plugin = kcontext_plugin(context);
  246. assert(plugin);
  247. ctx = kplugin_udata(plugin);
  248. assert(ctx);
  249. script = kcontext_script(context);
  250. if (!script) // Nothing to do
  251. return 0;
  252. sigemptyset(&sig_set);
  253. sig_new.sa_flags = 0;
  254. sig_new.sa_mask = sig_set;
  255. sig_new.sa_handler = laction;
  256. sigaction(SIGINT, &sig_new, &sig_old_int);
  257. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  258. status = exec_action(ctx, script);
  259. while ( wait(NULL) >= 0 || errno != ECHILD);
  260. // Restore SIGINT and SIGQUIT
  261. sigaction(SIGINT, &sig_old_int, NULL);
  262. sigaction(SIGQUIT, &sig_old_quit, NULL);
  263. err:
  264. return status;
  265. }
  266. static void free_ctx(struct lua_klish_data *ctx)
  267. {
  268. if (ctx->package_path_sw)
  269. faux_str_free(ctx->package_path_sw);
  270. if (ctx->autorun_path_sw)
  271. faux_str_free(ctx->autorun_path_sw);
  272. free(ctx);
  273. }
  274. int kplugin_lua_init(kcontext_t *context)
  275. {
  276. faux_ini_t *ini = NULL;
  277. kplugin_t *plugin = NULL;
  278. const char *p = NULL;
  279. struct lua_klish_data *ctx = NULL;
  280. const char *conf = NULL;
  281. assert(context);
  282. plugin = kcontext_plugin(context);
  283. assert(plugin);
  284. ctx = malloc(sizeof(*ctx));
  285. if (!ctx)
  286. return -1;
  287. conf = kplugin_conf(plugin);
  288. ctx->context = context;
  289. ctx->backtrace_sw = 1;
  290. ctx->package_path_sw = NULL;
  291. ctx->autorun_path_sw = NULL;
  292. ctx->L = NULL;
  293. if (conf) {
  294. ini = faux_ini_new();
  295. faux_ini_parse_str(ini, conf);
  296. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  297. ctx->backtrace_sw = p ? atoi(p) : 1;
  298. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  299. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  300. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  301. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  302. faux_ini_free(ini);
  303. }
  304. kplugin_set_udata(plugin, ctx);
  305. if (!lua_init(ctx)) {
  306. free_ctx(ctx);
  307. return -1;
  308. }
  309. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  310. return 0;
  311. }
  312. int kplugin_lua_fini(kcontext_t *context)
  313. {
  314. kplugin_t *plugin = NULL;
  315. lua_State *L = NULL;
  316. struct lua_klish_data *ctx = NULL;
  317. assert(context);
  318. plugin = kcontext_plugin(context);
  319. assert(plugin);
  320. ctx = kplugin_udata(plugin);
  321. if (!ctx)
  322. return 0;
  323. if (ctx->L)
  324. lua_close(ctx->L);
  325. free_ctx(ctx);
  326. return 0;
  327. }