From fc0f6407368adc5390c096d1e6f9d60942bab5e8 Mon Sep 17 00:00:00 2001
From: Ivan Grokhotkov <ivan@espressif.com>
Date: Thu, 27 Oct 2022 11:38:49 +0200
Subject: [PATCH] console: argtable3: upgrade to v3.2.2

Closes https://github.com/espressif/esp-idf/issues/9907
Closes https://github.com/espressif/esp-idf/pull/10016
---
 components/console/argtable3/arg_rex.c   |  4 ++-
 components/console/argtable3/argtable3.c | 33 +++++++++++++++++-------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/components/console/argtable3/arg_rex.c b/components/console/argtable3/arg_rex.c
index 858d3ea9853..8a7aa181712 100644
--- a/components/console/argtable3/arg_rex.c
+++ b/components/console/argtable3/arg_rex.c
@@ -101,7 +101,9 @@ typedef struct {
     int len;
 } TRexMatch;
 
-#ifdef __GNUC__
+#if defined(__clang__)
+TREX_API TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags) __attribute__((optnone));
+#elif defined(__GNUC__)
 TREX_API TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags) __attribute__((optimize(0)));
 #else
 TREX_API TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags);
diff --git a/components/console/argtable3/argtable3.c b/components/console/argtable3/argtable3.c
index 8b3244a3042..968b56159d0 100644
--- a/components/console/argtable3/argtable3.c
+++ b/components/console/argtable3/argtable3.c
@@ -509,11 +509,11 @@ static void arg_cat(char** pdest, const char* src, size_t* pndest) {
     char* end = dest + *pndest;
 
     /*locate null terminator of dest string */
-    while (dest < end && *dest != 0)
+    while (dest < end-1 && *dest != 0)
         dest++;
 
     /* concat src string to dest string */
-    while (dest < end && *src != 0)
+    while (dest < end-1 && *src != 0)
         *dest++ = *src++;
 
     /* null terminate dest string */
@@ -887,8 +887,8 @@ static void arg_print_formatted_ds(arg_dstr_t ds, const unsigned lmargin, const
     while (line_end > line_start) {
         /* Eat leading white spaces. This is essential because while
            wrapping lines, there will often be a whitespace at beginning
-           of line */
-        while (isspace((int)(*(text + line_start)))) {
+           of line. Preserve newlines */
+        while (isspace((int)(*(text + line_start))) && *(text + line_start) != '\n') {
             line_start++;
         }
 
@@ -900,18 +900,31 @@ static void arg_print_formatted_ds(arg_dstr_t ds, const unsigned lmargin, const
                 line_end--;
             }
 
-            /* Consume trailing spaces */
-            while ((line_end > line_start) && isspace((int)(*(text + line_end)))) {
-                line_end--;
-            }
+            /* If no whitespace could be found, eg. the text is one long word, break the word */
+            if (line_end == line_start) {
+                /* Set line_end to previous value */
+                line_end = line_start + colwidth;
+            } else {
+                /* Consume trailing spaces, except newlines */
+                while ((line_end > line_start) && isspace((int)(*(text + line_end))) && *(text + line_start) != '\n') {
+                    line_end--;
+                }
 
-            /* Restore the last non-space character */
-            line_end++;
+                /* Restore the last non-space character */
+                line_end++;
+            }
         }
 
         /* Output line of text */
         while (line_start < line_end) {
             char c = *(text + line_start);
+
+            /* If character is newline stop printing, skip this character, as a newline will be printed below. */
+            if (c == '\n') {
+                line_start++;
+                break;
+            }
+
             arg_dstr_catc(ds, c);
             line_start++;
         }