| 1 |
/* |
| 2 |
* tclInterp.c -- |
| 3 |
* |
| 4 |
* This file implements the "interp" command which allows creation and |
| 5 |
* manipulation of Tcl interpreters from within Tcl scripts. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 2004 Donal K. Fellows |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution |
| 11 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* A pointer to a string that holds an initialization script that if non-NULL |
| 18 |
* is evaluated in Tcl_Init() prior to the built-in initialization script |
| 19 |
* above. This variable can be modified by the function below. |
| 20 |
*/ |
| 21 |
|
| 22 |
static const char *tclPreInitScript = NULL; |
| 23 |
|
| 24 |
/* Forward declaration */ |
| 25 |
struct Target; |
| 26 |
|
| 27 |
/* |
| 28 |
* struct Alias: |
| 29 |
* |
| 30 |
* Stores information about an alias. Is stored in the child interpreter and |
| 31 |
* used by the source command to find the target command in the parent when |
| 32 |
* the source command is invoked. |
| 33 |
*/ |
| 34 |
|
| 35 |
typedef struct Alias { |
| 36 |
Tcl_Obj *token; /* Token for the alias command in the child |
| 37 |
* interp. This used to be the command name in |
| 38 |
* the child when the alias was first |
| 39 |
* created. */ |
| 40 |
Tcl_Interp *targetInterp; /* Interp in which target command will be |
| 41 |
* invoked. */ |
| 42 |
Tcl_Command childCmd; /* Source command in child interpreter, bound |
| 43 |
* to command that invokes the target command |
| 44 |
* in the target interpreter. */ |
| 45 |
Tcl_HashEntry *aliasEntryPtr; |
| 46 |
/* Entry for the alias hash table in child. |
| 47 |
* This is used by alias deletion to remove |
| 48 |
* the alias from the child interpreter alias |
| 49 |
* table. */ |
| 50 |
struct Target *targetPtr; /* Entry for target command in parent. This is |
| 51 |
* used in the parent interpreter to map back |
| 52 |
* from the target command to aliases |
| 53 |
* redirecting to it. */ |
| 54 |
int objc; /* Count of Tcl_Obj in the prefix of the |
| 55 |
* target command to be invoked in the target |
| 56 |
* interpreter. Additional arguments specified |
| 57 |
* when calling the alias in the child interp |
| 58 |
* will be appended to the prefix before the |
| 59 |
* command is invoked. */ |
| 60 |
Tcl_Obj *objPtr; /* The first actual prefix object - the target |
| 61 |
* command name; this has to be at the end of |
| 62 |
* the structure, which will be extended to |
| 63 |
* accommodate the remaining objects in the |
| 64 |
* prefix. */ |
| 65 |
} Alias; |
| 66 |
|
| 67 |
/* |
| 68 |
* |
| 69 |
* struct Child: |
| 70 |
* |
| 71 |
* Used by the "interp" command to record and find information about child |
| 72 |
* interpreters. Maps from a command name in the parent to information about a |
| 73 |
* child interpreter, e.g. what aliases are defined in it. |
| 74 |
*/ |
| 75 |
|
| 76 |
typedef struct Child { |
| 77 |
Tcl_Interp *parentInterp; /* Parent interpreter for this child. */ |
| 78 |
Tcl_HashEntry *childEntryPtr; |
| 79 |
/* Hash entry in parents child table for this |
| 80 |
* child interpreter. Used to find this |
| 81 |
* record, and used when deleting the child |
| 82 |
* interpreter to delete it from the parent's |
| 83 |
* table. */ |
| 84 |
Tcl_Interp *childInterp; /* The child interpreter. */ |
| 85 |
Tcl_Command interpCmd; /* Interpreter object command. */ |
| 86 |
Tcl_HashTable aliasTable; /* Table which maps from names of commands in |
| 87 |
* child interpreter to struct Alias defined |
| 88 |
* below. */ |
| 89 |
} Child; |
| 90 |
|
| 91 |
/* |
| 92 |
* struct Target: |
| 93 |
* |
| 94 |
* Maps from parent interpreter commands back to the source commands in child |
| 95 |
* interpreters. This is needed because aliases can be created between sibling |
| 96 |
* interpreters and must be deleted when the target interpreter is deleted. In |
| 97 |
* case they would not be deleted the source interpreter would be left with a |
| 98 |
* "dangling pointer". One such record is stored in the Parent record of the |
| 99 |
* parent interpreter with the parent for each alias which directs to a |
| 100 |
* command in the parent. These records are used to remove the source command |
| 101 |
* for an from a child if/when the parent is deleted. They are organized in a |
| 102 |
* doubly-linked list attached to the parent interpreter. |
| 103 |
*/ |
| 104 |
|
| 105 |
typedef struct Target { |
| 106 |
Tcl_Command childCmd; /* Command for alias in child interp. */ |
| 107 |
Tcl_Interp *childInterp; /* Child Interpreter. */ |
| 108 |
struct Target *nextPtr; /* Next in list of target records, or NULL if |
| 109 |
* at the end of the list of targets. */ |
| 110 |
struct Target *prevPtr; /* Previous in list of target records, or NULL |
| 111 |
* if at the start of the list of targets. */ |
| 112 |
} Target; |
| 113 |
|
| 114 |
/* |
| 115 |
* struct Parent: |
| 116 |
* |
| 117 |
* This record is used for two purposes: First, childTable (a hashtable) maps |
| 118 |
* from names of commands to child interpreters. This hashtable is used to |
| 119 |
* store information about child interpreters of this interpreter, to map over |
| 120 |
* all children, etc. The second purpose is to store information about all |
| 121 |
* aliases in children (or siblings) which direct to target commands in this |
| 122 |
* interpreter (using the targetsPtr doubly-linked list). |
| 123 |
* |
| 124 |
* NB: the flags field in the interp structure, used with SAFE_INTERP mask |
| 125 |
* denotes whether the interpreter is safe or not. Safe interpreters have |
| 126 |
* restricted functionality, can only create safe interpreters and can |
| 127 |
* only load safe extensions. |
| 128 |
*/ |
| 129 |
|
| 130 |
typedef struct Parent { |
| 131 |
Tcl_HashTable childTable; /* Hash table for child interpreters. Maps |
| 132 |
* from command names to Child records. */ |
| 133 |
Target *targetsPtr; /* The head of a doubly-linked list of all the |
| 134 |
* target records which denote aliases from |
| 135 |
* children or sibling interpreters that direct |
| 136 |
* to commands in this interpreter. This list |
| 137 |
* is used to remove dangling pointers from |
| 138 |
* the child (or sibling) interpreters when |
| 139 |
* this interpreter is deleted. */ |
| 140 |
} Parent; |
| 141 |
|
| 142 |
/* |
| 143 |
* The following structure keeps track of all the Parent and Child information |
| 144 |
* on a per-interp basis. |
| 145 |
*/ |
| 146 |
|
| 147 |
typedef struct InterpInfo { |
| 148 |
Parent parent; /* Keeps track of all interps for which this |
| 149 |
* interp is the Parent. */ |
| 150 |
Child child; /* Information necessary for this interp to |
| 151 |
* function as a child. */ |
| 152 |
} InterpInfo; |
| 153 |
|
| 154 |
/* |
| 155 |
* Limit callbacks handled by scripts are modelled as structures which are |
| 156 |
* stored in hashes indexed by a two-word key. Note that the type of the |
| 157 |
* 'type' field in the key is not int; this is to make sure that things are |
| 158 |
* likely to work properly on 64-bit architectures. |
| 159 |
*/ |
| 160 |
|
| 161 |
typedef struct ScriptLimitCallback { |
| 162 |
Tcl_Interp *interp; /* The interpreter in which to execute the |
| 163 |
* callback. */ |
| 164 |
Tcl_Obj *scriptObj; /* The script to execute to perform the |
| 165 |
* user-defined part of the callback. */ |
| 166 |
int type; /* What kind of callback is this. */ |
| 167 |
Tcl_HashEntry *entryPtr; /* The entry in the hash table maintained by |
| 168 |
* the target interpreter that refers to this |
| 169 |
* callback record, or NULL if the entry has |
| 170 |
* already been deleted from that hash |
| 171 |
* table. */ |
| 172 |
} ScriptLimitCallback; |
| 173 |
|
| 174 |
typedef struct ScriptLimitCallbackKey { |
| 175 |
Tcl_Interp *interp; /* The interpreter that the limit callback was |
| 176 |
* attached to. This is not the interpreter |
| 177 |
* that the callback runs in! */ |
| 178 |
int type; /* The type of callback that this is. */ |
| 179 |
} ScriptLimitCallbackKey; |
| 180 |
|
| 181 |
/* |
| 182 |
* TIP#143 limit handler internal representation. |
| 183 |
*/ |
| 184 |
|
| 185 |
struct LimitHandler { |
| 186 |
int flags; /* The state of this particular handler. */ |
| 187 |
Tcl_LimitHandlerProc *handlerProc; |
| 188 |
/* The handler callback. */ |
| 189 |
ClientData clientData; /* Opaque argument to the handler callback. */ |
| 190 |
Tcl_LimitHandlerDeleteProc *deleteProc; |
| 191 |
/* How to delete the clientData. */ |
| 192 |
LimitHandler *prevPtr; /* Previous item in linked list of |
| 193 |
* handlers. */ |
| 194 |
LimitHandler *nextPtr; /* Next item in linked list of handlers. */ |
| 195 |
}; |
| 196 |
|
| 197 |
/* |
| 198 |
* Values for the LimitHandler flags field. |
| 199 |
* LIMIT_HANDLER_ACTIVE - Whether the handler is currently being |
| 200 |
* processed; handlers are never to be reentered. |
| 201 |
* LIMIT_HANDLER_DELETED - Whether the handler has been deleted. This |
| 202 |
* should not normally be observed because when a handler is |
| 203 |
* deleted it is also spliced out of the list of handlers, but |
| 204 |
* even so we will be careful. |
| 205 |
*/ |
| 206 |
|
| 207 |
#define LIMIT_HANDLER_ACTIVE 0x01 |
| 208 |
#define LIMIT_HANDLER_DELETED 0x02 |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
/* |
| 213 |
* Prototypes for local static functions: |
| 214 |
*/ |
| 215 |
|
| 216 |
static int AliasCreate(Tcl_Interp *interp, |
| 217 |
Tcl_Interp *childInterp, Tcl_Interp *parentInterp, |
| 218 |
Tcl_Obj *namePtr, Tcl_Obj *targetPtr, int objc, |
| 219 |
Tcl_Obj *const objv[]); |
| 220 |
static int AliasDelete(Tcl_Interp *interp, |
| 221 |
Tcl_Interp *childInterp, Tcl_Obj *namePtr); |
| 222 |
static int AliasDescribe(Tcl_Interp *interp, |
| 223 |
Tcl_Interp *childInterp, Tcl_Obj *objPtr); |
| 224 |
static int AliasList(Tcl_Interp *interp, Tcl_Interp *childInterp); |
| 225 |
static int AliasObjCmd(ClientData dummy, |
| 226 |
Tcl_Interp *currentInterp, int objc, |
| 227 |
Tcl_Obj *const objv[]); |
| 228 |
static Tcl_ObjCmdProc AliasNRCmd; |
| 229 |
static Tcl_CmdDeleteProc AliasObjCmdDeleteProc; |
| 230 |
static Tcl_Interp * GetInterp(Tcl_Interp *interp, Tcl_Obj *pathPtr); |
| 231 |
static Tcl_Interp * GetInterp2(Tcl_Interp *interp, int objc, |
| 232 |
Tcl_Obj *const objv[]); |
| 233 |
static Tcl_InterpDeleteProc InterpInfoDeleteProc; |
| 234 |
static int ChildBgerror(Tcl_Interp *interp, |
| 235 |
Tcl_Interp *childInterp, int objc, |
| 236 |
Tcl_Obj *const objv[]); |
| 237 |
static Tcl_Interp * ChildCreate(Tcl_Interp *interp, Tcl_Obj *pathPtr, |
| 238 |
int safe); |
| 239 |
static int ChildDebugCmd(Tcl_Interp *interp, |
| 240 |
Tcl_Interp *childInterp, |
| 241 |
int objc, Tcl_Obj *const objv[]); |
| 242 |
static int ChildEval(Tcl_Interp *interp, Tcl_Interp *childInterp, |
| 243 |
int objc, Tcl_Obj *const objv[]); |
| 244 |
static int ChildExpose(Tcl_Interp *interp, |
| 245 |
Tcl_Interp *childInterp, int objc, |
| 246 |
Tcl_Obj *const objv[]); |
| 247 |
static int ChildHide(Tcl_Interp *interp, Tcl_Interp *childInterp, |
| 248 |
int objc, Tcl_Obj *const objv[]); |
| 249 |
static int ChildHidden(Tcl_Interp *interp, |
| 250 |
Tcl_Interp *childInterp); |
| 251 |
static int ChildInvokeHidden(Tcl_Interp *interp, |
| 252 |
Tcl_Interp *childInterp, |
| 253 |
const char *namespaceName, |
| 254 |
int objc, Tcl_Obj *const objv[]); |
| 255 |
static int ChildMarkTrusted(Tcl_Interp *interp, |
| 256 |
Tcl_Interp *childInterp); |
| 257 |
static int ChildObjCmd(ClientData dummy, Tcl_Interp *interp, |
| 258 |
int objc, Tcl_Obj *const objv[]); |
| 259 |
static Tcl_CmdDeleteProc ChildObjCmdDeleteProc; |
| 260 |
static int ChildRecursionLimit(Tcl_Interp *interp, |
| 261 |
Tcl_Interp *childInterp, int objc, |
| 262 |
Tcl_Obj *const objv[]); |
| 263 |
static int ChildCommandLimitCmd(Tcl_Interp *interp, |
| 264 |
Tcl_Interp *childInterp, int consumedObjc, |
| 265 |
int objc, Tcl_Obj *const objv[]); |
| 266 |
static int ChildTimeLimitCmd(Tcl_Interp *interp, |
| 267 |
Tcl_Interp *childInterp, int consumedObjc, |
| 268 |
int objc, Tcl_Obj *const objv[]); |
| 269 |
static void InheritLimitsFromParent(Tcl_Interp *childInterp, |
| 270 |
Tcl_Interp *parentInterp); |
| 271 |
static void SetScriptLimitCallback(Tcl_Interp *interp, int type, |
| 272 |
Tcl_Interp *targetInterp, Tcl_Obj *scriptObj); |
| 273 |
static void CallScriptLimitCallback(ClientData clientData, |
| 274 |
Tcl_Interp *interp); |
| 275 |
static void DeleteScriptLimitCallback(ClientData clientData); |
| 276 |
static void RunLimitHandlers(LimitHandler *handlerPtr, |
| 277 |
Tcl_Interp *interp); |
| 278 |
static void TimeLimitCallback(ClientData clientData); |
| 279 |
|
| 280 |
/* NRE enabling */ |
| 281 |
static Tcl_NRPostProc NRPostInvokeHidden; |
| 282 |
static Tcl_ObjCmdProc NRInterpCmd; |
| 283 |
static Tcl_ObjCmdProc NRChildCmd; |
| 284 |
|
| 285 |
|
| 286 |
/* |
| 287 |
*---------------------------------------------------------------------- |
| 288 |
* |
| 289 |
* TclSetPreInitScript -- |
| 290 |
* |
| 291 |
* This routine is used to change the value of the internal variable, |
| 292 |
* tclPreInitScript. |
| 293 |
* |
| 294 |
* Results: |
| 295 |
* Returns the current value of tclPreInitScript. |
| 296 |
* |
| 297 |
* Side effects: |
| 298 |
* Changes the way Tcl_Init() routine behaves. |
| 299 |
* |
| 300 |
*---------------------------------------------------------------------- |
| 301 |
*/ |
| 302 |
|
| 303 |
const char * |
| 304 |
TclSetPreInitScript( |
| 305 |
const char *string) /* Pointer to a script. */ |
| 306 |
{ |
| 307 |
const char *prevString = tclPreInitScript; |
| 308 |
tclPreInitScript = string; |
| 309 |
return prevString; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
*---------------------------------------------------------------------- |
| 314 |
* |
| 315 |
* Tcl_Init -- |
| 316 |
* |
| 317 |
* This function is typically invoked by Tcl_AppInit functions to find |
| 318 |
* and source the "init.tcl" script, which should exist somewhere on the |
| 319 |
* Tcl library path. |
| 320 |
* |
| 321 |
* Results: |
| 322 |
* Returns a standard Tcl completion code and sets the interp's result if |
| 323 |
* there is an error. |
| 324 |
* |
| 325 |
* Side effects: |
| 326 |
* Depends on what's in the init.tcl script. |
| 327 |
* |
| 328 |
*---------------------------------------------------------------------- |
| 329 |
*/ |
| 330 |
|
| 331 |
int |
| 332 |
Tcl_Init( |
| 333 |
Tcl_Interp *interp) /* Interpreter to initialize. */ |
| 334 |
{ |
| 335 |
if (tclPreInitScript != NULL) { |
| 336 |
if (Tcl_Eval(interp, tclPreInitScript) == TCL_ERROR) { |
| 337 |
return TCL_ERROR; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/* |
| 342 |
* In order to find init.tcl during initialization, the following script |
| 343 |
* is invoked by Tcl_Init(). It looks in several different directories: |
| 344 |
* |
| 345 |
* $tcl_library - can specify a primary location, if set, no |
| 346 |
* other locations will be checked. This is the |
| 347 |
* recommended way for a program that embeds |
| 348 |
* Tcl to specifically tell Tcl where to find |
| 349 |
* an init.tcl file. |
| 350 |
* |
| 351 |
* $env(TCL_LIBRARY) - highest priority so user can always override |
| 352 |
* the search path unless the application has |
| 353 |
* specified an exact directory above |
| 354 |
* |
| 355 |
* $tclDefaultLibrary - INTERNAL: This variable is set by Tcl on |
| 356 |
* those platforms where it can determine at |
| 357 |
* runtime the directory where it expects the |
| 358 |
* init.tcl file to be. After [tclInit] reads |
| 359 |
* and uses this value, it [unset]s it. |
| 360 |
* External users of Tcl should not make use of |
| 361 |
* the variable to customize [tclInit]. |
| 362 |
* |
| 363 |
* $tcl_libPath - OBSOLETE: This variable is no longer set by |
| 364 |
* Tcl itself, but [tclInit] examines it in |
| 365 |
* case some program that embeds Tcl is |
| 366 |
* customizing [tclInit] by setting this |
| 367 |
* variable to a list of directories in which |
| 368 |
* to search. |
| 369 |
* |
| 370 |
* [tcl::pkgconfig get scriptdir,runtime] |
| 371 |
* - the directory determined by configure to be |
| 372 |
* the place where Tcl's script library is to |
| 373 |
* be installed. |
| 374 |
* |
| 375 |
* The first directory on this path that contains a valid init.tcl script |
| 376 |
* will be set as the value of tcl_library. |
| 377 |
* |
| 378 |
* Note that this entire search mechanism can be bypassed by defining an |
| 379 |
* alternate tclInit command before calling Tcl_Init(). |
| 380 |
*/ |
| 381 |
|
| 382 |
return Tcl_Eval(interp, |
| 383 |
"if {[namespace which -command tclInit] eq \"\"} {\n" |
| 384 |
" proc tclInit {} {\n" |
| 385 |
" global tcl_libPath tcl_library env tclDefaultLibrary\n" |
| 386 |
" rename tclInit {}\n" |
| 387 |
" if {[info exists tcl_library]} {\n" |
| 388 |
" set scripts {{set tcl_library}}\n" |
| 389 |
" } else {\n" |
| 390 |
" set scripts {}\n" |
| 391 |
" if {[info exists env(TCL_LIBRARY)] && ($env(TCL_LIBRARY) ne {})} {\n" |
| 392 |
" lappend scripts {set env(TCL_LIBRARY)}\n" |
| 393 |
" lappend scripts {\n" |
| 394 |
"if {[regexp ^tcl(.*)$ [file tail $env(TCL_LIBRARY)] -> tail] == 0} continue\n" |
| 395 |
"if {$tail eq [info tclversion]} continue\n" |
| 396 |
"file join [file dirname $env(TCL_LIBRARY)] tcl[info tclversion]}\n" |
| 397 |
" }\n" |
| 398 |
" if {[info exists tclDefaultLibrary]} {\n" |
| 399 |
" lappend scripts {set tclDefaultLibrary}\n" |
| 400 |
" } else {\n" |
| 401 |
" lappend scripts {::tcl::pkgconfig get scriptdir,runtime}\n" |
| 402 |
" }\n" |
| 403 |
" lappend scripts {\n" |
| 404 |
"set parentDir [file dirname [file dirname [info nameofexecutable]]]\n" |
| 405 |
"set grandParentDir [file dirname $parentDir]\n" |
| 406 |
"file join $parentDir lib tcl[info tclversion]} \\\n" |
| 407 |
" {file join $grandParentDir lib tcl[info tclversion]} \\\n" |
| 408 |
" {file join $parentDir library} \\\n" |
| 409 |
" {file join $grandParentDir library} \\\n" |
| 410 |
" {file join $grandParentDir tcl[info patchlevel] library} \\\n" |
| 411 |
" {\n" |
| 412 |
"file join [file dirname $grandParentDir] tcl[info patchlevel] library}\n" |
| 413 |
" if {[info exists tcl_libPath]\n" |
| 414 |
" && [catch {llength $tcl_libPath} len] == 0} {\n" |
| 415 |
" for {set i 0} {$i < $len} {incr i} {\n" |
| 416 |
" lappend scripts [list lindex \\$tcl_libPath $i]\n" |
| 417 |
" }\n" |
| 418 |
" }\n" |
| 419 |
" }\n" |
| 420 |
" set dirs {}\n" |
| 421 |
" set errors {}\n" |
| 422 |
" foreach script $scripts {\n" |
| 423 |
" if {[set tcl_library [eval $script]] eq \"\"} continue\n" |
| 424 |
" set tclfile [file join $tcl_library init.tcl]\n" |
| 425 |
" if {[file exists $tclfile]} {\n" |
| 426 |
" if {[catch {uplevel #0 [list source $tclfile]} msg opts]} {\n" |
| 427 |
" append errors \"$tclfile: $msg\n\"\n" |
| 428 |
" append errors \"[dict get $opts -errorinfo]\n\"\n" |
| 429 |
" continue\n" |
| 430 |
" }\n" |
| 431 |
" unset -nocomplain tclDefaultLibrary\n" |
| 432 |
" return\n" |
| 433 |
" }\n" |
| 434 |
" lappend dirs $tcl_library\n" |
| 435 |
" }\n" |
| 436 |
" unset -nocomplain tclDefaultLibrary\n" |
| 437 |
" set msg \"Can't find a usable init.tcl in the following directories: \n\"\n" |
| 438 |
" append msg \" $dirs\n\n\"\n" |
| 439 |
" append msg \"$errors\n\n\"\n" |
| 440 |
" append msg \"This probably means that Tcl wasn't installed properly.\n\"\n" |
| 441 |
" error $msg\n" |
| 442 |
" }\n" |
| 443 |
"}\n" |
| 444 |
"tclInit"); |
| 445 |
} |
| 446 |
|
| 447 |
/* |
| 448 |
*--------------------------------------------------------------------------- |
| 449 |
* |
| 450 |
* TclInterpInit -- |
| 451 |
* |
| 452 |
* Initializes the invoking interpreter for using the parent, child and |
| 453 |
* safe interp facilities. This is called from inside Tcl_CreateInterp(). |
| 454 |
* |
| 455 |
* Results: |
| 456 |
* Always returns TCL_OK for backwards compatibility. |
| 457 |
* |
| 458 |
* Side effects: |
| 459 |
* Adds the "interp" command to an interpreter and initializes the |
| 460 |
* interpInfoPtr field of the invoking interpreter. |
| 461 |
* |
| 462 |
*--------------------------------------------------------------------------- |
| 463 |
*/ |
| 464 |
|
| 465 |
int |
| 466 |
TclInterpInit( |
| 467 |
Tcl_Interp *interp) /* Interpreter to initialize. */ |
| 468 |
{ |
| 469 |
InterpInfo *interpInfoPtr; |
| 470 |
Parent *parentPtr; |
| 471 |
Child *childPtr; |
| 472 |
|
| 473 |
interpInfoPtr = (InterpInfo *)ckalloc(sizeof(InterpInfo)); |
| 474 |
((Interp *) interp)->interpInfo = interpInfoPtr; |
| 475 |
|
| 476 |
parentPtr = &interpInfoPtr->parent; |
| 477 |
Tcl_InitHashTable(&parentPtr->childTable, TCL_STRING_KEYS); |
| 478 |
parentPtr->targetsPtr = NULL; |
| 479 |
|
| 480 |
childPtr = &interpInfoPtr->child; |
| 481 |
childPtr->parentInterp = NULL; |
| 482 |
childPtr->childEntryPtr = NULL; |
| 483 |
childPtr->childInterp = interp; |
| 484 |
childPtr->interpCmd = NULL; |
| 485 |
Tcl_InitHashTable(&childPtr->aliasTable, TCL_STRING_KEYS); |
| 486 |
|
| 487 |
Tcl_NRCreateCommand(interp, "interp", Tcl_InterpObjCmd, NRInterpCmd, |
| 488 |
NULL, NULL); |
| 489 |
|
| 490 |
Tcl_CallWhenDeleted(interp, InterpInfoDeleteProc, NULL); |
| 491 |
return TCL_OK; |
| 492 |
} |
| 493 |
|
| 494 |
/* |
| 495 |
*--------------------------------------------------------------------------- |
| 496 |
* |
| 497 |
* InterpInfoDeleteProc -- |
| 498 |
* |
| 499 |
* Invoked when an interpreter is being deleted. It releases all storage |
| 500 |
* used by the parent/child/safe interpreter facilities. |
| 501 |
* |
| 502 |
* Results: |
| 503 |
* None. |
| 504 |
* |
| 505 |
* Side effects: |
| 506 |
* Cleans up storage. Sets the interpInfoPtr field of the interp to NULL. |
| 507 |
* |
| 508 |
*--------------------------------------------------------------------------- |
| 509 |
*/ |
| 510 |
|
| 511 |
static void |
| 512 |
InterpInfoDeleteProc( |
| 513 |
ClientData clientData, /* Ignored. */ |
| 514 |
Tcl_Interp *interp) /* Interp being deleted. All commands for |
| 515 |
* child interps should already be deleted. */ |
| 516 |
{ |
| 517 |
InterpInfo *interpInfoPtr; |
| 518 |
Child *childPtr; |
| 519 |
Parent *parentPtr; |
| 520 |
Target *targetPtr; |
| 521 |
|
| 522 |
interpInfoPtr = (InterpInfo *) ((Interp *) interp)->interpInfo; |
| 523 |
|
| 524 |
/* |
| 525 |
* There shouldn't be any commands left. |
| 526 |
*/ |
| 527 |
|
| 528 |
parentPtr = &interpInfoPtr->parent; |
| 529 |
if (parentPtr->childTable.numEntries != 0) { |
| 530 |
Tcl_Panic("InterpInfoDeleteProc: still exist commands"); |
| 531 |
} |
| 532 |
Tcl_DeleteHashTable(&parentPtr->childTable); |
| 533 |
|
| 534 |
/* |
| 535 |
* Tell any interps that have aliases to this interp that they should |
| 536 |
* delete those aliases. If the other interp was already dead, it would |
| 537 |
* have removed the target record already. |
| 538 |
*/ |
| 539 |
|
| 540 |
for (targetPtr = parentPtr->targetsPtr; targetPtr != NULL; ) { |
| 541 |
Target *tmpPtr = targetPtr->nextPtr; |
| 542 |
Tcl_DeleteCommandFromToken(targetPtr->childInterp, |
| 543 |
targetPtr->childCmd); |
| 544 |
targetPtr = tmpPtr; |
| 545 |
} |
| 546 |
|
| 547 |
childPtr = &interpInfoPtr->child; |
| 548 |
if (childPtr->interpCmd != NULL) { |
| 549 |
/* |
| 550 |
* Tcl_DeleteInterp() was called on this interpreter, rather "interp |
| 551 |
* delete" or the equivalent deletion of the command in the parent. |
| 552 |
* First ensure that the cleanup callback doesn't try to delete the |
| 553 |
* interp again. |
| 554 |
*/ |
| 555 |
|
| 556 |
childPtr->childInterp = NULL; |
| 557 |
Tcl_DeleteCommandFromToken(childPtr->parentInterp, |
| 558 |
childPtr->interpCmd); |
| 559 |
} |
| 560 |
|
| 561 |
/* |
| 562 |
* There shouldn't be any aliases left. |
| 563 |
*/ |
| 564 |
|
| 565 |
if (childPtr->aliasTable.numEntries != 0) { |
| 566 |
Tcl_Panic("InterpInfoDeleteProc: still exist aliases"); |
| 567 |
} |
| 568 |
Tcl_DeleteHashTable(&childPtr->aliasTable); |
| 569 |
|
| 570 |
ckfree(interpInfoPtr); |
| 571 |
} |
| 572 |
|
| 573 |
/* |
| 574 |
*---------------------------------------------------------------------- |
| 575 |
* |
| 576 |
* Tcl_InterpObjCmd -- |
| 577 |
* |
| 578 |
* This function is invoked to process the "interp" Tcl command. See the |
| 579 |
* user documentation for details on what it does. |
| 580 |
* |
| 581 |
* Results: |
| 582 |
* A standard Tcl result. |
| 583 |
* |
| 584 |
* Side effects: |
| 585 |
* See the user documentation. |
| 586 |
* |
| 587 |
*---------------------------------------------------------------------- |
| 588 |
*/ |
| 589 |
|
| 590 |
int |
| 591 |
Tcl_InterpObjCmd( |
| 592 |
ClientData clientData, /* Unused. */ |
| 593 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 594 |
int objc, /* Number of arguments. */ |
| 595 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 596 |
{ |
| 597 |
return Tcl_NRCallObjProc(interp, NRInterpCmd, clientData, objc, objv); |
| 598 |
} |
| 599 |
|
| 600 |
static int |
| 601 |
NRInterpCmd( |
| 602 |
ClientData clientData, /* Unused. */ |
| 603 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 604 |
int objc, /* Number of arguments. */ |
| 605 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 606 |
{ |
| 607 |
Tcl_Interp *childInterp; |
| 608 |
int index; |
| 609 |
static const char *const options[] = { |
| 610 |
"alias", "aliases", "bgerror", "cancel", |
| 611 |
"children", "create", "debug", "delete", |
| 612 |
"eval", "exists", "expose", |
| 613 |
"hide", "hidden", "issafe", |
| 614 |
"invokehidden", "limit", "marktrusted", "recursionlimit", |
| 615 |
"slaves", "share", "target", "transfer", |
| 616 |
NULL |
| 617 |
}; |
| 618 |
enum interpOptionEnum { |
| 619 |
OPT_ALIAS, OPT_ALIASES, OPT_BGERROR, OPT_CANCEL, |
| 620 |
OPT_CHILDREN, OPT_CREATE, OPT_DEBUG, OPT_DELETE, |
| 621 |
OPT_EVAL, OPT_EXISTS, OPT_EXPOSE, |
| 622 |
OPT_HIDE, OPT_HIDDEN, OPT_ISSAFE, |
| 623 |
OPT_INVOKEHID, OPT_LIMIT, OPT_MARKTRUSTED,OPT_RECLIMIT, |
| 624 |
OPT_SLAVES, OPT_SHARE, OPT_TARGET, OPT_TRANSFER |
| 625 |
}; |
| 626 |
|
| 627 |
if (objc < 2) { |
| 628 |
Tcl_WrongNumArgs(interp, 1, objv, "cmd ?arg ...?"); |
| 629 |
return TCL_ERROR; |
| 630 |
} |
| 631 |
if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0, |
| 632 |
&index) != TCL_OK) { |
| 633 |
return TCL_ERROR; |
| 634 |
} |
| 635 |
switch ((enum interpOptionEnum)index) { |
| 636 |
case OPT_ALIAS: { |
| 637 |
Tcl_Interp *parentInterp; |
| 638 |
|
| 639 |
if (objc < 4) { |
| 640 |
aliasArgs: |
| 641 |
Tcl_WrongNumArgs(interp, 2, objv, |
| 642 |
"slavePath slaveCmd ?masterPath masterCmd? ?arg ...?"); |
| 643 |
return TCL_ERROR; |
| 644 |
} |
| 645 |
childInterp = GetInterp(interp, objv[2]); |
| 646 |
if (childInterp == NULL) { |
| 647 |
return TCL_ERROR; |
| 648 |
} |
| 649 |
if (objc == 4) { |
| 650 |
return AliasDescribe(interp, childInterp, objv[3]); |
| 651 |
} |
| 652 |
if ((objc == 5) && (TclGetString(objv[4])[0] == '\0')) { |
| 653 |
return AliasDelete(interp, childInterp, objv[3]); |
| 654 |
} |
| 655 |
if (objc > 5) { |
| 656 |
parentInterp = GetInterp(interp, objv[4]); |
| 657 |
if (parentInterp == NULL) { |
| 658 |
return TCL_ERROR; |
| 659 |
} |
| 660 |
|
| 661 |
return AliasCreate(interp, childInterp, parentInterp, objv[3], |
| 662 |
objv[5], objc - 6, objv + 6); |
| 663 |
} |
| 664 |
goto aliasArgs; |
| 665 |
} |
| 666 |
case OPT_ALIASES: |
| 667 |
childInterp = GetInterp2(interp, objc, objv); |
| 668 |
if (childInterp == NULL) { |
| 669 |
return TCL_ERROR; |
| 670 |
} |
| 671 |
return AliasList(interp, childInterp); |
| 672 |
case OPT_BGERROR: |
| 673 |
if (objc != 3 && objc != 4) { |
| 674 |
Tcl_WrongNumArgs(interp, 2, objv, "path ?cmdPrefix?"); |
| 675 |
return TCL_ERROR; |
| 676 |
} |
| 677 |
childInterp = GetInterp(interp, objv[2]); |
| 678 |
if (childInterp == NULL) { |
| 679 |
return TCL_ERROR; |
| 680 |
} |
| 681 |
return ChildBgerror(interp, childInterp, objc - 3, objv + 3); |
| 682 |
case OPT_CANCEL: { |
| 683 |
int i, flags; |
| 684 |
Tcl_Obj *resultObjPtr; |
| 685 |
static const char *const cancelOptions[] = { |
| 686 |
"-unwind", "--", NULL |
| 687 |
}; |
| 688 |
enum optionCancelEnum { |
| 689 |
OPT_UNWIND, OPT_LAST |
| 690 |
}; |
| 691 |
|
| 692 |
flags = 0; |
| 693 |
|
| 694 |
for (i = 2; i < objc; i++) { |
| 695 |
if (TclGetString(objv[i])[0] != '-') { |
| 696 |
break; |
| 697 |
} |
| 698 |
if (Tcl_GetIndexFromObj(interp, objv[i], cancelOptions, "option", |
| 699 |
0, &index) != TCL_OK) { |
| 700 |
return TCL_ERROR; |
| 701 |
} |
| 702 |
|
| 703 |
switch ((enum optionCancelEnum) index) { |
| 704 |
case OPT_UNWIND: |
| 705 |
/* |
| 706 |
* The evaluation stack in the target interp is to be unwound. |
| 707 |
*/ |
| 708 |
|
| 709 |
flags |= TCL_CANCEL_UNWIND; |
| 710 |
break; |
| 711 |
case OPT_LAST: |
| 712 |
i++; |
| 713 |
goto endOfForLoop; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
endOfForLoop: |
| 718 |
if (i < objc - 2) { |
| 719 |
Tcl_WrongNumArgs(interp, 2, objv, |
| 720 |
"?-unwind? ?--? ?path? ?result?"); |
| 721 |
return TCL_ERROR; |
| 722 |
} |
| 723 |
|
| 724 |
/* |
| 725 |
* Did they specify a child interp to cancel the script in progress |
| 726 |
* in? If not, use the current interp. |
| 727 |
*/ |
| 728 |
|
| 729 |
if (i < objc) { |
| 730 |
childInterp = GetInterp(interp, objv[i]); |
| 731 |
if (childInterp == NULL) { |
| 732 |
return TCL_ERROR; |
| 733 |
} |
| 734 |
i++; |
| 735 |
} else { |
| 736 |
childInterp = interp; |
| 737 |
} |
| 738 |
|
| 739 |
if (i < objc) { |
| 740 |
resultObjPtr = objv[i]; |
| 741 |
|
| 742 |
/* |
| 743 |
* Tcl_CancelEval removes this reference. |
| 744 |
*/ |
| 745 |
|
| 746 |
Tcl_IncrRefCount(resultObjPtr); |
| 747 |
i++; |
| 748 |
} else { |
| 749 |
resultObjPtr = NULL; |
| 750 |
} |
| 751 |
|
| 752 |
return Tcl_CancelEval(childInterp, resultObjPtr, 0, flags); |
| 753 |
} |
| 754 |
case OPT_CREATE: { |
| 755 |
int i, last, safe; |
| 756 |
Tcl_Obj *childPtr; |
| 757 |
char buf[16 + TCL_INTEGER_SPACE]; |
| 758 |
static const char *const createOptions[] = { |
| 759 |
"-safe", "--", NULL |
| 760 |
}; |
| 761 |
enum option { |
| 762 |
OPT_SAFE, OPT_LAST |
| 763 |
}; |
| 764 |
|
| 765 |
safe = Tcl_IsSafe(interp); |
| 766 |
|
| 767 |
/* |
| 768 |
* Weird historical rules: "-safe" is accepted at the end, too. |
| 769 |
*/ |
| 770 |
|
| 771 |
childPtr = NULL; |
| 772 |
last = 0; |
| 773 |
for (i = 2; i < objc; i++) { |
| 774 |
if ((last == 0) && (TclGetString(objv[i])[0] == '-')) { |
| 775 |
if (Tcl_GetIndexFromObj(interp, objv[i], createOptions, |
| 776 |
"option", 0, &index) != TCL_OK) { |
| 777 |
return TCL_ERROR; |
| 778 |
} |
| 779 |
if (index == OPT_SAFE) { |
| 780 |
safe = 1; |
| 781 |
continue; |
| 782 |
} |
| 783 |
i++; |
| 784 |
last = 1; |
| 785 |
} |
| 786 |
if (childPtr != NULL) { |
| 787 |
Tcl_WrongNumArgs(interp, 2, objv, "?-safe? ?--? ?path?"); |
| 788 |
return TCL_ERROR; |
| 789 |
} |
| 790 |
if (i < objc) { |
| 791 |
childPtr = objv[i]; |
| 792 |
} |
| 793 |
} |
| 794 |
buf[0] = '\0'; |
| 795 |
if (childPtr == NULL) { |
| 796 |
/* |
| 797 |
* Create an anonymous interpreter -- we choose its name and the |
| 798 |
* name of the command. We check that the command name that we use |
| 799 |
* for the interpreter does not collide with an existing command |
| 800 |
* in the parent interpreter. |
| 801 |
*/ |
| 802 |
|
| 803 |
for (i = 0; ; i++) { |
| 804 |
Tcl_CmdInfo cmdInfo; |
| 805 |
|
| 806 |
snprintf(buf, sizeof(buf), "interp%d", i); |
| 807 |
if (Tcl_GetCommandInfo(interp, buf, &cmdInfo) == 0) { |
| 808 |
break; |
| 809 |
} |
| 810 |
} |
| 811 |
childPtr = Tcl_NewStringObj(buf, -1); |
| 812 |
} |
| 813 |
if (ChildCreate(interp, childPtr, safe) == NULL) { |
| 814 |
if (buf[0] != '\0') { |
| 815 |
Tcl_DecrRefCount(childPtr); |
| 816 |
} |
| 817 |
return TCL_ERROR; |
| 818 |
} |
| 819 |
Tcl_SetObjResult(interp, childPtr); |
| 820 |
return TCL_OK; |
| 821 |
} |
| 822 |
case OPT_DEBUG: /* TIP #378 */ |
| 823 |
/* |
| 824 |
* Currently only -frame supported, otherwise ?-option ?value?? |
| 825 |
*/ |
| 826 |
|
| 827 |
if (objc < 3 || objc > 5) { |
| 828 |
Tcl_WrongNumArgs(interp, 2, objv, "path ?-frame ?bool??"); |
| 829 |
return TCL_ERROR; |
| 830 |
} |
| 831 |
childInterp = GetInterp(interp, objv[2]); |
| 832 |
if (childInterp == NULL) { |
| 833 |
return TCL_ERROR; |
| 834 |
} |
| 835 |
return ChildDebugCmd(interp, childInterp, objc - 3, objv + 3); |
| 836 |
case OPT_DELETE: { |
| 837 |
int i; |
| 838 |
InterpInfo *iiPtr; |
| 839 |
|
| 840 |
for (i = 2; i < objc; i++) { |
| 841 |
childInterp = GetInterp(interp, objv[i]); |
| 842 |
if (childInterp == NULL) { |
| 843 |
return TCL_ERROR; |
| 844 |
} else if (childInterp == interp) { |
| 845 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 846 |
"cannot delete the current interpreter", -1)); |
| 847 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 848 |
"DELETESELF", (char *)NULL); |
| 849 |
return TCL_ERROR; |
| 850 |
} |
| 851 |
iiPtr = (InterpInfo *) ((Interp *) childInterp)->interpInfo; |
| 852 |
Tcl_DeleteCommandFromToken(iiPtr->child.parentInterp, |
| 853 |
iiPtr->child.interpCmd); |
| 854 |
} |
| 855 |
return TCL_OK; |
| 856 |
} |
| 857 |
case OPT_EVAL: |
| 858 |
if (objc < 4) { |
| 859 |
Tcl_WrongNumArgs(interp, 2, objv, "path arg ?arg ...?"); |
| 860 |
return TCL_ERROR; |
| 861 |
} |
| 862 |
childInterp = GetInterp(interp, objv[2]); |
| 863 |
if (childInterp == NULL) { |
| 864 |
return TCL_ERROR; |
| 865 |
} |
| 866 |
return ChildEval(interp, childInterp, objc - 3, objv + 3); |
| 867 |
case OPT_EXISTS: { |
| 868 |
int exists = 1; |
| 869 |
|
| 870 |
childInterp = GetInterp2(interp, objc, objv); |
| 871 |
if (childInterp == NULL) { |
| 872 |
if (objc > 3) { |
| 873 |
return TCL_ERROR; |
| 874 |
} |
| 875 |
Tcl_ResetResult(interp); |
| 876 |
exists = 0; |
| 877 |
} |
| 878 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(exists)); |
| 879 |
return TCL_OK; |
| 880 |
} |
| 881 |
case OPT_EXPOSE: |
| 882 |
if ((objc < 4) || (objc > 5)) { |
| 883 |
Tcl_WrongNumArgs(interp, 2, objv, "path hiddenCmdName ?cmdName?"); |
| 884 |
return TCL_ERROR; |
| 885 |
} |
| 886 |
childInterp = GetInterp(interp, objv[2]); |
| 887 |
if (childInterp == NULL) { |
| 888 |
return TCL_ERROR; |
| 889 |
} |
| 890 |
return ChildExpose(interp, childInterp, objc - 3, objv + 3); |
| 891 |
case OPT_HIDE: |
| 892 |
if ((objc < 4) || (objc > 5)) { |
| 893 |
Tcl_WrongNumArgs(interp, 2, objv, "path cmdName ?hiddenCmdName?"); |
| 894 |
return TCL_ERROR; |
| 895 |
} |
| 896 |
childInterp = GetInterp(interp, objv[2]); |
| 897 |
if (childInterp == NULL) { |
| 898 |
return TCL_ERROR; |
| 899 |
} |
| 900 |
return ChildHide(interp, childInterp, objc - 3, objv + 3); |
| 901 |
case OPT_HIDDEN: |
| 902 |
childInterp = GetInterp2(interp, objc, objv); |
| 903 |
if (childInterp == NULL) { |
| 904 |
return TCL_ERROR; |
| 905 |
} |
| 906 |
return ChildHidden(interp, childInterp); |
| 907 |
case OPT_ISSAFE: |
| 908 |
childInterp = GetInterp2(interp, objc, objv); |
| 909 |
if (childInterp == NULL) { |
| 910 |
return TCL_ERROR; |
| 911 |
} |
| 912 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(Tcl_IsSafe(childInterp))); |
| 913 |
return TCL_OK; |
| 914 |
case OPT_INVOKEHID: { |
| 915 |
int i; |
| 916 |
const char *namespaceName; |
| 917 |
static const char *const hiddenOptions[] = { |
| 918 |
"-global", "-namespace", "--", NULL |
| 919 |
}; |
| 920 |
enum hiddenOption { |
| 921 |
OPT_GLOBAL, OPT_NAMESPACE, OPT_LAST |
| 922 |
}; |
| 923 |
|
| 924 |
namespaceName = NULL; |
| 925 |
for (i = 3; i < objc; i++) { |
| 926 |
if (TclGetString(objv[i])[0] != '-') { |
| 927 |
break; |
| 928 |
} |
| 929 |
if (Tcl_GetIndexFromObj(interp, objv[i], hiddenOptions, "option", |
| 930 |
0, &index) != TCL_OK) { |
| 931 |
return TCL_ERROR; |
| 932 |
} |
| 933 |
if (index == OPT_GLOBAL) { |
| 934 |
namespaceName = "::"; |
| 935 |
} else if (index == OPT_NAMESPACE) { |
| 936 |
if (++i == objc) { /* There must be more arguments. */ |
| 937 |
break; |
| 938 |
} else { |
| 939 |
namespaceName = TclGetString(objv[i]); |
| 940 |
} |
| 941 |
} else { |
| 942 |
i++; |
| 943 |
break; |
| 944 |
} |
| 945 |
} |
| 946 |
if (objc - i < 1) { |
| 947 |
Tcl_WrongNumArgs(interp, 2, objv, |
| 948 |
"path ?-namespace ns? ?-global? ?--? cmd ?arg ..?"); |
| 949 |
return TCL_ERROR; |
| 950 |
} |
| 951 |
childInterp = GetInterp(interp, objv[2]); |
| 952 |
if (childInterp == NULL) { |
| 953 |
return TCL_ERROR; |
| 954 |
} |
| 955 |
return ChildInvokeHidden(interp, childInterp, namespaceName, objc - i, |
| 956 |
objv + i); |
| 957 |
} |
| 958 |
case OPT_LIMIT: { |
| 959 |
static const char *const limitTypes[] = { |
| 960 |
"commands", "time", NULL |
| 961 |
}; |
| 962 |
enum LimitTypes { |
| 963 |
LIMIT_TYPE_COMMANDS, LIMIT_TYPE_TIME |
| 964 |
}; |
| 965 |
int limitType; |
| 966 |
|
| 967 |
if (objc < 4) { |
| 968 |
Tcl_WrongNumArgs(interp, 2, objv, |
| 969 |
"path limitType ?-option value ...?"); |
| 970 |
return TCL_ERROR; |
| 971 |
} |
| 972 |
childInterp = GetInterp(interp, objv[2]); |
| 973 |
if (childInterp == NULL) { |
| 974 |
return TCL_ERROR; |
| 975 |
} |
| 976 |
if (Tcl_GetIndexFromObj(interp, objv[3], limitTypes, "limit type", 0, |
| 977 |
&limitType) != TCL_OK) { |
| 978 |
return TCL_ERROR; |
| 979 |
} |
| 980 |
switch ((enum LimitTypes) limitType) { |
| 981 |
case LIMIT_TYPE_COMMANDS: |
| 982 |
return ChildCommandLimitCmd(interp, childInterp, 4, objc,objv); |
| 983 |
case LIMIT_TYPE_TIME: |
| 984 |
return ChildTimeLimitCmd(interp, childInterp, 4, objc, objv); |
| 985 |
} |
| 986 |
} |
| 987 |
break; |
| 988 |
case OPT_MARKTRUSTED: |
| 989 |
if (objc != 3) { |
| 990 |
Tcl_WrongNumArgs(interp, 2, objv, "path"); |
| 991 |
return TCL_ERROR; |
| 992 |
} |
| 993 |
childInterp = GetInterp(interp, objv[2]); |
| 994 |
if (childInterp == NULL) { |
| 995 |
return TCL_ERROR; |
| 996 |
} |
| 997 |
return ChildMarkTrusted(interp, childInterp); |
| 998 |
case OPT_RECLIMIT: |
| 999 |
if (objc != 3 && objc != 4) { |
| 1000 |
Tcl_WrongNumArgs(interp, 2, objv, "path ?newlimit?"); |
| 1001 |
return TCL_ERROR; |
| 1002 |
} |
| 1003 |
childInterp = GetInterp(interp, objv[2]); |
| 1004 |
if (childInterp == NULL) { |
| 1005 |
return TCL_ERROR; |
| 1006 |
} |
| 1007 |
return ChildRecursionLimit(interp, childInterp, objc - 3, objv + 3); |
| 1008 |
case OPT_CHILDREN: |
| 1009 |
case OPT_SLAVES: { |
| 1010 |
InterpInfo *iiPtr; |
| 1011 |
Tcl_Obj *resultPtr; |
| 1012 |
Tcl_HashEntry *hPtr; |
| 1013 |
Tcl_HashSearch hashSearch; |
| 1014 |
char *string; |
| 1015 |
|
| 1016 |
childInterp = GetInterp2(interp, objc, objv); |
| 1017 |
if (childInterp == NULL) { |
| 1018 |
return TCL_ERROR; |
| 1019 |
} |
| 1020 |
iiPtr = (InterpInfo *) ((Interp *) childInterp)->interpInfo; |
| 1021 |
TclNewObj(resultPtr); |
| 1022 |
hPtr = Tcl_FirstHashEntry(&iiPtr->parent.childTable, &hashSearch); |
| 1023 |
for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&hashSearch)) { |
| 1024 |
string = (char *)Tcl_GetHashKey(&iiPtr->parent.childTable, hPtr); |
| 1025 |
Tcl_ListObjAppendElement(NULL, resultPtr, |
| 1026 |
Tcl_NewStringObj(string, -1)); |
| 1027 |
} |
| 1028 |
Tcl_SetObjResult(interp, resultPtr); |
| 1029 |
return TCL_OK; |
| 1030 |
} |
| 1031 |
case OPT_TRANSFER: |
| 1032 |
case OPT_SHARE: { |
| 1033 |
Tcl_Interp *parentInterp; /* The parent of the child. */ |
| 1034 |
Tcl_Channel chan; |
| 1035 |
|
| 1036 |
if (objc != 5) { |
| 1037 |
Tcl_WrongNumArgs(interp, 2, objv, "srcPath channelId destPath"); |
| 1038 |
return TCL_ERROR; |
| 1039 |
} |
| 1040 |
parentInterp = GetInterp(interp, objv[2]); |
| 1041 |
if (parentInterp == NULL) { |
| 1042 |
return TCL_ERROR; |
| 1043 |
} |
| 1044 |
chan = Tcl_GetChannel(parentInterp, TclGetString(objv[3]), NULL); |
| 1045 |
if (chan == NULL) { |
| 1046 |
Tcl_TransferResult(parentInterp, TCL_OK, interp); |
| 1047 |
return TCL_ERROR; |
| 1048 |
} |
| 1049 |
childInterp = GetInterp(interp, objv[4]); |
| 1050 |
if (childInterp == NULL) { |
| 1051 |
return TCL_ERROR; |
| 1052 |
} |
| 1053 |
Tcl_RegisterChannel(childInterp, chan); |
| 1054 |
if (index == OPT_TRANSFER) { |
| 1055 |
/* |
| 1056 |
* When transferring, as opposed to sharing, we must unhitch the |
| 1057 |
* channel from the interpreter where it started. |
| 1058 |
*/ |
| 1059 |
|
| 1060 |
if (Tcl_UnregisterChannel(parentInterp, chan) != TCL_OK) { |
| 1061 |
Tcl_TransferResult(parentInterp, TCL_OK, interp); |
| 1062 |
return TCL_ERROR; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
return TCL_OK; |
| 1066 |
} |
| 1067 |
case OPT_TARGET: { |
| 1068 |
InterpInfo *iiPtr; |
| 1069 |
Tcl_HashEntry *hPtr; |
| 1070 |
Alias *aliasPtr; |
| 1071 |
const char *aliasName; |
| 1072 |
|
| 1073 |
if (objc != 4) { |
| 1074 |
Tcl_WrongNumArgs(interp, 2, objv, "path alias"); |
| 1075 |
return TCL_ERROR; |
| 1076 |
} |
| 1077 |
|
| 1078 |
childInterp = GetInterp(interp, objv[2]); |
| 1079 |
if (childInterp == NULL) { |
| 1080 |
return TCL_ERROR; |
| 1081 |
} |
| 1082 |
|
| 1083 |
aliasName = TclGetString(objv[3]); |
| 1084 |
|
| 1085 |
iiPtr = (InterpInfo *) ((Interp *) childInterp)->interpInfo; |
| 1086 |
hPtr = Tcl_FindHashEntry(&iiPtr->child.aliasTable, aliasName); |
| 1087 |
if (hPtr == NULL) { |
| 1088 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1089 |
"alias \"%s\" in path \"%s\" not found", |
| 1090 |
aliasName, TclGetString(objv[2]))); |
| 1091 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ALIAS", aliasName, |
| 1092 |
(char *)NULL); |
| 1093 |
return TCL_ERROR; |
| 1094 |
} |
| 1095 |
aliasPtr = (Alias *)Tcl_GetHashValue(hPtr); |
| 1096 |
if (Tcl_GetInterpPath(interp, aliasPtr->targetInterp) != TCL_OK) { |
| 1097 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1098 |
"target interpreter for alias \"%s\" in path \"%s\" is " |
| 1099 |
"not my descendant", aliasName, TclGetString(objv[2]))); |
| 1100 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 1101 |
"TARGETSHROUDED", (char *)NULL); |
| 1102 |
return TCL_ERROR; |
| 1103 |
} |
| 1104 |
return TCL_OK; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
return TCL_OK; |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* |
| 1111 |
*--------------------------------------------------------------------------- |
| 1112 |
* |
| 1113 |
* GetInterp2 -- |
| 1114 |
* |
| 1115 |
* Helper function for Tcl_InterpObjCmd() to convert the interp name |
| 1116 |
* potentially specified on the command line to an Tcl_Interp. |
| 1117 |
* |
| 1118 |
* Results: |
| 1119 |
* The return value is the interp specified on the command line, or the |
| 1120 |
* interp argument itself if no interp was specified on the command line. |
| 1121 |
* If the interp could not be found or the wrong number of arguments was |
| 1122 |
* specified on the command line, the return value is NULL and an error |
| 1123 |
* message is left in the interp's result. |
| 1124 |
* |
| 1125 |
* Side effects: |
| 1126 |
* None. |
| 1127 |
* |
| 1128 |
*--------------------------------------------------------------------------- |
| 1129 |
*/ |
| 1130 |
|
| 1131 |
static Tcl_Interp * |
| 1132 |
GetInterp2( |
| 1133 |
Tcl_Interp *interp, /* Default interp if no interp was specified |
| 1134 |
* on the command line. */ |
| 1135 |
int objc, /* Number of arguments. */ |
| 1136 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1137 |
{ |
| 1138 |
if (objc == 2) { |
| 1139 |
return interp; |
| 1140 |
} else if (objc == 3) { |
| 1141 |
return GetInterp(interp, objv[2]); |
| 1142 |
} else { |
| 1143 |
Tcl_WrongNumArgs(interp, 2, objv, "?path?"); |
| 1144 |
return NULL; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* |
| 1149 |
*---------------------------------------------------------------------- |
| 1150 |
* |
| 1151 |
* Tcl_CreateAlias -- |
| 1152 |
* |
| 1153 |
* Creates an alias between two interpreters. |
| 1154 |
* |
| 1155 |
* Results: |
| 1156 |
* A standard Tcl result. |
| 1157 |
* |
| 1158 |
* Side effects: |
| 1159 |
* Creates a new alias, manipulates the result field of childInterp. |
| 1160 |
* |
| 1161 |
*---------------------------------------------------------------------- |
| 1162 |
*/ |
| 1163 |
|
| 1164 |
int |
| 1165 |
Tcl_CreateAlias( |
| 1166 |
Tcl_Interp *childInterp, /* Interpreter for source command. */ |
| 1167 |
const char *childCmd, /* Command to install in child. */ |
| 1168 |
Tcl_Interp *targetInterp, /* Interpreter for target command. */ |
| 1169 |
const char *targetCmd, /* Name of target command. */ |
| 1170 |
int argc, /* How many additional arguments? */ |
| 1171 |
const char *const *argv) /* These are the additional args. */ |
| 1172 |
{ |
| 1173 |
Tcl_Obj *childObjPtr, *targetObjPtr; |
| 1174 |
Tcl_Obj **objv; |
| 1175 |
int i; |
| 1176 |
int result; |
| 1177 |
|
| 1178 |
objv = (Tcl_Obj **)TclStackAlloc(childInterp, sizeof(Tcl_Obj *) * argc); |
| 1179 |
for (i = 0; i < argc; i++) { |
| 1180 |
objv[i] = Tcl_NewStringObj(argv[i], -1); |
| 1181 |
Tcl_IncrRefCount(objv[i]); |
| 1182 |
} |
| 1183 |
|
| 1184 |
childObjPtr = Tcl_NewStringObj(childCmd, -1); |
| 1185 |
Tcl_IncrRefCount(childObjPtr); |
| 1186 |
|
| 1187 |
targetObjPtr = Tcl_NewStringObj(targetCmd, -1); |
| 1188 |
Tcl_IncrRefCount(targetObjPtr); |
| 1189 |
|
| 1190 |
result = AliasCreate(childInterp, childInterp, targetInterp, childObjPtr, |
| 1191 |
targetObjPtr, argc, objv); |
| 1192 |
|
| 1193 |
for (i = 0; i < argc; i++) { |
| 1194 |
Tcl_DecrRefCount(objv[i]); |
| 1195 |
} |
| 1196 |
TclStackFree(childInterp, objv); |
| 1197 |
Tcl_DecrRefCount(targetObjPtr); |
| 1198 |
Tcl_DecrRefCount(childObjPtr); |
| 1199 |
|
| 1200 |
return result; |
| 1201 |
} |
| 1202 |
|
| 1203 |
/* |
| 1204 |
*---------------------------------------------------------------------- |
| 1205 |
* |
| 1206 |
* Tcl_CreateAliasObj -- |
| 1207 |
* |
| 1208 |
* Object version: Creates an alias between two interpreters. |
| 1209 |
* |
| 1210 |
* Results: |
| 1211 |
* A standard Tcl result. |
| 1212 |
* |
| 1213 |
* Side effects: |
| 1214 |
* Creates a new alias. |
| 1215 |
* |
| 1216 |
*---------------------------------------------------------------------- |
| 1217 |
*/ |
| 1218 |
|
| 1219 |
int |
| 1220 |
Tcl_CreateAliasObj( |
| 1221 |
Tcl_Interp *childInterp, /* Interpreter for source command. */ |
| 1222 |
const char *childCmd, /* Command to install in child. */ |
| 1223 |
Tcl_Interp *targetInterp, /* Interpreter for target command. */ |
| 1224 |
const char *targetCmd, /* Name of target command. */ |
| 1225 |
int objc, /* How many additional arguments? */ |
| 1226 |
Tcl_Obj *const objv[]) /* Argument vector. */ |
| 1227 |
{ |
| 1228 |
Tcl_Obj *childObjPtr, *targetObjPtr; |
| 1229 |
int result; |
| 1230 |
|
| 1231 |
childObjPtr = Tcl_NewStringObj(childCmd, -1); |
| 1232 |
Tcl_IncrRefCount(childObjPtr); |
| 1233 |
|
| 1234 |
targetObjPtr = Tcl_NewStringObj(targetCmd, -1); |
| 1235 |
Tcl_IncrRefCount(targetObjPtr); |
| 1236 |
|
| 1237 |
result = AliasCreate(childInterp, childInterp, targetInterp, childObjPtr, |
| 1238 |
targetObjPtr, objc, objv); |
| 1239 |
|
| 1240 |
Tcl_DecrRefCount(childObjPtr); |
| 1241 |
Tcl_DecrRefCount(targetObjPtr); |
| 1242 |
return result; |
| 1243 |
} |
| 1244 |
|
| 1245 |
/* |
| 1246 |
*---------------------------------------------------------------------- |
| 1247 |
* |
| 1248 |
* Tcl_GetAlias -- |
| 1249 |
* |
| 1250 |
* Gets information about an alias. |
| 1251 |
* |
| 1252 |
* Results: |
| 1253 |
* A standard Tcl result. |
| 1254 |
* |
| 1255 |
* Side effects: |
| 1256 |
* None. |
| 1257 |
* |
| 1258 |
*---------------------------------------------------------------------- |
| 1259 |
*/ |
| 1260 |
|
| 1261 |
int |
| 1262 |
Tcl_GetAlias( |
| 1263 |
Tcl_Interp *interp, /* Interp to start search from. */ |
| 1264 |
const char *aliasName, /* Name of alias to find. */ |
| 1265 |
Tcl_Interp **targetInterpPtr, |
| 1266 |
/* (Return) target interpreter. */ |
| 1267 |
const char **targetCmdPtr, /* (Return) name of target command. */ |
| 1268 |
int *argcPtr, /* (Return) count of addnl args. */ |
| 1269 |
const char ***argvPtr) /* (Return) additional arguments. */ |
| 1270 |
{ |
| 1271 |
InterpInfo *iiPtr = (InterpInfo *) ((Interp *) interp)->interpInfo; |
| 1272 |
Tcl_HashEntry *hPtr; |
| 1273 |
Alias *aliasPtr; |
| 1274 |
int i, objc; |
| 1275 |
Tcl_Obj **objv; |
| 1276 |
|
| 1277 |
hPtr = Tcl_FindHashEntry(&iiPtr->child.aliasTable, aliasName); |
| 1278 |
if (hPtr == NULL) { |
| 1279 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1280 |
"alias \"%s\" not found", aliasName)); |
| 1281 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ALIAS", aliasName, (char *)NULL); |
| 1282 |
return TCL_ERROR; |
| 1283 |
} |
| 1284 |
aliasPtr = (Alias *)Tcl_GetHashValue(hPtr); |
| 1285 |
objc = aliasPtr->objc; |
| 1286 |
objv = &aliasPtr->objPtr; |
| 1287 |
|
| 1288 |
if (targetInterpPtr != NULL) { |
| 1289 |
*targetInterpPtr = aliasPtr->targetInterp; |
| 1290 |
} |
| 1291 |
if (targetCmdPtr != NULL) { |
| 1292 |
*targetCmdPtr = TclGetString(objv[0]); |
| 1293 |
} |
| 1294 |
if (argcPtr != NULL) { |
| 1295 |
*argcPtr = objc - 1; |
| 1296 |
} |
| 1297 |
if (argvPtr != NULL) { |
| 1298 |
*argvPtr = (const char **) |
| 1299 |
ckalloc(sizeof(const char *) * (objc - 1)); |
| 1300 |
for (i = 1; i < objc; i++) { |
| 1301 |
(*argvPtr)[i - 1] = TclGetString(objv[i]); |
| 1302 |
} |
| 1303 |
} |
| 1304 |
return TCL_OK; |
| 1305 |
} |
| 1306 |
|
| 1307 |
/* |
| 1308 |
*---------------------------------------------------------------------- |
| 1309 |
* |
| 1310 |
* Tcl_GetAliasObj -- |
| 1311 |
* |
| 1312 |
* Object version: Gets information about an alias. |
| 1313 |
* |
| 1314 |
* Results: |
| 1315 |
* A standard Tcl result. |
| 1316 |
* |
| 1317 |
* Side effects: |
| 1318 |
* None. |
| 1319 |
* |
| 1320 |
*---------------------------------------------------------------------- |
| 1321 |
*/ |
| 1322 |
|
| 1323 |
int |
| 1324 |
Tcl_GetAliasObj( |
| 1325 |
Tcl_Interp *interp, /* Interp to start search from. */ |
| 1326 |
const char *aliasName, /* Name of alias to find. */ |
| 1327 |
Tcl_Interp **targetInterpPtr, |
| 1328 |
/* (Return) target interpreter. */ |
| 1329 |
const char **targetCmdPtr, /* (Return) name of target command. */ |
| 1330 |
int *objcPtr, /* (Return) count of addnl args. */ |
| 1331 |
Tcl_Obj ***objvPtr) /* (Return) additional args. */ |
| 1332 |
{ |
| 1333 |
InterpInfo *iiPtr = (InterpInfo *) ((Interp *) interp)->interpInfo; |
| 1334 |
Tcl_HashEntry *hPtr; |
| 1335 |
Alias *aliasPtr; |
| 1336 |
int objc; |
| 1337 |
Tcl_Obj **objv; |
| 1338 |
|
| 1339 |
hPtr = Tcl_FindHashEntry(&iiPtr->child.aliasTable, aliasName); |
| 1340 |
if (hPtr == NULL) { |
| 1341 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1342 |
"alias \"%s\" not found", aliasName)); |
| 1343 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ALIAS", aliasName, (char *)NULL); |
| 1344 |
return TCL_ERROR; |
| 1345 |
} |
| 1346 |
aliasPtr = (Alias *)Tcl_GetHashValue(hPtr); |
| 1347 |
objc = aliasPtr->objc; |
| 1348 |
objv = &aliasPtr->objPtr; |
| 1349 |
|
| 1350 |
if (targetInterpPtr != NULL) { |
| 1351 |
*targetInterpPtr = aliasPtr->targetInterp; |
| 1352 |
} |
| 1353 |
if (targetCmdPtr != NULL) { |
| 1354 |
*targetCmdPtr = TclGetString(objv[0]); |
| 1355 |
} |
| 1356 |
if (objcPtr != NULL) { |
| 1357 |
*objcPtr = objc - 1; |
| 1358 |
} |
| 1359 |
if (objvPtr != NULL) { |
| 1360 |
*objvPtr = objv + 1; |
| 1361 |
} |
| 1362 |
return TCL_OK; |
| 1363 |
} |
| 1364 |
|
| 1365 |
/* |
| 1366 |
*---------------------------------------------------------------------- |
| 1367 |
* |
| 1368 |
* TclPreventAliasLoop -- |
| 1369 |
* |
| 1370 |
* When defining an alias or renaming a command, prevent an alias loop |
| 1371 |
* from being formed. |
| 1372 |
* |
| 1373 |
* Results: |
| 1374 |
* A standard Tcl object result. |
| 1375 |
* |
| 1376 |
* Side effects: |
| 1377 |
* If TCL_ERROR is returned, the function also stores an error message in |
| 1378 |
* the interpreter's result object. |
| 1379 |
* |
| 1380 |
* NOTE: |
| 1381 |
* This function is public internal (instead of being static to this |
| 1382 |
* file) because it is also used from TclRenameCommand. |
| 1383 |
* |
| 1384 |
*---------------------------------------------------------------------- |
| 1385 |
*/ |
| 1386 |
|
| 1387 |
int |
| 1388 |
TclPreventAliasLoop( |
| 1389 |
Tcl_Interp *interp, /* Interp in which to report errors. */ |
| 1390 |
Tcl_Interp *cmdInterp, /* Interp in which the command is being |
| 1391 |
* defined. */ |
| 1392 |
Tcl_Command cmd) /* Tcl command we are attempting to define. */ |
| 1393 |
{ |
| 1394 |
Command *cmdPtr = (Command *) cmd; |
| 1395 |
Alias *aliasPtr, *nextAliasPtr; |
| 1396 |
Tcl_Command aliasCmd; |
| 1397 |
Command *aliasCmdPtr; |
| 1398 |
|
| 1399 |
/* |
| 1400 |
* If we are not creating or renaming an alias, then it is always OK to |
| 1401 |
* create or rename the command. |
| 1402 |
*/ |
| 1403 |
|
| 1404 |
if (cmdPtr->objProc != AliasObjCmd) { |
| 1405 |
return TCL_OK; |
| 1406 |
} |
| 1407 |
|
| 1408 |
/* |
| 1409 |
* OK, we are dealing with an alias, so traverse the chain of aliases. If |
| 1410 |
* we encounter the alias we are defining (or renaming to) any in the |
| 1411 |
* chain then we have a loop. |
| 1412 |
*/ |
| 1413 |
|
| 1414 |
aliasPtr = (Alias *)cmdPtr->objClientData; |
| 1415 |
nextAliasPtr = aliasPtr; |
| 1416 |
while (1) { |
| 1417 |
Tcl_Obj *cmdNamePtr; |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* If the target of the next alias in the chain is the same as the |
| 1421 |
* source alias, we have a loop. |
| 1422 |
*/ |
| 1423 |
|
| 1424 |
if (Tcl_InterpDeleted(nextAliasPtr->targetInterp)) { |
| 1425 |
/* |
| 1426 |
* The child interpreter can be deleted while creating the alias. |
| 1427 |
* [Bug #641195] |
| 1428 |
*/ |
| 1429 |
|
| 1430 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1431 |
"cannot define or rename alias \"%s\": interpreter deleted", |
| 1432 |
Tcl_GetCommandName(cmdInterp, cmd))); |
| 1433 |
return TCL_ERROR; |
| 1434 |
} |
| 1435 |
cmdNamePtr = nextAliasPtr->objPtr; |
| 1436 |
aliasCmd = Tcl_FindCommand(nextAliasPtr->targetInterp, |
| 1437 |
TclGetString(cmdNamePtr), |
| 1438 |
Tcl_GetGlobalNamespace(nextAliasPtr->targetInterp), |
| 1439 |
/*flags*/ 0); |
| 1440 |
if (aliasCmd == NULL) { |
| 1441 |
return TCL_OK; |
| 1442 |
} |
| 1443 |
aliasCmdPtr = (Command *) aliasCmd; |
| 1444 |
if (aliasCmdPtr == cmdPtr) { |
| 1445 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1446 |
"cannot define or rename alias \"%s\": would create a loop", |
| 1447 |
Tcl_GetCommandName(cmdInterp, cmd))); |
| 1448 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 1449 |
"ALIASLOOP", (char *)NULL); |
| 1450 |
return TCL_ERROR; |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* |
| 1454 |
* Otherwise, follow the chain one step further. See if the target |
| 1455 |
* command is an alias - if so, follow the loop to its target command. |
| 1456 |
* Otherwise we do not have a loop. |
| 1457 |
*/ |
| 1458 |
|
| 1459 |
if (aliasCmdPtr->objProc != AliasObjCmd) { |
| 1460 |
return TCL_OK; |
| 1461 |
} |
| 1462 |
nextAliasPtr = (Alias *)aliasCmdPtr->objClientData; |
| 1463 |
} |
| 1464 |
} |
| 1465 |
|
| 1466 |
/* |
| 1467 |
*---------------------------------------------------------------------- |
| 1468 |
* |
| 1469 |
* AliasCreate -- |
| 1470 |
* |
| 1471 |
* Helper function to do the work to actually create an alias. |
| 1472 |
* |
| 1473 |
* Results: |
| 1474 |
* A standard Tcl result. |
| 1475 |
* |
| 1476 |
* Side effects: |
| 1477 |
* An alias command is created and entered into the alias table for the |
| 1478 |
* child interpreter. |
| 1479 |
* |
| 1480 |
*---------------------------------------------------------------------- |
| 1481 |
*/ |
| 1482 |
|
| 1483 |
static int |
| 1484 |
AliasCreate( |
| 1485 |
Tcl_Interp *interp, /* Interp for error reporting. */ |
| 1486 |
Tcl_Interp *childInterp, /* Interp where alias cmd will live or from |
| 1487 |
* which alias will be deleted. */ |
| 1488 |
Tcl_Interp *parentInterp, /* Interp in which target command will be |
| 1489 |
* invoked. */ |
| 1490 |
Tcl_Obj *namePtr, /* Name of alias cmd. */ |
| 1491 |
Tcl_Obj *targetCmdPtr, /* Name of target cmd. */ |
| 1492 |
int objc, /* Additional arguments to store */ |
| 1493 |
Tcl_Obj *const objv[]) /* with alias. */ |
| 1494 |
{ |
| 1495 |
Alias *aliasPtr; |
| 1496 |
Tcl_HashEntry *hPtr; |
| 1497 |
Target *targetPtr; |
| 1498 |
Child *childPtr; |
| 1499 |
Parent *parentPtr; |
| 1500 |
Tcl_Obj **prefv; |
| 1501 |
int isNew, i; |
| 1502 |
|
| 1503 |
aliasPtr = (Alias *)ckalloc(sizeof(Alias) + objc * sizeof(Tcl_Obj *)); |
| 1504 |
aliasPtr->token = namePtr; |
| 1505 |
Tcl_IncrRefCount(aliasPtr->token); |
| 1506 |
aliasPtr->targetInterp = parentInterp; |
| 1507 |
|
| 1508 |
aliasPtr->objc = objc + 1; |
| 1509 |
prefv = &aliasPtr->objPtr; |
| 1510 |
|
| 1511 |
*prefv = targetCmdPtr; |
| 1512 |
Tcl_IncrRefCount(targetCmdPtr); |
| 1513 |
for (i = 0; i < objc; i++) { |
| 1514 |
*(++prefv) = objv[i]; |
| 1515 |
Tcl_IncrRefCount(objv[i]); |
| 1516 |
} |
| 1517 |
|
| 1518 |
Tcl_Preserve(childInterp); |
| 1519 |
Tcl_Preserve(parentInterp); |
| 1520 |
|
| 1521 |
if (childInterp == parentInterp) { |
| 1522 |
aliasPtr->childCmd = Tcl_NRCreateCommand(childInterp, |
| 1523 |
TclGetString(namePtr), AliasObjCmd, AliasNRCmd, aliasPtr, |
| 1524 |
AliasObjCmdDeleteProc); |
| 1525 |
} else { |
| 1526 |
aliasPtr->childCmd = Tcl_CreateObjCommand(childInterp, |
| 1527 |
TclGetString(namePtr), AliasObjCmd, aliasPtr, |
| 1528 |
AliasObjCmdDeleteProc); |
| 1529 |
} |
| 1530 |
|
| 1531 |
if (TclPreventAliasLoop(interp, childInterp, |
| 1532 |
aliasPtr->childCmd) != TCL_OK) { |
| 1533 |
/* |
| 1534 |
* Found an alias loop! The last call to Tcl_CreateObjCommand made the |
| 1535 |
* alias point to itself. Delete the command and its alias record. Be |
| 1536 |
* careful to wipe out its client data first, so the command doesn't |
| 1537 |
* try to delete itself. |
| 1538 |
*/ |
| 1539 |
|
| 1540 |
Command *cmdPtr; |
| 1541 |
|
| 1542 |
Tcl_DecrRefCount(aliasPtr->token); |
| 1543 |
Tcl_DecrRefCount(targetCmdPtr); |
| 1544 |
for (i = 0; i < objc; i++) { |
| 1545 |
Tcl_DecrRefCount(objv[i]); |
| 1546 |
} |
| 1547 |
|
| 1548 |
cmdPtr = (Command *) aliasPtr->childCmd; |
| 1549 |
cmdPtr->clientData = NULL; |
| 1550 |
cmdPtr->deleteProc = NULL; |
| 1551 |
cmdPtr->deleteData = NULL; |
| 1552 |
Tcl_DeleteCommandFromToken(childInterp, aliasPtr->childCmd); |
| 1553 |
|
| 1554 |
ckfree(aliasPtr); |
| 1555 |
|
| 1556 |
/* |
| 1557 |
* The result was already set by TclPreventAliasLoop. |
| 1558 |
*/ |
| 1559 |
|
| 1560 |
Tcl_Release(childInterp); |
| 1561 |
Tcl_Release(parentInterp); |
| 1562 |
return TCL_ERROR; |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* |
| 1566 |
* Make an entry in the alias table. If it already exists, retry. |
| 1567 |
*/ |
| 1568 |
|
| 1569 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 1570 |
while (1) { |
| 1571 |
Tcl_Obj *newToken; |
| 1572 |
const char *string; |
| 1573 |
|
| 1574 |
string = TclGetString(aliasPtr->token); |
| 1575 |
hPtr = Tcl_CreateHashEntry(&childPtr->aliasTable, string, &isNew); |
| 1576 |
if (isNew != 0) { |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
|
| 1580 |
/* |
| 1581 |
* The alias name cannot be used as unique token, it is already taken. |
| 1582 |
* We can produce a unique token by prepending "::" repeatedly. This |
| 1583 |
* algorithm is a stop-gap to try to maintain the command name as |
| 1584 |
* token for most use cases, fearful of possible backwards compat |
| 1585 |
* problems. A better algorithm would produce unique tokens that need |
| 1586 |
* not be related to the command name. |
| 1587 |
* |
| 1588 |
* ATTENTION: the tests in interp.test and possibly safe.test depend |
| 1589 |
* on the precise definition of these tokens. |
| 1590 |
*/ |
| 1591 |
|
| 1592 |
TclNewLiteralStringObj(newToken, "::"); |
| 1593 |
Tcl_AppendObjToObj(newToken, aliasPtr->token); |
| 1594 |
Tcl_DecrRefCount(aliasPtr->token); |
| 1595 |
aliasPtr->token = newToken; |
| 1596 |
Tcl_IncrRefCount(aliasPtr->token); |
| 1597 |
} |
| 1598 |
|
| 1599 |
aliasPtr->aliasEntryPtr = hPtr; |
| 1600 |
Tcl_SetHashValue(hPtr, aliasPtr); |
| 1601 |
|
| 1602 |
/* |
| 1603 |
* Create the new command. We must do it after deleting any old command, |
| 1604 |
* because the alias may be pointing at a renamed alias, as in: |
| 1605 |
* |
| 1606 |
* interp alias {} foo {} bar # Create an alias "foo" |
| 1607 |
* rename foo zop # Now rename the alias |
| 1608 |
* interp alias {} foo {} zop # Now recreate "foo"... |
| 1609 |
*/ |
| 1610 |
|
| 1611 |
targetPtr = (Target *)ckalloc(sizeof(Target)); |
| 1612 |
targetPtr->childCmd = aliasPtr->childCmd; |
| 1613 |
targetPtr->childInterp = childInterp; |
| 1614 |
|
| 1615 |
parentPtr = &((InterpInfo*) ((Interp*) parentInterp)->interpInfo)->parent; |
| 1616 |
targetPtr->nextPtr = parentPtr->targetsPtr; |
| 1617 |
targetPtr->prevPtr = NULL; |
| 1618 |
if (parentPtr->targetsPtr != NULL) { |
| 1619 |
parentPtr->targetsPtr->prevPtr = targetPtr; |
| 1620 |
} |
| 1621 |
parentPtr->targetsPtr = targetPtr; |
| 1622 |
aliasPtr->targetPtr = targetPtr; |
| 1623 |
|
| 1624 |
Tcl_SetObjResult(interp, aliasPtr->token); |
| 1625 |
|
| 1626 |
Tcl_Release(childInterp); |
| 1627 |
Tcl_Release(parentInterp); |
| 1628 |
return TCL_OK; |
| 1629 |
} |
| 1630 |
|
| 1631 |
/* |
| 1632 |
*---------------------------------------------------------------------- |
| 1633 |
* |
| 1634 |
* AliasDelete -- |
| 1635 |
* |
| 1636 |
* Deletes the given alias from the child interpreter given. |
| 1637 |
* |
| 1638 |
* Results: |
| 1639 |
* A standard Tcl result. |
| 1640 |
* |
| 1641 |
* Side effects: |
| 1642 |
* Deletes the alias from the child interpreter. |
| 1643 |
* |
| 1644 |
*---------------------------------------------------------------------- |
| 1645 |
*/ |
| 1646 |
|
| 1647 |
static int |
| 1648 |
AliasDelete( |
| 1649 |
Tcl_Interp *interp, /* Interpreter for result & errors. */ |
| 1650 |
Tcl_Interp *childInterp, /* Interpreter containing alias. */ |
| 1651 |
Tcl_Obj *namePtr) /* Name of alias to delete. */ |
| 1652 |
{ |
| 1653 |
Child *childPtr; |
| 1654 |
Alias *aliasPtr; |
| 1655 |
Tcl_HashEntry *hPtr; |
| 1656 |
|
| 1657 |
/* |
| 1658 |
* If the alias has been renamed in the child, the parent can still use |
| 1659 |
* the original name (with which it was created) to find the alias to |
| 1660 |
* delete it. |
| 1661 |
*/ |
| 1662 |
|
| 1663 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 1664 |
hPtr = Tcl_FindHashEntry(&childPtr->aliasTable, TclGetString(namePtr)); |
| 1665 |
if (hPtr == NULL) { |
| 1666 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1667 |
"alias \"%s\" not found", TclGetString(namePtr))); |
| 1668 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ALIAS", |
| 1669 |
TclGetString(namePtr), (char *)NULL); |
| 1670 |
return TCL_ERROR; |
| 1671 |
} |
| 1672 |
aliasPtr = (Alias *)Tcl_GetHashValue(hPtr); |
| 1673 |
Tcl_DeleteCommandFromToken(childInterp, aliasPtr->childCmd); |
| 1674 |
return TCL_OK; |
| 1675 |
} |
| 1676 |
|
| 1677 |
/* |
| 1678 |
*---------------------------------------------------------------------- |
| 1679 |
* |
| 1680 |
* AliasDescribe -- |
| 1681 |
* |
| 1682 |
* Sets the interpreter's result object to a Tcl list describing the |
| 1683 |
* given alias in the given interpreter: its target command and the |
| 1684 |
* additional arguments to prepend to any invocation of the alias. |
| 1685 |
* |
| 1686 |
* Results: |
| 1687 |
* A standard Tcl result. |
| 1688 |
* |
| 1689 |
* Side effects: |
| 1690 |
* None. |
| 1691 |
* |
| 1692 |
*---------------------------------------------------------------------- |
| 1693 |
*/ |
| 1694 |
|
| 1695 |
static int |
| 1696 |
AliasDescribe( |
| 1697 |
Tcl_Interp *interp, /* Interpreter for result & errors. */ |
| 1698 |
Tcl_Interp *childInterp, /* Interpreter containing alias. */ |
| 1699 |
Tcl_Obj *namePtr) /* Name of alias to describe. */ |
| 1700 |
{ |
| 1701 |
Child *childPtr; |
| 1702 |
Tcl_HashEntry *hPtr; |
| 1703 |
Alias *aliasPtr; |
| 1704 |
Tcl_Obj *prefixPtr; |
| 1705 |
|
| 1706 |
/* |
| 1707 |
* If the alias has been renamed in the child, the parent can still use |
| 1708 |
* the original name (with which it was created) to find the alias to |
| 1709 |
* describe it. |
| 1710 |
*/ |
| 1711 |
|
| 1712 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 1713 |
hPtr = Tcl_FindHashEntry(&childPtr->aliasTable, TclGetString(namePtr)); |
| 1714 |
if (hPtr == NULL) { |
| 1715 |
return TCL_OK; |
| 1716 |
} |
| 1717 |
aliasPtr = (Alias *)Tcl_GetHashValue(hPtr); |
| 1718 |
prefixPtr = Tcl_NewListObj(aliasPtr->objc, &aliasPtr->objPtr); |
| 1719 |
Tcl_SetObjResult(interp, prefixPtr); |
| 1720 |
return TCL_OK; |
| 1721 |
} |
| 1722 |
|
| 1723 |
/* |
| 1724 |
*---------------------------------------------------------------------- |
| 1725 |
* |
| 1726 |
* AliasList -- |
| 1727 |
* |
| 1728 |
* Computes a list of aliases defined in a child interpreter. |
| 1729 |
* |
| 1730 |
* Results: |
| 1731 |
* A standard Tcl result. |
| 1732 |
* |
| 1733 |
* Side effects: |
| 1734 |
* None. |
| 1735 |
* |
| 1736 |
*---------------------------------------------------------------------- |
| 1737 |
*/ |
| 1738 |
|
| 1739 |
static int |
| 1740 |
AliasList( |
| 1741 |
Tcl_Interp *interp, /* Interp for data return. */ |
| 1742 |
Tcl_Interp *childInterp) /* Interp whose aliases to compute. */ |
| 1743 |
{ |
| 1744 |
Tcl_HashEntry *entryPtr; |
| 1745 |
Tcl_HashSearch hashSearch; |
| 1746 |
Tcl_Obj *resultPtr; |
| 1747 |
Alias *aliasPtr; |
| 1748 |
Child *childPtr; |
| 1749 |
|
| 1750 |
TclNewObj(resultPtr); |
| 1751 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 1752 |
|
| 1753 |
entryPtr = Tcl_FirstHashEntry(&childPtr->aliasTable, &hashSearch); |
| 1754 |
for ( ; entryPtr != NULL; entryPtr = Tcl_NextHashEntry(&hashSearch)) { |
| 1755 |
aliasPtr = (Alias *)Tcl_GetHashValue(entryPtr); |
| 1756 |
Tcl_ListObjAppendElement(NULL, resultPtr, aliasPtr->token); |
| 1757 |
} |
| 1758 |
Tcl_SetObjResult(interp, resultPtr); |
| 1759 |
return TCL_OK; |
| 1760 |
} |
| 1761 |
|
| 1762 |
/* |
| 1763 |
*---------------------------------------------------------------------- |
| 1764 |
* |
| 1765 |
* AliasObjCmd -- |
| 1766 |
* |
| 1767 |
* This is the function that services invocations of aliases in a child |
| 1768 |
* interpreter. One such command exists for each alias. When invoked, |
| 1769 |
* this function redirects the invocation to the target command in the |
| 1770 |
* parent interpreter as designated by the Alias record associated with |
| 1771 |
* this command. |
| 1772 |
* |
| 1773 |
* Results: |
| 1774 |
* A standard Tcl result. |
| 1775 |
* |
| 1776 |
* Side effects: |
| 1777 |
* Causes forwarding of the invocation; all possible side effects may |
| 1778 |
* occur as a result of invoking the command to which the invocation is |
| 1779 |
* forwarded. |
| 1780 |
* |
| 1781 |
*---------------------------------------------------------------------- |
| 1782 |
*/ |
| 1783 |
|
| 1784 |
static int |
| 1785 |
AliasNRCmd( |
| 1786 |
ClientData clientData, /* Alias record. */ |
| 1787 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1788 |
int objc, /* Number of arguments. */ |
| 1789 |
Tcl_Obj *const objv[]) /* Argument vector. */ |
| 1790 |
{ |
| 1791 |
Alias *aliasPtr = (Alias *)clientData; |
| 1792 |
int prefc, cmdc, i; |
| 1793 |
Tcl_Obj **prefv, **cmdv; |
| 1794 |
Tcl_Obj *listPtr; |
| 1795 |
List *listRep; |
| 1796 |
int flags = TCL_EVAL_INVOKE; |
| 1797 |
|
| 1798 |
/* |
| 1799 |
* Append the arguments to the command prefix and invoke the command in |
| 1800 |
* the target interp's global namespace. |
| 1801 |
*/ |
| 1802 |
|
| 1803 |
prefc = aliasPtr->objc; |
| 1804 |
prefv = &aliasPtr->objPtr; |
| 1805 |
cmdc = prefc + objc - 1; |
| 1806 |
|
| 1807 |
listPtr = Tcl_NewListObj(cmdc, NULL); |
| 1808 |
listRep = (List *)listPtr->internalRep.twoPtrValue.ptr1; |
| 1809 |
listRep->elemCount = cmdc; |
| 1810 |
cmdv = &listRep->elements; |
| 1811 |
|
| 1812 |
prefv = &aliasPtr->objPtr; |
| 1813 |
memcpy(cmdv, prefv, prefc * sizeof(Tcl_Obj *)); |
| 1814 |
memcpy(cmdv+prefc, objv+1, (objc-1) * sizeof(Tcl_Obj *)); |
| 1815 |
|
| 1816 |
for (i=0; i<cmdc; i++) { |
| 1817 |
Tcl_IncrRefCount(cmdv[i]); |
| 1818 |
} |
| 1819 |
|
| 1820 |
/* |
| 1821 |
* Use the ensemble rewriting machinery to ensure correct error messages: |
| 1822 |
* only the source command should show, not the full target prefix. |
| 1823 |
*/ |
| 1824 |
|
| 1825 |
if (TclInitRewriteEnsemble(interp, 1, prefc, objv)) { |
| 1826 |
TclNRAddCallback(interp, TclClearRootEnsemble, NULL, NULL, NULL, NULL); |
| 1827 |
} |
| 1828 |
TclSkipTailcall(interp); |
| 1829 |
return Tcl_NREvalObj(interp, listPtr, flags); |
| 1830 |
} |
| 1831 |
|
| 1832 |
static int |
| 1833 |
AliasObjCmd( |
| 1834 |
ClientData clientData, /* Alias record. */ |
| 1835 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1836 |
int objc, /* Number of arguments. */ |
| 1837 |
Tcl_Obj *const objv[]) /* Argument vector. */ |
| 1838 |
{ |
| 1839 |
#define ALIAS_CMDV_PREALLOC 10 |
| 1840 |
Alias *aliasPtr = (Alias *)clientData; |
| 1841 |
Tcl_Interp *targetInterp = aliasPtr->targetInterp; |
| 1842 |
int result, prefc, cmdc, i; |
| 1843 |
Tcl_Obj **prefv, **cmdv; |
| 1844 |
Tcl_Obj *cmdArr[ALIAS_CMDV_PREALLOC]; |
| 1845 |
Interp *tPtr = (Interp *) targetInterp; |
| 1846 |
int isRootEnsemble; |
| 1847 |
|
| 1848 |
/* |
| 1849 |
* Append the arguments to the command prefix and invoke the command in |
| 1850 |
* the target interp's global namespace. |
| 1851 |
*/ |
| 1852 |
|
| 1853 |
prefc = aliasPtr->objc; |
| 1854 |
prefv = &aliasPtr->objPtr; |
| 1855 |
cmdc = prefc + objc - 1; |
| 1856 |
if (cmdc <= ALIAS_CMDV_PREALLOC) { |
| 1857 |
cmdv = cmdArr; |
| 1858 |
} else { |
| 1859 |
cmdv = (Tcl_Obj **)TclStackAlloc(interp, cmdc * sizeof(Tcl_Obj *)); |
| 1860 |
} |
| 1861 |
|
| 1862 |
memcpy(cmdv, prefv, prefc * sizeof(Tcl_Obj *)); |
| 1863 |
memcpy(cmdv+prefc, objv+1, (objc-1) * sizeof(Tcl_Obj *)); |
| 1864 |
|
| 1865 |
Tcl_ResetResult(targetInterp); |
| 1866 |
|
| 1867 |
for (i=0; i<cmdc; i++) { |
| 1868 |
Tcl_IncrRefCount(cmdv[i]); |
| 1869 |
} |
| 1870 |
|
| 1871 |
/* |
| 1872 |
* Use the ensemble rewriting machinery to ensure correct error messages: |
| 1873 |
* only the source command should show, not the full target prefix. |
| 1874 |
*/ |
| 1875 |
|
| 1876 |
isRootEnsemble = TclInitRewriteEnsemble((Tcl_Interp *)tPtr, 1, prefc, objv); |
| 1877 |
|
| 1878 |
/* |
| 1879 |
* Protect the target interpreter if it isn't the same as the source |
| 1880 |
* interpreter so that we can continue to work with it after the target |
| 1881 |
* command completes. |
| 1882 |
*/ |
| 1883 |
|
| 1884 |
if (targetInterp != interp) { |
| 1885 |
Tcl_Preserve(targetInterp); |
| 1886 |
} |
| 1887 |
|
| 1888 |
/* |
| 1889 |
* Execute the target command in the target interpreter. |
| 1890 |
*/ |
| 1891 |
|
| 1892 |
result = Tcl_EvalObjv(targetInterp, cmdc, cmdv, TCL_EVAL_INVOKE); |
| 1893 |
|
| 1894 |
/* |
| 1895 |
* Clean up the ensemble rewrite info if we set it in the first place. |
| 1896 |
*/ |
| 1897 |
|
| 1898 |
if (isRootEnsemble) { |
| 1899 |
TclResetRewriteEnsemble((Tcl_Interp *)tPtr, 1); |
| 1900 |
} |
| 1901 |
|
| 1902 |
/* |
| 1903 |
* If it was a cross-interpreter alias, we need to transfer the result |
| 1904 |
* back to the source interpreter and release the lock we previously set |
| 1905 |
* on the target interpreter. |
| 1906 |
*/ |
| 1907 |
|
| 1908 |
if (targetInterp != interp) { |
| 1909 |
Tcl_TransferResult(targetInterp, result, interp); |
| 1910 |
Tcl_Release(targetInterp); |
| 1911 |
} |
| 1912 |
|
| 1913 |
for (i=0; i<cmdc; i++) { |
| 1914 |
Tcl_DecrRefCount(cmdv[i]); |
| 1915 |
} |
| 1916 |
if (cmdv != cmdArr) { |
| 1917 |
TclStackFree(interp, cmdv); |
| 1918 |
} |
| 1919 |
return result; |
| 1920 |
#undef ALIAS_CMDV_PREALLOC |
| 1921 |
} |
| 1922 |
|
| 1923 |
/* |
| 1924 |
*---------------------------------------------------------------------- |
| 1925 |
* |
| 1926 |
* AliasObjCmdDeleteProc -- |
| 1927 |
* |
| 1928 |
* Is invoked when an alias command is deleted in a child. Cleans up all |
| 1929 |
* storage associated with this alias. |
| 1930 |
* |
| 1931 |
* Results: |
| 1932 |
* None. |
| 1933 |
* |
| 1934 |
* Side effects: |
| 1935 |
* Deletes the alias record and its entry in the alias table for the |
| 1936 |
* interpreter. |
| 1937 |
* |
| 1938 |
*---------------------------------------------------------------------- |
| 1939 |
*/ |
| 1940 |
|
| 1941 |
static void |
| 1942 |
AliasObjCmdDeleteProc( |
| 1943 |
ClientData clientData) /* The alias record for this alias. */ |
| 1944 |
{ |
| 1945 |
Alias *aliasPtr = (Alias *)clientData; |
| 1946 |
Target *targetPtr; |
| 1947 |
int i; |
| 1948 |
Tcl_Obj **objv; |
| 1949 |
|
| 1950 |
Tcl_DecrRefCount(aliasPtr->token); |
| 1951 |
objv = &aliasPtr->objPtr; |
| 1952 |
for (i = 0; i < aliasPtr->objc; i++) { |
| 1953 |
Tcl_DecrRefCount(objv[i]); |
| 1954 |
} |
| 1955 |
Tcl_DeleteHashEntry(aliasPtr->aliasEntryPtr); |
| 1956 |
|
| 1957 |
/* |
| 1958 |
* Splice the target record out of the target interpreter's parent list. |
| 1959 |
*/ |
| 1960 |
|
| 1961 |
targetPtr = aliasPtr->targetPtr; |
| 1962 |
if (targetPtr->prevPtr != NULL) { |
| 1963 |
targetPtr->prevPtr->nextPtr = targetPtr->nextPtr; |
| 1964 |
} else { |
| 1965 |
Parent *parentPtr = &((InterpInfo *) ((Interp *) |
| 1966 |
aliasPtr->targetInterp)->interpInfo)->parent; |
| 1967 |
|
| 1968 |
parentPtr->targetsPtr = targetPtr->nextPtr; |
| 1969 |
} |
| 1970 |
if (targetPtr->nextPtr != NULL) { |
| 1971 |
targetPtr->nextPtr->prevPtr = targetPtr->prevPtr; |
| 1972 |
} |
| 1973 |
|
| 1974 |
ckfree(targetPtr); |
| 1975 |
ckfree(aliasPtr); |
| 1976 |
} |
| 1977 |
|
| 1978 |
/* |
| 1979 |
*---------------------------------------------------------------------- |
| 1980 |
* |
| 1981 |
* Tcl_CreateChild -- |
| 1982 |
* |
| 1983 |
* Creates a child interpreter. The childPath argument denotes the name |
| 1984 |
* of the new child relative to the current interpreter; the child is a |
| 1985 |
* direct descendant of the one-before-last component of the path, |
| 1986 |
* e.g. it is a descendant of the current interpreter if the childPath |
| 1987 |
* argument contains only one component. Optionally makes the child |
| 1988 |
* interpreter safe. |
| 1989 |
* |
| 1990 |
* Results: |
| 1991 |
* Returns the interpreter structure created, or NULL if an error |
| 1992 |
* occurred. |
| 1993 |
* |
| 1994 |
* Side effects: |
| 1995 |
* Creates a new interpreter and a new interpreter object command in the |
| 1996 |
* interpreter indicated by the childPath argument. |
| 1997 |
* |
| 1998 |
*---------------------------------------------------------------------- |
| 1999 |
*/ |
| 2000 |
|
| 2001 |
Tcl_Interp * |
| 2002 |
Tcl_CreateChild( |
| 2003 |
Tcl_Interp *interp, /* Interpreter to start search at. */ |
| 2004 |
const char *childPath, /* Name of child to create. */ |
| 2005 |
int isSafe) /* Should new child be "safe" ? */ |
| 2006 |
{ |
| 2007 |
Tcl_Obj *pathPtr; |
| 2008 |
Tcl_Interp *childInterp; |
| 2009 |
|
| 2010 |
pathPtr = Tcl_NewStringObj(childPath, -1); |
| 2011 |
childInterp = ChildCreate(interp, pathPtr, isSafe); |
| 2012 |
Tcl_DecrRefCount(pathPtr); |
| 2013 |
|
| 2014 |
return childInterp; |
| 2015 |
} |
| 2016 |
|
| 2017 |
/* |
| 2018 |
*---------------------------------------------------------------------- |
| 2019 |
* |
| 2020 |
* Tcl_GetChild -- |
| 2021 |
* |
| 2022 |
* Finds a child interpreter by its path name. |
| 2023 |
* |
| 2024 |
* Results: |
| 2025 |
* Returns a Tcl_Interp * for the named interpreter or NULL if not found. |
| 2026 |
* |
| 2027 |
* Side effects: |
| 2028 |
* None. |
| 2029 |
* |
| 2030 |
*---------------------------------------------------------------------- |
| 2031 |
*/ |
| 2032 |
|
| 2033 |
Tcl_Interp * |
| 2034 |
Tcl_GetChild( |
| 2035 |
Tcl_Interp *interp, /* Interpreter to start search from. */ |
| 2036 |
const char *childPath) /* Path of child to find. */ |
| 2037 |
{ |
| 2038 |
Tcl_Obj *pathPtr; |
| 2039 |
Tcl_Interp *childInterp; |
| 2040 |
|
| 2041 |
pathPtr = Tcl_NewStringObj(childPath, -1); |
| 2042 |
childInterp = GetInterp(interp, pathPtr); |
| 2043 |
Tcl_DecrRefCount(pathPtr); |
| 2044 |
|
| 2045 |
return childInterp; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* |
| 2049 |
*---------------------------------------------------------------------- |
| 2050 |
* |
| 2051 |
* Tcl_GetParent -- |
| 2052 |
* |
| 2053 |
* Finds the parent interpreter of a child interpreter. |
| 2054 |
* |
| 2055 |
* Results: |
| 2056 |
* Returns a Tcl_Interp * for the parent interpreter or NULL if none. |
| 2057 |
* |
| 2058 |
* Side effects: |
| 2059 |
* None. |
| 2060 |
* |
| 2061 |
*---------------------------------------------------------------------- |
| 2062 |
*/ |
| 2063 |
|
| 2064 |
Tcl_Interp * |
| 2065 |
Tcl_GetParent( |
| 2066 |
Tcl_Interp *interp) /* Get the parent of this interpreter. */ |
| 2067 |
{ |
| 2068 |
Child *childPtr; /* Child record of this interpreter. */ |
| 2069 |
|
| 2070 |
if (interp == NULL) { |
| 2071 |
return NULL; |
| 2072 |
} |
| 2073 |
childPtr = &((InterpInfo *) ((Interp *) interp)->interpInfo)->child; |
| 2074 |
return childPtr->parentInterp; |
| 2075 |
} |
| 2076 |
|
| 2077 |
/* |
| 2078 |
*---------------------------------------------------------------------- |
| 2079 |
* |
| 2080 |
* TclSetChildCancelFlags -- |
| 2081 |
* |
| 2082 |
* This function marks all child interpreters belonging to a given |
| 2083 |
* interpreter as being canceled or not canceled, depending on the |
| 2084 |
* provided flags. |
| 2085 |
* |
| 2086 |
* Results: |
| 2087 |
* None. |
| 2088 |
* |
| 2089 |
* Side effects: |
| 2090 |
* None. |
| 2091 |
* |
| 2092 |
*---------------------------------------------------------------------- |
| 2093 |
*/ |
| 2094 |
|
| 2095 |
void |
| 2096 |
TclSetChildCancelFlags( |
| 2097 |
Tcl_Interp *interp, /* Set cancel flags of this interpreter. */ |
| 2098 |
int flags, /* Collection of OR-ed bits that control |
| 2099 |
* the cancellation of the script. Only |
| 2100 |
* TCL_CANCEL_UNWIND is currently |
| 2101 |
* supported. */ |
| 2102 |
int force) /* Non-zero to ignore numLevels for the purpose |
| 2103 |
* of resetting the cancellation flags. */ |
| 2104 |
{ |
| 2105 |
Parent *parentPtr; /* Parent record of given interpreter. */ |
| 2106 |
Tcl_HashEntry *hPtr; /* Search element. */ |
| 2107 |
Tcl_HashSearch hashSearch; /* Search variable. */ |
| 2108 |
Child *childPtr; /* Child record of interpreter. */ |
| 2109 |
Interp *iPtr; |
| 2110 |
|
| 2111 |
if (interp == NULL) { |
| 2112 |
return; |
| 2113 |
} |
| 2114 |
|
| 2115 |
flags &= (CANCELED | TCL_CANCEL_UNWIND); |
| 2116 |
|
| 2117 |
parentPtr = &((InterpInfo *) ((Interp *) interp)->interpInfo)->parent; |
| 2118 |
|
| 2119 |
hPtr = Tcl_FirstHashEntry(&parentPtr->childTable, &hashSearch); |
| 2120 |
for ( ; hPtr != NULL; hPtr = Tcl_NextHashEntry(&hashSearch)) { |
| 2121 |
childPtr = (Child *)Tcl_GetHashValue(hPtr); |
| 2122 |
iPtr = (Interp *) childPtr->childInterp; |
| 2123 |
|
| 2124 |
if (iPtr == NULL) { |
| 2125 |
continue; |
| 2126 |
} |
| 2127 |
|
| 2128 |
if (flags == 0) { |
| 2129 |
TclResetCancellation((Tcl_Interp *) iPtr, force); |
| 2130 |
} else { |
| 2131 |
TclSetCancelFlags(iPtr, flags); |
| 2132 |
} |
| 2133 |
|
| 2134 |
/* |
| 2135 |
* Now, recursively handle this for the children of this child |
| 2136 |
* interpreter. |
| 2137 |
*/ |
| 2138 |
|
| 2139 |
TclSetChildCancelFlags((Tcl_Interp *) iPtr, flags, force); |
| 2140 |
} |
| 2141 |
} |
| 2142 |
|
| 2143 |
/* |
| 2144 |
*---------------------------------------------------------------------- |
| 2145 |
* |
| 2146 |
* Tcl_GetInterpPath -- |
| 2147 |
* |
| 2148 |
* Sets the result of the asking interpreter to a proper Tcl list |
| 2149 |
* containing the names of interpreters between the asking and target |
| 2150 |
* interpreters. The target interpreter must be either the same as the |
| 2151 |
* asking interpreter or one of its children (including recursively). |
| 2152 |
* |
| 2153 |
* Results: |
| 2154 |
* TCL_OK if the target interpreter is the same as, or a descendant of, |
| 2155 |
* the asking interpreter; TCL_ERROR else. This way one can distinguish |
| 2156 |
* between the case where the asking and target interps are the same (an |
| 2157 |
* empty list is the result, and TCL_OK is returned) and when the target |
| 2158 |
* is not a descendant of the asking interpreter (in which case the Tcl |
| 2159 |
* result is an error message and the function returns TCL_ERROR). |
| 2160 |
* |
| 2161 |
* Side effects: |
| 2162 |
* None. |
| 2163 |
* |
| 2164 |
*---------------------------------------------------------------------- |
| 2165 |
*/ |
| 2166 |
|
| 2167 |
int |
| 2168 |
Tcl_GetInterpPath( |
| 2169 |
Tcl_Interp *interp, /* Interpreter to start search from. */ |
| 2170 |
Tcl_Interp *targetInterp) /* Interpreter to find. */ |
| 2171 |
{ |
| 2172 |
InterpInfo *iiPtr; |
| 2173 |
|
| 2174 |
if (targetInterp == interp) { |
| 2175 |
Tcl_SetObjResult(interp, Tcl_NewObj()); |
| 2176 |
return TCL_OK; |
| 2177 |
} |
| 2178 |
if (targetInterp == NULL) { |
| 2179 |
return TCL_ERROR; |
| 2180 |
} |
| 2181 |
iiPtr = (InterpInfo *) ((Interp *) targetInterp)->interpInfo; |
| 2182 |
if (Tcl_GetInterpPath(interp, iiPtr->child.parentInterp) != TCL_OK){ |
| 2183 |
return TCL_ERROR; |
| 2184 |
} |
| 2185 |
Tcl_ListObjAppendElement(NULL, Tcl_GetObjResult(interp), |
| 2186 |
Tcl_NewStringObj((const char *)Tcl_GetHashKey(&iiPtr->parent.childTable, |
| 2187 |
iiPtr->child.childEntryPtr), -1)); |
| 2188 |
return TCL_OK; |
| 2189 |
} |
| 2190 |
|
| 2191 |
/* |
| 2192 |
*---------------------------------------------------------------------- |
| 2193 |
* |
| 2194 |
* GetInterp -- |
| 2195 |
* |
| 2196 |
* Helper function to find a child interpreter given a pathname. |
| 2197 |
* |
| 2198 |
* Results: |
| 2199 |
* Returns the child interpreter known by that name in the calling |
| 2200 |
* interpreter, or NULL if no interpreter known by that name exists. |
| 2201 |
* |
| 2202 |
* Side effects: |
| 2203 |
* Assigns to the pointer variable passed in, if not NULL. |
| 2204 |
* |
| 2205 |
*---------------------------------------------------------------------- |
| 2206 |
*/ |
| 2207 |
|
| 2208 |
static Tcl_Interp * |
| 2209 |
GetInterp( |
| 2210 |
Tcl_Interp *interp, /* Interp. to start search from. */ |
| 2211 |
Tcl_Obj *pathPtr) /* List object containing name of interp. to |
| 2212 |
* be found. */ |
| 2213 |
{ |
| 2214 |
Tcl_HashEntry *hPtr; /* Search element. */ |
| 2215 |
Child *childPtr; /* Interim child record. */ |
| 2216 |
Tcl_Obj **objv; |
| 2217 |
int objc, i; |
| 2218 |
Tcl_Interp *searchInterp; /* Interim storage for interp. to find. */ |
| 2219 |
InterpInfo *parentInfoPtr; |
| 2220 |
|
| 2221 |
if (TclListObjGetElements(interp, pathPtr, &objc, &objv) != TCL_OK) { |
| 2222 |
return NULL; |
| 2223 |
} |
| 2224 |
|
| 2225 |
searchInterp = interp; |
| 2226 |
for (i = 0; i < objc; i++) { |
| 2227 |
parentInfoPtr = (InterpInfo *) ((Interp *) searchInterp)->interpInfo; |
| 2228 |
hPtr = Tcl_FindHashEntry(&parentInfoPtr->parent.childTable, |
| 2229 |
TclGetString(objv[i])); |
| 2230 |
if (hPtr == NULL) { |
| 2231 |
searchInterp = NULL; |
| 2232 |
break; |
| 2233 |
} |
| 2234 |
childPtr = (Child *)Tcl_GetHashValue(hPtr); |
| 2235 |
searchInterp = childPtr->childInterp; |
| 2236 |
if (searchInterp == NULL) { |
| 2237 |
break; |
| 2238 |
} |
| 2239 |
} |
| 2240 |
if (searchInterp == NULL) { |
| 2241 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2242 |
"could not find interpreter \"%s\"", TclGetString(pathPtr))); |
| 2243 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INTERP", |
| 2244 |
TclGetString(pathPtr), (char *)NULL); |
| 2245 |
} |
| 2246 |
return searchInterp; |
| 2247 |
} |
| 2248 |
|
| 2249 |
/* |
| 2250 |
*---------------------------------------------------------------------- |
| 2251 |
* |
| 2252 |
* ChildBgerror -- |
| 2253 |
* |
| 2254 |
* Helper function to set/query the background error handling command |
| 2255 |
* prefix of an interp |
| 2256 |
* |
| 2257 |
* Results: |
| 2258 |
* A standard Tcl result. |
| 2259 |
* |
| 2260 |
* Side effects: |
| 2261 |
* When (objc == 1), childInterp will be set to a new background handler |
| 2262 |
* of objv[0]. |
| 2263 |
* |
| 2264 |
*---------------------------------------------------------------------- |
| 2265 |
*/ |
| 2266 |
|
| 2267 |
static int |
| 2268 |
ChildBgerror( |
| 2269 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2270 |
Tcl_Interp *childInterp, /* Interp in which limit is set/queried. */ |
| 2271 |
int objc, /* Set or Query. */ |
| 2272 |
Tcl_Obj *const objv[]) /* Argument strings. */ |
| 2273 |
{ |
| 2274 |
if (objc) { |
| 2275 |
int length; |
| 2276 |
|
| 2277 |
if (TCL_ERROR == TclListObjLength(NULL, objv[0], &length) |
| 2278 |
|| (length < 1)) { |
| 2279 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2280 |
"cmdPrefix must be list of length >= 1", -1)); |
| 2281 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 2282 |
"BGERRORFORMAT", (char *)NULL); |
| 2283 |
return TCL_ERROR; |
| 2284 |
} |
| 2285 |
TclSetBgErrorHandler(childInterp, objv[0]); |
| 2286 |
} |
| 2287 |
Tcl_SetObjResult(interp, TclGetBgErrorHandler(childInterp)); |
| 2288 |
return TCL_OK; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* |
| 2292 |
*---------------------------------------------------------------------- |
| 2293 |
* |
| 2294 |
* ChildCreate -- |
| 2295 |
* |
| 2296 |
* Helper function to do the actual work of creating a child interp and |
| 2297 |
* new object command. Also optionally makes the new child interpreter |
| 2298 |
* "safe". |
| 2299 |
* |
| 2300 |
* Results: |
| 2301 |
* Returns the new Tcl_Interp * if successful or NULL if not. If failed, |
| 2302 |
* the result of the invoking interpreter contains an error message. |
| 2303 |
* |
| 2304 |
* Side effects: |
| 2305 |
* Creates a new child interpreter and a new object command. |
| 2306 |
* |
| 2307 |
*---------------------------------------------------------------------- |
| 2308 |
*/ |
| 2309 |
|
| 2310 |
static Tcl_Interp * |
| 2311 |
ChildCreate( |
| 2312 |
Tcl_Interp *interp, /* Interp. to start search from. */ |
| 2313 |
Tcl_Obj *pathPtr, /* Path (name) of child to create. */ |
| 2314 |
int safe) /* Should we make it "safe"? */ |
| 2315 |
{ |
| 2316 |
Tcl_Interp *parentInterp, *childInterp; |
| 2317 |
Child *childPtr; |
| 2318 |
InterpInfo *parentInfoPtr; |
| 2319 |
Tcl_HashEntry *hPtr; |
| 2320 |
const char *path; |
| 2321 |
int isNew, objc; |
| 2322 |
Tcl_Obj **objv; |
| 2323 |
|
| 2324 |
if (TclListObjGetElements(interp, pathPtr, &objc, &objv) != TCL_OK) { |
| 2325 |
return NULL; |
| 2326 |
} |
| 2327 |
if (objc < 2) { |
| 2328 |
parentInterp = interp; |
| 2329 |
path = TclGetString(pathPtr); |
| 2330 |
} else { |
| 2331 |
Tcl_Obj *objPtr; |
| 2332 |
|
| 2333 |
objPtr = Tcl_NewListObj(objc - 1, objv); |
| 2334 |
parentInterp = GetInterp(interp, objPtr); |
| 2335 |
Tcl_DecrRefCount(objPtr); |
| 2336 |
if (parentInterp == NULL) { |
| 2337 |
return NULL; |
| 2338 |
} |
| 2339 |
path = TclGetString(objv[objc - 1]); |
| 2340 |
} |
| 2341 |
if (safe == 0) { |
| 2342 |
safe = Tcl_IsSafe(parentInterp); |
| 2343 |
} |
| 2344 |
|
| 2345 |
parentInfoPtr = (InterpInfo *) ((Interp *) parentInterp)->interpInfo; |
| 2346 |
hPtr = Tcl_CreateHashEntry(&parentInfoPtr->parent.childTable, path, |
| 2347 |
&isNew); |
| 2348 |
if (isNew == 0) { |
| 2349 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2350 |
"interpreter named \"%s\" already exists, cannot create", |
| 2351 |
path)); |
| 2352 |
return NULL; |
| 2353 |
} |
| 2354 |
|
| 2355 |
childInterp = Tcl_CreateInterp(); |
| 2356 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 2357 |
childPtr->parentInterp = parentInterp; |
| 2358 |
childPtr->childEntryPtr = hPtr; |
| 2359 |
childPtr->childInterp = childInterp; |
| 2360 |
childPtr->interpCmd = Tcl_NRCreateCommand(parentInterp, path, |
| 2361 |
ChildObjCmd, NRChildCmd, childInterp, ChildObjCmdDeleteProc); |
| 2362 |
Tcl_InitHashTable(&childPtr->aliasTable, TCL_STRING_KEYS); |
| 2363 |
Tcl_SetHashValue(hPtr, childPtr); |
| 2364 |
Tcl_SetVar(childInterp, "tcl_interactive", "0", TCL_GLOBAL_ONLY); |
| 2365 |
|
| 2366 |
/* |
| 2367 |
* Inherit the recursion limit. |
| 2368 |
*/ |
| 2369 |
|
| 2370 |
((Interp *) childInterp)->maxNestingDepth = |
| 2371 |
((Interp *) parentInterp)->maxNestingDepth; |
| 2372 |
|
| 2373 |
if (safe) { |
| 2374 |
if (Tcl_MakeSafe(childInterp) == TCL_ERROR) { |
| 2375 |
goto error; |
| 2376 |
} |
| 2377 |
} else { |
| 2378 |
if (Tcl_Init(childInterp) == TCL_ERROR) { |
| 2379 |
goto error; |
| 2380 |
} |
| 2381 |
|
| 2382 |
/* |
| 2383 |
* This will create the "memory" command in child interpreters if we |
| 2384 |
* compiled with TCL_MEM_DEBUG, otherwise it does nothing. |
| 2385 |
*/ |
| 2386 |
|
| 2387 |
Tcl_InitMemory(childInterp); |
| 2388 |
} |
| 2389 |
|
| 2390 |
/* |
| 2391 |
* Inherit the TIP#143 limits. |
| 2392 |
*/ |
| 2393 |
|
| 2394 |
InheritLimitsFromParent(childInterp, parentInterp); |
| 2395 |
|
| 2396 |
/* |
| 2397 |
* The [clock] command presents a safe API, but uses unsafe features in |
| 2398 |
* its implementation. This means it has to be implemented in safe interps |
| 2399 |
* as an alias to a version in the (trusted) parent. |
| 2400 |
*/ |
| 2401 |
|
| 2402 |
if (safe) { |
| 2403 |
Tcl_Obj *clockObj; |
| 2404 |
int status; |
| 2405 |
|
| 2406 |
TclNewLiteralStringObj(clockObj, "clock"); |
| 2407 |
Tcl_IncrRefCount(clockObj); |
| 2408 |
status = AliasCreate(interp, childInterp, parentInterp, clockObj, |
| 2409 |
clockObj, 0, NULL); |
| 2410 |
Tcl_DecrRefCount(clockObj); |
| 2411 |
if (status != TCL_OK) { |
| 2412 |
goto error2; |
| 2413 |
} |
| 2414 |
} |
| 2415 |
|
| 2416 |
return childInterp; |
| 2417 |
|
| 2418 |
error: |
| 2419 |
Tcl_TransferResult(childInterp, TCL_ERROR, interp); |
| 2420 |
error2: |
| 2421 |
Tcl_DeleteInterp(childInterp); |
| 2422 |
|
| 2423 |
return NULL; |
| 2424 |
} |
| 2425 |
|
| 2426 |
/* |
| 2427 |
*---------------------------------------------------------------------- |
| 2428 |
* |
| 2429 |
* ChildObjCmd -- |
| 2430 |
* |
| 2431 |
* Command to manipulate an interpreter, e.g. to send commands to it to |
| 2432 |
* be evaluated. One such command exists for each child interpreter. |
| 2433 |
* |
| 2434 |
* Results: |
| 2435 |
* A standard Tcl result. |
| 2436 |
* |
| 2437 |
* Side effects: |
| 2438 |
* See user documentation for details. |
| 2439 |
* |
| 2440 |
*---------------------------------------------------------------------- |
| 2441 |
*/ |
| 2442 |
|
| 2443 |
static int |
| 2444 |
ChildObjCmd( |
| 2445 |
ClientData clientData, /* Child interpreter. */ |
| 2446 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2447 |
int objc, /* Number of arguments. */ |
| 2448 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2449 |
{ |
| 2450 |
return Tcl_NRCallObjProc(interp, NRChildCmd, clientData, objc, objv); |
| 2451 |
} |
| 2452 |
|
| 2453 |
static int |
| 2454 |
NRChildCmd( |
| 2455 |
ClientData clientData, /* Child interpreter. */ |
| 2456 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2457 |
int objc, /* Number of arguments. */ |
| 2458 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2459 |
{ |
| 2460 |
Tcl_Interp *childInterp = (Tcl_Interp *)clientData; |
| 2461 |
int index; |
| 2462 |
static const char *const options[] = { |
| 2463 |
"alias", "aliases", "bgerror", "debug", |
| 2464 |
"eval", "expose", "hide", "hidden", |
| 2465 |
"issafe", "invokehidden", "limit", "marktrusted", |
| 2466 |
"recursionlimit", NULL |
| 2467 |
}; |
| 2468 |
enum childCmdOptionsEnum { |
| 2469 |
OPT_ALIAS, OPT_ALIASES, OPT_BGERROR, OPT_DEBUG, |
| 2470 |
OPT_EVAL, OPT_EXPOSE, OPT_HIDE, OPT_HIDDEN, |
| 2471 |
OPT_ISSAFE, OPT_INVOKEHIDDEN, OPT_LIMIT, OPT_MARKTRUSTED, |
| 2472 |
OPT_RECLIMIT |
| 2473 |
}; |
| 2474 |
|
| 2475 |
if (childInterp == NULL) { |
| 2476 |
Tcl_Panic("ChildObjCmd: interpreter has been deleted"); |
| 2477 |
} |
| 2478 |
|
| 2479 |
if (objc < 2) { |
| 2480 |
Tcl_WrongNumArgs(interp, 1, objv, "cmd ?arg ...?"); |
| 2481 |
return TCL_ERROR; |
| 2482 |
} |
| 2483 |
if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", 0, |
| 2484 |
&index) != TCL_OK) { |
| 2485 |
return TCL_ERROR; |
| 2486 |
} |
| 2487 |
|
| 2488 |
switch ((enum childCmdOptionsEnum) index) { |
| 2489 |
case OPT_ALIAS: |
| 2490 |
if (objc > 2) { |
| 2491 |
if (objc == 3) { |
| 2492 |
return AliasDescribe(interp, childInterp, objv[2]); |
| 2493 |
} |
| 2494 |
if (TclGetString(objv[3])[0] == '\0') { |
| 2495 |
if (objc == 4) { |
| 2496 |
return AliasDelete(interp, childInterp, objv[2]); |
| 2497 |
} |
| 2498 |
} else { |
| 2499 |
return AliasCreate(interp, childInterp, interp, objv[2], |
| 2500 |
objv[3], objc - 4, objv + 4); |
| 2501 |
} |
| 2502 |
} |
| 2503 |
Tcl_WrongNumArgs(interp, 2, objv, "aliasName ?targetName? ?arg ...?"); |
| 2504 |
return TCL_ERROR; |
| 2505 |
case OPT_ALIASES: |
| 2506 |
if (objc != 2) { |
| 2507 |
Tcl_WrongNumArgs(interp, 2, objv, NULL); |
| 2508 |
return TCL_ERROR; |
| 2509 |
} |
| 2510 |
return AliasList(interp, childInterp); |
| 2511 |
case OPT_BGERROR: |
| 2512 |
if (objc != 2 && objc != 3) { |
| 2513 |
Tcl_WrongNumArgs(interp, 2, objv, "?cmdPrefix?"); |
| 2514 |
return TCL_ERROR; |
| 2515 |
} |
| 2516 |
return ChildBgerror(interp, childInterp, objc - 2, objv + 2); |
| 2517 |
case OPT_DEBUG: |
| 2518 |
/* |
| 2519 |
* TIP #378 |
| 2520 |
* Currently only -frame supported, otherwise ?-option ?value? ...? |
| 2521 |
*/ |
| 2522 |
if (objc > 4) { |
| 2523 |
Tcl_WrongNumArgs(interp, 2, objv, "?-frame ?bool??"); |
| 2524 |
return TCL_ERROR; |
| 2525 |
} |
| 2526 |
return ChildDebugCmd(interp, childInterp, objc - 2, objv + 2); |
| 2527 |
case OPT_EVAL: |
| 2528 |
if (objc < 3) { |
| 2529 |
Tcl_WrongNumArgs(interp, 2, objv, "arg ?arg ...?"); |
| 2530 |
return TCL_ERROR; |
| 2531 |
} |
| 2532 |
return ChildEval(interp, childInterp, objc - 2, objv + 2); |
| 2533 |
case OPT_EXPOSE: |
| 2534 |
if ((objc < 3) || (objc > 4)) { |
| 2535 |
Tcl_WrongNumArgs(interp, 2, objv, "hiddenCmdName ?cmdName?"); |
| 2536 |
return TCL_ERROR; |
| 2537 |
} |
| 2538 |
return ChildExpose(interp, childInterp, objc - 2, objv + 2); |
| 2539 |
case OPT_HIDE: |
| 2540 |
if ((objc < 3) || (objc > 4)) { |
| 2541 |
Tcl_WrongNumArgs(interp, 2, objv, "cmdName ?hiddenCmdName?"); |
| 2542 |
return TCL_ERROR; |
| 2543 |
} |
| 2544 |
return ChildHide(interp, childInterp, objc - 2, objv + 2); |
| 2545 |
case OPT_HIDDEN: |
| 2546 |
if (objc != 2) { |
| 2547 |
Tcl_WrongNumArgs(interp, 2, objv, NULL); |
| 2548 |
return TCL_ERROR; |
| 2549 |
} |
| 2550 |
return ChildHidden(interp, childInterp); |
| 2551 |
case OPT_ISSAFE: |
| 2552 |
if (objc != 2) { |
| 2553 |
Tcl_WrongNumArgs(interp, 2, objv, NULL); |
| 2554 |
return TCL_ERROR; |
| 2555 |
} |
| 2556 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(Tcl_IsSafe(childInterp))); |
| 2557 |
return TCL_OK; |
| 2558 |
case OPT_INVOKEHIDDEN: { |
| 2559 |
int i; |
| 2560 |
const char *namespaceName; |
| 2561 |
static const char *const hiddenOptions[] = { |
| 2562 |
"-global", "-namespace", "--", NULL |
| 2563 |
}; |
| 2564 |
enum hiddenOption { |
| 2565 |
OPT_GLOBAL, OPT_NAMESPACE, OPT_LAST |
| 2566 |
}; |
| 2567 |
|
| 2568 |
namespaceName = NULL; |
| 2569 |
for (i = 2; i < objc; i++) { |
| 2570 |
if (TclGetString(objv[i])[0] != '-') { |
| 2571 |
break; |
| 2572 |
} |
| 2573 |
if (Tcl_GetIndexFromObj(interp, objv[i], hiddenOptions, "option", |
| 2574 |
0, &index) != TCL_OK) { |
| 2575 |
return TCL_ERROR; |
| 2576 |
} |
| 2577 |
if (index == OPT_GLOBAL) { |
| 2578 |
namespaceName = "::"; |
| 2579 |
} else if (index == OPT_NAMESPACE) { |
| 2580 |
if (++i == objc) { /* There must be more arguments. */ |
| 2581 |
break; |
| 2582 |
} else { |
| 2583 |
namespaceName = TclGetString(objv[i]); |
| 2584 |
} |
| 2585 |
} else { |
| 2586 |
i++; |
| 2587 |
break; |
| 2588 |
} |
| 2589 |
} |
| 2590 |
if (objc - i < 1) { |
| 2591 |
Tcl_WrongNumArgs(interp, 2, objv, |
| 2592 |
"?-namespace ns? ?-global? ?--? cmd ?arg ..?"); |
| 2593 |
return TCL_ERROR; |
| 2594 |
} |
| 2595 |
return ChildInvokeHidden(interp, childInterp, namespaceName, |
| 2596 |
objc - i, objv + i); |
| 2597 |
} |
| 2598 |
case OPT_LIMIT: { |
| 2599 |
static const char *const limitTypes[] = { |
| 2600 |
"commands", "time", NULL |
| 2601 |
}; |
| 2602 |
enum LimitTypes { |
| 2603 |
LIMIT_TYPE_COMMANDS, LIMIT_TYPE_TIME |
| 2604 |
}; |
| 2605 |
int limitType; |
| 2606 |
|
| 2607 |
if (objc < 3) { |
| 2608 |
Tcl_WrongNumArgs(interp, 2, objv, "limitType ?-option value ...?"); |
| 2609 |
return TCL_ERROR; |
| 2610 |
} |
| 2611 |
if (Tcl_GetIndexFromObj(interp, objv[2], limitTypes, "limit type", 0, |
| 2612 |
&limitType) != TCL_OK) { |
| 2613 |
return TCL_ERROR; |
| 2614 |
} |
| 2615 |
switch ((enum LimitTypes) limitType) { |
| 2616 |
case LIMIT_TYPE_COMMANDS: |
| 2617 |
return ChildCommandLimitCmd(interp, childInterp, 3, objc,objv); |
| 2618 |
case LIMIT_TYPE_TIME: |
| 2619 |
return ChildTimeLimitCmd(interp, childInterp, 3, objc, objv); |
| 2620 |
} |
| 2621 |
} |
| 2622 |
break; |
| 2623 |
case OPT_MARKTRUSTED: |
| 2624 |
if (objc != 2) { |
| 2625 |
Tcl_WrongNumArgs(interp, 2, objv, NULL); |
| 2626 |
return TCL_ERROR; |
| 2627 |
} |
| 2628 |
return ChildMarkTrusted(interp, childInterp); |
| 2629 |
case OPT_RECLIMIT: |
| 2630 |
if (objc != 2 && objc != 3) { |
| 2631 |
Tcl_WrongNumArgs(interp, 2, objv, "?newlimit?"); |
| 2632 |
return TCL_ERROR; |
| 2633 |
} |
| 2634 |
return ChildRecursionLimit(interp, childInterp, objc - 2, objv + 2); |
| 2635 |
} |
| 2636 |
|
| 2637 |
return TCL_ERROR; |
| 2638 |
} |
| 2639 |
|
| 2640 |
/* |
| 2641 |
*---------------------------------------------------------------------- |
| 2642 |
* |
| 2643 |
* ChildObjCmdDeleteProc -- |
| 2644 |
* |
| 2645 |
* Invoked when an object command for a child interpreter is deleted; |
| 2646 |
* cleans up all state associated with the child interpreter and destroys |
| 2647 |
* the child interpreter. |
| 2648 |
* |
| 2649 |
* Results: |
| 2650 |
* None. |
| 2651 |
* |
| 2652 |
* Side effects: |
| 2653 |
* Cleans up all state associated with the child interpreter and destroys |
| 2654 |
* the child interpreter. |
| 2655 |
* |
| 2656 |
*---------------------------------------------------------------------- |
| 2657 |
*/ |
| 2658 |
|
| 2659 |
static void |
| 2660 |
ChildObjCmdDeleteProc( |
| 2661 |
ClientData clientData) /* The ChildRecord for the command. */ |
| 2662 |
{ |
| 2663 |
Child *childPtr; /* Interim storage for Child record. */ |
| 2664 |
Tcl_Interp *childInterp = (Tcl_Interp *)clientData; |
| 2665 |
/* And for a child interp. */ |
| 2666 |
|
| 2667 |
childPtr = &((InterpInfo *) ((Interp *) childInterp)->interpInfo)->child; |
| 2668 |
|
| 2669 |
/* |
| 2670 |
* Unlink the child from its parent interpreter. |
| 2671 |
*/ |
| 2672 |
|
| 2673 |
Tcl_DeleteHashEntry(childPtr->childEntryPtr); |
| 2674 |
|
| 2675 |
/* |
| 2676 |
* Set to NULL so that when the InterpInfo is cleaned up in the child it |
| 2677 |
* does not try to delete the command causing all sorts of grief. See |
| 2678 |
* ChildRecordDeleteProc(). |
| 2679 |
*/ |
| 2680 |
|
| 2681 |
childPtr->interpCmd = NULL; |
| 2682 |
|
| 2683 |
if (childPtr->childInterp != NULL) { |
| 2684 |
Tcl_DeleteInterp(childPtr->childInterp); |
| 2685 |
} |
| 2686 |
} |
| 2687 |
|
| 2688 |
/* |
| 2689 |
*---------------------------------------------------------------------- |
| 2690 |
* |
| 2691 |
* ChildDebugCmd -- TIP #378 |
| 2692 |
* |
| 2693 |
* Helper function to handle 'debug' command in a child interpreter. |
| 2694 |
* |
| 2695 |
* Results: |
| 2696 |
* A standard Tcl result. |
| 2697 |
* |
| 2698 |
* Side effects: |
| 2699 |
* May modify INTERP_DEBUG_FRAME flag in the child. |
| 2700 |
* |
| 2701 |
*---------------------------------------------------------------------- |
| 2702 |
*/ |
| 2703 |
|
| 2704 |
static int |
| 2705 |
ChildDebugCmd( |
| 2706 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2707 |
Tcl_Interp *childInterp, /* The child interpreter in which command |
| 2708 |
* will be evaluated. */ |
| 2709 |
int objc, /* Number of arguments. */ |
| 2710 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2711 |
{ |
| 2712 |
static const char *const debugTypes[] = { |
| 2713 |
"-frame", NULL |
| 2714 |
}; |
| 2715 |
enum DebugTypes { |
| 2716 |
DEBUG_TYPE_FRAME |
| 2717 |
}; |
| 2718 |
int debugType; |
| 2719 |
Interp *iPtr; |
| 2720 |
Tcl_Obj *resultPtr; |
| 2721 |
|
| 2722 |
iPtr = (Interp *) childInterp; |
| 2723 |
if (objc == 0) { |
| 2724 |
TclNewObj(resultPtr); |
| 2725 |
Tcl_ListObjAppendElement(NULL, resultPtr, |
| 2726 |
Tcl_NewStringObj("-frame", -1)); |
| 2727 |
Tcl_ListObjAppendElement(NULL, resultPtr, |
| 2728 |
Tcl_NewBooleanObj(iPtr->flags & INTERP_DEBUG_FRAME)); |
| 2729 |
Tcl_SetObjResult(interp, resultPtr); |
| 2730 |
} else { |
| 2731 |
if (Tcl_GetIndexFromObj(interp, objv[0], debugTypes, "debug option", |
| 2732 |
0, &debugType) != TCL_OK) { |
| 2733 |
return TCL_ERROR; |
| 2734 |
} |
| 2735 |
if (debugType == DEBUG_TYPE_FRAME) { |
| 2736 |
if (objc == 2) { /* set */ |
| 2737 |
if (Tcl_GetBooleanFromObj(interp, objv[1], &debugType) |
| 2738 |
!= TCL_OK) { |
| 2739 |
return TCL_ERROR; |
| 2740 |
} |
| 2741 |
|
| 2742 |
/* |
| 2743 |
* Quietly ignore attempts to disable interp debugging. This |
| 2744 |
* is a one-way switch as frame debug info is maintained in a |
| 2745 |
* stack that must be consistent once turned on. |
| 2746 |
*/ |
| 2747 |
|
| 2748 |
if (debugType) { |
| 2749 |
iPtr->flags |= INTERP_DEBUG_FRAME; |
| 2750 |
} |
| 2751 |
} |
| 2752 |
Tcl_SetObjResult(interp, |
| 2753 |
Tcl_NewBooleanObj(iPtr->flags & INTERP_DEBUG_FRAME)); |
| 2754 |
} |
| 2755 |
} |
| 2756 |
return TCL_OK; |
| 2757 |
} |
| 2758 |
|
| 2759 |
/* |
| 2760 |
*---------------------------------------------------------------------- |
| 2761 |
* |
| 2762 |
* ChildEval -- |
| 2763 |
* |
| 2764 |
* Helper function to evaluate a command in a child interpreter. |
| 2765 |
* |
| 2766 |
* Results: |
| 2767 |
* A standard Tcl result. |
| 2768 |
* |
| 2769 |
* Side effects: |
| 2770 |
* Whatever the command does. |
| 2771 |
* |
| 2772 |
*---------------------------------------------------------------------- |
| 2773 |
*/ |
| 2774 |
|
| 2775 |
static int |
| 2776 |
ChildEval( |
| 2777 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2778 |
Tcl_Interp *childInterp, /* The child interpreter in which command |
| 2779 |
* will be evaluated. */ |
| 2780 |
int objc, /* Number of arguments. */ |
| 2781 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2782 |
{ |
| 2783 |
int result; |
| 2784 |
|
| 2785 |
/* |
| 2786 |
* TIP #285: If necessary, reset the cancellation flags for the child |
| 2787 |
* interpreter now; otherwise, canceling a script in a parent interpreter |
| 2788 |
* can result in a situation where a child interpreter can no longer |
| 2789 |
* evaluate any scripts unless somebody calls the TclResetCancellation |
| 2790 |
* function for that particular Tcl_Interp. |
| 2791 |
*/ |
| 2792 |
|
| 2793 |
TclSetChildCancelFlags(childInterp, 0, 0); |
| 2794 |
|
| 2795 |
Tcl_Preserve(childInterp); |
| 2796 |
Tcl_AllowExceptions(childInterp); |
| 2797 |
|
| 2798 |
if (objc == 1) { |
| 2799 |
/* |
| 2800 |
* TIP #280: Make actual argument location available to eval'd script. |
| 2801 |
*/ |
| 2802 |
|
| 2803 |
Interp *iPtr = (Interp *) interp; |
| 2804 |
CmdFrame *invoker = iPtr->cmdFramePtr; |
| 2805 |
int word = 0; |
| 2806 |
|
| 2807 |
TclArgumentGet(interp, objv[0], &invoker, &word); |
| 2808 |
|
| 2809 |
result = TclEvalObjEx(childInterp, objv[0], 0, invoker, word); |
| 2810 |
} else { |
| 2811 |
Tcl_Obj *objPtr = Tcl_ConcatObj(objc, objv); |
| 2812 |
Tcl_IncrRefCount(objPtr); |
| 2813 |
result = Tcl_EvalObjEx(childInterp, objPtr, 0); |
| 2814 |
Tcl_DecrRefCount(objPtr); |
| 2815 |
} |
| 2816 |
Tcl_TransferResult(childInterp, result, interp); |
| 2817 |
|
| 2818 |
Tcl_Release(childInterp); |
| 2819 |
return result; |
| 2820 |
} |
| 2821 |
|
| 2822 |
/* |
| 2823 |
*---------------------------------------------------------------------- |
| 2824 |
* |
| 2825 |
* ChildExpose -- |
| 2826 |
* |
| 2827 |
* Helper function to expose a command in a child interpreter. |
| 2828 |
* |
| 2829 |
* Results: |
| 2830 |
* A standard Tcl result. |
| 2831 |
* |
| 2832 |
* Side effects: |
| 2833 |
* After this call scripts in the child will be able to invoke the newly |
| 2834 |
* exposed command. |
| 2835 |
* |
| 2836 |
*---------------------------------------------------------------------- |
| 2837 |
*/ |
| 2838 |
|
| 2839 |
static int |
| 2840 |
ChildExpose( |
| 2841 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2842 |
Tcl_Interp *childInterp, /* Interp in which command will be exposed. */ |
| 2843 |
int objc, /* Number of arguments. */ |
| 2844 |
Tcl_Obj *const objv[]) /* Argument strings. */ |
| 2845 |
{ |
| 2846 |
const char *name; |
| 2847 |
|
| 2848 |
if (Tcl_IsSafe(interp)) { |
| 2849 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2850 |
"permission denied: safe interpreter cannot expose commands", |
| 2851 |
-1)); |
| 2852 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "UNSAFE", |
| 2853 |
(char *)NULL); |
| 2854 |
return TCL_ERROR; |
| 2855 |
} |
| 2856 |
|
| 2857 |
name = TclGetString(objv[(objc == 1) ? 0 : 1]); |
| 2858 |
if (Tcl_ExposeCommand(childInterp, TclGetString(objv[0]), |
| 2859 |
name) != TCL_OK) { |
| 2860 |
Tcl_TransferResult(childInterp, TCL_ERROR, interp); |
| 2861 |
return TCL_ERROR; |
| 2862 |
} |
| 2863 |
return TCL_OK; |
| 2864 |
} |
| 2865 |
|
| 2866 |
/* |
| 2867 |
*---------------------------------------------------------------------- |
| 2868 |
* |
| 2869 |
* ChildRecursionLimit -- |
| 2870 |
* |
| 2871 |
* Helper function to set/query the Recursion limit of an interp |
| 2872 |
* |
| 2873 |
* Results: |
| 2874 |
* A standard Tcl result. |
| 2875 |
* |
| 2876 |
* Side effects: |
| 2877 |
* When (objc == 1), childInterp will be set to a new recursion limit of |
| 2878 |
* objv[0]. |
| 2879 |
* |
| 2880 |
*---------------------------------------------------------------------- |
| 2881 |
*/ |
| 2882 |
|
| 2883 |
static int |
| 2884 |
ChildRecursionLimit( |
| 2885 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2886 |
Tcl_Interp *childInterp, /* Interp in which limit is set/queried. */ |
| 2887 |
int objc, /* Set or Query. */ |
| 2888 |
Tcl_Obj *const objv[]) /* Argument strings. */ |
| 2889 |
{ |
| 2890 |
Interp *iPtr; |
| 2891 |
int limit; |
| 2892 |
|
| 2893 |
if (objc) { |
| 2894 |
if (Tcl_IsSafe(interp)) { |
| 2895 |
Tcl_SetObjResult(interp, Tcl_NewStringObj("permission denied: " |
| 2896 |
"safe interpreters cannot change recursion limit", -1)); |
| 2897 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "UNSAFE", |
| 2898 |
(char *)NULL); |
| 2899 |
return TCL_ERROR; |
| 2900 |
} |
| 2901 |
if (TclGetIntFromObj(interp, objv[0], &limit) == TCL_ERROR) { |
| 2902 |
return TCL_ERROR; |
| 2903 |
} |
| 2904 |
if (limit <= 0) { |
| 2905 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2906 |
"recursion limit must be > 0", -1)); |
| 2907 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "BADLIMIT", |
| 2908 |
(char *)NULL); |
| 2909 |
return TCL_ERROR; |
| 2910 |
} |
| 2911 |
Tcl_SetRecursionLimit(childInterp, limit); |
| 2912 |
iPtr = (Interp *) childInterp; |
| 2913 |
if (interp == childInterp && iPtr->numLevels > limit) { |
| 2914 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2915 |
"falling back due to new recursion limit", -1)); |
| 2916 |
Tcl_SetErrorCode(interp, "TCL", "RECURSION", (char *)NULL); |
| 2917 |
return TCL_ERROR; |
| 2918 |
} |
| 2919 |
Tcl_SetObjResult(interp, objv[0]); |
| 2920 |
return TCL_OK; |
| 2921 |
} else { |
| 2922 |
limit = Tcl_SetRecursionLimit(childInterp, 0); |
| 2923 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(limit)); |
| 2924 |
return TCL_OK; |
| 2925 |
} |
| 2926 |
} |
| 2927 |
|
| 2928 |
/* |
| 2929 |
*---------------------------------------------------------------------- |
| 2930 |
* |
| 2931 |
* ChildHide -- |
| 2932 |
* |
| 2933 |
* Helper function to hide a command in a child interpreter. |
| 2934 |
* |
| 2935 |
* Results: |
| 2936 |
* A standard Tcl result. |
| 2937 |
* |
| 2938 |
* Side effects: |
| 2939 |
* After this call scripts in the child will no longer be able to invoke |
| 2940 |
* the named command. |
| 2941 |
* |
| 2942 |
*---------------------------------------------------------------------- |
| 2943 |
*/ |
| 2944 |
|
| 2945 |
static int |
| 2946 |
ChildHide( |
| 2947 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 2948 |
Tcl_Interp *childInterp, /* Interp in which command will be exposed. */ |
| 2949 |
int objc, /* Number of arguments. */ |
| 2950 |
Tcl_Obj *const objv[]) /* Argument strings. */ |
| 2951 |
{ |
| 2952 |
const char *name; |
| 2953 |
|
| 2954 |
if (Tcl_IsSafe(interp)) { |
| 2955 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2956 |
"permission denied: safe interpreter cannot hide commands", |
| 2957 |
-1)); |
| 2958 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "UNSAFE", |
| 2959 |
(char *)NULL); |
| 2960 |
return TCL_ERROR; |
| 2961 |
} |
| 2962 |
|
| 2963 |
name = TclGetString(objv[(objc == 1) ? 0 : 1]); |
| 2964 |
if (Tcl_HideCommand(childInterp, TclGetString(objv[0]), name) != TCL_OK) { |
| 2965 |
Tcl_TransferResult(childInterp, TCL_ERROR, interp); |
| 2966 |
return TCL_ERROR; |
| 2967 |
} |
| 2968 |
return TCL_OK; |
| 2969 |
} |
| 2970 |
|
| 2971 |
/* |
| 2972 |
*---------------------------------------------------------------------- |
| 2973 |
* |
| 2974 |
* ChildHidden -- |
| 2975 |
* |
| 2976 |
* Helper function to compute list of hidden commands in a child |
| 2977 |
* interpreter. |
| 2978 |
* |
| 2979 |
* Results: |
| 2980 |
* A standard Tcl result. |
| 2981 |
* |
| 2982 |
* Side effects: |
| 2983 |
* None. |
| 2984 |
* |
| 2985 |
*---------------------------------------------------------------------- |
| 2986 |
*/ |
| 2987 |
|
| 2988 |
static int |
| 2989 |
ChildHidden( |
| 2990 |
Tcl_Interp *interp, /* Interp for data return. */ |
| 2991 |
Tcl_Interp *childInterp) /* Interp whose hidden commands to query. */ |
| 2992 |
{ |
| 2993 |
Tcl_Obj *listObjPtr; /* Local object pointer. */ |
| 2994 |
Tcl_HashTable *hTblPtr; /* For local searches. */ |
| 2995 |
Tcl_HashEntry *hPtr; /* For local searches. */ |
| 2996 |
Tcl_HashSearch hSearch; /* For local searches. */ |
| 2997 |
|
| 2998 |
TclNewObj(listObjPtr); |
| 2999 |
hTblPtr = ((Interp *) childInterp)->hiddenCmdTablePtr; |
| 3000 |
if (hTblPtr != NULL) { |
| 3001 |
for (hPtr = Tcl_FirstHashEntry(hTblPtr, &hSearch); |
| 3002 |
hPtr != NULL; |
| 3003 |
hPtr = Tcl_NextHashEntry(&hSearch)) { |
| 3004 |
Tcl_ListObjAppendElement(NULL, listObjPtr, |
| 3005 |
Tcl_NewStringObj((const char *)Tcl_GetHashKey(hTblPtr, hPtr), -1)); |
| 3006 |
} |
| 3007 |
} |
| 3008 |
Tcl_SetObjResult(interp, listObjPtr); |
| 3009 |
return TCL_OK; |
| 3010 |
} |
| 3011 |
|
| 3012 |
/* |
| 3013 |
*---------------------------------------------------------------------- |
| 3014 |
* |
| 3015 |
* ChildInvokeHidden -- |
| 3016 |
* |
| 3017 |
* Helper function to invoke a hidden command in a child interpreter. |
| 3018 |
* |
| 3019 |
* Results: |
| 3020 |
* A standard Tcl result. |
| 3021 |
* |
| 3022 |
* Side effects: |
| 3023 |
* Whatever the hidden command does. |
| 3024 |
* |
| 3025 |
*---------------------------------------------------------------------- |
| 3026 |
*/ |
| 3027 |
|
| 3028 |
static int |
| 3029 |
ChildInvokeHidden( |
| 3030 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 3031 |
Tcl_Interp *childInterp, /* The child interpreter in which command will |
| 3032 |
* be invoked. */ |
| 3033 |
const char *namespaceName, /* The namespace to use, if any. */ |
| 3034 |
int objc, /* Number of arguments. */ |
| 3035 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 3036 |
{ |
| 3037 |
int result; |
| 3038 |
|
| 3039 |
if (Tcl_IsSafe(interp)) { |
| 3040 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3041 |
"not allowed to invoke hidden commands from safe interpreter", |
| 3042 |
-1)); |
| 3043 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "UNSAFE", |
| 3044 |
(char *)NULL); |
| 3045 |
return TCL_ERROR; |
| 3046 |
} |
| 3047 |
|
| 3048 |
Tcl_Preserve(childInterp); |
| 3049 |
Tcl_AllowExceptions(childInterp); |
| 3050 |
|
| 3051 |
if (namespaceName == NULL) { |
| 3052 |
NRE_callback *rootPtr = TOP_CB(childInterp); |
| 3053 |
|
| 3054 |
Tcl_NRAddCallback(interp, NRPostInvokeHidden, childInterp, |
| 3055 |
rootPtr, NULL, NULL); |
| 3056 |
return TclNRInvoke(NULL, childInterp, objc, objv); |
| 3057 |
} else { |
| 3058 |
Namespace *nsPtr, *dummy1, *dummy2; |
| 3059 |
const char *tail; |
| 3060 |
|
| 3061 |
result = TclGetNamespaceForQualName(childInterp, namespaceName, NULL, |
| 3062 |
TCL_FIND_ONLY_NS | TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG |
| 3063 |
| TCL_CREATE_NS_IF_UNKNOWN, &nsPtr, &dummy1, &dummy2, &tail); |
| 3064 |
if (result == TCL_OK) { |
| 3065 |
result = TclObjInvokeNamespace(childInterp, objc, objv, |
| 3066 |
(Tcl_Namespace *) nsPtr, TCL_INVOKE_HIDDEN); |
| 3067 |
} |
| 3068 |
} |
| 3069 |
|
| 3070 |
Tcl_TransferResult(childInterp, result, interp); |
| 3071 |
|
| 3072 |
Tcl_Release(childInterp); |
| 3073 |
return result; |
| 3074 |
} |
| 3075 |
|
| 3076 |
static int |
| 3077 |
NRPostInvokeHidden( |
| 3078 |
ClientData data[], |
| 3079 |
Tcl_Interp *interp, |
| 3080 |
int result) |
| 3081 |
{ |
| 3082 |
Tcl_Interp *childInterp = (Tcl_Interp *)data[0]; |
| 3083 |
NRE_callback *rootPtr = (NRE_callback *)data[1]; |
| 3084 |
|
| 3085 |
if (interp != childInterp) { |
| 3086 |
result = TclNRRunCallbacks(childInterp, result, rootPtr); |
| 3087 |
Tcl_TransferResult(childInterp, result, interp); |
| 3088 |
} |
| 3089 |
Tcl_Release(childInterp); |
| 3090 |
return result; |
| 3091 |
} |
| 3092 |
|
| 3093 |
/* |
| 3094 |
*---------------------------------------------------------------------- |
| 3095 |
* |
| 3096 |
* ChildMarkTrusted -- |
| 3097 |
* |
| 3098 |
* Helper function to mark a child interpreter as trusted (unsafe). |
| 3099 |
* |
| 3100 |
* Results: |
| 3101 |
* A standard Tcl result. |
| 3102 |
* |
| 3103 |
* Side effects: |
| 3104 |
* After this call the hard-wired security checks in the core no longer |
| 3105 |
* prevent the child from performing certain operations. |
| 3106 |
* |
| 3107 |
*---------------------------------------------------------------------- |
| 3108 |
*/ |
| 3109 |
|
| 3110 |
static int |
| 3111 |
ChildMarkTrusted( |
| 3112 |
Tcl_Interp *interp, /* Interp for error return. */ |
| 3113 |
Tcl_Interp *childInterp) /* The child interpreter which will be marked |
| 3114 |
* trusted. */ |
| 3115 |
{ |
| 3116 |
if (Tcl_IsSafe(interp)) { |
| 3117 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3118 |
"permission denied: safe interpreter cannot mark trusted", |
| 3119 |
-1)); |
| 3120 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "UNSAFE", |
| 3121 |
(char *)NULL); |
| 3122 |
return TCL_ERROR; |
| 3123 |
} |
| 3124 |
((Interp *) childInterp)->flags &= ~SAFE_INTERP; |
| 3125 |
return TCL_OK; |
| 3126 |
} |
| 3127 |
|
| 3128 |
/* |
| 3129 |
*---------------------------------------------------------------------- |
| 3130 |
* |
| 3131 |
* Tcl_IsSafe -- |
| 3132 |
* |
| 3133 |
* Determines whether an interpreter is safe |
| 3134 |
* |
| 3135 |
* Results: |
| 3136 |
* 1 if it is safe, 0 if it is not. |
| 3137 |
* |
| 3138 |
* Side effects: |
| 3139 |
* None. |
| 3140 |
* |
| 3141 |
*---------------------------------------------------------------------- |
| 3142 |
*/ |
| 3143 |
|
| 3144 |
int |
| 3145 |
Tcl_IsSafe( |
| 3146 |
Tcl_Interp *interp) /* Is this interpreter "safe" ? */ |
| 3147 |
{ |
| 3148 |
Interp *iPtr = (Interp *) interp; |
| 3149 |
|
| 3150 |
if (iPtr == NULL) { |
| 3151 |
return 0; |
| 3152 |
} |
| 3153 |
return (iPtr->flags & SAFE_INTERP) ? 1 : 0; |
| 3154 |
} |
| 3155 |
|
| 3156 |
/* |
| 3157 |
*---------------------------------------------------------------------- |
| 3158 |
* |
| 3159 |
* Tcl_MakeSafe -- |
| 3160 |
* |
| 3161 |
* Makes its argument interpreter contain only functionality that is |
| 3162 |
* defined to be part of Safe Tcl. Unsafe commands are hidden, the env |
| 3163 |
* array is unset, and the standard channels are removed. |
| 3164 |
* |
| 3165 |
* Results: |
| 3166 |
* None. |
| 3167 |
* |
| 3168 |
* Side effects: |
| 3169 |
* Hides commands in its argument interpreter, and removes settings and |
| 3170 |
* channels. |
| 3171 |
* |
| 3172 |
*---------------------------------------------------------------------- |
| 3173 |
*/ |
| 3174 |
|
| 3175 |
int |
| 3176 |
Tcl_MakeSafe( |
| 3177 |
Tcl_Interp *interp) /* Interpreter to be made safe. */ |
| 3178 |
{ |
| 3179 |
Tcl_Channel chan; /* Channel to remove from safe interpreter. */ |
| 3180 |
Interp *iPtr = (Interp *) interp; |
| 3181 |
Tcl_Interp *parent = ((InterpInfo*) iPtr->interpInfo)->child.parentInterp; |
| 3182 |
|
| 3183 |
TclHideUnsafeCommands(interp); |
| 3184 |
|
| 3185 |
if (parent != NULL) { |
| 3186 |
/* |
| 3187 |
* Alias these function implementations in the child to those in the |
| 3188 |
* parent; the overall implementations are safe, but they're normally |
| 3189 |
* defined by init.tcl which is not sourced by safe interpreters. |
| 3190 |
* Assume these functions all work. [Bug 2895741] |
| 3191 |
*/ |
| 3192 |
|
| 3193 |
(void) Tcl_Eval(interp, |
| 3194 |
"namespace eval ::tcl {namespace eval mathfunc {}}"); |
| 3195 |
(void) Tcl_CreateAlias(interp, "::tcl::mathfunc::min", parent, |
| 3196 |
"::tcl::mathfunc::min", 0, NULL); |
| 3197 |
(void) Tcl_CreateAlias(interp, "::tcl::mathfunc::max", parent, |
| 3198 |
"::tcl::mathfunc::max", 0, NULL); |
| 3199 |
} |
| 3200 |
|
| 3201 |
iPtr->flags |= SAFE_INTERP; |
| 3202 |
|
| 3203 |
/* |
| 3204 |
* Unsetting variables : (which should not have been set in the first |
| 3205 |
* place, but...) |
| 3206 |
*/ |
| 3207 |
|
| 3208 |
/* |
| 3209 |
* No env array in a safe interpreter. |
| 3210 |
*/ |
| 3211 |
|
| 3212 |
Tcl_UnsetVar(interp, "env", TCL_GLOBAL_ONLY); |
| 3213 |
|
| 3214 |
/* |
| 3215 |
* Remove unsafe parts of tcl_platform |
| 3216 |
*/ |
| 3217 |
|
| 3218 |
Tcl_UnsetVar2(interp, "tcl_platform", "os", TCL_GLOBAL_ONLY); |
| 3219 |
Tcl_UnsetVar2(interp, "tcl_platform", "osVersion", TCL_GLOBAL_ONLY); |
| 3220 |
Tcl_UnsetVar2(interp, "tcl_platform", "machine", TCL_GLOBAL_ONLY); |
| 3221 |
Tcl_UnsetVar2(interp, "tcl_platform", "user", TCL_GLOBAL_ONLY); |
| 3222 |
|
| 3223 |
/* |
| 3224 |
* Unset path information variables (the only one remaining is [info |
| 3225 |
* nameofexecutable]) |
| 3226 |
*/ |
| 3227 |
|
| 3228 |
Tcl_UnsetVar(interp, "tclDefaultLibrary", TCL_GLOBAL_ONLY); |
| 3229 |
Tcl_UnsetVar(interp, "tcl_library", TCL_GLOBAL_ONLY); |
| 3230 |
Tcl_UnsetVar(interp, "tcl_pkgPath", TCL_GLOBAL_ONLY); |
| 3231 |
|
| 3232 |
/* |
| 3233 |
* Remove the standard channels from the interpreter; safe interpreters do |
| 3234 |
* not ordinarily have access to stdin, stdout and stderr. |
| 3235 |
* |
| 3236 |
* NOTE: These channels are not added to the interpreter by the |
| 3237 |
* Tcl_CreateInterp call, but may be added later, by another I/O |
| 3238 |
* operation. We want to ensure that the interpreter does not have these |
| 3239 |
* channels even if it is being made safe after being used for some time.. |
| 3240 |
*/ |
| 3241 |
|
| 3242 |
chan = Tcl_GetStdChannel(TCL_STDIN); |
| 3243 |
if (chan != NULL) { |
| 3244 |
Tcl_UnregisterChannel(interp, chan); |
| 3245 |
} |
| 3246 |
chan = Tcl_GetStdChannel(TCL_STDOUT); |
| 3247 |
if (chan != NULL) { |
| 3248 |
Tcl_UnregisterChannel(interp, chan); |
| 3249 |
} |
| 3250 |
chan = Tcl_GetStdChannel(TCL_STDERR); |
| 3251 |
if (chan != NULL) { |
| 3252 |
Tcl_UnregisterChannel(interp, chan); |
| 3253 |
} |
| 3254 |
|
| 3255 |
return TCL_OK; |
| 3256 |
} |
| 3257 |
|
| 3258 |
/* |
| 3259 |
*---------------------------------------------------------------------- |
| 3260 |
* |
| 3261 |
* Tcl_LimitExceeded -- |
| 3262 |
* |
| 3263 |
* Tests whether any limit has been exceeded in the given interpreter |
| 3264 |
* (i.e. whether the interpreter is currently unable to process further |
| 3265 |
* scripts). |
| 3266 |
* |
| 3267 |
* Results: |
| 3268 |
* A boolean value. |
| 3269 |
* |
| 3270 |
* Side effects: |
| 3271 |
* None. |
| 3272 |
* |
| 3273 |
* Notes: |
| 3274 |
* If you change this function, you MUST also update TclLimitExceeded() in |
| 3275 |
* tclInt.h. |
| 3276 |
*---------------------------------------------------------------------- |
| 3277 |
*/ |
| 3278 |
|
| 3279 |
int |
| 3280 |
Tcl_LimitExceeded( |
| 3281 |
Tcl_Interp *interp) |
| 3282 |
{ |
| 3283 |
Interp *iPtr = (Interp *) interp; |
| 3284 |
|
| 3285 |
return iPtr->limit.exceeded != 0; |
| 3286 |
} |
| 3287 |
|
| 3288 |
/* |
| 3289 |
*---------------------------------------------------------------------- |
| 3290 |
* |
| 3291 |
* Tcl_LimitReady -- |
| 3292 |
* |
| 3293 |
* Find out whether any limit has been set on the interpreter, and if so |
| 3294 |
* check whether the granularity of that limit is such that the full |
| 3295 |
* limit check should be carried out. |
| 3296 |
* |
| 3297 |
* Results: |
| 3298 |
* A boolean value that indicates whether to call Tcl_LimitCheck. |
| 3299 |
* |
| 3300 |
* Side effects: |
| 3301 |
* Increments the limit granularity counter. |
| 3302 |
* |
| 3303 |
* Notes: |
| 3304 |
* If you change this function, you MUST also update TclLimitReady() in |
| 3305 |
* tclInt.h. |
| 3306 |
* |
| 3307 |
*---------------------------------------------------------------------- |
| 3308 |
*/ |
| 3309 |
|
| 3310 |
int |
| 3311 |
Tcl_LimitReady( |
| 3312 |
Tcl_Interp *interp) |
| 3313 |
{ |
| 3314 |
Interp *iPtr = (Interp *) interp; |
| 3315 |
|
| 3316 |
if (iPtr->limit.active != 0) { |
| 3317 |
int ticker = ++iPtr->limit.granularityTicker; |
| 3318 |
|
| 3319 |
if ((iPtr->limit.active & TCL_LIMIT_COMMANDS) && |
| 3320 |
((iPtr->limit.cmdGranularity == 1) || |
| 3321 |
(ticker % iPtr->limit.cmdGranularity == 0))) { |
| 3322 |
return 1; |
| 3323 |
} |
| 3324 |
if ((iPtr->limit.active & TCL_LIMIT_TIME) && |
| 3325 |
((iPtr->limit.timeGranularity == 1) || |
| 3326 |
(ticker % iPtr->limit.timeGranularity == 0))) { |
| 3327 |
return 1; |
| 3328 |
} |
| 3329 |
} |
| 3330 |
return 0; |
| 3331 |
} |
| 3332 |
|
| 3333 |
/* |
| 3334 |
*---------------------------------------------------------------------- |
| 3335 |
* |
| 3336 |
* Tcl_LimitCheck -- |
| 3337 |
* |
| 3338 |
* Check all currently set limits in the interpreter (where permitted by |
| 3339 |
* granularity). If a limit is exceeded, call its callbacks and, if the |
| 3340 |
* limit is still exceeded after the callbacks have run, make the |
| 3341 |
* interpreter generate an error that cannot be caught within the limited |
| 3342 |
* interpreter. |
| 3343 |
* |
| 3344 |
* Results: |
| 3345 |
* A Tcl result value (TCL_OK if no limit is exceeded, and TCL_ERROR if a |
| 3346 |
* limit has been exceeded). |
| 3347 |
* |
| 3348 |
* Side effects: |
| 3349 |
* May invoke system calls. May invoke other interpreters. May be |
| 3350 |
* reentrant. May put the interpreter into a state where it can no longer |
| 3351 |
* execute commands without outside intervention. |
| 3352 |
* |
| 3353 |
*---------------------------------------------------------------------- |
| 3354 |
*/ |
| 3355 |
|
| 3356 |
int |
| 3357 |
Tcl_LimitCheck( |
| 3358 |
Tcl_Interp *interp) |
| 3359 |
{ |
| 3360 |
Interp *iPtr = (Interp *) interp; |
| 3361 |
int ticker = iPtr->limit.granularityTicker; |
| 3362 |
|
| 3363 |
if (Tcl_InterpDeleted(interp)) { |
| 3364 |
return TCL_OK; |
| 3365 |
} |
| 3366 |
|
| 3367 |
if ((iPtr->limit.active & TCL_LIMIT_COMMANDS) && |
| 3368 |
((iPtr->limit.cmdGranularity == 1) || |
| 3369 |
(ticker % iPtr->limit.cmdGranularity == 0)) && |
| 3370 |
(iPtr->limit.cmdCount < iPtr->cmdCount)) { |
| 3371 |
iPtr->limit.exceeded |= TCL_LIMIT_COMMANDS; |
| 3372 |
Tcl_Preserve(interp); |
| 3373 |
RunLimitHandlers(iPtr->limit.cmdHandlers, interp); |
| 3374 |
if (iPtr->limit.cmdCount >= iPtr->cmdCount) { |
| 3375 |
iPtr->limit.exceeded &= ~TCL_LIMIT_COMMANDS; |
| 3376 |
} else if (iPtr->limit.exceeded & TCL_LIMIT_COMMANDS) { |
| 3377 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3378 |
"command count limit exceeded", -1)); |
| 3379 |
Tcl_SetErrorCode(interp, "TCL", "LIMIT", "COMMANDS", (char *)NULL); |
| 3380 |
Tcl_Release(interp); |
| 3381 |
return TCL_ERROR; |
| 3382 |
} |
| 3383 |
Tcl_Release(interp); |
| 3384 |
} |
| 3385 |
|
| 3386 |
if ((iPtr->limit.active & TCL_LIMIT_TIME) && |
| 3387 |
((iPtr->limit.timeGranularity == 1) || |
| 3388 |
(ticker % iPtr->limit.timeGranularity == 0))) { |
| 3389 |
Tcl_Time now; |
| 3390 |
|
| 3391 |
Tcl_GetTime(&now); |
| 3392 |
if (iPtr->limit.time.sec < now.sec || |
| 3393 |
(iPtr->limit.time.sec == now.sec && |
| 3394 |
iPtr->limit.time.usec < now.usec)) { |
| 3395 |
iPtr->limit.exceeded |= TCL_LIMIT_TIME; |
| 3396 |
Tcl_Preserve(interp); |
| 3397 |
RunLimitHandlers(iPtr->limit.timeHandlers, interp); |
| 3398 |
if (iPtr->limit.time.sec > now.sec || |
| 3399 |
(iPtr->limit.time.sec == now.sec && |
| 3400 |
iPtr->limit.time.usec >= now.usec)) { |
| 3401 |
iPtr->limit.exceeded &= ~TCL_LIMIT_TIME; |
| 3402 |
} else if (iPtr->limit.exceeded & TCL_LIMIT_TIME) { |
| 3403 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3404 |
"time limit exceeded", -1)); |
| 3405 |
Tcl_SetErrorCode(interp, "TCL", "LIMIT", "TIME", (char *)NULL); |
| 3406 |
Tcl_Release(interp); |
| 3407 |
return TCL_ERROR; |
| 3408 |
} |
| 3409 |
Tcl_Release(interp); |
| 3410 |
} |
| 3411 |
} |
| 3412 |
|
| 3413 |
return TCL_OK; |
| 3414 |
} |
| 3415 |
|
| 3416 |
/* |
| 3417 |
*---------------------------------------------------------------------- |
| 3418 |
* |
| 3419 |
* RunLimitHandlers -- |
| 3420 |
* |
| 3421 |
* Invoke all the limit handlers in a list (for a particular limit). |
| 3422 |
* Note that no particular limit handler callback will be invoked |
| 3423 |
* reentrantly. |
| 3424 |
* |
| 3425 |
* Results: |
| 3426 |
* None. |
| 3427 |
* |
| 3428 |
* Side effects: |
| 3429 |
* Depends on the limit handlers. |
| 3430 |
* |
| 3431 |
*---------------------------------------------------------------------- |
| 3432 |
*/ |
| 3433 |
|
| 3434 |
static void |
| 3435 |
RunLimitHandlers( |
| 3436 |
LimitHandler *handlerPtr, |
| 3437 |
Tcl_Interp *interp) |
| 3438 |
{ |
| 3439 |
LimitHandler *nextPtr; |
| 3440 |
for (; handlerPtr!=NULL ; handlerPtr=nextPtr) { |
| 3441 |
if (handlerPtr->flags & (LIMIT_HANDLER_DELETED|LIMIT_HANDLER_ACTIVE)) { |
| 3442 |
/* |
| 3443 |
* Reentrant call or something seriously strange in the delete |
| 3444 |
* code. |
| 3445 |
*/ |
| 3446 |
|
| 3447 |
nextPtr = handlerPtr->nextPtr; |
| 3448 |
continue; |
| 3449 |
} |
| 3450 |
|
| 3451 |
/* |
| 3452 |
* Set the ACTIVE flag while running the limit handler itself so we |
| 3453 |
* cannot reentrantly call this handler and know to use the alternate |
| 3454 |
* method of deletion if necessary. |
| 3455 |
*/ |
| 3456 |
|
| 3457 |
handlerPtr->flags |= LIMIT_HANDLER_ACTIVE; |
| 3458 |
handlerPtr->handlerProc(handlerPtr->clientData, interp); |
| 3459 |
handlerPtr->flags &= ~LIMIT_HANDLER_ACTIVE; |
| 3460 |
|
| 3461 |
/* |
| 3462 |
* Rediscover this value; it might have changed during the processing |
| 3463 |
* of a limit handler. We have to record it here because we might |
| 3464 |
* delete the structure below, and reading a value out of a deleted |
| 3465 |
* structure is unsafe (even if actually legal with some |
| 3466 |
* malloc()/free() implementations.) |
| 3467 |
*/ |
| 3468 |
|
| 3469 |
nextPtr = handlerPtr->nextPtr; |
| 3470 |
|
| 3471 |
/* |
| 3472 |
* If we deleted the current handler while we were executing it, we |
| 3473 |
* will have spliced it out of the list and set the |
| 3474 |
* LIMIT_HANDLER_DELETED flag. |
| 3475 |
*/ |
| 3476 |
|
| 3477 |
if (handlerPtr->flags & LIMIT_HANDLER_DELETED) { |
| 3478 |
if (handlerPtr->deleteProc != NULL) { |
| 3479 |
handlerPtr->deleteProc(handlerPtr->clientData); |
| 3480 |
} |
| 3481 |
ckfree(handlerPtr); |
| 3482 |
} |
| 3483 |
} |
| 3484 |
} |
| 3485 |
|
| 3486 |
/* |
| 3487 |
*---------------------------------------------------------------------- |
| 3488 |
* |
| 3489 |
* Tcl_LimitAddHandler -- |
| 3490 |
* |
| 3491 |
* Add a callback handler for a particular resource limit. |
| 3492 |
* |
| 3493 |
* Results: |
| 3494 |
* None. |
| 3495 |
* |
| 3496 |
* Side effects: |
| 3497 |
* Extends the internal linked list of handlers for a limit. |
| 3498 |
* |
| 3499 |
*---------------------------------------------------------------------- |
| 3500 |
*/ |
| 3501 |
|
| 3502 |
/* Bug 52dbc4b3f8: wrap Tcl_Free since it is not a Tcl_LimitHandlerDeleteProc. */ |
| 3503 |
static void |
| 3504 |
WrapFree( |
| 3505 |
void *ptr) |
| 3506 |
{ |
| 3507 |
ckfree(ptr); |
| 3508 |
} |
| 3509 |
|
| 3510 |
void |
| 3511 |
Tcl_LimitAddHandler( |
| 3512 |
Tcl_Interp *interp, |
| 3513 |
int type, |
| 3514 |
Tcl_LimitHandlerProc *handlerProc, |
| 3515 |
ClientData clientData, |
| 3516 |
Tcl_LimitHandlerDeleteProc *deleteProc) |
| 3517 |
{ |
| 3518 |
Interp *iPtr = (Interp *) interp; |
| 3519 |
LimitHandler *handlerPtr; |
| 3520 |
|
| 3521 |
/* |
| 3522 |
* Convert everything into a real deletion callback. |
| 3523 |
*/ |
| 3524 |
|
| 3525 |
if (deleteProc == (Tcl_LimitHandlerDeleteProc *) TCL_DYNAMIC) { |
| 3526 |
deleteProc = WrapFree; |
| 3527 |
} |
| 3528 |
|
| 3529 |
/* |
| 3530 |
* Allocate a handler record. |
| 3531 |
*/ |
| 3532 |
|
| 3533 |
handlerPtr = (LimitHandler *)ckalloc(sizeof(LimitHandler)); |
| 3534 |
handlerPtr->flags = 0; |
| 3535 |
handlerPtr->handlerProc = handlerProc; |
| 3536 |
handlerPtr->clientData = clientData; |
| 3537 |
handlerPtr->deleteProc = deleteProc; |
| 3538 |
handlerPtr->prevPtr = NULL; |
| 3539 |
|
| 3540 |
/* |
| 3541 |
* Prepend onto the front of the correct linked list. |
| 3542 |
*/ |
| 3543 |
|
| 3544 |
switch (type) { |
| 3545 |
case TCL_LIMIT_COMMANDS: |
| 3546 |
handlerPtr->nextPtr = iPtr->limit.cmdHandlers; |
| 3547 |
if (handlerPtr->nextPtr != NULL) { |
| 3548 |
handlerPtr->nextPtr->prevPtr = handlerPtr; |
| 3549 |
} |
| 3550 |
iPtr->limit.cmdHandlers = handlerPtr; |
| 3551 |
return; |
| 3552 |
|
| 3553 |
case TCL_LIMIT_TIME: |
| 3554 |
handlerPtr->nextPtr = iPtr->limit.timeHandlers; |
| 3555 |
if (handlerPtr->nextPtr != NULL) { |
| 3556 |
handlerPtr->nextPtr->prevPtr = handlerPtr; |
| 3557 |
} |
| 3558 |
iPtr->limit.timeHandlers = handlerPtr; |
| 3559 |
return; |
| 3560 |
} |
| 3561 |
|
| 3562 |
Tcl_Panic("unknown type of resource limit"); |
| 3563 |
} |
| 3564 |
|
| 3565 |
/* |
| 3566 |
*---------------------------------------------------------------------- |
| 3567 |
* |
| 3568 |
* Tcl_LimitRemoveHandler -- |
| 3569 |
* |
| 3570 |
* Remove a callback handler for a particular resource limit. |
| 3571 |
* |
| 3572 |
* Results: |
| 3573 |
* None. |
| 3574 |
* |
| 3575 |
* Side effects: |
| 3576 |
* The handler is spliced out of the internal linked list for the limit, |
| 3577 |
* and if not currently being invoked, deleted. Otherwise it is just |
| 3578 |
* marked for deletion and removed when the limit handler has finished |
| 3579 |
* executing. |
| 3580 |
* |
| 3581 |
*---------------------------------------------------------------------- |
| 3582 |
*/ |
| 3583 |
|
| 3584 |
void |
| 3585 |
Tcl_LimitRemoveHandler( |
| 3586 |
Tcl_Interp *interp, |
| 3587 |
int type, |
| 3588 |
Tcl_LimitHandlerProc *handlerProc, |
| 3589 |
ClientData clientData) |
| 3590 |
{ |
| 3591 |
Interp *iPtr = (Interp *) interp; |
| 3592 |
LimitHandler *handlerPtr; |
| 3593 |
|
| 3594 |
switch (type) { |
| 3595 |
case TCL_LIMIT_COMMANDS: |
| 3596 |
handlerPtr = iPtr->limit.cmdHandlers; |
| 3597 |
break; |
| 3598 |
case TCL_LIMIT_TIME: |
| 3599 |
handlerPtr = iPtr->limit.timeHandlers; |
| 3600 |
break; |
| 3601 |
default: |
| 3602 |
Tcl_Panic("unknown type of resource limit"); |
| 3603 |
return; |
| 3604 |
} |
| 3605 |
|
| 3606 |
for (; handlerPtr!=NULL ; handlerPtr=handlerPtr->nextPtr) { |
| 3607 |
if ((handlerPtr->handlerProc != handlerProc) || |
| 3608 |
(handlerPtr->clientData != clientData)) { |
| 3609 |
continue; |
| 3610 |
} |
| 3611 |
|
| 3612 |
/* |
| 3613 |
* We've found the handler to delete; mark it as doomed if not already |
| 3614 |
* so marked (which shouldn't actually happen). |
| 3615 |
*/ |
| 3616 |
|
| 3617 |
if (handlerPtr->flags & LIMIT_HANDLER_DELETED) { |
| 3618 |
return; |
| 3619 |
} |
| 3620 |
handlerPtr->flags |= LIMIT_HANDLER_DELETED; |
| 3621 |
|
| 3622 |
/* |
| 3623 |
* Splice the handler out of the doubly-linked list. |
| 3624 |
*/ |
| 3625 |
|
| 3626 |
if (handlerPtr->prevPtr == NULL) { |
| 3627 |
switch (type) { |
| 3628 |
case TCL_LIMIT_COMMANDS: |
| 3629 |
iPtr->limit.cmdHandlers = handlerPtr->nextPtr; |
| 3630 |
break; |
| 3631 |
case TCL_LIMIT_TIME: |
| 3632 |
iPtr->limit.timeHandlers = handlerPtr->nextPtr; |
| 3633 |
break; |
| 3634 |
} |
| 3635 |
} else { |
| 3636 |
handlerPtr->prevPtr->nextPtr = handlerPtr->nextPtr; |
| 3637 |
} |
| 3638 |
if (handlerPtr->nextPtr != NULL) { |
| 3639 |
handlerPtr->nextPtr->prevPtr = handlerPtr->prevPtr; |
| 3640 |
} |
| 3641 |
|
| 3642 |
/* |
| 3643 |
* If nothing is currently executing the handler, delete its client |
| 3644 |
* data and the overall handler structure now. Otherwise it will all |
| 3645 |
* go away when the handler returns. |
| 3646 |
*/ |
| 3647 |
|
| 3648 |
if (!(handlerPtr->flags & LIMIT_HANDLER_ACTIVE)) { |
| 3649 |
if (handlerPtr->deleteProc != NULL) { |
| 3650 |
handlerPtr->deleteProc(handlerPtr->clientData); |
| 3651 |
} |
| 3652 |
ckfree(handlerPtr); |
| 3653 |
} |
| 3654 |
return; |
| 3655 |
} |
| 3656 |
} |
| 3657 |
|
| 3658 |
/* |
| 3659 |
*---------------------------------------------------------------------- |
| 3660 |
* |
| 3661 |
* TclLimitRemoveAllHandlers -- |
| 3662 |
* |
| 3663 |
* Remove all limit callback handlers for an interpreter. This is invoked |
| 3664 |
* as part of deleting the interpreter. |
| 3665 |
* |
| 3666 |
* Results: |
| 3667 |
* None. |
| 3668 |
* |
| 3669 |
* Side effects: |
| 3670 |
* Limit handlers are deleted or marked for deletion (as with |
| 3671 |
* Tcl_LimitRemoveHandler). |
| 3672 |
* |
| 3673 |
*---------------------------------------------------------------------- |
| 3674 |
*/ |
| 3675 |
|
| 3676 |
void |
| 3677 |
TclLimitRemoveAllHandlers( |
| 3678 |
Tcl_Interp *interp) |
| 3679 |
{ |
| 3680 |
Interp *iPtr = (Interp *) interp; |
| 3681 |
LimitHandler *handlerPtr, *nextHandlerPtr; |
| 3682 |
|
| 3683 |
/* |
| 3684 |
* Delete all command-limit handlers. |
| 3685 |
*/ |
| 3686 |
|
| 3687 |
for (handlerPtr=iPtr->limit.cmdHandlers, iPtr->limit.cmdHandlers=NULL; |
| 3688 |
handlerPtr!=NULL; handlerPtr=nextHandlerPtr) { |
| 3689 |
nextHandlerPtr = handlerPtr->nextPtr; |
| 3690 |
|
| 3691 |
/* |
| 3692 |
* Do not delete here if it has already been marked for deletion. |
| 3693 |
*/ |
| 3694 |
|
| 3695 |
if (handlerPtr->flags & LIMIT_HANDLER_DELETED) { |
| 3696 |
continue; |
| 3697 |
} |
| 3698 |
handlerPtr->flags |= LIMIT_HANDLER_DELETED; |
| 3699 |
handlerPtr->prevPtr = NULL; |
| 3700 |
handlerPtr->nextPtr = NULL; |
| 3701 |
|
| 3702 |
/* |
| 3703 |
* If nothing is currently executing the handler, delete its client |
| 3704 |
* data and the overall handler structure now. Otherwise it will all |
| 3705 |
* go away when the handler returns. |
| 3706 |
*/ |
| 3707 |
|
| 3708 |
if (!(handlerPtr->flags & LIMIT_HANDLER_ACTIVE)) { |
| 3709 |
if (handlerPtr->deleteProc != NULL) { |
| 3710 |
handlerPtr->deleteProc(handlerPtr->clientData); |
| 3711 |
} |
| 3712 |
ckfree(handlerPtr); |
| 3713 |
} |
| 3714 |
} |
| 3715 |
|
| 3716 |
/* |
| 3717 |
* Delete all time-limit handlers. |
| 3718 |
*/ |
| 3719 |
|
| 3720 |
for (handlerPtr=iPtr->limit.timeHandlers, iPtr->limit.timeHandlers=NULL; |
| 3721 |
handlerPtr!=NULL; handlerPtr=nextHandlerPtr) { |
| 3722 |
nextHandlerPtr = handlerPtr->nextPtr; |
| 3723 |
|
| 3724 |
/* |
| 3725 |
* Do not delete here if it has already been marked for deletion. |
| 3726 |
*/ |
| 3727 |
|
| 3728 |
if (handlerPtr->flags & LIMIT_HANDLER_DELETED) { |
| 3729 |
continue; |
| 3730 |
} |
| 3731 |
handlerPtr->flags |= LIMIT_HANDLER_DELETED; |
| 3732 |
handlerPtr->prevPtr = NULL; |
| 3733 |
handlerPtr->nextPtr = NULL; |
| 3734 |
|
| 3735 |
/* |
| 3736 |
* If nothing is currently executing the handler, delete its client |
| 3737 |
* data and the overall handler structure now. Otherwise it will all |
| 3738 |
* go away when the handler returns. |
| 3739 |
*/ |
| 3740 |
|
| 3741 |
if (!(handlerPtr->flags & LIMIT_HANDLER_ACTIVE)) { |
| 3742 |
if (handlerPtr->deleteProc != NULL) { |
| 3743 |
handlerPtr->deleteProc(handlerPtr->clientData); |
| 3744 |
} |
| 3745 |
ckfree(handlerPtr); |
| 3746 |
} |
| 3747 |
} |
| 3748 |
|
| 3749 |
/* |
| 3750 |
* Delete the timer callback that is used to trap limits that occur in |
| 3751 |
* [vwait]s... |
| 3752 |
*/ |
| 3753 |
|
| 3754 |
if (iPtr->limit.timeEvent != NULL) { |
| 3755 |
Tcl_DeleteTimerHandler(iPtr->limit.timeEvent); |
| 3756 |
iPtr->limit.timeEvent = NULL; |
| 3757 |
} |
| 3758 |
} |
| 3759 |
|
| 3760 |
/* |
| 3761 |
*---------------------------------------------------------------------- |
| 3762 |
* |
| 3763 |
* Tcl_LimitTypeEnabled -- |
| 3764 |
* |
| 3765 |
* Check whether a particular limit has been enabled for an interpreter. |
| 3766 |
* |
| 3767 |
* Results: |
| 3768 |
* A boolean value. |
| 3769 |
* |
| 3770 |
* Side effects: |
| 3771 |
* None. |
| 3772 |
* |
| 3773 |
*---------------------------------------------------------------------- |
| 3774 |
*/ |
| 3775 |
|
| 3776 |
int |
| 3777 |
Tcl_LimitTypeEnabled( |
| 3778 |
Tcl_Interp *interp, |
| 3779 |
int type) |
| 3780 |
{ |
| 3781 |
Interp *iPtr = (Interp *) interp; |
| 3782 |
|
| 3783 |
return (iPtr->limit.active & type) != 0; |
| 3784 |
} |
| 3785 |
|
| 3786 |
/* |
| 3787 |
*---------------------------------------------------------------------- |
| 3788 |
* |
| 3789 |
* Tcl_LimitTypeExceeded -- |
| 3790 |
* |
| 3791 |
* Check whether a particular limit has been exceeded for an interpreter. |
| 3792 |
* |
| 3793 |
* Results: |
| 3794 |
* A boolean value (note that Tcl_LimitExceeded will always return |
| 3795 |
* non-zero when this function returns non-zero). |
| 3796 |
* |
| 3797 |
* Side effects: |
| 3798 |
* None. |
| 3799 |
* |
| 3800 |
*---------------------------------------------------------------------- |
| 3801 |
*/ |
| 3802 |
|
| 3803 |
int |
| 3804 |
Tcl_LimitTypeExceeded( |
| 3805 |
Tcl_Interp *interp, |
| 3806 |
int type) |
| 3807 |
{ |
| 3808 |
Interp *iPtr = (Interp *) interp; |
| 3809 |
|
| 3810 |
return (iPtr->limit.exceeded & type) != 0; |
| 3811 |
} |
| 3812 |
|
| 3813 |
/* |
| 3814 |
*---------------------------------------------------------------------- |
| 3815 |
* |
| 3816 |
* Tcl_LimitTypeSet -- |
| 3817 |
* |
| 3818 |
* Enable a particular limit for an interpreter. |
| 3819 |
* |
| 3820 |
* Results: |
| 3821 |
* None. |
| 3822 |
* |
| 3823 |
* Side effects: |
| 3824 |
* The limit is turned on and will be checked in future at an interval |
| 3825 |
* determined by the frequency of calling of Tcl_LimitReady and the |
| 3826 |
* granularity of the limit in question. |
| 3827 |
* |
| 3828 |
*---------------------------------------------------------------------- |
| 3829 |
*/ |
| 3830 |
|
| 3831 |
void |
| 3832 |
Tcl_LimitTypeSet( |
| 3833 |
Tcl_Interp *interp, |
| 3834 |
int type) |
| 3835 |
{ |
| 3836 |
Interp *iPtr = (Interp *) interp; |
| 3837 |
|
| 3838 |
iPtr->limit.active |= type; |
| 3839 |
} |
| 3840 |
|
| 3841 |
/* |
| 3842 |
*---------------------------------------------------------------------- |
| 3843 |
* |
| 3844 |
* Tcl_LimitTypeReset -- |
| 3845 |
* |
| 3846 |
* Disable a particular limit for an interpreter. |
| 3847 |
* |
| 3848 |
* Results: |
| 3849 |
* None. |
| 3850 |
* |
| 3851 |
* Side effects: |
| 3852 |
* The limit is disabled. If the limit was exceeded when this function |
| 3853 |
* was called, the limit will no longer be exceeded afterwards and the |
| 3854 |
* interpreter will be free to execute further scripts (assuming it isn't |
| 3855 |
* also deleted, of course). |
| 3856 |
* |
| 3857 |
*---------------------------------------------------------------------- |
| 3858 |
*/ |
| 3859 |
|
| 3860 |
void |
| 3861 |
Tcl_LimitTypeReset( |
| 3862 |
Tcl_Interp *interp, |
| 3863 |
int type) |
| 3864 |
{ |
| 3865 |
Interp *iPtr = (Interp *) interp; |
| 3866 |
|
| 3867 |
iPtr->limit.active &= ~type; |
| 3868 |
iPtr->limit.exceeded &= ~type; |
| 3869 |
} |
| 3870 |
|
| 3871 |
/* |
| 3872 |
*---------------------------------------------------------------------- |
| 3873 |
* |
| 3874 |
* Tcl_LimitSetCommands -- |
| 3875 |
* |
| 3876 |
* Set the command limit for an interpreter. |
| 3877 |
* |
| 3878 |
* Results: |
| 3879 |
* None. |
| 3880 |
* |
| 3881 |
* Side effects: |
| 3882 |
* Also resets whether the command limit was exceeded. This might permit |
| 3883 |
* a small amount of further execution in the interpreter even if the |
| 3884 |
* limit itself is theoretically exceeded. |
| 3885 |
* |
| 3886 |
*---------------------------------------------------------------------- |
| 3887 |
*/ |
| 3888 |
|
| 3889 |
void |
| 3890 |
Tcl_LimitSetCommands( |
| 3891 |
Tcl_Interp *interp, |
| 3892 |
int commandLimit) |
| 3893 |
{ |
| 3894 |
Interp *iPtr = (Interp *) interp; |
| 3895 |
|
| 3896 |
iPtr->limit.cmdCount = commandLimit; |
| 3897 |
iPtr->limit.exceeded &= ~TCL_LIMIT_COMMANDS; |
| 3898 |
} |
| 3899 |
|
| 3900 |
/* |
| 3901 |
*---------------------------------------------------------------------- |
| 3902 |
* |
| 3903 |
* Tcl_LimitGetCommands -- |
| 3904 |
* |
| 3905 |
* Get the number of commands that may be executed in the interpreter |
| 3906 |
* before the command-limit is reached. |
| 3907 |
* |
| 3908 |
* Results: |
| 3909 |
* An upper bound on the number of commands. |
| 3910 |
* |
| 3911 |
* Side effects: |
| 3912 |
* None. |
| 3913 |
* |
| 3914 |
*---------------------------------------------------------------------- |
| 3915 |
*/ |
| 3916 |
|
| 3917 |
int |
| 3918 |
Tcl_LimitGetCommands( |
| 3919 |
Tcl_Interp *interp) |
| 3920 |
{ |
| 3921 |
Interp *iPtr = (Interp *) interp; |
| 3922 |
|
| 3923 |
return iPtr->limit.cmdCount; |
| 3924 |
} |
| 3925 |
|
| 3926 |
/* |
| 3927 |
*---------------------------------------------------------------------- |
| 3928 |
* |
| 3929 |
* Tcl_LimitSetTime -- |
| 3930 |
* |
| 3931 |
* Set the time limit for an interpreter by copying it from the value |
| 3932 |
* pointed to by the timeLimitPtr argument. |
| 3933 |
* |
| 3934 |
* Results: |
| 3935 |
* None. |
| 3936 |
* |
| 3937 |
* Side effects: |
| 3938 |
* Also resets whether the time limit was exceeded. This might permit a |
| 3939 |
* small amount of further execution in the interpreter even if the limit |
| 3940 |
* itself is theoretically exceeded. |
| 3941 |
* |
| 3942 |
*---------------------------------------------------------------------- |
| 3943 |
*/ |
| 3944 |
|
| 3945 |
void |
| 3946 |
Tcl_LimitSetTime( |
| 3947 |
Tcl_Interp *interp, |
| 3948 |
Tcl_Time *timeLimitPtr) |
| 3949 |
{ |
| 3950 |
Interp *iPtr = (Interp *) interp; |
| 3951 |
Tcl_Time nextMoment; |
| 3952 |
|
| 3953 |
memcpy(&iPtr->limit.time, timeLimitPtr, sizeof(Tcl_Time)); |
| 3954 |
if (iPtr->limit.timeEvent != NULL) { |
| 3955 |
Tcl_DeleteTimerHandler(iPtr->limit.timeEvent); |
| 3956 |
} |
| 3957 |
nextMoment.sec = timeLimitPtr->sec; |
| 3958 |
nextMoment.usec = timeLimitPtr->usec+10; |
| 3959 |
if (nextMoment.usec >= 1000000) { |
| 3960 |
nextMoment.sec++; |
| 3961 |
nextMoment.usec -= 1000000; |
| 3962 |
} |
| 3963 |
iPtr->limit.timeEvent = TclCreateAbsoluteTimerHandler(&nextMoment, |
| 3964 |
TimeLimitCallback, interp); |
| 3965 |
iPtr->limit.exceeded &= ~TCL_LIMIT_TIME; |
| 3966 |
} |
| 3967 |
|
| 3968 |
/* |
| 3969 |
*---------------------------------------------------------------------- |
| 3970 |
* |
| 3971 |
* TimeLimitCallback -- |
| 3972 |
* |
| 3973 |
* Callback that allows time limits to be enforced even when doing a |
| 3974 |
* blocking wait for events. |
| 3975 |
* |
| 3976 |
* Results: |
| 3977 |
* None. |
| 3978 |
* |
| 3979 |
* Side effects: |
| 3980 |
* May put the interpreter into a state where it can no longer execute |
| 3981 |
* commands. May make callbacks into other interpreters. |
| 3982 |
* |
| 3983 |
*---------------------------------------------------------------------- |
| 3984 |
*/ |
| 3985 |
|
| 3986 |
static void |
| 3987 |
TimeLimitCallback( |
| 3988 |
ClientData clientData) |
| 3989 |
{ |
| 3990 |
Tcl_Interp *interp = (Tcl_Interp *)clientData; |
| 3991 |
Interp *iPtr = (Interp *)clientData; |
| 3992 |
int code; |
| 3993 |
|
| 3994 |
Tcl_Preserve(interp); |
| 3995 |
iPtr->limit.timeEvent = NULL; |
| 3996 |
|
| 3997 |
/* |
| 3998 |
* Must reset the granularity ticker here to force an immediate full |
| 3999 |
* check. This is OK because we're swallowing the cost in the overall cost |
| 4000 |
* of the event loop. [Bug 2891362] |
| 4001 |
*/ |
| 4002 |
|
| 4003 |
iPtr->limit.granularityTicker = 0; |
| 4004 |
|
| 4005 |
code = Tcl_LimitCheck(interp); |
| 4006 |
if (code != TCL_OK) { |
| 4007 |
Tcl_AddErrorInfo(interp, "\n (while waiting for event)"); |
| 4008 |
Tcl_BackgroundException(interp, code); |
| 4009 |
} |
| 4010 |
Tcl_Release(interp); |
| 4011 |
} |
| 4012 |
|
| 4013 |
/* |
| 4014 |
*---------------------------------------------------------------------- |
| 4015 |
* |
| 4016 |
* Tcl_LimitGetTime -- |
| 4017 |
* |
| 4018 |
* Get the current time limit. |
| 4019 |
* |
| 4020 |
* Results: |
| 4021 |
* The time limit (by it being copied into the variable pointed to by the |
| 4022 |
* timeLimitPtr). |
| 4023 |
* |
| 4024 |
* Side effects: |
| 4025 |
* None. |
| 4026 |
* |
| 4027 |
*---------------------------------------------------------------------- |
| 4028 |
*/ |
| 4029 |
|
| 4030 |
void |
| 4031 |
Tcl_LimitGetTime( |
| 4032 |
Tcl_Interp *interp, |
| 4033 |
Tcl_Time *timeLimitPtr) |
| 4034 |
{ |
| 4035 |
Interp *iPtr = (Interp *) interp; |
| 4036 |
|
| 4037 |
memcpy(timeLimitPtr, &iPtr->limit.time, sizeof(Tcl_Time)); |
| 4038 |
} |
| 4039 |
|
| 4040 |
/* |
| 4041 |
*---------------------------------------------------------------------- |
| 4042 |
* |
| 4043 |
* Tcl_LimitSetGranularity -- |
| 4044 |
* |
| 4045 |
* Set the granularity divisor (which must be positive) for a particular |
| 4046 |
* limit. |
| 4047 |
* |
| 4048 |
* Results: |
| 4049 |
* None. |
| 4050 |
* |
| 4051 |
* Side effects: |
| 4052 |
* The granularity is updated. |
| 4053 |
* |
| 4054 |
*---------------------------------------------------------------------- |
| 4055 |
*/ |
| 4056 |
|
| 4057 |
void |
| 4058 |
Tcl_LimitSetGranularity( |
| 4059 |
Tcl_Interp *interp, |
| 4060 |
int type, |
| 4061 |
int granularity) |
| 4062 |
{ |
| 4063 |
Interp *iPtr = (Interp *) interp; |
| 4064 |
if (granularity < 1) { |
| 4065 |
Tcl_Panic("limit granularity must be positive"); |
| 4066 |
} |
| 4067 |
|
| 4068 |
switch (type) { |
| 4069 |
case TCL_LIMIT_COMMANDS: |
| 4070 |
iPtr->limit.cmdGranularity = granularity; |
| 4071 |
return; |
| 4072 |
case TCL_LIMIT_TIME: |
| 4073 |
iPtr->limit.timeGranularity = granularity; |
| 4074 |
return; |
| 4075 |
} |
| 4076 |
Tcl_Panic("unknown type of resource limit"); |
| 4077 |
} |
| 4078 |
|
| 4079 |
/* |
| 4080 |
*---------------------------------------------------------------------- |
| 4081 |
* |
| 4082 |
* Tcl_LimitGetGranularity -- |
| 4083 |
* |
| 4084 |
* Get the granularity divisor for a particular limit. |
| 4085 |
* |
| 4086 |
* Results: |
| 4087 |
* The granularity divisor for the given limit. |
| 4088 |
* |
| 4089 |
* Side effects: |
| 4090 |
* None. |
| 4091 |
* |
| 4092 |
*---------------------------------------------------------------------- |
| 4093 |
*/ |
| 4094 |
|
| 4095 |
int |
| 4096 |
Tcl_LimitGetGranularity( |
| 4097 |
Tcl_Interp *interp, |
| 4098 |
int type) |
| 4099 |
{ |
| 4100 |
Interp *iPtr = (Interp *) interp; |
| 4101 |
|
| 4102 |
switch (type) { |
| 4103 |
case TCL_LIMIT_COMMANDS: |
| 4104 |
return iPtr->limit.cmdGranularity; |
| 4105 |
case TCL_LIMIT_TIME: |
| 4106 |
return iPtr->limit.timeGranularity; |
| 4107 |
} |
| 4108 |
Tcl_Panic("unknown type of resource limit"); |
| 4109 |
return -1; /* NOT REACHED */ |
| 4110 |
} |
| 4111 |
|
| 4112 |
/* |
| 4113 |
*---------------------------------------------------------------------- |
| 4114 |
* |
| 4115 |
* DeleteScriptLimitCallback -- |
| 4116 |
* |
| 4117 |
* Callback for when a script limit (a limit callback implemented as a |
| 4118 |
* Tcl script in a parent interpreter, as set up from Tcl) is deleted. |
| 4119 |
* |
| 4120 |
* Results: |
| 4121 |
* None. |
| 4122 |
* |
| 4123 |
* Side effects: |
| 4124 |
* The reference to the script callback from the controlling interpreter |
| 4125 |
* is removed. |
| 4126 |
* |
| 4127 |
*---------------------------------------------------------------------- |
| 4128 |
*/ |
| 4129 |
|
| 4130 |
static void |
| 4131 |
DeleteScriptLimitCallback( |
| 4132 |
ClientData clientData) |
| 4133 |
{ |
| 4134 |
ScriptLimitCallback *limitCBPtr = (ScriptLimitCallback *)clientData; |
| 4135 |
|
| 4136 |
Tcl_DecrRefCount(limitCBPtr->scriptObj); |
| 4137 |
if (limitCBPtr->entryPtr != NULL) { |
| 4138 |
Tcl_DeleteHashEntry(limitCBPtr->entryPtr); |
| 4139 |
} |
| 4140 |
ckfree(limitCBPtr); |
| 4141 |
} |
| 4142 |
|
| 4143 |
/* |
| 4144 |
*---------------------------------------------------------------------- |
| 4145 |
* |
| 4146 |
* CallScriptLimitCallback -- |
| 4147 |
* |
| 4148 |
* Invoke a script limit callback. Used to implement limit callbacks set |
| 4149 |
* at the Tcl level on child interpreters. |
| 4150 |
* |
| 4151 |
* Results: |
| 4152 |
* None. |
| 4153 |
* |
| 4154 |
* Side effects: |
| 4155 |
* Depends on the callback script. Errors are reported as background |
| 4156 |
* errors. |
| 4157 |
* |
| 4158 |
*---------------------------------------------------------------------- |
| 4159 |
*/ |
| 4160 |
|
| 4161 |
static void |
| 4162 |
CallScriptLimitCallback( |
| 4163 |
ClientData clientData, |
| 4164 |
Tcl_Interp *interp) /* Interpreter which failed the limit */ |
| 4165 |
{ |
| 4166 |
ScriptLimitCallback *limitCBPtr = (ScriptLimitCallback *)clientData; |
| 4167 |
int code; |
| 4168 |
|
| 4169 |
if (Tcl_InterpDeleted(limitCBPtr->interp)) { |
| 4170 |
return; |
| 4171 |
} |
| 4172 |
Tcl_Preserve(limitCBPtr->interp); |
| 4173 |
code = Tcl_EvalObjEx(limitCBPtr->interp, limitCBPtr->scriptObj, |
| 4174 |
TCL_EVAL_GLOBAL); |
| 4175 |
if (code != TCL_OK && !Tcl_InterpDeleted(limitCBPtr->interp)) { |
| 4176 |
Tcl_BackgroundException(limitCBPtr->interp, code); |
| 4177 |
} |
| 4178 |
Tcl_Release(limitCBPtr->interp); |
| 4179 |
} |
| 4180 |
|
| 4181 |
/* |
| 4182 |
*---------------------------------------------------------------------- |
| 4183 |
* |
| 4184 |
* SetScriptLimitCallback -- |
| 4185 |
* |
| 4186 |
* Install (or remove, if scriptObj is NULL) a limit callback script that |
| 4187 |
* is called when the target interpreter exceeds the type of limit |
| 4188 |
* specified. Each interpreter may only have one callback set on another |
| 4189 |
* interpreter through this mechanism (though as many interpreters may be |
| 4190 |
* limited as the programmer chooses overall). |
| 4191 |
* |
| 4192 |
* Results: |
| 4193 |
* None. |
| 4194 |
* |
| 4195 |
* Side effects: |
| 4196 |
* A limit callback implemented as an invocation of a Tcl script in |
| 4197 |
* another interpreter is either installed or removed. |
| 4198 |
* |
| 4199 |
*---------------------------------------------------------------------- |
| 4200 |
*/ |
| 4201 |
|
| 4202 |
static void |
| 4203 |
SetScriptLimitCallback( |
| 4204 |
Tcl_Interp *interp, |
| 4205 |
int type, |
| 4206 |
Tcl_Interp *targetInterp, |
| 4207 |
Tcl_Obj *scriptObj) |
| 4208 |
{ |
| 4209 |
ScriptLimitCallback *limitCBPtr; |
| 4210 |
Tcl_HashEntry *hashPtr; |
| 4211 |
int isNew; |
| 4212 |
ScriptLimitCallbackKey key; |
| 4213 |
Interp *iPtr = (Interp *) interp; |
| 4214 |
|
| 4215 |
if (interp == targetInterp) { |
| 4216 |
Tcl_Panic("installing limit callback to the limited interpreter"); |
| 4217 |
} |
| 4218 |
|
| 4219 |
key.interp = targetInterp; |
| 4220 |
key.type = type; |
| 4221 |
|
| 4222 |
if (scriptObj == NULL) { |
| 4223 |
hashPtr = Tcl_FindHashEntry(&iPtr->limit.callbacks, (char *) &key); |
| 4224 |
if (hashPtr != NULL) { |
| 4225 |
Tcl_LimitRemoveHandler(targetInterp, type, CallScriptLimitCallback, |
| 4226 |
Tcl_GetHashValue(hashPtr)); |
| 4227 |
} |
| 4228 |
return; |
| 4229 |
} |
| 4230 |
|
| 4231 |
hashPtr = Tcl_CreateHashEntry(&iPtr->limit.callbacks, &key, |
| 4232 |
&isNew); |
| 4233 |
if (!isNew) { |
| 4234 |
limitCBPtr = (ScriptLimitCallback *)Tcl_GetHashValue(hashPtr); |
| 4235 |
limitCBPtr->entryPtr = NULL; |
| 4236 |
Tcl_LimitRemoveHandler(targetInterp, type, CallScriptLimitCallback, |
| 4237 |
limitCBPtr); |
| 4238 |
} |
| 4239 |
|
| 4240 |
limitCBPtr = (ScriptLimitCallback *)ckalloc(sizeof(ScriptLimitCallback)); |
| 4241 |
limitCBPtr->interp = interp; |
| 4242 |
limitCBPtr->scriptObj = scriptObj; |
| 4243 |
limitCBPtr->entryPtr = hashPtr; |
| 4244 |
limitCBPtr->type = type; |
| 4245 |
Tcl_IncrRefCount(scriptObj); |
| 4246 |
|
| 4247 |
Tcl_LimitAddHandler(targetInterp, type, CallScriptLimitCallback, |
| 4248 |
limitCBPtr, DeleteScriptLimitCallback); |
| 4249 |
Tcl_SetHashValue(hashPtr, limitCBPtr); |
| 4250 |
} |
| 4251 |
|
| 4252 |
/* |
| 4253 |
*---------------------------------------------------------------------- |
| 4254 |
* |
| 4255 |
* TclRemoveScriptLimitCallbacks -- |
| 4256 |
* |
| 4257 |
* Remove all script-implemented limit callbacks that make calls back |
| 4258 |
* into the given interpreter. This invoked as part of deleting an |
| 4259 |
* interpreter. |
| 4260 |
* |
| 4261 |
* Results: |
| 4262 |
* None. |
| 4263 |
* |
| 4264 |
* Side effects: |
| 4265 |
* The script limit callbacks are removed or marked for later removal. |
| 4266 |
* |
| 4267 |
*---------------------------------------------------------------------- |
| 4268 |
*/ |
| 4269 |
|
| 4270 |
void |
| 4271 |
TclRemoveScriptLimitCallbacks( |
| 4272 |
Tcl_Interp *interp) |
| 4273 |
{ |
| 4274 |
Interp *iPtr = (Interp *) interp; |
| 4275 |
Tcl_HashEntry *hashPtr; |
| 4276 |
Tcl_HashSearch search; |
| 4277 |
ScriptLimitCallbackKey *keyPtr; |
| 4278 |
|
| 4279 |
hashPtr = Tcl_FirstHashEntry(&iPtr->limit.callbacks, &search); |
| 4280 |
while (hashPtr != NULL) { |
| 4281 |
keyPtr = (ScriptLimitCallbackKey *) |
| 4282 |
Tcl_GetHashKey(&iPtr->limit.callbacks, hashPtr); |
| 4283 |
Tcl_LimitRemoveHandler(keyPtr->interp, keyPtr->type, |
| 4284 |
CallScriptLimitCallback, Tcl_GetHashValue(hashPtr)); |
| 4285 |
hashPtr = Tcl_NextHashEntry(&search); |
| 4286 |
} |
| 4287 |
Tcl_DeleteHashTable(&iPtr->limit.callbacks); |
| 4288 |
} |
| 4289 |
|
| 4290 |
/* |
| 4291 |
*---------------------------------------------------------------------- |
| 4292 |
* |
| 4293 |
* TclInitLimitSupport -- |
| 4294 |
* |
| 4295 |
* Initialise all the parts of the interpreter relating to resource limit |
| 4296 |
* management. This allows an interpreter to both have limits set upon |
| 4297 |
* itself and set limits upon other interpreters. |
| 4298 |
* |
| 4299 |
* Results: |
| 4300 |
* None. |
| 4301 |
* |
| 4302 |
* Side effects: |
| 4303 |
* The resource limit subsystem is initialised for the interpreter. |
| 4304 |
* |
| 4305 |
*---------------------------------------------------------------------- |
| 4306 |
*/ |
| 4307 |
|
| 4308 |
void |
| 4309 |
TclInitLimitSupport( |
| 4310 |
Tcl_Interp *interp) |
| 4311 |
{ |
| 4312 |
Interp *iPtr = (Interp *) interp; |
| 4313 |
|
| 4314 |
iPtr->limit.active = 0; |
| 4315 |
iPtr->limit.granularityTicker = 0; |
| 4316 |
iPtr->limit.exceeded = 0; |
| 4317 |
iPtr->limit.cmdCount = 0; |
| 4318 |
iPtr->limit.cmdHandlers = NULL; |
| 4319 |
iPtr->limit.cmdGranularity = 1; |
| 4320 |
memset(&iPtr->limit.time, 0, sizeof(Tcl_Time)); |
| 4321 |
iPtr->limit.timeHandlers = NULL; |
| 4322 |
iPtr->limit.timeEvent = NULL; |
| 4323 |
iPtr->limit.timeGranularity = 10; |
| 4324 |
/* See [f7495f63c0]: Don't use sizeof(ScriptLimitCallbackKey) here, because it |
| 4325 |
* counts the end padding as well */ |
| 4326 |
Tcl_InitHashTable(&iPtr->limit.callbacks, |
| 4327 |
TclOffset(ScriptLimitCallbackKey, type)/sizeof(int) + 1); |
| 4328 |
} |
| 4329 |
|
| 4330 |
/* |
| 4331 |
*---------------------------------------------------------------------- |
| 4332 |
* |
| 4333 |
* InheritLimitsFromParent -- |
| 4334 |
* |
| 4335 |
* Derive the interpreter limit configuration for a child interpreter |
| 4336 |
* from the limit config for the parent. |
| 4337 |
* |
| 4338 |
* Results: |
| 4339 |
* None. |
| 4340 |
* |
| 4341 |
* Side effects: |
| 4342 |
* The child interpreter limits are set so that if the parent has a |
| 4343 |
* limit, it may not exceed it by handing off work to child interpreters. |
| 4344 |
* Note that this does not transfer limit callbacks from the parent to |
| 4345 |
* the child. |
| 4346 |
* |
| 4347 |
*---------------------------------------------------------------------- |
| 4348 |
*/ |
| 4349 |
|
| 4350 |
static void |
| 4351 |
InheritLimitsFromParent( |
| 4352 |
Tcl_Interp *childInterp, |
| 4353 |
Tcl_Interp *parentInterp) |
| 4354 |
{ |
| 4355 |
Interp *childPtr = (Interp *) childInterp; |
| 4356 |
Interp *parentPtr = (Interp *) parentInterp; |
| 4357 |
|
| 4358 |
if (parentPtr->limit.active & TCL_LIMIT_COMMANDS) { |
| 4359 |
childPtr->limit.active |= TCL_LIMIT_COMMANDS; |
| 4360 |
childPtr->limit.cmdCount = 0; |
| 4361 |
childPtr->limit.cmdGranularity = parentPtr->limit.cmdGranularity; |
| 4362 |
} |
| 4363 |
if (parentPtr->limit.active & TCL_LIMIT_TIME) { |
| 4364 |
childPtr->limit.active |= TCL_LIMIT_TIME; |
| 4365 |
memcpy(&childPtr->limit.time, &parentPtr->limit.time, |
| 4366 |
sizeof(Tcl_Time)); |
| 4367 |
childPtr->limit.timeGranularity = parentPtr->limit.timeGranularity; |
| 4368 |
} |
| 4369 |
} |
| 4370 |
|
| 4371 |
/* |
| 4372 |
*---------------------------------------------------------------------- |
| 4373 |
* |
| 4374 |
* ChildCommandLimitCmd -- |
| 4375 |
* |
| 4376 |
* Implementation of the [interp limit $i commands] and [$i limit |
| 4377 |
* commands] subcommands. See the interp manual page for a full |
| 4378 |
* description. |
| 4379 |
* |
| 4380 |
* Results: |
| 4381 |
* A standard Tcl result. |
| 4382 |
* |
| 4383 |
* Side effects: |
| 4384 |
* Depends on the arguments. |
| 4385 |
* |
| 4386 |
*---------------------------------------------------------------------- |
| 4387 |
*/ |
| 4388 |
|
| 4389 |
static int |
| 4390 |
ChildCommandLimitCmd( |
| 4391 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 4392 |
Tcl_Interp *childInterp, /* Interpreter being adjusted. */ |
| 4393 |
int consumedObjc, /* Number of args already parsed. */ |
| 4394 |
int objc, /* Total number of arguments. */ |
| 4395 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 4396 |
{ |
| 4397 |
static const char *const options[] = { |
| 4398 |
"-command", "-granularity", "-value", NULL |
| 4399 |
}; |
| 4400 |
enum Options { |
| 4401 |
OPT_CMD, OPT_GRAN, OPT_VAL |
| 4402 |
}; |
| 4403 |
Interp *iPtr = (Interp *) interp; |
| 4404 |
int index; |
| 4405 |
ScriptLimitCallbackKey key; |
| 4406 |
ScriptLimitCallback *limitCBPtr; |
| 4407 |
Tcl_HashEntry *hPtr; |
| 4408 |
|
| 4409 |
/* |
| 4410 |
* First, ensure that we are not reading or writing the calling |
| 4411 |
* interpreter's limits; it may only manipulate its children. Note that |
| 4412 |
* the low level API enforces this with Tcl_Panic, which we want to |
| 4413 |
* avoid. [Bug 3398794] |
| 4414 |
*/ |
| 4415 |
|
| 4416 |
if (interp == childInterp) { |
| 4417 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4418 |
"limits on current interpreter inaccessible", -1)); |
| 4419 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "SELF", (char *)NULL); |
| 4420 |
return TCL_ERROR; |
| 4421 |
} |
| 4422 |
|
| 4423 |
if (objc == consumedObjc) { |
| 4424 |
Tcl_Obj *dictPtr; |
| 4425 |
|
| 4426 |
TclNewObj(dictPtr); |
| 4427 |
key.interp = childInterp; |
| 4428 |
key.type = TCL_LIMIT_COMMANDS; |
| 4429 |
hPtr = Tcl_FindHashEntry(&iPtr->limit.callbacks, (char *) &key); |
| 4430 |
if (hPtr != NULL) { |
| 4431 |
limitCBPtr = (ScriptLimitCallback *)Tcl_GetHashValue(hPtr); |
| 4432 |
if (limitCBPtr != NULL && limitCBPtr->scriptObj != NULL) { |
| 4433 |
TclDictPut(NULL, dictPtr, options[0], limitCBPtr->scriptObj); |
| 4434 |
} else { |
| 4435 |
goto putEmptyCommandInDict; |
| 4436 |
} |
| 4437 |
} else { |
| 4438 |
Tcl_Obj *empty; |
| 4439 |
|
| 4440 |
putEmptyCommandInDict: |
| 4441 |
TclNewObj(empty); |
| 4442 |
TclDictPut(NULL, dictPtr, options[0], empty); |
| 4443 |
} |
| 4444 |
TclDictPut(NULL, dictPtr, options[1], Tcl_NewIntObj( |
| 4445 |
Tcl_LimitGetGranularity(childInterp, TCL_LIMIT_COMMANDS))); |
| 4446 |
|
| 4447 |
if (Tcl_LimitTypeEnabled(childInterp, TCL_LIMIT_COMMANDS)) { |
| 4448 |
TclDictPut(NULL, dictPtr, options[2], Tcl_NewIntObj( |
| 4449 |
Tcl_LimitGetCommands(childInterp))); |
| 4450 |
} else { |
| 4451 |
Tcl_Obj *empty; |
| 4452 |
|
| 4453 |
TclNewObj(empty); |
| 4454 |
TclDictPut(NULL, dictPtr, options[2], empty); |
| 4455 |
} |
| 4456 |
Tcl_SetObjResult(interp, dictPtr); |
| 4457 |
return TCL_OK; |
| 4458 |
} else if (objc == consumedObjc+1) { |
| 4459 |
if (Tcl_GetIndexFromObj(interp, objv[consumedObjc], options, "option", |
| 4460 |
0, &index) != TCL_OK) { |
| 4461 |
return TCL_ERROR; |
| 4462 |
} |
| 4463 |
switch ((enum Options) index) { |
| 4464 |
case OPT_CMD: |
| 4465 |
key.interp = childInterp; |
| 4466 |
key.type = TCL_LIMIT_COMMANDS; |
| 4467 |
hPtr = Tcl_FindHashEntry(&iPtr->limit.callbacks, (char *) &key); |
| 4468 |
if (hPtr != NULL) { |
| 4469 |
limitCBPtr = (ScriptLimitCallback *)Tcl_GetHashValue(hPtr); |
| 4470 |
if (limitCBPtr != NULL && limitCBPtr->scriptObj != NULL) { |
| 4471 |
Tcl_SetObjResult(interp, limitCBPtr->scriptObj); |
| 4472 |
} |
| 4473 |
} |
| 4474 |
break; |
| 4475 |
case OPT_GRAN: |
| 4476 |
Tcl_SetObjResult(interp, Tcl_NewIntObj( |
| 4477 |
Tcl_LimitGetGranularity(childInterp, TCL_LIMIT_COMMANDS))); |
| 4478 |
break; |
| 4479 |
case OPT_VAL: |
| 4480 |
if (Tcl_LimitTypeEnabled(childInterp, TCL_LIMIT_COMMANDS)) { |
| 4481 |
Tcl_SetObjResult(interp, |
| 4482 |
Tcl_NewIntObj(Tcl_LimitGetCommands(childInterp))); |
| 4483 |
} |
| 4484 |
break; |
| 4485 |
} |
| 4486 |
return TCL_OK; |
| 4487 |
} else if ((objc-consumedObjc) & 1 /* isOdd(objc-consumedObjc) */) { |
| 4488 |
Tcl_WrongNumArgs(interp, consumedObjc, objv, "?-option value ...?"); |
| 4489 |
return TCL_ERROR; |
| 4490 |
} else { |
| 4491 |
int i, scriptLen = 0, limitLen = 0; |
| 4492 |
Tcl_Obj *scriptObj = NULL, *granObj = NULL, *limitObj = NULL; |
| 4493 |
int gran = 0, limit = 0; |
| 4494 |
|
| 4495 |
for (i=consumedObjc ; i<objc ; i+=2) { |
| 4496 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, |
| 4497 |
&index) != TCL_OK) { |
| 4498 |
return TCL_ERROR; |
| 4499 |
} |
| 4500 |
switch ((enum Options) index) { |
| 4501 |
case OPT_CMD: |
| 4502 |
scriptObj = objv[i+1]; |
| 4503 |
(void) TclGetStringFromObj(scriptObj, &scriptLen); |
| 4504 |
break; |
| 4505 |
case OPT_GRAN: |
| 4506 |
granObj = objv[i+1]; |
| 4507 |
if (TclGetIntFromObj(interp, objv[i+1], &gran) != TCL_OK) { |
| 4508 |
return TCL_ERROR; |
| 4509 |
} |
| 4510 |
if (gran < 1) { |
| 4511 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4512 |
"granularity must be at least 1", -1)); |
| 4513 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4514 |
"BADVALUE", (char *)NULL); |
| 4515 |
return TCL_ERROR; |
| 4516 |
} |
| 4517 |
break; |
| 4518 |
case OPT_VAL: |
| 4519 |
limitObj = objv[i+1]; |
| 4520 |
(void) TclGetStringFromObj(objv[i+1], &limitLen); |
| 4521 |
if (limitLen == 0) { |
| 4522 |
break; |
| 4523 |
} |
| 4524 |
if (TclGetIntFromObj(interp, objv[i+1], &limit) != TCL_OK) { |
| 4525 |
return TCL_ERROR; |
| 4526 |
} |
| 4527 |
if (limit < 0) { |
| 4528 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4529 |
"command limit value must be at least 0", -1)); |
| 4530 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4531 |
"BADVALUE", (char *)NULL); |
| 4532 |
return TCL_ERROR; |
| 4533 |
} |
| 4534 |
break; |
| 4535 |
} |
| 4536 |
} |
| 4537 |
if (scriptObj != NULL) { |
| 4538 |
SetScriptLimitCallback(interp, TCL_LIMIT_COMMANDS, childInterp, |
| 4539 |
(scriptLen > 0 ? scriptObj : NULL)); |
| 4540 |
} |
| 4541 |
if (granObj != NULL) { |
| 4542 |
Tcl_LimitSetGranularity(childInterp, TCL_LIMIT_COMMANDS, gran); |
| 4543 |
} |
| 4544 |
if (limitObj != NULL) { |
| 4545 |
if (limitLen > 0) { |
| 4546 |
Tcl_LimitSetCommands(childInterp, limit); |
| 4547 |
Tcl_LimitTypeSet(childInterp, TCL_LIMIT_COMMANDS); |
| 4548 |
} else { |
| 4549 |
Tcl_LimitTypeReset(childInterp, TCL_LIMIT_COMMANDS); |
| 4550 |
} |
| 4551 |
} |
| 4552 |
return TCL_OK; |
| 4553 |
} |
| 4554 |
} |
| 4555 |
|
| 4556 |
/* |
| 4557 |
*---------------------------------------------------------------------- |
| 4558 |
* |
| 4559 |
* ChildTimeLimitCmd -- |
| 4560 |
* |
| 4561 |
* Implementation of the [interp limit $i time] and [$i limit time] |
| 4562 |
* subcommands. See the interp manual page for a full description. |
| 4563 |
* |
| 4564 |
* Results: |
| 4565 |
* A standard Tcl result. |
| 4566 |
* |
| 4567 |
* Side effects: |
| 4568 |
* Depends on the arguments. |
| 4569 |
* |
| 4570 |
*---------------------------------------------------------------------- |
| 4571 |
*/ |
| 4572 |
|
| 4573 |
static int |
| 4574 |
ChildTimeLimitCmd( |
| 4575 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 4576 |
Tcl_Interp *childInterp, /* Interpreter being adjusted. */ |
| 4577 |
int consumedObjc, /* Number of args already parsed. */ |
| 4578 |
int objc, /* Total number of arguments. */ |
| 4579 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 4580 |
{ |
| 4581 |
static const char *const options[] = { |
| 4582 |
"-command", "-granularity", "-milliseconds", "-seconds", NULL |
| 4583 |
}; |
| 4584 |
enum Options { |
| 4585 |
OPT_CMD, OPT_GRAN, OPT_MILLI, OPT_SEC |
| 4586 |
}; |
| 4587 |
Interp *iPtr = (Interp *) interp; |
| 4588 |
int index; |
| 4589 |
ScriptLimitCallbackKey key; |
| 4590 |
ScriptLimitCallback *limitCBPtr; |
| 4591 |
Tcl_HashEntry *hPtr; |
| 4592 |
|
| 4593 |
/* |
| 4594 |
* First, ensure that we are not reading or writing the calling |
| 4595 |
* interpreter's limits; it may only manipulate its children. Note that |
| 4596 |
* the low level API enforces this with Tcl_Panic, which we want to |
| 4597 |
* avoid. [Bug 3398794] |
| 4598 |
*/ |
| 4599 |
|
| 4600 |
if (interp == childInterp) { |
| 4601 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4602 |
"limits on current interpreter inaccessible", -1)); |
| 4603 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", "SELF", (char *)NULL); |
| 4604 |
return TCL_ERROR; |
| 4605 |
} |
| 4606 |
|
| 4607 |
if (objc == consumedObjc) { |
| 4608 |
Tcl_Obj *dictPtr; |
| 4609 |
|
| 4610 |
TclNewObj(dictPtr); |
| 4611 |
key.interp = childInterp; |
| 4612 |
key.type = TCL_LIMIT_TIME; |
| 4613 |
hPtr = Tcl_FindHashEntry(&iPtr->limit.callbacks, (char *) &key); |
| 4614 |
if (hPtr != NULL) { |
| 4615 |
limitCBPtr = (ScriptLimitCallback *)Tcl_GetHashValue(hPtr); |
| 4616 |
if (limitCBPtr != NULL && limitCBPtr->scriptObj != NULL) { |
| 4617 |
TclDictPut(NULL, dictPtr, options[0], limitCBPtr->scriptObj); |
| 4618 |
} else { |
| 4619 |
goto putEmptyCommandInDict; |
| 4620 |
} |
| 4621 |
} else { |
| 4622 |
Tcl_Obj *empty; |
| 4623 |
putEmptyCommandInDict: |
| 4624 |
TclNewObj(empty); |
| 4625 |
TclDictPut(NULL, dictPtr, options[0], empty); |
| 4626 |
} |
| 4627 |
TclDictPut(NULL, dictPtr, options[1], Tcl_NewIntObj( |
| 4628 |
Tcl_LimitGetGranularity(childInterp, TCL_LIMIT_TIME))); |
| 4629 |
|
| 4630 |
if (Tcl_LimitTypeEnabled(childInterp, TCL_LIMIT_TIME)) { |
| 4631 |
Tcl_Time limitMoment; |
| 4632 |
|
| 4633 |
Tcl_LimitGetTime(childInterp, &limitMoment); |
| 4634 |
TclDictPut(NULL, dictPtr, options[2], |
| 4635 |
Tcl_NewLongObj(limitMoment.usec / 1000)); |
| 4636 |
TclDictPut(NULL, dictPtr, options[3], |
| 4637 |
Tcl_NewLongObj(limitMoment.sec)); |
| 4638 |
} else { |
| 4639 |
Tcl_Obj *empty; |
| 4640 |
|
| 4641 |
TclNewObj(empty); |
| 4642 |
TclDictPut(NULL, dictPtr, options[2], empty); |
| 4643 |
TclDictPut(NULL, dictPtr, options[3], empty); |
| 4644 |
} |
| 4645 |
Tcl_SetObjResult(interp, dictPtr); |
| 4646 |
return TCL_OK; |
| 4647 |
} else if (objc == consumedObjc+1) { |
| 4648 |
if (Tcl_GetIndexFromObj(interp, objv[consumedObjc], options, "option", |
| 4649 |
0, &index) != TCL_OK) { |
| 4650 |
return TCL_ERROR; |
| 4651 |
} |
| 4652 |
switch ((enum Options) index) { |
| 4653 |
case OPT_CMD: |
| 4654 |
key.interp = childInterp; |
| 4655 |
key.type = TCL_LIMIT_TIME; |
| 4656 |
hPtr = Tcl_FindHashEntry(&iPtr->limit.callbacks, (char *) &key); |
| 4657 |
if (hPtr != NULL) { |
| 4658 |
limitCBPtr = (ScriptLimitCallback *)Tcl_GetHashValue(hPtr); |
| 4659 |
if (limitCBPtr != NULL && limitCBPtr->scriptObj != NULL) { |
| 4660 |
Tcl_SetObjResult(interp, limitCBPtr->scriptObj); |
| 4661 |
} |
| 4662 |
} |
| 4663 |
break; |
| 4664 |
case OPT_GRAN: |
| 4665 |
Tcl_SetObjResult(interp, Tcl_NewIntObj( |
| 4666 |
Tcl_LimitGetGranularity(childInterp, TCL_LIMIT_TIME))); |
| 4667 |
break; |
| 4668 |
case OPT_MILLI: |
| 4669 |
if (Tcl_LimitTypeEnabled(childInterp, TCL_LIMIT_TIME)) { |
| 4670 |
Tcl_Time limitMoment; |
| 4671 |
|
| 4672 |
Tcl_LimitGetTime(childInterp, &limitMoment); |
| 4673 |
Tcl_SetObjResult(interp, |
| 4674 |
Tcl_NewLongObj(limitMoment.usec/1000)); |
| 4675 |
} |
| 4676 |
break; |
| 4677 |
case OPT_SEC: |
| 4678 |
if (Tcl_LimitTypeEnabled(childInterp, TCL_LIMIT_TIME)) { |
| 4679 |
Tcl_Time limitMoment; |
| 4680 |
|
| 4681 |
Tcl_LimitGetTime(childInterp, &limitMoment); |
| 4682 |
Tcl_SetObjResult(interp, Tcl_NewWideIntObj(limitMoment.sec)); |
| 4683 |
} |
| 4684 |
break; |
| 4685 |
} |
| 4686 |
return TCL_OK; |
| 4687 |
} else if ((objc-consumedObjc) & 1 /* isOdd(objc-consumedObjc) */) { |
| 4688 |
Tcl_WrongNumArgs(interp, consumedObjc, objv, "?-option value ...?"); |
| 4689 |
return TCL_ERROR; |
| 4690 |
} else { |
| 4691 |
int i, scriptLen = 0, milliLen = 0, secLen = 0; |
| 4692 |
Tcl_Obj *scriptObj = NULL, *granObj = NULL; |
| 4693 |
Tcl_Obj *milliObj = NULL, *secObj = NULL; |
| 4694 |
int gran = 0; |
| 4695 |
Tcl_Time limitMoment; |
| 4696 |
int tmp; |
| 4697 |
|
| 4698 |
Tcl_LimitGetTime(childInterp, &limitMoment); |
| 4699 |
for (i=consumedObjc ; i<objc ; i+=2) { |
| 4700 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, |
| 4701 |
&index) != TCL_OK) { |
| 4702 |
return TCL_ERROR; |
| 4703 |
} |
| 4704 |
switch ((enum Options) index) { |
| 4705 |
case OPT_CMD: |
| 4706 |
scriptObj = objv[i+1]; |
| 4707 |
(void) TclGetStringFromObj(objv[i+1], &scriptLen); |
| 4708 |
break; |
| 4709 |
case OPT_GRAN: |
| 4710 |
granObj = objv[i+1]; |
| 4711 |
if (TclGetIntFromObj(interp, objv[i+1], &gran) != TCL_OK) { |
| 4712 |
return TCL_ERROR; |
| 4713 |
} |
| 4714 |
if (gran < 1) { |
| 4715 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4716 |
"granularity must be at least 1", -1)); |
| 4717 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4718 |
"BADVALUE", (char *)NULL); |
| 4719 |
return TCL_ERROR; |
| 4720 |
} |
| 4721 |
break; |
| 4722 |
case OPT_MILLI: |
| 4723 |
milliObj = objv[i+1]; |
| 4724 |
(void) TclGetStringFromObj(objv[i+1], &milliLen); |
| 4725 |
if (milliLen == 0) { |
| 4726 |
break; |
| 4727 |
} |
| 4728 |
if (TclGetIntFromObj(interp, objv[i+1], &tmp) != TCL_OK) { |
| 4729 |
return TCL_ERROR; |
| 4730 |
} |
| 4731 |
if (tmp < 0) { |
| 4732 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4733 |
"milliseconds must be at least 0", -1)); |
| 4734 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4735 |
"BADVALUE", (char *)NULL); |
| 4736 |
return TCL_ERROR; |
| 4737 |
} |
| 4738 |
limitMoment.usec = ((long) tmp)*1000; |
| 4739 |
break; |
| 4740 |
case OPT_SEC: { |
| 4741 |
Tcl_WideInt sec; |
| 4742 |
secObj = objv[i+1]; |
| 4743 |
(void) TclGetStringFromObj(objv[i+1], &secLen); |
| 4744 |
if (secLen == 0) { |
| 4745 |
break; |
| 4746 |
} |
| 4747 |
if (TclGetWideIntFromObj(interp, objv[i+1], &sec) != TCL_OK) { |
| 4748 |
return TCL_ERROR; |
| 4749 |
} |
| 4750 |
if (sec > LONG_MAX) { |
| 4751 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 4752 |
"seconds must be between 0 and %ld", LONG_MAX)); |
| 4753 |
goto badValue; |
| 4754 |
} |
| 4755 |
if (sec < 0) { |
| 4756 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4757 |
"seconds must be at least 0", -1)); |
| 4758 |
badValue: |
| 4759 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4760 |
"BADVALUE", (char *)NULL); |
| 4761 |
return TCL_ERROR; |
| 4762 |
} |
| 4763 |
limitMoment.sec = sec; |
| 4764 |
break; |
| 4765 |
} |
| 4766 |
} |
| 4767 |
} |
| 4768 |
if (milliObj != NULL || secObj != NULL) { |
| 4769 |
if (milliObj != NULL) { |
| 4770 |
/* |
| 4771 |
* Setting -milliseconds but clearing -seconds, or resetting |
| 4772 |
* -milliseconds but not resetting -seconds? Bad voodoo! |
| 4773 |
*/ |
| 4774 |
|
| 4775 |
if (secObj != NULL && secLen == 0 && milliLen > 0) { |
| 4776 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4777 |
"may only set -milliseconds if -seconds is not " |
| 4778 |
"also being reset", -1)); |
| 4779 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4780 |
"BADUSAGE", (char *)NULL); |
| 4781 |
return TCL_ERROR; |
| 4782 |
} |
| 4783 |
if (milliLen == 0 && (secObj == NULL || secLen > 0)) { |
| 4784 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 4785 |
"may only reset -milliseconds if -seconds is " |
| 4786 |
"also being reset", -1)); |
| 4787 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "INTERP", |
| 4788 |
"BADUSAGE", (char *)NULL); |
| 4789 |
return TCL_ERROR; |
| 4790 |
} |
| 4791 |
} |
| 4792 |
|
| 4793 |
if (milliLen > 0 || secLen > 0) { |
| 4794 |
/* |
| 4795 |
* Force usec to be in range [0..1000000), possibly |
| 4796 |
* incrementing sec in the process. This makes it much easier |
| 4797 |
* for people to write scripts that do small time increments. |
| 4798 |
*/ |
| 4799 |
|
| 4800 |
limitMoment.sec += limitMoment.usec / 1000000; |
| 4801 |
limitMoment.usec %= 1000000; |
| 4802 |
|
| 4803 |
Tcl_LimitSetTime(childInterp, &limitMoment); |
| 4804 |
Tcl_LimitTypeSet(childInterp, TCL_LIMIT_TIME); |
| 4805 |
} else { |
| 4806 |
Tcl_LimitTypeReset(childInterp, TCL_LIMIT_TIME); |
| 4807 |
} |
| 4808 |
} |
| 4809 |
if (scriptObj != NULL) { |
| 4810 |
SetScriptLimitCallback(interp, TCL_LIMIT_TIME, childInterp, |
| 4811 |
(scriptLen > 0 ? scriptObj : NULL)); |
| 4812 |
} |
| 4813 |
if (granObj != NULL) { |
| 4814 |
Tcl_LimitSetGranularity(childInterp, TCL_LIMIT_TIME, gran); |
| 4815 |
} |
| 4816 |
return TCL_OK; |
| 4817 |
} |
| 4818 |
} |
| 4819 |
|
| 4820 |
/* |
| 4821 |
* Local Variables: |
| 4822 |
* mode: c |
| 4823 |
* c-basic-offset: 4 |
| 4824 |
* fill-column: 78 |
| 4825 |
* End: |
| 4826 |
*/ |