how to implement a exec command in tcl? - tcl

It is possible to call other programs from Tcl using the Tcl exec function. Let’s use this
command to create a Tcl script that will take all of the Tcl code you have written so far and
create a single PDF from it. The filename of the single PDF file should be TCL CODE.pdf.
Recall that the Tcl foreach command makes it very easy to go through a list of items.
You should use the exec command to call the Linux program called enscript to produce
a postscript file for each of the Tcl files. You should then use the Linux program called
ps2pdf to turn each of the postscript files into a PDF. Finally, merge all of the PDFs using
the Linux program called gs (short for ghostscript). Invoke ghostscript using the following
command

Some documentation
Tcl documentation
the exec man page
Tcl tutorial
the wiki entry for exec

Related

Fail to launch tclsh from tclsh

I'm launching multiple tclsh from inside a TCL script to emulate multi threading. However the calls all fail.
I've simplified the problem down to a test where a TCL proc launches a tclsh.
proc launch_tcl {} {
set cmd "tclsh script.tcl"
set pid [ eval $cmd & ]
}
This produces : invalid command name "tclsh"
I can give following lines on the TCL prompt and they work fine.
set cmd "tclsh script.tcl"
set pid [ eval $cmd & ]
I have tried tclsh with full path to the binary as well with same failure.
Why does the same commands fail inside the proc?
Thanks,
Gert
While Tcl syntax looks more related to shell syntax like bash or tcsh tcl is actually more closely related to Perl or PHP or Ruby. Tcl only interprets tcl code thus just typing the name of another executable does not launch that executable*.
Just like Perl or Ruby (or indeed C and C++) tcl does indeed have mechanisms to launch executables. For that you need the exec command:
exec tclsh script.tcl
Warning on how exec works:
Unlike other languages where the command to spawn external binaries accept a string, tcl's exec is more closely related to C or javascript in that it accepts structured data. If you try to do this:
exec "tclsh script.tcl"
You'll get an error complaining "tclsh script.tcl" does not exist. It will look for an executable called "tclsh script.tcl" (because unix, from the very beginning, allows program names to contain spaces). Instead you need to pass the program name and each individual argument separately. Similarly if you tried:
exec tclsh "arg1 arg2"
The exec command will execute tclsh and pass the string "arg1 arg2" as the first argument.
See the documentation of exec for more info: https://www.tcl.tk/man/tcl/TclCmd/exec.htm
What to do if you have a command in a string?
Don't store commands in strings. There's no real safe way to separate arguments in strings in tcl. Instead compose your command as a list and then use the {*} operator to expand the list when you call exec:
set cmd [list tclsh script.tcl]
exec {*}$cmd
*Note: There may be confusion about this because in interactive mode tcl DOES indeed launch executables if you type it. But this is only a feature of interactive mode.

How to open a window in Vivado using Tcl script?

I'd like to open a .vhd and .vhi file in window for editing in Vivado from Tcl Console, but I can't find any command for that.
As of at least Vivado 2014.2 any unrecognized Tcl command will be sent to the OS shell for execution, so you can simply open whatever editor you like as if you were not in the Tcl shell. It basically automatically runs exec for you. Older versions you may have to run exec yourself.
eg
nedit file.vhd
Vivado being a design tool works on projects instead of individual files. So to edit a file, say xyz.vhd, the file needs to be part of a project. This can be done through Tcl console by creating a new project, adding xyz.vhd file to it and then loading the project.
Create a new project using the following command:
project -new
Add files:
add_file -vhd "xyz.vhd"
Save the project and run.
project -save
project -run
You can find further resources at this link.

Why tcl script runs as shell script?

I don't program in TCL but I do use them such as tkcvs and tkdiff. I notice that they declare themselves as shell script
#!/bin/sh
#-*-tcl-*-
What's more, running them through tclsh doesn't work either and I get error like this:
Initialization failed
The second line in the header baffles me too because AFAIK, shell only looks at the #! line. How's this working?
Tcl scripts are normally setup to run using slightly more than you have shown. It is typically and most robustly done like the following:
#!/bin/sh
# \
exec tclsh "$0" ${1+"$#"}
They use the shell initially because there was no standard installation location for tcl so the location could not be relied on. Instead, by starting the system shell and using that to start the tclsh executable you could be certain to run the installed tclsh as long as it was present on the PATH. The shell evaluates the script and sees the exec tclsh "$0" which causes it to execute the installed tclsh binary and pass it $0 (the script file name) as the first argument, which re-runs the script using the tcl interpreter.
The second line in my example comments out the third line when the script is evaluated by the tcl interpreter. The backslash causes the second and third lines to be treated as a single comment when read by tclsh so that tcl doesn't try and run the exec again.
In your snipped the # -*-tcl-*- is marker to indicate the mode to be used by emacs when editing the file. See the documentation.
There is not really enough to go on for the error message. It doesn't seem to be from the Tcl interpreter itself. (That is 'git grep' in the tcl sources does not match that string).

Copy file in windows 7 using Tcl command

How can use Tcl command in windows-7 ? I want to copy one file to other locaiton using a .bat file.
How can use Tcl command in windows-7 ?
Windows (of all versions) has never come with a Tcl interpreter pre-installed, By far the simplest way is to get a copy of ActiveTcl installed.
Once you've got that installed, either run it interactively and just type your Tcl command in at the prompt, or use a text editor (like Notepad) to make a file (conventionally with extension .tcl) that contains the command or commands to execute; you'll probably be able to make the file run by just double-clicking on it.
I want to copy one file to other locaiton using a .bat file.
That's not really got much to do with Tcl. With Tcl, you would use:
file copy {C:\Where\To\Copy\From.txt} {C:\Where\To\Copy.to}
Note, we've put the filenames in {curly braces} here so that we can use backslashes; if we weren't doing that, we'd need to use double-backslashes instead (\\) or forward slashes (/).
The alternative, if you're really wanting to use a .bat file, is to look up what the cmd.exe commands COPY and (less likely) XCOPY do. But that's not a Tcl question.

Execute TCL on windows XP

I installed tcl803.exe on my windows XP Operating System and my TCL path is C:\Tcl. Now I am unable to execute the TCL script on windows XP operating system. Please help me. Here I am very new. Please tell me each and every steps. Here is my TCL script. I write in a notepad and save it as a.tcl extension.
set x 50
set y 400
puts stdout "$x+$y=[expr {$x+$y}]"
puts stdout "-The addition of two variables $x and $y is [expr $x+$y]"
Do this at a DOS prompt:
assoc .tcl=TclScript
ftype TclScript=c:\Tcl\tclsh.exe %1 %*
However, it would be much easier for you to install ActiveTcl -- that install process will manage the file associations for you.
Tcl files aren't compiled, they're scripts that require they be handed to a Tcl interpreter to run. You have two primary options on Windows:
Run the tcl interpreter with the script as an argument:
tclsh.exe ddd.tcl
Associate .tcl files with the tclsh (or wish) interpreters. I don't recall how to do this offhand, but it's similar to associate .doc items with Word... When you double click on the .tcl file it calls the executable (Tcl) and gives it the name of the .tcl file you clicked on.
Note: You can "compile" tcl code into an executable (tclkit, starkit), but you probably aren't at a point where you'd be comfortable doing so.