klish_lua.c 9.5 KB

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