123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- #include "shellmatta.h"
- #include "shellmatta_autocomplete.h"
- #include "shellmatta_history.h"
- #include "shellmatta_utils.h"
- #include "shellmatta_escape.h"
- #include <stddef.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdio.h>
- shellmatta_retCode_t shellmatta_doInit(
- shellmatta_instance_t *inst,
- 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)
- {
-
- if( (NULL != inst)
- && (NULL != handle)
- && (NULL != buffer)
- && (0u != bufferSize)
- && (NULL != prompt)
- && (NULL != writeFct)
- && ((NULL != historyBuffer) || (0u == historyBufferSize)))
- {
-
- inst->buffer = buffer;
- inst->bufferSize = bufferSize;
- inst->inputCount = 0u;
- inst->lastNewlineIdx = 0u;
- inst->cursor = 0u;
- inst->historyBuffer = historyBuffer;
- inst->historyBufferSize = historyBufferSize;
- inst->historyStart = 0u;
- inst->historyEnd = 0u;
- inst->historyRead = 0u;
- inst->historyReadUp = true;
- inst->write = writeFct;
- inst->prompt = prompt;
- inst->echoEnabled = true;
- inst->dirty = false;
- inst->tabCounter = 0u;
- inst->escapeCounter = 0u;
- inst->hereStartIdx = 0u;
- inst->hereDelimiterIdx = 0u;
- inst->hereLength = 0u;
- inst->mode = SHELLMATTA_MODE_INSERT;
- inst->cmdList = &helpCmd;
- inst->cmdListIsConst = false;
- if(NULL != cmdList)
- {
- helpCmd.next = (shellmatta_cmd_t *) cmdList;
- inst->cmdListIsConst = true;
- }
- inst->magic = SHELLMATTA_MAGIC;
- *handle = (shellmatta_handle_t)inst;
-
- utils_terminateInput(inst);
- }
- return SHELLMATTA_OK;
- }
- shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
- {
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
- shellmatta_cmd_t *tempCmd;
- shellmatta_cmd_t **prevCmd;
- bool cmdPlaced = false;
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- int cmdDiff = 0;
- int aliasDiff = 0;
-
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic)
- && (false == inst->cmdListIsConst))
- {
- tempCmd = inst->cmdList;
- prevCmd = &inst->cmdList;
-
- if (NULL == tempCmd)
- {
- inst->cmdList = cmd;
- cmd->next = NULL;
- }
-
- else
- {
- while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
- {
- cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
- aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
-
- if((0u == cmdDiff) || (0u == aliasDiff))
- {
- ret = SHELLMATTA_DUPLICATE;
- }
- else if(0 < cmdDiff)
- {
- cmd->next = tempCmd;
- *prevCmd = cmd;
- cmdPlaced = true;
- }
- else if(NULL == tempCmd->next)
- {
- tempCmd->next = cmd;
- cmd->next = NULL;
- cmdPlaced = true;
- }
- else
- {
-
- }
- prevCmd = &(tempCmd->next);
- tempCmd = tempCmd->next;
- }
- }
- }
- else
- {
- ret = SHELLMATTA_USE_FAULT;
- }
- return ret;
- }
- shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
- char *data,
- uint32_t size)
- {
- shellmatta_cmd_t *cmd;
- uint8_t cmdExecuted = 0u;
- uint32_t cmdLen;
- char *tempString;
- char *argumentString;
- uint32_t argumentLength;
- uint32_t byteCounter;
- uint32_t idx;
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
-
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
-
- for (byteCounter = 0u; byteCounter < size; byteCounter++)
- {
-
- if(inst->escapeCounter != 0u)
- {
- escape_handleSequence(inst, *data);
- }
-
- else if((0u == inst->inputCount) && ('\n' == *data))
- {
-
- }
-
- else if ('\r' == *data)
- {
- if(0u == inst->hereLength)
- {
-
-
- tempString = strstr(inst->buffer, "<<");
- if(NULL != tempString)
- {
-
- if(inst->inputCount > ((uint32_t)(tempString - inst->buffer) + 2u))
- {
- inst->hereStartIdx = (uint32_t)(tempString - inst->buffer);
- inst->hereDelimiterIdx = inst->hereStartIdx + 2u;
- while((inst->hereDelimiterIdx < inst->inputCount)
- && ( ('\0' == inst->buffer[inst->hereDelimiterIdx])
- || (' ' == inst->buffer[inst->hereDelimiterIdx])))
- {
- inst->hereDelimiterIdx ++;
- }
- inst->hereLength = inst->inputCount - inst->hereDelimiterIdx;
- inst->dirty = true;
- utils_insertChars(inst, data, 1);
- inst->lastNewlineIdx = inst->inputCount;
- }
- else
- {
- inst->hereLength = 0u;
-
- inst->dirty = true;
- history_storeCmd(inst);
- history_reset(inst);
- }
- }
- else
- {
- argumentString = inst->buffer;
- argumentLength = inst->inputCount;
-
- inst->dirty = true;
- history_storeCmd(inst);
- history_reset(inst);
- }
- }
- else
- {
- tempString = &inst->buffer[inst->lastNewlineIdx];
- cmdLen = inst->inputCount - inst->lastNewlineIdx;
-
- while(('\n' == *tempString) || ('\r' == *tempString))
- {
- tempString ++;
- cmdLen --;
- }
- if( (inst->hereLength == cmdLen)
- && (0 == strncmp( &inst->buffer[inst->hereDelimiterIdx],
- tempString,
- inst->hereLength)))
- {
- argumentLength = inst->lastNewlineIdx;
-
- inst->dirty = true;
- history_storeCmd(inst);
- history_reset(inst);
-
- for(idx = 1u; idx <= inst->hereStartIdx; idx++)
- {
- inst->buffer[inst->hereDelimiterIdx + inst->hereLength - idx] = inst->buffer[inst->hereStartIdx - idx];
- }
- argumentString = &(inst->buffer[inst->hereDelimiterIdx + inst->hereLength - inst->hereStartIdx]);
- argumentLength = inst->lastNewlineIdx - ((inst->hereDelimiterIdx + inst->hereLength) - inst->hereStartIdx);
- inst->hereLength = 0u;
- }
- else
- {
- inst->lastNewlineIdx = inst->inputCount;
- utils_insertChars(inst, data, 1);
- }
- }
- if(0u == inst->hereLength)
- {
- cmd = inst->cmdList;
- argumentString[argumentLength] = 0u;
-
- cmdLen = 0u;
- while( (cmdLen < argumentLength)
- && (' ' != argumentString[cmdLen])
- && ('\r' != argumentString[cmdLen])
- && ('\n' != argumentString[cmdLen])
- && ('\0' != argumentString[cmdLen]))
- {
- cmdLen ++;
- }
-
- while (NULL != cmd)
- {
-
- if ( ((0 == strncmp( argumentString,
- cmd->cmd,
- cmdLen))
- && (cmdLen == strlen(cmd->cmd)))
- || ((0 == strncmp( argumentString,
- cmd->cmdAlias,
- cmdLen))
- && (cmdLen == strlen(cmd->cmdAlias))))
- {
- inst->write("\r\n", 2u);
- cmdExecuted = 1u;
- cmd->cmdFct(inst, argumentString, argumentLength);
- cmd = NULL;
- }
- else
- {
- cmd = cmd->next;
- }
- }
- if ((cmdExecuted == 0u) && (inst->inputCount > 0))
- {
- inst->write("\r\nCommand: ", 11u);
- inst->write(argumentString, argumentLength);
- inst->write(" not found", 10u);
- }
- utils_terminateInput(inst);
- }
- }
-
- else if('\t' == *data)
- {
- inst->dirty = true;
- autocomplete_run(inst);
- }
-
- else if(3 == *data)
- {
- inst->dirty = false;
- history_reset(inst);
- utils_terminateInput(inst);
- }
-
- else if('\b' == *data)
- {
- inst->dirty = true;
- utils_removeChars(inst, 1u, true);
- }
-
- else if(0x7eu == *data)
- {
- inst->dirty = true;
- utils_removeChars(inst, 1u, false);
- }
-
- else if('\x1b' == *data)
- {
- inst->escapeCounter = 1u;
- }
- else
- {
- inst->dirty = true;
- utils_insertChars(inst, data, 1);
- }
-
- if ('\t' != *data)
- {
- inst->tabCounter = 0u;
- }
- data ++;
- }
- }
- else
- {
- ret = SHELLMATTA_USE_FAULT;
- }
- return ret;
- }
- shellmatta_retCode_t shellmatta_write( shellmatta_handle_t handle,
- char *data,
- uint32_t length)
- {
- shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
-
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
-
- ret = inst->write(data, length);
- }
- return ret;
- }
- #ifndef SHELLMATTA_STRIP_PRINTF
- shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
- const char *fmt,
- ...)
- {
- char outputBuffer[SHELLMATTA_OUTPUT_BUFFER_SIZE];
- va_list arg;
- int length;
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
-
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
-
- va_start(arg, fmt);
- length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
- va_end(arg);
- if(length < 0)
- {
- ret = SHELLMATTA_ERROR;
- }
- else
- {
- inst->write(outputBuffer, length);
- }
- }
- else
- {
- ret = SHELLMATTA_USE_FAULT;
- }
- return ret;
- }
- #endif
|