123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- /*
- * 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;
- }
- /**
- * @}
- */
|