如何将用户定义的 C API tcl 命令 "get_failed" 的结果存储到 tcl 变量中?

How can I store the result of user defined C API tcl command "get_failed" into a tcl variable?

在tcl中,我们可以将内联命令的返回字符设置到一个tcl变量中,如下所示。

Tcl> set x [pwd]
/home/user_leo/tmp
Tcl> puts $x
/home/user_leo/tmp

我们通过 Tcl C/C++ API 实现了一个用户定义的命令“get_failed”,这个命令会将结果打印到屏幕上,如下所示:

Tcl> get_failed
a1 a3 a5 a7

我们希望可以将证明结果存储到 tcl 变量中。但是我们试过了,失败了。

Tcl> set y [get_failed]
a1 a3 a5 a7
Tcl> puts $y

变量 $y 中没有存储任何内容。

那么我怎样才能让它发挥作用呢?如何将用户定义的 tcl 命令“get_failed”的结果存储到 tcl 变量中?我希望它能像下面这样工作:

Tcl> set y [get_failed]
a1 a3 a5 a7
Tcl> puts $y
a1 a3 a5 a7

我希望如此,因为我想将“get_failed”的结果传递给另一个用户定义的命令,示例如下。

Tcl> check_by_method2 [get_failed]
checking a1...
done, pass.
checking a3...
done, fail.
checking a5...
done, pass
checking a7...
done, pass

在 C return 中实现的 Tcl 命令使用 Tcl_SetObjResult()Tcl_SetResult() 将值返回到 tcl 运行时(对于简单字符串)。据我们所知,由于您没有为 get_failed 命令提供任何示例源,您正在将值打印到标准输出而不是构建结果对象。既然你想要 return 一个列表,它可能看起来像

Tcl_Obj *my_list = Tcl_NewListObj(0, NULL);
// Populate the list however is appropriate for your command
Tcl_ListObjAppendElement(interp, my_list, Tcl_NewStringObj("a1", -1));
Tcl_SetObjResult(interp, my_list);
return TCL_OK;