safetk.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. # safetk.tcl --
  2. #
  3. # Support procs to use Tk in safe interpreters.
  4. #
  5. # RCS: @(#) $Id: safetk.tcl,v 1.8.8.1 2006/01/25 18:21:41 dgp Exp $
  6. #
  7. # Copyright (c) 1997 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. # see safetk.n for documentation
  12. #
  13. #
  14. # Note: It is now ok to let untrusted code being executed
  15. #       between the creation of the interp and the actual loading
  16. #       of Tk in that interp because the C side Tk_Init will
  17. #       now look up the master interp and ask its safe::TkInit
  18. #       for the actual parameters to use for it's initialization (if allowed),
  19. #       not relying on the slave state.
  20. #
  21. # We use opt (optional arguments parsing)
  22. package require opt 0.4.1;
  23. namespace eval ::safe {
  24.     # counter for safe toplevels
  25.     variable tkSafeId 0;
  26.     #
  27.     # tkInterpInit : prepare the slave interpreter for tk loading
  28.     #                most of the real job is done by loadTk
  29.     # returns the slave name (tkInterpInit does)
  30.     #
  31.     proc ::safe::tkInterpInit {slave argv} {
  32. global env tk_library
  33. # We have to make sure that the tk_library variable uses a file
  34. # pathname that works better in Tk (of the style returned by
  35. # [file join], ie C:/path/to/tk/lib, not C:pathtotklib
  36. set tk_library [file join $tk_library]
  37. # Clear Tk's access for that interp (path).
  38. allowTk $slave $argv
  39. # there seems to be an obscure case where the tk_library
  40. # variable value is changed to point to a sym link destination
  41. # dir instead of the sym link itself, and thus where the $tk_library
  42. # would then not be anymore one of the auto_path dir, so we use
  43. # the addToAccessPath which adds if it's not already in instead
  44. # of the more conventional findInAccessPath.
  45. # Might be usefull for masters without Tk really loaded too.
  46. ::interp eval $slave [list set tk_library [::safe::interpAddToAccessPath $slave $tk_library]]
  47. return $slave
  48.     }
  49. # tkInterpLoadTk : 
  50. # Do additional configuration as needed (calling tkInterpInit) 
  51. # and actually load Tk into the slave.
  52. # Either contained in the specified windowId (-use) or
  53. # creating a decorated toplevel for it.
  54. # empty definition for auto_mkIndex
  55. proc ::safe::loadTk {} {}
  56.    
  57. ::tcl::OptProc loadTk {
  58.     {slave -interp "name of the slave interpreter"}
  59.     {-use  -windowId {} "window Id to use (new toplevel otherwise)"}
  60.     {-display -displayName {} "display name to use (current one otherwise)"}
  61. } {
  62.     set displayGiven [::tcl::OptProcArgGiven "-display"]
  63.     if {!$displayGiven} {
  64. # Try to get the current display from "."
  65. # (which might not exist if the master is tk-less)
  66. if {[catch {set display [winfo screen .]}]} {
  67.     if {[info exists ::env(DISPLAY)]} {
  68. set display $::env(DISPLAY)
  69.     } else {
  70. Log $slave "no winfo screen . nor env(DISPLAY)" WARNING
  71. set display ":0.0"
  72.     }
  73. }
  74.     }
  75.     if {![::tcl::OptProcArgGiven "-use"]} {
  76. # create a decorated toplevel
  77. ::tcl::Lassign [tkTopLevel $slave $display] w use
  78. # set our delete hook (slave arg is added by interpDelete)
  79. # to clean up both window related code and tkInit(slave)
  80. Set [DeleteHookName $slave] [list tkDelete {} $w]
  81.     } else {
  82. # set our delete hook (slave arg is added by interpDelete)
  83. # to clean up tkInit(slave)
  84.     
  85. Set [DeleteHookName $slave] [list disallowTk]
  86. # Let's be nice and also accept tk window names instead of ids
  87. if {[string match ".*" $use]} {
  88.     set windowName $use
  89.     set use [winfo id $windowName]
  90.     set nDisplay [winfo screen $windowName]
  91. } else {
  92.     # Check for a better -display value
  93.     # (works only for multi screens on single host, but not
  94.     #  cross hosts, for that a tk window name would be better
  95.     #  but embeding is also usefull for non tk names)
  96.     
  97.     if {![catch {winfo pathname $use} name]} {
  98. set nDisplay [winfo screen $name]
  99.     } else {
  100. # Can't have a better one
  101. set nDisplay $display
  102.     }
  103. }
  104. if {$nDisplay ne $display} {
  105.     if {$displayGiven} {
  106. error "conflicting -display $display and -use
  107. $use -> $nDisplay"
  108.     } else {
  109. set display $nDisplay
  110.     }
  111. }
  112.     }
  113.     # Prepares the slave for tk with those parameters
  114.     
  115.     tkInterpInit $slave [list "-use" $use "-display" $display]
  116.     
  117.     load {} Tk $slave
  118.     return $slave
  119. }
  120. proc ::safe::TkInit {interpPath} {
  121.     variable tkInit
  122.     if {[info exists tkInit($interpPath)]} {
  123. set value $tkInit($interpPath)
  124. Log $interpPath "TkInit called, returning "$value"" NOTICE
  125. return $value
  126.     } else {
  127. Log $interpPath "TkInit called for interp with clearance:
  128. preventing Tk init" ERROR
  129. error "not allowed"
  130.     }
  131. }
  132. # safe::allowTk --
  133. #
  134. # Set tkInit(interpPath) to allow Tk to be initialized in
  135. # safe::TkInit.
  136. #
  137. # Arguments:
  138. # interpPath slave interpreter handle
  139. # argv arguments passed to safe::TkInterpInit
  140. #
  141. # Results:
  142. # none.
  143. proc ::safe::allowTk {interpPath argv} {
  144.     variable tkInit
  145.     set tkInit($interpPath) $argv
  146.     return
  147. }
  148. # safe::disallowTk --
  149. #
  150. # Unset tkInit(interpPath) to disallow Tk from getting initialized
  151. # in safe::TkInit.
  152. #
  153. # Arguments:
  154. # interpPath slave interpreter handle
  155. #
  156. # Results:
  157. # none.
  158. proc ::safe::disallowTk {interpPath} {
  159.     variable tkInit
  160.     # This can already be deleted by the DeleteHook of the interp
  161.     if {[info exists tkInit($interpPath)]} {
  162. unset tkInit($interpPath)
  163.     }
  164.     return
  165. }
  166. # safe::tkDelete --
  167. #
  168. # Clean up the window associated with the interp being deleted.
  169. #
  170. # Arguments:
  171. # interpPath slave interpreter handle
  172. #
  173. # Results:
  174. # none.
  175. proc ::safe::tkDelete {W window slave} {
  176.     # we are going to be called for each widget... skip untill it's
  177.     # top level
  178.     Log $slave "Called tkDelete $W $window" NOTICE
  179.     if {[::interp exists $slave]} {
  180. if {[catch {::safe::interpDelete $slave} msg]} {
  181.     Log $slave "Deletion error : $msg"
  182. }
  183.     }
  184.     if {[winfo exists $window]} {
  185. Log $slave "Destroy toplevel $window" NOTICE
  186. destroy $window
  187.     }
  188.     
  189.     # clean up tkInit(slave)
  190.     disallowTk $slave
  191.     return
  192. }
  193. proc ::safe::tkTopLevel {slave display} {
  194.     variable tkSafeId
  195.     incr tkSafeId
  196.     set w ".safe$tkSafeId"
  197.     if {[catch {toplevel $w -screen $display -class SafeTk} msg]} {
  198. return -code error "Unable to create toplevel for
  199. safe slave "$slave" ($msg)"
  200.     }
  201.     Log $slave "New toplevel $w" NOTICE
  202.     set msg "Untrusted Tcl applet ($slave)"
  203.     wm title $w $msg
  204.     # Control frame
  205.     set wc $w.fc
  206.     frame $wc -bg red -borderwidth 3 -relief ridge
  207.     # We will destroy the interp when the window is destroyed
  208.     bindtags $wc [concat Safe$wc [bindtags $wc]]
  209.     bind Safe$wc <Destroy> [list ::safe::tkDelete %W $w $slave]
  210.     label $wc.l -text $msg -padx 2 -pady 0 -anchor w
  211.     # We want the button to be the last visible item
  212.     # (so be packed first) and at the right and not resizing horizontally
  213.     # frame the button so it does not expand horizontally
  214.     # but still have the default background instead of red one from the parent
  215.     frame  $wc.fb -bd 0
  216.     button $wc.fb.b -text "Delete" 
  217.     -bd 1  -padx 2 -pady 0 -highlightthickness 0 
  218.     -command [list ::safe::tkDelete $w $w $slave]
  219.     pack $wc.fb.b -side right -fill both
  220.     pack $wc.fb -side right -fill both -expand 1
  221.     pack $wc.l -side left  -fill both -expand 1
  222.     pack $wc -side bottom -fill x
  223.     # Container frame
  224.     frame $w.c -container 1
  225.     pack $w.c -fill both -expand 1
  226.     
  227.     # return both the toplevel window name and the id to use for embedding
  228.     list $w [winfo id $w.c]
  229. }
  230. }