nr_micro_shell.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**
  2. * @file nr_micro_shell.c
  3. * @author Ji Youzhou
  4. * @version V0.1
  5. * @date 28 Oct 2019
  6. * @brief [brief]
  7. * *****************************************************************************
  8. * @attention
  9. *
  10. * MIT License
  11. *
  12. * Copyright (C) 2019 Ji Youzhou. or its affiliates. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in all
  22. * copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "nr_micro_shell.h"
  34. #include <string.h>
  35. #include <ctype.h>
  36. NR_SHELL_CMD_EXPORT_START(0,NULL);
  37. NR_SHELL_CMD_EXPORT_END(n,NULL);
  38. shell_st nr_shell =
  39. {
  40. .user_name = NR_SHELL_USER_NAME,
  41. .static_cmd = nr_cmd_start_add,
  42. };
  43. static char *nr_shell_strtok(char *string_org, const char *demial)
  44. {
  45. static unsigned char *last;
  46. unsigned char *str;
  47. const unsigned char *ctrl = (const unsigned char *)demial;
  48. unsigned char map[32];
  49. int count;
  50. for (count = 0; count < 32; count++)
  51. {
  52. map[count] = 0;
  53. }
  54. do
  55. {
  56. map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  57. } while (*ctrl++);
  58. if (string_org)
  59. {
  60. str = (unsigned char *)string_org;
  61. }
  62. else
  63. {
  64. str = last;
  65. }
  66. while ((map[*str >> 3] & (1 << (*str & 7))) && *str)
  67. {
  68. str++;
  69. }
  70. string_org = (char *)str;
  71. for (; *str; str++)
  72. {
  73. if (map[*str >> 3] & (1 << (*str & 7)))
  74. {
  75. *str++ = '\0';
  76. break;
  77. }
  78. }
  79. last = str;
  80. if (string_org == (char *)str)
  81. {
  82. return NULL;
  83. }
  84. else
  85. {
  86. return string_org;
  87. }
  88. }
  89. void _shell_init(shell_st *shell)
  90. {
  91. #ifdef NR_SHELL_SHOW_LOG
  92. shell_printf(" _ _ ____ __ __ _ ____ _ _ _ \r\n");
  93. shell_printf("| \\ | | _ \\ | \\/ (_) ___ _ __ ___ / ___|| |__ ___| | |\r\n");
  94. shell_printf("| \\| | |_) | | |\\/| | |/ __| '__/ _ \\ \\___ \\| '_ \\ / _ \\ | |\r\n");
  95. shell_printf("| |\\ | _ < | | | | | (__| | | (_) | ___) | | | | __/ | |\r\n");
  96. shell_printf("|_| \\_|_| \\_\\ |_| |_|_|\\___|_| \\___/ |____/|_| |_|\\___|_|_|\r\n");
  97. shell_printf(" \r\n");
  98. #endif
  99. shell_printf("%s",shell->user_name);
  100. shell_his_queue_init(&shell->cmd_his);
  101. shell_his_queue_add_cmd(&shell->cmd_his, "ls cmd");
  102. shell->cmd_his.index = 1;
  103. }
  104. shell_fun_t shell_search_cmd(shell_st *shell, char *str)
  105. {
  106. unsigned int i = 0;
  107. while (shell->static_cmd[i].fp != NULL)
  108. {
  109. if (!strcmp(str, shell->static_cmd[i].cmd))
  110. {
  111. return shell->static_cmd[i].fp;
  112. }
  113. i++;
  114. }
  115. return NULL;
  116. }
  117. void shell_parser(shell_st *shell, char *str)
  118. {
  119. char argc = 0;
  120. char argv[NR_SHELL_CMD_LINE_MAX_LENGTH + NR_SHELL_CMD_PARAS_MAX_NUM];
  121. char *token = str;
  122. shell_fun_t fp;
  123. char index = NR_SHELL_CMD_PARAS_MAX_NUM;
  124. if (shell_his_queue_search_cmd(&shell->cmd_his, str) == 0 && str[0] != '\0')
  125. {
  126. shell_his_queue_add_cmd(&shell->cmd_his, str);
  127. }
  128. if (strlen(str) > NR_SHELL_CMD_LINE_MAX_LENGTH)
  129. {
  130. shell_printf("this command is too long."NR_SHELL_NEXT_LINE);
  131. shell_printf("%s",shell->user_name);
  132. return;
  133. }
  134. token = nr_shell_strtok(token, " ");
  135. fp = shell_search_cmd(shell, str);
  136. if (fp == NULL)
  137. {
  138. if (isalpha(str[0]))
  139. {
  140. shell_printf("no command named: %s"NR_SHELL_NEXT_LINE, token);
  141. }
  142. }
  143. else
  144. {
  145. argv[argc] = index;
  146. strcpy(argv + index, str);
  147. index += strlen(str) + 1;
  148. argc++;
  149. token = nr_shell_strtok(NULL, " ");
  150. while (token != NULL)
  151. {
  152. argv[argc] = index;
  153. strcpy(argv + index, token);
  154. index += strlen(token) + 1;
  155. argc++;
  156. token = nr_shell_strtok(NULL, " ");
  157. }
  158. }
  159. if (fp != NULL)
  160. {
  161. fp(argc, argv);
  162. }
  163. shell_printf("%s",shell->user_name);
  164. }
  165. char *shell_cmd_complete(shell_st *shell, char *str)
  166. {
  167. char *temp = NULL;
  168. unsigned char i;
  169. char *best_matched = NULL;
  170. unsigned char min_position = 255;
  171. for (i = 0; shell->static_cmd[i].cmd[0] != '\0'; i++)
  172. {
  173. temp = NULL;
  174. temp = strstr(shell->static_cmd[i].cmd, str);
  175. if (temp != NULL && ((unsigned long)temp - (unsigned long)(&shell->static_cmd[i]) < min_position))
  176. {
  177. min_position = (unsigned long)temp - (unsigned long)(&shell->static_cmd[i]);
  178. best_matched = (char *)&shell->static_cmd[i];
  179. if (min_position == 0)
  180. {
  181. break;
  182. }
  183. }
  184. }
  185. return best_matched;
  186. }
  187. void shell_his_queue_init(shell_his_queue_st *queue)
  188. {
  189. queue->fp = 0;
  190. queue->rp = 0;
  191. queue->len = 0;
  192. queue->store_front = 0;
  193. queue->store_rear = 0;
  194. queue->store_num = 0;
  195. }
  196. void shell_his_queue_add_cmd(shell_his_queue_st *queue, char *str)
  197. {
  198. unsigned short int str_len;
  199. unsigned short int i;
  200. str_len = strlen(str);
  201. if (str_len > NR_SHELL_CMD_HISTORY_BUF_LENGTH)
  202. {
  203. return;
  204. }
  205. while (str_len > (NR_SHELL_CMD_HISTORY_BUF_LENGTH - queue->store_num) || queue->len == NR_SHELL_MAX_CMD_HISTORY_NUM)
  206. {
  207. queue->fp++;
  208. queue->fp = (queue->fp > NR_SHELL_MAX_CMD_HISTORY_NUM) ? 0 : queue->fp;
  209. queue->len--;
  210. if (queue->store_front <= queue->queue[queue->fp])
  211. {
  212. queue->store_num -= queue->queue[queue->fp] - queue->store_front;
  213. }
  214. else
  215. {
  216. queue->store_num -= queue->queue[queue->fp] + NR_SHELL_CMD_HISTORY_BUF_LENGTH - queue->store_front + 1;
  217. }
  218. queue->store_front = queue->queue[queue->fp];
  219. }
  220. queue->queue[queue->rp] = queue->store_rear;
  221. queue->rp++;
  222. queue->rp = (queue->rp > NR_SHELL_MAX_CMD_HISTORY_NUM) ? 0 : queue->rp;
  223. queue->len++;
  224. for (i = 0; i < str_len; i++)
  225. {
  226. queue->buf[queue->store_rear] = str[i];
  227. queue->store_rear++;
  228. queue->store_rear = (queue->store_rear > NR_SHELL_CMD_HISTORY_BUF_LENGTH) ? 0 : queue->store_rear;
  229. queue->store_num++;
  230. }
  231. queue->queue[queue->rp] = queue->store_rear;
  232. }
  233. unsigned short int shell_his_queue_search_cmd(shell_his_queue_st *queue, char *str)
  234. {
  235. unsigned short int str_len;
  236. unsigned short int i, j;
  237. unsigned short int index_temp = queue->fp;
  238. unsigned short int start;
  239. unsigned short int end;
  240. unsigned short int cmd_len;
  241. unsigned short int matched_id = 0;
  242. unsigned short int buf_index;
  243. if (queue->len == 0)
  244. {
  245. return matched_id;
  246. }
  247. else
  248. {
  249. str_len = strlen(str);
  250. for (i = 0; i < queue->len; i++)
  251. {
  252. start = queue->queue[index_temp];
  253. index_temp++;
  254. index_temp = (index_temp > NR_SHELL_MAX_CMD_HISTORY_NUM) ? 0 : index_temp;
  255. end = queue->queue[index_temp];
  256. if (start <= end)
  257. {
  258. cmd_len = end - start;
  259. }
  260. else
  261. {
  262. cmd_len = NR_SHELL_CMD_HISTORY_BUF_LENGTH + 1 - start + end;
  263. }
  264. if (cmd_len == str_len)
  265. {
  266. matched_id = i + 1;
  267. buf_index = start;
  268. for (j = 0; j < str_len; j++)
  269. {
  270. if (queue->buf[buf_index] != str[j])
  271. {
  272. matched_id = 0;
  273. break;
  274. }
  275. buf_index++;
  276. buf_index = (buf_index > NR_SHELL_CMD_HISTORY_BUF_LENGTH) ? 0 : buf_index;
  277. }
  278. if (matched_id != 0)
  279. {
  280. return matched_id;
  281. }
  282. }
  283. }
  284. return 0;
  285. }
  286. }
  287. void shell_his_copy_queue_item(shell_his_queue_st *queue, unsigned short i, char *str_buf)
  288. {
  289. unsigned short index_temp;
  290. unsigned short start;
  291. unsigned short end;
  292. unsigned short j;
  293. if (i <= queue->len)
  294. {
  295. index_temp = queue->fp + i - 1;
  296. index_temp = (index_temp > NR_SHELL_MAX_CMD_HISTORY_NUM) ? (index_temp - NR_SHELL_MAX_CMD_HISTORY_NUM - 1) : index_temp;
  297. start = queue->queue[index_temp];
  298. index_temp++;
  299. index_temp = (index_temp > NR_SHELL_MAX_CMD_HISTORY_NUM) ? 0 : index_temp;
  300. end = queue->queue[index_temp];
  301. if (start < end)
  302. {
  303. for (j = start; j < end; j++)
  304. {
  305. str_buf[j - start] = queue->buf[j];
  306. }
  307. str_buf[j - start] = '\0';
  308. }
  309. else
  310. {
  311. for (j = start; j < NR_SHELL_CMD_HISTORY_BUF_LENGTH + 1; j++)
  312. {
  313. str_buf[j - start] = queue->buf[j];
  314. }
  315. for (j = 0; j < end; j++)
  316. {
  317. str_buf[j + NR_SHELL_CMD_HISTORY_BUF_LENGTH + 1 - start] = queue->buf[j];
  318. }
  319. str_buf[j + NR_SHELL_CMD_HISTORY_BUF_LENGTH + 1 - start] = '\0';
  320. }
  321. }
  322. }
  323. /******************* (C) COPYRIGHT 2019 Ji Youzhou *****END OF FILE*****************/