Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Tcl from crashing on unknown command

Tags:

tcl

I'm doing eval on the content of file. The file is made out of labels which I parse. Each line has a label, and I have a proc defined for each label, so that the eval succeeds. However, sometimes users add new labels and then the eval command fails, because of unknown command.

Is there a way to prevent Tcl from crashing when trying to eval an unknown command?
Ideally, it should allow me to substitute with my own defined behaviour - such as priting an error and continuing with the eval.


EDIT:

Unfortunately, I can only use Tcl 8.4.
I tried doing the following, as suggested here:

proc handle_unknown_label {cmd args} { ... }

and then:

rename unknown _old_system_unknown
rename handle_unknown_label unknown

catch {set ret [eval $x]} err

rename unknown handle_unknown_label
rename _old_system_unknown unknown

but I still get the same behavior for the eval, and it prints the following errors:

procedure unknown is a protected proc and will not get renamed
procedure unknown is a protected proc and will not get overriden
procedure unknown is a protected proc and will not get renamed
procedure unknown is a protected proc and will not get overriden

like image 863
Amir Rachum Avatar asked May 11 '11 07:05

Amir Rachum


1 Answers

I think I'm stating the obvious, but eval'ing a file is dangerous: any user can embed [exec whatever they wish] into such a file.

Much better would be to read those "labels" and use a giantic switch or a hash map or whatever to execute a predetermined command. Catching non-existing "labels" also becomes a non-issue.

like image 181
kostix Avatar answered Oct 02 '22 17:10

kostix