prozessorkern 5 years ago
parent
commit
f3b03b80d3
1 changed files with 66 additions and 4 deletions
  1. 66 4
      src/shellmatta.c

+ 66 - 4
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' - 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
@@ -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;
     }
 }
@@ -1074,6 +1127,13 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t     handle,
     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)
@@ -1085,6 +1145,7 @@ shellmatta_retCode_t shellmatta_write(  shellmatta_handle_t handle,
     if(     (NULL               != inst)
         &&  (SHELLMATTA_MAGIC   == inst->magic))
     {
+        /** -# pass the data to the write function of the instance  */
         ret = inst->write(data, length);
     }
 
@@ -1115,6 +1176,7 @@ shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle,
     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);