lua.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. ** $Id: lua.c,v 1.160.1.2 2007/12/28 15:32:23 roberto Exp $
  3. ** Lua stand-alone interpreter
  4. ** See Copyright Notice in lua.h
  5. */
  6. // #include "c_signal.h"
  7. #include "c_stdio.h"
  8. #include "c_stdlib.h"
  9. #include "c_string.h"
  10. #include "user_interface.h"
  11. #include "user_version.h"
  12. #include "driver/readline.h"
  13. #include "driver/uart.h"
  14. #include "platform.h"
  15. #define lua_c
  16. #include "lua.h"
  17. #include "lauxlib.h"
  18. #include "lualib.h"
  19. #include "legc.h"
  20. #ifdef LUA_FLASH_STORE
  21. #include "lflash.h"
  22. #endif
  23. #include "os_type.h"
  24. lua_State *globalL = NULL;
  25. lua_Load gLoad;
  26. static const char *progname = LUA_PROGNAME;
  27. static void l_message (const char *pname, const char *msg) {
  28. #if defined(LUA_USE_STDIO)
  29. if (pname) c_fprintf(c_stderr, "%s: ", pname);
  30. c_fprintf(c_stderr, "%s\n", msg);
  31. c_fflush(c_stderr);
  32. #else
  33. if (pname) luai_writestringerror("%s: ", pname);
  34. luai_writestringerror("%s\n", msg);
  35. #endif
  36. }
  37. static int report (lua_State *L, int status) {
  38. if (status && !lua_isnil(L, -1)) {
  39. const char *msg = lua_tostring(L, -1);
  40. if (msg == NULL) msg = "(error object is not a string)";
  41. l_message(progname, msg);
  42. lua_pop(L, 1);
  43. }
  44. return status;
  45. }
  46. static int traceback (lua_State *L) {
  47. if (!lua_isstring(L, 1)) /* 'message' not a string? */
  48. return 1; /* keep it intact */
  49. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  50. if (!lua_istable(L, -1) && !lua_isrotable(L, -1)) {
  51. lua_pop(L, 1);
  52. return 1;
  53. }
  54. lua_getfield(L, -1, "traceback");
  55. if (!lua_isfunction(L, -1) && !lua_islightfunction(L, -1)) {
  56. lua_pop(L, 2);
  57. return 1;
  58. }
  59. lua_pushvalue(L, 1); /* pass error message */
  60. lua_pushinteger(L, 2); /* skip this function and traceback */
  61. lua_call(L, 2, 1); /* call debug.traceback */
  62. return 1;
  63. }
  64. static int docall (lua_State *L, int narg, int clear) {
  65. int status;
  66. int base = lua_gettop(L) - narg; /* function index */
  67. lua_pushcfunction(L, traceback); /* push traceback function */
  68. lua_insert(L, base); /* put it under chunk and args */
  69. // signal(SIGINT, laction);
  70. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  71. // signal(SIGINT, SIG_DFL);
  72. lua_remove(L, base); /* remove traceback function */
  73. /* force a complete garbage collection in case of errors */
  74. if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  75. return status;
  76. }
  77. static void print_version (lua_State *L) {
  78. lua_pushliteral (L, "\n" NODE_VERSION " build " BUILD_DATE " powered by " LUA_RELEASE " on SDK ");
  79. lua_pushstring (L, SDK_VERSION);
  80. lua_concat (L, 2);
  81. const char *msg = lua_tostring (L, -1);
  82. l_message (NULL, msg);
  83. lua_pop (L, 1);
  84. }
  85. static int getargs (lua_State *L, char **argv, int n) {
  86. int narg;
  87. int i;
  88. int argc = 0;
  89. while (argv[argc]) argc++; /* count total number of arguments */
  90. narg = argc - (n + 1); /* number of arguments to the script */
  91. luaL_checkstack(L, narg + 3, "too many arguments to script");
  92. for (i=n+1; i < argc; i++)
  93. lua_pushstring(L, argv[i]);
  94. lua_createtable(L, narg, n + 1);
  95. for (i=0; i < argc; i++) {
  96. lua_pushstring(L, argv[i]);
  97. lua_rawseti(L, -2, i - n);
  98. }
  99. return narg;
  100. }
  101. static int dofsfile (lua_State *L, const char *name) {
  102. int status = luaL_loadfsfile(L, name) || docall(L, 0, 1);
  103. return report(L, status);
  104. }
  105. static int dostring (lua_State *L, const char *s, const char *name) {
  106. int status = luaL_loadbuffer(L, s, c_strlen(s), name) || docall(L, 0, 1);
  107. return report(L, status);
  108. }
  109. static int dolibrary (lua_State *L, const char *name) {
  110. lua_getglobal(L, "require");
  111. lua_pushstring(L, name);
  112. return report(L, docall(L, 1, 1));
  113. }
  114. static const char *get_prompt (lua_State *L, int firstline) {
  115. const char *p;
  116. lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
  117. p = lua_tostring(L, -1);
  118. if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
  119. lua_pop(L, 1); /* remove global */
  120. return p;
  121. }
  122. static int incomplete (lua_State *L, int status) {
  123. if (status == LUA_ERRSYNTAX) {
  124. size_t lmsg;
  125. const char *msg = lua_tolstring(L, -1, &lmsg);
  126. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  127. if (c_strstr(msg, LUA_QL("<eof>")) == tp) {
  128. lua_pop(L, 1);
  129. return 1;
  130. }
  131. }
  132. return 0; /* else... */
  133. }
  134. /* check that argument has no extra characters at the end */
  135. #define notail(x) {if ((x)[2] != '\0') return -1;}
  136. static int collectargs (char **argv, int *pi, int *pv, int *pe) {
  137. int i;
  138. for (i = 1; argv[i] != NULL; i++) {
  139. if (argv[i][0] != '-') /* not an option? */
  140. return i;
  141. switch (argv[i][1]) { /* option */
  142. case '-':
  143. notail(argv[i]);
  144. return (argv[i+1] != NULL ? i+1 : 0);
  145. case '\0':
  146. return i;
  147. case 'i':
  148. notail(argv[i]);
  149. *pi = 1; /* go through */
  150. case 'v':
  151. notail(argv[i]);
  152. *pv = 1;
  153. break;
  154. case 'e':
  155. *pe = 1; /* go through */
  156. case 'm': /* go through */
  157. case 'l':
  158. if (argv[i][2] == '\0') {
  159. i++;
  160. if (argv[i] == NULL) return -1;
  161. }
  162. break;
  163. default: return -1; /* invalid option */
  164. }
  165. }
  166. return 0;
  167. }
  168. static int runargs (lua_State *L, char **argv, int n) {
  169. int i;
  170. for (i = 1; i < n; i++) {
  171. if (argv[i] == NULL) continue;
  172. lua_assert(argv[i][0] == '-');
  173. switch (argv[i][1]) { /* option */
  174. case 'e': {
  175. const char *chunk = argv[i] + 2;
  176. if (*chunk == '\0') chunk = argv[++i];
  177. lua_assert(chunk != NULL);
  178. if (dostring(L, chunk, "=(command line)") != 0)
  179. return 1;
  180. break;
  181. }
  182. case 'm': {
  183. const char *limit = argv[i] + 2;
  184. int memlimit=0;
  185. if (*limit == '\0') limit = argv[++i];
  186. lua_assert(limit != NULL);
  187. memlimit = c_atoi(limit);
  188. lua_gc(L, LUA_GCSETMEMLIMIT, memlimit);
  189. break;
  190. }
  191. case 'l': {
  192. const char *filename = argv[i] + 2;
  193. if (*filename == '\0') filename = argv[++i];
  194. lua_assert(filename != NULL);
  195. if (dolibrary(L, filename))
  196. return 1; /* stop if file fails */
  197. break;
  198. }
  199. default: break;
  200. }
  201. }
  202. return 0;
  203. }
  204. #ifdef LUA_INIT_STRING
  205. const char lua_init_value[] = LUA_INIT_STRING;
  206. #else
  207. const char lua_init_value[] = "@init.lua";
  208. #endif
  209. static int handle_luainit (lua_State *L) {
  210. const char *init = c_getenv(LUA_INIT);
  211. if (init == NULL) return 0; /* status OK */
  212. else if (init[0] == '@')
  213. return dofsfile(L, init+1);
  214. else
  215. return dostring(L, init, "=" LUA_INIT);
  216. }
  217. struct Smain {
  218. int argc;
  219. char **argv;
  220. int status;
  221. };
  222. static int pmain (lua_State *L) {
  223. struct Smain *s = (struct Smain *)lua_touserdata(L, 1);
  224. char **argv = s->argv;
  225. int script;
  226. int has_i = 0, has_v = 0, has_e = 0;
  227. globalL = L;
  228. if (argv[0] && argv[0][0]) progname = argv[0];
  229. lua_gc(L, LUA_GCSTOP, 0); /* stop collector during initialization */
  230. luaL_openlibs(L); /* open libraries */
  231. lua_gc(L, LUA_GCRESTART, 0);
  232. print_version(L);
  233. s->status = handle_luainit(L);
  234. script = collectargs(argv, &has_i, &has_v, &has_e);
  235. if (script < 0) { /* invalid args? */
  236. s->status = 1;
  237. return 0;
  238. }
  239. s->status = runargs(L, argv, (script > 0) ? script : s->argc);
  240. if (s->status != 0) return 0;
  241. return 0;
  242. }
  243. static void dojob(lua_Load *load);
  244. static bool readline(lua_Load *load);
  245. char line_buffer[LUA_MAXINPUT];
  246. #ifdef LUA_RPC
  247. int main (int argc, char **argv) {
  248. #else
  249. int lua_main (int argc, char **argv) {
  250. #endif
  251. int status;
  252. struct Smain s;
  253. #if defined(NODE_DEBUG) && defined(DEVELOPMENT_USE_GDB) && \
  254. defined(DEVELOPMENT_BREAK_ON_STARTUP_PIN) && DEVELOPMENT_BREAK_ON_STARTUP_PIN > 0
  255. platform_gpio_mode( DEVELOPMENT_BREAK_ON_STARTUP_PIN, PLATFORM_GPIO_INPUT, PLATFORM_GPIO_PULLUP );
  256. lua_assert(platform_gpio_read(DEVELOPMENT_BREAK_ON_STARTUP_PIN)); // Break if pin pulled low
  257. #endif
  258. lua_State *L = lua_open(); /* create state */
  259. if (L == NULL) {
  260. l_message(argv[0], "cannot create state: not enough memory");
  261. return EXIT_FAILURE;
  262. }
  263. s.argc = argc;
  264. s.argv = argv;
  265. status = lua_cpcall(L, &pmain, &s);
  266. report(L, status);
  267. gLoad.L = L;
  268. gLoad.firstline = 1;
  269. gLoad.done = 0;
  270. gLoad.line = line_buffer;
  271. gLoad.len = LUA_MAXINPUT;
  272. gLoad.line_position = 0;
  273. gLoad.prmt = get_prompt(L, 1);
  274. dojob(&gLoad);
  275. NODE_DBG("Heap size::%d.\n",system_get_free_heap_size());
  276. legc_set_mode( L, EGC_ALWAYS, 4096 );
  277. // legc_set_mode( L, EGC_ON_MEM_LIMIT, 4096 );
  278. // lua_close(L);
  279. return (status || s.status) ? EXIT_FAILURE : EXIT_SUCCESS;
  280. }
  281. void lua_handle_input (bool force)
  282. {
  283. while (gLoad.L && (force || readline (&gLoad)))
  284. dojob (&gLoad);
  285. }
  286. void donejob(lua_Load *load){
  287. lua_close(load->L);
  288. }
  289. static void dojob(lua_Load *load){
  290. size_t l, rs;
  291. int status;
  292. char *b = load->line;
  293. lua_State *L = load->L;
  294. const char *oldprogname = progname;
  295. progname = NULL;
  296. do{
  297. if(load->done == 1){
  298. l = c_strlen(b);
  299. if (l > 0 && b[l-1] == '\n') /* line ends with newline? */
  300. b[l-1] = '\0'; /* remove it */
  301. if (load->firstline && b[0] == '=') /* first line starts with `=' ? */
  302. lua_pushfstring(L, "return %s", b+1); /* change it to `return' */
  303. else
  304. lua_pushstring(L, b);
  305. if(load->firstline != 1){
  306. lua_pushliteral(L, "\n"); /* add a new line... */
  307. lua_insert(L, -2); /* ...between the two lines */
  308. lua_concat(L, 3); /* join them */
  309. }
  310. status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
  311. if (!incomplete(L, status)) { /* cannot try to add lines? */
  312. lua_remove(L, 1); /* remove line */
  313. if (status == 0) {
  314. status = docall(L, 0, 0);
  315. }
  316. report(L, status);
  317. if (status == 0 && lua_gettop(L) > 0) { /* any result to print? */
  318. lua_getglobal(L, "print");
  319. lua_insert(L, 1);
  320. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  321. l_message(progname, lua_pushfstring(L,
  322. "error calling " LUA_QL("print") " (%s)",
  323. lua_tostring(L, -1)));
  324. }
  325. load->firstline = 1;
  326. load->prmt = get_prompt(L, 1);
  327. lua_settop(L, 0);
  328. /* force a complete garbage collection in case of errors */
  329. if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  330. } else {
  331. load->firstline = 0;
  332. load->prmt = get_prompt(L, 0);
  333. }
  334. }
  335. }while(0);
  336. progname = oldprogname;
  337. load->done = 0;
  338. load->line_position = 0;
  339. c_memset(load->line, 0, load->len);
  340. c_puts(load->prmt);
  341. }
  342. #ifndef uart_putc
  343. #define uart_putc uart0_putc
  344. #endif
  345. extern bool uart_on_data_cb(const char *buf, size_t len);
  346. extern bool uart0_echo;
  347. extern bool run_input;
  348. extern uint16_t need_len;
  349. extern int16_t end_char;
  350. static char last_nl_char = '\0';
  351. static bool readline(lua_Load *load){
  352. // NODE_DBG("readline() is called.\n");
  353. bool need_dojob = false;
  354. char ch;
  355. while (uart_getc(&ch))
  356. {
  357. if(run_input)
  358. {
  359. char tmp_last_nl_char = last_nl_char;
  360. // reset marker, will be finally set below when newline is processed
  361. last_nl_char = '\0';
  362. /* handle CR & LF characters
  363. filters second char of LF&CR (\n\r) or CR&LF (\r\n) sequences */
  364. if ((ch == '\r' && tmp_last_nl_char == '\n') || // \n\r sequence -> skip \r
  365. (ch == '\n' && tmp_last_nl_char == '\r')) // \r\n sequence -> skip \n
  366. {
  367. continue;
  368. }
  369. /* backspace key */
  370. else if (ch == 0x7f || ch == 0x08)
  371. {
  372. if (load->line_position > 0)
  373. {
  374. if(uart0_echo) uart_putc(0x08);
  375. if(uart0_echo) uart_putc(' ');
  376. if(uart0_echo) uart_putc(0x08);
  377. load->line_position--;
  378. }
  379. load->line[load->line_position] = 0;
  380. continue;
  381. }
  382. /* EOT(ctrl+d) */
  383. // else if (ch == 0x04)
  384. // {
  385. // if (load->line_position == 0)
  386. // // No input which makes lua interpreter close
  387. // donejob(load);
  388. // else
  389. // continue;
  390. // }
  391. /* end of line */
  392. if (ch == '\r' || ch == '\n')
  393. {
  394. last_nl_char = ch;
  395. load->line[load->line_position] = 0;
  396. if(uart0_echo) uart_putc('\n');
  397. uart_on_data_cb(load->line, load->line_position);
  398. if (load->line_position == 0)
  399. {
  400. /* Get a empty line, then go to get a new line */
  401. c_puts(load->prmt);
  402. } else {
  403. load->done = 1;
  404. need_dojob = true;
  405. }
  406. continue;
  407. }
  408. /* other control character or not an acsii character */
  409. // if (ch < 0x20 || ch >= 0x80)
  410. // {
  411. // continue;
  412. // }
  413. /* echo */
  414. if(uart0_echo) uart_putc(ch);
  415. /* it's a large line, discard it */
  416. if ( load->line_position + 1 >= load->len ){
  417. load->line_position = 0;
  418. }
  419. }
  420. load->line[load->line_position] = ch;
  421. load->line_position++;
  422. if(!run_input)
  423. {
  424. if( ((need_len!=0) && (load->line_position >= need_len)) || \
  425. (load->line_position >= load->len) || \
  426. ((end_char>=0) && ((unsigned char)ch==(unsigned char)end_char)) )
  427. {
  428. uart_on_data_cb(load->line, load->line_position);
  429. load->line_position = 0;
  430. }
  431. }
  432. ch = 0;
  433. }
  434. if( (load->line_position > 0) && (!run_input) && (need_len==0) && (end_char<0) )
  435. {
  436. uart_on_data_cb(load->line, load->line_position);
  437. load->line_position = 0;
  438. }
  439. return need_dojob;
  440. }