close #14 - added a resetShell api function + fixed some problems
This commit is contained in:
@@ -111,6 +111,53 @@ shellmatta_retCode_t shellmatta_doInit(
|
||||
return SHELLMATTA_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief resets the whole shellmatta instance
|
||||
* @param[in] handle shellmatta instance handle
|
||||
* @param[in] printPrompt print a new command prompt
|
||||
*
|
||||
* This function can be used e.g. when working with connection based interfaces (e.g. sockets) to clear
|
||||
* the shell from old content when a new connection is opened.
|
||||
* It resets all internal states - the buffers are left as they are - they will be overwritten.
|
||||
* The history buffer is deleted as well.
|
||||
*/
|
||||
shellmatta_retCode_t shellmatta_resetShell( shellmatta_handle_t handle, bool printPrompt)
|
||||
{
|
||||
shellmatta_instance_t *inst = (shellmatta_instance_t *)handle;
|
||||
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
||||
|
||||
/*! -# check if the instance is plausible */
|
||||
if( (NULL != handle)
|
||||
&& (SHELLMATTA_MAGIC == inst->magic))
|
||||
{
|
||||
inst->inputCount = 0u;
|
||||
inst->lastNewlineIdx = 0u;
|
||||
inst->cursor = 0u;
|
||||
inst->historyStart = 0u;
|
||||
inst->historyEnd = 0u;
|
||||
inst->historyRead = 0u;
|
||||
inst->historyReadUp = true;
|
||||
inst->dirty = false;
|
||||
inst->tabCounter = 0u;
|
||||
inst->escapeCounter = 0u;
|
||||
inst->hereStartIdx = 0u;
|
||||
inst->hereDelimiterIdx = 0u;
|
||||
inst->hereLength = 0u;
|
||||
|
||||
if(true == printPrompt)
|
||||
{
|
||||
/** -# print a prompt if requested */
|
||||
utils_terminateInput(inst);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = SHELLMATTA_USE_FAULT;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief adds a command to the command list alphabetically ordered
|
||||
* @param[in] handle shellmatta instance handle
|
||||
@@ -153,7 +200,8 @@ shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cm
|
||||
while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
|
||||
{
|
||||
cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
|
||||
if(NULL != cmd->cmdAlias)
|
||||
if( (NULL != cmd->cmdAlias)
|
||||
&& (NULL != tempCmd->cmdAlias))
|
||||
{
|
||||
aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
|
||||
}
|
||||
@@ -275,8 +323,8 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
||||
uint8_t cmdExecuted = 0u;
|
||||
uint32_t cmdLen;
|
||||
char *tempString;
|
||||
char *argumentString = NULL;
|
||||
uint32_t argumentLength = 0u;
|
||||
char *argumentString;
|
||||
uint32_t argumentLength;
|
||||
uint32_t byteCounter;
|
||||
uint32_t idx;
|
||||
|
||||
@@ -290,6 +338,10 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
||||
/** -# process byte wise */
|
||||
for (byteCounter = 0u; byteCounter < size; byteCounter++)
|
||||
{
|
||||
/*! -# set default values for the command argument - can be overwritten by heredoc */
|
||||
argumentString = inst->buffer;
|
||||
argumentLength = inst->inputCount;
|
||||
|
||||
/** -# handle escape sequences */
|
||||
if(inst->escapeCounter != 0u)
|
||||
{
|
||||
@@ -315,7 +367,8 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
||||
* }
|
||||
* \enddot */
|
||||
|
||||
/** -# check for heredoc */
|
||||
/** -# check for heredoc - add string delimiter to stop strstr from searching too far */
|
||||
inst->buffer[inst->inputCount] = '\0';
|
||||
tempString = strstr(inst->buffer, "<<");
|
||||
if(NULL != tempString)
|
||||
{
|
||||
@@ -349,9 +402,6 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentString = inst->buffer;
|
||||
argumentLength = inst->inputCount;
|
||||
|
||||
/** -# store the current command and reset the history buffer */
|
||||
inst->dirty = true;
|
||||
history_storeCmd(inst);
|
||||
@@ -396,8 +446,9 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
||||
}
|
||||
else
|
||||
{
|
||||
/*! -# the party goes on - print the \r and add a \n to satisfy most terminals */
|
||||
inst->lastNewlineIdx = inst->inputCount;
|
||||
utils_insertChars(inst, data, 1);
|
||||
utils_insertChars(inst, data, 1u);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -47,7 +47,6 @@ shellmatta_retCode_t escape_processArrowKeys(shellmatta_instance_t *inst)
|
||||
history_navigate(inst, -1);
|
||||
}
|
||||
inst->historyReadUp = true;
|
||||
utils_clearInput(inst);
|
||||
history_restoreCmd(inst);
|
||||
history_navigate(inst, -1);
|
||||
|
||||
@@ -63,7 +62,6 @@ shellmatta_retCode_t escape_processArrowKeys(shellmatta_instance_t *inst)
|
||||
}
|
||||
inst->historyReadUp = false;
|
||||
history_navigate(inst, 1);
|
||||
utils_clearInput(inst);
|
||||
history_restoreCmd(inst);
|
||||
}
|
||||
break;
|
||||
|
@@ -201,8 +201,16 @@ void history_restoreCmd(shellmatta_instance_t *inst)
|
||||
{
|
||||
char byte;
|
||||
bool ret = true;
|
||||
bool anythingToRestore = false;
|
||||
|
||||
ret = getHistoryByte(inst, &byte);
|
||||
|
||||
/*! -# delete the input if there is data in the history buffer */
|
||||
if(true == ret)
|
||||
{
|
||||
utils_clearInput(inst);
|
||||
anythingToRestore = true;
|
||||
}
|
||||
while((ret == true) && (byte != 0u))
|
||||
{
|
||||
inst->buffer[inst->inputCount] = byte;
|
||||
@@ -211,9 +219,12 @@ void history_restoreCmd(shellmatta_instance_t *inst)
|
||||
ret = getHistoryByte(inst, &byte);
|
||||
}
|
||||
|
||||
utils_writeEcho(inst, inst->buffer, inst->inputCount);
|
||||
if(true == anythingToRestore)
|
||||
{
|
||||
utils_writeEcho(inst, inst->buffer, inst->inputCount);
|
||||
inst->dirty = false;
|
||||
}
|
||||
history_navigate(inst, 1);
|
||||
inst->dirty = false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user