Get the output from Tcl C Procedures

2019-02-19 19:40发布

问题:

I have a C shell that usually calls Tcl routines using Tcl_Eval. Normally I was fine with just executing what the user typed and getting some status as a result. However, now I need to receive the actual stdio output from the command that user typed. Is there any way to get it using the Tcl C procedures?

As a side note: I need to figure out the list of current procedures available in the Tcl interpreter, both built in and user sourced. Basically, the output from info procs *.

回答1:

I think you could go like this:

  1. Create a pipe by calling pipe(2).
  2. Then in your interp:

    1. Close stdout by calling Tcl_Close() on it.
    2. Turn the write-end file descriptor of your pipe into Tcl's stdout channel by calling Tcl_MakeChannel() right after closing stdout.

    Or use just replace the stdout with a call to Tcl_SetStdChannel().

  3. Process the data coming from the pipe.

As to your side note — I think you could just call Tcl_Eval() in your interpreter and process the returned list using the list-processing functions from the Tcl API.

Update (from one of my comments): after some more thought I think it might be possible to just create a custom Tcl channel which implementation would just save away the data written to it and then register an instance of such a channel as stdout. See Tcl_CreateChannel() and Tcl_RegisterChannel().



标签: tcl