shellmatta/src/shellmatta_history.c

233 lines
6.1 KiB
C
Raw Normal View History

/*
* Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
*
* 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/.
*/
/**
* @file shellmatta_history.c
* @brief history buffer functions of shellmatta
* @author Stefan Strobel <stefan.strobel@shimatta.net>
*/
/**
* @addtogroup shellmatta_history
* @{
*/
#include "shellmatta_history.h"
#include "shellmatta.h"
#include "shellmatta_utils.h"
/**
* @brief appends a byte to the history ring stack buffer
* @param[in] inst pointer to a shellmatta instance
* @param[in] byte byte to append to the history buffer
*/
static void appendHistoryByte(shellmatta_instance_t *inst, char byte)
{
/** -# calculate the new history buffer index */
inst->historyEnd ++;
if(inst->historyEnd >= inst->historyBufferSize)
{
inst->historyEnd = 0u;
}
/** -# append the byte */
inst->historyBuffer[inst->historyEnd] = byte;
/** -# check if the we overwrite an existing stored command */
if(inst->historyEnd == inst->historyStart)
{
/** -# move the start pointer to the next termination (0) */
do
{
inst->historyStart ++;
if(inst->historyStart >= inst->historyBufferSize)
{
inst->historyStart = 0u;
}
}while(0u != inst->historyBuffer[inst->historyStart]);
}
}
/**
* @brief reads a byte from the history buffer and decreases the read index
* @param[in] inst pointer to a shellmatta instance
* @param[out] byte pointer to a char where the read out byte will be stored
* @return
*/
static bool getHistoryByte(shellmatta_instance_t *inst, char *byte)
{
bool ret = false;
/** -# check if we have reached the end of the buffer already */
if(inst->historyRead != inst->historyStart)
{
/** -# read out one byte and decrease the read index */
*byte = inst->historyBuffer[inst->historyRead];
if(0u == inst->historyRead)
{
inst->historyRead = inst->historyBufferSize;
}
inst->historyRead --;
ret = true;
}
return ret;
}
bool history_navigate(shellmatta_instance_t *inst, int32_t cnt)
{
bool ret = true;
uint32_t tempReadIdx = 0u;
while((cnt > 0) && (true == ret))
{
if(inst->historyRead != inst->historyEnd)
{
inst->historyRead ++;
}
while(inst->historyRead != inst->historyEnd)
{
inst->historyRead ++;
if(inst->historyRead >= inst->historyBufferSize)
{
inst->historyRead = 0u;
}
if( (inst->historyRead != inst->historyEnd)
&& (0u == inst->historyBuffer[inst->historyRead]))
{
if(0u == inst->historyRead)
{
inst->historyRead = inst->historyBufferSize;
}
inst->historyRead --;
cnt -= 1;
break;
}
}
if(inst->historyRead == inst->historyEnd)
{
ret = false;
}
}
while((cnt < 0) && (true == ret))
{
tempReadIdx = inst->historyRead;
while(inst->historyRead != inst->historyStart)
{
if(0u == inst->historyRead)
{
inst->historyRead = inst->historyBufferSize;
}
inst->historyRead --;
if( (inst->historyRead != inst->historyStart)
&& (0u == inst->historyBuffer[inst->historyRead]))
{
if(0u == inst->historyRead)
{
inst->historyRead = inst->historyBufferSize;
}
inst->historyRead --;
cnt += 1;
break;
}
}
if(inst->historyRead == inst->historyStart)
{
inst->historyRead = tempReadIdx;
inst->historyReadUp = false;
ret = false;
}
}
return ret;
}
/**
* @brief stores the current command from the instances buffer into the
* history buffer
* @param[in] inst pointer to a shellmatta instance
*/
void history_storeCmd(shellmatta_instance_t *inst)
{
uint32_t i;
/** -# check if we have enough room for the command in the history buffer
* and there is a new command to be stored */
if( (inst->historyBufferSize > inst->inputCount)
&& (0u != inst->inputCount)
&& (true == inst->dirty))
{
/** -# append the command termination */
appendHistoryByte(inst, 0u);
/** -# append the command byte wise in reverse direction */
for(i = inst->inputCount; i > 0u; i --)
{
appendHistoryByte(inst, inst->buffer[i - 1u]);
}
}
/** -# remove the dirty flag - everything is nice and saved */
inst->dirty = false;
}
/**
* @brief navigates in the history buffer by the given number of commands
* @param[in] inst pointer to a shellmatta instance
* @param[in] cnt direction and count to navigate
* @return
*/
/**
* @brief restores the command from the history buffer where the read
* index points on
* @param[in] inst pointer to a shellmatta instance
*/
void history_restoreCmd(shellmatta_instance_t *inst)
{
char byte;
bool ret = true;
ret = getHistoryByte(inst, &byte);
while((ret == true) && (byte != 0u))
{
inst->buffer[inst->inputCount] = byte;
inst->inputCount ++;
inst->cursor ++;
ret = getHistoryByte(inst, &byte);
}
utils_writeEcho(inst, inst->buffer, inst->inputCount);
history_navigate(inst, 1);
inst->dirty = false;
}
/**
* @brief resets the history buffer pointers to show to the most recent
* command again
* @param[in] inst pointer to a shellmatta instance
*/
void history_reset(shellmatta_instance_t *inst)
{
inst->historyRead = inst->historyEnd;
inst->historyReadUp = true;
}
/**
* @}
*/