klish_lua.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. #include <locale.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <sys/wait.h>
  6. #include <signal.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <klish/kplugin.h>
  10. #include <klish/kcontext.h>
  11. #include <faux/ini.h>
  12. #include <faux/str.h>
  13. #include <lua.h>
  14. #include <lualib.h>
  15. #include <lauxlib.h>
  16. #include "lua-compat.h"
  17. #define LUA_CONTEXT "klish_context"
  18. #define LUA_AUTORUN_SW "autostart"
  19. #define LUA_BACKTRACE_SW "backtrace"
  20. #define LUA_PACKAGE_PATH_SW "package.path"
  21. const uint8_t kplugin_lua_major = KPLUGIN_MAJOR;
  22. const uint8_t kplugin_lua_minor = KPLUGIN_MINOR;
  23. struct lua_klish_data {
  24. lua_State *L;
  25. kcontext_t *context;
  26. char *package_path_sw;
  27. char *autorun_path_sw;
  28. int backtrace_sw; // show traceback
  29. };
  30. static lua_State *globalL = NULL;
  31. static int luaB_par(lua_State *L);
  32. static int luaB_ppar(lua_State *L);
  33. static const luaL_Reg klish_lib[] = {
  34. { "par", luaB_par },
  35. { "ppar", luaB_ppar },
  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_par(lua_State *L, int parent)
  129. {
  130. unsigned int k = 0;
  131. kcontext_t *context;
  132. const kpargv_t *pars;
  133. kpargv_pargs_node_t *par_i;
  134. kparg_t *p = NULL;
  135. struct lua_klish_data *ctx;
  136. const char *name = luaL_optstring(L, 1, NULL);
  137. if (!name)
  138. lua_newtable(L);
  139. ctx = lua_context(L);
  140. assert(ctx);
  141. context = ctx->context;
  142. assert(context);
  143. pars = (parent)?kcontext_parent_pargv(context):kcontext_pargv(context);
  144. if (!pars)
  145. return (name == NULL);
  146. par_i = kpargv_pargs_iter(pars);
  147. if (kpargv_pargs_len(pars) > 0) {
  148. while ((p = kpargv_pargs_each(&par_i))) {
  149. const char *n = kentry_name(kparg_entry(p));
  150. if (!name) {
  151. lua_pushnumber(L, ++ k);
  152. lua_pushstring(L, n);
  153. lua_rawset(L, -3);
  154. lua_pushstring(L, n);
  155. lua_pushstring(L, kparg_value(p));
  156. lua_rawset(L, -3);
  157. } else if (!strcmp(n, name)) {
  158. lua_pushstring(L, kparg_value(p));
  159. return 1;
  160. }
  161. }
  162. }
  163. return (name == NULL);
  164. }
  165. static int luaB_par(lua_State *L)
  166. {
  167. return _luaB_par(L, 0);
  168. }
  169. static int luaB_ppar(lua_State *L)
  170. {
  171. return _luaB_par(L, 1);
  172. }
  173. static int luaopen_klish(lua_State *L)
  174. {
  175. luaL_newlib(L, klish_lib);
  176. return 1;
  177. }
  178. static int clish_env(lua_State *L)
  179. {
  180. luaL_requiref(L, "klish", luaopen_klish, 1);
  181. return 0;
  182. }
  183. static int package_path(struct lua_klish_data *ctx)
  184. {
  185. int rc = 0;
  186. char *str = NULL;
  187. char *path = ctx->package_path_sw;
  188. faux_str_cat(&str, "package.path=\"");
  189. faux_str_cat(&str, path);
  190. faux_str_cat(&str, "\"");
  191. rc = dostring(ctx, str);
  192. clear(ctx->L);
  193. faux_str_free(str);
  194. return rc;
  195. }
  196. static void lstop(lua_State *L, lua_Debug *ar)
  197. {
  198. lua_sethook(L, NULL, 0, 0);
  199. // luaL_error(L, "interrupted!");
  200. ar = ar; // Unused arg
  201. }
  202. static void laction (int i) {
  203. if (!globalL)
  204. return;
  205. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  206. globalL = NULL; // run only once
  207. }
  208. static void locale_set()
  209. {
  210. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  211. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  212. }
  213. static void locale_reset()
  214. {
  215. setlocale(LC_NUMERIC, "");
  216. setlocale(LC_CTYPE, "");
  217. }
  218. static lua_State *lua_init(struct lua_klish_data *ctx)
  219. {
  220. lua_State *L = NULL;
  221. locale_set();
  222. #if LUA_VERSION_NUM >= 502
  223. L = luaL_newstate();
  224. #else
  225. L = lua_open();
  226. #endif
  227. if (!L) {
  228. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  229. locale_reset();
  230. return NULL;
  231. }
  232. ctx->L = L; // lua state
  233. luaL_openlibs(L);
  234. if (ctx->package_path_sw && package_path(ctx)) {
  235. fprintf(stderr, "Error: Failed to define package env.\n");
  236. goto err;
  237. }
  238. if (clish_env(L)) {
  239. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  240. goto err;
  241. }
  242. if (ctx->autorun_path_sw) {
  243. if (loadscript(ctx, ctx->autorun_path_sw)) {
  244. goto err;
  245. }
  246. }
  247. globalL = L;
  248. locale_reset();
  249. return L;
  250. err:
  251. lua_close(L);
  252. locale_reset();
  253. return NULL;
  254. }
  255. static int exec_action(struct lua_klish_data *ctx, const char *script)
  256. {
  257. int rc = 0;
  258. lua_State *L = ctx->L;
  259. assert(L);
  260. globalL = L;
  261. lua_pushlightuserdata(L, ctx);
  262. lua_setglobal(L, LUA_CONTEXT);
  263. locale_set();
  264. rc = dostring(ctx, script);
  265. locale_reset();
  266. fflush(stdout);
  267. fflush(stderr);
  268. clear(L);
  269. return rc;
  270. }
  271. int klish_plugin_lua_action(kcontext_t *context)
  272. {
  273. int status = -1;
  274. const char *script = NULL;
  275. struct sigaction sig_old_int;
  276. struct sigaction sig_old_quit;
  277. struct sigaction sig_new;
  278. sigset_t sig_set;
  279. const kplugin_t *plugin;
  280. struct lua_klish_data *ctx;
  281. assert(context);
  282. plugin = kcontext_plugin(context);
  283. assert(plugin);
  284. ctx = kplugin_udata(plugin);
  285. assert(ctx);
  286. script = kcontext_script(context);
  287. if (!script) // Nothing to do
  288. return 0;
  289. sigemptyset(&sig_set);
  290. sig_new.sa_flags = 0;
  291. sig_new.sa_mask = sig_set;
  292. sig_new.sa_handler = laction;
  293. sigaction(SIGINT, &sig_new, &sig_old_int);
  294. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  295. status = exec_action(ctx, script);
  296. while ( wait(NULL) >= 0 || errno != ECHILD);
  297. // Restore SIGINT and SIGQUIT
  298. sigaction(SIGINT, &sig_old_int, NULL);
  299. sigaction(SIGQUIT, &sig_old_quit, NULL);
  300. err:
  301. return status;
  302. }
  303. static void free_ctx(struct lua_klish_data *ctx)
  304. {
  305. if (ctx->package_path_sw)
  306. faux_str_free(ctx->package_path_sw);
  307. if (ctx->autorun_path_sw)
  308. faux_str_free(ctx->autorun_path_sw);
  309. free(ctx);
  310. }
  311. int kplugin_lua_init(kcontext_t *context)
  312. {
  313. faux_ini_t *ini = NULL;
  314. kplugin_t *plugin = NULL;
  315. const char *p = NULL;
  316. struct lua_klish_data *ctx = NULL;
  317. const char *conf = NULL;
  318. assert(context);
  319. plugin = kcontext_plugin(context);
  320. assert(plugin);
  321. ctx = malloc(sizeof(*ctx));
  322. if (!ctx)
  323. return -1;
  324. conf = kplugin_conf(plugin);
  325. ctx->context = context;
  326. ctx->backtrace_sw = 1;
  327. ctx->package_path_sw = NULL;
  328. ctx->autorun_path_sw = NULL;
  329. ctx->L = NULL;
  330. if (conf) {
  331. ini = faux_ini_new();
  332. faux_ini_parse_str(ini, conf);
  333. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  334. ctx->backtrace_sw = p ? atoi(p) : 1;
  335. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  336. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  337. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  338. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  339. faux_ini_free(ini);
  340. }
  341. kplugin_set_udata(plugin, ctx);
  342. if (!lua_init(ctx)) {
  343. free_ctx(ctx);
  344. return -1;
  345. }
  346. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  347. return 0;
  348. }
  349. int kplugin_lua_fini(kcontext_t *context)
  350. {
  351. kplugin_t *plugin = NULL;
  352. lua_State *L = NULL;
  353. struct lua_klish_data *ctx = NULL;
  354. assert(context);
  355. plugin = kcontext_plugin(context);
  356. assert(plugin);
  357. ctx = kplugin_udata(plugin);
  358. if (!ctx)
  359. return 0;
  360. if (ctx->L)
  361. lua_close(ctx->L);
  362. free_ctx(ctx);
  363. return 0;
  364. }