diff --git a/src/shellmatta.c b/src/shellmatta.c index feb2109..59ac79e 100644 --- a/src/shellmatta.c +++ b/src/shellmatta.c @@ -66,6 +66,53 @@ static void writeEcho( shellmatta_instance_t *inst, } } +/** + * @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' + 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 @@ -312,8 +359,11 @@ static void rewindCursor(shellmatta_instance_t *inst, uint32_t length) length = SHELLMATTA_MIN (length, inst->cursor); if(length > 0u) { - size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uD", length); - writeEcho(inst, terminalCmd, size); + terminalCmd[0] = '\e'; + terminalCmd[1] = '['; + size = 2u + shellItoa(length, &terminalCmd[2], 10); + terminalCmd[size] = 'D'; + writeEcho(inst, terminalCmd, size + 1u); inst->cursor -= length; } } @@ -331,8 +381,11 @@ static void forwardCursor(shellmatta_instance_t *inst, uint32_t length) length = SHELLMATTA_MAX (length, (inst->inputCount - inst->cursor)); if (length > 0u) { - size = snprintf(terminalCmd, sizeof(terminalCmd), "\e[%uC", length); - writeEcho(inst, terminalCmd, size); + terminalCmd[0] = '\e'; + terminalCmd[1] = '['; + size = 2u + shellItoa(length, &terminalCmd[2], 10); + terminalCmd[size] = 'C'; + writeEcho(inst, terminalCmd, size + 1u); inst->cursor += length; } }