123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- #include "shellmatta_history.h"
- #include "shellmatta.h"
- #include "shellmatta_utils.h"
- static void appendHistoryByte(shellmatta_instance_t *inst, char byte)
- {
-
- inst->historyEnd ++;
- if(inst->historyEnd >= inst->historyBufferSize)
- {
- inst->historyEnd = 0u;
- }
-
- inst->historyBuffer[inst->historyEnd] = byte;
-
- if(inst->historyEnd == inst->historyStart)
- {
-
- do
- {
- inst->historyStart ++;
- if(inst->historyStart >= inst->historyBufferSize)
- {
- inst->historyStart = 0u;
- }
- }while(0u != inst->historyBuffer[inst->historyStart]);
- }
- }
- static bool getHistoryByte(shellmatta_instance_t *inst, char *byte)
- {
- bool ret = false;
-
- if(inst->historyRead != inst->historyStart)
- {
-
- *byte = inst->historyBuffer[inst->historyRead];
- if(0u == inst->historyRead)
- {
- inst->historyRead = inst->historyBufferSize;
- }
- inst->historyRead --;
- ret = true;
- }
- return ret;
- }
- static bool compareLastCommand(shellmatta_instance_t *inst)
- {
- bool ret = false;
- uint32_t i;
- uint32_t cnt;
-
- if(inst->historyStart != inst->historyEnd)
- {
- i = inst->historyEnd;
- cnt = 0u;
- ret = true;
- while((true == ret) && (cnt < inst->inputCount))
- {
-
- if((inst->historyBuffer[i] != inst->buffer[cnt]) || (0u == inst->historyBuffer[i]))
- {
- ret = false;
- }
- if(0u == i)
- {
- i = (inst->historyBufferSize - 1u);
- }
- else
- {
- i --;
- }
- cnt ++;
- }
-
- if(0u != inst->historyBuffer[i])
- {
- ret = false;
- }
- }
- 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 -= inst->historyBufferSize;
- }
- 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;
- }
- void history_storeCmd(shellmatta_instance_t *inst)
- {
- uint32_t i;
-
- if( (inst->historyBufferSize > inst->inputCount)
- && (0u != inst->inputCount)
- && (true == inst->dirty)
- && (true != compareLastCommand(inst)))
- {
-
- appendHistoryByte(inst, 0u);
-
- for(i = inst->inputCount; i > 0u; i --)
- {
- appendHistoryByte(inst, inst->buffer[i - 1u]);
- }
- }
- inst->dirty = false;
- }
- void history_restoreCmd(shellmatta_instance_t *inst)
- {
- char byte;
- bool ret = true;
- bool anythingToRestore = false;
- ret = getHistoryByte(inst, &byte);
-
- if(true == ret)
- {
- utils_clearInput(inst);
- anythingToRestore = true;
- }
- while((ret == true) && (byte != 0u))
- {
- inst->buffer[inst->inputCount] = byte;
- inst->inputCount ++;
- inst->cursor ++;
- ret = getHistoryByte(inst, &byte);
- }
- if(true == anythingToRestore)
- {
- utils_writeEcho(inst, inst->buffer, inst->inputCount);
- inst->dirty = false;
- }
- (void)history_navigate(inst, 1);
- }
- void history_reset(shellmatta_instance_t *inst)
- {
- inst->historyRead = inst->historyEnd;
- inst->historyReadUp = true;
- }
|