tcl command redirect to a variable, tcl version is 8.4
Asked Answered
B

6

10

I want to redirect the output of 1 command to a variable, where the OUTPUT is usually to STDOUT. I am running an EDA tool, which has tcl interpeter & it's own commands. Let's say that the tool has a tcl query, which says

TOOL> find_transistor m*
m1 m2 m3 m4

I want to have a way of doing the following:

TOOL> set ret redirect {find_transistor m*}
TOOL> puts $ret
m1 m2 m3 m4

Any ideas?

Burns answered 6/10, 2010 at 19:14 Comment(1)
are you certain that find_transistor prints to stdout, or are you assuming that based on your interactive experience?Injured
S
9

This might work:

redirect -variable ret {find_transistor m*}

puts $ret
Scamander answered 21/2, 2012 at 17:4 Comment(3)
Well, this only works for tools of the company Synopsys. Probably it's still a good answer to keep, even if it does not answer the original question. At least it likely solves the issue for a lot of folks coming here. However redirect is not a plain Tcl command, and thus does not work in plain Tcl, and very unlikely in any other Tcl based EDA tools.Carbonate
It is >10 years after this question was asked, but redirect also worked for me in Cadence's LEC tool, so this is not only limited SynopsysDiadromous
Unfortunately not working in simulators - tried two separate vendors. Also does not natively work in tclsh - at least the version distributed with MSYS2.Puddle
R
6

well in pure Tcl

set ret [find_transistor m*]

would probably do what you want. Try reading the Tcl tutorial.

Recognizance answered 6/10, 2010 at 19:21 Comment(3)
This won't work if "find_transistor" writes its result to stdout. All your example does is save whatever find_transistor returns, which unfortunately is not what the question is asking.Injured
You are of course correct - if the command really is writing to STDOUT then a different approach will be required. We really need the originator of the question to come back and talk to us!Recognizance
I'm not the originator of the question, but I encountered the same problem. [] will not work for some EDA tool internal commands and those commands do print messages to STDOUT. Any suggestion?Grubbs
B
4

Simplest way I've found is exec: set VAR [exec COMMAND]

Baton answered 30/8, 2016 at 22:7 Comment(1)
Worked like a charm for me. Should be on top. @LiorDagan, is this what you're looking for? I notice this question has no accepted answer...Scarecrow
C
3

If your application does not have a redirect command, you can create your own.

Please have a look at my answer to the more general question of how to redirect in plain Tcl?

To redirect into a variable you could do:

proc redirect_variable {varname cmd} {
    rename puts ::tcl::orig::puts

    global __puts_redirect
    set __puts_redirect {}

    proc puts args {
        global __puts_redirect
        set __puts_redirect [concat $__puts_redirect [lindex $args end]]
        set args [lreplace $args end end]
        if {[lsearch -regexp $args {^-nonewline}]<0} {
            set __puts_redirect "$__puts_redirect\n"
        }
        return
    }

    uplevel $cmd

    upvar $varname destination
    set destination $__puts_redirect
    unset __puts_redirect

    rename puts {}
    rename ::tcl::orig::puts puts
}
Carbonate answered 19/12, 2012 at 8:38 Comment(0)
D
1

I tried everything mentioned here. Finally this is the one that actually worked for me:

redirect -variable <myvar> {puts [<some_tcl_command>] }
puts $<myvar>

PS: This worked in a Cadence tool.

Diuresis answered 19/4, 2018 at 12:43 Comment(0)
G
0

Before anyone comes up with an elegant solution, I share my ugly last resort:

find_transistor m* > tmp
set fp [open "tmp" r]
set file_data [read $fp]
close $fp

Bear in mind that the output of the command should be relatively small.

Grubbs answered 29/8, 2011 at 3:23 Comment(1)
This does not work in plain Tcl 8.4. Although I do assume that it fixes the problem for the original asker and lots of folks coming here, since several EDA tools implement shell like output redirection. Plain Tcl would choke on test >f1 with wrong # args: should be "test" if proc test {} { puts "Hi, there!" }Carbonate

© 2022 - 2024 — McMap. All rights reserved.