shellmatta.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 shellmatta command function definition
  47. * @param[in] handle pointer to the instance which is calling the cmd
  48. * @param[in] arguments argument string called to run this command beginning
  49. * with the command itself
  50. * @param[in] length length of the argument string
  51. */
  52. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
  53. const char *arguments,
  54. uint32_t length);
  55. /**
  56. * @brief shellmatta write function definition
  57. * @param[in] data data to be written to the output
  58. * @param[in] length length of the data to be written
  59. */
  60. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  61. /**
  62. * @brief structure of one shellmatta command
  63. */
  64. typedef struct shellmatta_cmd
  65. {
  66. char *cmd; /**< command name */
  67. char *cmdAlias; /**< command alias */
  68. char *helpText; /**< help text to print in "help" command */
  69. char *usageText; /**< usage text to print on parameter error */
  70. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
  71. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  72. } shellmatta_cmd_t;
  73. /**
  74. * @brief structure of one shellmatta instance
  75. */
  76. typedef struct
  77. {
  78. uint32_t magic; /**< magic number to check if initialized */
  79. char *buffer; /**< input buffer */
  80. uint32_t bufferSize; /**< size of the input buffer */
  81. uint32_t inputCount; /**< offset of the current write operation */
  82. uint32_t cursor; /**< offset where the cursor is at */
  83. char *historyBuffer; /**< buffer to store the last commands */
  84. uint32_t historyBufferSize; /**< size of the history buffer */
  85. uint32_t historyStart; /**< index of the oldest stored command */
  86. uint32_t historyEnd; /**< index of the newest stored command */
  87. uint32_t historyRead; /**< index of the current search */
  88. bool historyReadUp; /**< flag to show the last history dir */
  89. uint32_t tabCounter; /**< counts the tabulator key presses */
  90. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  91. char escapeChars[4]; /**< buffer to save the escape characters */
  92. bool echoEnabled; /**< if true the input is printed */
  93. bool dirty; /**< dirty flag to show changes */
  94. const char *prompt; /**< prompt is printed after every command */
  95. shellmatta_mode_t mode; /**< mode of the shell */
  96. shellmatta_write_t write; /**< pointer to write function */
  97. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  98. bool cmdListIsConst; /**< true if the #cmdList was passed during
  99. initialization */
  100. } shellmatta_instance_t;
  101. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  102. shellmatta_handle_t *handle,
  103. char *buffer,
  104. uint32_t bufferSize,
  105. char *historyBuffer,
  106. uint32_t historyBufferSize,
  107. const char *prompt,
  108. const shellmatta_cmd_t *cmdList,
  109. shellmatta_write_t writeFct);
  110. shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
  111. shellmatta_cmd_t *cmd);
  112. shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
  113. char *data,
  114. uint32_t size);
  115. shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
  116. char *data,
  117. uint32_t length);
  118. #ifndef SHELLMATTA_STRIP_PRINTF
  119. shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
  120. const char *fmt,
  121. ...);
  122. #endif
  123. #endif
  124. /** @} */