gpt4 book ai didi

tcl - 创建一个仅支持我提供的命令的 TCL 解释器

转载 作者:行者123 更新时间:2023-12-04 17:11:35 24 4
gpt4 key购买 nike

假设我已经定义了 proc f1 proc f2 和 proc f3。现在我想创建一个 TCL 解释器,将 proc f1 proc f2 和 proc f3 的代码源到该解释器中,并限制除 f1、f2 和 f3 之外的所有命令都在该解释器中。我怎么能做到这一点?

编辑:

如果在解释器中调用了 f1、f2 和 f3 以外的命令,则应发出错误消息并执行源自解释器的代码(假设这是源自同一解释器的另一个代码,在获得应该停止带有 f1、f2 和 f3 过程的代码。

最佳答案

你不能完全做到这一点,但你可以做一些对于大多数目的来说足够相似的事情。

您应该做的是通常在解释器中创建命令 f1、f2 和 f3,然后创建一个根本没有 Tcl 命令的子解释器,并将您希望在该子解释器中公开的命令别名为命令在父级。

# First define f1-f3 in whatever way you want

# Now make the context; we'll use a safe interpreter for good measure...
set slave [interp create -safe]

# Scrub namespaces, then global vars, then commands
foreach ns [$slave eval namespace children ::] {
$slave eval namespace delete $ns
}
foreach v [$slave eval info vars] {
$slave eval unset $v
}
foreach cmd [$slave eval info commands] {
# Note: we're hiding, not completely removing
$slave hide $cmd
}

# Make the aliases for the things we want
foreach cmd {f1 f2 f3} {
$slave alias $cmd $cmd
}

# And evaluate the untrusted script in it
catch {$slave invokehidden source $theScript}

# Finally, kill the untrusted interpreter
interp delete $slave

关于tcl - 创建一个仅支持我提供的命令的 TCL 解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10501630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com