klish_lua.c 11 KB

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