cli_inter_actor.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "cli_inter_actor.h"
  2. #include "cli.h"
  3. #include "cli_inter_preter.h"
  4. #include <stdarg.h>
  5. #include <stdio.h>
  6. #include <sys/_types.h>
  7. #define CLI_ITT_ANSI_ENABLE O
  8. static char cli_itt_list[CLI_ITT_LIST_NUM][CLI_ITT_LIST_LEN] = {0};
  9. static char cli_itt_list_hint_index = 0, cli_itt_list_his_cnt = 0;
  10. static unsigned char cli_itt_list_index = 0;
  11. unsigned char g_cli_chl = CLI_CHL_COM;
  12. void eoch(char c)
  13. {
  14. if (g_cli_chl == CLI_CHL_COM)
  15. CLI_PRINTF("%c", c);
  16. }
  17. void cli_itt_list_add(unsigned char *item)
  18. {
  19. strcpy(cli_itt_list[cli_itt_list_index], (const char *)item);
  20. cli_itt_list_index = (cli_itt_list_index + 1) % CLI_ITT_LIST_NUM;
  21. cli_itt_list_his_cnt++;
  22. if (cli_itt_list_his_cnt >= CLI_ITT_LIST_NUM)
  23. cli_itt_list_his_cnt = CLI_ITT_LIST_NUM;
  24. }
  25. unsigned char diff_num(unsigned char a, unsigned char b)
  26. {
  27. return a > b ? a - b : b - a;
  28. }
  29. unsigned char cli_itt_list_index_last(void)
  30. {
  31. unsigned char index;
  32. index = (cli_itt_list_hint_index + CLI_ITT_LIST_NUM - 1) % CLI_ITT_LIST_NUM;
  33. if ((index != cli_itt_list_index) && (diff_num(cli_itt_list_hint_index, cli_itt_list_index) < cli_itt_list_his_cnt))
  34. {
  35. cli_itt_list_hint_index = index;
  36. }
  37. return cli_itt_list_hint_index;
  38. }
  39. unsigned char cli_itt_list_index_next(void)
  40. {
  41. unsigned char index;
  42. index = (cli_itt_list_hint_index + 1) % CLI_ITT_LIST_NUM;
  43. if ((index != cli_itt_list_index) && (diff_num(cli_itt_list_hint_index, cli_itt_list_index) < cli_itt_list_his_cnt))
  44. {
  45. cli_itt_list_hint_index = index;
  46. }
  47. else
  48. {
  49. // cli_itt_list_hint_index = cli_itt_list_index;
  50. }
  51. return cli_itt_list_hint_index;
  52. }
  53. void clear_line(void)
  54. {
  55. CLI_PRINTF("\r");
  56. for (char i = 0; i < CLI_ITT_LIST_LEN; i++)
  57. {
  58. CLI_PRINTF(" ");
  59. }
  60. CLI_PRINTF("\r");
  61. }
  62. void clear_line_ansi(unsigned int cursor)
  63. {
  64. CLI_PRINTF("%c[%dD", '\033', cursor);
  65. CLI_PRINTF("%c[K", '\033');
  66. }
  67. void cli_printf(char *msg, ...)
  68. {
  69. va_list vl;
  70. // char s[10];
  71. if (g_cli_chl == CLI_CHL_COM)
  72. {
  73. va_start(vl, msg);
  74. // vsprintf(s, msg, vl);
  75. vprintf(msg, vl);
  76. va_end(vl);
  77. }
  78. else
  79. {
  80. #ifdef TELNET
  81. va_start(vl, msg);
  82. telnet_esp32_vprintf(msg, vl);
  83. va_end(vl);
  84. #endif
  85. }
  86. }
  87. void cli_itt_input(CLI_CHL chl, char c)
  88. {
  89. // static unsigned char enter = 0;
  90. static unsigned char dir = 0, ch;
  91. static char item[CLI_ITT_LIST_LEN];
  92. static unsigned char item_index = 0;
  93. int res;
  94. char hint[CLI_ITT_LIST_LEN];
  95. ch = (unsigned char)c;
  96. // CLI_PRINTF("%02x", ch);
  97. // return;
  98. switch (ch)
  99. {
  100. case '\r':
  101. #define OS_MAC
  102. #ifdef OS_MAC:
  103. item[item_index] = '\0';
  104. if (g_cli_chl == CLI_CHL_COM)
  105. CLI_PRINTF("\r\n");
  106. if (strlen(item) != 0)
  107. {
  108. cli_itt_list_add((unsigned char *)item);
  109. cli_itt_list_hint_index = cli_itt_list_index;
  110. }
  111. command_entry_match(main_table, item);
  112. memset(item, 0, CLI_ITT_LIST_LEN);
  113. // emter = 0;
  114. item_index = 0;
  115. break;
  116. #else
  117. // enter = 1;
  118. return;
  119. #endif
  120. case '\n':
  121. item[item_index] = '\0';
  122. if (g_cli_chl == CLI_CHL_COM)
  123. CLI_PRINTF("\r\n");
  124. if (strlen(item) != 0)
  125. {
  126. cli_itt_list_add((unsigned char *)item);
  127. cli_itt_list_hint_index = cli_itt_list_index;
  128. }
  129. command_entry_match(main_table, item);
  130. memset(item, 0, CLI_ITT_LIST_LEN);
  131. // emter = 0;
  132. item_index = 0;
  133. break;
  134. case '\t': // Tab
  135. res = command_entry_hint(main_table, item, hint);
  136. if (res == 0)
  137. {
  138. }
  139. else if (res == 1)
  140. {
  141. CLI_PRINTF("\r\n%s\r\n# %s", hint, item);
  142. }
  143. else if (res == 2)
  144. {
  145. #if CLI_ITT_ANSI_ENABLE == 0
  146. clear_line();
  147. #else
  148. clear_line_ansi(item_index);
  149. #endif
  150. CLI_PRINTF("\r# %s", hint);
  151. strcpy(item, hint);
  152. item_index = strlen(item);
  153. }
  154. else if (res == 3)
  155. {
  156. #if CLI_ITT_ANSI_ENABLE == 0
  157. clear_line();
  158. #else
  159. clear_line_ansi(item_index);
  160. #endif
  161. CLI_PRINTF("\r# %s", hint);
  162. strcpy(item, hint);
  163. item_index = strlen(item);
  164. }
  165. else
  166. {
  167. }
  168. break;
  169. case 0x1b: // ESC/UP-1
  170. dir = 1;
  171. break;
  172. case 0x5b:
  173. dir = 2;
  174. break;
  175. // case 0x1b: // ESC
  176. // memcpy(item, 0, sizeof(item));
  177. // item_index = 0;
  178. // CLI_PRINTF("\r%s", item);
  179. // break;
  180. case 8: // BackSpace
  181. if (item_index > 0)
  182. {
  183. item_index--;
  184. #if CLI_ITT_ANSI_ENABLE == 1
  185. CLI_PRINTF("%c[1D", '\033');
  186. CLI_PRINTF("%c[K", '\033');
  187. #endif
  188. item[item_index] = '\0';
  189. #if CLI_ITT_ANSI_ENABLE == 0
  190. clear_line();
  191. CLI_PRINTF("\r# %s", item);
  192. #endif
  193. break;
  194. }
  195. default:
  196. if ((ch == 0x41 || ch == 0x42) && (dir == 2))
  197. {
  198. if (ch == 0x41) // UP
  199. {
  200. strcpy(item, cli_itt_list[cli_itt_list_index_last()]);
  201. }
  202. else // DOWN
  203. {
  204. if (cli_itt_list_hint_index == cli_itt_list_index)
  205. {
  206. }
  207. else if (((cli_itt_list_hint_index + CLI_ITT_LIST_NUM + 1) % CLI_ITT_LIST_NUM) == cli_itt_list_index)
  208. {
  209. cli_itt_list_hint_index = cli_itt_list_index;
  210. memset(item, 0, CLI_ITT_LIST_NUM);
  211. }
  212. else
  213. {
  214. strcpy(item, cli_itt_list[cli_itt_list_index_next()]);
  215. }
  216. }
  217. #if CLI_ITT_ANSI_ENABLE == 1
  218. clear_line_ansi(item_index);
  219. #endif
  220. item_index = strlen(item);
  221. #if CLI_ITT_ANSI_ENABLE == 0
  222. clear_line();
  223. #endif
  224. CLI_PRINTF("\r# %s", item);
  225. dir = 0;
  226. break;
  227. }
  228. if ((ch == 0x43 || ch == 0x44) && (dir == 2))
  229. {
  230. break;
  231. }
  232. eoch(c);
  233. if (item_index <= (CLI_ITT_LIST_LEN - 2))
  234. {
  235. item[item_index++] = c;
  236. }
  237. break;
  238. }
  239. }