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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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"
|
2019-06-24 23:31:26 +02:00
|
|
|
#include "shellmatta_autocomplete.h"
|
|
|
|
#include "shellmatta_history.h"
|
|
|
|
#include "shellmatta_utils.h"
|
|
|
|
#include "shellmatta_escape.h"
|
2019-06-10 22:34:12 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2019-06-21 04:47:35 +02:00
|
|
|
#include <stdarg.h>
|
2019-06-24 23:31:26 +02:00
|
|
|
#include <stdio.h>
|
2019-06-23 22:17:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @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 */
|
2019-06-24 23:31:26 +02:00
|
|
|
utils_terminateInput(inst);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
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 */
|
|
|
|
}
|
2019-06-26 21:54:15 +02:00
|
|
|
prevCmd = &(tempCmd->next);
|
2019-06-23 22:17:15 +02:00
|
|
|
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;
|
2019-07-28 22:33:45 +02:00
|
|
|
uint32_t cmdLen;
|
2019-06-23 22:17:15 +02:00
|
|
|
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)
|
|
|
|
{
|
2019-06-24 23:31:26 +02:00
|
|
|
escape_handleSequence(inst, *data);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
2019-06-26 22:00:55 +02:00
|
|
|
/** -# ignore newline as first character (to be compatible to
|
|
|
|
* terminals sending newline after return */
|
|
|
|
else if((0u == inst->inputCount) && ('\n' == *data))
|
|
|
|
{
|
|
|
|
/* do nothing */
|
|
|
|
}
|
2019-06-23 22:17:15 +02:00
|
|
|
/** -# 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;
|
2019-06-24 23:31:26 +02:00
|
|
|
history_storeCmd(inst);
|
|
|
|
history_reset(inst);
|
2019-06-21 04:47:35 +02:00
|
|
|
|
2019-07-28 22:33:45 +02:00
|
|
|
/** -# determine the cmd len (chars until first space or \0 is found */
|
|
|
|
cmdLen = 0u;
|
|
|
|
while( (cmdLen < inst->inputCount)
|
|
|
|
&& (' ' != inst->buffer[cmdLen])
|
|
|
|
&& ('\0' != inst->buffer[cmdLen]))
|
|
|
|
{
|
|
|
|
cmdLen ++;
|
|
|
|
}
|
|
|
|
|
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-07-28 22:33:45 +02:00
|
|
|
/** -# compare command string and length */
|
|
|
|
if ( ((0 == strncmp( inst->buffer,
|
2019-06-27 22:57:29 +02:00
|
|
|
cmd->cmd,
|
2019-07-28 22:33:45 +02:00
|
|
|
cmdLen))
|
|
|
|
&& (cmdLen == strlen(cmd->cmd)))
|
|
|
|
|| ((0 == strncmp( inst->buffer,
|
2019-06-27 22:57:29 +02:00
|
|
|
cmd->cmdAlias,
|
2019-07-28 22:33:45 +02:00
|
|
|
cmdLen))
|
|
|
|
&& (cmdLen == strlen(cmd->cmdAlias))))
|
2019-06-23 22:17:15 +02:00
|
|
|
{
|
|
|
|
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-24 23:31:26 +02:00
|
|
|
utils_terminateInput(inst);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
|
|
|
/** -# check for tabulator key - auto complete */
|
|
|
|
else if('\t' == *data)
|
|
|
|
{
|
|
|
|
inst->dirty = true;
|
2019-06-24 23:31:26 +02:00
|
|
|
autocomplete_run(inst);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
|
|
|
/** -# check for cancel -
|
|
|
|
* terminate current input and print prompt again */
|
|
|
|
else if(3 == *data)
|
|
|
|
{
|
|
|
|
inst->dirty = false;
|
2019-06-24 23:31:26 +02:00
|
|
|
history_reset(inst);
|
|
|
|
utils_terminateInput(inst);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
|
|
|
/** -# check for backspace */
|
|
|
|
else if('\b' == *data)
|
|
|
|
{
|
|
|
|
inst->dirty = true;
|
2019-06-24 23:31:26 +02:00
|
|
|
utils_removeChars(inst, 1u, true);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
|
|
|
/** -# check for delete key */
|
|
|
|
else if(0x7eu == *data)
|
|
|
|
{
|
|
|
|
inst->dirty = true;
|
2019-06-24 23:31:26 +02:00
|
|
|
utils_removeChars(inst, 1u, false);
|
2019-06-23 22:17:15 +02:00
|
|
|
}
|
|
|
|
/** -# check for start of escape sequence */
|
|
|
|
else if('\e' == *data)
|
|
|
|
{
|
|
|
|
inst->escapeCounter = 1u;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inst->dirty = true;
|
2019-06-24 23:31:26 +02:00
|
|
|
utils_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
|
|
|
|