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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With