TCL - feeding STDIN to exec - tcl

I'm trying to implement a retry mechanism when executing an external program using TCL. I'm having some issues when trying to feed STDIN to the external program. I'm now working with a simplified example trying to solve the issue. Take the following python script (simple.py):
x = raw_input()
y = raw_input()
print x + y
Inputs 2 strings from the output will be the concatenation result of the strings.
Now the following command works from the TCL interpreter:
% exec python stuff.py << 1\n2
12
However when I try to split it in separate commands, or add them to a string before doing this, it fails.
Fail 1:
% set cmd "python simple.py << 1\n2"
% exec $cmd
couldn't execute "python simple.py << 1
2": no such file or directory
Fail 2:
% set cmd1 "python simple.py"
% set cmd2 "1\n2"
% exec $cmd1 << $cmd2
couldn't execute "python simple.py": no such file or directory
Fail 3:
% set fullCommandString "exec python simple.py << 1\n2"
% eval $fullCommandString
Traceback (most recent call last):
File "simple.py", line 2, in <module>
y = raw_input()
EOFError: EOF when reading a line
The 3rd case seems that starts the script, but it interprets both lines of STDIN as one.
Any help is appreciated.

Tcl's commands do not reinterpret whitespace in their arguments by default. exec is one of these, and it follows the same rules. That means that you need to tell Tcl to interpret that list of words as a list of words as otherwise it is just a string. Fortunately, there's {*} for this; the expansion operator syntax interprets the rest of the word as a Tcl list, and uses the words out of that list at the point where you write it. It's very useful I find.
The simplest to fix is actually your second case:
% set cmd1 "python simple.py"
% set cmd2 "1\n2"
% exec {*}$cmd1 << $cmd2
You can fix the first and third by adding Tcl list quoting to ensure the 1\n2 is still interpreted as a single word (as otherwise newline is a perfectly reasonable list item separator).
% set cmd "python simple.py << {1\n2}"
% exec $cmd
% set fullCommandString "exec python simple.py << {1\n2}"
% eval $fullCommandString
The third can be written more economically though:
% set fullCommandString "exec python simple.py << {1\n2}"
% {*}$fullCommandString
As a rule of thumb, if you see eval in modern Tcl (note: not namespace eval or interp eval or uplevel) then it's usually an indication that some code could be made more efficient and to have fewer bugs by switching to using expansion carefully.
tl;dr: Put {*} before $cmd1 in your second example to get the idiomatic fix.

Related

Reading cmd arguments in TCL file

I am trying to run a tcl script through .bat file. I want to read some cmd arguments in the tcl script. Below is my code:
Command to run:
D:\Cadence\Sigrity2021.1\tools\bin\PowerSI.exe -tcl abcd.tcl %new_var%.spd %new_file_name%
Below is how I am trying to read the variable in the tcl file:
sigrity::open document [lindex $argv 0] {!}
It open up the Cadence Sigrity, but I see the below error:
How do I read cmd argument in tcl?
If you have no other way to do it that you can find (and it sounds like that might be the case) then you can fake it by writing a helper file with content like this, filling in the real arguments in the appropriate places:
# Name of script to call
set ::argv0 "abcd.tcl"
# Arguments to pass
set ::argv {}
lappend ::argv "%new_var%.spd"
lappend ::argv "%new_file_name%"
# Number of arguments (rarely used)
set ::argc [llength $::argv]
# Do the call
source $::argv0
Then you can pass that file to PowerSI and it will set things up and chain to the real file. It's messy, but practical.
If you're writing this from Tcl, use the list command to do the quoting of the strings (instead of putting them in double quotes) as it will do exactly the right thing for you. If you're writing the file from another language, you'll want to make sure you put backslashes in before \, ", $ and [ characters. The fiddlyness of doing that depends on your language.

How to use Tcl script in unix pipeline?

Want to use some custom function (written in tcl) in Unix pipeline ie grep patt file.rpt | tclsh summary.tcl. How to make tcl script to take output from the pipeline, process and out on the commandline as if a normal unix command?
This is very easy! The script should read input from stdin (probably with gets) and write output to stdout (with puts; this is the default destination).
Here's a very simple by-line filter script:
set lineCount 0
while {[gets stdin line] >= 0} {
incr lineCount
puts stdout "$lineCount >> $line <<"
}
puts "Processed $lineCount lines in total"
You probably want to do something more sophisticated!
The gets command has two ways of working. The most useful one here is the one where it takes two arguments (channel name, variable name), writes the line it has read from the channel into the variable, and returns the number of characters read or -1 when it has an EOF (or would block in non-blocking mode, or has certain kinds of problems; you can ignore these cases). That works very well with the style of working described above in the sample script. (You can distinguish the non-success cases with eof stdin and fblocked stdin, but don't need to for this use case.)

taking file content as file name itself in tcl

I am running one tcl script who is taking file as a input by "stdin".The problem is that its taking the file content as a filename and throwing error while running the script on command line processor.
tcl script is
#!/bin/sh
# SystemInfo_2.tcl \
exec tclsh "$0" ${1+"$#"}
set traps [read stdin];
#set traps "snmp trap test"
set timetrap [clock format [clock seconds]];
set trapout [open Database_traps_event.txt a+];
set javaout [open JavaTrapOutput.txt a+];
puts $trapout $timetrap;
puts $trapout $traps;
puts $trapout "Before executing java program";
set javaprogargs "open {|java -cp mysql-connector-java-5.1.10.jar;. EventAlarmHandling \"$traps\"} r";
puts $trapout $javaprogargs;
set javaprogram [eval $javaprogargs];
puts $trapout "Execution of java is over"
while { [gets $javaprogram line] != -1 } {
puts $javaout $line;
}
close $javaprogram;
puts $trapout "After excution of java program\r\n\r\n\r\n\r\n\r\n";
close $trapout;
close $javaout;
exit;
input file content is -
<UNKNOWN>
UDP: [192.168.1.19]:60572->[0.0.0.0]:0
.iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.sysUpTimeInstance 1:9:58:56.61
.iso.org.dod.internet.snmpV2.snmpModules.snmpMIB.snmpMIBObjects.snmpTrap.snmpTrapOID.0 .iso.org.dod.internet.snmpV2.snmpModules.snmpMIB.snmpMIBObjects.snmpTraps.linkDown
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifIndex.1 8
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus.8 up
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus.8 down
From command line it ran like below
E:\eventAlarmHandling>tclsh TclTempFile.tcl < traps.txt
couldn't read file "UNKNOWN>
UDP: [192.168.1.19]:60572->[0.0.0.0]:0
.iso.org.dod.internet.mgmt.mib-2.system.sysUpTime.sysUpTimeInstance 1:9:58:56.61
.iso.org.dod.internet.snmpV2.snmpModules.snmpMIB.snmpMIBObjects.snmpTrap.snmpTrapOID.0 .iso.org.dod.intern
et.snmpV2.snmpModules.snmpMIB.snmpMIBObjects.snmpTraps.linkDown
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifIndex.1 8
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifAdminStatus.8 up
.iso.org.dod.internet.mgmt.mib-2.interfaces.ifTable.ifEntry.ifOperStatus.8 down": No error
while executing
"open {|java -cp mysql-connector-java-5.1.10.jar;. EventAlarmHandling "<UNKNOWN>
UDP: [192.168.1.19]:60572->[0.0.0.0]:0
.iso.org.dod.internet.mgmt.mib..."
("eval" body line 1)
invoked from within
"eval $javaprogargs"
invoked from within
"set javaprogram [eval $javaprogargs]"
(file "TclTempFile.tcl" line 26)
So clearly in command line its showing that "couldn't read file UNKNOWN> ......"
So please explain it that whats happening here in command line.I am new to tcl.So hoping that someone help me out.
Thanks
You're having problems with one of the trickier bits of how pipelines work in Tcl. If we look at the documentation carefully, we see:
If the first character of fileName is “|” then the remaining characters of fileName are treated as a list of arguments that describe a command pipeline to invoke, in the same style as the arguments for exec.
That means you have to have the first character be | and the rest, after stripping that first character, be a proper list. In your case, you've not got that. Instead, you're doing:
set javaprogargs "open {|java -cp mysql-connector-java-5.1.10.jar;. EventAlarmHandling \"$traps\"} r";
That's pretty complicated anyway. Let's build this in the idiomatic fashion instead:
set CPsep ";"
set classpath [list mysql-connector-java-5.1.10.jar .]
set javaprogargs [list open |[list \
java -cp [join $classpath $CPsep] EventAlarmHandling $traps]]
It helps to split the classpath out; it's got a ; character in it (on Windows; you'll need to change that if you port to Linux or OSX) and it's nicer to use list in Tcl to build things and then join to convert into what Java expects.
We also no longer need any backslash-quoted substrings in there (except the one I put in to keep lines short and readable); the pattern of list commands there will add everything that is required. Note the |[list …] there: that's non-idiomatic everywhere in Tcl except when creating a pipeline when it is recommended practice as it is doing in reverse what open expects to parse.
The other thing you're running into is this:
If an arg (or pair of args) has one of the forms described below then it is used by exec to control the flow of input and output among the subprocess(es). Such arguments will not be passed to the subprocess(es).
[…]
< fileName
The file named by fileName is opened and used as the standard input for the first command in the pipeline.
Your argument from $traps starts with a < and so it triggers this rule.
Unfortunately, there's no simple workaround for this and this is a severe, known, and very annoying limitation of the pipeline creation code. The only known techniques for dealing with this are to move to transferring that data by either a file or via the subprocess's standard input, both of which require modifying the subprocess's implementation. If you can make that Java program read from System.in (a good idea anyway, so you don't hit Windows's command line length limitations!) then you can pass the value like this:
set CPsep ";"
set classpath [list mysql-connector-java-5.1.10.jar .]
set javaprogargs [list open |[list \
java -cp [join $classpath $CPsep] EventAlarmHandling << $traps]]
That is just by adding a << in there immediately before the value.

gnuplot output filename from csv file

I have a CSV file and I am plotting the columns one by one in a do for[] loop. I would like to save the plot as a PNG file, with the filename coming from the columnheader. What is the best way to go about this where text.png is replaced by the ith column header?
#!/bin/bash
set datafile separator ","
set key autotitle columnhead
set xlabel "time/date"
nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"
set term png
do for [i = 2:5] {
set output "test.png"
plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
}
As long as you're using awk, you could use it once more to get the header name from inside a gnuplot macro:
#!/usr/bin/env gnuplot
set datafile separator ","
set key autotitle columnhead
set xlabel "time/date"
nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"
# Define a macro which, when evaluated, assigns the ith column header
# to the variable 'head'
awkhead(i) = "head = \"\`awk -F, 'NR == 1 {print $".i."}' input.csv\`\""
set term png
do for [i = 2:5] {
eval awkhead(i) # evaluate the macro
set output head.".png" # use the 'head' variable assigned by the macro
plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
}
There is almost certainly a cleaner way to do this with another awk-like utility, or even within gnuplot. Gnuplot offers a few ways to run arbitrary internal/external commands, as you can see from my mix of backtics and macro evaluation.
By the way, it is a little strange to me that you have the bash shebang (#!/bin/bash) at the start of the script if it will presumably be interpreted by gnuplot. I assume you call it as gnuplot myscript.plt. In this case the shebang is just a comment (as far as gnuplot is concerned) and doesn't do anything because gnuplot is the interpreter. In my example I use #!/usr/bin/env gnuplot and I run the script as an executable in bash, like ./myscript.plt. The shebang in this case tells bash to make gnuplot the interpreter (or whatever command you would get by typing gnuplot at the command prompt). Of course you could also set the shebang to be #!/usr/bin/gnuplot if you're not worried about the path changing.

how to pass command line parameter containing '<' to 'exec'

$ date > '< abcd'
$ cat '< abcd'
<something>
$ tclsh8.5
% exec cat {< abcd}
couldn't read file " abcd": no such file or directory
whoops. This is due to the the specification of 'exec'.
If an arg (or pair of args) has one of the forms described below then it is used by exec to control the flow of input and output among the subprocess(es). Such arguments will not be passed to the subprocess(es). In forms such as “< fileName”, fileName may either be in a separate argument from “<” or in the same argument with no intervening space".
Is there a way to work around this?
Does the value have to be passed as an argument? If not, you can use something like this:
set strToPass "< foo"
exec someProgram << $strToPass
For filenames, you can (almost always) pass the fully qualified name instead. The fully qualified name can be obtained with file normalize:
exec someProgram [file normalize "< foo"] ;# Odd filename!
But if you need to pass in an argument where < (or >) is the first character, you're stuck. The exec command always consumes such arguments as redirections; unlike with the Unix shell, you can't just use quoting to work around it.
But you can use a helper program. Thus, on Unix you can do this:
exec /bin/sh -c "exec someProgram \"$strToPass\""
(The subprogram just replaces itself with what you want to run passing in the argument you really wanted. You might need to use string map or regsub to put backslashes in front of problematic metacharacters.)
On Windows, you have to write a batch file and run that, which has a lot of caveats and nasty side issues, especially for GUI applications.
One simple solution: ensure the word does not begin with the redirection character:
exec cat "./< abcd"
One slightly more complex:
exec sh -c {cat '< abcd'}
# also
set f {< abcd}
exec sh -c "cat '$f'"
This page on the Tcl Wiki talks about the issue a bit.
Have you tried this?
% exec {cat < abcd}
Try:
set myfile "< abcd"
exec cat $myfile