shellmatta.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file shellmatta.h
  10. * @brief API definition of the Shellmatta terminal implementation
  11. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  12. */
  13. /**
  14. * @addtogroup shellmatta_api Shellmatta API description
  15. * @{
  16. */
  17. #ifndef _SHELLMATTA_H_
  18. #define _SHELLMATTA_H_
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. /* global defines */
  22. /*
  23. * Define the printf format specifier for all GCC versions > 3.3
  24. * This will let the compiler know that shelmatta_printf() is a function taking printf-like format specifiers.
  25. */
  26. #ifndef SHELLMATTA_ATTR_FORMAT
  27. # if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)))
  28. # define SHELLMATTA_ATTR_FORMAT(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
  29. # else
  30. # define SHELLMATTA_ATTR_FORMAT(fmt, args)
  31. # endif
  32. #else
  33. # define SHELLMATTA_ATTR_FORMAT(fmt, args)
  34. #endif
  35. /**
  36. * @brief definition of a shellmatta handle
  37. */
  38. typedef void* shellmatta_handle_t;
  39. /**
  40. * @brief definition of shellmatta return codes
  41. */
  42. typedef enum
  43. {
  44. SHELLMATTA_OK = 0u, /**< everything is OK */
  45. SHELLMATTA_ERROR , /**< error occured */
  46. SHELLMATTA_CONTINUE , /**< the function is not over */
  47. SHELLMATTA_USE_FAULT , /**< parameter error - wrong usage */
  48. SHELLMATTA_DUPLICATE , /**< duplicate command */
  49. SHELLMATTA_BUSY /**< command is busy keep calling */
  50. } shellmatta_retCode_t;
  51. /**
  52. * @brief definition of shellmatta insert mode
  53. */
  54. typedef enum
  55. {
  56. SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
  57. SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
  58. } shellmatta_mode_t;
  59. /**
  60. * @brief definition of shellmatta optionparser agument type
  61. */
  62. typedef enum
  63. {
  64. SHELLMATTA_OPT_ARG_NONE = 0u, /**< no argument expected */
  65. SHELLMATTA_OPT_ARG_REQUIRED, /**< argument is required */
  66. SHELLMATTA_OPT_ARG_OPTIONAL, /**< argument is optional */
  67. } shellmatta_opt_argtype_t;
  68. /**
  69. * @brief definition of shellmatta optionparser agument type
  70. */
  71. typedef struct
  72. {
  73. const char *paramLong; /**< long parameter string */
  74. const char paramShort; /**< short parameter char */
  75. shellmatta_opt_argtype_t argtype; /**< argument type expected */
  76. } shellmatta_opt_long_t;
  77. /**
  78. * @brief definition of shellmatta optionparser structure
  79. */
  80. typedef struct
  81. {
  82. uint32_t argStart; /**< start of the arguments of the command */
  83. uint32_t offset; /**< current offset of the option parser */
  84. uint32_t nextOffset; /**< offset of the next hunk */
  85. uint32_t len; /**< length of the current hunk */
  86. } shellmatta_opt_t;
  87. /**
  88. * @brief shellmatta command function definition
  89. * @param[in] handle pointer to the instance which is calling the cmd
  90. * @param[in] arguments argument string called to run this command beginning
  91. * with the command itself
  92. * @param[in] length length of the argument string
  93. */
  94. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
  95. const char *arguments,
  96. uint32_t length);
  97. /**
  98. * @brief shellmatta write function definition
  99. * @param[in] data data to be written to the output
  100. * @param[in] length length of the data to be written
  101. */
  102. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  103. /**
  104. * @brief structure of one shellmatta command
  105. */
  106. typedef struct shellmatta_cmd
  107. {
  108. char *cmd; /**< command name */
  109. char *cmdAlias; /**< command alias */
  110. char *helpText; /**< help text to print in "help" command */
  111. char *usageText; /**< usage text - printed on "help cmd" */
  112. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
  113. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  114. } shellmatta_cmd_t;
  115. /**
  116. * @brief structure of one shellmatta instance
  117. */
  118. typedef struct
  119. {
  120. uint32_t magic; /**< magic number to check if initialized */
  121. char *buffer; /**< input buffer */
  122. uint32_t bufferSize; /**< size of the input buffer */
  123. uint32_t inputCount; /**< offset of the current write operation */
  124. uint32_t byteCounter; /**< counter used to loop over input data */
  125. uint32_t lastNewlineIdx; /**< index of the lest newline */
  126. uint32_t cursor; /**< offset where the cursor is at */
  127. uint32_t stdinIdx; /**< start index of stdin in buffer */
  128. uint32_t stdinLength; /**< length of the stdin data */
  129. char *historyBuffer; /**< buffer to store the last commands */
  130. uint32_t historyBufferSize; /**< size of the history buffer */
  131. uint32_t historyStart; /**< index of the oldest stored command */
  132. uint32_t historyEnd; /**< index of the newest stored command */
  133. uint32_t historyRead; /**< index of the current search */
  134. bool historyReadUp; /**< flag to show the last history dir */
  135. uint32_t tabCounter; /**< counts the tabulator key presses */
  136. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  137. char escapeChars[4u]; /**< buffer to save the escape characters */
  138. uint32_t hereStartIdx; /**< heredoc start of "<<" */
  139. uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
  140. uint32_t hereLength; /**< length of the heredoc delimiter */
  141. bool echoEnabled; /**< if true the input is printed */
  142. bool dirty; /**< dirty flag to show changes */
  143. const char *prompt; /**< prompt is printed after every command */
  144. char delimiter; /**< delimiter (return) to terminate a cmd */
  145. shellmatta_mode_t mode; /**< mode of the shell */
  146. shellmatta_write_t write; /**< pointer to write function */
  147. shellmatta_cmd_t helpCmd; /**< help command structure */
  148. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  149. shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
  150. shellmatta_cmd_t *busyCmd; /**< command to be polled (busy mode) */
  151. bool cmdListIsConst; /**< true if the #cmdList was passed during
  152. initialization */
  153. shellmatta_opt_t optionParser; /**< option parser sructure */
  154. } shellmatta_instance_t;
  155. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  156. shellmatta_handle_t *handle,
  157. char *buffer,
  158. uint32_t bufferSize,
  159. char *historyBuffer,
  160. uint32_t historyBufferSize,
  161. const char *prompt,
  162. const shellmatta_cmd_t *cmdList,
  163. shellmatta_write_t writeFct);
  164. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
  165. bool printPrompt);
  166. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  167. shellmatta_cmd_t *cmd);
  168. shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
  169. shellmatta_cmd_t *cmd);
  170. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  171. shellmatta_mode_t mode,
  172. bool echoEnabled,
  173. char delimiter);
  174. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  175. char *data,
  176. uint32_t size);
  177. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  178. char *data,
  179. uint32_t length);
  180. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  181. char **data,
  182. uint32_t *length);
  183. shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
  184. const char *optionString,
  185. char *option,
  186. char **argument,
  187. uint32_t *argLen);
  188. shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
  189. const shellmatta_opt_long_t *longOptions,
  190. char *option,
  191. char **argument,
  192. uint32_t *argLen);
  193. #ifndef SHELLMATTA_STRIP_PRINTF
  194. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  195. const char *fmt,
  196. ...)
  197. SHELLMATTA_ATTR_FORMAT(2, 3);
  198. #endif
  199. #endif
  200. /** @} */