shellmatta.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. * @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_BUSY /**< command is busy keep calling */
  37. } shellmatta_retCode_t;
  38. /**
  39. * @brief definition of shellmatta insert mode
  40. */
  41. typedef enum
  42. {
  43. SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
  44. SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
  45. } shellmatta_mode_t;
  46. /**
  47. * @brief definition of shellmatta optionparser agument type
  48. */
  49. typedef enum
  50. {
  51. SHELLMATTA_OPT_ARG_NONE = 0u, /**< no argument expected */
  52. SHELLMATTA_OPT_ARG_REQUIRED, /**< argument is required */
  53. SHELLMATTA_OPT_ARG_OPTIONAL, /**< argument is optional */
  54. } shellmatta_opt_argtype_t;
  55. /**
  56. * @brief definition of shellmatta optionparser agument type
  57. */
  58. typedef struct
  59. {
  60. const char *paramLong; /**< long parameter string */
  61. const char paramShort; /**< short parameter char */
  62. shellmatta_opt_argtype_t argtype; /**< argument type expected */
  63. } shellmatta_opt_long_t;
  64. /**
  65. * @brief definition of shellmatta optionparser structure
  66. */
  67. typedef struct
  68. {
  69. uint32_t argStart; /**< start of the arguments of the command */
  70. uint32_t offset; /**< current offset of the option parser */
  71. uint32_t nextOffset; /**< offset of the next hunk */
  72. uint32_t len; /**< length of the current hunk */
  73. } shellmatta_opt_t;
  74. /**
  75. * @brief shellmatta command function definition
  76. * @param[in] handle pointer to the instance which is calling the cmd
  77. * @param[in] arguments argument string called to run this command beginning
  78. * with the command itself
  79. * @param[in] length length of the argument string
  80. */
  81. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
  82. const char *arguments,
  83. uint32_t length);
  84. /**
  85. * @brief shellmatta write function definition
  86. * @param[in] data data to be written to the output
  87. * @param[in] length length of the data to be written
  88. */
  89. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  90. #ifdef SHELLMATTA_AUTHENTICATION
  91. /**
  92. * @brief user role matrix
  93. */
  94. typedef struct
  95. {
  96. uint32_t userId; /**< id of the user (!= 0) */
  97. const char *username; /**< name of the user role */
  98. const char *password; /**< password of the user role or NULL (custom auth) */
  99. } shellmatta_auth_user_t;
  100. /**
  101. * @brief typedefinition of one line of the authentication table
  102. */
  103. typedef struct
  104. {
  105. const char* cmd; /**< command to grand access to */
  106. const uint32_t *userIds; /**< list of user ids with access to the cmd */
  107. const uint32_t userIdslength; /**< length of the user list */
  108. } shellmatta_auth_perm_t;
  109. /**
  110. * @brief custom shellmatta authentication method
  111. * @param[in] userId user id to log in (name of the user role)
  112. * @param[in] password password for the login
  113. * @return #SHELLMATTA_OK if the username and password is correct
  114. */
  115. typedef shellmatta_retCode_t (*shellmatta_auth_check_t)(const uint32_t userId, const char* password);
  116. /**
  117. * @brief login states
  118. */
  119. typedef enum
  120. {
  121. SHELLMATTA_AUTH_IDLE, /**< authentication system waits */
  122. SHELLMATTA_AUTH_USERNAME, /**< input of username */
  123. SHELLMATTA_AUTH_PASSWORD /**< input of password */
  124. } shellmatta_auth_state_t;
  125. #endif
  126. /**
  127. * @brief structure of one shellmatta command
  128. */
  129. typedef struct shellmatta_cmd
  130. {
  131. char *cmd; /**< command name */
  132. char *cmdAlias; /**< command alias */
  133. char *helpText; /**< help text to print in "help" command */
  134. char *usageText; /**< usage text - printed on "help cmd" */
  135. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
  136. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  137. #ifdef SHELLMATTA_AUTHENTICATION
  138. shellmatta_auth_perm_t *authLink; /**< internally used - pointer to perm list */
  139. #endif
  140. } shellmatta_cmd_t;
  141. /**
  142. * @brief structure of one shellmatta instance
  143. */
  144. typedef struct
  145. {
  146. uint32_t magic; /**< magic number to check if initialized */
  147. char *buffer; /**< input buffer */
  148. uint32_t bufferSize; /**< size of the input buffer */
  149. uint32_t inputCount; /**< offset of the current write operation */
  150. uint32_t byteCounter; /**< counter used to loop over input data */
  151. uint32_t lastNewlineIdx; /**< index of the lest newline */
  152. uint32_t cursor; /**< offset where the cursor is at */
  153. uint32_t stdinIdx; /**< start index of stdin in buffer */
  154. uint32_t stdinLength; /**< length of the stdin data */
  155. char *historyBuffer; /**< buffer to store the last commands */
  156. uint32_t historyBufferSize; /**< size of the history buffer */
  157. uint32_t historyStart; /**< index of the oldest stored command */
  158. uint32_t historyEnd; /**< index of the newest stored command */
  159. uint32_t historyRead; /**< index of the current search */
  160. bool historyReadUp; /**< flag to show the last history dir */
  161. uint32_t tabCounter; /**< counts the tabulator key presses */
  162. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  163. char escapeChars[4u]; /**< buffer to save the escape characters */
  164. uint32_t hereStartIdx; /**< heredoc start of "<<" */
  165. uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
  166. uint32_t hereLength; /**< length of the heredoc delimiter */
  167. bool echoEnabled; /**< if true the input is printed */
  168. bool dirty; /**< dirty flag to show changes */
  169. const char *prompt; /**< prompt is printed after every command */
  170. char delimiter; /**< delimiter (return) to terminate a cmd */
  171. shellmatta_mode_t mode; /**< mode of the shell */
  172. shellmatta_write_t write; /**< pointer to write function */
  173. shellmatta_cmd_t helpCmd; /**< help command structure */
  174. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  175. shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
  176. shellmatta_cmd_t *busyCmd; /**< command to be polled (busy mode) */
  177. bool cmdListIsConst; /**< true if the #cmdList was passed during
  178. initialization */
  179. shellmatta_opt_t optionParser; /**< option parser sructure */
  180. #ifdef SHELLMATTA_AUTHENTICATION
  181. shellmatta_auth_state_t loginState; /**< state variable of the login cmd */
  182. shellmatta_cmd_t loginCmd; /**< login command structure */
  183. shellmatta_cmd_t logoutCmd; /**< logout command structure */
  184. uint32_t userId; /**< user ID of the current session */
  185. uint32_t tmpUserId; /**< remporary user ID during input */
  186. shellmatta_auth_user_t *userPointer; /**< pointer to the user in the user list */
  187. shellmatta_auth_user_t *userList; /**< user list */
  188. uint32_t userListLength; /**< length of the user list */
  189. shellmatta_auth_perm_t *permList; /**< permission list */
  190. uint32_t permListLength; /**< length of the permission list */
  191. shellmatta_auth_check_t checkFct; /**< custom credential check function */
  192. #endif
  193. } shellmatta_instance_t;
  194. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  195. shellmatta_handle_t *handle,
  196. char *buffer,
  197. uint32_t bufferSize,
  198. char *historyBuffer,
  199. uint32_t historyBufferSize,
  200. const char *prompt,
  201. const shellmatta_cmd_t *cmdList,
  202. shellmatta_write_t writeFct);
  203. shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
  204. bool printPrompt);
  205. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  206. shellmatta_cmd_t *cmd);
  207. shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
  208. shellmatta_cmd_t *cmd);
  209. shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
  210. shellmatta_mode_t mode,
  211. bool echoEnabled,
  212. char delimiter);
  213. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  214. char *data,
  215. uint32_t size);
  216. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  217. char *data,
  218. uint32_t length);
  219. shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
  220. char **data,
  221. uint32_t *length);
  222. shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
  223. const char *optionString,
  224. char *option,
  225. char **argument,
  226. uint32_t *argLen);
  227. shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
  228. const shellmatta_opt_long_t *longOptions,
  229. char *option,
  230. char **argument,
  231. uint32_t *argLen);
  232. #ifndef SHELLMATTA_STRIP_PRINTF
  233. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  234. const char *fmt,
  235. ...);
  236. #endif
  237. #ifdef SHELLMATTA_AUTHENTICATION
  238. shellmatta_retCode_t shellmatta_auth_init( shellmatta_handle_t handle,
  239. shellmatta_auth_user_t *userList,
  240. uint32_t userListLength,
  241. shellmatta_auth_perm_t *permList,
  242. uint32_t permListLength,
  243. bool customLogin,
  244. shellmatta_auth_check_t checkFct);
  245. shellmatta_retCode_t shellmatta_auth_login( shellmatta_handle_t handle,
  246. uint32_t userId);
  247. shellmatta_retCode_t shellmatta_auth_logout( shellmatta_handle_t handle);
  248. uint32_t shellmatta_auth_getLoggedInUserId( shellmatta_handle_t handle);
  249. shellmatta_retCode_t shellmatta_auth_getLoggedInUserName( shellmatta_handle_t handle,
  250. char **data,
  251. uint32_t *length);
  252. #endif
  253. #endif
  254. /** @} */