ascaller.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 namespace ::control {
00012
00013 ret CommandAsCaller (type cmdVar , type resultVar , optional where ={) {codeVar code}} {
00014 x = [expr {[string equal "" $where]
00015 ? {} : [subst -nobackslashes {\n ($where)}]}]
00016 script = [subst -nobackslashes -nocommands {
00017 $codeVar = [catch {uplevel 1 $$cmdVar} $resultVar]
00018 if {$$codeVar > 1} {
00019 return -code $$codeVar $$resultVar
00020 }
00021 if {$$codeVar == 1} {
00022 if {[string equal {"uplevel 1 $$cmdVar"} \
00023 [lindex [split [ ::errorInfo = ] \n] end]]} {
00024 $codeVar = [join \
00025 [lrange [split [ ::errorInfo = ] \n] 0 \
00026 end-[expr {4+[llength [split $$cmdVar \n]]}]] \n]
00027 } else {
00028 $codeVar = [join \
00029 [lrange [split [ ::errorInfo = ] \n] 0 end-1] \n]
00030 }
00031 return -code error -errorcode [ ::errorCode = ] \
00032 -errorinfo "$$codeVar$x" $$resultVar
00033 }
00034 }]
00035 return $script
00036 }
00037
00038 ret BodyAsCaller (type bodyVar , type resultVar , type codeVar , optional where ={)} {
00039 set x [expr {[string equal "" $where]
00040 ? {} : [subst -nobackslashes -nocommands \
00041 {\n ($where[string map {{ ("uplevel"} {}} \
00042 [lindex [split [set ::errorInfo] \n] end]]}]}]
00043 script = [subst -nobackslashes -nocommands {
00044 $codeVar = [catch {uplevel 1 $$bodyVar} $resultVar]
00045 if {$$codeVar == 1} {
00046 if {[string equal {"uplevel 1 $$bodyVar"} \
00047 [lindex [split [ ::errorInfo = ] \n] end]]} {
00048 ::errorInfo = [join \
00049 [lrange [split [ ::errorInfo = ] \n] 0 end-2] \n]
00050 }
00051 $codeVar = [join \
00052 [lrange [split [ ::errorInfo = ] \n] 0 end-1] \n]
00053 return -code error -errorcode [ ::errorCode = ] \
00054 -errorinfo "$$codeVar$x" $$resultVar
00055 }
00056 }]
00057 return $script
00058 }
00059
00060 ret ErrorInfoAsCaller (type find , type replace) {
00061 set info $::errorInfo
00062 set i [string last "\n (\"$find" $info]
00063 if {$i == -1} {return $info}
00064 set result [string range $info 0 [incr i 6]] ;# keep "\n (\""
00065 append result $replace ;# $find -> $replace
00066 incr i [string length $find]
00067 set j [string first ) $info [incr i]] ;# keep rest of parenthetical
00068 append result [string range $info $i $j]
00069 return $result
00070 }
00071
00072 }
00073