shell.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file shell.c
  15. * @brief Simple CLI shell code.
  16. *
  17. * @addtogroup SHELL
  18. * @{
  19. */
  20. #include <string.h>
  21. #include "ch.h"
  22. #include "hal.h"
  23. #include "shell.h"
  24. #include "shell_cmd.h"
  25. #include "chprintf.h"
  26. /*===========================================================================*/
  27. /* Module local definitions. */
  28. /*===========================================================================*/
  29. /*===========================================================================*/
  30. /* Module exported variables. */
  31. /*===========================================================================*/
  32. #if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
  33. /**
  34. * @brief Shell termination event source.
  35. */
  36. event_source_t shell_terminated;
  37. #endif
  38. /*===========================================================================*/
  39. /* Module local types. */
  40. /*===========================================================================*/
  41. /*===========================================================================*/
  42. /* Module local variables. */
  43. /*===========================================================================*/
  44. /*===========================================================================*/
  45. /* Module local functions. */
  46. /*===========================================================================*/
  47. static char *parse_arguments(char *str, char **saveptr) {
  48. char *p;
  49. if (str != NULL)
  50. *saveptr = str;
  51. p = *saveptr;
  52. if (!p) {
  53. return NULL;
  54. }
  55. /* Skipping white space.*/
  56. p += strspn(p, " \t");
  57. if (*p == '"') {
  58. /* If an argument starts with a double quote then its delimiter is another
  59. quote.*/
  60. p++;
  61. *saveptr = strpbrk(p, "\"");
  62. }
  63. else {
  64. /* The delimiter is white space.*/
  65. *saveptr = strpbrk(p, " \t");
  66. }
  67. /* Replacing the delimiter with a zero.*/
  68. if (*saveptr != NULL) {
  69. *(*saveptr)++ = '\0';
  70. }
  71. return *p != '\0' ? p : NULL;
  72. }
  73. static void list_commands(BaseSequentialStream *chp, const ShellCommand *scp) {
  74. while (scp->sc_name != NULL) {
  75. chprintf(chp, "%s ", scp->sc_name);
  76. scp++;
  77. }
  78. }
  79. static bool cmdexec(const ShellCommand *scp, BaseSequentialStream *chp,
  80. char *name, int argc, char *argv[]) {
  81. while (scp->sc_name != NULL) {
  82. if (strcmp(scp->sc_name, name) == 0) {
  83. scp->sc_function(chp, argc, argv);
  84. return false;
  85. }
  86. scp++;
  87. }
  88. return true;
  89. }
  90. #if (SHELL_USE_HISTORY == TRUE) || defined(__DOXYGEN__)
  91. static void del_histbuff_entry(ShellHistory *shp) {
  92. int pos = shp->sh_beg + *(shp->sh_buffer + shp->sh_beg) + 1;
  93. if (pos >= shp->sh_size)
  94. pos -= shp->sh_size;
  95. shp->sh_beg = pos;
  96. }
  97. static bool is_histbuff_space(ShellHistory *shp, int length) {
  98. if (shp->sh_end >= shp->sh_beg) {
  99. if (length < (shp->sh_size - (shp->sh_end - shp->sh_beg + 1)))
  100. return true;
  101. }
  102. else {
  103. if (length < (shp->sh_beg - shp->sh_end - 1))
  104. return true;
  105. }
  106. return false;
  107. }
  108. static void save_history(ShellHistory *shp, char *line, int length) {
  109. if (shp == NULL)
  110. return;
  111. if (length > shp->sh_size - 2)
  112. return;
  113. while ((*(line + length -1) == ' ') && (length > 0))
  114. length--;
  115. if (length <= 0)
  116. return;
  117. while (!is_histbuff_space(shp, length))
  118. del_histbuff_entry(shp);
  119. if (length < shp->sh_size - shp->sh_end - 1)
  120. memcpy(shp->sh_buffer + shp->sh_end + 1, line, length);
  121. else {
  122. /*
  123. * Since there isn't enough room left at the end of the buffer,
  124. * split the line to save up to the end of the buffer and then
  125. * wrap back to the beginning of the buffer.
  126. */
  127. int part_len = shp->sh_size - shp->sh_end - 1;
  128. memcpy(shp->sh_buffer + shp->sh_end + 1, line, part_len);
  129. memcpy(shp->sh_buffer, line + part_len, length - part_len);
  130. }
  131. /* Save the length of the current line and move the buffer end pointer */
  132. *(shp->sh_buffer + shp->sh_end) = (char)length;
  133. shp->sh_end += length + 1;
  134. if (shp->sh_end >= shp->sh_size)
  135. shp->sh_end -= shp->sh_size;
  136. *(shp->sh_buffer + shp->sh_end) = 0;
  137. shp->sh_cur = 0;
  138. }
  139. static int get_history(ShellHistory *shp, char *line, int dir) {
  140. int count=0;
  141. if (shp == NULL)
  142. return -1;
  143. /* Count the number of lines saved in the buffer */
  144. int idx = shp->sh_beg;
  145. while (idx != shp->sh_end) {
  146. idx += *(shp->sh_buffer + idx) + 1;
  147. if (idx >= shp->sh_size)
  148. idx -= shp->sh_size;
  149. count++;
  150. }
  151. if (dir == SHELL_HIST_DIR_FW) {
  152. if (shp->sh_cur > 0)
  153. shp->sh_cur -= 2;
  154. else
  155. return 0;
  156. }
  157. if (count >= shp->sh_cur) {
  158. idx = shp->sh_beg;
  159. int i = 0;
  160. while (idx != shp->sh_end && shp->sh_cur != (count - i - 1)) {
  161. idx += *(shp->sh_buffer + idx) + 1;
  162. if (idx >= shp->sh_size)
  163. idx -= shp->sh_size;
  164. i++;
  165. }
  166. int length = *(shp->sh_buffer + idx);
  167. if (length > 0) {
  168. shp->sh_cur++;
  169. memset(line, 0, SHELL_MAX_LINE_LENGTH);
  170. if ((idx + length) < shp->sh_size) {
  171. memcpy(line, (shp->sh_buffer + idx + 1), length);
  172. }
  173. else {
  174. /*
  175. * Since the saved line was split at the end of the buffer,
  176. * get the line in two parts.
  177. */
  178. int part_len = shp->sh_size - idx - 1;
  179. memcpy(line, shp->sh_buffer + idx + 1, part_len);
  180. memcpy(line + part_len, shp->sh_buffer, length - part_len);
  181. }
  182. return length;
  183. }
  184. else if (dir == SHELL_HIST_DIR_FW) {
  185. shp->sh_cur++;
  186. return 0;
  187. }
  188. }
  189. return -1;
  190. }
  191. #endif
  192. #if (SHELL_USE_COMPLETION == TRUE) || defined(__DOXYGEN__)
  193. static void get_completions(ShellConfig *scfg, char *line) {
  194. const ShellCommand *lcp = shell_local_commands;
  195. const ShellCommand *scp = scfg->sc_commands;
  196. char **scmp = scfg->sc_completion;
  197. char help_cmp[] = "help";
  198. if (strstr(help_cmp, line) == help_cmp) {
  199. *scmp++ = help_cmp;
  200. }
  201. while (lcp->sc_name != NULL) {
  202. if (strstr(lcp->sc_name, line) == lcp->sc_name) {
  203. *scmp++ = (char *)lcp->sc_name;
  204. }
  205. lcp++;
  206. }
  207. if (scp != NULL) {
  208. while (scp->sc_name != NULL) {
  209. if (strstr(scp->sc_name, line) == scp->sc_name) {
  210. *scmp++ = (char *)scp->sc_name;
  211. }
  212. scp++;
  213. }
  214. }
  215. *scmp = NULL;
  216. }
  217. static int process_completions(ShellConfig *scfg, char *line, int length, unsigned size) {
  218. char **scmp = scfg->sc_completion;
  219. char **cmp = scmp + 1;
  220. char *c = line + length;
  221. int clen = 0;
  222. if (*scmp != NULL) {
  223. if (*cmp == NULL) {
  224. clen = strlen(*scmp);
  225. int i = length;
  226. while ((c < line + clen) && (c < line + size - 1))
  227. *c++ = *(*scmp + i++);
  228. if (c < line + size -1) {
  229. *c = ' ';
  230. clen++;
  231. }
  232. }
  233. else {
  234. while (*(*scmp + clen) != 0) {
  235. while ((*(*scmp + clen) == *(*cmp + clen)) &&
  236. (*(*cmp + clen) != 0) && (*cmp != NULL)) {
  237. cmp++;
  238. }
  239. if (*cmp == NULL) {
  240. if ((c < line + size - 1) && (clen >= length))
  241. *c++ = *(*scmp + clen);
  242. cmp = scmp + 1;
  243. clen++;
  244. }
  245. else {
  246. break;
  247. }
  248. }
  249. }
  250. *(line + clen) = 0;
  251. }
  252. return clen;
  253. }
  254. static void write_completions(ShellConfig *scfg, char *line, int pos) {
  255. BaseSequentialStream *chp = scfg->sc_channel;
  256. char **scmp = scfg->sc_completion;
  257. if (*(scmp + 1) != NULL) {
  258. chprintf(chp, SHELL_NEWLINE_STR);
  259. while (*scmp != NULL)
  260. chprintf(chp, " %s", *scmp++);
  261. chprintf(chp, SHELL_NEWLINE_STR);
  262. chprintf(chp, SHELL_PROMPT_STR);
  263. chprintf(chp, "%s", line);
  264. }
  265. else {
  266. chprintf(chp, "%s", line + pos);
  267. }
  268. }
  269. #endif
  270. /*===========================================================================*/
  271. /* Module exported functions. */
  272. /*===========================================================================*/
  273. /**
  274. * @brief Shell thread function.
  275. *
  276. * @param[in] p pointer to a @p BaseSequentialStream object
  277. */
  278. THD_FUNCTION(shellThread, p) {
  279. int n;
  280. ShellConfig *scfg = p;
  281. BaseSequentialStream *chp = scfg->sc_channel;
  282. const ShellCommand *scp = scfg->sc_commands;
  283. char *lp, *cmd, *tokp, line[SHELL_MAX_LINE_LENGTH];
  284. char *args[SHELL_MAX_ARGUMENTS + 1];
  285. #if SHELL_USE_HISTORY == TRUE
  286. *(scfg->sc_histbuf) = 0;
  287. ShellHistory hist = {
  288. scfg->sc_histbuf,
  289. scfg->sc_histsize,
  290. 0,
  291. 0,
  292. 0
  293. };
  294. ShellHistory *shp = &hist;
  295. #else
  296. ShellHistory *shp = NULL;
  297. #endif
  298. chprintf(chp, SHELL_NEWLINE_STR);
  299. chprintf(chp, "ChibiOS/RT Shell" SHELL_NEWLINE_STR);
  300. #if !defined(_CHIBIOS_NIL_)
  301. while (!chThdShouldTerminateX()) {
  302. #else
  303. while (true) {
  304. #endif
  305. chprintf(chp, SHELL_PROMPT_STR);
  306. if (shellGetLine(scfg, line, sizeof(line), shp)) {
  307. #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
  308. chprintf(chp, SHELL_NEWLINE_STR);
  309. chprintf(chp, "logout");
  310. break;
  311. #else
  312. /* Putting a delay in order to avoid an endless loop trying to read
  313. an unavailable stream.*/
  314. osalThreadSleepMilliseconds(100);
  315. #endif
  316. }
  317. lp = parse_arguments(line, &tokp);
  318. cmd = lp;
  319. n = 0;
  320. while ((lp = parse_arguments(NULL, &tokp)) != NULL) {
  321. if (n >= SHELL_MAX_ARGUMENTS) {
  322. chprintf(chp, "too many arguments" SHELL_NEWLINE_STR);
  323. cmd = NULL;
  324. break;
  325. }
  326. args[n++] = lp;
  327. }
  328. args[n] = NULL;
  329. if (cmd != NULL) {
  330. if (strcmp(cmd, "help") == 0) {
  331. if (n > 0) {
  332. shellUsage(chp, "help");
  333. continue;
  334. }
  335. chprintf(chp, "Commands: help ");
  336. list_commands(chp, shell_local_commands);
  337. if (scp != NULL)
  338. list_commands(chp, scp);
  339. chprintf(chp, SHELL_NEWLINE_STR);
  340. }
  341. else if (cmdexec(shell_local_commands, chp, cmd, n, args) &&
  342. ((scp == NULL) || cmdexec(scp, chp, cmd, n, args))) {
  343. chprintf(chp, "%s", cmd);
  344. chprintf(chp, " ?" SHELL_NEWLINE_STR);
  345. }
  346. }
  347. }
  348. #if !defined(_CHIBIOS_NIL_)
  349. shellExit(MSG_OK);
  350. #endif
  351. }
  352. /**
  353. * @brief Shell manager initialization.
  354. *
  355. * @api
  356. */
  357. void shellInit(void) {
  358. #if !defined(_CHIBIOS_NIL_)
  359. chEvtObjectInit(&shell_terminated);
  360. #endif
  361. }
  362. #if !defined(_CHIBIOS_NIL_) || defined(__DOXYGEN__)
  363. /**
  364. * @brief Terminates the shell.
  365. * @note Must be invoked from the command handlers.
  366. * @note Does not return.
  367. *
  368. * @param[in] msg shell exit code
  369. *
  370. * @api
  371. */
  372. void shellExit(msg_t msg) {
  373. /* Atomically broadcasting the event source and terminating the thread,
  374. there is not a chSysUnlock() because the thread terminates upon return.*/
  375. chSysLock();
  376. chEvtBroadcastI(&shell_terminated);
  377. chThdExitS(msg);
  378. }
  379. #endif
  380. /**
  381. * @brief Reads a whole line from the input channel.
  382. * @note Input chars are echoed on the same stream object with the
  383. * following exceptions:
  384. * - DEL and BS are echoed as BS-SPACE-BS.
  385. * - CR is echoed as CR-LF.
  386. * - 0x4 is echoed as "^D".
  387. * - Other values below 0x20 are not echoed.
  388. * .
  389. *
  390. * @param[in] scfg pointer to a @p ShellConfig object
  391. * @param[in] line pointer to the line buffer
  392. * @param[in] size buffer maximum length
  393. * @param[in] shp pointer to a @p ShellHistory object or NULL
  394. * @return The operation status.
  395. * @retval true the channel was reset or CTRL-D pressed.
  396. * @retval false operation successful.
  397. *
  398. * @api
  399. */
  400. bool shellGetLine(ShellConfig *scfg, char *line, unsigned size, ShellHistory *shp) {
  401. char *p = line;
  402. BaseSequentialStream *chp = scfg->sc_channel;
  403. #if SHELL_USE_ESC_SEQ == TRUE
  404. bool escape = false;
  405. bool bracket = false;
  406. #endif
  407. #if SHELL_USE_HISTORY != TRUE
  408. (void) shp;
  409. #endif
  410. while (true) {
  411. char c;
  412. if (streamRead(chp, (uint8_t *)&c, 1) == 0)
  413. return true;
  414. #if SHELL_USE_ESC_SEQ == TRUE
  415. if (c == 27) {
  416. escape = true;
  417. continue;
  418. }
  419. if (escape) {
  420. escape = false;
  421. if (c == '[') {
  422. escape = true;
  423. bracket = true;
  424. continue;
  425. }
  426. if (bracket) {
  427. bracket = false;
  428. #if SHELL_USE_HISTORY == TRUE
  429. if (c == 'A') {
  430. int len = get_history(shp, line, SHELL_HIST_DIR_BK);
  431. if (len > 0) {
  432. _shell_reset_cur(chp);
  433. _shell_clr_line(chp);
  434. chprintf(chp, "%s", line);
  435. p = line + len;
  436. }
  437. continue;
  438. }
  439. if (c == 'B') {
  440. int len = get_history(shp, line, SHELL_HIST_DIR_FW);
  441. if (len == 0)
  442. *line = 0;
  443. if (len >= 0) {
  444. _shell_reset_cur(chp);
  445. _shell_clr_line(chp);
  446. chprintf(chp, "%s", line);
  447. p = line + len;
  448. }
  449. continue;
  450. }
  451. #endif
  452. }
  453. continue;
  454. }
  455. #endif
  456. #if (SHELL_CMD_EXIT_ENABLED == TRUE) && !defined(_CHIBIOS_NIL_)
  457. if (c == 4) {
  458. chprintf(chp, "^D");
  459. return true;
  460. }
  461. #endif
  462. if ((c == 8) || (c == 127)) {
  463. if (p != line) {
  464. streamPut(chp, 0x08);
  465. streamPut(chp, 0x20);
  466. streamPut(chp, 0x08);
  467. p--;
  468. }
  469. continue;
  470. }
  471. if (c == '\r') {
  472. chprintf(chp, SHELL_NEWLINE_STR);
  473. #if SHELL_USE_HISTORY == TRUE
  474. save_history(shp, line, p - line);
  475. #endif
  476. *p = 0;
  477. return false;
  478. }
  479. #if SHELL_USE_COMPLETION == TRUE
  480. if (c == '\t') {
  481. if (p < line + size - 1) {
  482. *p = 0;
  483. get_completions(scfg, line);
  484. int len = process_completions(scfg, line, p - line, size);
  485. if (len > 0) {
  486. write_completions(scfg, line, p - line);
  487. p = line + len;
  488. }
  489. }
  490. continue;
  491. }
  492. #endif
  493. #if SHELL_USE_HISTORY == TRUE
  494. if (c == 14) {
  495. int len = get_history(shp, line, SHELL_HIST_DIR_FW);
  496. if (len == 0)
  497. *line = 0;
  498. if (len >= 0) {
  499. _shell_reset_cur(chp);
  500. _shell_clr_line(chp);
  501. chprintf(chp, "%s", line);
  502. p = line + len;
  503. }
  504. continue;
  505. }
  506. if (c == 16) {
  507. int len = get_history(shp, line, SHELL_HIST_DIR_BK);
  508. if (len > 0) {
  509. _shell_reset_cur(chp);
  510. _shell_clr_line(chp);
  511. chprintf(chp, "%s", line);
  512. p = line + len;
  513. }
  514. continue;
  515. }
  516. #endif
  517. if (c < 0x20)
  518. continue;
  519. if (p < line + size - 1) {
  520. streamPut(chp, c);
  521. *p++ = (char)c;
  522. }
  523. }
  524. }
  525. /** @} */