This is an automated email from Gerrit.

"Antonio Borneo <[email protected]>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9782

-- gerrit

commit 5be560e694d326ef60894d5aea02e854c13a7584
Author: Antonio Borneo <[email protected]>
Date:   Sun Jul 12 11:31:47 2026 +0200

    helper: tcl-common: add tcl_escape_alloc()
    
    OpenOCD commands can produce a Tcl output ready to be parsed in
    Tcl scripts.
    When a string has to be converted in a single Tcl element, the
    OpenOCD command has to guarantee that he string does not contains
    character that can confuse the Tcl parsing. A typical example is
    the presence of whitespace that can split the element in two,
    square brackets that can be expanded executing the content.
    
    Rely on Jim Tcl API Jim_NewListObj() to handle all such corner
    cases.
    
    The string returned by tcl_escape_alloc() has to be free() by the
    caller.
    
    Change-Id: I898c314a02d455946e35223a43f7a65ed4c92833
    Signed-off-by: Antonio Borneo <[email protected]>

diff --git a/src/helper/tcl-common.h b/src/helper/tcl-common.h
index 959c39972a..4e0e815024 100644
--- a/src/helper/tcl-common.h
+++ b/src/helper/tcl-common.h
@@ -5,4 +5,13 @@
 
 #include <jim.h>
 
+/**
+ * Convert a C string to a string that can be used for Tcl list.
+ * The returned string has to be deallocated through free().
+ * @param interp: the Tcl interpreter
+ * @param s: the C string to convert
+ * @returns converted string or NULL on error
+ */
+char *tcl_escape_alloc(Jim_Interp *interp, const char *s);
+
 #endif /* OPENOCD_HELPER_TCL_COMMON_H */
diff --git a/src/helper/tcl-libjim.c b/src/helper/tcl-libjim.c
index fb2183c23f..e409c2e171 100644
--- a/src/helper/tcl-libjim.c
+++ b/src/helper/tcl-libjim.c
@@ -17,4 +17,22 @@
 #include "config.h"
 #endif
 
+#include <assert.h>
+#include <string.h>
+
 #include <helper/tcl-common.h>
+
+char *tcl_escape_alloc(Jim_Interp *interp, const char *s)
+{
+       assert(s);
+
+       Jim_Obj *o1 = Jim_NewStringObj(interp, s, -1);
+       Jim_Obj *o2 = Jim_NewListObj(interp, &o1, 1);
+       Jim_IncrRefCount(o2);
+
+       char *out = strdup(Jim_String(o2));
+
+       Jim_DecrRefCount(interp, o2);
+
+       return out;
+}

-- 

Reply via email to