| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203 |
- /*
- * 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.c
- * @brief Main implementation of the Shellmatta terminal implementation
- * @author Stefan Strobel <stefan.strobel@shimatta.net>
- */
- /**
- * @addtogroup shellmatta
- * @{
- * @addtogroup shellmatta_private
- * @{
- */
- #include "shellmatta.h"
- #include <stdio.h>
- #include <stddef.h>
- #include <string.h>
- #include <stdarg.h>
- /* defines */
- #ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE
- #define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u
- #endif
- #define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a))
- #define SHELLMATTA_MAX(a,b) (((a) < (b)) ? (b) : (a))
- #define SHELLMATTA_PRINT_BUFFER(buffer,cnt,fct) \
- while((cnt) > sizeof((buffer))) \
- { \
- (cnt) -= sizeof((buffer)); \
- (fct)((buffer), sizeof((buffer))); \
- } \
- if((cnt) != 0u) \
- { \
- (fct)((buffer), (cnt)); \
- }
- #define SHELLMATTA_MAGIC 0x5101E110u
- /* static functions */
- /**
- * @brief function to write an echo to the output depending on
- * the echo enabled state of the instance
- * @param[in] inst pointer to a shellmatta instance
- * @param[in] data pointer to the data so send
- * @param[in] length length of the data to send (in byte)
- */
- static void writeEcho( shellmatta_instance_t *inst,
- const char *data,
- uint32_t length)
- {
- if(true == inst->echoEnabled)
- {
- inst->write(data, length);
- }
- }
- /**
- * @brief itoa like function to convert int to an ascii string
- * @warning you have to provide a large enough buffer
- * @param[in] value
- * @param[in,out] buffer
- * @param[in] base
- * @return number of bytes in string
- */
- static uint32_t shellItoa(int32_t value, char *buffer, uint32_t base)
- {
- char tempBuffer[34u];
- uint32_t i;
- uint32_t bufferIdx = 0u;
- char digitValue;
- /** -# check the base for plausibility */
- if((2 <= base) && (16 >= base))
- {
- /** -# check for sign */
- if(0 > value)
- {
- value = value * (-1);
- buffer[0u] = '-';
- bufferIdx += 1u;
- }
- /** -# loop through all digits in reverse order */
- i = 0u;
- do
- {
- digitValue = (char) (value % base);
- tempBuffer[i] = (digitValue < 10u) ? ('0' + digitValue) : (('A' - 10) + digitValue);
- value /= base;
- i ++;
- }while(value > 0);
- /** -# store the string in the correct order onto the buffer */
- while(i > 0u)
- {
- buffer[bufferIdx] = tempBuffer[i - 1u];
- i --;
- bufferIdx ++;
- }
- }
- return bufferIdx;
- }
- /**
- * @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;
- }
- /**
- * @brief stores the current command from the instances buffer into the
- * history buffer
- * @param[in] inst pointer to a shellmatta instance
- */
- static void storeHistoryCmd(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
- */
- static bool navigateHistoryCmd(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 restores the command from the history buffer where the read
- * index points on
- * @param[in] inst pointer to a shellmatta instance
- */
- static void restoreHistoryCmd(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);
- }
- writeEcho(inst, inst->buffer, inst->inputCount);
- navigateHistoryCmd(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
- */
- static void resetHistoryBuffer(shellmatta_instance_t *inst)
- {
- inst->historyRead = inst->historyEnd;
- inst->historyReadUp = true;
- }
- /**
- * @brief tells the terminal to save the current cursor position
- * @param[in] inst pointer to shellmatta instance
- */
- static void saveCursorPos(shellmatta_instance_t *inst)
- {
- writeEcho(inst, "\e[s", 3u);
- }
- /**
- * @brief tells the terminal to restore the saved cursor position
- * @param[in] inst pointer to shellmatta instance
- */
- static void restoreCursorPos(shellmatta_instance_t *inst)
- {
- writeEcho(inst, "\e[u", 3u);
- }
- /**
- * @brief tells the terminal to erase the line on the right side of the
- * cursor
- * @param[in] inst pointer to shellmatta instance
- */
- static void eraseLine(shellmatta_instance_t *inst)
- {
- writeEcho(inst, "\e[K", 3u);
- }
- /**
- * @brief moves the cursor back by the given amoung of characters
- * @param[in] inst pointer to shellmatta instance
- * @param[in] length number of characters to rewind
- */
- static void rewindCursor(shellmatta_instance_t *inst, uint32_t length)
- {
- char terminalCmd[16];
- size_t size;
- length = SHELLMATTA_MIN (length, inst->cursor);
- if(length > 0u)
- {
- terminalCmd[0] = '\e';
- terminalCmd[1] = '[';
- size = 2u + shellItoa(length, &terminalCmd[2], 10);
- terminalCmd[size] = 'D';
- writeEcho(inst, terminalCmd, size + 1u);
- inst->cursor -= length;
- }
- }
- /**
- * @brief moves the cursor forward by the given amoung of characters
- * @param[in] inst pointer to shellmatta instance
- * @param[in] length number of characters to move forward
- */
- static void forwardCursor(shellmatta_instance_t *inst, uint32_t length)
- {
- char terminalCmd[16];
- size_t size;
- length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor));
- if (length > 0u)
- {
- terminalCmd[0] = '\e';
- terminalCmd[1] = '[';
- size = 2u + shellItoa(length, &terminalCmd[2], 10);
- terminalCmd[size] = 'C';
- writeEcho(inst, terminalCmd, size + 1u);
- inst->cursor += length;
- }
- }
- /**
- * @brief inserts the given amount of characters at the cursor position
- * @param[in] inst pointer to shellmatta instance
- * @param[in] data pointer to the data to be inserted
- * @param[in] length length of the data to be inserted
- */
- static void insertChars(shellmatta_instance_t *inst,
- char *data,
- uint32_t length)
- {
- if(0u != length)
- {
- /** -# check if we have to move chars in the buffer */
- if( (inst->cursor != inst->inputCount)
- && (SHELLMATTA_MODE_INSERT == inst->mode))
- {
- /** -# move the existing chars */
- for ( uint32_t i = inst->inputCount;
- i > inst->cursor;
- i --)
- {
- inst->buffer[i + length - 1] = inst->buffer[i - 1];
- }
- /** -# store and print the new chars */
- memcpy(&(inst->buffer[inst->cursor]), data, length);
- writeEcho(inst, data, length);
- /** -# print the other chars and restore the cursor to this position */
- eraseLine(inst);
- saveCursorPos(inst);
- writeEcho( inst,
- &(inst->buffer[inst->cursor + length]),
- inst->inputCount - inst->cursor);
- restoreCursorPos(inst);
- }
- /** -# just overwrite/append the chars */
- else
- {
- memcpy(&(inst->buffer[inst->cursor]), data, length);
- writeEcho(inst, data, length);
- }
- inst->inputCount += length;
- inst->cursor += length;
- }
- }
- /**
- * @brief removes the given amount of characters from the current cursor
- * position
- * @param[in] inst pointer to a shellmatta instance
- * @param[in] length number of characters to remove
- * @param[in] backspace remove characters left of the cursor
- */
- static void removeChars(shellmatta_instance_t *inst,
- uint32_t length,
- bool backspace)
- {
- if(0u != length)
- {
- /** -# rewind the cursor in case of backspace */
- if(true == backspace)
- {
- length = SHELLMATTA_MIN (length, inst->cursor);
- rewindCursor(inst, length);
- }
- else
- {
- length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
- }
- /** -# delete the char at the cursor position */
- for ( uint32_t i = inst->cursor;
- i < (inst->inputCount - length);
- i++)
- {
- inst->buffer[i] = inst->buffer[i + length];
- }
- /** -# print the rest of the line again */
- eraseLine(inst);
- saveCursorPos(inst);
- writeEcho( inst,
- &(inst->buffer[inst->cursor]),
- (inst->inputCount - inst->cursor - length));
- restoreCursorPos(inst);
- inst->inputCount -= length;
- }
- }
- /**
- * @brief clears the input buffer and removes the currend command from
- * the terminal output
- * @param[in] inst pointer to a shellmatta instance
- */
- static void clearInput(shellmatta_instance_t *inst)
- {
- rewindCursor(inst, inst->cursor);
- eraseLine(inst);
- inst->inputCount = 0u;
- inst->dirty = false;
- }
- /**
- * @brief processes the excape character stream of the instance and chacks
- * if an arrow key was pressed
- * @param[in] inst pointer to a shellmatta instance
- * @return #SHELLMATTA_OK if an arrow key was pressed
- */
- static shellmatta_retCode_t processArrowKeys(shellmatta_instance_t *inst)
- {
- shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
- if ('[' == inst->escapeChars[0])
- {
- ret = SHELLMATTA_OK;
- switch (inst->escapeChars[1])
- {
- case 'A': /* arrow up */
- storeHistoryCmd(inst);
- if(false == inst->historyReadUp)
- {
- navigateHistoryCmd(inst, -1);
- }
- inst->historyReadUp = true;
- clearInput(inst);
- restoreHistoryCmd(inst);
- navigateHistoryCmd(inst, -1);
- break;
- case 'B': /* arrow down */
- if((inst->historyRead != inst->historyEnd))
- {
- storeHistoryCmd(inst);
- if(true == inst->historyReadUp)
- {
- navigateHistoryCmd(inst, 1);
- }
- inst->historyReadUp = false;
- navigateHistoryCmd(inst, 1);
- clearInput(inst);
- restoreHistoryCmd(inst);
- }
- break;
- case 'C': /* arrow right */
- forwardCursor(inst, 1u);
- break;
- case 'D': /* arrow left */
- rewindCursor(inst, 1u);
- break;
- default:
- /** ignore unknown escape */
- ret = SHELLMATTA_USE_FAULT;
- break;
- }
- }
- return ret;
- }
- /**
- * @brief handles a ANSI escape sequence to be able to react to some
- * special keys
- * @param[in] inst pointer to a shellmatta instance
- * @param[in] data new received character of the escape sequence
- */
- static void handleEscapeSequence(shellmatta_instance_t *inst, char data)
- {
- switch (inst->escapeCounter)
- {
- case 1u:
- inst->escapeChars[inst->escapeCounter - 1] = data;
- inst->escapeCounter ++;
- break;
- case 2u:
- inst->escapeChars[inst->escapeCounter - 1] = data;
- /** -# check if an arrow key was pressed */
- if(SHELLMATTA_OK == processArrowKeys(inst))
- {
- inst->escapeCounter = 0u;
- }
- /** -# check if end was pressed */
- else if ( (0x4f == inst->escapeChars[0])
- && (0x46 == inst->escapeChars[1]))
- {
- forwardCursor(inst, inst->inputCount - inst->cursor);
- inst->escapeCounter = 0u;
- }
- /** -# if the highest bit is not set this is usually not the end of the
- * sequence - so we go on */
- else if(0u == (0x40 & data))
- {
- inst->escapeCounter ++;
- }
- else
- {
- inst->escapeCounter = 0u;
- }
- break;
- case 3u:
- /** -# check if delete was pressed */
- inst->escapeChars[inst->escapeCounter - 1] = data;
- if( (0x5b == inst->escapeChars[0])
- && (0x33 == inst->escapeChars[1])
- && (0x7e == inst->escapeChars[2]))
- {
- removeChars(inst, 1u, false);
- inst->escapeCounter = 0u;
- }
- /** -# check if pos1 was pressed */
- else if ( (0x5bu == inst->escapeChars[0])
- && (0x31u == inst->escapeChars[1])
- && (0x7eu == inst->escapeChars[2]))
- {
- rewindCursor(inst, inst->cursor);
- inst->escapeCounter = 0u;
- }
- else if(0u == (0x40u & data))
- {
- inst->escapeCounter ++;
- }
- else
- {
- inst->escapeCounter = 0u;
- }
- break;
- default:
- inst->escapeCounter = 0u;
- break;
- }
- }
- /**
- * @brief terminates an input and prints the prompt again
- * @param[in] inst pointer to a shellmatta instance
- */
- static void terminateInput(shellmatta_instance_t *inst)
- {
- inst->inputCount = 0u;
- inst->cursor = 0u;
- inst->write("\r\n", 2u);
- inst->write(inst->prompt, strlen(inst->prompt));
- }
- /**
- * @brief searches the registered commands for matching ones and prints
- * the common part of all matching commands on the output
- * if called twice all matching commands are printed
- * @param[in] inst pointer to a shellmatta instance
- */
- static void doAutocomplete(shellmatta_instance_t *inst)
- {
- shellmatta_cmd_t *cmd = inst->cmdList;
- char *tempCmd = NULL;
- uint32_t minLen = 0u;
- uint32_t sizeDiff = 0u;
- bool exactMatch = true;
- uint32_t printedLen = 0u;
- uint32_t tempCursor = 0u;
- /** -# increase the tab counter to print all matching commands next time */
- inst->tabCounter++;
- /** -# on douple tab show all matching commands */
- if (1u < inst->tabCounter)
- {
- inst->tabCounter = 0u;
- /** -# loop through all registered commands */
- while (NULL != cmd)
- {
- /** -# check if command matches the input */
- if( (strlen(cmd->cmd) >= inst->cursor)
- && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
- {
- /** -# add newline on first find */
- if(0u == printedLen)
- {
- inst->write("\r\n", 2u);
- }
- inst->write(cmd->cmd, strlen(cmd->cmd));
- printedLen += strlen(cmd->cmd);
- inst->write(" ", 4u);
- printedLen += 4u;
- }
- /** -# check if command alias matches the input */
- if( (strlen(cmd->cmdAlias) >= inst->cursor)
- && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
- {
- /** -# add newline on first find */
- if(0u == printedLen)
- {
- inst->write("\r\n", 2u);
- }
- inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
- printedLen += strlen(cmd->cmdAlias);
- inst->write(" ", 4u);
- printedLen += 4u;
- }
- cmd = cmd->next;
- }
- /** -# print input again if a commands was found */
- if(printedLen > 0u)
- {
- writeEcho(inst, "\r\n", 2u);
- writeEcho(inst, inst->prompt, strlen(inst->prompt));
- writeEcho(inst, inst->buffer, inst->inputCount);
- tempCursor = inst->cursor;
- inst->cursor = inst->inputCount;
- rewindCursor(inst, inst->inputCount - tempCursor);
- }
- }
- /** -# on single tab autocomplete as far as possible */
- else if(0u != inst->cursor)
- {
- /** -# loop through all registered commands */
- while (NULL != cmd)
- {
- /** -# check if command matches the input */
- if( (strlen(cmd->cmd) >= inst->cursor)
- && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
- {
- /** -# store first match */
- if(NULL == tempCmd)
- {
- tempCmd = cmd->cmd;
- minLen = strlen(cmd->cmd);
- }
- /** -# find common part of the matching commands */
- else
- {
- exactMatch = false;
- minLen = SHELLMATTA_MIN(strlen(cmd->cmd), minLen);
- for(uint32_t i = 0u; i < minLen; i++)
- {
- if(cmd->cmd[i] != tempCmd[i])
- {
- minLen = i;
- }
- }
- }
- }
- /** -# check if command Alias matches the input */
- if( (strlen(cmd->cmdAlias) >= inst->cursor)
- && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
- {
- /** -# store first match */
- if(NULL == tempCmd)
- {
- tempCmd = cmd->cmdAlias;
- minLen = strlen(cmd->cmdAlias);
- }
- /** -# find common part of the matches */
- else
- {
- exactMatch = false;
- minLen = SHELLMATTA_MIN(strlen(cmd->cmdAlias), minLen);
- for(uint32_t i = 0u; i < minLen; i++)
- {
- if(cmd->cmdAlias[i] != tempCmd[i])
- {
- minLen = i;
- }
- }
- }
- }
- cmd = cmd->next;
- }
- /** -# autocomplete found command */
- if(NULL != tempCmd)
- {
- /** -# calculate the size of the command to be inserted */
- sizeDiff = minLen - inst->cursor;
- /** -# copy the found part into the buffer and display it */
- insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff);
- /** -# on exact match there is no need to double Tab to display all */
- if(true == exactMatch)
- {
- inst->tabCounter = 0u;
- }
- }
- }
- else
- {
- /* nothing to do here */
- }
- }
- /**
- * @brief prints all possible commands with description and usage
- * @param[in] handle handle shellmatta instance handle
- * @param[in] arguments not used here
- * @param[in] length not used here
- * @return #SHELLMATTA_OK
- * #SHELLMATTA_ERROR (buffer overflow)
- */
- static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t *inst = (shellmatta_instance_t*) handle;
- shellmatta_cmd_t *cmd = inst->cmdList;
- size_t maxCmdLen = 0u;
- size_t maxCmdAliasLen = 0u;
- size_t maxCmdHelpLen = 0u;
- size_t cmdLen = 0u;
- size_t cmdAliasLen = 0u;
- size_t cmdHelpLen = 0u;
- uint32_t tabCnt = 0u;
- static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
- ' ', ' ', ' ', ' ',
- ' ', ' ', ' ', ' ',
- ' ', ' ', ' ', ' '};
- /** -# loop through all commands to determine the lengths of each cmd */
- while(NULL != cmd)
- {
- maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
- maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
- maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
- cmd = cmd->next;
- }
- /** -# loop through all commands and print all possible information */
- cmd = inst->cmdList;
- while(NULL != cmd)
- {
- /** -# determine the length of each field to add padding */
- cmdLen = strlen(cmd->cmd);
- cmdAliasLen = strlen(cmd->cmdAlias);
- cmdHelpLen = strlen(cmd->helpText);
- inst->write(cmd->cmd, strlen(cmd->cmd));
- tabCnt = (maxCmdLen - cmdLen) + 2u;
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
- inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
- tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
- inst->write(cmd->helpText, strlen(cmd->helpText));
- tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
- inst->write(cmd->usageText, strlen(cmd->usageText));
- inst->write("\r\n", 2u);
- cmd = cmd->next;
- }
- (void)arguments;
- (void)length;
- return ret;
- }
- shellmatta_cmd_t helpCmd = {"help", "h", "Print this help text", "help", helpCmdFct, NULL};
- /* interface functions */
- /**
- * @}
- * @addtogroup shellmatta_api
- * @{
- */
- /**
- * @brief initialize the shellmatta terminal and provide all callbacks
- * @param[in,out] inst pointer to a shellmatta instance
- * @param[out] handle pointer to shellmatta handle -
- * has to be used for all furterh api calls
- * @param[in] buffer pointer to the input buffer to use
- * @param[in] bufferSize size of the provided input buffer
- * @param[in] historyBuffer pointer to the history buffer to use
- * NULL in case of no history buffer
- * @param[in] historyBufferSize size of the history buffer
- * 0 in case of no history buffer
- * @param[in] prompt pointer to prompt string - is printed
- * after each command
- * @param[in] cmdList constant command list if no dynamic
- * adding of commands is desired
- * @param[in] writeFct function pointer to output function
- */
- 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)
- {
- /** -# check parameters for plausibility */
- if( (NULL != inst)
- && (NULL != handle)
- && (NULL != buffer)
- && (0u != bufferSize)
- && (NULL != prompt)
- && (NULL != writeFct)
- && ((NULL != historyBuffer) || (0u == historyBufferSize)))
- {
- /** -# copy all provided buffers into the shellmatta instance */
- inst->buffer = buffer;
- inst->bufferSize = bufferSize;
- inst->inputCount = 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->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;
- /** -# print the first prompt */
- terminateInput(inst);
- }
- return SHELLMATTA_OK;
- }
- /**
- * @brief adds a command to the command list alphabetically ordered
- * @param[in] handle shellmatta instance handle
- * @param[in] cmd pointer to the command to add type #shellmatta_cmd_t
- * @return errorcode #SHELLMATTA_OK
- * #SHELLMATTA_USE_FAULT (param err)
- * SHELLMATTA_DUPLICATE
- */
- 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;
- /** -# check parameters for plausibility */
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic)
- && (false == inst->cmdListIsConst))
- {
- tempCmd = inst->cmdList;
- prevCmd = &inst->cmdList;
- /** -# register first command as list entry */
- if (NULL == tempCmd)
- {
- inst->cmdList = cmd;
- cmd->next = NULL;
- }
- /** -# append the new command sorted */
- else
- {
- while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
- {
- cmdDiff = strcmp(tempCmd->cmd, cmd->cmd);
- aliasDiff = strcmp(tempCmd->cmdAlias, cmd->cmdAlias);
- /** -# check for a duplicate command */
- 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
- {
- /* nothing to do */
- }
- prevCmd = &tempCmd;
- tempCmd = tempCmd->next;
- }
- }
- }
- else
- {
- ret = SHELLMATTA_USE_FAULT;
- }
- return ret;
- }
- /**
- * @brief processes the passed amount of data
- * @param[in] handle shellmatta instance handle
- * @param[in] data pointer to input data to process
- * @param[in] size length of input data to process
- * @return errorcode #SHELLMATTA_OK
- * #SHELLMATTA_USE_FAULT
- */
- shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
- char *data,
- uint32_t size)
- {
- shellmatta_cmd_t *cmd;
- uint8_t cmdExecuted = 0u;
- shellmatta_retCode_t ret = SHELLMATTA_OK;
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
- /** -# check parameters for plausibility */
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
- /** -# process byte wise */
- for (uint32_t i = 0u; i < size; i++)
- {
- /** -# handle escape sequences */
- if(inst->escapeCounter != 0u)
- {
- handleEscapeSequence(inst, *data);
- }
- /** -# handle return as start of processing the command */
- else if ('\r' == *data)
- {
- cmd = inst->cmdList;
- inst->buffer[inst->inputCount] = 0u;
- /** -# store the current command and reset the history buffer */
- inst->dirty = true;
- storeHistoryCmd(inst);
- resetHistoryBuffer(inst);
- /** -# search for a matching command */
- while (NULL != cmd)
- {
- if ( (0 == strcmp(inst->buffer, cmd->cmd))
- || (0 == strcmp(inst->buffer, cmd->cmdAlias)))
- {
- inst->write("\r\n", 2u);
- cmdExecuted = 1u;
- cmd->cmdFct(inst, inst->buffer, inst->inputCount);
- cmd = NULL;
- }
- else
- {
- cmd = cmd->next;
- }
- }
- if ((cmdExecuted == 0u) && (inst->inputCount > 0))
- {
- inst->buffer[inst->inputCount] = '\0';
- inst->write("\r\nCommand: ", 11u);
- inst->write(inst->buffer, inst->inputCount);
- inst->write(" not found", 10u);
- }
- terminateInput(inst);
- }
- /** -# check for tabulator key - auto complete */
- else if('\t' == *data)
- {
- inst->dirty = true;
- doAutocomplete(inst);
- }
- /** -# check for cancel -
- * terminate current input and print prompt again */
- else if(3 == *data)
- {
- inst->dirty = false;
- resetHistoryBuffer(inst);
- terminateInput(inst);
- }
- /** -# check for backspace */
- else if('\b' == *data)
- {
- inst->dirty = true;
- removeChars(inst, 1u, true);
- }
- /** -# check for delete key */
- else if(0x7eu == *data)
- {
- inst->dirty = true;
- removeChars(inst, 1u, false);
- }
- /** -# check for start of escape sequence */
- else if('\e' == *data)
- {
- inst->escapeCounter = 1u;
- }
- else
- {
- inst->dirty = true;
- insertChars(inst, data, 1);
- }
- /** -# reset tab counter on not a tab */
- if ('\t' != *data)
- {
- inst->tabCounter = 0u;
- }
- data ++;
- }
- }
- else
- {
- ret = SHELLMATTA_USE_FAULT;
- }
- return ret;
- }
- /**
- * @brief simple write function to write a datastream to the output of an instance
- * @param[in] handle shellmatta instance handle
- * @param[in] data data to be written
- * @param[in] length amount of data to be written
- * @return
- */
- 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;
- /** -# check parameters for plausibility */
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
- /** -# pass the data to the write function of the instance */
- ret = inst->write(data, length);
- }
- return ret;
- }
- #ifndef SHELLMATTA_STRIP_PRINTF
- /**
- * @brief printf like function to print output to the instances output
- * @param[in] handle shellmatta instance handle
- * @param[in] fmt format string and parameters
- * @return errorcode #SHELLMATTA_OK
- * #SHELLMATTA_USE_FAULT (no valid instance)
- * #SHELLMATTA_ERROR (buffer overflow or format err)
- */
- 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;
- /** -# check parameters for plausibility */
- if( (NULL != inst)
- && (SHELLMATTA_MAGIC == inst->magic))
- {
- /** -# assemble output string and write it */
- 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
- /** @} */
- /** @} */
|