2019-06-16 22:03:27 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
|
|
|
|
*
|
2019-06-23 22:17:15 +02:00
|
|
|
* 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/.
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-06-23 22:17:15 +02:00
|
|
|
* @file shellmatta.c
|
|
|
|
* @brief Main implementation of the Shellmatta terminal implementation
|
|
|
|
* @author Stefan Strobel <stefan.strobel@shimatta.net>
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup shellmatta
|
|
|
|
* @{
|
2019-06-23 22:17:15 +02:00
|
|
|
* @addtogroup shellmatta_private
|
|
|
|
* @{
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
2019-06-10 22:34:12 +02:00
|
|
|
|
|
|
|
#include "shellmatta.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2019-06-21 04:47:35 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
/* defines */
|
|
|
|
|
|
|
|
#ifndef SHELLMATTA_OUTPUT_BUFFER_SIZE
|
|
|
|
#define SHELLMATTA_OUTPUT_BUFFER_SIZE 128u
|
|
|
|
#endif
|
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
#define SHELLMATTA_MIN(a,b) (((a) > (b)) ? (b) : (a))
|
2019-06-23 22:17:15 +02:00
|
|
|
#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
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/* 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)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
if(true == inst->echoEnabled)
|
|
|
|
{
|
|
|
|
inst->write(data, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:07:12 +02:00
|
|
|
/**
|
|
|
|
* @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);
|
2019-06-24 18:54:35 +02:00
|
|
|
tempBuffer[i] = (digitValue < 10u) ? ('0' + digitValue) : (('A' - 10) + digitValue);
|
2019-06-24 18:07:12 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# calculate the new history buffer index */
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->historyEnd ++;
|
|
|
|
if(inst->historyEnd >= inst->historyBufferSize)
|
|
|
|
{
|
|
|
|
inst->historyEnd = 0u;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# append the byte */
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->historyBuffer[inst->historyEnd] = byte;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if the we overwrite an existing stored command */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(inst->historyEnd == inst->historyStart)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# move the start pointer to the next termination (0) */
|
2019-06-21 04:47:35 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
inst->historyStart ++;
|
|
|
|
|
|
|
|
if(inst->historyStart >= inst->historyBufferSize)
|
|
|
|
{
|
|
|
|
inst->historyStart = 0u;
|
|
|
|
}
|
|
|
|
|
|
|
|
}while(0u != inst->historyBuffer[inst->historyStart]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if we have reached the end of the buffer already */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(inst->historyRead != inst->historyStart)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# read out one byte and decrease the read index */
|
2019-06-21 04:47:35 +02:00
|
|
|
*byte = inst->historyBuffer[inst->historyRead];
|
|
|
|
|
|
|
|
if(0u == inst->historyRead)
|
|
|
|
{
|
|
|
|
inst->historyRead = inst->historyBufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
inst->historyRead --;
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief stores the current command from the instances buffer into the
|
|
|
|
* history buffer
|
|
|
|
* @param[in] inst pointer to a shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void storeHistoryCmd(shellmatta_instance_t *inst)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
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 */
|
2019-06-21 04:47:35 +02:00
|
|
|
if( (inst->historyBufferSize > inst->inputCount)
|
|
|
|
&& (0u != inst->inputCount)
|
|
|
|
&& (true == inst->dirty))
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# append the command termination */
|
2019-06-21 04:47:35 +02:00
|
|
|
appendHistoryByte(inst, 0u);
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# append the command byte wise in reverse direction */
|
|
|
|
for(i = inst->inputCount; i > 0u; i --)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
appendHistoryByte(inst, inst->buffer[i - 1u]);
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
/** -# remove the dirty flag - everything is nice and saved */
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->dirty = false;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief restores the command from the history buffer where the read
|
|
|
|
* index points on
|
|
|
|
* @param[in] inst pointer to a shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void restoreHistoryCmd(shellmatta_instance_t *inst)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
char byte;
|
2019-06-21 04:47:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief resets the history buffer pointers to show to the most recent
|
|
|
|
* command again
|
|
|
|
* @param[in] inst pointer to a shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void resetHistoryBuffer(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
inst->historyRead = inst->historyEnd;
|
|
|
|
inst->historyReadUp = true;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief tells the terminal to save the current cursor position
|
|
|
|
* @param[in] inst pointer to shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void saveCursorPos(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
writeEcho(inst, "\e[s", 3u);
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief tells the terminal to restore the saved cursor position
|
|
|
|
* @param[in] inst pointer to shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void restoreCursorPos(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
writeEcho(inst, "\e[u", 3u);
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief tells the terminal to erase the line on the right side of the
|
|
|
|
* cursor
|
|
|
|
* @param[in] inst pointer to shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void eraseLine(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
writeEcho(inst, "\e[K", 3u);
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void rewindCursor(shellmatta_instance_t *inst, uint32_t length)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
char terminalCmd[16];
|
|
|
|
size_t size;
|
2019-06-21 04:47:35 +02:00
|
|
|
|
|
|
|
length = SHELLMATTA_MIN (length, inst->cursor);
|
|
|
|
if(length > 0u)
|
|
|
|
{
|
2019-06-24 18:07:12 +02:00
|
|
|
terminalCmd[0] = '\e';
|
|
|
|
terminalCmd[1] = '[';
|
|
|
|
size = 2u + shellItoa(length, &terminalCmd[2], 10);
|
|
|
|
terminalCmd[size] = 'D';
|
|
|
|
writeEcho(inst, terminalCmd, size + 1u);
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->cursor -= length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void forwardCursor(shellmatta_instance_t *inst, uint32_t length)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
char terminalCmd[16];
|
2019-06-21 04:47:35 +02:00
|
|
|
size_t size;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor));
|
2019-06-21 04:47:35 +02:00
|
|
|
if (length > 0u)
|
|
|
|
{
|
2019-06-24 18:07:12 +02:00
|
|
|
terminalCmd[0] = '\e';
|
|
|
|
terminalCmd[1] = '[';
|
|
|
|
size = 2u + shellItoa(length, &terminalCmd[2], 10);
|
|
|
|
terminalCmd[size] = 'C';
|
|
|
|
writeEcho(inst, terminalCmd, size + 1u);
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->cursor += length;
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void insertChars(shellmatta_instance_t *inst,
|
2019-06-23 22:17:15 +02:00
|
|
|
char *data,
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t length)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
if(0u != length)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if we have to move chars in the buffer */
|
|
|
|
if( (inst->cursor != inst->inputCount)
|
|
|
|
&& (SHELLMATTA_MODE_INSERT == inst->mode))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# move the existing chars */
|
|
|
|
for ( uint32_t i = inst->inputCount;
|
|
|
|
i > inst->cursor;
|
|
|
|
i --)
|
|
|
|
{
|
|
|
|
inst->buffer[i + length - 1] = inst->buffer[i - 1];
|
|
|
|
}
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# store and print the new chars */
|
|
|
|
memcpy(&(inst->buffer[inst->cursor]), data, length);
|
|
|
|
writeEcho(inst, data, length);
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# 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);
|
|
|
|
}
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->inputCount += length;
|
|
|
|
inst->cursor += length;
|
|
|
|
}
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void removeChars(shellmatta_instance_t *inst,
|
|
|
|
uint32_t length,
|
|
|
|
bool backspace)
|
|
|
|
{
|
|
|
|
if(0u != length)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# rewind the cursor in case of backspace */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(true == backspace)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
length = SHELLMATTA_MIN (length, inst->cursor);
|
2019-06-21 04:47:35 +02:00
|
|
|
rewindCursor(inst, length);
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
length = SHELLMATTA_MIN (length, inst->inputCount - inst->cursor);
|
|
|
|
}
|
|
|
|
/** -# delete the char at the cursor position */
|
2019-06-21 04:47:35 +02:00
|
|
|
for ( uint32_t i = inst->cursor;
|
|
|
|
i < (inst->inputCount - length);
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
inst->buffer[i] = inst->buffer[i + length];
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
/** -# print the rest of the line again */
|
2019-06-21 04:47:35 +02:00
|
|
|
eraseLine(inst);
|
|
|
|
saveCursorPos(inst);
|
|
|
|
writeEcho( inst,
|
|
|
|
&(inst->buffer[inst->cursor]),
|
|
|
|
(inst->inputCount - inst->cursor - length));
|
|
|
|
restoreCursorPos(inst);
|
|
|
|
|
|
|
|
inst->inputCount -= length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief clears the input buffer and removes the currend command from
|
|
|
|
* the terminal output
|
|
|
|
* @param[in] inst pointer to a shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void clearInput(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
rewindCursor(inst, inst->cursor);
|
|
|
|
eraseLine(inst);
|
|
|
|
inst->inputCount = 0u;
|
|
|
|
inst->dirty = false;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
switch (inst->escapeCounter)
|
|
|
|
{
|
|
|
|
case 1u:
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->escapeChars[inst->escapeCounter - 1] = data;
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->escapeCounter ++;
|
|
|
|
break;
|
|
|
|
case 2u:
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->escapeChars[inst->escapeCounter - 1] = data;
|
|
|
|
/** -# check if an arrow key was pressed */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(SHELLMATTA_OK == processArrowKeys(inst))
|
|
|
|
{
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if end was pressed */
|
|
|
|
else if ( (0x4f == inst->escapeChars[0])
|
|
|
|
&& (0x46 == inst->escapeChars[1]))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
forwardCursor(inst, inst->inputCount - inst->cursor);
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# 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))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
inst->escapeCounter ++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3u:
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if delete was pressed */
|
|
|
|
inst->escapeChars[inst->escapeCounter - 1] = data;
|
|
|
|
if( (0x5b == inst->escapeChars[0])
|
|
|
|
&& (0x33 == inst->escapeChars[1])
|
|
|
|
&& (0x7e == inst->escapeChars[2]))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
removeChars(inst, 1u, false);
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if pos1 was pressed */
|
2019-06-21 04:47:35 +02:00
|
|
|
else if ( (0x5bu == inst->escapeChars[0])
|
|
|
|
&& (0x31u == inst->escapeChars[1])
|
|
|
|
&& (0x7eu == inst->escapeChars[2]))
|
|
|
|
{
|
|
|
|
rewindCursor(inst, inst->cursor);
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
else if(0u == (0x40u & data))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
|
|
|
inst->escapeCounter ++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
inst->escapeCounter = 0u;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @brief terminates an input and prints the prompt again
|
|
|
|
* @param[in] inst pointer to a shellmatta instance
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void terminateInput(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
inst->inputCount = 0u;
|
|
|
|
inst->cursor = 0u;
|
2019-06-24 16:51:23 +02:00
|
|
|
inst->write("\r\n", 2u);
|
|
|
|
inst->write(inst->prompt, strlen(inst->prompt));
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
static void doAutocomplete(shellmatta_instance_t *inst)
|
|
|
|
{
|
|
|
|
shellmatta_cmd_t *cmd = inst->cmdList;
|
2019-06-23 22:17:15 +02:00
|
|
|
char *tempCmd = NULL;
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t minLen = 0u;
|
|
|
|
uint32_t sizeDiff = 0u;
|
|
|
|
bool exactMatch = true;
|
|
|
|
uint32_t printedLen = 0u;
|
2019-06-23 22:17:15 +02:00
|
|
|
uint32_t tempCursor = 0u;
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# increase the tab counter to print all matching commands next time */
|
2019-06-21 04:47:35 +02:00
|
|
|
inst->tabCounter++;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# on douple tab show all matching commands */
|
2019-06-21 04:47:35 +02:00
|
|
|
if (1u < inst->tabCounter)
|
|
|
|
{
|
|
|
|
inst->tabCounter = 0u;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# loop through all registered commands */
|
2019-06-21 04:47:35 +02:00
|
|
|
while (NULL != cmd)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if command matches the input */
|
|
|
|
if( (strlen(cmd->cmd) >= inst->cursor)
|
2019-06-21 04:47:35 +02:00
|
|
|
&& (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# add newline on first find */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(0u == printedLen)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->write("\r\n", 2u);
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
inst->write(cmd->cmd, strlen(cmd->cmd));
|
|
|
|
printedLen += strlen(cmd->cmd);
|
|
|
|
inst->write(" ", 4u);
|
|
|
|
printedLen += 4u;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if command alias matches the input */
|
|
|
|
if( (strlen(cmd->cmdAlias) >= inst->cursor)
|
2019-06-21 04:47:35 +02:00
|
|
|
&& (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# add newline on first find */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(0u == printedLen)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->write("\r\n", 2u);
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
|
|
|
|
printedLen += strlen(cmd->cmdAlias);
|
|
|
|
inst->write(" ", 4u);
|
|
|
|
printedLen += 4u;
|
|
|
|
}
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# print input again if a commands was found */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(printedLen > 0u)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
writeEcho(inst, "\r\n", 2u);
|
2019-06-21 04:47:35 +02:00
|
|
|
writeEcho(inst, inst->prompt, strlen(inst->prompt));
|
|
|
|
writeEcho(inst, inst->buffer, inst->inputCount);
|
2019-06-23 22:17:15 +02:00
|
|
|
tempCursor = inst->cursor;
|
|
|
|
inst->cursor = inst->inputCount;
|
|
|
|
rewindCursor(inst, inst->inputCount - tempCursor);
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# on single tab autocomplete as far as possible */
|
2019-06-21 04:47:35 +02:00
|
|
|
else if(0u != inst->cursor)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# loop through all registered commands */
|
2019-06-21 04:47:35 +02:00
|
|
|
while (NULL != cmd)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if command matches the input */
|
|
|
|
if( (strlen(cmd->cmd) >= inst->cursor)
|
2019-06-21 04:47:35 +02:00
|
|
|
&& (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor)))
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# store first match */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(NULL == tempCmd)
|
|
|
|
{
|
|
|
|
tempCmd = cmd->cmd;
|
|
|
|
minLen = strlen(cmd->cmd);
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# find common part of the matching commands */
|
2019-06-21 04:47:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check if command Alias matches the input */
|
|
|
|
if( (strlen(cmd->cmdAlias) >= inst->cursor)
|
2019-06-21 04:47:35 +02:00
|
|
|
&& (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor)))
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# store first match */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(NULL == tempCmd)
|
|
|
|
{
|
|
|
|
tempCmd = cmd->cmdAlias;
|
|
|
|
minLen = strlen(cmd->cmdAlias);
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# find common part of the matches */
|
2019-06-21 04:47:35 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# autocomplete found command */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(NULL != tempCmd)
|
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# calculate the size of the command to be inserted */
|
2019-06-21 04:47:35 +02:00
|
|
|
sizeDiff = minLen - inst->cursor;
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# copy the found part into the buffer and display it */
|
2019-06-21 04:47:35 +02:00
|
|
|
insertChars(inst, &(tempCmd[inst->cursor]), sizeDiff);
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# on exact match there is no need to double Tab to display all */
|
2019-06-21 04:47:35 +02:00
|
|
|
if(true == exactMatch)
|
|
|
|
{
|
|
|
|
inst->tabCounter = 0u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* nothing to do here */
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-16 22:03:27 +02:00
|
|
|
/**
|
2019-06-23 22:17:15 +02:00
|
|
|
* @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);
|
|
|
|
|
2019-06-24 16:51:23 +02:00
|
|
|
inst->write(cmd->cmd, strlen(cmd->cmd));
|
|
|
|
tabCnt = (maxCmdLen - cmdLen) + 2u;
|
2019-06-23 22:17:15 +02:00
|
|
|
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
2019-06-24 16:51:23 +02:00
|
|
|
|
|
|
|
inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias));
|
|
|
|
tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
|
2019-06-23 22:17:15 +02:00
|
|
|
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
2019-06-24 16:51:23 +02:00
|
|
|
|
|
|
|
inst->write(cmd->helpText, strlen(cmd->helpText));
|
|
|
|
tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
|
2019-06-23 22:17:15 +02:00
|
|
|
SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
2019-06-24 16:51:23 +02:00
|
|
|
|
|
|
|
inst->write(cmd->usageText, strlen(cmd->usageText));
|
|
|
|
inst->write("\r\n", 2u);
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
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
|
2019-06-16 22:03:27 +02:00
|
|
|
* @param[in,out] inst pointer to a shellmatta instance
|
2019-06-23 22:17:15 +02:00
|
|
|
* @param[out] handle pointer to shellmatta handle -
|
|
|
|
* has to be used for all furterh api calls
|
2019-06-16 22:03:27 +02:00
|
|
|
* @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
|
2019-06-21 04:47:35 +02:00
|
|
|
* @param[in] prompt pointer to prompt string - is printed
|
|
|
|
* after each command
|
2019-06-23 22:17:15 +02:00
|
|
|
* @param[in] cmdList constant command list if no dynamic
|
|
|
|
* adding of commands is desired
|
2019-06-21 04:47:35 +02:00
|
|
|
* @param[in] writeFct function pointer to output function
|
2019-06-16 22:03:27 +02:00
|
|
|
*/
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_retCode_t shellmatta_doInit(
|
|
|
|
shellmatta_instance_t *inst,
|
2019-06-23 22:17:15 +02:00
|
|
|
shellmatta_handle_t *handle,
|
|
|
|
char *buffer,
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t bufferSize,
|
2019-06-23 22:17:15 +02:00
|
|
|
char *historyBuffer,
|
2019-06-21 04:47:35 +02:00
|
|
|
uint32_t historyBufferSize,
|
|
|
|
const char *prompt,
|
2019-06-23 22:17:15 +02:00
|
|
|
const shellmatta_cmd_t *cmdList,
|
2019-06-21 04:47:35 +02:00
|
|
|
shellmatta_write_t writeFct)
|
2019-06-10 22:34:12 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# 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);
|
|
|
|
}
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
2019-06-16 22:03:27 +02:00
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
/**
|
2019-06-23 22:17:15 +02:00
|
|
|
* @brief adds a command to the command list alphabetically ordered
|
|
|
|
* @param[in] handle shellmatta instance handle
|
2019-06-21 04:47:35 +02:00
|
|
|
* @param[in] cmd pointer to the command to add type #shellmatta_cmd_t
|
2019-06-23 22:17:15 +02:00
|
|
|
* @return errorcode #SHELLMATTA_OK
|
|
|
|
* #SHELLMATTA_USE_FAULT (param err)
|
|
|
|
* SHELLMATTA_DUPLICATE
|
2019-06-21 04:47:35 +02:00
|
|
|
*/
|
2019-06-23 22:17:15 +02:00
|
|
|
shellmatta_retCode_t shellmatta_addCmd(shellmatta_handle_t handle, shellmatta_cmd_t *cmd)
|
2019-06-10 22:34:12 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
|
shellmatta_cmd_t *tempCmd;
|
|
|
|
shellmatta_cmd_t **prevCmd;
|
2019-06-21 04:47:35 +02:00
|
|
|
bool cmdPlaced = false;
|
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
|
|
int cmdDiff = 0;
|
|
|
|
int aliasDiff = 0;
|
2019-06-16 22:03:27 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# check parameters for plausibility */
|
|
|
|
if( (NULL != inst)
|
|
|
|
&& (SHELLMATTA_MAGIC == inst->magic)
|
|
|
|
&& (false == inst->cmdListIsConst))
|
2019-06-16 22:03:27 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
tempCmd = inst->cmdList;
|
|
|
|
prevCmd = &inst->cmdList;
|
|
|
|
/** -# register first command as list entry */
|
|
|
|
if (NULL == tempCmd)
|
2019-06-16 22:03:27 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->cmdList = cmd;
|
|
|
|
cmd->next = NULL;
|
|
|
|
}
|
|
|
|
/** -# append the new command sorted */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while ((false == cmdPlaced) && (SHELLMATTA_OK == ret))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
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;
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
2019-06-16 22:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = SHELLMATTA_USE_FAULT;
|
|
|
|
}
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
return ret;
|
2019-06-10 22:34:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-21 04:47:35 +02:00
|
|
|
/**
|
2019-06-23 22:17:15 +02:00
|
|
|
* @brief processes the passed amount of data
|
|
|
|
* @param[in] handle shellmatta instance handle
|
2019-06-21 04:47:35 +02:00
|
|
|
* @param[in] data pointer to input data to process
|
|
|
|
* @param[in] size length of input data to process
|
2019-06-23 22:17:15 +02:00
|
|
|
* @return errorcode #SHELLMATTA_OK
|
|
|
|
* #SHELLMATTA_USE_FAULT
|
2019-06-21 04:47:35 +02:00
|
|
|
*/
|
2019-06-23 22:17:15 +02:00
|
|
|
shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle,
|
|
|
|
char *data,
|
|
|
|
uint32_t size)
|
2019-06-10 22:34:12 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
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))
|
2019-06-16 22:03:27 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
/** -# process byte wise */
|
|
|
|
for (uint32_t i = 0u; i < size; i++)
|
2019-06-16 22:03:27 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# 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;
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# store the current command and reset the history buffer */
|
|
|
|
inst->dirty = true;
|
|
|
|
storeHistoryCmd(inst);
|
|
|
|
resetHistoryBuffer(inst);
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# search for a matching command */
|
|
|
|
while (NULL != cmd)
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
if ( (0 == strcmp(inst->buffer, cmd->cmd))
|
|
|
|
|| (0 == strcmp(inst->buffer, cmd->cmdAlias)))
|
|
|
|
{
|
|
|
|
inst->write("\r\n", 2u);
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
cmdExecuted = 1u;
|
|
|
|
cmd->cmdFct(inst, inst->buffer, inst->inputCount);
|
|
|
|
cmd = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd = cmd->next;
|
|
|
|
}
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
if ((cmdExecuted == 0u) && (inst->inputCount > 0))
|
2019-06-21 04:47:35 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->buffer[inst->inputCount] = '\0';
|
2019-06-24 16:51:23 +02:00
|
|
|
inst->write("\r\nCommand: ", 11u);
|
|
|
|
inst->write(inst->buffer, inst->inputCount);
|
|
|
|
inst->write(" not found", 10u);
|
2019-06-21 04:47:35 +02:00
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
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);
|
2019-06-16 22:03:27 +02:00
|
|
|
}
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# reset tab counter on not a tab */
|
|
|
|
if ('\t' != *data)
|
2019-06-16 22:03:27 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
inst->tabCounter = 0u;
|
2019-06-16 22:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
data ++;
|
2019-06-16 22:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = SHELLMATTA_USE_FAULT;
|
|
|
|
}
|
|
|
|
return ret;
|
2019-06-10 22:34:12 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 17:11:22 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2019-06-24 16:51:23 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-06-24 17:11:22 +02:00
|
|
|
/** -# pass the data to the write function of the instance */
|
2019-06-24 16:51:23 +02:00
|
|
|
ret = inst->write(data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SHELLMATTA_STRIP_PRINTF
|
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @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,
|
|
|
|
...)
|
2019-06-10 22:34:12 +02:00
|
|
|
{
|
2019-06-23 22:17:15 +02:00
|
|
|
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))
|
|
|
|
{
|
2019-06-24 17:11:22 +02:00
|
|
|
/** -# assemble output string and write it */
|
2019-06-23 22:17:15 +02:00
|
|
|
va_start(arg, fmt);
|
|
|
|
length = vsnprintf(outputBuffer, SHELLMATTA_OUTPUT_BUFFER_SIZE, fmt, arg);
|
|
|
|
va_end(arg);
|
2019-06-10 22:34:12 +02:00
|
|
|
|
2019-06-23 22:17:15 +02:00
|
|
|
if(length < 0)
|
|
|
|
{
|
|
|
|
ret = SHELLMATTA_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst->write(outputBuffer, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = SHELLMATTA_USE_FAULT;
|
|
|
|
}
|
|
|
|
return ret;
|
2019-06-10 22:34:12 +02:00
|
|
|
}
|
2019-06-24 16:51:23 +02:00
|
|
|
#endif
|
2019-06-16 22:03:27 +02:00
|
|
|
|
|
|
|
/** @} */
|
2019-06-23 22:17:15 +02:00
|
|
|
/** @} */
|
|
|
|
|