klish_lua.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. const uint8_t kplugin_lua_opt_global = 1; // RTLD_GLOBAL flag for dlopen()
  24. struct lua_klish_data {
  25. lua_State *L;
  26. kcontext_t *context;
  27. char *package_path_sw;
  28. char *autorun_path_sw;
  29. int backtrace_sw; // show traceback
  30. };
  31. static lua_State *globalL = NULL;
  32. static int luaB_par(lua_State *L);
  33. static int luaB_ppar(lua_State *L);
  34. static int luaB_path(lua_State *L);
  35. static const luaL_Reg klish_lib[] = {
  36. { "par", luaB_par },
  37. { "ppar", luaB_ppar },
  38. { "path", luaB_path },
  39. { NULL, NULL }
  40. };
  41. #if LUA_VERSION_NUM >= 502
  42. static int traceback (lua_State *L)
  43. {
  44. const char *msg = lua_tostring(L, 1);
  45. if (msg)
  46. luaL_traceback(L, L, msg, 1);
  47. else if (!lua_isnoneornil(L, 1)) { // is there an error object?
  48. if (!luaL_callmeta(L, 1, "__tostring")) // try its 'tostring' metamethod
  49. lua_pushliteral(L, "(no error message)");
  50. }
  51. return 1;
  52. }
  53. #else
  54. static int traceback (lua_State *L)
  55. {
  56. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  57. if (!lua_istable(L, -1)) {
  58. lua_pop(L, 1);
  59. return 1;
  60. }
  61. lua_getfield(L, -1, "traceback");
  62. if (!lua_isfunction(L, -1)) {
  63. lua_pop(L, 2);
  64. return 1;
  65. }
  66. lua_pushvalue(L, 1); // pass error message
  67. lua_pushinteger(L, 2); // skip this function and traceback
  68. lua_call(L, 2, 1); // call debug.traceback
  69. return 1;
  70. }
  71. #endif
  72. static int report (lua_State *L, int status)
  73. {
  74. if (status && !lua_isnil(L, -1)) {
  75. const char *msg = lua_tostring(L, -1);
  76. if (msg == NULL)
  77. msg = "(error object is not a string)";
  78. fprintf(stderr,"Error: %s\n", msg);
  79. lua_pop(L, 1);
  80. status = -1;
  81. }
  82. return status;
  83. }
  84. static int docall(struct lua_klish_data *ctx, int narg)
  85. {
  86. int status = 0;
  87. int base = 0;
  88. if (ctx->backtrace_sw) {
  89. base = lua_gettop(ctx->L) - narg; // function index
  90. lua_pushcfunction(ctx->L, traceback); // push traceback function
  91. lua_insert(ctx->L, base); // put it under chunk and args
  92. }
  93. status = lua_pcall(ctx->L, narg, LUA_MULTRET, base);
  94. if (ctx->backtrace_sw)
  95. lua_remove(ctx->L, base); // remove traceback function
  96. // force a complete garbage collection in case of errors
  97. if (status != 0)
  98. lua_gc(ctx->L, LUA_GCCOLLECT, 0);
  99. return status;
  100. }
  101. static int clear(lua_State *L)
  102. {
  103. int N = lua_gettop(L);
  104. lua_pop(L, N);
  105. return 0;
  106. }
  107. static int loadscript(struct lua_klish_data *ctx, const char *path)
  108. {
  109. int status = 0;
  110. status = luaL_loadfile(ctx->L, path);
  111. if (!status) {
  112. status = docall(ctx, 0);
  113. }
  114. status = report(ctx->L, status);
  115. clear(ctx->L);
  116. return status;
  117. }
  118. static int dostring(struct lua_klish_data *ctx, const char *s)
  119. {
  120. int status = luaL_loadstring(ctx->L, s) || docall(ctx, 0);
  121. return report(ctx->L, status);
  122. }
  123. static struct lua_klish_data *lua_context(lua_State *L)
  124. {
  125. struct lua_klish_data *ctx;
  126. lua_getglobal(L, LUA_CONTEXT);
  127. ctx = lua_touserdata(L, -1);
  128. lua_pop(L, 1);
  129. return ctx;
  130. }
  131. static int _luaB_par(lua_State *L, int parent)
  132. {
  133. unsigned int k = 0, i = 0;
  134. kcontext_t *context;
  135. const kpargv_t *pars;
  136. kpargv_pargs_node_t *par_i;
  137. kparg_t *p = NULL;
  138. const kentry_t *last_entry = NULL;
  139. struct lua_klish_data *ctx;
  140. const char *name = luaL_optstring(L, 1, NULL);
  141. lua_newtable(L);
  142. ctx = lua_context(L);
  143. assert(ctx);
  144. context = ctx->context;
  145. assert(context);
  146. pars = (parent)?kcontext_parent_pargv(context):kcontext_pargv(context);
  147. if (!pars)
  148. return 1;
  149. par_i = kpargv_pargs_iter(pars);
  150. if (kpargv_pargs_len(pars) <= 0)
  151. return 1;
  152. while ((p = kpargv_pargs_each(&par_i))) {
  153. const kentry_t *entry = kparg_entry(p);
  154. const char *n = kentry_name(entry);
  155. if (!name) {
  156. if (last_entry != entry) {
  157. lua_pushnumber(L, ++k);
  158. lua_pushstring(L, n);
  159. lua_rawset(L, -3);
  160. lua_pushstring(L, n);
  161. lua_newtable(L);
  162. lua_rawset(L, -3);
  163. i = 0;
  164. }
  165. lua_pushstring(L, n);
  166. lua_rawget(L, -2);
  167. lua_pushnumber(L, ++ i);
  168. lua_pushstring(L, kparg_value(p));
  169. lua_rawset(L, -3);
  170. lua_pop(L, 1);
  171. last_entry = entry;
  172. } else if (!strcmp(n, name)) {
  173. lua_pushnumber(L, ++ k);
  174. lua_pushstring(L, kparg_value(p));
  175. lua_rawset(L, -3);
  176. }
  177. }
  178. return 1;
  179. }
  180. static int luaB_path(lua_State *L)
  181. {
  182. int k = 0;
  183. kpath_t *path = NULL;
  184. kpath_levels_node_t *iter = NULL;
  185. klevel_t *level = NULL;
  186. struct lua_klish_data *ctx;
  187. kcontext_t *context;
  188. ctx = lua_context(L);
  189. assert(ctx);
  190. context = ctx->context;
  191. assert(context);
  192. path = ksession_path(kcontext_session(context));
  193. assert(path);
  194. iter = kpath_iter(path);
  195. lua_newtable(L);
  196. while ((level = kpath_each(&iter))) {
  197. lua_pushnumber(L, ++ k);
  198. lua_pushstring(L, kentry_name(klevel_entry(level)));
  199. lua_rawset(L, -3);
  200. }
  201. return 1;
  202. }
  203. static int luaB_par(lua_State *L)
  204. {
  205. return _luaB_par(L, 0);
  206. }
  207. static int luaB_ppar(lua_State *L)
  208. {
  209. return _luaB_par(L, 1);
  210. }
  211. static int luaopen_klish(lua_State *L)
  212. {
  213. luaL_newlib(L, klish_lib);
  214. return 1;
  215. }
  216. static int clish_env(lua_State *L)
  217. {
  218. luaL_requiref(L, "klish", luaopen_klish, 1);
  219. return 0;
  220. }
  221. static int package_path(struct lua_klish_data *ctx)
  222. {
  223. int rc = 0;
  224. char *str = NULL;
  225. char *path = ctx->package_path_sw;
  226. faux_str_cat(&str, "package.path=\"");
  227. faux_str_cat(&str, path);
  228. faux_str_cat(&str, "\"");
  229. rc = dostring(ctx, str);
  230. clear(ctx->L);
  231. faux_str_free(str);
  232. return rc;
  233. }
  234. static void lstop(lua_State *L, lua_Debug *ar)
  235. {
  236. lua_sethook(L, NULL, 0, 0);
  237. // luaL_error(L, "interrupted!");
  238. ar = ar; // Unused arg
  239. }
  240. static void laction (int i) {
  241. if (!globalL)
  242. return;
  243. lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
  244. globalL = NULL; // run only once
  245. i = i; // Happy compiler
  246. }
  247. static void locale_set()
  248. {
  249. setlocale(LC_NUMERIC, "C"); // to avoid . -> , in numbers
  250. setlocale(LC_CTYPE, "C"); // to avoid lower/upper problems
  251. }
  252. static void locale_reset()
  253. {
  254. setlocale(LC_NUMERIC, "");
  255. setlocale(LC_CTYPE, "");
  256. }
  257. static lua_State *lua_init(struct lua_klish_data *ctx)
  258. {
  259. lua_State *L = NULL;
  260. locale_set();
  261. #if LUA_VERSION_NUM >= 502
  262. L = luaL_newstate();
  263. #else
  264. L = lua_open();
  265. #endif
  266. if (!L) {
  267. fprintf(stderr, "Error: Failed to instantiate Lua interpreter\n");
  268. locale_reset();
  269. return NULL;
  270. }
  271. ctx->L = L; // lua state
  272. luaL_openlibs(L);
  273. if (ctx->package_path_sw && package_path(ctx)) {
  274. fprintf(stderr, "Error: Failed to define package env.\n");
  275. goto err;
  276. }
  277. if (clish_env(L)) {
  278. fprintf(stderr, "Error: Failed to define Lua clish env.\n");
  279. goto err;
  280. }
  281. if (ctx->autorun_path_sw) {
  282. if (loadscript(ctx, ctx->autorun_path_sw)) {
  283. goto err;
  284. }
  285. }
  286. globalL = L;
  287. locale_reset();
  288. return L;
  289. err:
  290. lua_close(L);
  291. locale_reset();
  292. return NULL;
  293. }
  294. static int exec_action(struct lua_klish_data *ctx, const char *script)
  295. {
  296. int rc = 0;
  297. lua_State *L = ctx->L;
  298. assert(L);
  299. globalL = L;
  300. lua_pushlightuserdata(L, ctx);
  301. lua_setglobal(L, LUA_CONTEXT);
  302. locale_set();
  303. rc = dostring(ctx, script);
  304. locale_reset();
  305. fflush(stdout);
  306. fflush(stderr);
  307. clear(L);
  308. return rc;
  309. }
  310. int klish_plugin_lua_action(kcontext_t *context)
  311. {
  312. int status = -1;
  313. const char *script = NULL;
  314. struct sigaction sig_old_int;
  315. struct sigaction sig_old_quit;
  316. struct sigaction sig_new;
  317. sigset_t sig_set;
  318. const kplugin_t *plugin;
  319. struct lua_klish_data *ctx;
  320. assert(context);
  321. plugin = kcontext_plugin(context);
  322. assert(plugin);
  323. ctx = kplugin_udata(plugin);
  324. assert(ctx);
  325. script = kcontext_script(context);
  326. if (!script) // Nothing to do
  327. return 0;
  328. sigemptyset(&sig_set);
  329. sig_new.sa_flags = 0;
  330. sig_new.sa_mask = sig_set;
  331. sig_new.sa_handler = laction;
  332. sigaction(SIGINT, &sig_new, &sig_old_int);
  333. sigaction(SIGQUIT, &sig_new, &sig_old_quit);
  334. status = exec_action(ctx, script);
  335. while ( wait(NULL) >= 0 || errno != ECHILD);
  336. // Restore SIGINT and SIGQUIT
  337. sigaction(SIGINT, &sig_old_int, NULL);
  338. sigaction(SIGQUIT, &sig_old_quit, NULL);
  339. return status;
  340. }
  341. static void free_ctx(struct lua_klish_data *ctx)
  342. {
  343. if (ctx->package_path_sw)
  344. faux_str_free(ctx->package_path_sw);
  345. if (ctx->autorun_path_sw)
  346. faux_str_free(ctx->autorun_path_sw);
  347. free(ctx);
  348. }
  349. int kplugin_lua_init(kcontext_t *context)
  350. {
  351. faux_ini_t *ini = NULL;
  352. kplugin_t *plugin = NULL;
  353. const char *p = NULL;
  354. struct lua_klish_data *ctx = NULL;
  355. const char *conf = NULL;
  356. assert(context);
  357. plugin = kcontext_plugin(context);
  358. assert(plugin);
  359. ctx = malloc(sizeof(*ctx));
  360. if (!ctx)
  361. return -1;
  362. conf = kplugin_conf(plugin);
  363. ctx->context = context;
  364. ctx->backtrace_sw = 1;
  365. ctx->package_path_sw = NULL;
  366. ctx->autorun_path_sw = NULL;
  367. ctx->L = NULL;
  368. if (conf) {
  369. ini = faux_ini_new();
  370. faux_ini_parse_str(ini, conf);
  371. p = faux_ini_find(ini, LUA_BACKTRACE_SW);
  372. ctx->backtrace_sw = p ? atoi(p) : 1;
  373. p = faux_ini_find(ini, LUA_PACKAGE_PATH_SW);
  374. ctx->package_path_sw = p ? faux_str_dup(p): NULL;
  375. p = faux_ini_find(ini, LUA_AUTORUN_SW);
  376. ctx->autorun_path_sw = p ? faux_str_dup(p): NULL;
  377. faux_ini_free(ini);
  378. }
  379. kplugin_set_udata(plugin, ctx);
  380. if (!lua_init(ctx)) {
  381. free_ctx(ctx);
  382. return -1;
  383. }
  384. kplugin_add_syms(plugin, ksym_new("lua", klish_plugin_lua_action));
  385. return 0;
  386. }
  387. int kplugin_lua_fini(kcontext_t *context)
  388. {
  389. kplugin_t *plugin = NULL;
  390. struct lua_klish_data *ctx = NULL;
  391. assert(context);
  392. plugin = kcontext_plugin(context);
  393. assert(plugin);
  394. ctx = kplugin_udata(plugin);
  395. if (!ctx)
  396. return 0;
  397. if (ctx->L)
  398. lua_close(ctx->L);
  399. free_ctx(ctx);
  400. return 0;
  401. }