shell.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*
  2. * Copyright (c) 2015-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. */
  20. #include <assert.h>
  21. #include <errno.h>
  22. #include <stdarg.h>
  23. #include <stddef.h>
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <kern/bulletin.h>
  28. #include <kern/hash.h>
  29. #include <kern/init.h>
  30. #include <kern/log.h>
  31. #include <kern/macros.h>
  32. #include <kern/mutex.h>
  33. #include <kern/printf.h>
  34. #include <kern/shell.h>
  35. #include <kern/thread.h>
  36. #define SHELL_COMPLETION_MATCH_FMT "-16s"
  37. #define SHELL_COMPLETION_NR_MATCHES_PER_LINE 4
  38. /*
  39. * Escape sequence states.
  40. *
  41. * Here is an incomplete description of escape sequences :
  42. * http://en.wikipedia.org/wiki/ANSI_escape_code
  43. *
  44. * These values must be different from 0.
  45. */
  46. #define SHELL_ESC_STATE_START 1
  47. #define SHELL_ESC_STATE_CSI 2
  48. typedef void (*shell_esc_seq_fn) (struct shell *);
  49. struct shell_esc_seq
  50. {
  51. const char *str;
  52. shell_esc_seq_fn fn;
  53. };
  54. #define SHELL_SEPARATOR ' '
  55. /*
  56. * Commonly used backspace control characters.
  57. *
  58. * XXX Adjust for your needs.
  59. */
  60. #define SHELL_ERASE_BS '\b'
  61. #define SHELL_ERASE_DEL '\x7f'
  62. static struct bulletin_sub shell_log_bulletin_sub;
  63. static const char*
  64. shell_find_word (const char *str)
  65. {
  66. for ( ; ; ++str)
  67. if (!*str || *str != SHELL_SEPARATOR)
  68. return (str);
  69. }
  70. void
  71. shell_cmd_init (struct shell_cmd *cmd, const char *name,
  72. shell_fn_t fn, const char *usage,
  73. const char *short_desc, const char *long_desc)
  74. {
  75. cmd->ht_next = NULL;
  76. cmd->ls_next = NULL;
  77. cmd->name = name;
  78. cmd->fn = fn;
  79. cmd->usage = usage;
  80. cmd->short_desc = short_desc;
  81. cmd->long_desc = long_desc;
  82. }
  83. static const char*
  84. shell_cmd_name (const struct shell_cmd *cmd)
  85. {
  86. return (cmd->name);
  87. }
  88. static int
  89. shell_cmd_check_char (char c)
  90. {
  91. if ((c >= 'a' && c <= 'z') ||
  92. (c >= 'A' && c <= 'Z') ||
  93. (c >= '0' && c <= '9') ||
  94. c == '-' || c == '_')
  95. return (0);
  96. return (EINVAL);
  97. }
  98. static int
  99. shell_cmd_check (const struct shell_cmd *cmd)
  100. {
  101. size_t len = strlen (cmd->name);
  102. if (! len)
  103. return (EINVAL);
  104. for (size_t i = 0; i < len; i++)
  105. {
  106. int error = shell_cmd_check_char (cmd->name[i]);
  107. if (error)
  108. return (error);
  109. }
  110. return (0);
  111. }
  112. static const char*
  113. shell_line_str (const struct shell_line *line)
  114. {
  115. return (line->str);
  116. }
  117. static size_t
  118. shell_line_size (const struct shell_line *line)
  119. {
  120. return (line->size);
  121. }
  122. static void
  123. shell_line_reset (struct shell_line *line)
  124. {
  125. line->str[0] = '\0';
  126. line->size = 0;
  127. }
  128. static void
  129. shell_line_copy (struct shell_line *dest, const struct shell_line *src)
  130. {
  131. strcpy (dest->str, src->str);
  132. dest->size = src->size;
  133. }
  134. static int
  135. shell_line_cmp (const struct shell_line *a, const struct shell_line *b)
  136. {
  137. return (strcmp (a->str, b->str));
  138. }
  139. static int
  140. shell_line_insert (struct shell_line *line, size_t index, char c)
  141. {
  142. if (index > line->size)
  143. return (EINVAL);
  144. else if (line->size + 1 == sizeof (line->str))
  145. return (ENOMEM);
  146. size_t remaining_chars = line->size - index;
  147. if (remaining_chars)
  148. memmove (&line->str[index + 1], &line->str[index], remaining_chars);
  149. line->str[index] = c;
  150. line->str[++line->size] = '\0';
  151. return (0);
  152. }
  153. static int
  154. shell_line_erase (struct shell_line *line, size_t index)
  155. {
  156. if (index >= line->size)
  157. return (EINVAL);
  158. size_t remaining_chars = line->size - index - 1;
  159. if (remaining_chars)
  160. memmove (&line->str[index], &line->str[index + 1], remaining_chars);
  161. line->str[--line->size] = '\0';
  162. return (0);
  163. }
  164. static struct shell_line*
  165. shell_history_get (struct shell_history *history, size_t index)
  166. {
  167. return (&history->lines[index % ARRAY_SIZE (history->lines)]);
  168. }
  169. static void
  170. shell_history_init (struct shell_history *history)
  171. {
  172. for (size_t i = 0; i < ARRAY_SIZE (history->lines); i++)
  173. shell_line_reset (shell_history_get (history, i));
  174. history->newest = 0;
  175. history->oldest = 0;
  176. history->index = 0;
  177. }
  178. static struct shell_line*
  179. shell_history_get_newest (struct shell_history *history)
  180. {
  181. return (shell_history_get (history, history->newest));
  182. }
  183. static struct shell_line*
  184. shell_history_get_index (struct shell_history *history)
  185. {
  186. return (shell_history_get (history, history->index));
  187. }
  188. static void
  189. shell_history_reset_index (struct shell_history *history)
  190. {
  191. history->index = history->newest;
  192. }
  193. static int
  194. shell_history_same_newest (struct shell_history *history)
  195. {
  196. return (history->newest != history->oldest &&
  197. shell_line_cmp (shell_history_get_newest (history),
  198. shell_history_get (history, history->newest - 1))
  199. == 0);
  200. }
  201. static void
  202. shell_history_push (struct shell_history *history)
  203. {
  204. if (shell_line_size (shell_history_get_newest (history)) == 0 ||
  205. shell_history_same_newest (history))
  206. {
  207. shell_history_reset_index (history);
  208. return;
  209. }
  210. ++history->newest;
  211. shell_history_reset_index (history);
  212. // Mind integer overflows.
  213. if (history->newest - history->oldest >= ARRAY_SIZE (history->lines))
  214. history->oldest = history->newest - ARRAY_SIZE (history->lines) + 1;
  215. }
  216. static void
  217. shell_history_back (struct shell_history *history)
  218. {
  219. if (history->index == history->oldest)
  220. return;
  221. --history->index;
  222. shell_line_copy (shell_history_get_newest (history),
  223. shell_history_get_index (history));
  224. }
  225. static void
  226. shell_history_forward (struct shell_history *history)
  227. {
  228. if (history->index == history->newest)
  229. return;
  230. else if (++history->index == history->newest)
  231. shell_line_reset (shell_history_get_newest (history));
  232. else
  233. shell_line_copy (shell_history_get_newest (history),
  234. shell_history_get_index (history));
  235. }
  236. static void
  237. shell_history_print (struct shell_history *history, struct shell *shell)
  238. {
  239. // Mind integer overflows.
  240. for (size_t i = history->oldest; i != history->newest; i++)
  241. {
  242. _Auto line = shell_history_get (history, i);
  243. shell_printf (shell, "%6zu %s\n",
  244. (size_t) (i - history->oldest),
  245. shell_line_str (line));
  246. }
  247. }
  248. static void
  249. shell_cmd_set_lock (struct shell_cmd_set *cmd_set)
  250. {
  251. mutex_lock (&cmd_set->lock);
  252. }
  253. static void
  254. shell_cmd_set_unlock (struct shell_cmd_set *cmd_set)
  255. {
  256. mutex_unlock (&cmd_set->lock);
  257. }
  258. static struct shell_bucket*
  259. shell_cmd_set_get_bucket (struct shell_cmd_set *cmd_set, const char *name)
  260. {
  261. size_t index = hash_bytes (name, strlen (name)) %
  262. ARRAY_SIZE (cmd_set->htable);
  263. return (&cmd_set->htable[index]);
  264. }
  265. static const struct shell_cmd*
  266. shell_cmd_set_lookup (struct shell_cmd_set *cmd_set, const char *name)
  267. {
  268. shell_cmd_set_lock (cmd_set);
  269. const _Auto bucket = shell_cmd_set_get_bucket (cmd_set, name);
  270. _Auto cmd = bucket->cmd;
  271. for (; cmd != NULL; cmd = cmd->ht_next)
  272. if (strcmp (cmd->name, name) == 0)
  273. break;
  274. shell_cmd_set_unlock (cmd_set);
  275. return (cmd);
  276. }
  277. /*
  278. * Look up the first command that matches a given string.
  279. *
  280. * The input string is defined by the given string pointer and size.
  281. *
  282. * The global lock must be acquired before calling this function.
  283. */
  284. static const struct shell_cmd*
  285. shell_cmd_set_match (const struct shell_cmd_set *cmd_set,
  286. const char *str, size_t size)
  287. {
  288. for (_Auto cmd = cmd_set->cmd_list; cmd; cmd = cmd->ls_next)
  289. if (strncmp (cmd->name, str, size) == 0)
  290. return (cmd);
  291. return (NULL);
  292. }
  293. /*
  294. * Attempt command auto-completion.
  295. *
  296. * The given string is the beginning of a command, or the empty string.
  297. * The sizep parameter initially points to the size of the given string.
  298. * If the string matches any registered command, the cmdp pointer is
  299. * updated to point to the first matching command in the sorted list of
  300. * commands, and sizep is updated to the number of characters in the
  301. * command name that are common in subsequent commands. The command
  302. * pointer and the returned size can be used to print a list of commands
  303. * eligible for completion.
  304. *
  305. * If there is a single match for the given string, return 0. If there
  306. * are more than one match, return EAGAIN. If there is no match,
  307. * return EINVAL.
  308. */
  309. static int
  310. shell_cmd_set_complete (struct shell_cmd_set *cmd_set, const char *str,
  311. size_t *sizep, const struct shell_cmd **cmdp)
  312. {
  313. size_t size = *sizep;
  314. /*
  315. * Start with looking up a command that matches the given argument.
  316. * If there is no match, return an error.
  317. */
  318. const _Auto cmd = shell_cmd_set_match (cmd_set, str, size);
  319. if (! cmd)
  320. return (EINVAL);
  321. *cmdp = cmd;
  322. /*
  323. * If at least one command matches, try to complete it.
  324. * There can be two cases :
  325. * 1/ There is one and only one match, which is directly returned.
  326. * 2/ There are several matches, in which case the common length is
  327. * computed.
  328. */
  329. const struct shell_cmd *next = cmd->ls_next;
  330. if (!next || strncmp (cmd->name, next->name, size))
  331. {
  332. *sizep = strlen (cmd->name);
  333. return (0);
  334. }
  335. /*
  336. * When computing the common length, all the commands that can match
  337. * must be evaluated. Considering the current command is the first
  338. * that can match, the only other variable missing is the last
  339. * command that can match.
  340. */
  341. for (; next->ls_next; next = next->ls_next)
  342. if (strncmp (cmd->name, next->ls_next->name, size))
  343. break;
  344. if (! size)
  345. size = 1;
  346. while (cmd->name[size - 1] != '\0' &&
  347. cmd->name[size - 1] == next->name[size - 1])
  348. size++;
  349. *sizep = --size;
  350. return (EAGAIN);
  351. }
  352. struct shell_cmd_set*
  353. shell_get_cmd_set (struct shell *shell)
  354. {
  355. return (shell->cmd_set);
  356. }
  357. static struct shell_history*
  358. shell_get_history (struct shell *shell)
  359. {
  360. return (&shell->history);
  361. }
  362. static void
  363. shell_cb_help (struct shell *shell, int argc, char *argv[])
  364. {
  365. _Auto cmd_set = shell_get_cmd_set (shell);
  366. if (argc > 2)
  367. {
  368. argc = 2;
  369. argv[1] = "help";
  370. }
  371. if (argc == 2)
  372. {
  373. _Auto cmd = shell_cmd_set_lookup (cmd_set, argv[1]);
  374. if (! cmd)
  375. {
  376. shell_printf (shell, "shell: help: %s: command not found\n",
  377. argv[1]);
  378. return;
  379. }
  380. shell_printf (shell, "usage: %s\n%s\n", cmd->usage, cmd->short_desc);
  381. if (cmd->long_desc)
  382. shell_printf (shell, "\n%s\n", cmd->long_desc);
  383. return;
  384. }
  385. shell_cmd_set_lock (cmd_set);
  386. for (_Auto cmd = cmd_set->cmd_list; cmd; cmd = cmd->ls_next)
  387. shell_printf (shell, "%19s %s\n", cmd->name, cmd->short_desc);
  388. shell_cmd_set_unlock (cmd_set);
  389. }
  390. static void
  391. shell_cb_history (struct shell *shell, int argc __unused, char **argv __unused)
  392. {
  393. shell_history_print (shell_get_history (shell), shell);
  394. }
  395. static struct shell_cmd shell_default_cmds[] =
  396. {
  397. SHELL_CMD_INITIALIZER ("help", shell_cb_help,
  398. "help [command]",
  399. "obtain help about shell commands"),
  400. SHELL_CMD_INITIALIZER ("history", shell_cb_history,
  401. "history",
  402. "display history list"),
  403. };
  404. void
  405. shell_cmd_set_init (struct shell_cmd_set *cmd_set)
  406. {
  407. mutex_init (&cmd_set->lock);
  408. memset (cmd_set->htable, 0, sizeof (cmd_set->htable));
  409. cmd_set->cmd_list = NULL;
  410. SHELL_REGISTER_CMDS (shell_default_cmds, cmd_set);
  411. }
  412. static int
  413. shell_cmd_set_add_htable (struct shell_cmd_set *cmd_set, struct shell_cmd *cmd)
  414. {
  415. _Auto bucket = shell_cmd_set_get_bucket (cmd_set, cmd->name);
  416. _Auto tmp = bucket->cmd;
  417. if (! tmp)
  418. {
  419. bucket->cmd = cmd;
  420. return (0);
  421. }
  422. for ( ; ; tmp = tmp->ht_next)
  423. if (strcmp (cmd->name, tmp->name) == 0)
  424. return (EEXIST);
  425. else if (!tmp->ht_next)
  426. break;
  427. tmp->ht_next = cmd;
  428. return (0);
  429. }
  430. static void
  431. shell_cmd_set_add_list (struct shell_cmd_set *cmd_set, struct shell_cmd *cmd)
  432. {
  433. _Auto prev = cmd_set->cmd_list;
  434. if (!prev || strcmp (cmd->name, prev->name) < 0)
  435. {
  436. cmd_set->cmd_list = cmd;
  437. cmd->ls_next = prev;
  438. return;
  439. }
  440. struct shell_cmd *next;
  441. while (1)
  442. {
  443. next = prev->ls_next;
  444. if (!next || strcmp (cmd->name, next->name) < 0)
  445. break;
  446. prev = next;
  447. }
  448. prev->ls_next = cmd;
  449. cmd->ls_next = next;
  450. }
  451. static int
  452. shell_cmd_set_add (struct shell_cmd_set *cmd_set, struct shell_cmd *cmd)
  453. {
  454. int error = shell_cmd_set_add_htable (cmd_set, cmd);
  455. if (error)
  456. return (error);
  457. shell_cmd_set_add_list (cmd_set, cmd);
  458. return (0);
  459. }
  460. int
  461. shell_cmd_set_register (struct shell_cmd_set *cmd_set, struct shell_cmd *cmd)
  462. {
  463. int error = shell_cmd_check (cmd);
  464. if (error)
  465. return (error);
  466. shell_cmd_set_lock (cmd_set);
  467. error = shell_cmd_set_add (cmd_set, cmd);
  468. shell_cmd_set_unlock (cmd_set);
  469. return (error);
  470. }
  471. void
  472. shell_init (struct shell *shell, struct shell_cmd_set *cmd_set,
  473. struct stream *stream)
  474. {
  475. shell->cmd_set = cmd_set;
  476. shell->stream = stream;
  477. shell_history_init (&shell->history);
  478. shell->esc_seq_index = 0;
  479. }
  480. static void
  481. shell_prompt (struct shell *shell)
  482. {
  483. shell_printf (shell, "shell> ");
  484. }
  485. static void
  486. shell_reset (struct shell *shell)
  487. {
  488. shell_line_reset (shell_history_get_newest (&shell->history));
  489. shell->cursor = 0;
  490. shell_prompt (shell);
  491. }
  492. static void
  493. shell_erase (struct shell *shell)
  494. {
  495. _Auto current_line = shell_history_get_newest (&shell->history);
  496. size_t remaining_chars = shell_line_size (current_line);
  497. while (shell->cursor != remaining_chars)
  498. {
  499. shell_printf (shell, " ");
  500. ++shell->cursor;
  501. }
  502. while (remaining_chars != 0)
  503. {
  504. shell_printf (shell, "\b \b");
  505. --remaining_chars;
  506. }
  507. shell->cursor = 0;
  508. }
  509. static void
  510. shell_restore (struct shell *shell)
  511. {
  512. _Auto current_line = shell_history_get_newest (&shell->history);
  513. shell_printf (shell, "%s", shell_line_str (current_line));
  514. shell->cursor = shell_line_size (current_line);
  515. }
  516. static int
  517. shell_is_ctrl_char (char c)
  518. {
  519. return (c < ' ' || c >= 0x7f);
  520. }
  521. static void
  522. shell_process_left (struct shell *shell)
  523. {
  524. if (!shell->cursor)
  525. return;
  526. --shell->cursor;
  527. shell_printf (shell, "\e[1D");
  528. }
  529. static int
  530. shell_process_right (struct shell *shell)
  531. {
  532. size_t size = shell_line_size (shell_history_get_newest (&shell->history));
  533. if (shell->cursor >= size)
  534. return (EAGAIN);
  535. ++shell->cursor;
  536. shell_printf (shell, "\e[1C");
  537. return (0);
  538. }
  539. static void
  540. shell_process_up (struct shell *shell)
  541. {
  542. shell_erase (shell);
  543. shell_history_back (&shell->history);
  544. shell_restore (shell);
  545. }
  546. static void
  547. shell_process_down (struct shell *shell)
  548. {
  549. shell_erase (shell);
  550. shell_history_forward (&shell->history);
  551. shell_restore (shell);
  552. }
  553. static void
  554. shell_process_backspace (struct shell *shell)
  555. {
  556. _Auto current_line = shell_history_get_newest (&shell->history);
  557. int error = shell_line_erase (current_line, shell->cursor - 1);
  558. if (error)
  559. return;
  560. --shell->cursor;
  561. shell_printf (shell, "\b%s ", shell_line_str (current_line) + shell->cursor);
  562. size_t remaining_chars = shell_line_size (current_line) - shell->cursor + 1;
  563. for (; remaining_chars; --remaining_chars)
  564. shell_printf (shell, "\b");
  565. }
  566. static int
  567. shell_process_raw_char (struct shell *shell, char c)
  568. {
  569. _Auto current_line = shell_history_get_newest (&shell->history);
  570. int error = shell_line_insert (current_line, shell->cursor, c);
  571. if (error)
  572. {
  573. shell_printf (shell, "\nshell: line too long\n");
  574. return (error);
  575. }
  576. else if (++shell->cursor == shell_line_size (current_line))
  577. {
  578. shell_printf (shell, "%c", c);
  579. return (0);
  580. }
  581. /*
  582. * This assumes that the backspace character only moves the cursor
  583. * without erasing characters.
  584. */
  585. shell_printf (shell, "%s",
  586. shell_line_str (current_line) + shell->cursor - 1);
  587. size_t remaining_chars = shell_line_size (current_line) - shell->cursor;
  588. for (; remaining_chars; --remaining_chars)
  589. shell_printf (shell, "\b");
  590. return (0);
  591. }
  592. /*
  593. * Print a list of commands eligible for completion, starting at the
  594. * given command. Other eligible commands share the same prefix, as
  595. * defined by the size argument.
  596. *
  597. * The global lock must be acquired before calling this function.
  598. */
  599. static void
  600. shell_print_cmd_matches (struct shell *shell, const struct shell_cmd *cmd,
  601. size_t size)
  602. {
  603. shell_printf (shell, "\n");
  604. size_t i = 1;
  605. for (_Auto tmp = cmd; tmp != NULL; tmp = tmp->ls_next, ++i)
  606. {
  607. if (strncmp (cmd->name, tmp->name, size) != 0)
  608. break;
  609. shell_printf (shell, "%" SHELL_COMPLETION_MATCH_FMT, tmp->name);
  610. if ((i % SHELL_COMPLETION_NR_MATCHES_PER_LINE) == 0)
  611. shell_printf (shell, "\n");
  612. }
  613. if ((i % SHELL_COMPLETION_NR_MATCHES_PER_LINE) != 1)
  614. shell_printf (shell, "\n");
  615. }
  616. static int
  617. shell_process_tabulation (struct shell *shell)
  618. {
  619. const struct shell_cmd *cmd = NULL;
  620. _Auto cmd_set = shell->cmd_set;
  621. shell_cmd_set_lock (cmd_set);
  622. _Auto str = shell_line_str (shell_history_get_newest (&shell->history));
  623. const char *word = shell_find_word (str);
  624. size_t size = shell->cursor - (word - str),
  625. cmd_cursor = shell->cursor - size;
  626. int error = shell_cmd_set_complete (cmd_set, word, &size, &cmd);
  627. if (error && error != EAGAIN)
  628. {
  629. error = 0;
  630. goto out;
  631. }
  632. else if (error == EAGAIN)
  633. {
  634. size_t cursor = shell->cursor;
  635. shell_print_cmd_matches (shell, cmd, size);
  636. shell_prompt (shell);
  637. shell_restore (shell);
  638. // Keep existing arguments as they are.
  639. while (shell->cursor != cursor)
  640. shell_process_left (shell);
  641. }
  642. const char *name = shell_cmd_name (cmd);
  643. while (shell->cursor != cmd_cursor)
  644. shell_process_backspace (shell);
  645. for (size_t i = 0; i < size; i++)
  646. {
  647. error = shell_process_raw_char (shell, name[i]);
  648. if (error)
  649. goto out;
  650. }
  651. error = 0;
  652. out:
  653. shell_cmd_set_unlock (cmd_set);
  654. return (error);
  655. }
  656. static void
  657. shell_esc_seq_up (struct shell *shell)
  658. {
  659. shell_process_up (shell);
  660. }
  661. static void
  662. shell_esc_seq_down (struct shell *shell)
  663. {
  664. shell_process_down (shell);
  665. }
  666. static void
  667. shell_esc_seq_next (struct shell *shell)
  668. {
  669. shell_process_right (shell);
  670. }
  671. static void
  672. shell_esc_seq_prev (struct shell *shell)
  673. {
  674. shell_process_left (shell);
  675. }
  676. static void
  677. shell_esc_seq_home (struct shell *shell)
  678. {
  679. while (shell->cursor)
  680. shell_process_left (shell);
  681. }
  682. static void
  683. shell_esc_seq_del (struct shell *shell)
  684. {
  685. if (shell_process_right (shell) == 0)
  686. shell_process_backspace (shell);
  687. }
  688. static void
  689. shell_esc_seq_end (struct shell *shell)
  690. {
  691. size_t size = shell_line_size (shell_history_get_newest (&shell->history));
  692. while (shell->cursor < size)
  693. shell_process_right (shell);
  694. }
  695. static const struct shell_esc_seq shell_esc_seqs[] =
  696. {
  697. { "A", shell_esc_seq_up },
  698. { "B", shell_esc_seq_down },
  699. { "C", shell_esc_seq_next },
  700. { "D", shell_esc_seq_prev },
  701. { "H", shell_esc_seq_home },
  702. { "1~", shell_esc_seq_home },
  703. { "3~", shell_esc_seq_del },
  704. { "F", shell_esc_seq_end },
  705. { "4~", shell_esc_seq_end },
  706. };
  707. static const struct shell_esc_seq*
  708. shell_esc_seq_lookup (const char *str)
  709. {
  710. for (size_t i = 0; i < ARRAY_SIZE (shell_esc_seqs); i++)
  711. if (strcmp (shell_esc_seqs[i].str, str) == 0)
  712. return (&shell_esc_seqs[i]);
  713. return (NULL);
  714. }
  715. /*
  716. * Process a single escape sequence character.
  717. *
  718. * Return the next escape state or 0 if the sequence is complete.
  719. */
  720. static int
  721. shell_process_esc_sequence (struct shell *shell, char c)
  722. {
  723. if (shell->esc_seq_index >= ARRAY_SIZE (shell->esc_seq) - 1)
  724. {
  725. shell_printf (shell, "shell: escape sequence too long\n");
  726. goto reset;
  727. }
  728. shell->esc_seq[shell->esc_seq_index++] = c;
  729. shell->esc_seq[shell->esc_seq_index] = '\0';
  730. if (!(c >= '@' && c <= '~'))
  731. return (SHELL_ESC_STATE_CSI);
  732. _Auto seq = shell_esc_seq_lookup (shell->esc_seq);
  733. if (seq)
  734. seq->fn (shell);
  735. reset:
  736. shell->esc_seq_index = 0;
  737. return (0);
  738. }
  739. static int
  740. shell_process_args (struct shell *shell)
  741. {
  742. snprintf (shell->tmp_line, sizeof (shell->tmp_line), "%s",
  743. shell_line_str (shell_history_get_newest (&shell->history)));
  744. char c, prev;
  745. size_t i;
  746. int j;
  747. for (i = 0, j = 0, prev = SHELL_SEPARATOR;
  748. (c = shell->tmp_line[i]) != '\0';
  749. i++, prev = c)
  750. {
  751. if (c != SHELL_SEPARATOR)
  752. {
  753. if (prev == SHELL_SEPARATOR)
  754. {
  755. shell->argv[j] = &shell->tmp_line[i];
  756. j++;
  757. if (j == ARRAY_SIZE (shell->argv))
  758. {
  759. shell_printf (shell, "shell: too many arguments\n");
  760. return (EINVAL);
  761. }
  762. shell->argv[j] = NULL;
  763. }
  764. }
  765. else if (prev != SHELL_SEPARATOR)
  766. shell->tmp_line[i] = '\0';
  767. }
  768. shell->argc = j;
  769. return (0);
  770. }
  771. static void
  772. shell_process_line (struct shell *shell)
  773. {
  774. const struct shell_cmd *cmd = NULL;
  775. int error = shell_process_args (shell);
  776. if (error || shell->argc == 0)
  777. goto out;
  778. cmd = shell_cmd_set_lookup (shell->cmd_set, shell->argv[0]);
  779. if (! cmd)
  780. shell_printf (shell, "shell: %s: command not found\n", shell->argv[0]);
  781. out:
  782. shell_history_push (&shell->history);
  783. if (cmd)
  784. cmd->fn (shell, shell->argc, shell->argv);
  785. }
  786. /*
  787. * Process a single control character.
  788. *
  789. * Return an error if the caller should reset the current line state.
  790. */
  791. static int
  792. shell_process_ctrl_char (struct shell *shell, char c)
  793. {
  794. switch (c)
  795. {
  796. case SHELL_ERASE_BS:
  797. case SHELL_ERASE_DEL:
  798. shell_process_backspace (shell);
  799. break;
  800. case '\t':
  801. return (shell_process_tabulation (shell));
  802. case '\n':
  803. case '\r':
  804. shell_printf (shell, "\n");
  805. shell_process_line (shell);
  806. return (EAGAIN);
  807. default:
  808. return (0);
  809. }
  810. return (0);
  811. }
  812. static void
  813. shell_run (struct shell *shell)
  814. {
  815. while (1)
  816. {
  817. shell_reset (shell);
  818. int error, escape = 0;
  819. while (1)
  820. {
  821. int c = stream_getc (shell->stream);
  822. if (escape)
  823. {
  824. switch (escape)
  825. {
  826. case SHELL_ESC_STATE_START:
  827. // XXX CSI and SS3 sequence processing is the same.
  828. if (c == '[' || c == 'O')
  829. escape = SHELL_ESC_STATE_CSI;
  830. else
  831. escape = 0;
  832. break;
  833. case SHELL_ESC_STATE_CSI:
  834. escape = shell_process_esc_sequence (shell, c);
  835. break;
  836. default:
  837. escape = 0;
  838. }
  839. error = 0;
  840. }
  841. else if (shell_is_ctrl_char (c))
  842. {
  843. if (c == '\e')
  844. {
  845. escape = SHELL_ESC_STATE_START;
  846. error = 0;
  847. }
  848. else
  849. {
  850. error = shell_process_ctrl_char (shell, c);
  851. if (error)
  852. break;
  853. }
  854. }
  855. else
  856. error = shell_process_raw_char (shell, c);
  857. if (error)
  858. break;
  859. }
  860. }
  861. }
  862. void
  863. shell_printf (struct shell *shell, const char *format, ...)
  864. {
  865. va_list ap;
  866. va_start (ap, format);
  867. shell_vprintf (shell, format, ap);
  868. va_end (ap);
  869. }
  870. void
  871. shell_vprintf (struct shell *shell, const char *format, va_list ap)
  872. {
  873. fmt_vxprintf (shell->stream, format, ap);
  874. }
  875. static struct shell_cmd_set shell_main_cmd_set;
  876. static struct shell shell_main;
  877. static void
  878. shell_main_run (void *arg)
  879. {
  880. shell_run (arg);
  881. }
  882. static void
  883. shell_start (uintptr_t value __unused, void *arg __unused)
  884. {
  885. struct thread_attr attr;
  886. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "shell");
  887. thread_attr_set_detached (&attr);
  888. int error = thread_create (NULL, &attr, shell_main_run, &shell_main);
  889. error_check (error, "thread_create");
  890. }
  891. static int __init
  892. shell_setup (void)
  893. {
  894. shell_cmd_set_init (&shell_main_cmd_set);
  895. shell_init (&shell_main, &shell_main_cmd_set, console_stream);
  896. bulletin_subscribe (log_get_bulletin (), &shell_log_bulletin_sub,
  897. shell_start, NULL);
  898. return (0);
  899. }
  900. INIT_OP_DEFINE (shell_setup,
  901. INIT_OP_DEP (log_setup, true),
  902. INIT_OP_DEP (mutex_setup, true),
  903. INIT_OP_DEP (printf_setup, true),
  904. INIT_OP_DEP (stream_setup, true));
  905. struct shell_cmd_set* __init
  906. shell_get_main_cmd_set (void)
  907. {
  908. return (&shell_main_cmd_set);
  909. }