shellmatta.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /**
  25. * @file shellmatta.h
  26. * @brief API definition of the Shellmatta terminal implementation
  27. * @author Stefan Strobel <stefan.strobel@shimatta.net>
  28. */
  29. /**
  30. * @addtogroup shellmatta
  31. * @{
  32. */
  33. #ifndef _SHELLMATTA_H_
  34. #define _SHELLMATTA_H_
  35. #include <stdint.h>
  36. #include <stdbool.h>
  37. /**
  38. * @brief definition of shellmatta return codes
  39. */
  40. typedef enum
  41. {
  42. SHELLMATTA_OK = 0u, /**< everything is OK */
  43. SHELLMATTA_ERROR , /**< error occured */
  44. SHELLMATTA_CONTINUE , /**< the function is not over */
  45. SHELLMATTA_USE_FAULT , /**< parameter error - wrong usage */
  46. SHELLMATTA_DUPLICATE /**< duplicate command */
  47. } shellmatta_retCode_t;
  48. /**
  49. * @brief definition of shellmatta insert mode
  50. */
  51. typedef enum
  52. {
  53. SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
  54. SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
  55. } shellmatta_mode_t;
  56. /**
  57. * @brief shellmatta command function definition
  58. */
  59. typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(int argc, char *argv[]);
  60. /**
  61. * @brief shellmatta write function definition
  62. */
  63. typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
  64. /**
  65. * @brief structure of one shellmatta command
  66. */
  67. typedef struct shellmatta_cmd
  68. {
  69. char *cmd; /**< command name */
  70. char *cmdAlias; /**< command alias */
  71. char *helpText; /**< help text to print in "help" command */
  72. char *usageText; /**< usage text to print on parameter error */
  73. shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
  74. struct shellmatta_cmd *next; /**< pointer to next command or NULL */
  75. } shellmatta_cmd_t;
  76. /**
  77. * @brief structure of one shellmatta instance
  78. */
  79. typedef struct
  80. {
  81. uint8_t *buffer; /**< input buffer */
  82. uint32_t bufferSize; /**< size of the input buffer */
  83. uint32_t inputCount; /**< offset of the current write operation */
  84. uint32_t cursor; /**< offset where the cursor is at */
  85. uint8_t *historyBuffer; /**< buffer to store the last commands */
  86. uint32_t historyBufferSize; /**< size of the history buffer */
  87. uint32_t historyStart; /**< index of the oldest stored command */
  88. uint32_t historyEnd; /**< index of the newest stored command */
  89. uint32_t historyRead; /**< index of the current search */
  90. bool historyReadUp; /**< flag to show the last history dir */
  91. uint32_t tabCounter; /**< counts the tabulator key presses */
  92. uint32_t escapeCounter; /**< counts the characters of an escape seq */
  93. uint8_t escapeChars[4]; /**< buffer to save the escape characters */
  94. bool echoEnabled; /**< if true the input is printed */
  95. bool dirty; /**< dirty flag to show changes */
  96. const char *prompt; /**< prompt is printed after every command */
  97. shellmatta_mode_t mode; /**< mode of the shell */
  98. shellmatta_write_t write; /**< pointer to write function */
  99. shellmatta_cmd_t *cmdList; /**< pointer to the first command */
  100. } shellmatta_instance_t;
  101. shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
  102. uint8_t *buffer,
  103. uint32_t bufferSize,
  104. uint8_t *historyBuffer,
  105. uint32_t historyBufferSize,
  106. const char *prompt,
  107. shellmatta_write_t writeFct);
  108. shellmatta_retCode_t shellmatta_addCmd(shellmatta_instance_t *inst, shellmatta_cmd_t *cmd);
  109. void shellmatta_doTask(shellmatta_instance_t *inst, uint32_t time);
  110. void shellmatta_processData(shellmatta_instance_t *inst, char *data, uint32_t size);
  111. void shellmatta_printf(shellmatta_instance_t *inst, const char *fmt, ...);
  112. void shellmatta_getArg(uint32_t cnt, uint8_t *arg);
  113. #endif
  114. /** @} */