|
@@ -54,14 +54,14 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base)
|
|
int8_t digitValue;
|
|
int8_t digitValue;
|
|
|
|
|
|
/** -# check the base for plausibility */
|
|
/** -# check the base for plausibility */
|
|
- if((2 <= base) && (16 >= base))
|
|
|
|
|
|
+ if((base >= 2) && (base <= 16))
|
|
{
|
|
{
|
|
/** -# check for sign */
|
|
/** -# check for sign */
|
|
- if(0 > value)
|
|
|
|
|
|
+ if(value < 0)
|
|
{
|
|
{
|
|
- value = value * (-1);
|
|
|
|
- buffer[0u] = '-';
|
|
|
|
- bufferIdx += 1u;
|
|
|
|
|
|
+ value = value * (-1);
|
|
|
|
+ buffer[0u] = '-';
|
|
|
|
+ bufferIdx += 1u;
|
|
}
|
|
}
|
|
|
|
|
|
/** -# loop through all digits in reverse order */
|
|
/** -# loop through all digits in reverse order */
|
|
@@ -162,46 +162,51 @@ void utils_forwardCursor(shellmatta_instance_t *inst, uint32_t length)
|
|
* @param[in] inst pointer to shellmatta instance
|
|
* @param[in] inst pointer to shellmatta instance
|
|
* @param[in] data pointer to the data to be inserted
|
|
* @param[in] data pointer to the data to be inserted
|
|
* @param[in] length length of the data to be inserted
|
|
* @param[in] length length of the data to be inserted
|
|
- * @todo this function shall check buffer overflows
|
|
|
|
*/
|
|
*/
|
|
void utils_insertChars( shellmatta_instance_t *inst,
|
|
void utils_insertChars( shellmatta_instance_t *inst,
|
|
char *data,
|
|
char *data,
|
|
uint32_t length)
|
|
uint32_t length)
|
|
{
|
|
{
|
|
- if(0u != length)
|
|
|
|
|
|
+ uint32_t tmpLength = length;
|
|
|
|
+ /** -# limit the length to the space left in the buffer */
|
|
|
|
+ if((inst->inputCount + tmpLength) > inst->bufferSize)
|
|
{
|
|
{
|
|
|
|
+ tmpLength = inst->bufferSize - inst->inputCount;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(0u != tmpLength)
|
|
|
|
+ {
|
|
|
|
+
|
|
/** -# check if we have to move chars in the buffer */
|
|
/** -# check if we have to move chars in the buffer */
|
|
if( (inst->cursor != inst->inputCount)
|
|
if( (inst->cursor != inst->inputCount)
|
|
&& (SHELLMATTA_MODE_INSERT == inst->mode))
|
|
&& (SHELLMATTA_MODE_INSERT == inst->mode))
|
|
{
|
|
{
|
|
/** -# move the existing chars */
|
|
/** -# move the existing chars */
|
|
- for ( uint32_t i = inst->inputCount;
|
|
|
|
- i > inst->cursor;
|
|
|
|
- i --)
|
|
|
|
|
|
+ for (uint32_t i = inst->inputCount; i > inst->cursor; i --)
|
|
{
|
|
{
|
|
- inst->buffer[i + length - 1] = inst->buffer[i - 1];
|
|
|
|
|
|
+ inst->buffer[i + tmpLength - 1] = inst->buffer[i - 1];
|
|
}
|
|
}
|
|
|
|
|
|
/** -# store and print the new chars */
|
|
/** -# store and print the new chars */
|
|
- memcpy(&(inst->buffer[inst->cursor]), data, length);
|
|
|
|
- utils_writeEcho(inst, data, length);
|
|
|
|
|
|
+ memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
|
|
|
|
+ utils_writeEcho(inst, data, tmpLength);
|
|
|
|
|
|
/** -# print the other chars and restore the cursor to this position */
|
|
/** -# print the other chars and restore the cursor to this position */
|
|
utils_eraseLine(inst);
|
|
utils_eraseLine(inst);
|
|
utils_saveCursorPos(inst);
|
|
utils_saveCursorPos(inst);
|
|
utils_writeEcho( inst,
|
|
utils_writeEcho( inst,
|
|
- &(inst->buffer[inst->cursor + length]),
|
|
|
|
|
|
+ &(inst->buffer[inst->cursor + tmpLength]),
|
|
inst->inputCount - inst->cursor);
|
|
inst->inputCount - inst->cursor);
|
|
utils_restoreCursorPos(inst);
|
|
utils_restoreCursorPos(inst);
|
|
- inst->cursor += length;
|
|
|
|
- inst->inputCount += length;
|
|
|
|
|
|
+ inst->cursor += tmpLength;
|
|
|
|
+ inst->inputCount += tmpLength;
|
|
}
|
|
}
|
|
/** -# overwrite - if the cursor reaches the end of the input it is pushed further */
|
|
/** -# overwrite - if the cursor reaches the end of the input it is pushed further */
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- memcpy(&(inst->buffer[inst->cursor]), data, length);
|
|
|
|
- utils_writeEcho(inst, data, length);
|
|
|
|
- inst->cursor += length;
|
|
|
|
|
|
+ memcpy(&(inst->buffer[inst->cursor]), data, tmpLength);
|
|
|
|
+ utils_writeEcho(inst, data, tmpLength);
|
|
|
|
+ inst->cursor += tmpLength;
|
|
if(inst->cursor > inst->inputCount)
|
|
if(inst->cursor > inst->inputCount)
|
|
{
|
|
{
|
|
inst->inputCount = inst->cursor;
|
|
inst->inputCount = inst->cursor;
|
|
@@ -267,11 +272,48 @@ void utils_clearInput(shellmatta_instance_t *inst)
|
|
inst->dirty = false;
|
|
inst->dirty = false;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * @brief prints the usage information of one passed command
|
|
|
|
+ * @param[in] inst handle shellmatta instance handle
|
|
|
|
+ * @param[in] cmd pointer to a command structure to print
|
|
|
|
+ * @return #SHELLMATTA_OK
|
|
|
|
+ * #SHELLMATTA_ERROR
|
|
|
|
+ */
|
|
|
|
+static shellmatta_retCode_t printUsage(const shellmatta_instance_t *inst, const shellmatta_cmd_t *cmd)
|
|
|
|
+{
|
|
|
|
+ shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
|
|
+
|
|
|
|
+ /** -# write the command and alias if configured */
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write("Help for command: ", 18u));
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->cmd, strlen(cmd->cmd)));
|
|
|
|
+ if((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(" (", 2u));
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->cmdAlias, strlen(cmd->cmdAlias)));
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(")", 1u));
|
|
|
|
+ }
|
|
|
|
+ /** -# write the help text if configured */
|
|
|
|
+ if((NULL != cmd->helpText) && (0u != strlen(cmd->helpText)))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write("\r\n\r\n", 4u));
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->helpText, strlen(cmd->helpText)));
|
|
|
|
+ }
|
|
|
|
+ /** -# write the usage text if configured */
|
|
|
|
+ if((NULL != cmd->usageText) && (0u != strlen(cmd->usageText)))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write("\r\n\r\nUsage: \r\n", 13u));
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->usageText, strlen(cmd->usageText)));
|
|
|
|
+ }
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write("\r\n", 2u));
|
|
|
|
+
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @brief prints all possible commands with description and usage
|
|
* @brief prints all possible commands with description and usage
|
|
* @param[in] handle handle shellmatta instance handle
|
|
* @param[in] handle handle shellmatta instance handle
|
|
- * @param[in] arguments not used here
|
|
|
|
- * @param[in] length not used here
|
|
|
|
|
|
+ * @param[in] arguments arguments containing a command name or alias
|
|
|
|
+ * @param[in] length length of the arguments
|
|
* @return #SHELLMATTA_OK
|
|
* @return #SHELLMATTA_OK
|
|
* #SHELLMATTA_ERROR (buffer overflow)
|
|
* #SHELLMATTA_ERROR (buffer overflow)
|
|
*/
|
|
*/
|
|
@@ -279,72 +321,113 @@ static shellmatta_retCode_t helpCmdFct(const shellmatta_handle_t handle, const c
|
|
{
|
|
{
|
|
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
shellmatta_retCode_t ret = SHELLMATTA_OK;
|
|
const shellmatta_instance_t *inst = (const shellmatta_instance_t*) handle;
|
|
const shellmatta_instance_t *inst = (const shellmatta_instance_t*) handle;
|
|
- shellmatta_cmd_t *cmd = inst->cmdList;
|
|
|
|
|
|
+ shellmatta_cmd_t *cmd = NULL;
|
|
size_t maxCmdLen = 0u;
|
|
size_t maxCmdLen = 0u;
|
|
size_t maxCmdAliasLen = 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;
|
|
|
|
|
|
+ size_t cmdLen = 0u;
|
|
|
|
+ const char *subCmd = NULL;
|
|
|
|
+ size_t cmdAliasLen;
|
|
|
|
+ uint32_t tabCnt;
|
|
|
|
+ uint32_t i;
|
|
static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
|
|
static const char tabBuffer[] = { ' ', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' '};
|
|
' ', ' ', ' ', ' '};
|
|
|
|
|
|
- /** -# loop through all commands to determine the lengths of each cmd */
|
|
|
|
- while(NULL != cmd)
|
|
|
|
|
|
+ /** -# check if help is called with a command - find first space */
|
|
|
|
+ for(i = 1u; i < length; i ++)
|
|
{
|
|
{
|
|
- maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
|
|
|
|
- if(NULL != cmd->cmdAlias)
|
|
|
|
- {
|
|
|
|
- maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
|
|
|
|
- }
|
|
|
|
- if(NULL != cmd->helpText)
|
|
|
|
|
|
+ if(' ' == arguments[i - 1])
|
|
{
|
|
{
|
|
- maxCmdHelpLen = SHELLMATTA_MAX(maxCmdHelpLen, strlen(cmd->helpText));
|
|
|
|
|
|
+ subCmd = &(arguments[i]);
|
|
|
|
+
|
|
|
|
+ /** -# determine subcommand length*/
|
|
|
|
+ cmdLen = 0u;
|
|
|
|
+ while( ((i + cmdLen) < length)
|
|
|
|
+ && (' ' != arguments[i + cmdLen])
|
|
|
|
+ && ('\r' != arguments[i + cmdLen])
|
|
|
|
+ && ('\n' != arguments[i + cmdLen])
|
|
|
|
+ && ('\0' != arguments[i + cmdLen]))
|
|
|
|
+ {
|
|
|
|
+ cmdLen ++;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- cmd = cmd->next;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- /** -# loop through all commands and print all possible information */
|
|
|
|
- cmd = inst->cmdList;
|
|
|
|
- while(NULL != cmd)
|
|
|
|
|
|
+ /* print detailled help */
|
|
|
|
+ if(NULL != subCmd)
|
|
{
|
|
{
|
|
- /** -# determine the length of each field to add padding */
|
|
|
|
- cmdLen = strlen(cmd->cmd);
|
|
|
|
- cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
|
|
|
|
- cmdHelpLen = (NULL != cmd->helpText) ? strlen(cmd->helpText) : 0u;
|
|
|
|
|
|
+ cmd = inst->cmdList;
|
|
|
|
|
|
- inst->write(cmd->cmd, strlen(cmd->cmd));
|
|
|
|
- tabCnt = (maxCmdLen - cmdLen) + 2u;
|
|
|
|
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
|
|
|
-
|
|
|
|
- if(NULL != cmd->cmdAlias)
|
|
|
|
|
|
+ /** -# search for a matching command */
|
|
|
|
+ while (NULL != cmd)
|
|
{
|
|
{
|
|
- inst->write(cmd->cmdAlias, cmdAliasLen);
|
|
|
|
|
|
+ /** -# compare command and alias string and length */
|
|
|
|
+ if ( ((cmdLen == strlen(cmd->cmd))
|
|
|
|
+ && (0 == strncmp(subCmd, cmd->cmd, cmdLen)))
|
|
|
|
+ || ((NULL != cmd->cmdAlias)
|
|
|
|
+ && (cmdLen == strlen(cmd->cmdAlias))
|
|
|
|
+ && (0 == strncmp(subCmd, cmd->cmdAlias, cmdLen))))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_RET(ret, printUsage(inst, cmd));
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cmd = cmd->next;
|
|
}
|
|
}
|
|
- tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
|
|
|
|
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- if(NULL != cmd->helpText)
|
|
|
|
|
|
+ /** -# print help list if no sub cmd was found */
|
|
|
|
+ if(NULL == cmd)
|
|
|
|
+ {
|
|
|
|
+ /** -# loop through all commands to determine the lengths of each cmd */
|
|
|
|
+ cmd = inst->cmdList;
|
|
|
|
+ while(NULL != cmd)
|
|
{
|
|
{
|
|
- inst->write(cmd->helpText, cmdHelpLen);
|
|
|
|
|
|
+ maxCmdLen = SHELLMATTA_MAX(maxCmdLen, strlen(cmd->cmd));
|
|
|
|
+ if(NULL != cmd->cmdAlias)
|
|
|
|
+ {
|
|
|
|
+ maxCmdAliasLen = SHELLMATTA_MAX(maxCmdAliasLen, strlen(cmd->cmdAlias));
|
|
|
|
+ }
|
|
|
|
+ cmd = cmd->next;
|
|
}
|
|
}
|
|
- tabCnt = (maxCmdHelpLen - cmdHelpLen) + 2u;
|
|
|
|
- SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
|
|
|
|
|
|
|
- if(NULL != cmd->usageText)
|
|
|
|
|
|
+ /** -# loop through all commands and print all possible information */
|
|
|
|
+ cmd = inst->cmdList;
|
|
|
|
+ while(NULL != cmd)
|
|
{
|
|
{
|
|
- inst->write(cmd->usageText, strlen(cmd->usageText));
|
|
|
|
- }
|
|
|
|
- inst->write("\r\n", 2u);
|
|
|
|
|
|
+ /** -# determine the length of each field to add padding */
|
|
|
|
+ cmdLen = strlen(cmd->cmd);
|
|
|
|
+ cmdAliasLen = (NULL != cmd->cmdAlias) ? strlen(cmd->cmdAlias) : 0u;
|
|
|
|
|
|
- cmd = cmd->next;
|
|
|
|
- }
|
|
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->cmd, strlen(cmd->cmd)));
|
|
|
|
+ tabCnt = (maxCmdLen - cmdLen) + 2u;
|
|
|
|
+
|
|
|
|
+ /** -# add padding if there is anything to be printed afterwards */
|
|
|
|
+ if( ((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
|
|
|
|
+ || ((NULL != cmd->helpText) && (0u != strlen(cmd->helpText))))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if((NULL != cmd->cmdAlias) && (0u != strlen(cmd->cmdAlias)))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->cmdAlias, cmdAliasLen));
|
|
|
|
+ }
|
|
|
|
+ tabCnt = (maxCmdAliasLen - cmdAliasLen) + 2u;
|
|
|
|
|
|
- (void)arguments;
|
|
|
|
- (void)length;
|
|
|
|
|
|
+ if((NULL != cmd->helpText) && (0u != strlen(cmd->helpText)))
|
|
|
|
+ {
|
|
|
|
+ SHELLMATTA_PRINT_BUFFER(tabBuffer, tabCnt, inst->write);
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write(cmd->helpText, strlen(cmd->helpText)));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ SHELLMATTA_RET(ret, inst->write("\r\n", 2u));
|
|
|
|
+
|
|
|
|
+ cmd = cmd->next;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|