1
0

lua_in_finsh.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * run lua interpreter from finsh
  3. */
  4. #include "rtthread.h"
  5. #include "finsh.h"
  6. #include "shell.h"
  7. struct
  8. {
  9. struct rt_semaphore sem;
  10. rt_device_t device;
  11. } dev4lua;
  12. extern int lua_main(int argc, char **argv);
  13. rt_err_t lua_rx_ind(rt_device_t dev, rt_size_t size)
  14. {
  15. rt_sem_release(&dev4lua.sem);
  16. return RT_EOK;
  17. }
  18. void finsh_lua(void *parameter)
  19. {
  20. rt_err_t (*rx_indicate)(rt_device_t dev, rt_size_t size);
  21. rt_sem_init(&(dev4lua.sem), "luasem", 0, 0);
  22. /* save old rx_indicate */
  23. rx_indicate = dev4lua.device->rx_indicate;
  24. /* set new rx_indicate */
  25. rt_device_set_rx_indicate(dev4lua.device, lua_rx_ind);
  26. {
  27. int argc = 1;
  28. char *argv[] = {"lua", NULL};
  29. /* run lua interpreter */
  30. lua_main(argc, argv);
  31. }
  32. /* recover old rx_indicate */
  33. rt_device_set_rx_indicate(dev4lua.device, rx_indicate);
  34. }
  35. static void lua(void)
  36. {
  37. rt_thread_t lua_thread;
  38. const char* device_name = finsh_get_device();
  39. rt_device_t device = rt_device_find(device_name);
  40. if (device == RT_NULL)
  41. {
  42. rt_kprintf("%s not find\n", device_name);
  43. return;
  44. }
  45. dev4lua.device = device;
  46. #if 0
  47. /* Run lua interpreter in separate thread */
  48. lua_thread = rt_thread_create("lua",
  49. finsh_lua,
  50. 0,
  51. 2048,
  52. rt_thread_self()->current_priority + 1,
  53. 20);
  54. if (lua_thread != RT_NULL)
  55. {
  56. rt_thread_startup(lua_thread);
  57. }
  58. #else
  59. /* Directly run lua interpreter in finsh */
  60. finsh_lua(0);
  61. #endif
  62. }
  63. FINSH_FUNCTION_EXPORT(lua, lua interpreter)
  64. int readline4lua(const char *prompt, char *buffer, int buffer_size)
  65. {
  66. char ch;
  67. int line_position;
  68. start:
  69. /* show prompt */
  70. rt_kprintf(prompt);
  71. line_position = 0;
  72. memset(buffer, 0, buffer_size);
  73. while (1)
  74. {
  75. if (rt_sem_take(&dev4lua.sem, RT_WAITING_FOREVER) != RT_EOK)
  76. {
  77. return 0;
  78. }
  79. while (rt_device_read(dev4lua.device, 0, &ch, 1) == 1)
  80. {
  81. /* handle CR key */
  82. if (ch == '\r')
  83. {
  84. char next;
  85. if (rt_device_read(dev4lua.device, 0, &next, 1) == 1)
  86. ch = next;
  87. }
  88. /* backspace key */
  89. else if (ch == 0x7f || ch == 0x08)
  90. {
  91. if (line_position > 0)
  92. {
  93. rt_kprintf("%c %c", ch, ch);
  94. line_position--;
  95. }
  96. buffer[line_position] = 0;
  97. continue;
  98. }
  99. /* EOF(ctrl+d) */
  100. else if (ch == 0x04)
  101. {
  102. if (line_position == 0)
  103. /* No input which makes lua interpreter close */
  104. return 0;
  105. else
  106. continue;
  107. }
  108. /* end of line */
  109. if (ch == '\r' || ch == '\n')
  110. {
  111. buffer[line_position] = 0;
  112. rt_kprintf("\n");
  113. if (line_position == 0)
  114. {
  115. /* Get a empty line, then go to get a new line */
  116. goto start;
  117. }
  118. else
  119. {
  120. return line_position;
  121. }
  122. }
  123. /* other control character or not an acsii character */
  124. if (ch < 0x20 || ch >= 0x80)
  125. {
  126. continue;
  127. }
  128. /* echo */
  129. rt_kprintf("%c", ch);
  130. buffer[line_position] = ch;
  131. ch = 0;
  132. line_position++;
  133. /* it's a large line, discard it */
  134. if (line_position >= buffer_size)
  135. line_position = 0;
  136. }
  137. }
  138. }