2019-06-16 22:03:27 +02:00
|
|
|
/*
|
2021-01-22 23:13:20 +01:00
|
|
|
* Copyright (c) 2019 - 2021 Stefan Strobel <stefan.strobel@shimatta.net>
|
2019-06-16 22:03:27 +02:00
|
|
|
*
|
2019-06-23 22:17:15 +02:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-06-23 22:17:15 +02:00
|
|
|
* @file shellmatta.h
|
|
|
|
* @brief API definition of the Shellmatta terminal implementation
|
|
|
|
* @author Stefan Strobel <stefan.strobel@shimatta.net>
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-24 01:10:44 +01:00
|
|
|
* @addtogroup shellmatta_api Shellmatta API description
|
2019-06-16 22:03:27 +02:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SHELLMATTA_H_
|
|
|
|
#define _SHELLMATTA_H_
|
2019-06-10 22:34:12 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-06-21 04:47:35 +02:00
|
|
|
#include <stdbool.h>
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/* global defines */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief definition of a shellmatta handle
|
|
|
|
*/
|
|
|
|
typedef void* shellmatta_handle_t;
|
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/**
|
|
|
|
* @brief definition of shellmatta return codes
|
|
|
|
*/
|
2019-06-10 22:34:12 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
2019-06-16 22:03:27 +02:00
|
|
|
SHELLMATTA_OK = 0u, /**< everything is OK */
|
|
|
|
SHELLMATTA_ERROR , /**< error occured */
|
|
|
|
SHELLMATTA_CONTINUE , /**< the function is not over */
|
2019-06-21 04:47:35 +02:00
|
|
|
SHELLMATTA_USE_FAULT , /**< parameter error - wrong usage */
|
2020-03-28 11:26:50 +01:00
|
|
|
SHELLMATTA_DUPLICATE , /**< duplicate command */
|
|
|
|
SHELLMATTA_BUSY /**< command is busy keep calling */
|
2019-06-10 22:34:12 +02:00
|
|
|
} shellmatta_retCode_t;
|
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
/**
|
|
|
|
* @brief definition of shellmatta insert mode
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SHELLMATTA_MODE_INSERT = 0u, /**< insert mode */
|
|
|
|
SHELLMATTA_MODE_OVERWRITE , /**< overwrite mode */
|
|
|
|
} shellmatta_mode_t;
|
|
|
|
|
2020-03-08 22:02:51 +01:00
|
|
|
/**
|
|
|
|
* @brief definition of shellmatta optionparser agument type
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
SHELLMATTA_OPT_ARG_NONE = 0u, /**< no argument expected */
|
|
|
|
SHELLMATTA_OPT_ARG_REQUIRED, /**< argument is required */
|
|
|
|
SHELLMATTA_OPT_ARG_OPTIONAL, /**< argument is optional */
|
|
|
|
} shellmatta_opt_argtype_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief definition of shellmatta optionparser agument type
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *paramLong; /**< long parameter string */
|
|
|
|
const char paramShort; /**< short parameter char */
|
|
|
|
shellmatta_opt_argtype_t argtype; /**< argument type expected */
|
|
|
|
} shellmatta_opt_long_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief definition of shellmatta optionparser structure
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-05-01 13:19:33 +02:00
|
|
|
uint32_t argStart; /**< start of the arguments of the command */
|
2020-03-08 22:02:51 +01:00
|
|
|
uint32_t offset; /**< current offset of the option parser */
|
2020-03-16 22:08:06 +01:00
|
|
|
uint32_t nextOffset; /**< offset of the next hunk */
|
|
|
|
uint32_t len; /**< length of the current hunk */
|
2020-03-08 22:02:51 +01:00
|
|
|
} shellmatta_opt_t;
|
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/**
|
|
|
|
* @brief shellmatta command function definition
|
2019-06-23 22:17:15 +02:00
|
|
|
* @param[in] handle pointer to the instance which is calling the cmd
|
|
|
|
* @param[in] arguments argument string called to run this command beginning
|
|
|
|
* with the command itself
|
|
|
|
* @param[in] length length of the argument string
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
2019-06-23 22:17:15 +02:00
|
|
|
typedef shellmatta_retCode_t (*shellmatta_cmdFct_t)(const shellmatta_handle_t handle,
|
|
|
|
const char *arguments,
|
|
|
|
uint32_t length);
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
/**
|
|
|
|
* @brief shellmatta write function definition
|
2019-06-23 22:17:15 +02:00
|
|
|
* @param[in] data data to be written to the output
|
|
|
|
* @param[in] length length of the data to be written
|
2019-06-21 04:47:35 +02:00
|
|
|
*/
|
|
|
|
typedef shellmatta_retCode_t (*shellmatta_write_t)(const char* data, uint32_t length);
|
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/**
|
|
|
|
* @brief structure of one shellmatta command
|
|
|
|
*/
|
2019-06-10 22:34:12 +02:00
|
|
|
typedef struct shellmatta_cmd
|
|
|
|
{
|
2019-06-21 04:47:35 +02:00
|
|
|
char *cmd; /**< command name */
|
|
|
|
char *cmdAlias; /**< command alias */
|
|
|
|
char *helpText; /**< help text to print in "help" command */
|
2021-01-24 19:46:12 +01:00
|
|
|
char *usageText; /**< usage text - printed on "help cmd" */
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_cmdFct_t cmdFct; /**< pointer to the cmd callack function */
|
|
|
|
struct shellmatta_cmd *next; /**< pointer to next command or NULL */
|
2019-06-10 22:34:12 +02:00
|
|
|
} shellmatta_cmd_t;
|
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/**
|
|
|
|
* @brief structure of one shellmatta instance
|
|
|
|
*/
|
2019-06-10 22:34:12 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
uint32_t magic; /**< magic number to check if initialized */
|
|
|
|
char *buffer; /**< input buffer */
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t bufferSize; /**< size of the input buffer */
|
|
|
|
uint32_t inputCount; /**< offset of the current write operation */
|
2020-03-28 11:26:50 +01:00
|
|
|
uint32_t byteCounter; /**< counter used to loop over input data */
|
2019-07-29 01:30:16 +02:00
|
|
|
uint32_t lastNewlineIdx; /**< index of the lest newline */
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t cursor; /**< offset where the cursor is at */
|
2020-03-22 20:37:00 +01:00
|
|
|
uint32_t stdinIdx; /**< start index of stdin in buffer */
|
|
|
|
uint32_t stdinLength; /**< length of the stdin data */
|
2019-06-23 22:17:15 +02:00
|
|
|
char *historyBuffer; /**< buffer to store the last commands */
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t historyBufferSize; /**< size of the history buffer */
|
|
|
|
uint32_t historyStart; /**< index of the oldest stored command */
|
|
|
|
uint32_t historyEnd; /**< index of the newest stored command */
|
|
|
|
uint32_t historyRead; /**< index of the current search */
|
|
|
|
bool historyReadUp; /**< flag to show the last history dir */
|
|
|
|
uint32_t tabCounter; /**< counts the tabulator key presses */
|
|
|
|
uint32_t escapeCounter; /**< counts the characters of an escape seq */
|
2019-07-29 01:30:16 +02:00
|
|
|
char escapeChars[4u]; /**< buffer to save the escape characters */
|
2019-07-30 23:55:12 +02:00
|
|
|
uint32_t hereStartIdx; /**< heredoc start of "<<" */
|
|
|
|
uint32_t hereDelimiterIdx; /**< heredoc delimiter index in input */
|
2019-07-29 01:30:16 +02:00
|
|
|
uint32_t hereLength; /**< length of the heredoc delimiter */
|
2019-06-21 04:47:35 +02:00
|
|
|
bool echoEnabled; /**< if true the input is printed */
|
|
|
|
bool dirty; /**< dirty flag to show changes */
|
|
|
|
const char *prompt; /**< prompt is printed after every command */
|
2020-03-27 18:34:43 +01:00
|
|
|
char delimiter; /**< delimiter (return) to terminate a cmd */
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_mode_t mode; /**< mode of the shell */
|
|
|
|
shellmatta_write_t write; /**< pointer to write function */
|
2020-03-22 20:07:30 +01:00
|
|
|
shellmatta_cmd_t helpCmd; /**< help command structure */
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_cmd_t *cmdList; /**< pointer to the first command */
|
2020-03-22 21:27:18 +01:00
|
|
|
shellmatta_cmd_t *continuousCmd; /**< command to be called continuously */
|
2020-03-28 11:26:50 +01:00
|
|
|
shellmatta_cmd_t *busyCmd; /**< command to be polled (busy mode) */
|
2019-06-23 22:17:15 +02:00
|
|
|
bool cmdListIsConst; /**< true if the #cmdList was passed during
|
|
|
|
initialization */
|
2020-03-08 22:02:51 +01:00
|
|
|
shellmatta_opt_t optionParser; /**< option parser sructure */
|
2019-06-10 22:34:12 +02:00
|
|
|
} shellmatta_instance_t;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_retCode_t shellmatta_doInit( shellmatta_instance_t *inst,
|
2019-06-23 22:17:15 +02:00
|
|
|
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);
|
2020-03-01 18:45:30 +01:00
|
|
|
|
2020-03-01 21:07:08 +01:00
|
|
|
shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle,
|
|
|
|
bool printPrompt);
|
|
|
|
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_retCode_t shellmatta_addCmd( shellmatta_handle_t handle,
|
|
|
|
shellmatta_cmd_t *cmd);
|
2020-03-01 18:45:30 +01:00
|
|
|
|
2020-02-03 21:35:20 +01:00
|
|
|
shellmatta_retCode_t shellmatta_removeCmd( shellmatta_handle_t handle,
|
|
|
|
shellmatta_cmd_t *cmd);
|
2020-03-01 18:45:30 +01:00
|
|
|
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_retCode_t shellmatta_configure( shellmatta_handle_t handle,
|
|
|
|
shellmatta_mode_t mode,
|
2020-03-27 18:34:43 +01:00
|
|
|
bool echoEnabled,
|
|
|
|
char delimiter);
|
2020-03-08 19:56:04 +01:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
|
|
|
char *data,
|
|
|
|
uint32_t size);
|
2020-03-01 18:45:30 +01:00
|
|
|
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
|
|
|
|
char *data,
|
|
|
|
uint32_t length);
|
2020-03-22 20:37:00 +01:00
|
|
|
|
|
|
|
shellmatta_retCode_t shellmatta_read( shellmatta_handle_t handle,
|
|
|
|
char **data,
|
|
|
|
uint32_t *length);
|
2020-03-08 22:02:51 +01:00
|
|
|
|
|
|
|
shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
|
2020-03-28 12:08:01 +01:00
|
|
|
const char *optionString,
|
2020-03-08 22:02:51 +01:00
|
|
|
char *option,
|
|
|
|
char **argument,
|
|
|
|
uint32_t *argLen);
|
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
|
|
|
|
const shellmatta_opt_long_t *longOptions,
|
|
|
|
char *option,
|
|
|
|
char **argument,
|
|
|
|
uint32_t *argLen);
|
2020-03-08 22:02:51 +01:00
|
|
|
|
2019-06-24 16:51:23 +02:00
|
|
|
#ifndef SHELLMATTA_STRIP_PRINTF
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
|
|
|
|
const char *fmt,
|
|
|
|
...);
|
2019-06-24 16:51:23 +02:00
|
|
|
#endif
|
2019-06-16 22:03:27 +02:00
|
|
|
|
|
|
|
#endif
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/** @} */
|