Tcl crash prevention from unknown command

I am doing an eval in the contents of the file. The file is made of tags that I parse. Each line has a label, and for each label there is a proc , so eval succeeds. However, sometimes users add new tags, and then the eval command fails due to an unknown command.

Is there a way to prevent Tcl from crashing when trying an eval an unknown command?
Ideally, this should allow me to substitute my own specific behavior - for example, bring an error and continue with eval .


EDIT :

Unfortunately, I can only use Tcl 8.4.
I tried to do 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 eval and it prints the following errors:

unknown procedure - a protected procedure and will not be renamed to an unknown procedure is a protected procedure and will not be overcome unknown procedure is a protected procedure and will not be renamed
an unknown procedure is a protected procedure and will not exceed

+6
source share
2 answers

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

It would be much better to read these shortcuts and use a giant switch or hash map or something else to execute a given command. Capturing non-existent "labels" also becomes inappropriate.

+3
source

Add your own command, called unknown , which takes as its arguments all the words that make up the command that cannot be found and its arguments:

 proc unknown {cmdName args} { puts "UNKNOWN COMMAND: $cmdName" puts "CALLED WITH ARGUMENTS: $args" } 

This is about all you need to do, although you can become more attractive with namespace unknown .

+2
source

Source: https://habr.com/ru/post/887797/


All Articles