shell.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.h
  15. * @brief Simple CLI shell header.
  16. *
  17. * @addtogroup SHELL
  18. * @{
  19. */
  20. #ifndef SHELL_H
  21. #define SHELL_H
  22. #if defined(SHELL_CONFIG_FILE)
  23. #include "shellconf.h"
  24. #endif
  25. /*===========================================================================*/
  26. /* Module constants. */
  27. /*===========================================================================*/
  28. /**
  29. * @brief Shell History Constants
  30. */
  31. #define SHELL_HIST_DIR_BK 0
  32. #define SHELL_HIST_DIR_FW 1
  33. /*===========================================================================*/
  34. /* Module pre-compile time settings. */
  35. /*===========================================================================*/
  36. /**
  37. * @brief Shell maximum input line length.
  38. */
  39. #if !defined(SHELL_MAX_LINE_LENGTH) || defined(__DOXYGEN__)
  40. #define SHELL_MAX_LINE_LENGTH 64
  41. #endif
  42. /**
  43. * @brief Shell maximum arguments per command.
  44. */
  45. #if !defined(SHELL_MAX_ARGUMENTS) || defined(__DOXYGEN__)
  46. #define SHELL_MAX_ARGUMENTS 4
  47. #endif
  48. /**
  49. * @brief Shell maximum command history.
  50. */
  51. #if !defined(SHELL_MAX_HIST_BUFF) || defined(__DOXYGEN__)
  52. #define SHELL_MAX_HIST_BUFF 8 * SHELL_MAX_LINE_LENGTH
  53. #endif
  54. /**
  55. * @brief Enable shell command history
  56. */
  57. #if !defined(SHELL_USE_HISTORY) || defined(__DOXYGEN__)
  58. #define SHELL_USE_HISTORY FALSE
  59. #endif
  60. /**
  61. * @brief Enable shell command completion
  62. */
  63. #if !defined(SHELL_USE_COMPLETION) || defined(__DOXYGEN__)
  64. #define SHELL_USE_COMPLETION FALSE
  65. #endif
  66. /**
  67. * @brief Shell Maximum Completions (Set to max commands with common prefix)
  68. */
  69. #if !defined(SHELL_MAX_COMPLETIONS) || defined(__DOXYGEN__)
  70. #define SHELL_MAX_COMPLETIONS 8
  71. #endif
  72. /**
  73. * @brief Enable shell escape sequence processing
  74. */
  75. #if !defined(SHELL_USE_ESC_SEQ) || defined(__DOXYGEN__)
  76. #define SHELL_USE_ESC_SEQ FALSE
  77. #endif
  78. /**
  79. * @brief Prompt string
  80. */
  81. #if !defined(SHELL_PROMPT_STR) || defined(__DOXYGEN__)
  82. #define SHELL_PROMPT_STR "ch> "
  83. #endif
  84. /**
  85. * @brief Newline string
  86. */
  87. #if !defined(SHELL_NEWLINE_STR) || defined(__DOXYGEN__)
  88. #define SHELL_NEWLINE_STR "\r\n"
  89. #endif
  90. /*===========================================================================*/
  91. /* Derived constants and error checks. */
  92. /*===========================================================================*/
  93. /*===========================================================================*/
  94. /* Module data structures and types. */
  95. /*===========================================================================*/
  96. /**
  97. * @brief Command handler function type.
  98. */
  99. typedef void (*shellcmd_t)(BaseSequentialStream *chp, int argc, char *argv[]);
  100. /**
  101. * @brief Custom command entry type.
  102. */
  103. typedef struct {
  104. const char *sc_name; /**< @brief Command name. */
  105. shellcmd_t sc_function; /**< @brief Command function. */
  106. } ShellCommand;
  107. /**
  108. * @brief Shell history type.
  109. */
  110. typedef struct {
  111. char *sh_buffer; /**< @brief Buffer to store command
  112. history. */
  113. const int sh_size; /**< @brief Shell history buffer
  114. size. */
  115. int sh_beg; /**< @brief Beginning command index
  116. in buffer. */
  117. int sh_end; /**< @brief Ending command index
  118. in buffer. */
  119. int sh_cur; /**< @brief Currently selected
  120. command in buffer. */
  121. } ShellHistory;
  122. /**
  123. * @brief Shell descriptor type.
  124. */
  125. typedef struct {
  126. BaseSequentialStream *sc_channel; /**< @brief I/O channel associated
  127. to the shell. */
  128. const ShellCommand *sc_commands; /**< @brief Shell extra commands
  129. table. */
  130. #if (SHELL_USE_HISTORY == TRUE) || defined(__DOXYGEN__)
  131. char *sc_histbuf; /**< @brief Shell command history
  132. buffer. */
  133. const int sc_histsize; /**< @brief Shell history buffer
  134. size. */
  135. #endif
  136. #if (SHELL_USE_COMPLETION == TRUE) || defined(__DOXYGEN__)
  137. char **sc_completion; /**< @brief Shell command completion
  138. buffer. */
  139. #endif
  140. } ShellConfig;
  141. /*===========================================================================*/
  142. /* Module macros. */
  143. /*===========================================================================*/
  144. /**
  145. * @brief Send escape codes to move cursor to the beginning of the line
  146. *
  147. * @param[in] stream pointer to a @p BaseSequentialStream object
  148. *
  149. * @notapi
  150. */
  151. #define _shell_reset_cur(stream) chprintf(stream, "\033[%dD\033[%dC", \
  152. SHELL_MAX_LINE_LENGTH + \
  153. strlen(SHELL_PROMPT_STR) + 2, \
  154. strlen(SHELL_PROMPT_STR))
  155. /**
  156. * @brief Send escape codes to clear the rest of the line
  157. *
  158. * @param[in] stream pointer to a @p BaseSequentialStream object
  159. *
  160. * @notapi
  161. */
  162. #define _shell_clr_line(stream) chprintf(stream, "\033[K")
  163. /**
  164. * @brief Prints out usage message
  165. *
  166. * @param[in] stream pointer to a @p BaseSequentialStream object
  167. * @param[in] message pointer to message string
  168. *
  169. * @api
  170. */
  171. #define shellUsage(stream, message) \
  172. chprintf(stream, "Usage: %s" SHELL_NEWLINE_STR, message)
  173. /*===========================================================================*/
  174. /* External declarations. */
  175. /*===========================================================================*/
  176. #if !defined(__DOXYGEN__)
  177. extern event_source_t shell_terminated;
  178. #endif
  179. #ifdef __cplusplus
  180. extern "C" {
  181. #endif
  182. void shellInit(void);
  183. THD_FUNCTION(shellThread, p);
  184. void shellExit(msg_t msg);
  185. bool shellGetLine(ShellConfig *scfg, char *line,
  186. unsigned size, ShellHistory *shp);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. /*===========================================================================*/
  191. /* Module inline functions. */
  192. /*===========================================================================*/
  193. #endif /* SHELL_H */
  194. /** @} */