klish_lua.c 8.9 KB

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