shellmatta.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2019 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
  15. * @{
  16. */
  17. #ifndef _SHELLMATTA_H_
  18. #define _SHELLMATTA_H_
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. /* global defines */
  22. /**
  23. * @brief definition of a shellmatta handle
  24. */
  25. typedef void* shellmatta_handle_t;
  26. /**
  27. * @brief definition of shellmatta return codes
  28. */
  29. typedef enum
  30. {
  31. SHELLMATTA_OK = 0u, /**< everything is OK */
  32. SHELLMATTA_ERROR , /**< error occured */
  33. SHELLMATTA_CONTINUE , /**< the function is not over */
  34. SHELLMATTA_USE_FAULT , /**< parameter error - wrong usage */
  35. SHELLMATTA_DUPLICATE /**< duplicate command */
  36. } shellmatta_retCode_t;
  37. /**
  38. * @brief definition of shellmatta insert mode
  39. */
  40. typedef enum
  41. {
  42. SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
  43. SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
  44. } shellmatta_mode_t;
  45. /**
  46. * @brief definition of shellmatta optionparser agument type
  47. */
  48. typedef enum
  49. {
  50. SHELLMATTA_OPT_ARG_NONE = 0u, /**< no argument expected */
  51. SHELLMATTA_OPT_ARG_REQUIRED, /**< argument is required */
  52. SHELLMATTA_OPT_ARG_OPTIONAL, /**< argument is optional */
  53. } shellmatta_opt_argtype_t;
  54. /**
  55. * @brief definition of shellmatta optionparser agument type
  56. */
  57. typedef struct
  58. {
  59. const char *paramLong; /**< long parameter string */
  60. const char paramShort; /**< short parameter char */
  61. shellmatta_opt_argtype_t argtype; /**< argument type expected */
  62. } shellmatta_opt_long_t;
  63. /**
  64. * @brief definition of shellmatta optionparser structure
  65. */
  66. typedef struct
  67. {
  68. uint32_t offset; /**< current offset of the option parser */
  69. uint32_t nextOffset; /**< offset of the next hunk */
  70. uint32_t len; /**< length of the current hunk */
  71. } shellmatta_opt_t;
  72. /**
  73. * @brief shellmatta command function definition
  74. * @param[in] handle pointer to the instance which is calling the cmd
  75. * @param[in] arguments argument string called to run this command beginning
  76. * with the command itself
  77. * @param[in] length length of the argument string
  78. */
  79. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
  80. const char *arguments,
  81. uint32_t length);
  82. /**
  83. * @brief shellmatta write function definition
  84. * @param[in] data data to be written to the output
  85. * @param[in] length length of the data to be written
  86. */
  87. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  88. /**
  89. * @brief structure of one shellmatta command
  90. */
  91. typedef struct shellmatta_cmd
  92. {
  93. char *cmd; /**< command name */
  94. char *cmdAlias; /**< command alias */
  95. char *helpText; /**< help text to print in "help" command */
  96. char *usageText; /**< usage text to print on parameter error */
  97. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
  98. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  99. } shellmatta_cmd_t;
  100. /**
  101. * @brief structure of one shellmatta instance
  102. */
  103. typedef struct
  104. {
  105. uint32_t magic; /**< magic number to check if initialized */
  106. char *buffer; /**< input buffer */
  107. uint32_t bufferSize; /**< size of the input buffer */
  108. uint32_t inputCount; /**< offset of the current write operation */
  109. uint32_t lastNewlineIdx; /**< index of the lest newline */
  110. uint32_t cursor; /**< offset where the cursor is at */
  111. uint32_t stdinIdx; /**< start index of stdin in buffer */
  112. uint32_t stdinLength; /**< length of the stdin data */
  113. char *historyBuffer; /**< buffer to store the last commands */
  114. uint32_t historyBufferSize; /**< size of the history buffer */
  115. uint32_t historyStart; /**< index of the oldest stored command */
  116. uint32_t historyEnd; /**< index of the newest stored command */
  117. uint32_t historyRead; /**< index of the current search */
  118. bool historyReadUp; /**< flag to show the last history dir */
  119. uint32_t tabCounter; /**< counts the tabulator key presses */
  120. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  121. char escapeChars[4u]; /**< buffer to save the escape characters */
  122. uint32_t hereStartIdx; /**< heredoc start of "<<" */
  123. uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
  124. uint32_t hereLength; /**< length of the heredoc delimiter */
  125. bool echoEnabled; /**< if true the input is printed */
  126. bool dirty; /**< dirty flag to show changes */
  127. const char *prompt; /**< prompt is printed after every command */
  128. shellmatta_mode_t mode; /**< mode of the shell */
  129. shellmatta_write_t write; /**< pointer to write function */
  130. shellmatta_cmd_t helpCmd; /**< help command structure */
  131. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  132. shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
  133. bool cmdListIsConst; /**< true if the #cmdList was passed during
  134. initialization */
  135. shellmatta_opt_t optionParser; /**< option parser sructure */
  136. } shellmatta_instance_t;
  137. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  138. shellmatta_handle_t *handle,
  139. char *buffer,
  140. uint32_t bufferSize,
  141. char *historyBuffer,
  142. uint32_t historyBufferSize,
  143. const char *prompt,
  144. const shellmatta_cmd_t *cmdList,
  145. shellmatta_write_t writeFct);
  146. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
  147. bool printPrompt);
  148. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  149. shellmatta_cmd_t *cmd);
  150. shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
  151. shellmatta_cmd_t *cmd);
  152. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  153. shellmatta_mode_t mode,
  154. bool echoEnabled);
  155. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  156. char *data,
  157. uint32_t size);
  158. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  159. char *data,
  160. uint32_t length);
  161. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  162. char **data,
  163. uint32_t *length);
  164. shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
  165. char *optionString,
  166. char *option,
  167. char **argument,
  168. uint32_t *argLen);
  169. shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
  170. shellmatta_opt_long_t *longOptions,
  171. char *option,
  172. char **argument,
  173. uint32_t *argLen);
  174. #ifndef SHELLMATTA_STRIP_PRINTF
  175. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  176. const char *fmt,
  177. ...);
  178. #endif
  179. #endif
  180. /** @} */