shell.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. *
  21. * Minimalist shell for embedded systems.
  22. */
  23. #ifndef KERN_SHELL_H
  24. #define KERN_SHELL_H
  25. #include <stdarg.h>
  26. #include <stddef.h>
  27. #include <kern/error.h>
  28. #include <kern/init.h>
  29. #include <kern/macros.h>
  30. #include <kern/mutex.h>
  31. #include <kern/stream.h>
  32. struct shell;
  33. // Type for command implementation callbacks.
  34. typedef void (*shell_fn_t) (struct shell *shell, int argc, char **argv);
  35. // Shell command structure.
  36. struct shell_cmd
  37. {
  38. struct shell_cmd *ht_next;
  39. struct shell_cmd *ls_next;
  40. const char *name;
  41. shell_fn_t fn;
  42. const char *usage;
  43. const char *short_desc;
  44. const char *long_desc;
  45. };
  46. struct shell_bucket
  47. {
  48. struct shell_cmd *cmd;
  49. };
  50. // Binary exponent and size of the hash table used to store commands.
  51. #define SHELL_HTABLE_BITS 6
  52. #define SHELL_HTABLE_SIZE (1 << SHELL_HTABLE_BITS)
  53. // Command container, shareable across multiple shell instances
  54. struct shell_cmd_set
  55. {
  56. struct mutex lock;
  57. struct shell_bucket htable[SHELL_HTABLE_SIZE];
  58. struct shell_cmd *cmd_list;
  59. };
  60. #define SHELL_LINE_MAX_SIZE 64
  61. /*
  62. * Line containing a shell entry.
  63. *
  64. * The string must be nul-terminated. The size doesn't include this
  65. * additional nul character, the same way strlen() doesn't account for it.
  66. */
  67. struct shell_line
  68. {
  69. char str[SHELL_LINE_MAX_SIZE];
  70. size_t size;
  71. };
  72. /*
  73. * Number of entries in the history.
  74. *
  75. * One of these entryes is used as the current line.
  76. */
  77. #define SHELL_HISTORY_SIZE 21
  78. #if SHELL_HISTORY_SIZE == 0
  79. #error "shell history size must be non-zero"
  80. #endif
  81. /*
  82. * Shell history.
  83. *
  84. * The history is never empty. There is always at least one entry, the
  85. * current line, referenced by the newest (most recent) index. The array
  86. * is used like a circular buffer, i.e. old entries are implicitely
  87. * erased by new ones. The index references the entry used as a template
  88. * for the current line.
  89. */
  90. struct shell_history
  91. {
  92. struct shell_line lines[SHELL_HISTORY_SIZE];
  93. size_t newest;
  94. size_t oldest;
  95. size_t index;
  96. };
  97. // This value changes depending on the standard used and was chosen arbitrarily.
  98. #define SHELL_ESC_SEQ_MAX_SIZE 8
  99. #define SHELL_MAX_ARGS 16
  100. /*
  101. * Shell structure.
  102. *
  103. * A shell instance can include temporary variables to minimize stack usage.
  104. */
  105. struct shell
  106. {
  107. struct shell_cmd_set *cmd_set;
  108. struct stream *stream;
  109. struct shell_history history;
  110. // Cursor within the current line.
  111. size_t cursor;
  112. // Members used for escape sequence parsing.
  113. char esc_seq[SHELL_ESC_SEQ_MAX_SIZE];
  114. size_t esc_seq_index;
  115. /*
  116. * Buffer used to store the current line during argument processing.
  117. *
  118. * The pointers in the argv array point inside this buffer. The
  119. * separators immediately following the arguments are replaced with
  120. * null characters.
  121. */
  122. char tmp_line[SHELL_LINE_MAX_SIZE];
  123. int argc;
  124. char *argv[SHELL_MAX_ARGS];
  125. };
  126. #define SHELL_REGISTER_CMDS(cmds, cmd_set) \
  127. MACRO_BEGIN \
  128. \
  129. for (size_t i_ = 0; i_ < ARRAY_SIZE (cmds); i_++) \
  130. { \
  131. int error_ = shell_cmd_set_register (cmd_set, &(cmds)[i_]); \
  132. error_check (error_, __func__); \
  133. } \
  134. MACRO_END
  135. // Static shell command initializers.
  136. #define SHELL_CMD_INITIALIZER(name, fn, usage, short_desc) \
  137. { NULL, NULL, name, fn, usage, short_desc, NULL }
  138. #define SHELL_CMD_INITIALIZER2(name, fn, usage, short_desc, long_desc) \
  139. { NULL, NULL, name, fn, usage, short_desc, long_desc }
  140. // Initialize a shell command structure.
  141. void shell_cmd_init (struct shell_cmd *cmd, const char *name,
  142. shell_fn_t fn, const char *usage,
  143. const char *short_desc, const char *long_desc);
  144. // Initialize a command set.
  145. void shell_cmd_set_init (struct shell_cmd_set *cmd_set);
  146. /*
  147. * Register a shell command.
  148. *
  149. * The command name must be unique. It must not include characters outside
  150. * the [a-zA-Z0-9-_] class.
  151. *
  152. * Commands may safely be registered while the command set is used.
  153. *
  154. * The command structure must persist in memory as long as the command set
  155. * is used.
  156. */
  157. int shell_cmd_set_register (struct shell_cmd_set *cmd_set,
  158. struct shell_cmd *cmd);
  159. /*
  160. * Initialize a shell instance.
  161. *
  162. * On return, shell commands can be registered.
  163. */
  164. void shell_init (struct shell *shell, struct shell_cmd_set *cmd_set,
  165. struct stream *stream);
  166. // Obtain the command set associated with a shell.
  167. struct shell_cmd_set* shell_get_cmd_set (struct shell *shell);
  168. // Printf-like functions specific to the given shell instance.
  169. void shell_printf (struct shell *shell, const char *format, ...)
  170. __attribute__ ((format (printf, 2, 3)));
  171. void shell_vprintf (struct shell *shell, const char *format, va_list ap)
  172. __attribute__ ((format (printf, 2, 0)));
  173. /*
  174. * This init operation provides :
  175. * - main shell command registration
  176. */
  177. INIT_OP_DECLARE (shell_setup);
  178. struct shell_cmd_set* shell_get_main_cmd_set (void);
  179. #endif