123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- #ifndef _SHELLMATTA_H_
- #define _SHELLMATTA_H_
- #include <stdint.h>
- #include <stdbool.h>
- #ifndef SHELLMATTA_ATTR_FORMAT
- # if ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 3)))
- # define SHELLMATTA_ATTR_FORMAT(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
- # else
- # define SHELLMATTA_ATTR_FORMAT(fmt, args)
- # endif
- #else
- # define SHELLMATTA_ATTR_FORMAT(fmt, args)
- #endif
- typedef void* shellmatta_handle_t;
- typedef enum
- {
- SHELLMATTA_OK = 0u,
- SHELLMATTA_ERROR ,
- SHELLMATTA_CONTINUE ,
- SHELLMATTA_USE_FAULT ,
- SHELLMATTA_DUPLICATE ,
- SHELLMATTA_BUSY
- } shellmatta_retCode_t;
- typedef enum
- {
- SHELLMATTA_MODE_INSERT = 0u,
- SHELLMATTA_MODE_OVERWRITE ,
- } shellmatta_mode_t;
- typedef enum
- {
- SHELLMATTA_OPT_ARG_NONE = 0u,
- SHELLMATTA_OPT_ARG_REQUIRED,
- SHELLMATTA_OPT_ARG_OPTIONAL,
- } shellmatta_opt_argtype_t;
- typedef struct
- {
- const char *paramLong;
- const char paramShort;
- shellmatta_opt_argtype_t argtype;
- } shellmatta_opt_long_t;
- typedef struct
- {
- uint32_t argStart;
- uint32_t offset;
- uint32_t nextOffset;
- uint32_t len;
- } shellmatta_opt_t;
- typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
- const char *arguments,
- uint32_t length);
- typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
- #ifdef SHELLMATTA_AUTHENTICATION
- typedef struct
- {
- uint32_t userId;
- bool superuser;
- const char *username;
- const char *password;
- } shellmatta_auth_user_t;
- typedef struct
- {
- const char* cmd;
- const uint32_t *userIds;
- const uint32_t userIdslength;
- } shellmatta_auth_perm_t;
- typedef shellmatta_retCode_t (*shellmatta_auth_check_t)(const uint32_t userId, const char* password);
- typedef void (*shellmatta_auth_log_t)(const uint32_t userId, bool success);
- typedef enum
- {
- SHELLMATTA_AUTH_IDLE,
- SHELLMATTA_AUTH_USERNAME,
- SHELLMATTA_AUTH_PASSWORD
- } shellmatta_auth_state_t;
- #endif
- typedef struct shellmatta_cmd
- {
- char *cmd;
- char *cmdAlias;
- char *helpText;
- char *usageText;
- shellmatta_cmdFct_t cmdFct;
- #ifdef SHELLMATTA_AUTHENTICATION
- shellmatta_auth_perm_t *authLink;
- #endif
- struct shellmatta_cmd *next;
- } shellmatta_cmd_t;
- typedef struct
- {
- uint32_t magic;
- char *buffer;
- uint32_t bufferSize;
- uint32_t inputCount;
- uint32_t byteCounter;
- uint32_t lastNewlineIdx;
- uint32_t cursor;
- uint32_t stdinIdx;
- uint32_t stdinLength;
- char *historyBuffer;
- uint32_t historyBufferSize;
- uint32_t historyStart;
- uint32_t historyEnd;
- uint32_t historyRead;
- bool historyReadUp;
- uint32_t tabCounter;
- uint32_t escapeCounter;
- char escapeChars[4u];
- uint32_t hereStartIdx;
- uint32_t hereDelimiterIdx;
- uint32_t hereLength;
- bool echoEnabled;
- bool dirty;
- const char *prompt;
- char delimiter;
- shellmatta_mode_t mode;
- shellmatta_write_t write;
- shellmatta_cmd_t helpCmd;
- shellmatta_cmd_t *cmdList;
- shellmatta_cmd_t *continuousCmd;
- shellmatta_cmd_t *busyCmd;
- bool cmdListIsConst;
- shellmatta_opt_t optionParser;
- #ifdef SHELLMATTA_AUTHENTICATION
- shellmatta_auth_state_t loginState;
- shellmatta_cmd_t loginCmd;
- shellmatta_cmd_t logoutCmd;
- uint32_t userId;
- uint32_t tmpUserId;
- shellmatta_auth_user_t *userPointer;
- shellmatta_auth_user_t *userList;
- uint32_t userListLength;
- shellmatta_auth_perm_t *permList;
- uint32_t permListLength;
- shellmatta_auth_check_t checkFct;
- shellmatta_auth_log_t logFct;
- #endif
- } shellmatta_instance_t;
- shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
- shellmatta_handle_t *handle,
- char *buffer,
- uint32_t bufferSize,
- char *historyBuffer,
- uint32_t historyBufferSize,
- const char *prompt,
- const shellmatta_cmd_t *cmdList,
- shellmatta_write_t writeFct);
- shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
- bool printPrompt);
- shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
- shellmatta_cmd_t *cmd);
- shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
- const shellmatta_cmd_t *cmd);
- shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
- shellmatta_mode_t mode,
- bool echoEnabled,
- char delimiter);
- shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
- char *data,
- uint32_t size);
- shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
- char *data,
- uint32_t length);
- shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
- char **data,
- uint32_t *length);
- shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
- const char *optionString,
- char *option,
- char **argument,
- uint32_t *argLen);
- shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
- const shellmatta_opt_long_t *longOptions,
- char *option,
- char **argument,
- uint32_t *argLen);
- #ifndef SHELLMATTA_STRIP_PRINTF
- shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
- const char *fmt,
- ...)
- SHELLMATTA_ATTR_FORMAT(2, 3);
- #endif
- #ifdef SHELLMATTA_AUTHENTICATION
- shellmatta_retCode_t shellmatta_auth_init( shellmatta_handle_t handle,
- shellmatta_auth_user_t *userList,
- uint32_t userListLength,
- shellmatta_auth_perm_t *permList,
- uint32_t permListLength,
- bool customLogin,
- shellmatta_auth_check_t checkFct,
- shellmatta_auth_log_t logFct);
- shellmatta_retCode_t shellmatta_auth_login( shellmatta_handle_t handle,
- uint32_t userId);
- shellmatta_retCode_t shellmatta_auth_logout( shellmatta_handle_t handle);
- uint32_t shellmatta_auth_getLoggedInUserId( shellmatta_handle_t handle);
- shellmatta_retCode_t shellmatta_auth_getLoggedInUserName( shellmatta_handle_t handle,
- char *data,
- uint32_t *length);
- shellmatta_retCode_t shellmatta_auth_chpasswd( shellmatta_handle_t handle,
- const char *username,
- const char *password);
- #endif
- #endif
|