ascaller.tcl

Go to the documentation of this file.
00001 /*  ascaller.tcl - */
00002 /* */
00003 /*        A few utility procs that manage the evaluation of a command*/
00004 /*  or a script in the context of a caller, taking care of all */
00005 /*  the ugly details of proper return codes, errorcodes, and*/
00006 /*  a good stack trace in ::errorInfo as appropriate.*/
00007 /*  -------------------------------------------------------------------------*/
00008 /* */
00009 /*  RCS: @(#) $Id: ascaller.tcl,v 1.3 2004/01/15 06:36:12 andreas_kupries Exp $*/
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 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1