tags:

views:

58

answers:

1

Hello,

I want to create a simple Console in Tcl/Tk

I have two problems. First changing every * with a [glob *] but also, when my entry contains "ls -a" it doesn't understand that ls is the command and -a the first arg.

How can I manage to do that ?

Thanks

proc execute {} {
    # ajoute le contenu de .add_frame.add_entry
    set value [.add_frame.add_entry get]
    if {[string compare "$value" ""] == 1} {
    .text insert end "\n\n% $value\n"
        .text insert end [exec $value]
    .add_frame.add_entry delete 0 end
    }
}

frame .add_frame

label .add_frame.add_label -text "Nouvel élément : "
entry .add_frame.add_entry
button .add_frame.add_button -text "Executer" -command execute
button .add_frame.exit_button -text "Quitter" -command exit

bind  .add_frame.add_entry  <Return> execute
bind  .add_frame.add_entry  <KP_Enter> execute
bind  .  <Escape> exit
bind  .  <Control-q> exit

pack .add_frame.add_label -side left
pack .add_frame.exit_button -side right
pack .add_frame.add_button -side right
pack .add_frame.add_entry -fill x -expand true

pack .add_frame -side top -fill x

text .text
.text insert end  "% Tcl/Tk Console"

pack .text -side bottom -fill both -expand true
+6  A: 

The simple answer in Tcl 8.5 is to use this:

exec {*}$value

In 8.4 and before, that syntax didn't exist. That meant that many people wrote this:

eval exec $value

But in reality, the safe version was one of these:

eval exec [lrange $value 0 end]
eval [linsert $value 0 exec]

Of course, if the $value is coming straight from the user then you're better off using a system shell to evaluate it since more users expect that sort of syntax:

exec /usr/bin/bash -c $value
Donal Fellows
+1 for suggesting "sh -c $value" -- you probably don't want to build a shell parser in Tcl.
glenn jackman
But if you really want to emulate shell syntax in Tcl, you might like to have a look at my code linked from http://wiki.tcl.tk/gush , specifically the tokeinise and shellrun procedures.
Colin Macleod