Debian Codegraph @

doc/interp.n

raw
1 '\"
2 '\" Copyright (c) 1995-1996 Sun Microsystems, Inc.
3 '\" Copyright (c) 2004 Donal K. Fellows
4 '\" Copyright (c) 2006-2008 Joe Mistachkin.
5 '\"
6 '\" See the file "license.terms" for information on usage and redistribution
7 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 '\"
9 .TH interp n 8.6 Tcl "Tcl Built-In Commands"
10 .so man.macros
11 .BS
12 '\" Note: do not modify the .SH NAME line immediately below!
13 .SH NAME
14 interp \- Create and manipulate Tcl interpreters
15 .SH SYNOPSIS
16 \fBinterp \fIsubcommand \fR?\fIarg arg ...\fR?
17 .BE
18 .SH DESCRIPTION
19 .PP
20 This command makes it possible to create one or more new Tcl
21 interpreters that co-exist with the creating interpreter in the
22 same application. The creating interpreter is called the \fIparent\fR
23 and the new interpreter is called a \fIchild\fR.
24 A parent can create any number of children, and each child can
25 itself create additional children for which it is parent, resulting
26 in a hierarchy of interpreters.
27 .PP
28 Each interpreter is independent from the others: it has its own name
29 space for commands, procedures, and global variables.
30 A parent interpreter may create connections between its children and
31 itself using a mechanism called an \fIalias\fR. An \fIalias\fR is
32 a command in a child interpreter which, when invoked, causes a
33 command to be invoked in its parent interpreter or in another child
34 interpreter. The only other connections between interpreters are
35 through environment variables (the \fBenv\fR variable), which are
36 normally shared among all interpreters in the application,
37 and by resource limit exceeded callbacks. Note that the
38 name space for files (such as the names returned by the \fBopen\fR command)
39 is no longer shared between interpreters. Explicit commands are provided to
40 share files and to transfer references to open files from one interpreter
41 to another.
42 .PP
43 The \fBinterp\fR command also provides support for \fIsafe\fR
44 interpreters. A safe interpreter is a child whose functions have
45 been greatly restricted, so that it is safe to execute untrusted
46 scripts without fear of them damaging other interpreters or the
47 application's environment. For example, all IO channel creation
48 commands and subprocess creation commands are made inaccessible to safe
49 interpreters.
50 See \fBSAFE INTERPRETERS\fR below for more information on
51 what features are present in a safe interpreter.
52 The dangerous functionality is not removed from the safe interpreter;
53 instead, it is \fIhidden\fR, so that only trusted interpreters can obtain
54 access to it. For a detailed explanation of hidden commands, see
55 \fBHIDDEN COMMANDS\fR, below.
56 The alias mechanism can be used for protected communication (analogous to a
57 kernel call) between a child interpreter and its parent.
58 See \fBALIAS INVOCATION\fR, below, for more details
59 on how the alias mechanism works.
60 .PP
61 A qualified interpreter name is a proper Tcl list containing a subset of its
62 ancestors in the interpreter hierarchy, terminated by the string naming the
63 interpreter in its immediate parent. Interpreter names are relative to the
64 interpreter in which they are used. For example, if
65 .QW \fBa\fR
66 is a child of the current interpreter and it has a child
67 .QW \fBa1\fR ,
68 which in turn has a child
69 .QW \fBa11\fR ,
70 the qualified name of
71 .QW \fBa11\fR
72 in
73 .QW \fBa\fR
74 is the list
75 .QW "\fBa1 a11\fR" .
76 .PP
77 The \fBinterp\fR command, described below, accepts qualified interpreter
78 names as arguments; the interpreter in which the command is being evaluated
79 can always be referred to as \fB{}\fR (the empty list or string). Note that
80 it is impossible to refer to a parent (ancestor) interpreter by name in a
81 child interpreter except through aliases. Also, there is no global name by
82 which one can refer to the first interpreter created in an application.
83 Both restrictions are motivated by safety concerns.
84 .SH "THE INTERP COMMAND"
85 .PP
86 The \fBinterp\fR command is used to create, delete, and manipulate
87 child interpreters, and to share or transfer
88 channels between interpreters. It can have any of several forms, depending
89 on the \fIsubcommand\fR argument:
90 .TP
91 \fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcToken\fR
92 .
93 Returns a Tcl list whose elements are the \fItargetCmd\fR and
94 \fIarg\fRs associated with the alias represented by \fIsrcToken\fR
95 (this is the value returned when the alias was
96 created; it is possible that the name of the source command in the
97 child is different from \fIsrcToken\fR).
98 .TP
99 \fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcToken\fR \fB{}\fR
100 .
101 Deletes the alias for \fIsrcToken\fR in the child interpreter identified by
102 \fIsrcPath\fR.
103 \fIsrcToken\fR refers to the value returned when the alias
104 was created; if the source command has been renamed, the renamed
105 command will be deleted.
106 .TP
107 \fBinterp\fR \fBalias\fR \fIsrcPath\fR \fIsrcCmd\fR \fItargetPath\fR \fItargetCmd \fR?\fIarg arg ...\fR?
108 .
109 This command creates an alias between one child and another (see the
110 \fBalias\fR child command below for creating aliases between a child
111 and its parent). In this command, either of the child interpreters
112 may be anywhere in the hierarchy of interpreters under the interpreter
113 invoking the command.
114 \fISrcPath\fR and \fIsrcCmd\fR identify the source of the alias.
115 \fISrcPath\fR is a Tcl list whose elements select a particular
116 interpreter. For example,
117 .QW "\fBa b\fR"
118 identifies an interpreter
119 .QW \fBb\fR ,
120 which is a child of interpreter
121 .QW \fBa\fR ,
122 which is a child of the invoking interpreter. An empty list specifies
123 the interpreter invoking the command. \fIsrcCmd\fR gives the name of
124 a new command, which will be created in the source interpreter.
125 \fITargetPath\fR and \fItargetCmd\fR specify a target interpreter
126 and command, and the \fIarg\fR arguments, if any, specify additional
127 arguments to \fItargetCmd\fR which are prepended to any arguments specified
128 in the invocation of \fIsrcCmd\fR.
129 \fITargetCmd\fR may be undefined at the time of this call, or it may
130 already exist; it is not created by this command.
131 The alias arranges for the given target command to be invoked
132 in the target interpreter whenever the given source command is
133 invoked in the source interpreter. See \fBALIAS INVOCATION\fR below for
134 more details.
135 The command returns a token that uniquely identifies the command created
136 \fIsrcCmd\fR, even if the command is renamed afterwards. The token may but
137 does not have to be equal to \fIsrcCmd\fR.
138 .TP
139 \fBinterp\fR \fBaliases \fR?\fIpath\fR?
140 .
141 This command returns a Tcl list of the tokens of all the source commands for
142 aliases defined in the interpreter identified by \fIpath\fR. The tokens
143 correspond to the values returned when
144 the aliases were created (which may not be the same
145 as the current names of the commands).
146 .TP
147 \fBinterp bgerror \fIpath\fR ?\fIcmdPrefix\fR?
148 .
149 This command either gets or sets the current background exception handler
150 for the interpreter identified by \fIpath\fR. If \fIcmdPrefix\fR is
151 absent, the current background exception handler is returned, and if it is
152 present, it is a list of words (of minimum length one) that describes
153 what to set the interpreter's background exception handler to. See the
154 \fBBACKGROUND EXCEPTION HANDLING\fR section for more details.
155 .TP
156 \fBinterp\fR \fBcancel \fR?\fB\-unwind\fR? ?\fB\-\|\-\fR? ?\fIpath\fR? ?\fIresult\fR?
157 .VS 8.6
158 Cancels the script being evaluated in the interpreter identified by
159 \fIpath\fR. Without the \fB\-unwind\fR switch the evaluation stack for
160 the interpreter is unwound until an enclosing catch command is found or
161 there are no further invocations of the interpreter left on the call
162 stack. With the \fB\-unwind\fR switch the evaluation stack for the
163 interpreter is unwound without regard to any intervening catch command
164 until there are no further invocations of the interpreter left on the
165 call stack. The \fB\-\|\-\fR switch can be used to mark the end of
166 switches; it may be needed if \fIpath\fR is an unusual value such
167 as \fB\-safe\fR. If \fIresult\fR is present, it will be used as the
168 error message string; otherwise, a default error message string will be
169 used.
170 .VE 8.6
171 .TP
172 \fBinterp\fR \fBcreate \fR?\fB\-safe\fR? ?\fB\-\|\-\fR? ?\fIpath\fR?
173 .
174 Creates a child interpreter identified by \fIpath\fR and a new command,
175 called a \fIchild command\fR. The name of the child command is the last
176 component of \fIpath\fR. The new child interpreter and the child command
177 are created in the interpreter identified by the path obtained by removing
178 the last component from \fIpath\fR. For example, if \fIpath\fR is \fBa b
179 c\fR then a new child interpreter and child command named \fBc\fR are
180 created in the interpreter identified by the path \fBa b\fR.
181 The child command may be used to manipulate the new interpreter as
182 described below. If \fIpath\fR is omitted, Tcl creates a unique name of the
183 form \fBinterp\fIx\fR, where \fIx\fR is an integer, and uses it for the
184 interpreter and the child command. If the \fB\-safe\fR switch is specified
185 (or if the parent interpreter is a safe interpreter), the new child
186 interpreter will be created as a safe interpreter with limited
187 functionality; otherwise the child will include the full set of Tcl
188 built-in commands and variables. The \fB\-\|\-\fR switch can be used to
189 mark the end of switches; it may be needed if \fIpath\fR is an unusual
190 value such as \fB\-safe\fR. The result of the command is the name of the
191 new interpreter. The name of a child interpreter must be unique among all
192 the children for its parent; an error occurs if a child interpreter by the
193 given name already exists in this parent.
194 The initial recursion limit of the child interpreter is set to the
195 current recursion limit of its parent interpreter.
196 .TP
197 \fBinterp\fR \fBdebug \fIpath\fR ?\fB\-frame\fR ?\fIbool\fR??
198 .
199 Controls whether frame-level stack information is captured in the
200 child interpreter identified by \fIpath\fR. If no arguments are
201 given, option and current setting are returned. If \fB\-frame\fR
202 is given, the debug setting is set to the given boolean if provided
203 and the current setting is returned.
204 This only affects the output of \fBinfo frame\fR, in that exact
205 frame-level information for command invocation at the bytecode level
206 is only captured with this setting on.
207 .RS
208 .PP
209 For example, with code like
210 .PP
211 .CS
212 \fBproc\fR mycontrol {... script} {
213 ...
214 \fBuplevel\fR 1 $script
215 ...
216 }
217
218 \fBproc\fR dosomething {...} {
219 ...
220 mycontrol {
221 somecode
222 }
223 }
224 .CE
225 .PP
226 the standard setting will provide a relative line number for the
227 command \fBsomecode\fR and the relevant frame will be of type
228 \fBeval\fR. With frame-debug active on the other hand the tracking
229 extends so far that the system will be able to determine the file and
230 absolute line number of this command, and return a frame of type
231 \fBsource\fR. This more exact information is paid for with slower
232 execution of all commands.
233 .PP
234 Note that once it is on, this flag cannot be switched back off: such
235 attempts are silently ignored. This is needed to maintain the
236 consistency of the underlying interpreter's state.
237 .RE
238 .TP
239 \fBinterp\fR \fBdelete \fR?\fIpath ...?\fR
240 .
241 Deletes zero or more interpreters given by the optional \fIpath\fR
242 arguments, and for each interpreter, it also deletes its children. The
243 command also deletes the child command for each interpreter deleted.
244 For each \fIpath\fR argument, if no interpreter by that name
245 exists, the command raises an error.
246 .TP
247 \fBinterp\fR \fBeval\fR \fIpath arg \fR?\fIarg ...\fR?
248 .
249 This command concatenates all of the \fIarg\fR arguments in the same
250 fashion as the \fBconcat\fR command, then evaluates the resulting string as
251 a Tcl script in the child interpreter identified by \fIpath\fR. The result
252 of this evaluation (including all \fBreturn\fR options,
253 such as \fB\-errorinfo\fR and \fB\-errorcode\fR information, if an
254 error occurs) is returned to the invoking interpreter.
255 Note that the script will be executed in the current context stack frame of the
256 \fIpath\fR interpreter; this is so that the implementations (in a parent
257 interpreter) of aliases in a child interpreter can execute scripts in
258 the child that find out information about the child's current state
259 and stack frame.
260 .TP
261 \fBinterp exists \fIpath\fR
262 .
263 Returns \fB1\fR if a child interpreter by the specified \fIpath\fR
264 exists in this parent, \fB0\fR otherwise. If \fIpath\fR is omitted, the
265 invoking interpreter is used.
266 .TP
267 \fBinterp expose \fIpath\fR \fIhiddenName\fR ?\fIexposedCmdName\fR?
268 .
269 Makes the hidden command \fIhiddenName\fR exposed, eventually bringing
270 it back under a new \fIexposedCmdName\fR name (this name is currently
271 accepted only if it is a valid global name space name without any ::),
272 in the interpreter
273 denoted by \fIpath\fR.
274 If an exposed command with the targeted name already exists, this command
275 fails.
276 Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
277 .TP
278 \fBinterp\fR \fBhide\fR \fIpath\fR \fIexposedCmdName\fR ?\fIhiddenCmdName\fR?
279 .
280 Makes the exposed command \fIexposedCmdName\fR hidden, renaming
281 it to the hidden command \fIhiddenCmdName\fR, or keeping the same name if
282 \fIhiddenCmdName\fR is not given, in the interpreter denoted
283 by \fIpath\fR.
284 If a hidden command with the targeted name already exists, this command
285 fails.
286 Currently both \fIexposedCmdName\fR and \fIhiddenCmdName\fR can
287 not contain namespace qualifiers, or an error is raised.
288 Commands to be hidden by \fBinterp hide\fR are looked up in the global
289 namespace even if the current namespace is not the global one. This
290 prevents children from fooling a parent interpreter into hiding the wrong
291 command, by making the current namespace be different from the global one.
292 Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
293 .TP
294 \fBinterp\fR \fBhidden\fR \fIpath\fR
295 .
296 Returns a list of the names of all hidden commands in the interpreter
297 identified by \fIpath\fR.
298 .TP
299 \fBinterp\fR \fBinvokehidden\fR \fIpath\fR ?\fI\-option ...\fR? \fIhiddenCmdName\fR ?\fIarg ...\fR?
300 .
301 Invokes the hidden command \fIhiddenCmdName\fR with the arguments supplied
302 in the interpreter denoted by \fIpath\fR. No substitutions or evaluation
303 are applied to the arguments. Three \fI\-option\fRs are supported, all
304 of which start with \fB\-\fR: \fB\-namespace\fR (which takes a single
305 argument afterwards, \fInsName\fR), \fB\-global\fR, and \fB\-\|\-\fR.
306 If the \fB\-namespace\fR flag is present, the hidden command is invoked in
307 the namespace called \fInsName\fR in the target interpreter.
308 If the \fB\-global\fR flag is present, the hidden command is invoked at the
309 global level in the target interpreter; otherwise it is invoked at the
310 current call frame and can access local variables in that and outer call
311 frames.
312 The \fB\-\|\-\fR flag allows the \fIhiddenCmdName\fR argument to start with a
313 .QW \-
314 character, and is otherwise unnecessary.
315 If both the \fB\-namespace\fR and \fB\-global\fR flags are present, the
316 \fB\-namespace\fR flag is ignored.
317 Note that the hidden command will be executed (by default) in the
318 current context stack frame of the \fIpath\fR interpreter.
319 Hidden commands are explained in more detail in \fBHIDDEN COMMANDS\fR, below.
320 .TP
321 \fBinterp issafe\fR ?\fIpath\fR?
322 .
323 Returns \fB1\fR if the interpreter identified by the specified \fIpath\fR
324 is safe, \fB0\fR otherwise.
325 .TP
326 \fBinterp\fR \fBlimit\fR \fIpath\fR \fIlimitType\fR ?\fI\-option\fR? ?\fIvalue\fR \fI...\fR?
327 .
328 Sets up, manipulates and queries the configuration of the resource
329 limit \fIlimitType\fR for the interpreter denoted by \fIpath\fR. If
330 no \fI\-option\fR is specified, return the current configuration of the
331 limit. If \fI\-option\fR is the sole argument, return the value of that
332 option. Otherwise, a list of \fI\-option\fR/\fIvalue\fR argument pairs
333 must supplied. See \fBRESOURCE LIMITS\fR below for a more detailed
334 explanation of what limits and options are supported.
335 .TP
336 \fBinterp marktrusted\fR \fIpath\fR
337 .
338 Marks the interpreter identified by \fIpath\fR as trusted. Does
339 not expose the hidden commands. This command can only be invoked from a
340 trusted interpreter.
341 The command has no effect if the interpreter identified by \fIpath\fR is
342 already trusted.
343 .TP
344 \fBinterp\fR \fBrecursionlimit\fR \fIpath\fR ?\fInewlimit\fR?
345 .
346 Returns the maximum allowable nesting depth for the interpreter
347 specified by \fIpath\fR. If \fInewlimit\fR is specified,
348 the interpreter recursion limit will be set so that nesting
349 of more than \fInewlimit\fR calls to \fBTcl_Eval\fR
350 and related procedures in that interpreter will return an error.
351 The \fInewlimit\fR value is also returned.
352 The \fInewlimit\fR value must be a positive integer between 1 and the
353 maximum value of a non-long integer on the platform.
354 .RS
355 .PP
356 The command sets the maximum size of the Tcl call stack only. It cannot
357 by itself prevent stack overflows on the C stack being used by the
358 application. If your machine has a limit on the size of the C stack, you
359 may get stack overflows before reaching the limit set by the command. If
360 this happens, see if there is a mechanism in your system for increasing
361 the maximum size of the C stack.
362 .RE
363 .TP
364 \fBinterp\fR \fBshare\fR \fIsrcPath channelId destPath\fR
365 .
366 Causes the IO channel identified by \fIchannelId\fR to become shared
367 between the interpreter identified by \fIsrcPath\fR and the interpreter
368 identified by \fIdestPath\fR. Both interpreters have the same permissions
369 on the IO channel.
370 Both interpreters must close it to close the underlying IO channel; IO
371 channels accessible in an interpreter are automatically closed when an
372 interpreter is destroyed.
373 .TP
374 \fBinterp\fR \fBslaves\fR ?\fIpath\fR?
375 .
376 Returns a Tcl list of the names of all the child interpreters associated
377 with the interpreter identified by \fIpath\fR. If \fIpath\fR is omitted,
378 the invoking interpreter is used.
379 .VS "TIP 581"
380 .TP
381 \fBinterp\fR \fBchildren\fR ?\fIpath\fR?
382 .
383 Synonym for . \fBinterp\fR \fBslaves\fR ?\fIpath\fR?
384 .VE "TIP 581"
385 .TP
386 \fBinterp\fR \fBtarget\fR \fIpath alias\fR
387 .
388 Returns a Tcl list describing the target interpreter for an alias. The
389 alias is specified with an interpreter path and source command name, just
390 as in \fBinterp alias\fR above. The name of the target interpreter is
391 returned as an interpreter path, relative to the invoking interpreter.
392 If the target interpreter for the alias is the invoking interpreter then an
393 empty list is returned. If the target interpreter for the alias is not the
394 invoking interpreter or one of its descendants then an error is generated.
395 The target command does not have to be defined at the time of this invocation.
396 .TP
397 \fBinterp\fR \fBtransfer\fR \fIsrcPath channelId destPath\fR
398 .
399 Causes the IO channel identified by \fIchannelId\fR to become available in
400 the interpreter identified by \fIdestPath\fR and unavailable in the
401 interpreter identified by \fIsrcPath\fR.
402 .SH "child COMMAND"
403 .PP
404 For each child interpreter created with the \fBinterp\fR command, a
405 new Tcl command is created in the parent interpreter with the same
406 name as the new interpreter. This command may be used to invoke
407 various operations on the interpreter. It has the following
408 general form:
409 .PP
410 .CS
411 \fIchild command \fR?\fIarg arg ...\fR?
412 .CE
413 .PP
414 \fIchild\fR is the name of the interpreter, and \fIcommand\fR
415 and the \fIarg\fRs determine the exact behavior of the command.
416 The valid forms of this command are:
417 .TP
418 \fIchild \fBaliases\fR
419 .
420 Returns a Tcl list whose elements are the tokens of all the
421 aliases in \fIchild\fR. The tokens correspond to the values returned when
422 the aliases were created (which may not be the same
423 as the current names of the commands).
424 .TP
425 \fIchild \fBalias \fIsrcToken\fR
426 .
427 Returns a Tcl list whose elements are the \fItargetCmd\fR and
428 \fIarg\fRs associated with the alias represented by \fIsrcToken\fR
429 (this is the value returned when the alias was
430 created; it is possible that the actual source command in the
431 child is different from \fIsrcToken\fR).
432 .TP
433 \fIchild \fBalias \fIsrcToken \fB{}\fR
434 .
435 Deletes the alias for \fIsrcToken\fR in the child interpreter.
436 \fIsrcToken\fR refers to the value returned when the alias
437 was created; if the source command has been renamed, the renamed
438 command will be deleted.
439 .TP
440 \fIchild \fBalias \fIsrcCmd targetCmd \fR?\fIarg ..\fR?
441 .
442 Creates an alias such that whenever \fIsrcCmd\fR is invoked
443 in \fIchild\fR, \fItargetCmd\fR is invoked in the parent.
444 The \fIarg\fR arguments will be passed to \fItargetCmd\fR as additional
445 arguments, prepended before any arguments passed in the invocation of
446 \fIsrcCmd\fR.
447 See \fBALIAS INVOCATION\fR below for details.
448 The command returns a token that uniquely identifies the command created
449 \fIsrcCmd\fR, even if the command is renamed afterwards. The token may but
450 does not have to be equal to \fIsrcCmd\fR.
451 .TP
452 \fIchild \fBbgerror\fR ?\fIcmdPrefix\fR?
453 .
454 This command either gets or sets the current background exception handler
455 for the \fIchild\fR interpreter. If \fIcmdPrefix\fR is
456 absent, the current background exception handler is returned, and if it is
457 present, it is a list of words (of minimum length one) that describes
458 what to set the interpreter's background exception handler to. See the
459 \fBBACKGROUND EXCEPTION HANDLING\fR section for more details.
460 .TP
461 \fIchild \fBeval \fIarg \fR?\fIarg ..\fR?
462 .
463 This command concatenates all of the \fIarg\fR arguments in
464 the same fashion as the \fBconcat\fR command, then evaluates
465 the resulting string as a Tcl script in \fIchild\fR.
466 The result of this evaluation (including all \fBreturn\fR options,
467 such as \fB\-errorinfo\fR and \fB\-errorcode\fR information, if an
468 error occurs) is returned to the invoking interpreter.
469 Note that the script will be executed in the current context stack frame
470 of \fIchild\fR; this is so that the implementations (in a parent
471 interpreter) of aliases in a child interpreter can execute scripts in
472 the child that find out information about the child's current state
473 and stack frame.
474 .TP
475 \fIchild \fBexpose \fIhiddenName \fR?\fIexposedCmdName\fR?
476 .
477 This command exposes the hidden command \fIhiddenName\fR, eventually bringing
478 it back under a new \fIexposedCmdName\fR name (this name is currently
479 accepted only if it is a valid global name space name without any ::),
480 in \fIchild\fR.
481 If an exposed command with the targeted name already exists, this command
482 fails.
483 For more details on hidden commands, see \fBHIDDEN COMMANDS\fR, below.
484 .TP
485 \fIchild \fBhide \fIexposedCmdName\fR ?\fIhiddenCmdName\fR?
486 .
487 This command hides the exposed command \fIexposedCmdName\fR, renaming it to
488 the hidden command \fIhiddenCmdName\fR, or keeping the same name if the
489 argument is not given, in the \fIchild\fR interpreter.
490 If a hidden command with the targeted name already exists, this command
491 fails.
492 Currently both \fIexposedCmdName\fR and \fIhiddenCmdName\fR can
493 not contain namespace qualifiers, or an error is raised.
494 Commands to be hidden are looked up in the global
495 namespace even if the current namespace is not the global one. This
496 prevents children from fooling a parent interpreter into hiding the wrong
497 command, by making the current namespace be different from the global one.
498 For more details on hidden commands, see \fBHIDDEN COMMANDS\fR, below.
499 .TP
500 \fIchild \fBhidden\fR
501 .
502 Returns a list of the names of all hidden commands in \fIchild\fR.
503 .TP
504 \fIchild \fBinvokehidden\fR ?\fI\-option ...\fR? \fIhiddenName \fR?\fIarg ..\fR?
505 .
506 This command invokes the hidden command \fIhiddenName\fR with the
507 supplied arguments, in \fIchild\fR. No substitutions or evaluations are
508 applied to the arguments. Three \fI\-option\fRs are supported, all
509 of which start with \fB\-\fR: \fB\-namespace\fR (which takes a single
510 argument afterwards, \fInsName\fR), \fB\-global\fR, and \fB\-\|\-\fR.
511 If the \fB\-namespace\fR flag is given, the hidden command is invoked in
512 the specified namespace in the child.
513 If the \fB\-global\fR flag is given, the command is invoked at the global
514 level in the child; otherwise it is invoked at the current call frame and
515 can access local variables in that or outer call frames.
516 The \fB\-\|\-\fR flag allows the \fIhiddenCmdName\fR argument to start with a
517 .QW \-
518 character, and is otherwise unnecessary.
519 If both the \fB\-namespace\fR and \fB\-global\fR flags are given, the
520 \fB\-namespace\fR flag is ignored.
521 Note that the hidden command will be executed (by default) in the
522 current context stack frame of \fIchild\fR.
523 For more details on hidden commands,
524 see \fBHIDDEN COMMANDS\fR, below.
525 .TP
526 \fIchild \fBissafe\fR
527 .
528 Returns \fB1\fR if the child interpreter is safe, \fB0\fR otherwise.
529 .TP
530 \fIchild \fBlimit\fR \fIlimitType\fR ?\fI\-option\fR? ?\fIvalue\fR \fI...\fR?
531 .
532 Sets up, manipulates and queries the configuration of the resource
533 limit \fIlimitType\fR for the child interpreter. If no \fI\-option\fR
534 is specified, return the current configuration of the limit. If
535 \fI\-option\fR is the sole argument, return the value of that option.
536 Otherwise, a list of \fI\-option\fR/\fIvalue\fR argument pairs must
537 supplied. See \fBRESOURCE LIMITS\fR below for a more detailed explanation of
538 what limits and options are supported.
539 .TP
540 \fIchild \fBmarktrusted\fR
541 .
542 Marks the child interpreter as trusted. Can only be invoked by a
543 trusted interpreter. This command does not expose any hidden
544 commands in the child interpreter. The command has no effect if the child
545 is already trusted.
546 .TP
547 \fIchild\fR \fBrecursionlimit\fR ?\fInewlimit\fR?
548 .
549 Returns the maximum allowable nesting depth for the \fIchild\fR interpreter.
550 If \fInewlimit\fR is specified, the recursion limit in \fIchild\fR will be
551 set so that nesting of more than \fInewlimit\fR calls to \fBTcl_Eval()\fR
552 and related procedures in \fIchild\fR will return an error.
553 The \fInewlimit\fR value is also returned.
554 The \fInewlimit\fR value must be a positive integer between 1 and the
555 maximum value of a non-long integer on the platform.
556 .RS
557 .PP
558 The command sets the maximum size of the Tcl call stack only. It cannot
559 by itself prevent stack overflows on the C stack being used by the
560 application. If your machine has a limit on the size of the C stack, you
561 may get stack overflows before reaching the limit set by the command. If
562 this happens, see if there is a mechanism in your system for increasing
563 the maximum size of the C stack.
564 .RE
565 .SH "SAFE INTERPRETERS"
566 .PP
567 A safe interpreter is one with restricted functionality, so that
568 is safe to execute an arbitrary script from your worst enemy without
569 fear of that script damaging the enclosing application or the rest
570 of your computing environment. In order to make an interpreter
571 safe, certain commands and variables are removed from the interpreter.
572 For example, commands to create files on disk are removed, and the
573 \fBexec\fR command is removed, since it could be used to cause damage
574 through subprocesses.
575 Limited access to these facilities can be provided, by creating
576 aliases to the parent interpreter which check their arguments carefully
577 and provide restricted access to a safe subset of facilities.
578 For example, file creation might be allowed in a particular subdirectory
579 and subprocess invocation might be allowed for a carefully selected and
580 fixed set of programs.
581 .PP
582 A safe interpreter is created by specifying the \fB\-safe\fR switch
583 to the \fBinterp create\fR command. Furthermore, any child created
584 by a safe interpreter will also be safe.
585 .PP
586 A safe interpreter is created with exactly the following set of
587 built-in commands:
588 .DS
589 .ta 1.2i 2.4i 3.6i
590 \fBafter\fR \fBappend\fR \fBapply\fR \fBarray\fR
591 \fBbinary\fR \fBbreak\fR \fBcatch\fR \fBchan\fR
592 \fBclock\fR \fBclose\fR \fBconcat\fR \fBcontinue\fR
593 \fBdict\fR \fBeof\fR \fBerror\fR \fBeval\fR
594 \fBexpr\fR \fBfblocked\fR \fBfcopy\fR \fBfileevent\fR
595 \fBflush\fR \fBfor\fR \fBforeach\fR \fBformat\fR
596 \fBgets\fR \fBglobal\fR \fBif\fR \fBincr\fR
597 \fBinfo\fR \fBinterp\fR \fBjoin\fR \fBlappend\fR
598 \fBlassign\fR \fBlindex\fR \fBlinsert\fR \fBlist\fR
599 \fBllength\fR \fBlrange\fR \fBlrepeat\fR \fBlreplace\fR
600 \fBlsearch\fR \fBlset\fR \fBlsort\fR \fBnamespace\fR
601 \fBpackage\fR \fBpid\fR \fBproc\fR \fBputs\fR
602 \fBread\fR \fBregexp\fR \fBregsub\fR \fBrename\fR
603 \fBreturn\fR \fBscan\fR \fBseek\fR \fBset\fR
604 \fBsplit\fR \fBstring\fR \fBsubst\fR \fBswitch\fR
605 \fBtell\fR \fBtime\fR \fBtrace\fR \fBunset\fR
606 \fBupdate\fR \fBuplevel\fR \fBupvar\fR \fBvariable\fR
607 \fBvwait\fR \fBwhile\fR
608 \fBzlib\fR
609 .DE
610 The following commands are hidden by \fBinterp create\fR when it
611 creates a safe interpreter:
612 .DS
613 .ta 1.2i 2.4i 3.6i
614 \fBcd\fR \fBencoding\fR \fBexec\fR \fBexit\fR
615 \fBfconfigure\fR \fBfile\fR \fBglob\fR \fBload\fR
616 \fBopen\fR \fBpwd\fR \fBsocket\fR \fBsource\fR
617 \fBunload\fR
618 .DE
619 These commands can be recreated later as Tcl procedures or aliases, or
620 re-exposed by \fBinterp expose\fR.
621 .PP
622 The following commands from Tcl's library of support procedures are
623 not present in a safe interpreter:
624 .DS
625 .ta 1.6i 3.2i
626 \fBauto_exec_ok\fR \fBauto_import\fR \fBauto_load\fR
627 \fBauto_load_index\fR \fBauto_qualify\fR \fBunknown\fR
628 .DE
629 Note in particular that safe interpreters have no default \fBunknown\fR
630 command, so Tcl's default autoloading facilities are not available.
631 Autoload access to Tcl's commands that are normally autoloaded:
632 .DS
633 .ta 2.1i
634 \fBauto_mkindex\fR \fBauto_mkindex_old\fR
635 \fBauto_reset\fR \fBhistory\fR
636 \fBparray\fR \fBpkg_mkIndex\fR
637 \fB::pkg::create\fR \fB::safe::interpAddToAccessPath\fR
638 \fB::safe::interpCreate\fR \fB::safe::interpConfigure\fR
639 \fB::safe::interpDelete\fR \fB::safe::interpFindInAccessPath\fR
640 \fB::safe::interpInit\fR \fB::safe::setLogCmd\fR
641 \fBtcl_endOfWord\fR \fBtcl_findLibrary\fR
642 \fBtcl_startOfNextWord\fR \fBtcl_startOfPreviousWord\fR
643 \fBtcl_wordBreakAfter\fR \fBtcl_wordBreakBefore\fR
644 .DE
645 can only be provided by explicit definition of an \fBunknown\fR command
646 in the safe interpreter. This will involve exposing the \fBsource\fR
647 command. This is most easily accomplished by creating the safe interpreter
648 with Tcl's \fBSafe\-Tcl\fR mechanism. \fBSafe\-Tcl\fR provides safe
649 versions of \fBsource\fR, \fBload\fR, and other Tcl commands needed
650 to support autoloading of commands and the loading of packages.
651 .PP
652 In addition, the \fBenv\fR variable is not present in a safe interpreter,
653 so it cannot share environment variables with other interpreters. The
654 \fBenv\fR variable poses a security risk, because users can store
655 sensitive information in an environment variable. For example, the PGP
656 manual recommends storing the PGP private key protection password in
657 the environment variable \fIPGPPASS\fR. Making this variable available
658 to untrusted code executing in a safe interpreter would incur a
659 security risk.
660 .PP
661 If extensions are loaded into a safe interpreter, they may also restrict
662 their own functionality to eliminate unsafe commands. For a discussion of
663 management of extensions for safety see the manual entries for
664 \fBSafe\-Tcl\fR and the \fBload\fR Tcl command.
665 .PP
666 A safe interpreter may not alter the recursion limit of any interpreter,
667 including itself.
668 .SH "ALIAS INVOCATION"
669 .PP
670 The alias mechanism has been carefully designed so that it can
671 be used safely in an untrusted script which is being executed in a
672 safe interpreter even if the target of the alias is not a safe
673 interpreter. The most important thing in guaranteeing safety is to
674 ensure that information passed from the child to the parent is
675 never evaluated or substituted in the parent; if this were to
676 occur, it would enable an evil script in the child to invoke
677 arbitrary functions in the parent, which would compromise security.
678 .PP
679 When the source for an alias is invoked in the child interpreter, the
680 usual Tcl substitutions are performed when parsing that command.
681 These substitutions are carried out in the source interpreter just
682 as they would be for any other command invoked in that interpreter.
683 The command procedure for the source command takes its arguments
684 and merges them with the \fItargetCmd\fR and \fIarg\fRs for the
685 alias to create a new array of arguments. If the words
686 of \fIsrcCmd\fR were
687 .QW "\fIsrcCmd arg1 arg2 ... argN\fR" ,
688 the new set of words will be
689 .QW "\fItargetCmd arg arg ... arg arg1 arg2 ... argN\fR" ,
690 where \fItargetCmd\fR and \fIarg\fRs are the values supplied when the
691 alias was created. \fITargetCmd\fR is then used to locate a command
692 procedure in the target interpreter, and that command procedure
693 is invoked with the new set of arguments. An error occurs if
694 there is no command named \fItargetCmd\fR in the target interpreter.
695 No additional substitutions are performed on the words: the
696 target command procedure is invoked directly, without
697 going through the normal Tcl evaluation mechanism.
698 Substitutions are thus performed on each word exactly once:
699 \fItargetCmd\fR and \fIargs\fR were substituted when parsing the command
700 that created the alias, and \fIarg1 - argN\fR are substituted when
701 the alias's source command is parsed in the source interpreter.
702 .PP
703 When writing the \fItargetCmd\fRs for aliases in safe interpreters,
704 it is very important that the arguments to that command never be
705 evaluated or substituted, since this would provide an escape
706 mechanism whereby the child interpreter could execute arbitrary
707 code in the parent. This in turn would compromise the security
708 of the system.
709 .SH "HIDDEN COMMANDS"
710 .PP
711 Safe interpreters greatly restrict the functionality available to Tcl
712 programs executing within them.
713 Allowing the untrusted Tcl program to have direct access to this
714 functionality is unsafe, because it can be used for a variety of
715 attacks on the environment.
716 However, there are times when there is a legitimate need to use the
717 dangerous functionality in the context of the safe interpreter. For
718 example, sometimes a program must be \fBsource\fRd into the interpreter.
719 Another example is Tk, where windows are bound to the hierarchy of windows
720 for a specific interpreter; some potentially dangerous functions, e.g.
721 window management, must be performed on these windows within the
722 interpreter context.
723 .PP
724 The \fBinterp\fR command provides a solution to this problem in the form of
725 \fIhidden commands\fR. Instead of removing the dangerous commands entirely
726 from a safe interpreter, these commands are hidden so they become
727 unavailable to Tcl scripts executing in the interpreter. However, such
728 hidden commands can be invoked by any trusted ancestor of the safe
729 interpreter, in the context of the safe interpreter, using \fBinterp
730 invoke\fR. Hidden commands and exposed commands reside in separate name
731 spaces. It is possible to define a hidden command and an exposed command by
732 the same name within one interpreter.
733 .PP
734 Hidden commands in a child interpreter can be invoked in the body of
735 procedures called in the parent during alias invocation. For example, an
736 alias for \fBsource\fR could be created in a child interpreter. When it is
737 invoked in the child interpreter, a procedure is called in the parent
738 interpreter to check that the operation is allowable (e.g. it asks to
739 source a file that the child interpreter is allowed to access). The
740 procedure then it invokes the hidden \fBsource\fR command in the child
741 interpreter to actually source in the contents of the file. Note that two
742 commands named \fBsource\fR exist in the child interpreter: the alias, and
743 the hidden command.
744 .PP
745 Because a parent interpreter may invoke a hidden command as part of
746 handling an alias invocation, great care must be taken to avoid evaluating
747 any arguments passed in through the alias invocation.
748 Otherwise, malicious child interpreters could cause a trusted parent
749 interpreter to execute dangerous commands on their behalf. See the section
750 on \fBALIAS INVOCATION\fR for a more complete discussion of this topic.
751 To help avoid this problem, no substitutions or evaluations are
752 applied to arguments of \fBinterp invokehidden\fR.
753 .PP
754 Safe interpreters are not allowed to invoke hidden commands in themselves
755 or in their descendants. This prevents them from gaining access to
756 hidden functionality in themselves or their descendants.
757 .PP
758 The set of hidden commands in an interpreter can be manipulated by a trusted
759 interpreter using \fBinterp expose\fR and \fBinterp hide\fR. The \fBinterp
760 expose\fR command moves a hidden command to the
761 set of exposed commands in the interpreter identified by \fIpath\fR,
762 potentially renaming the command in the process. If an exposed command by
763 the targeted name already exists, the operation fails. Similarly,
764 \fBinterp hide\fR moves an exposed command to the set of hidden commands in
765 that interpreter. Safe interpreters are not allowed to move commands
766 between the set of hidden and exposed commands, in either themselves or
767 their descendants.
768 .PP
769 Currently, the names of hidden commands cannot contain namespace
770 qualifiers, and you must first rename a command in a namespace to the
771 global namespace before you can hide it.
772 Commands to be hidden by \fBinterp hide\fR are looked up in the global
773 namespace even if the current namespace is not the global one. This
774 prevents children from fooling a parent interpreter into hiding the wrong
775 command, by making the current namespace be different from the global one.
776 .SH "RESOURCE LIMITS"
777 .PP
778 Every interpreter has two kinds of resource limits that may be imposed by any
779 parent interpreter upon its children. Command limits (of type \fBcommand\fR)
780 restrict the total number of Tcl commands that may be executed by an
781 interpreter (as can be inspected via the \fBinfo cmdcount\fR command), and
782 time limits (of type \fBtime\fR) place a limit by which execution within the
783 interpreter must complete. Note that time limits are expressed as
784 \fIabsolute\fR times (as in \fBclock seconds\fR) and not relative times (as in
785 \fBafter\fR) because they may be modified after creation.
786 .PP
787 When a limit is exceeded for an interpreter, first any handler callbacks
788 defined by parent interpreters are called. If those callbacks increase or
789 remove the limit, execution within the (previously) limited interpreter
790 continues. If the limit is still in force, an error is generated at that point
791 and normal processing of errors within the interpreter (by the \fBcatch\fR
792 command) is disabled, so the error propagates outwards (building a stack-trace
793 as it goes) to the point where the limited interpreter was invoked (e.g. by
794 \fBinterp eval\fR) where it becomes the responsibility of the calling code to
795 catch and handle.
796 .SS "LIMIT OPTIONS"
797 .PP
798 Every limit has a number of options associated with it, some of which are
799 common across all kinds of limits, and others of which are particular to the
800 kind of limit.
801 .TP
802 \fB\-command\fR
803 .
804 This option (common for all limit types) specifies (if non-empty) a Tcl script
805 to be executed in the global namespace of the interpreter reading and writing
806 the option when the particular limit in the limited interpreter is exceeded.
807 The callback may modify the limit on the interpreter if it wishes the limited
808 interpreter to continue executing. If the callback generates an exception, it
809 is reported through the background exception mechanism (see
810 \fBBACKGROUND EXCEPTION HANDLING\fR).
811 Note that the callbacks defined by one interpreter are
812 completely isolated from the callbacks defined by another, and that the order
813 in which those callbacks are called is undefined.
814 .TP
815 \fB\-granularity\fR
816 .
817 This option (common for all limit types) specifies how frequently (out of the
818 points when the Tcl interpreter is in a consistent state where limit checking
819 is possible) that the limit is actually checked. This allows the tuning of how
820 frequently a limit is checked, and hence how often the limit-checking overhead
821 (which may be substantial in the case of time limits) is incurred.
822 .TP
823 \fB\-milliseconds\fR
824 .
825 This option specifies the number of milliseconds after the moment defined in
826 the \fB\-seconds\fR option that the time limit will fire. It should only ever
827 be specified in conjunction with the \fB\-seconds\fR option (whether it was
828 set previously or is being set this invocation.)
829 .TP
830 \fB\-seconds\fR
831 .
832 This option specifies the number of seconds after the epoch (see \fBclock
833 seconds\fR) that the time limit for the interpreter will be triggered. The
834 limit will be triggered at the start of the second unless specified at a
835 sub-second level using the \fB\-milliseconds\fR option. This option may be the
836 empty string, which indicates that a time limit is not set for the
837 interpreter.
838 .TP
839 \fB\-value\fR
840 .
841 This option specifies the number of commands that the interpreter may execute
842 before triggering the command limit. This option may be the empty string,
843 which indicates that a command limit is not set for the interpreter.
844 .PP
845 Where an interpreter with a resource limit set on it creates a child
846 interpreter, that child interpreter will have resource limits imposed on it
847 that are at least as restrictive as the limits on the creating parent
848 interpreter. If the parent interpreter of the limited parent wishes to relax
849 these conditions, it should hide the \fBinterp\fR command in the child and
850 then use aliases and the \fBinterp invokehidden\fR subcommand to provide such
851 access as it chooses to the \fBinterp\fR command to the limited parent as
852 necessary.
853 .SH "BACKGROUND EXCEPTION HANDLING"
854 .PP
855 When an exception happens in a situation where it cannot be reported directly up
856 the stack (e.g. when processing events in an \fBupdate\fR or \fBvwait\fR call)
857 the exception is instead reported through the background exception handling mechanism.
858 Every interpreter has a background exception handler registered; the default exception
859 handler arranges for the \fBbgerror\fR command in the interpreter's global
860 namespace to be called, but other exception handlers may be installed and process
861 background exceptions in substantially different ways.
862 .PP
863 A background exception handler consists of a non-empty list of words to which will
864 be appended two further words at invocation time. The first word will be the
865 interpreter result at time of the exception, typically an error message,
866 and the second will be the dictionary of return options at the time of
867 the exception. These are the same values that \fBcatch\fR can capture
868 when it controls script evaluation in a non-background situation.
869 The resulting list will then be executed
870 in the interpreter's global namespace without further substitutions being
871 performed.
872 .SH CREDITS
873 The safe interpreter mechanism is based on the Safe-Tcl prototype implemented
874 by Nathaniel Borenstein and Marshall Rose.
875 .SH EXAMPLES
876 .PP
877 Creating and using an alias for a command in the current interpreter:
878 .PP
879 .CS
880 \fBinterp alias\fR {} getIndex {} lsearch {alpha beta gamma delta}
881 set idx [getIndex delta]
882 .CE
883 .PP
884 Executing an arbitrary command in a safe interpreter where every
885 invocation of \fBlappend\fR is logged:
886 .PP
887 .CS
888 set i [\fBinterp create\fR -safe]
889 \fBinterp hide\fR $i lappend
890 \fBinterp alias\fR $i lappend {} loggedLappend $i
891 proc loggedLappend {i args} {
892 puts "logged invocation of lappend $args"
893 \fBinterp invokehidden\fR $i lappend {*}$args
894 }
895 \fBinterp eval\fR $i $someUntrustedScript
896 .CE
897 .PP
898 Setting a resource limit on an interpreter so that an infinite loop
899 terminates.
900 .PP
901 .CS
902 set i [\fBinterp create\fR]
903 \fBinterp limit\fR $i command -value 1000
904 \fBinterp eval\fR $i {
905 set x 0
906 while {1} {
907 puts "Counting up... [incr x]"
908 }
909 }
910 .CE
911 .SH "SEE ALSO"
912 bgerror(n), load(n), safe(n), Tcl_CreateChild(3), Tcl_Eval(3), Tcl_BackgroundException(3)
913 .SH KEYWORDS
914 alias, parent interpreter, safe interpreter, child interpreter
915 '\"Local Variables:
916 '\"mode: nroff
917 '\"End: