shell.h 5.6 KB

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