aio recvfrom was not null terminating the result
[jimtcl.git] / jim_tcl.txt
blob4299d37748552e97054de7f8002a9c3c70c6a814
1 Jim Tcl(n)
2 ==========
4 NAME
5 ----
6 Jim Tcl v0.63 - overview of the Jim tool command language facilities
8 SYNOPSIS
9 --------
11   cc <source> -ljim
15   jimsh <script>
17 .Quick Index
18 * <<CommandIndex,Command Reference>>
19 * <<OperatorPrecedence,Operator Precedence>>
20 * <<BuiltinVariables, Builtin Variables>>
21 * <<BackslashSequences, Backslash Sequences>>
23 INTRODUCTION
24 ------------
25 Jim is a reimplementation of Tcl, combining some features from
26 earlier, smaller versions of Tcl (6.x) as well as more modern
27 features from later versions of Tcl (7.x, 8.x). It also has some some
28 entirely new features not available in any version of Tcl.
30 This version is approximately the same size as "tinytcl" (6.8) but
31 is faster and has more features.
33 Note that most of this man page is the original 6.8 Tcl man page, with
34 changes made for differences with Jim.
36 The major differences are:
38 1. Object-based I/O (aio) with backward compatibility wrapper
39 2. I/O: Support for sockets (client and server)
40 3. I/O: Support for readable/writable event handlers
41 4. Integers are 64bit
42 5. Support for references ('ref'/'getref'/'setref') and garbage collection
43 6. Builtin dictionary type ('dict')
44 7. 'file mkdir', 'file rename', 'file tempfile' (Tcl 7.x, 8.x)
45 8. 'env' command to access environment variables
46 9. List: 'lmap', 'lset', 'lreverse', 'lassign' (Tcl 8.x)
47 10. 'os.fork', 'os.wait', 'rand' 
48 11. '\{*\}'/'\{expand\}'
49 12. 'string map' (Tcl 7.x)
50 13. 'subst' (Tcl 7.x)
51 14. 'switch' (Tcl 7.x) (note that 'case' is provided for compatibility)
52 15. Must better error reporting. 'info stacktrace' as a replacement for 'errorInfo', 'errorCode'
53 16. Support for "static" variables in procedures
54 17. Significantly faster for many scripts/operations
55 18. Support for tail-call optimisation, 'tailcall'
56 19. Command pipelines via open "|..." are not supported (but see 'exec' and 'socket pipe')
57 20. Variable traces are not supported
58 21. The history command is not supported
60 CHANGES
61 -------
62 Since v0.62:
64 1. 'source' now checks that a script is complete (.i.e. not missing a brace)
65 2. 'info complete' now uses the real parser and so is 100% accurate
66 3. Better access to live stack frames with 'info frame', 'stacktrace' and 'stackdump'
67 4. 'tailcall' no longer loses stack trace information
68 5. Add 'alias' and 'curry'
69 6. 'lambda', 'alias' and 'curry' are implemented via 'tailcall' for efficiency
70 7. 'local' allows procedures to be deleted automatically at the end of the current procedure
71 8. udp sockets are now supported for both clients and servers.
72 9. vfork-based exec is now working correctly
73 10. Add 'file tempfile'
74 11. Add 'socket pipe'
75 12. Enhance 'try ... on ... finally' to be more Tcl 8.6 compatible
76 13. It is now possible to 'return' from within 'try'
77 14. IPv6 support is now included
78 15. Add 'string is'
80 Since v0.61:
82 1. Add support to 'exec' for '>&', '>>&', '|&', '2>@1'
83 2. Fix 'exec' error messages when special token (e.g. '>') is the last token
84 3. Fix 'subst' handling of backslash escapes.
85 4. Allow abbreviated options for 'subst'
86 5. Add support for 'return', 'break', 'continue' in subst
87 6. Many 'expr' bug fixes
88 7. Add support for functions in 'expr' (e.g. int(), abs()), and also 'in', 'ni' list operations
89 8. The variable name argument to 'regsub' is now optional
90 9. Add support for 'unset -nocomplain'
91 10. Add support for list commands: 'lassign', 'lrepeat'
92 11. Fully-functional 'lsearch' is now implemented
93 12. Add 'info nameofexecutable' and 'info returncodes'
94 13. Allow 'catch' to determine what return codes are caught
95 14. Allow 'incr' to increment an unset variable by first setting to 0
96 15. Allow 'args' and optional arguments to the left or required arguments in 'proc'
97 16. Add 'file copy'
98 17. Add 'try ... finally' command
100 TCL INTRODUCTION
101 -----------------
102 Tcl stands for 'tool command language' and is pronounced 'tickle.'
103 It is actually two things: a language and a library.
105 First, Tcl is a simple textual language, intended primarily for
106 issuing commands to interactive programs such as text editors,
107 debuggers, illustrators, and shells.  It has a simple syntax and is also
108 programmable, so Tcl users can write command procedures to provide more
109 powerful commands than those in the built-in set.
111 Second, Tcl is a library package that can be embedded in application
112 programs.  The Tcl library consists of a parser for the Tcl language,
113 routines to implement the Tcl built-in commands, and procedures that
114 allow each application to extend Tcl with additional commands specific
115 to that application.  The application program generates Tcl commands and
116 passes them to the Tcl parser for execution.  Commands may be generated
117 by reading characters from an input source, or by associating command
118 strings with elements of the application's user interface, such as menu
119 entries, buttons, or keystrokes.
121 When the Tcl library receives commands it parses them into component
122 fields and executes built-in commands directly.  For commands implemented
123 by the application, Tcl calls back to the application to execute the
124 commands.  In many cases commands will invoke recursive invocations of the
125 Tcl interpreter by passing in additional strings to execute (procedures,
126 looping commands, and conditional commands all work in this way).
128 An application program gains three advantages by using Tcl for its command
129 language.  First, Tcl provides a standard syntax:  once users know Tcl,
130 they will be able to issue commands easily to any Tcl-based application.
131 Second, Tcl provides programmability.  All a Tcl application needs
132 to do is to implement a few application-specific low-level commands.
133 Tcl provides many utility commands plus a general programming interface
134 for building up complex command procedures.  By using Tcl, applications
135 need not re-implement these features.
137 Third, Tcl can be used as a common language for communicating between
138 applications.  Inter-application communication is not built into the
139 Tcl core described here, but various add-on libraries, such as the Tk
140 toolkit, allow applications to issue commands to each other.  This makes
141 it possible for applications to work together in much more powerful ways
142 than was previously possible.
144 This manual page focuses primarily on the Tcl language.  It describes
145 the language syntax and the built-in commands that will be available
146 in any application based on Tcl.  The individual library procedures are
147 described in more detail in separate manual pages, one per procedure.
149 INTERPRETERS
150 ------------
151 The central data structure in Tcl is an interpreter (C type 'Jim_Interp').
152 An interpreter consists of a set of command bindings, a set of variable
153 values, and a few other miscellaneous pieces of state.  Each Tcl command
154 is interpreted in the context of a particular interpreter.
156 Some Tcl-based applications will maintain multiple interpreters
157 simultaneously, each associated with a different widget or portion of
158 the application.  Interpreters are relatively lightweight structures.
159 They can be created and deleted quickly, so application programmers should
160 feel free to use multiple interpreters if that simplifies the application.
162 DATA TYPES
163 ----------
164 Tcl supports only one type of data:  strings.  All commands, all arguments
165 to commands, all command results, and all variable values are strings.
167 Where commands require numeric arguments or return numeric results,
168 the arguments and results are passed as strings.  Many commands expect
169 their string arguments to have certain formats, but this interpretation
170 is up to the individual commands.  For example, arguments often contain
171 Tcl command strings, which may get executed as part of the commands.
172 The easiest way to understand the Tcl interpreter is to remember that
173 everything is just an operation on a string.  In many cases Tcl constructs
174 will look similar to more structured constructs from other languages.
175 However, the Tcl constructs are not structured at all; they are just
176 strings of characters, and this gives them a different behavior than
177 the structures they may look like.
179 Although the exact interpretation of a Tcl string depends on who is doing
180 the interpretation, there are three common forms that strings take:
181 commands, expressions, and lists.  The major sections below discuss
182 these three forms in more detail.
184 BASIC COMMAND SYNTAX
185 --------------------
186 The Tcl language has syntactic similarities to both the Unix shells
187 and Lisp.  However, the interpretation of commands is different
188 in Tcl than in either of those other two systems.
189 A Tcl command string consists of one or more commands separated
190 by newline characters or semi-colons.
191 Each command consists of a collection of fields separated by
192 white space (spaces or tabs).
193 The first field must be the name of a command, and the
194 additional fields, if any, are arguments that will be passed to
195 that command.  For example, the command:
197     set a 22
199 has three fields:  the first, 'set', is the name of a Tcl command, and
200 the last two, 'a' and '22', will be passed as arguments to
201 the 'set' command.  The command name may refer either to a built-in
202 Tcl command, an application-specific command bound in with the library
203 procedure 'Jim_CreateCommand', or a command procedure defined with the
204 'proc' built-in command.
206 Arguments are passed literally as text strings.  Individual commands may
207 interpret those strings in any fashion they wish.  The 'set' command,
208 for example, will treat its first argument as the name of a variable
209 and its second argument as a string value to assign to that variable.
210 For other commands arguments may be interpreted as integers, lists,
211 file names, or Tcl commands.
213 Command names should normally be typed completely (e.g. no abbreviations).
214 However, if the Tcl interpreter cannot locate a command it invokes a
215 special command named 'unknown' which attempts to find or create the
216 command.
218 For example, at many sites 'unknown' will search through library
219 directories for the desired command and create it as a Tcl procedure if
220 it is found.  The 'unknown' command often provides automatic completion
221 of abbreviated commands, but usually only for commands that were typed
222 interactively.
224 It's probably a bad idea to use abbreviations in command scripts and
225 other forms that will be re-used over time:  changes to the command set
226 may cause abbreviations to become ambiguous, resulting in scripts that
227 no longer work.
229 COMMENTS
230 --------
231 If the first non-blank character in a command is '\#', then everything
232 from the '\#' up through the next newline character is treated as
233 a comment and ignored.  When comments are embedded inside nested
234 commands (e.g. fields enclosed in braces) they must have properly-matched
235 braces (this is necessary because when Tcl parses the top-level command
236 it doesn't yet know that the nested field will be used as a command so
237 it cannot process the nested comment character as a comment).
239 GROUPING ARGUMENTS WITH DOUBLE-QUOTES
240 -------------------------------------
241 Normally each argument field ends at the next white space, but
242 double-quotes may be used to create arguments with embedded space.
244 If an argument field begins with a double-quote, then the argument isn't
245 terminated by white space (including newlines) or a semi-colon (see below
246 for information on semi-colons); instead it ends at the next double-quote
247 character.  The double-quotes are not included in the resulting argument.
248 For example, the command
250     set a "This is a single argument"
252 will pass two arguments to 'set':  'a' and 'This is a single argument'.
254 Within double-quotes, command substitutions, variable substitutions,
255 and backslash substitutions still occur, as described below.  If the
256 first character of a command field is not a quote, then quotes receive
257 no special interpretation in the parsing of that field.
259 GROUPING ARGUMENTS WITH BRACES
260 ------------------------------
261 Curly braces may also be used for grouping arguments.  They are similar
262 to quotes except for two differences.  First, they nest; this makes them
263 easier to use for complicated arguments like nested Tcl command strings.
264 Second, the substitutions described below for commands, variables, and
265 backslashes do *not* occur in arguments enclosed in braces, so braces
266 can be used to prevent substitutions where they are undesirable.
268 If an argument field begins with a left brace, then the argument ends
269 at the matching right brace.  Tcl will strip off the outermost layer
270 of braces and pass the information between the braces to the command
271 without any further modification.  For example, in the command
273     set a {xyz a {b c d}}
275 the 'set' command will receive two arguments: 'a'
276 and 'xyz a {b c d}'.
278 When braces or quotes are in effect, the matching brace or quote need
279 not be on the same line as the starting quote or brace; in this case
280 the newline will be included in the argument field along with any other
281 characters up to the matching brace or quote.  For example, the 'eval'
282 command takes one argument, which is a command string; 'eval' invokes
283 the Tcl interpreter to execute the command string.  The command
285     eval {
286       set a 22
287       set b 33
288     }
290 will assign the value '22' to 'a' and '33' to 'b'.
292 If the first character of a command field is not a left
293 brace, then neither left nor right
294 braces in the field will be treated specially (except as part of
295 variable substitution; see below).
297 COMMAND SUBSTITUTION WITH BRACKETS
298 ----------------------------------
299 If an open bracket occurs in a field of a command, then command
300 substitution occurs (except for fields enclosed in braces).  All of the
301 text up to the matching close bracket is treated as a Tcl command and
302 executed immediately.  Then the result of that command is substituted
303 for the bracketed text.  For example, consider the command
305     set a [set b]
307 When the 'set' command has only a single argument, it is the name of a
308 variable and 'set' returns the contents of that variable.  In this case,
309 if variable 'b' has the value 'foo', then the command above is equivalent
310 to the command
312     set a foo
314 Brackets can be used in more complex ways.  For example, if the variable
315 'b' has the value 'foo' and the variable 'c' has the value 'gorp',
316 then the command
318     set a xyz[set b].[set c]
320 is equivalent to the command
322     set a xyzfoo.gorp
325 A bracketed command may contain multiple commands separated by newlines
326 or semi-colons in the usual fashion.  In this case the value of the last
327 command is used for substitution.  For example, the command
329     set a x[set b 22
330     expr $b+2]x
332 is equivalent to the command
334     set a x24x
337 If a field is enclosed in braces then the brackets and the characters
338 between them are not interpreted specially; they are passed through to
339 the argument verbatim.
341 VARIABLE SUBSTITUTION WITH $
342 ----------------------------
343 The dollar sign ('$') may be used as a special shorthand form for
344 substituting variable values.  If '$' appears in an argument that isn't
345 enclosed in braces then variable substitution will occur.  The characters
346 after the '$', up to the first character that isn't a number, letter,
347 or underscore, are taken as a variable name and the string value of that
348 variable is substituted for the name.
350 For example, if variable 'foo' has the value 'test', then the command
352     set a $foo.c
354 is equivalent to the command
356     set a test.c
358 There are two special forms for variable substitution.  If the next
359 character after the name of the variable is an open parenthesis, then
360 the variable is assumed to be an array name, and all of the characters
361 between the open parenthesis and the next close parenthesis are taken as
362 an index into the array.  Command substitutions and variable substitutions
363 are performed on the information between the parentheses before it is
364 used as an index.
366 For example, if the variable 'x' is an array with one element named
367 'first' and value '87' and another element named '14' and value 'more',
368 then the command
370     set a xyz$x(first)zyx
372 is equivalent to the command
374     set a xyz87zyx
376 If the variable 'index' has the value '14', then the command
378     set a xyz$x($index)zyx
380 is equivalent to the command
382     set a xyzmorezyx
384 For more information on arrays, see VARIABLES AND ARRAYS below.
386 The second special form for variables occurs when the dollar sign is
387 followed by an open curly brace.  In this case the variable name consists
388 of all the characters up to the next curly brace.
390 Array references are not possible in this form:  the name between braces
391 is assumed to refer to a scalar variable.  For example, if variable
392 'foo' has the value 'test', then the command
394     set a abc${foo}bar
396 is equivalent to the command
398     set a abctestbar
401 Variable substitution does not occur in arguments that are enclosed in
402 braces:  the dollar sign and variable name are passed through to the
403 argument verbatim.
405 The dollar sign abbreviation is simply a shorthand form.  '$a' is
406 completely equivalent to '[set a]'; it is provided as a convenience
407 to reduce typing.
409 SEPARATING COMMANDS WITH SEMI-COLONS
410 ------------------------------------
411 Normally, each command occupies one line (the command is terminated by a
412 newline character).  However, semi-colon (';') is treated as a command
413 separator character; multiple commands may be placed on one line by
414 separating them with a semi-colon.  Semi-colons are not treated as
415 command separators if they appear within curly braces or double-quotes.
417 BACKSLASH SUBSTITUTION
418 ----------------------
419 Backslashes may be used to insert non-printing characters into command
420 fields and also to insert special characters like braces and brackets
421 into fields without them being interpreted specially as described above.
423 The backslash sequences understood by the Tcl interpreter are
424 listed below.  In each case, the backslash
425 sequence is replaced by the given character:
426 [[BackslashSequences]]
427 +{backslash}b+::
428     Backspace (0x8)
430 +{backslash}f+::
431     Form feed (0xc)
433 +{backslash}n+::
434     Newline (0xa)
436 +{backslash}r+::
437     Carriage-return (0xd).
439 +{backslash}t+::
440     Tab (0x9).
442 +{backslash}v+::
443     Vertical tab (0xb).
445 +{backslash}{+::
446     Left brace ({).
448 +{backslash}}+::
449     Right brace (}).
451 +{backslash}[+::
452     Open bracket ([).
454 +{backslash}]+::
455     Close bracket (]).
457 +{backslash}$+::
458     Dollar sign ($).
460 +{backslash}<space>+::
461     Space ( ): doesn't terminate argument.
463 +{backslash};+::
464     Semi-colon: doesn't terminate command.
466 +{backslash}"+::
467     Double-quote.
469 +{backslash}<newline>+::
470     Nothing:  this joins two lines together
471     into a single line.  This backslash feature is unique in that
472     it will be applied even when the sequence occurs within braces.
474 +{backslash}{backslash}+::
475     Backslash ('{backslash}').
477 +{backslash}*ddd*+::
478     The digits *ddd* (one, two, or three of them) give the octal value of
479     the character.  Note that Jim supports null characters in strings.
481 For example, in the command
483     set a \{x\[\ yz\141
485 the second argument to 'set' will be '{x[ yza'.
487 If a backslash is followed by something other than one of the options
488 described above, then the backslash is transmitted to the argument
489 field without any special processing, and the Tcl scanner continues
490 normal processing with the next character.  For example, in the
491 command
493     set \*a \\\{foo
495 The first argument to 'set' will be '{backslash}*a' and the second
496 argument will be '{backslash}{foo'.
498 If an argument is enclosed in braces, then backslash sequences inside
499 the argument are parsed but no substitution occurs (except for
500 backslash-newline):  the backslash
501 sequence is passed through to the argument as is, without making
502 any special interpretation of the characters in the backslash sequence.
503 In particular, backslashed braces are not counted in locating the
504 matching right brace that terminates the argument.
505 For example, in the
506 command
508     set a {\{abc}
510 the second argument to 'set' will be '{backslash}{abc'.
512 This backslash mechanism is not sufficient to generate absolutely
513 any argument structure; it only covers the
514 most common cases.  To produce particularly complicated arguments
515 it is probably easiest to use the 'format' command along with
516 command substitution.
518 STRING AND LIST INDEX SPECIFICATIONS
519 ------------------------------------
521 Many string and list commands take one or more 'index' parameters which
522 specify a position in the string relative to the start or end of the string/list.
524 The index may be one of the following forms:
526 `integer`::
527     A simple integer, where '0' refers to the first element of the string
528     or list.
530 `integer+integer` or::
531 `integer-integer`::
532     The sum or difference of the two integers. e.g. +2+3+ refers to the 5th element.
533     This is useful when used with (e.g.) +$i+1+ rather than the more verbose
534     +[expr {$i+1\}]+
536 `end`::
537     The last element of the string or list.
539 `end-integer`::
540     The 'nth-from-last' element of the string or list.
542 COMMAND SUMMARY
543 ---------------
544 1. A command is just a string.
545 2. Within a string commands are separated by newlines or semi-colons
546    (unless the newline or semi-colon is within braces or brackets
547    or is backslashed).
548 3. A command consists of fields.  The first field is the name of the command.
549    The other fields are strings that are passed to that command as arguments.
550 4. Fields are normally separated by white space.
551 5. Double-quotes allow white space and semi-colons to appear within
552    a single argument.
553    Command substitution, variable substitution, and backslash substitution
554    still occur inside quotes.
555 6. Braces defer interpretation of special characters.
556    If a field begins with a left brace, then it consists of everything
557    between the left brace and the matching right brace. The
558    braces themselves are not included in the argument.
559    No further processing is done on the information between the braces
560    except that backslash-newline sequences are eliminated.
561 7. If a field doesn't begin with a brace then backslash,
562    variable, and command substitution are done on the field.  Only a
563    single level of processing is done:  the results of one substitution
564    are not scanned again for further substitutions or any other
565    special treatment.  Substitution can
566    occur on any field of a command, including the command name
567    as well as the arguments.
568 8. If the first non-blank character of a command is a '\#', everything
569    from the '\#' up through the next newline is treated as a comment
570    and ignored.
572 EXPRESSIONS
573 -----------
574 The second major interpretation applied to strings in Tcl is
575 as expressions.  Several commands, such as 'expr', 'for',
576 and 'if', treat one or more of their arguments as expressions
577 and call the Tcl expression processors ('Jim_ExprLong',
578 'Jim_ExprBoolean', etc.) to evaluate them.
580 The operators permitted in Tcl expressions are a subset of
581 the operators permitted in C expressions, and they have the
582 same meaning and precedence as the corresponding C operators.
583 Expressions almost always yield numeric results
584 (integer or floating-point values).
585 For example, the expression
587     8.2 + 6
589 evaluates to 14.2.
591 Tcl expressions differ from C expressions in the way that
592 operands are specified, and in that Tcl expressions support
593 non-numeric operands and string comparisons.
595 A Tcl expression consists of a combination of operands, operators,
596 and parentheses.
598 White space may be used between the operands and operators and
599 parentheses; it is ignored by the expression processor.
600 Where possible, operands are interpreted as integer values.
602 Integer values may be specified in decimal (the normal case), in octal (if the
603 first character of the operand is '0'), or in hexadecimal (if the first
604 two characters of the operand are '0x').
606 If an operand does not have one of the integer formats given
607 above, then it is treated as a floating-point number if that is
608 possible.  Floating-point numbers may be specified in any of the
609 ways accepted by an ANSI-compliant C compiler (except that the
610 'f', 'F', 'l', and 'L' suffixes will not be permitted in
611 most installations).  For example, all of the
612 following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
614 If no numeric interpretation is possible, then an operand is left
615 as a string (and only a limited set of operators may be applied to
616 it).
618 1. Operands may be specified in any of the following ways:
620 2. As a numeric value, either integer or floating-point.
622 3. As a Tcl variable, using standard '$' notation.
623 The variable's value will be used as the operand.
625 4. As a string enclosed in double-quotes.
626 The expression parser will perform backslash, variable, and
627 command substitutions on the information between the quotes,
628 and use the resulting value as the operand
630 5. As a string enclosed in braces.
631 The characters between the open brace and matching close brace
632 will be used as the operand without any substitutions.
634 6. As a Tcl command enclosed in brackets.
635 The command will be executed and its result will be used as
636 the operand.
638 Where substitutions occur above (e.g. inside quoted strings), they
639 are performed by the expression processor.
640 However, an additional layer of substitution may already have
641 been performed by the command parser before the expression
642 processor was called.
644 As discussed below, it is usually best to enclose expressions
645 in braces to prevent the command parser from performing substitutions
646 on the contents.
648 For some examples of simple expressions, suppose the variable 'a' has
649 the value 3 and the variable 'b' has the value 6.  Then the expression
650 on the left side of each of the lines below will evaluate to the value
651 on the right side of the line:
653     $a + 3.1                6.1
654     2 + "$a.$b"             5.6
655     4*[llength "6 2"]       8
656     {word one} < "word $a"  0
658 The valid operators are listed below, grouped in decreasing order
659 of precedence:
660 [[OperatorPrecedence]]
661 `int() double() round() abs()`::
662     Unary functions.
663     int() converts the numeric argument to an integer by truncating down.
664     double() converts the numeric argument to floating point.
665     round() converts the numeric argument to the closest integer value.
666     abs() takes the absolute value of the numeric argument.
668 `sin() cos() tan() asin() acos() atan() sinh() cosh() tanh() ceil() floor() exp() log() log10() sqrt()`::
669     Unary math functions.
670     If Jim is compiled with math support, these functions are available.
672 `- + ~ !`::
673     Unary minus, unary plus, bit-wise NOT, logical NOT.  None of these operands
674     may be applied to string operands, and bit-wise NOT may be
675     applied only to integers.
677 `**`::
678     Power. e.g. pow(). If Jim is compiled with math support, supports doubles and
679     integers. Otherwise supports integers only.
681 `* / %`::
682     Multiply, divide, remainder.  None of these operands may be
683     applied to string operands, and remainder may be applied only
684     to integers.
686 `+ -`::
687     Add and subtract.  Valid for any numeric operands.
689 `<<  >> <<< >>>`::
690     Left and right shift, left and right rotate.  Valid for integer operands only.
692 `<  >  \<=  >=`::
693     Boolean less, greater, less than or equal, and greater than or equal.
694     Each operator produces 1 if the condition is true, 0 otherwise.
695     These operators may be applied to strings as well as numeric operands,
696     in which case string comparison is used.
698 `==  !=`::
699     Boolean equal and not equal.  Each operator produces a zero/one result.
700     Valid for all operand types. *Note* that values will be converted to integers
701     if possible, then floating point types, and finally strings will be compared.
702     It is recommended that 'eq' and 'ne' should be used for string comparison.
704 `eq ne`::
705     String equal and not equal.  Uses the string value directly without
706     attempting to convert to a number first.
708 `in ni`::
709     String in list and not in list. For 'in', result is 1 if the left operand (as a string)
710     is contained in the right operand (as a list), or 0 otherwise. The result for
711     '{$a ni $list}' is equivalent to '{!($a in $list)}'.
713 `&`::
714     Bit-wise AND.  Valid for integer operands only.
716 `|`::
717     Bit-wise OR.  Valid for integer operands only.
719 `^`::
720     Bit-wise exclusive OR.  Valid for integer operands only.
722 `&&`::
723     Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
724     Valid for numeric operands only (integers or floating-point).
726 `||`::
727     Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
728     Valid for numeric operands only (integers or floating-point).
730 `x ? y : z`::
731     If-then-else, as in C.  If *x*
732     evaluates to non-zero, then the result is the value of *y*.
733     Otherwise the result is the value of *z*.
734     The *x* operand must have a numeric value, while *y* and *z* can
735     be of any type.
737 See the C manual for more details on the results
738 produced by each operator.
739 All of the binary operators group left-to-right within the same
740 precedence level.  For example, the expression
742     4*2 < 7
744 evaluates to 0.
746 The '&&', '||', and '?:' operators have 'lazy
747 evaluation', just as in C, 
748 which means that operands are not evaluated if they are
749 not needed to determine the outcome.  For example, in
751     $v ? [a] : [b]
753 only one of '[a]' or '[b]' will actually be evaluated,
754 depending on the value of '$v'.
756 All internal computations involving integers are done with the C
757 type 'long long' if available, or 'long' otherwise, and all internal
758 computations involving floating-point are done with the C type
759 'double'.
761 When converting a string to floating-point, exponent overflow is
762 detected and results in a Tcl error.
763 For conversion to integer from string, detection of overflow depends
764 on the behavior of some routines in the local C library, so it should
765 be regarded as unreliable.
766 In any case, overflow and underflow are generally not detected
767 reliably for intermediate results.
769 Conversion among internal representations for integer, floating-point,
770 and string operands is done automatically as needed.
771 For arithmetic computations, integers are used until some
772 floating-point number is introduced, after which floating-point is used.
773 For example,
775     5 / 4
777 yields the result 1, while
779     5 / 4.0
780     5 / ( [string length "abcd"] + 0.0 )
782 both yield the result 1.25.
784 String values may be used as operands of the comparison operators,
785 although the expression evaluator tries to do comparisons as integer
786 or floating-point when it can.
787 If one of the operands of a comparison is a string and the other
788 has a numeric value, the numeric operand is converted back to
789 a string using the C 'sprintf' format specifier
790 '%d' for integers and '%g' for floating-point values.
791 For example, the expressions
793     "0x03" > "2"
794     "0y" < "0x12"
796 both evaluate to 1.  The first comparison is done using integer
797 comparison, and the second is done using string comparison after
798 the second operand is converted to the string '18'.
800 In general it is safest to enclose an expression in braces when
801 entering it in a command:  otherwise, if the expression contains
802 any white space then the Tcl interpreter will split it
803 among several arguments.  For example, the command
805     expr $a + $b
807 results in three arguments being passed to 'expr':  '$a',
808 '+', and '$b'.  In addition, if the expression isn't in braces
809 then the Tcl interpreter will perform variable and command substitution
810 immediately (it will happen in the command parser rather than in
811 the expression parser).  In many cases the expression is being
812 passed to a command that will evaluate the expression later (or
813 even many times if, for example, the expression is to be used to
814 decide when to exit a loop).  Usually the desired goal is to re-do
815 the variable or command substitutions each time the expression is
816 evaluated, rather than once and for all at the beginning.  For example,
817 the command
819     for {set i 1} $i<=10 {incr i} {...}        *** WRONG ***
821 is probably intended to iterate over all values of `i` from 1 to 10.
822 After each iteration of the body of the loop, 'for' will pass
823 its second argument to the expression evaluator to see whether or not
824 to continue processing.  Unfortunately, in this case the value of `i`
825 in the second argument will be substituted once and for all when the
826 'for' command is parsed.  If `i` was 0 before the 'for'
827 command was invoked then for's second argument will be `0\<=10`
828 which will always evaluate to 1, even though `i` eventually
829 becomes greater than 10.  In the above case the loop will never
830 terminate.  Instead, the expression should be placed in braces:
832     for {set i 1} {$i<=10} {incr i} {...}      *** RIGHT ***
834 This causes the substitution of 'i'
835 to be delayed; it will be re-done each time the expression is
836 evaluated, which is the desired result.
838 LISTS
839 -----
840 The third major way that strings are interpreted in Tcl is as lists.
841 A list is just a string with a list-like structure
842 consisting of fields separated by white space.  For example, the
843 string
845     Al Sue Anne John
847 is a list with four elements or fields.
848 Lists have the same basic structure as command strings, except
849 that a newline character in a list is treated as a field separator
850 just like space or tab.  Conventions for braces and quotes
851 and backslashes are the same for lists as for commands.  For example,
852 the string
854     a b\ c {d e {f g h}}
856 is a list with three elements:  'a', 'b c', and 'd e {f g h}'.
858 Whenever an element is extracted from a list, the same rules about
859 braces and quotes and backslashes are applied as for commands.  Thus in
860 the example above when the third element is extracted from the list,
861 the result is
863     d e {f g h}
865 (when the field was extracted, all that happened was to strip off
866 the outermost layer of braces).  Command substitution and
867 variable substitution are never
868 made on a list (at least, not by the list-processing commands; the
869 list can always be passed to the Tcl interpreter for evaluation).
871 The Tcl commands 'concat', 'foreach', 'lappend', 'lindex', 'linsert',
872 'list', 'llength', 'lrange', 'lreplace', 'lsearch', and 'lsort' allow
873 you to build lists, extract elements from them, search them, and perform
874 other list-related functions.
876 Advanced list commands include 'lrepeat', 'lreverse', 'lmap', 'lassign', 'lset'.
878 LIST EXPANSION
879 --------------
881 A new addition to Tcl 8.5 is the ability to expand a list into separate
882 arguments. Support for this feature is also available in Jim.
884 Consider the following attempt to exec a list:
886     set cmd {ls -l}
887     exec $cmd
889 This will attempt to exec the a command named "ls -l", which will clearly not
890 work. Typically eval and concat are required to solve this problem, however
891 it can be solved much more easily with '\{*\}'.
893     exec {*}$cmd
895 This will expand the following argument into individual elements and then evaluate
896 the resulting command.
898 Note that the official Tcl syntax is '\{*\}', however '\{expand\}' is retained
899 for backward compatibility with experimental versions of this feature.
901 REGULAR EXPRESSIONS
902 -------------------
903 Tcl provides two commands that support string matching using
904 'egrep'-style regular expressions: 'regexp' and 'regsub'.
906 Regular expressions are implemented using the system's C library as
907 Extended Regular Expressions (EREs) rather than Basic Regular Expressions (BREs).
909 See regex(3) and regex(7) for full details.
911 *NOTE* Tcl 7.x and 8.x use perl-style Advanced Regular Expressions (AREs).
913 COMMAND RESULTS
914 ---------------
915 Each command produces two results:  a code and a string.  The
916 code indicates whether the command completed successfully or not,
917 and the string gives additional information.  The valid codes are
918 defined in jim.h, and are:
920 +JIM_OK(0)+::
921     This is the normal return code, and indicates that the command completed
922     successfully.  The string gives the command's return value.
924 +JIM_ERR(1)+::
925     Indicates that an error occurred; the string gives a message describing
926     the error.
928 +JIM_RETURN(2)+::
929     Indicates that the 'return' command has been invoked, and that the
930     current procedure (or top-level command or 'source' command)
931     should return immediately.  The
932     string gives the return value for the procedure or command.
934 +JIM_BREAK(3)+::
935     Indicates that the 'break' command has been invoked, so the
936     innermost loop should abort immediately.  The string should always
937     be empty.
939 +JIM_CONTINUE(4)+::
940     Indicates that the 'continue' command has been invoked, so the
941     innermost loop should go on to the next iteration.  The string
942     should always be empty.
944 +JIM_SIGNAL(5)+::
945     Indicates that a signal was caught while executing a commands.
946     The string contains the name of the signal caught.
947     See the 'signal' and 'catch' commands.
949 +JIM_EXIT(6)+::
950     Indicates that the command called the 'exit' command.
951     The string contains the exit code.
953 Tcl programmers do not normally need to think about return codes,
954 since +JIM_OK+ is almost always returned.  If anything else is returned
955 by a command, then the Tcl interpreter immediately stops processing
956 commands and returns to its caller.  If there are several nested
957 invocations of the Tcl interpreter in progress, then each nested
958 command will usually return the error to its caller, until eventually
959 the error is reported to the top-level application code.  The
960 application will then display the error message for the user.
962 In a few cases, some commands will handle certain 'error' conditions
963 themselves and not return them upwards.  For example, the 'for'
964 command checks for the +JIM_BREAK+ code; if it occurs, then 'for'
965 stops executing the body of the loop and returns +JIM_OK+ to its
966 caller.  The 'for' command also handles +JIM_CONTINUE+ codes and the
967 procedure interpreter handles +JIM_RETURN+ codes.  The 'catch'
968 command allows Tcl programs to catch errors and handle them without
969 aborting command interpretation any further.
971 The 'info returncodes' command may be used to programmatically map between
972 return codes and names.
974 PROCEDURES
975 ----------
976 Tcl allows you to extend the command interface by defining
977 procedures.  A Tcl procedure can be invoked just like any other Tcl
978 command (it has a name and it receives one or more arguments).
979 The only difference is that its body isn't a piece of C code linked
980 into the program; it is a string containing one or more other
981 Tcl commands.
983 The 'proc' command is used to create a new Tcl command procedure:
985 +*proc* 'name args ?statics? body'+
987 The new command is name *name*, and it replaces any existing command
988 there may have been by that name. Whenever the new command is
989 invoked, the contents of *body* will be executed by the Tcl
990 interpreter.
992 *args* specifies the formal arguments to the procedure.
993 It consists of a list, possibly empty, of the following
994 argument specifiers:
996 +name+::
997     Required Argument - A simple argument name.
999 +name default+::
1000     Optional Argument - A two-element list consisting of the
1001     argument name, followed by the default value, which will
1002     be used if the corresponding argument is not supplied.
1004 +*args*+::
1005     Variable Argument - The special name 'args', which is
1006     assigned all remaining arguments (including none). The
1007     variable argument may only be specified once.
1009 Arguments must be provided in the following order, any of which
1010 may be omitted:
1012 1. Required Arguments (Left)
1013 2. Optional Arguments
1014 3. Variable Argument
1015 4. Required Arguments (Right)
1017 When the command is invoked, a local variable will be created for each of
1018 the formal arguments to the procedure; its value will be the value
1019 of corresponding argument in the invoking command or the argument's
1020 default value.
1022 Arguments with default values need not be specified in a procedure
1023 invocation.  However, there must be enough actual arguments for all
1024 required arguments, and there must not be any extra actual arguments
1025 (unless the Variable Argument is specified).
1027 Actual arguments are assigned to formal arguments as follows:
1029 1. Left Required Arguments are assigned from the left
1030 2. Right Required Arguments are assigned from the right
1031 3. Default Arguments are assigned from the left, following the Left Required Arguments.
1032 4. A list is formed from any remaining arguments, which are then
1033    are assigned to the 'args' Variable Argument (if specified). The list will be empty
1034    if there are no remaining arguments.
1036 When *body* is being executed, variable names normally refer to local
1037 variables, which are created automatically when referenced and deleted
1038 when the procedure returns.  One local variable is automatically created
1039 for each of the procedure's arguments.  Global variables can be
1040 accessed by invoking the 'global' command or via the '::' prefix.
1042 *New in Jim*
1044 In addition to procedure arguments, Jim procedures may declare static variables.
1045 These variables scoped to the procedure and initialised at procedure definition.
1046 Either from the static variable definition, or from the enclosing scope.
1048 Consider the following example:
1050     jim> set a 1
1051     jim> proc a {} {a {b 2}} {
1052         set c 1
1053         puts "$a $b $c"
1054         incr a
1055         incr b
1056         incr c
1057     }
1058     jim> a
1059     1 2 1
1060     jim> a
1061     2 3 1
1063 The static variable *a* has no initialiser, so it is initialised from
1064 the enclosing scope with the value 1. (Note that it is an error if there
1065 is no variable with the same name in the enclosing scope). However *b*
1066 has an initialiser, so it is initialised to 2.
1068 Unlike a local variable, the value of a static variable is retained across
1069 invocations of the procedure.
1070     
1071 See the 'proc' command for information on
1072 how to define procedures and what happens when they are invoked.
1074 VARIABLES - SCALARS AND ARRAYS
1075 ------------------------------
1076 Tcl allows the definition of variables and the use of their values
1077 either through '$'-style variable substitution, the 'set'
1078 command, or a few other mechanisms.
1080 Variables need not be declared:  a new variable will automatically
1081 be created each time a new variable name is used.
1083 Tcl supports two types of variables:  scalars and arrays.
1084 A scalar variable has a single value, whereas an array variable
1085 can have any number of elements, each with a name (called
1086 its 'index') and a value.
1088 Array indexes may be arbitrary strings; they need not be numeric.
1089 Parentheses are used refer to array elements in Tcl commands.
1090 For example, the command
1092     set x(first) 44
1094 will modify the element of 'x' whose index is 'first'
1095 so that its new value is '44'.
1097 Two-dimensional arrays can be simulated in Tcl by using indexes
1098 that contain multiple concatenated values.
1099 For example, the commands
1101     set a(2,3) 1
1102     set a(3,6) 2
1104 set the elements of 'a' whose indexes are '2,3' and '3,6'.
1106 In general, array elements may be used anywhere in Tcl that scalar
1107 variables may be used.
1109 If an array is defined with a particular name, then there may
1110 not be a scalar variable with the same name.
1112 Similarly, if there is a scalar variable with a particular
1113 name then it is not possible to make array references to the
1114 variable.
1116 To convert a scalar variable to an array or vice versa, remove
1117 the existing variable with the 'unset' command.
1119 The 'array' command provides several features for dealing
1120 with arrays, such as querying the names of all the elements of
1121 the array and converting between an array and a list.
1123 Variables may be either global or local.  If a variable
1124 name is used when a procedure isn't being executed, then it
1125 automatically refers to a global variable.  Variable names used
1126 within a procedure normally refer to local variables associated with that
1127 invocation of the procedure.  Local variables are deleted whenever
1128 a procedure exits.  Either 'global' command may be used to request
1129 that a name refer to a global variable for the duration of the current
1130 procedure (this is somewhat analogous to 'extern' in C), or the variable
1131 may be explicitly scoped with the '::' prefix. For example
1133     set a 1
1134     set b 2
1135     proc p {} {
1136         set c 3
1137         global a
1139         puts "$a $::b $c"
1140     }
1141     p
1143 will output:
1145     1 2 3
1147 ARRAYS AS LISTS IN JIM
1148 ----------------------
1149 Unlike Tcl, Jim can automatically convert between a list (with an even
1150 number of elements) and an array value. This is similar to the way Tcl
1151 can convert between a string and a list.
1153 For example:
1155   set a {1 one 2 two}
1156   puts $a(2)
1158 will output:
1160   two
1162 Thus 'array set' is equivalent to 'set' when the variable does not
1163 exist or is empty.
1165 The reverse is also true where an array will be converted into
1166 a list.
1168   set a(1) one; set a(2) two
1169   puts $a
1171 will output:
1173   1 one 2 two
1175 DICTIONARY VALUES
1176 -----------------
1177 Tcl 8.5 introduced the dict command, and Jim Tcl has added a version
1178 of this command. Dictionaries provide efficient access to key-value
1179 pairs, just like arrays, but dictionaries are pure values. This
1180 means that you can pass them to a procedure just as a list or a
1181 string. Tcl dictionaries are therefore much more like Tcl lists,
1182 except that they represent a mapping from keys to values, rather
1183 than an ordered sequence.
1185 You can nest dictionaries, so that the value for a particular key
1186 consists of another dictionary. That way you can elegantly build
1187 complicated data structures, such as hierarchical databases. You
1188 can also combine dictionaries with other Tcl data structures. For
1189 instance, you can build a list of dictionaries that themselves
1190 contain lists.
1192 Dictionaries are values that contain an efficient, order-preserving
1193 mapping from arbitrary keys to arbitrary values. Each key in the
1194 dictionary maps to a single value. They have a textual format that
1195 is exactly that of any list with an even number of elements, with
1196 each mapping in the dictionary being represented as two items in
1197 the list. When a command takes a dictionary and produces a new
1198 dictionary based on it (either returning it or writing it back into
1199 the variable that the starting dictionary was read from) the new
1200 dictionary will have the same order of keys, modulo any deleted
1201 keys and with new keys added on to the end. When a string is
1202 interpreted as a dictionary and it would otherwise have duplicate
1203 keys, only the last value for a particular key is used; the others
1204 are ignored, meaning that, "apple banana" and "apple carrot apple
1205 banana" are equivalent dictionaries (with different string
1206 representations).
1208 Note that in Jim, arrays are implemented as dictionaries.
1209 Thus automatic conversion between lists and dictionaries applies
1210 as it does for arrays.
1212   jim> dict set a 1 one
1213   1 one
1214   jim> dict set a 2 two
1215   1 one 2 two
1216   jim> puts $a
1217   1 one 2 two
1218   jim> puts $a(2)
1219   two
1220   jim> dict set a 3 T three
1221   1 one 2 two 3 {T three}
1223 See the 'dict' command for more details.
1225 GARBAGE COLLECTION, REFERENCES, LAMBDA
1226 --------------------------------------
1227 Unlike Tcl, Jim has some sophistocated support for functional programming.
1228 These are described briefly below.
1230 More information may be found at http://wiki.tcl.tk/13847
1232 References
1233 ~~~~~~~~~~
1234 A reference can be thought of as holding a value with one level of indirection,
1235 where the value may be garbage collected when unreferenced.
1236 Consider the following example:
1238     jim> set r [ref "One String" test]
1239     <reference.<test___>.00000000000000000000>
1240     jim> getref $r
1241     One String
1243 The operation 'ref' creates a references to the value specfied by the
1244 first argument. (The second argument is a "type" used for documentation purposes).
1246 The operation 'getref' is the dereferencing operation which retrieves the value
1247 stored in the reference.
1249     jim> setref $r "New String"
1250     New String
1251     jim> getref $r
1252     New String
1254 The operation 'setref' replaces the value stored by the reference. If the old value
1255 is no longer accessible by any reference, it will eventually be automatically be garbage
1256 collected.
1258 Garbage Collection
1259 ~~~~~~~~~~~~~~~~~~
1260 Normally, all values in Tcl are passed by value. As such values are copied and released
1261 automatically as necessary.
1263 With the introduction of references, it is possible to create values whose lifetime
1264 transcend their scope. To support this, case, the Jim system will periodically identify
1265 and discard objects which are no longer accessible by any reference.
1267 The 'collect' command may be used to force garbage collection.  Consider a reference created
1268 with a finalizer:
1270     jim> proc f {ref value} { puts "Finaliser called for $ref,$value" }
1271     jim> set r [ref "One String" test f]
1272     <reference.<test___>.00000000000
1273     jim> collect
1274     0
1275     jim> set r ""
1276     jim> collect
1277     Finaliser called for <reference.<test___>.00000000000,One String
1278     1
1280 Note that once the reference, 'r', was modified so that it no longer
1281 contained a reference to the value, the garbage collector discarded
1282 the value (after calling the finalizer).
1284 The finalizer for a reference may be examined or changed with the 'finalize' command
1286     jim> finalize $r
1287     f
1288     jim> finalize $r newf
1289     newf
1291 Lambda
1292 ~~~~~~
1293 Jim provides a garbage collected lambda function. This is a procedure
1294 which is able to create an anonymous procedure.  Consider:
1296     jim> set f [lambda {a} {{x 0}} { incr x $a }]
1297     jim> $f 1
1298     1
1299     jim> $f 2
1300     3
1301     jim> set f ""
1303 This create an anonymous procedure (with the name stored in 'f'), with a static variable
1304 which is incremented by the supplied value and the result returned.
1306 Once the procedure name is no longer accessible, it will automatically be deleted
1307 when the garbage collector runs.
1309 The procedure may also be delete immediately by renaming it "". e.g.
1311     jim> rename $f ""
1313 BUILT-IN COMMANDS
1314 -----------------
1315 The Tcl library provides the following built-in commands, which will
1316 be available in any application using Tcl.  In addition to these
1317 built-in commands, there may be additional commands defined by each
1318 application, plus commands defined as Tcl procedures.
1320 In the command syntax descriptions below, words in +*boldface*+ are
1321 literals that you type verbatim to Tcl.
1323 Words in +'italics'+ are meta-symbols; they serve as names for any of
1324 a range of values that you can type.
1326 Optional arguments or groups of arguments are indicated by enclosing them
1327 in +?question-marks?+.
1329 Ellipses (+...+) indicate that any number of additional
1330 arguments or groups of arguments may appear, in the same format
1331 as the preceding argument(s).
1333 [[CommandIndex]]
1334 Command Index
1335 ~~~~~~~~~~~~~
1336 // @INSERTINDEX@
1338 alarm
1339 ~~~~~
1340 +*alarm* 'seconds'+
1342 Delivers the 'SIGALRM' signal to the process after the given
1343 number of seconds. If the platform supports 'ularm(3)' then
1344 the argument may be a floating point value. Otherwise it must
1345 be an integer.
1347 Note that unless a signal handler for 'SIGALRM' has been installed
1348 (see 'signal'), the process will exit on this signal.
1350 alias
1351 ~~~~~
1352 +*alias* 'name args...'+
1354 Creates a single word alias (proc) for one or more words. For example,
1355 the following creates an alias for the command 'info exists'.
1357     alias e info exists
1358     if {[e var]} {
1359       ...
1360     }
1362 'alias' returns *name*, allowing it to be used with 'local.
1364 See also 'proc', 'curry', 'lambda', 'local'.
1366 append
1367 ~~~~~~
1368 +*append* 'varName value ?value value ...?'+
1370 Append all of the *value* arguments to the current value
1371 of variable *varName*.  If *varName* doesn't exist,
1372 it is given a value equal to the concatenation of all the
1373 *value* arguments.
1375 This command provides an efficient way to build up long
1376 variables incrementally.
1377 For example, 'append a $b' is much more efficient than
1378 'set a $a$b' if '$a' is long.
1380 array
1381 ~~~~~
1382 +*array* 'option arrayName ?arg arg ...?'+
1384 This command performs one of several operations on the
1385 variable given by *arrayName*.
1387 Note that in general, if the named array does not exist, the *array* command behaves
1388 as though the array exists but is empty.
1390 The *option* argument determines what action is carried out by the
1391 command.  The legal *options* (which may be abbreviated) are:
1393 +*array exists* 'arrayName'+::
1394     Returns 1 if arrayName is an array variable, 0 if there is
1395     no variable by that name.  This command is essentially
1396     identical to 'info exists'
1398 +*array get* 'arrayName ?pattern?'+::
1399     Returns a list containing pairs of elements. The first
1400     element in each pair is the name of an element in arrayName
1401     and the second element of each pair is the value of the
1402     array element. The order of the pairs is undefined. If
1403     pattern is not specified, then all of the elements of the
1404     array are included in the result. If pattern is specified,
1405     then only those elements whose names match pattern (using
1406     the matching rules of string match) are included. If arrayName
1407     isn't the name of an array variable, or if the array contains
1408     no elements, then an empty list is returned.
1410 +*array names* 'arrayName ?pattern?'+::
1411     Returns a list containing the names of all of the elements
1412     in the array that match pattern. If pattern is omitted then
1413     the command returns all of the element names in the array.
1414     If pattern is specified, then only those elements whose
1415     names match pattern (using the matching rules of string
1416     match) are included. If there are no (matching) elements
1417     in the array, or if arrayName isn't the name of an array
1418     variable, then an empty string is returned.
1420 +*array set* 'arrayName list'+::
1421     Sets the values of one or more elements in arrayName. list
1422     must have a form like that returned by array get, consisting
1423     of an even number of elements. Each odd-numbered element
1424     in list is treated as an element name within arrayName, and
1425     the following element in list is used as a new value for
1426     that array element. If the variable arrayName does not
1427     already exist and list is empty, arrayName is created with
1428     an empty array value.
1430 +*array size* 'arrayName'+::
1431     Returns the number of elements in the array. If arrayName
1432     isn't the name of an array then 0 is returned.
1434 +*array unset* 'arrayName ?pattern?'+::
1435     Unsets all of the elements in the array that match pattern
1436     (using the matching rules of string match). If arrayName
1437     isn't the name of an array variable or there are no matching
1438     elements in the array, no error will be raised. If pattern
1439     is omitted and arrayName is an array variable, then the
1440     command unsets the entire array. The command always returns
1441     an empty string.
1443 break
1444 ~~~~~
1445 +*break*+
1447 This command may be invoked only inside the body of a loop command
1448 such as 'for' or 'foreach' or 'while'.  It returns a +JIM_BREAK+ code
1449 to signal the innermost containing loop command to return immediately.
1451 case
1452 ~~~~
1453 +*case* 'string' ?*in*? 'patList body ?patList body ...?'+
1455 +*case* 'string' ?*in*? {'patList body ?patList body ...?'}+
1457 *Note* that the switch command should generally be preferred unless compatibility
1458 with Tcl 6.x is desired.
1460 Match *string* against each of the *patList* arguments
1461 in order.  If one matches, then evaluate the following *body* argument
1462 by passing it recursively to the Tcl interpreter, and return the result
1463 of that evaluation.  Each *patList* argument consists of a single
1464 pattern or list of patterns.  Each pattern may contain any of the wild-cards
1465 described under 'string match'.
1467 If a *patList* argument is 'default', the corresponding body will be
1468 evaluated if no *patList* matches *string*.  If no *patList* argument
1469 matches *string* and no default is given, then the 'case' command returns
1470 an empty string.
1472 Two syntaxes are provided.
1474 The first uses a separate argument for each of the patterns and commands;
1475 this form is convenient if substitutions are desired on some of the
1476 patterns or commands.
1478 The second form places all of the patterns and commands together into
1479 a single argument; the argument must have proper list structure, with
1480 the elements of the list being the patterns and commands.
1482 The second form makes it easy to construct multi-line case commands,
1483 since the braces around the whole list make it unnecessary to include a
1484 backslash at the end of each line.
1486 Since the *patList* arguments are in braces in the second form,
1487 no command or variable substitutions are performed on them;  this makes
1488 the behavior of the second form different than the first form in some
1489 cases.
1491 Below are some examples of 'case' commands:
1493     case abc in {a b} {format 1} default {format 2} a* {format 3}
1495 will return '3', 
1497     case a in {
1498         {a b} {format 1}
1499         default {format 2}
1500         a* {format 3}
1501     }
1503 will return '1', and
1505     case xyz {
1506         {a b}
1507             {format 1}
1508         default
1509             {format 2}
1510         a*
1511             {format 3}
1512     }
1514 will return '2'.
1516 catch
1517 ~~~~~
1518 +*catch* '?-?no?code ...?' *?--?* 'command ?resultVarName? ?optionsVarName?'+
1520 The 'catch' command may be used to prevent errors from aborting
1521 command interpretation.  'Catch' evalues *command*, and returns a
1522 +JIM_OK+ code, regardless of any errors that might occur while
1523 executing *command* (with the possible exception of +JIM_SIGNAL+ -
1524 see below).
1526 The return value from 'catch' is a decimal string giving the code
1527 returned by the Tcl interpreter after executing *command*.  This
1528 will be '0' (+JIM_OK+) if there were no errors in *command*; otherwise
1529 it will have a non-zero value corresponding to one of the exceptional
1530 return codes (see jim.h for the definitions of code values, or the
1531 'info returncodes' command).
1533 If the *resultVarName* argument is given, then it gives the name
1534 of a variable; 'catch' will set the value of the variable to the
1535 string returned from *command* (either a result or an error message).
1537 If the *optionsVarName* argument is given, then it gives the name
1538 of a variable; 'catch' will set the value of the variable to a
1539 dictionary. For any return code other than +JIM_RETURN+, the value
1540 for the key +-code+ will be set to the return code. For +JIM_RETURN+
1541 it will be set to the code given in 'return -code'. Additionally,
1542 for the return code +JIM_ERR+, the value of the key +-errorinfo+
1543 will contain the current stack trace (the same result as 'info
1544 stacktrace') and the value of the key +-level+ will be the current
1545 return level (see 'return -level').  This can be useful to rethrow an error:
1547     if {[catch {...} msg opts]} {
1548         ...maybe do something with the error...
1549                 incr opts(-level)
1550         return {*}$opts $msg
1551     }
1553 Normally 'catch' will *not* catch any of the codes +JIM_EXIT+, +JIM_EVAL+ or +JIM_SIGNAL+.
1554 The set of codes which will be caught may be modified by specifying the one more codes before
1555 *command*.
1557 e.g. To catch +JIM_EXIT+ but not +JIM_BREAK+ or +JIM_CONTINUE+
1559     catch -exit -nobreak -nocontinue -- { ... }
1561 The use of +--+ is optional. It signifies that no more return code options follow.
1563 Note that if a signal marked as 'signal handle' is caught with 'catch -signal', the return value
1564 (stored in *resultVarName*) is name of the signal caught.
1568 +*cd* 'dirName'+
1570 Change the current working directory to *dirName*.
1572 Returns an empty string.
1574 This command can potentially be disruptive to an application, so it may
1575 be removed in some applications.
1577 clock
1578 ~~~~~
1579 +*clock seconds*+::
1580     Returns the current time as seconds since the epoch.
1582 +*clock format* 'seconds' ?*-format* 'format?'+::
1583     Format the given time (seconds since the epoch) according to the given
1584     format. See strftime(3) for supported formats.
1585     If no format is supplied, "%c" is used.
1587 +*clock scan* 'str' *-format* 'format'+::
1588     Scan the given time string using the given format string.
1589     See strptime(3) for supported formats.
1591 close
1592 ~~~~~
1593 +*close* 'fileId'+
1595 +'fileId' *close*+
1597 Closes the file given by *fileId*.
1598 *fileId* must be the return value from a previous invocation
1599 of the 'open' command; after this command, it should not be
1600 used anymore.
1602 collect
1603 ~~~~~~~
1604 +*collect*+
1606 Normally reference garbage collection is automatically performed periodically.
1607 However it may be run immediately with the 'collect' command.
1609 See GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
1611 concat
1612 ~~~~~~
1613 +*concat* 'arg ?arg ...?'+
1615 This command treats each argument as a list and concatenates them
1616 into a single list.  It permits any number of arguments.  For example,
1617 the command
1619     concat a b {c d e} {f {g h}}
1621 will return
1623     a b c d e f {g h}
1625 as its result.
1627 continue
1628 ~~~~~~~~
1629 +*continue*+
1631 This command may be invoked only inside the body of a loop command such
1632 as 'for' or 'foreach' or 'while'.  It returns a  +JIM_CONTINUE+ code to
1633 signal the innermost containing loop command to skip the remainder of
1634 the loop's body but continue with the next iteration of the loop.
1636 curry
1637 ~~~~~
1638 +*alias* 'args...'+
1640 Similar to 'alias' except it creates an anonymous procedure (lambda) instead of
1641 a named procedure.
1643 the following creates a local, unnamed alias for the command 'info exists'.
1645     set e [local curry info exists]
1646     if {[$e var]} {
1647       ...
1648     }
1650 'curry' returns the name of the procedure.
1652 See also 'proc', 'alias', 'lambda', 'local'.
1654 dict
1655 ~~~~
1656 +*dict* 'option ?arg arg ...?'+
1658 Performs one of several operations on dictionary values.
1660 The *option* argument determines what action is carried out by the
1661 command.  The legal *options* are:
1663 *dict create* '?key value ...?'+::
1664     Create and return a new dictionary value that contains each of
1665     the key/value mappings listed as  arguments (keys and values
1666     alternating, with each key being followed by its associated
1667     value.)
1669 *dict exists* 'dictionary key ?key ...?'+::
1670     Returns a boolean value indicating whether the given key (or path
1671     of keys through a set of nested dictionaries) exists in the given
1672     dictionary value.  This returns a true value exactly when 'dict get'
1673     on that path will succeed.
1675 *dict get* 'dictionary ?key ...?'+::
1676     Given a dictionary value (first argument) and a key (second argument),
1677     this will retrieve the value for that key. Where several keys are
1678     supplied, the behaviour of the command shall be as if the result
1679     of 'dict get $dictVal $key' was passed as the first argument to
1680     dict get with the remaining arguments as second (and possibly
1681     subsequent) arguments. This facilitates lookups in nested dictionaries.
1682     If no keys are provided, dict would return a list containing pairs
1683     of elements in a man- ner similar to array get. That is, the first
1684     element of each pair would be the key and the second element would
1685     be the value for that key.  It is an error to attempt to retrieve
1686     a value for a key that is not present in the dictionary.
1688 *dict set* 'dictionaryName key ?key ...? value'+::
1689     This operation takes the *name* of a variable containing a dictionary
1690     value and places an updated dictionary value in that variable
1691     containing a mapping from the given key to the given value. When
1692     multiple keys are present, this operation creates or updates a chain
1693     of nested dictionaries.
1695 *dict unset* 'dictionaryName key ?key ...? value'+::
1696     This operation (the companion to 'dict set') takes the name of a
1697     variable containing a dictionary value and places an updated
1698     dictionary value in that variable that does not contain a mapping
1699     for the given key. Where multiple keys are present, this describes
1700     a path through nested dictionaries to the mapping to remove. At
1701     least one key must be specified, but the last key on the key-path
1702     need not exist. All other components on the path must exist.
1706 +*env* '?name? ?default?'+
1708 If *name* is supplied, returns the value of *name* from the initial
1709 environment (see getenv(3)). An error is returned if *name* does not
1710 exist in the environment, unless *default* is supplied - in which case
1711 that value is returned instead.
1713 If no arguments are supplied, returns a list of all environment variables
1714 and their values as +{name value ...}+
1716 See also the global variable '::env'
1720 +*eof* 'fileId'+
1722 +'fileId' *eof*+
1724 Returns 1 if an end-of-file condition has occurred on *fileId*,
1725 0 otherwise.
1727 *fileId* must have been the return value from a previous call to 'open',
1728 or it may be 'stdin', 'stdout', or 'stderr' to refer to one of the
1729 standard I/O channels.
1731 error
1732 ~~~~~
1733 +*error* 'message ?stacktrace?'+
1735 Returns a +JIM_ERR+ code, which causes command interpretation to be
1736 unwound.  *message* is a string that is returned to the application
1737 to indicate what went wrong.
1739 If the *stacktrace* argument is provided and is non-empty,
1740 it is used to initialize the stacktrace.
1742 This feature is most useful in conjunction with the 'catch' command:
1743 if a caught error cannot be handled successfully, *stacktrace* can be used
1744 to return a stack trace reflecting the original point of occurrence
1745 of the error:
1747     catch {...} errMsg
1748     ...
1749     error $errMsg [info stacktrace]
1751 See also 'errorInfo', 'info stacktrace', 'catch' and 'return'
1753 errorInfo
1754 ~~~~~~~~~
1755 +*errorInfo* 'error ?stacktrace?'+
1757 Returns a human-readable representation of the given error message and stack trace.
1758 Typical usage is:
1760     if {[catch {...} error]} {
1761         puts stderr [errorInfo $error [info stacktrace]]
1762         exit 1
1763     }
1765 See also 'error'.
1767 eval
1768 ~~~~
1769 +*eval* 'arg ?arg ...?'+
1771 'eval' takes one or more arguments, which together comprise a Tcl
1772 command (or collection of Tcl commands separated by newlines in the
1773 usual way).  'eval' concatenates all its arguments in the same
1774 fashion as the 'concat' command, passes the concatenated string to the
1775 Tcl interpreter recursively, and returns the result of that
1776 evaluation (or any error generated by it).
1778 exec
1779 ~~~~
1780 +*exec* 'arg ?arg ...?'+
1782 This command treats its arguments as the specification
1783 of one or more UNIX commands to execute as subprocesses.
1784 The commands take the form of a standard shell pipeline;
1785 '|' arguments separate commands in the
1786 pipeline and cause standard output of the preceding command
1787 to be piped into standard input of the next command (or '|&' for
1788 both standard output and standard error).
1790 Under normal conditions the result of the 'exec' command
1791 consists of the standard output produced by the last command
1792 in the pipeline.
1794 If any of the commands in the pipeline exit abnormally or
1795 are killed or suspended, then 'exec' will return an error
1796 and the error message will include the pipeline's output followed by
1797 error messages describing the abnormal terminations.
1799 If any of the commands writes to its standard error file,
1800 then 'exec' will return an error, and the error message
1801 will include the pipeline's output, followed by messages
1802 about abnormal terminations (if any), followed by the standard error
1803 output.
1805 If the last character of the result or error message
1806 is a newline then that character is deleted from the result
1807 or error message for consistency with normal
1808 Tcl return values.
1810 An *arg* may have one of the following special forms:
1812 +>filename+::
1813     The standard output of the last command in the pipeline
1814     is redirected to the file.  In this situation 'exec'
1815     will normally return an empty string.
1817 +>>filename+::
1818     As above, but append to the file.
1820 +>@fileId+::
1821     The standard output of the last command in the pipeline is
1822     redirected to the given (writable) file descriptor (e.g. stdout,
1823     stderr, or the result of 'open'). In this situation 'exec'
1824     will normally return an empty string.
1826 +2>filename+::
1827     The standard error of the last command in the pipeline
1828     is redirected to the file.
1830 +2>>filename+::
1831     As above, but append to the file.
1833 +2>@fileId+::
1834     The standard error of the last command in the pipeline is
1835     redirected to the given (writable) file descriptor.
1837 +2>@1+::
1838     The standard error of the last command in the pipeline is
1839     redirected to the same file descriptor as the standard output.
1841 +>&filename+::
1842     Both the standard output and standard error of the last command
1843     in the pipeline is redirected to the file.
1845 +>>&filename+::
1846     As above, but append to the file.
1848 +<filename+::
1849     The standard input of the first command in the pipeline
1850     is taken from the file.
1852 +<<string+::
1853     The standard input of the first command is taken as the
1854     given immediate value.
1856 +<@fileId+::
1857     The standard input of the first command in the pipeline
1858     is taken from the given (readable) file descriptor.
1860 If there is no redirection of standard input, standard error
1861 or standard output, these are connected to the corresponding
1862 input or output of the application.
1864 If the last *arg* is '&' then the command will be
1865 executed in background.
1866 In this case the standard output from the last command
1867 in the pipeline will
1868 go to the application's standard output unless
1869 redirected in the command, and error output from all
1870 the commands in the pipeline will go to the application's
1871 standard error file. The return value of exec in this case
1872 is a list of process ids (pids) in the pipeline.
1874 Each *arg* becomes one word for a command, except for
1875 '|', '<', '<<', '>', and '&' arguments, and the
1876 arguments that follow '<', '<<', and '>'.
1878 The first word in each command is taken as the command name;
1879 the directories in the PATH environment variable are searched for
1880 an executable by the given name.
1882 No 'glob' expansion or other shell-like substitutions
1883 are performed on the arguments to commands.
1885 exit
1886 ~~~~
1887 +*exit* '?returnCode?'+
1889 Terminate the process, returning *returnCode* to the
1890 parent as the exit status.
1892 If *returnCode* isn't specified then it defaults
1893 to 0.
1895 Note that exit can be caught with *catch*.
1897 expr
1898 ~~~~
1899 +*expr* 'arg'+
1901 Calls the expression processor to evaluate *arg*, and returns
1902 the result as a string.  See the section EXPRESSIONS above.
1904 file
1905 ~~~~
1906 +*file* 'option name ?arg arg ...?'+
1908 Operate on a file or a file name.  *name* is the name of a file.
1910 *Option* indicates what to do with the file name.  Any unique
1911 abbreviation for *option* is acceptable.  The valid options are:
1913 +*file atime* 'name'+::
1914     Return a decimal string giving the time at which file *name*
1915     was last accessed.  The time is measured in the standard UNIX
1916     fashion as seconds from a fixed starting time (often January 1, 1970).
1917     If the file doesn't exist or its access time cannot be queried then an
1918     error is generated.
1920 +*file copy ?-force?* 'source target'+::
1921     Copies file *source* to file *target*. The source file must exist.
1922     The target file must not exist, unless *-force* is specified.
1924 +*file delete* 'name ...'+::
1925     Deletes file or directory *name*. If the file or directory doesn't exist, nothing happens.
1926     If it can't be deleted, an error is generated. Non-empty directories will not be deleted.
1928 +*file dirname* 'name'+::
1929     Return all of the characters in *name* up to but not including
1930     the last slash character.  If there are no slashes in *name*
1931     then return '.' (a single dot).  If the last slash in *name* is its first
1932     character, then return '/'.
1934 +*file executable* 'name'+::
1935     Return '1' if file *name* is executable by
1936     the current user, '0' otherwise.
1938 +*file exists* 'name'+::
1939     Return '1' if file *name* exists and the current user has
1940     search privileges for the directories leading to it, '0' otherwise.
1942 +*file extension* 'name'+::
1943     Return all of the characters in *name* after and including the
1944     last dot in *name*.  If there is no dot in *name* then return
1945     the empty string.
1947 +*file isdirectory* 'name'+::
1948     Return '1' if file *name* is a directory,
1949     '0' otherwise.
1951 +*file isfile* 'name'+::
1952     Return '1' if file *name* is a regular file,
1953     '0' otherwise.
1955 +*file join* 'arg arg ...'+::
1956     Joins multiple path components. Note that if any components is
1957     an absolute path, the preceding components are ignored.
1958     Thus 'file join /tmp /root' returns '/root'.
1960 +*file lstat* 'name varName'+::
1961     Same as 'stat' option (see below) except uses the *lstat*
1962     kernel call instead of *stat*.  This means that if *name*
1963     refers to a symbolic link the information returned in *varName*
1964     is for the link rather than the file it refers to.  On systems that
1965     don't support symbolic links this option behaves exactly the same
1966     as the 'stat' option.
1968 +*file mkdir* 'dir1 ?dir2? ...'+::
1969     Creates each directory specified. For each pathname *dir* specified,
1970     this command will create all non-existing parent directories
1971     as well as *dir* itself. If an existing directory is specified,
1972     then no action is taken and no error is returned. Trying to
1973     overwrite an existing file with a directory will result in an
1974     error.  Arguments are processed in the order specified, halting
1975     at the first error, if any.
1977 +*file mtime* 'name'+::
1978     Return a decimal string giving the time at which file *name*
1979     was last modified.  The time is measured in the standard UNIX
1980     fashion as seconds from a fixed starting time (often January 1, 1970).
1981     If the file doesn't exist or its modified time cannot be queried then an
1982     error is generated.
1984 +*file normalize* 'name'+::
1985     Return the normalized path of *name*. See realpath(3).
1987 +*file owned* 'name'+::
1988     Return '1' if file *name* is owned by the current user,
1989     '0' otherwise.
1991 +*file readable* 'name'+::
1992     Return '1' if file *name* is readable by
1993     the current user, '0' otherwise.
1995 +*file readlink* 'name'+::
1996     Returns the value of the symbolic link given by *name* (i.e. the
1997     name of the file it points to).  If
1998     *name* isn't a symbolic link or its value cannot be read, then
1999     an error is returned.  On systems that don't support symbolic links
2000     this option is undefined.
2002 +*file rename* 'oldname' 'newname'+::
2003     Renames the file from the old name to the new name.
2005 +*file rootname* 'name'+::
2006     Return all of the characters in *name* up to but not including
2007     the last '.' character in the name.  If *name* doesn't contain
2008     a dot, then return *name*.
2010 +*file size* 'name'+::
2011     Return a decimal string giving the size of file *name* in bytes.
2012     If the file doesn't exist or its size cannot be queried then an
2013     error is generated.
2015 +*file stat* 'name varName'+::
2016     Invoke the 'stat' kernel call on *name*, and use the
2017     variable given by *varName* to hold information returned from
2018     the kernel call.
2019     *VarName* is treated as an array variable,
2020     and the following elements of that variable are set: 'atime',
2021     'ctime', 'dev', 'gid', 'ino', 'mode', 'mtime',
2022     'nlink', 'size', 'type', 'uid'.
2023     Each element except 'type' is a decimal string with the value of
2024     the corresponding field from the 'stat' return structure; see the
2025     manual entry for 'stat' for details on the meanings of the values.
2026     The 'type' element gives the type of the file in the same form
2027     returned by the command 'file type'.
2028     This command returns an empty string.
2030 +*file tail* 'name'+::
2031     Return all of the characters in *name* after the last slash.
2032     If *name* contains no slashes then return *name*.
2034 +*file tempfile* '?template?'+::
2035     Creates and returns the name of a unique temporary file. If *template* is omitted, a
2036     default template will be used to place the file in /tmp. See mkstemp(3) for
2037     the format of the template and security concerns.
2039 +*file type* 'name'+::
2040     Returns a string giving the type of file *name*, which will be
2041     one of 'file', 'directory', 'characterSpecial',
2042     'blockSpecial', 'fifo', 'link', or 'socket'.
2044 +*file writable* 'name'+::
2045     Return '1' if file *name* is writable by
2046     the current user, '0' otherwise.
2048 The 'file' commands that return 0/1 results are often used in
2049 conditional or looping commands, for example:
2051     if {![file exists foo]} then {error {bad file name}} else {...}
2053 finalize
2054 ~~~~~~~~
2055 +*finalize* 'reference ?command?'+
2057 If *command* is omitted, returns the finalizer command for the given reference.
2059 Otherwise, sets a new finalizer command for the given reference. *command* may be
2060 the empty string to remove the current finalizer.
2062 The reference must be a valid reference create with the 'ref'
2063 command.
2065 See GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
2067 flush
2068 ~~~~~
2069 +*flush* 'fileId'+
2071 +'fileId' *flush*+
2073 Flushes any output that has been buffered for *fileId*.  *fileId* must
2074 have been the return value from a previous call to 'open', or it may be
2075 'stdout' or 'stderr' to access one of the standard I/O streams; it must
2076 refer to a file that was opened for writing.  This command returns an
2077 empty string.
2081 +*for* 'start test next body'+
2083 'For' is a looping command, similar in structure to the C 'for' statement.
2084 The *start*, *next*, and *body* arguments must be Tcl command strings,
2085 and *test* is an expression string.
2087 The 'for' command first invokes the Tcl interpreter to execute *start*.
2088 Then it repeatedly evaluates *test* as an expression; if the result is
2089 non-zero it invokes the Tcl interpreter on *body*, then invokes the Tcl
2090 interpreter on *next*, then repeats the loop.  The command terminates
2091 when *test* evaluates to 0.
2093 If a 'continue' command is invoked within *body* then any remaining
2094 commands in the current execution of *body* are skipped; processing
2095 continues by invoking the Tcl interpreter on *next*, then evaluating
2096 *test*, and so on.
2098 If a 'break' command is invoked within *body* or *next*, then the 'for'
2099 command will return immediately.
2101 The operation of 'break' and 'continue' are similar to the corresponding
2102 statements in C.
2104 'For' returns an empty string.
2106 foreach
2107 ~~~~~~~
2108 +*foreach* 'varName list body'+
2110 +*foreach* 'varList list ?varList2 list2 ...? body'+
2112 In this command, *varName* is the name of a variable, *list*
2113 is a list of values to assign to *varName*, and *body* is a
2114 collection of Tcl commands. 
2116 For each field in *list* (in order from left to right),'foreach' assigns
2117 the contents of the field to *varName* (as if the 'lindex' command
2118 had been used to extract the field), then calls the Tcl interpreter to
2119 execute *body*.
2121 If instead of being a simple name, *varList* is used, multiple assignments
2122 are made each time through the loop, one for each element of *varList*.
2124 For example, if there are two elements in *varList* and six elements in
2125 the list, the loop will be executed three times.
2127 If the length of the list doesn't evenly divide by the number of elements
2128 in *varList*, the value of the remaining variables in the last iteration
2129 of the loop are undefined.
2131 The 'break' and 'continue' statements may be invoked inside *body*,
2132 with the same effect as in the 'for' command.
2134 'foreach' returns an empty string.
2136 format
2137 ~~~~~~
2138 +*format* 'formatString ?arg arg ...?'+
2140 This command generates a formatted string in the same way as the
2141 C 'sprintf' procedure (it uses 'sprintf' in its
2142 implementation).  *FormatString* indicates how to format
2143 the result, using '%' fields as in 'sprintf', and the additional
2144 arguments, if any, provide values to be substituted into the result.
2146 All of the 'sprintf' options are valid; see the 'sprintf'
2147 man page for details.  Each *arg* must match the expected type
2148 from the '%' field in *formatString*; the 'format' command
2149 converts each argument to the correct type (floating, integer, etc.)
2150 before passing it to 'sprintf' for formatting.
2152 The only unusual conversion is for '%c'; in this case the argument
2153 must be a decimal string, which will then be converted to the corresponding
2154 ASCII character value.
2156 'Format' does backslash substitution on its *formatString*
2157 argument, so backslash sequences in *formatString* will be handled
2158 correctly even if the argument is in braces.
2160 The return value from 'format' is the formatted string.
2162 getref
2163 ~~~~~~
2164 +*getref* 'reference'+
2166 Returns the string associated with *reference*. The reference must
2167 be a valid reference create with the 'ref' command.
2169 See GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
2171 gets
2172 ~~~~
2173 +*gets* 'fileId ?varName?'+
2175 +'fileId' *gets* '?varName?'+
2177 Reads the next line from the file given by *fileId* and discards
2178 the terminating newline character.
2180 If *varName* is specified, then the line is placed in the variable
2181 by that name and the return value is a count of the number of characters
2182 read (not including the newline).
2184 If the end of the file is reached before reading
2185 any characters then -1 is returned and *varName* is set to an
2186 empty string.
2188 If *varName* is not specified then the return value will be
2189 the line (minus the newline character) or an empty string if
2190 the end of the file is reached before reading any characters.
2192 An empty string will also be returned if a line contains no characters
2193 except the newline, so 'eof' may have to be used to determine
2194 what really happened.
2196 If the last character in the file is not a newline character, then
2197 'gets' behaves as if there were an additional newline character
2198 at the end of the file.
2200 *fileId* must be 'stdin' or the return value from a previous
2201 call to 'open'; it must refer to a file that was opened
2202 for reading.
2204 glob
2205 ~~~~
2206 +*glob* ?*-nocomplain*? 'pattern ?pattern ...?'+
2208 This command performs filename globbing, using csh rules.  The returned
2209 value from 'glob' is the list of expanded filenames.
2211 If '-nocomplain' is specified as the first argument then an empty
2212 list may be returned;  otherwise an error is returned if the expanded
2213 list is empty.  The '-nocomplain' argument must be provided
2214 exactly: an abbreviation will not be accepted.
2216 global
2217 ~~~~~~
2219 +*global* 'varName ?varName ...?'+
2221 This command is ignored unless a Tcl procedure is being interpreted.
2222 If so, then it declares each given *varName* to be a global variable
2223 rather than a local one.  For the duration of the current procedure
2224 (and only while executing in the current procedure), any reference to
2225 *varName* will be bound to a global variable instead
2226 of a local one.
2228 An alternative to using 'global' is to use the '::' prefix
2229 to explicitly name a variable in the global scope.
2233 +*if* 'expr1' ?*then*? 'body1' *elseif* 'expr2' ?*then*? 'body2' *elseif* ... ?*else*? ?'bodyN'?+
2235 The 'if' command evaluates *expr1* as an expression (in the same way
2236 that 'expr' evaluates its argument).  The value of the expression must
2237 be numeric; if it is non-zero then *body1* is executed by passing it to
2238 the Tcl interpreter.
2240 Otherwise *expr2* is evaluated as an expression and if it is non-zero
2241 then *body2* is executed, and so on.
2243 If none of the expressions evaluates to non-zero then *bodyN* is executed.
2245 The 'then' and 'else' arguments are optional 'noise words' to make the
2246 command easier to read.
2248 There may be any number of 'elseif' clauses, including zero.  *bodyN*
2249 may also be omitted as long as 'else' is omitted too.
2251 The return value from the command is the result of the body script that
2252 was executed, or an empty string if none of the expressions was non-zero
2253 and there was no *bodyN*.
2255 incr
2256 ~~~~
2257 +*incr* 'varName ?increment?'+
2259 Increment the value stored in the variable whose name is *varName*.
2260 The value of the variable must be integral.
2262 If *increment* is supplied then its value (which must be an
2263 integer) is added to the value of variable *varName*;  otherwise
2264 1 is added to *varName*.
2266 The new value is stored as a decimal string in variable *varName*
2267 and also returned as result.
2269 If the variable does not exist, the variable is implicitly created
2270 and set to +0+ first.
2272 info
2273 ~~~~
2275 +*info* 'option ?arg arg ...?'+::
2277 Provide information about various internals to the Tcl interpreter.
2278 The legal *option*'s (which may be abbreviated) are:
2280 +*info args* 'procname'+::
2281     Returns a list containing the names of the arguments to procedure
2282     *procname*, in order.  *Procname* must be the name of a
2283     Tcl command procedure.
2285 +*info body* 'procname'+::
2286     Returns the body of procedure *procname*.  *Procname* must be
2287     the name of a Tcl command procedure.
2289 +*info commands* ?'pattern'?+::
2290     If *pattern* isn't specified, returns a list of names of all the
2291     Tcl commands, including both the built-in commands written in C and
2292     the command procedures defined using the 'proc' command.
2293     If *pattern* is specified, only those names matching *pattern*
2294     are returned.  Matching is determined using the same rules as for
2295     'string match'.
2297 +*info complete* 'command'+::
2298     Returns 1 if *command* is a complete Tcl command in the sense of
2299     having no unclosed quotes, braces, brackets or array element names,
2300     If the command doesn't appear to be complete then 0 is returned.
2301     This command is typically used in line-oriented input environments
2302     to allow users to type in commands that span multiple lines;  if the
2303     command isn't complete, the script can delay evaluating it until additional
2304     lines have been typed to complete the command.
2306 +*info exists* 'varName'+::
2307     Returns '1' if the variable named *varName* exists in the
2308     current context (either as a global or local variable), returns '0'
2309     otherwise.
2311 +*info frame* ?'number'?+::
2312     If *number* is not specified, this command returns a number
2313     which is the same result as 'info level' - the current stack frame level.
2314     If *number* is specified, then the result is a list consisting of the procedure,
2315     filename and line number for the procedure call at level *number* on the stack.
2316     If *number* is positive then it selects a particular stack level (1 refers
2317     to the top-most active procedure, 2 to the procedure it called, and
2318     so on); otherwise it gives a level relative to the current level
2319     (0 refers to the current procedure, -1 to its caller, and so on).
2320     The level has an identical meaning to 'info level'.
2322 +*info globals* ?'pattern'?+::
2323     If *pattern* isn't specified, returns a list of all the names
2324     of currently-defined global variables.
2325     If *pattern* is specified, only those names matching *pattern*
2326     are returned.  Matching is determined using the same rules as for
2327     'string match'.
2329 +*info hostname*+::
2330     An alias for 'os.gethostname' for compatibility with Tcl 6.x
2332 +*info level* ?'number'?+::
2333     If *number* is not specified, this command returns a number
2334     giving the stack level of the invoking procedure, or 0 if the
2335     command is invoked at top-level.  If *number* is specified,
2336     then the result is a list consisting of the name and arguments for the
2337     procedure call at level *number* on the stack.  If *number*
2338     is positive then it selects a particular stack level (1 refers
2339     to the top-most active procedure, 2 to the procedure it called, and
2340     so on); otherwise it gives a level relative to the current level
2341     (0 refers to the current procedure, -1 to its caller, and so on).
2342     See the 'uplevel' command for more information on what stack
2343     levels mean.
2345 +*info locals* ?'pattern'?+::
2346     If *pattern* isn't specified, returns a list of all the names
2347     of currently-defined local variables, including arguments to the
2348     current procedure, if any.  Variables defined with the 'global'
2349     and 'upvar' commands will not be returned.  If *pattern* is
2350     specified, only those names matching *pattern* are returned.
2351     Matching is determined using the same rules as for 'string match'.
2353 +*info nameofexecutable*+::
2354     Returns the name of the binary file from which the application
2355     was invoked, either
2356     as a path relative to the current directory or as a full
2357     path. If the path can't be determined, returns the empty
2358     string.
2360 +*info procs* ?'pattern'?+::
2361     If *pattern* isn't specified, returns a list of all the
2362     names of Tcl command procedures.
2363     If *pattern* is specified, only those names matching *pattern*
2364     are returned.  Matching is determined using the same rules as for
2365     'string match'.
2367 +*info returncodes* ?'code'?+::
2368     Returns a list representing the mapping of standard return codes
2369     to names. e.g. +{0 ok 1 error 2 return ...}+. If a code is given,
2370     instead returns the name for the given code.
2372 +*info script*+::
2373     If a Tcl script file is currently being evaluated (i.e. there is a
2374     call to 'Jim_EvalFile' active or there is an active invocation
2375     of the 'source' command), then this command returns the name
2376     of the innermost file being processed.  Otherwise the command returns an
2377     empty string.
2379 +*info source* 'script'+::
2380     Returns the original source location of the given script as a list of
2381     +{filename linenumber}+. If the source location can't be determined, the
2382     list +{{} 0}+ is returned.
2384 +*info stacktrace*+::
2385     After an error is caught with 'catch', returns the stack trace as a list
2386     of +{procedure filename line ...}+.
2388 +*info version*+::
2389     Returns the version number for this version of Jim in the form *x.yy*.
2391 +*info vars* ?'pattern'?+::
2392     If *pattern* isn't specified,
2393     returns a list of all the names of currently-visible variables, including
2394     both locals and currently-visible globals.
2395     If *pattern* is specified, only those names matching *pattern*
2396     are returned.  Matching is determined using the same rules as for
2397     'string match'.
2399 join
2400 ~~~~
2401 +*join* 'list ?joinString?'+
2403 The *list* argument must be a valid Tcl list.  This command returns the
2404 string formed by joining all of the elements of *list* together with
2405 *joinString* separating each adjacent pair of elements.
2407 The *joinString* argument defaults to a space character.
2409 kill
2410 ~~~~
2411 +*kill* ?'SIG'|*-0*? 'pid'+
2413 Sends the given signal to the process identified by *pid*.
2415 The signal may be specified by name or number in one of the following forms:
2417 * +TERM+
2418 * +SIGTERM+
2419 * +-TERM+
2420 * +15+
2421 * +-15+
2423 The signal name may be in either upper or lower case.
2425 The special signal name '-0' simply checks that a signal *could* be sent.
2427 If no signal is specified, SIGTERM is used.
2429 An error is raised if the signal could not be delivered.
2431 lambda
2432 ~~~~~~
2433 +*lambda* 'args ?statics? body'+
2435 The 'lambda' command is identical to 'proc', except rather than
2436 creating a named procedure, it creates an anonymous procedure and returns
2437 the name of the procedure.
2439 See 'proc' and GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
2441 lappend
2442 ~~~~~~~
2443 +*lappend* 'varName value ?value value ...?'+
2445 Treat the variable given by *varName* as a list and append each of
2446 the *value* arguments to that list as a separate element, with spaces
2447 between elements.
2449 If *varName* doesn't exist, it is created as a list with elements given
2450 by the *value* arguments. 'lappend' is similar to 'append' except that
2451 each *value* is appended as a list element rather than raw text.
2453 This command provides a relatively efficient way to build up large lists.
2454 For example, 'lappend a $b' is much more efficient than
2456     set a [concat $a [list $b]]
2457     
2458 when '$a' is long.
2460 lassign
2461 ~~~~~~~
2462 +*lassign* 'list varName ?varName? ...'+
2464 This command treats the value *list* as a list and assigns successive elements from that list to
2465 the variables given by the *varName* arguments in order. If there are more variable names than
2466 list elements, the remaining variables are set to the empty string. If there are more list ele-
2467 ments than variables, a list of unassigned elements is returned.
2469     jim> lassign {1 2 3} a b; puts a=$a,b=$b
2470     3
2471     a=1,b=2
2473 local
2474 ~~~~~
2475 +*local* 'args'+
2477 Executes it's arguments as a command (per 'eval') and considers the return
2478 value to be a procedure name, which is marked as having local scope.
2479 This means that when the current procedure exits, the specified
2480 procedure is deleted. This can be useful with 'lambda' or simply
2481 local procedures.
2483 In this example, a local procedure is created. Note that the procedure
2484 continues to have global scope while it is active.
2486     proc outer {} {
2487           # proc ... returns "inner" which is marked local
2488       local proc inner {} {
2489                 # will be deleted when 'outer' exits
2490           }
2492           inner
2493           ...
2494         }
2496 In this example, the lambda is deleted at the end of the procedure rather
2497 than waiting until garbage collection.
2499     proc outer {} {
2500       set x [lambda inner {args} {
2501                 # will be deleted when 'outer' exits
2502           }]
2503           # Use 'function' here which simply returns $x
2504           local function $x
2506           $x ...
2507           ...
2508         }
2510 lindex
2511 ~~~~~~
2512 +*lindex* 'list index'+
2514 Treats *list* as a Tcl list and returns element *index* from it
2515 (0 refers to the first element of the list).
2516 See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *index*.
2518 In extracting the element, *lindex* observes the same rules concerning
2519 braces and quotes and backslashes as the Tcl command interpreter; however,
2520 variable substitution and command substitution do not occur.
2522 If *index* is negative or greater than or equal to the number of elements
2523 in *value*, then an empty string is returned.
2525 linsert
2526 ~~~~~~~
2527 +*linsert* 'list index element ?element element ...?'+
2529 This command produces a new list from *list* by inserting all
2530 of the *element* arguments just before the element *index*
2531 of *list*. Each *element* argument will become
2532 a separate element of the new list. If *index* is less than
2533 or equal to zero, then the new elements are inserted at the
2534 beginning of the list. If *index* is greater than or equal
2535 to the number of elements in the list, then the new elements are
2536 appended to the list.
2538 See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *index*.
2540 list
2541 ~~~~
2543 +*list* 'arg ?arg ...?'+
2545 This command returns a list comprised of all the arguments, *arg*. Braces
2546 and backslashes get added as necessary, so that the 'index' command
2547 may be used on the result to re-extract the original arguments, and also
2548 so that 'eval' may be used to execute the resulting list, with
2549 *arg1* comprising the command's name and the other args comprising
2550 its arguments. 'List' produces slightly different results than
2551 'concat':  'concat' removes one level of grouping before forming
2552 the list, while 'list' works directly from the original arguments.
2553 For example, the command
2555     list a b {c d e} {f {g h}}
2557 will return
2559     a b {c d e} {f {g h}}
2561 while 'concat' with the same arguments will return
2563     a b c d e f {g h}
2565 llength
2566 ~~~~~~~
2567 +*llength* 'list'+
2569 Treats *list* as a list and returns a decimal string giving
2570 the number of elements in it.
2572 lset
2573 ~~~~
2574 +*lset* 'varName ?index ..? newValue'+
2576 Sets an element in a list.
2578 The 'lset' command accepts a parameter, *varName*, which it interprets
2579 as the name of a variable containing a Tcl list. It also accepts
2580 zero or more indices into the list. Finally, it accepts a new value
2581 for an element of varName. If no indices are presented, the command
2582 takes the form:
2584     lset varName newValue
2586 In this case, newValue replaces the old value of the variable
2587 varName.
2589 When presented with a single index, the 'lset' command
2590 treats the content of the varName variable as a Tcl list. It addresses
2591 the index'th element in it (0 refers to the first element of the
2592 list). When interpreting the list, 'lset' observes the same rules
2593 concerning braces and quotes and backslashes as the Tcl command
2594 interpreter; however, variable substitution and command substitution
2595 do not occur. The command constructs a new list in which the
2596 designated element is replaced with newValue. This new list is
2597 stored in the variable varName, and is also the return value from
2598 the 'lset' command.
2600 If index is negative or greater than or equal to the number of
2601 elements in $varName, then an error occurs.
2603 See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *index*.
2605 If additional index arguments are supplied, then each argument is
2606 used in turn to address an element within a sublist designated by
2607 the previous indexing operation, allowing the script to alter
2608 elements in sublists. The command,
2610     lset a 1 2 newValue
2612 replaces element 2 of sublist 1 with *newValue*.
2614 The integer appearing in each index argument must be greater than
2615 or equal to zero. The integer appearing in each index argument must
2616 be strictly less than the length of the corresponding list. In other
2617 words, the 'lset' command cannot change the size of a list. If an
2618 index is outside the permitted range, an error is reported.
2620 lmap
2621 ~~~~
2623 +*lmap* 'varName list body'+
2625 +*lmap* 'varList list ?varList2 list2 ...? body'+
2627 'lmap' is a "collecting 'foreach'" which returns a list of its results.
2629 For example:
2631     jim> lmap i {1 2 3 4 5} {expr $i*$i}
2632     1 4 9 16 25
2633     jim> lmap a {1 2 3} b {A B C} {list $a $b}
2634     {1 A} {2 B} {3 C}
2636 If the body invokes 'continue', no value is added for this iteration.
2637 If the body invokes 'break', the loop ends and no more values are added.
2639 lrange
2640 ~~~~~~
2641 +*lrange* 'list first last'+
2643 *List* must be a valid Tcl list. This command will return a new
2644 list consisting of elements *first* through *last*, inclusive.
2646 See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *first* and *last*.
2648 If *last* is greater than or equal to the number of elements
2649 in the list, then it is treated as if it were 'end'.
2651 If *first* is greater than *last* then an empty string
2652 is returned.
2654 Note: 'lrange *list first first*' does not always produce the
2655 same result as 'lindex *list first*' (although it often does
2656 for simple fields that aren't enclosed in braces); it does, however,
2657 produce exactly the same results as 'list [lindex *list first*]'
2659 lreplace
2660 ~~~~~~~~
2662 +*lreplace* 'list first last ?element element ...?'+
2664 Returns a new list formed by replacing one or more elements of
2665 *list* with the *element* arguments.
2667 *First* gives the index in *list* of the first element
2668 to be replaced.
2670 If *first* is less than zero then it refers to the first
2671 element of *list*;  the element indicated by *first*
2672 must exist in the list.
2674 *Last* gives the index in *list* of the last element
2675 to be replaced;  it must be greater than or equal to *first*.
2677 See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *first* and *last*.
2679 The *element* arguments specify zero or more new arguments to
2680 be added to the list in place of those that were deleted.
2682 Each *element* argument will become a separate element of
2683 the list.
2685 If no *element* arguments are specified, then the elements
2686 between *first* and *last* are simply deleted.
2688 lrepeat
2689 ~~~~~~~~
2690 +*lrepeat* 'number element1 ?element2 ...?'+
2692 Build a list by repeating elements *number* times (which must be
2693 a positive integer).
2695     jim> lrepeat 3 a b
2696     a b a b a b
2698 lreverse
2699 ~~~~~~~~
2700 +*lreverse* 'list'+
2702 Returns the list in reverse order.
2704     jim> lreverse {1 2 3}
2705     3 2 1
2707 lsearch
2708 ~~~~~~~
2709 +*lsearch* '?options? list pattern'+
2711 This command searches the elements *list* to see if one of them matches *pattern*. If so, the
2712 command returns the index of the first matching element (unless the options -all, -inline or -bool are
2713 specified.) If not, the command returns -1. The option arguments indicates how the elements of
2714 the list are to be matched against pattern and must have one of the values below:
2716 *Note* that this command is different from Tcl in that default match type is '-exact' rather than '-glob'.
2718 +'-exact'+::
2719     *pattern* is a literal string that is compared for exact equality against each list element.
2720     This is the default.
2722 +'-glob'+::
2723     *pattern* is a glob-style pattern which is matched against each list element using the same
2724     rules as the string match command.
2726 +'-regexp'+::
2727     *pattern* is treated as a regular expression and matched against each list element using
2728     the rules described by 'regexp'.
2730 +'-all'+::
2731     Changes the result to be the list of all matching indices (or all matching values if
2732     '-inline' is specified as well). If indices are returned, the indices will be in numeric
2733     order. If values are returned, the order of the values will be the order of those values
2734     within the input list.
2736 +'-inline'+::
2737     The matching value is returned instead of its index (or an empty string if no value
2738     matches). If '-all' is also specified, then the result of the command is the list of all
2739     values that matched. The '-inline' and '-bool' options are mutually exclusive.
2741 +'-bool'+::
2742     Changes the result to '1' if a match was found, or '0' otherwise. If '-all' is also specified,
2743     the result will be a list of '0' and '1' for each element of the list depending upon whether
2744     the corresponding element matches. The '-inline' and '-bool' options are mutually exclusive.
2746 +'-not'+::
2747     This negates the sense of the match, returning the index (or value
2748     if '-inline' is specified) of the first non-matching value in the
2749     list. If '-bool' is also specified, the '0' will be returned if a
2750     match is found, or '1' otherwise. If '-all' is also specified,
2751     non-matches will be returned rather than matches.
2753 +'-nocase'+::
2754     Causes comparisons to be handled in a case-insensitive manner.
2756 lsort
2757 ~~~~~
2758 +*lsort* ?*-integer*|*-command* 'cmdname'? ?*-decreasing*|*-increasing*? 'list'+
2760 Sort the elements of *list*, returning a new list in sorted order.
2761 By default, ASCII sorting is used, with the result in increasing order.
2763 If *-integer* is specified, numeric sorting is used.
2765 If *-command cmdname* is specified, *cmdname* is treated as a
2766 command name. For each comparison, *cmdname $value1 $value2* is called
2767 which should return '-1' if *$value1* is less than *$value2*, '0' if
2768 they are equal, or '1' otherwise.
2770 If *-decreasing* is specified, the resulting list is in the opposite
2771 order to what it would be otherwise. *-increasing* is the default.
2773 open
2774 ~~~~
2775 +*open* 'fileName ?access?'+
2777 Opens a file and returns an identifier
2778 that may be used in future invocations
2779 of commands like 'read', 'puts', and 'close'.
2780 *fileName* gives the name of the file to open.
2782 The *access* argument indicates the way in which the file is to be accessed.
2783 It may have any of the following values:
2785 +r+::
2786     Open the file for reading only; the file must already exist.
2788 +r++::
2789     Open the file for both reading and writing; the file must
2790     already exist.
2792 +w+::
2793     Open the file for writing only. Truncate it if it exists. If it doesn't
2794     exist, create a new file.
2796 +w++::
2797     Open the file for reading and writing. Truncate it if it exists.
2798     If it doesn't exist, create a new file.
2800 +a+::
2801     Open the file for writing only. The file must already exist, and the file
2802     is positioned so that new data is appended to the file.
2804 +a++::
2805     Open the file for reading and writing. If the file doesn't
2806     exist, create a new empty file. Set the initial access position
2807     to the end of the file.
2809 *Access* defaults to 'r'.
2811 If a file is opened for both reading and writing, then 'seek'
2812 must be invoked between a read and a write, or vice versa.
2814 See also 'socket'
2816 package
2817 ~~~~~~~
2818 +*package provide* 'name ?version?'+
2820 Indicates that the current script provides the package named *name*.
2821 If no version is specified, '1.0' is used.
2823 Any script which provides a package may include this statement
2824 as the first statement, although it is not required.
2826 +*package require* 'name ?version?'*+
2828 Searches for the package with the given *name* by examining each path
2829 in '$::auto_path' and trying to load '$path/$name.tcl'.
2831 If either file exists, it is loaded with 'source'.
2833 Typically '$::auto_path' contains the paths '.' and '/lib/jim'.
2837 +*pid*+
2839 Returns the process identifier of the current process.
2841 proc
2842 ~~~~
2843 +*proc* 'name args ?statics? body'+
2845 The 'proc' command creates a new Tcl command procedure, *name*.
2846 When the new command is invoked, the contents of *body* will be executed.
2847 Tcl interpreter. *args* specifies the formal arguments to the procedure.
2848 If specified, *static*, declares static variables which are bound to the
2849 procedure.
2851 See PROCEDURES for detailed information about Tcl procedures.
2853 The 'proc' command returns *name* (which is useful with 'local').
2855 When a procedure is invoked, the procedure's return value is the
2856 value specified in a 'return' command.  If the procedure doesn't
2857 execute an explicit 'return', then its return value is the value
2858 of the last command executed in the procedure's body.
2860 If an error occurs while executing the procedure body, then the
2861 procedure-as-a-whole will return that same error.
2863 puts
2864 ~~~~
2865 +*puts* ?*-nonewline*? '?fileId? string'+
2867 +'fileId' *puts* ?*-nonewline*? 'string'+
2869 Writes the characters given by *string* to the file given
2870 by *fileId*. *fileId* must have been the return
2871 value from a previous call to 'open', or it may be
2872 'stdout' or 'stderr' to refer to one of the standard I/O
2873 channels; it must refer to a file that was opened for
2874 writing.
2876 In the first form, if no *fileId* is specified then it defaults to 'stdout'.
2877 'puts' normally outputs a newline character after *string*,
2878 but this feature may be suppressed by specifying the '-nonewline'
2879 switch.
2881 Output to files is buffered internally by Tcl; the 'flush'
2882 command may be used to force buffered characters to be output.
2886 +*pwd*+
2888 Returns the path name of the current working directory.
2890 rand
2891 ~~~~
2892 +*rand* '?min? ?max?'+
2894 Returns a random integer between *min* (defaults to 0) and *max*
2895 (defaults to the maximum integer).
2897 If only one argument is given, it is interpreted as *max*.
2899 range
2900 ~~~~
2901 +*range* '?start? end ?step?'+
2903 Returns a list of integers starting at *start* (defaults to 0)
2904 and ranging up to but not including *end* in steps of *step* defaults to 1).
2906     jim> range 5
2907     0 1 2 3 4
2908     jim> range 2 5
2909     2 3 4
2910     jim> range 2 10 4
2911     2 6
2912     jim> range 7 4 -2
2913     7 5
2915 read
2916 ~~~~
2917 +*read* ?*-nonewline*? 'fileId'+
2919 +'fileId' *read* ?*-nonewline*?+
2921 +*read* 'fileId numBytes'+
2923 +'fileId' *read* 'numBytes'+
2926 In the first form, all of the remaining bytes are read from the file
2927 given by *fileId*; they are returned as the result of the command.
2928 If the '-nonewline' switch is specified then the last
2929 character of the file is discarded if it is a newline.
2931 In the second form, the extra argument specifies how many bytes to read;
2932 exactly this many bytes will be read and returned, unless there are fewer than
2933 *numBytes* bytes left in the file; in this case, all the remaining
2934 bytes are returned.
2936 *fileId* must be 'stdin' or the return value from a previous call
2937 to 'open'; it must refer to a file that was opened for reading.
2939 regexp
2940 ~~~~~~
2941 +*regexp ?-nocase? ?-line? ?-indices? ?-start* 'offset'? *?-all? ?-inline? ?--?* 'exp string ?matchVar? ?subMatchVar subMatchVar ...?'+
2943 Determines whether the regular expression *exp* matches part or
2944 all of *string* and returns 1 if it does, 0 if it doesn't.
2946 See REGULAR EXPRESSIONS above for complete information on the
2947 syntax of *exp* and how it is matched against *string*.
2949 If additional arguments are specified after *string* then they
2950 are treated as the names of variables to use to return
2951 information about which part(s) of *string* matched *exp*.
2952 *matchVar* will be set to the range of *string* that
2953 matched all of *exp*. The first *subMatchVar* will contain
2954 the characters in *string* that matched the leftmost parenthesized
2955 subexpression within *exp*, the next *subMatchVar* will
2956 contain the characters that matched the next parenthesized
2957 subexpression to the right in *exp*, and so on.
2959 Normally, *matchVar* and the each *subMatchVar* are set to hold the
2960 matching characters from 'string', however see '-indices' and
2961 '-inline' below.
2963 If there are more values for *subMatchVar* than parenthesized subexpressions
2964 within *exp*, or if a particular subexpression in *exp* doesn't
2965 match the string (e.g. because it was in a portion of the expression
2966 that wasn't matched), then the corresponding *subMatchVar* will be
2967 set to '"-1 -1"' if '-indices' has been specified or to an empty
2968 string otherwise.
2970 The following switches modify the behaviour of *regexp*
2972 +*-nocase*+::
2973     Causes upper-case and lower-case characters to be treated as
2974     identical during the matching process.
2976 +*-line*+::
2977     Use newline-sensitive matching. By default, newline
2978     is a completely ordinary character with no special meaning in
2979     either REs or strings. With this flag, '[^' bracket expressions
2980     and '.' never match newline, a '^' anchor matches the null
2981     string after any newline in the string in addition to its normal
2982     function, and the '$' anchor matches the null string before any
2983     newline in the string in addition to its normal function.
2985 +*-indices*+::
2986     Changes what is stored in the subMatchVars. Instead of
2987     storing the matching characters from string, each variable
2988     will contain a list of two decimal strings giving the indices
2989     in string of the first and last characters in the matching
2990     range of characters.
2992 +*-start* 'offset'+::
2993     Specifies a character index offset into the string at which to start
2994     matching the regular expression. If '-indices' is
2995     specified, the indices will be indexed starting from the
2996     absolute beginning of the input string. *offset* will be
2997     constrained to the bounds of the input string.
2999 +*-all*+::
3000     Causes the regular expression to be matched as many times as possible
3001     in the string, returning the total number of matches found. If this
3002     is specified with match variables, they will contain information
3003     for the last match only.
3005 +*-inline*+::
3006     Causes the command to return, as a list, the data that would otherwise
3007     be placed in match variables. When using '-inline', match variables
3008     may not be specified. If used with '-all', the list will be concatenated
3009     at each iteration, such that a flat list is always returned. For
3010     each match iteration, the command will append the overall match
3011     data, plus one element for each subexpression in the regular
3012     expression.
3014 +*--*+::
3015     Marks the end of switches. The argument following this one will be
3016     treated as *exp* even if it starts with a +-+.
3018 regsub
3019 ~~~~~~
3020 +*regsub ?-nocase? ?-all? ?-line? ?-start* 'offset'? ?*--*? 'exp string subSpec ?varName?'+
3022 This command matches the regular expression *exp* against
3023 *string* using the rules described in REGULAR EXPRESSIONS
3024 above.
3026 If *varName* is specified, the commands stores *string* to *varName*
3027 with the susbstitutions detailed below, and returns the number of
3028 substitutions made (normally 1 unless '-all' is specified).
3029 This is 0 if there were no matches.
3031 If *varName* is not specified, the substituted string will be returned
3032 instead.
3034 When copying *string*, the portion of *string* that
3035 matched *exp* is replaced with *subSpec*.
3036 If *subSpec* contains a '&' or '{backslash}0', then it is replaced
3037 in the substitution with the portion of *string* that
3038 matched *exp*.
3040 If *subSpec* contains a '{backslash}*n*', where *n* is a digit
3041 between 1 and 9, then it is replaced in the substitution with
3042 the portion of *string* that matched the **n**-th
3043 parenthesized subexpression of *exp*.
3044 Additional backslashes may be used in *subSpec* to prevent special
3045 interpretation of '&' or '{backslash}0' or '{backslash}*n*' or
3046 backslash.
3048 The use of backslashes in *subSpec* tends to interact badly
3049 with the Tcl parser's use of backslashes, so it's generally
3050 safest to enclose *subSpec* in braces if it includes
3051 backslashes.
3053 The following switches modify the behaviour of *regsub*
3055 +*-nocase*+::
3056     Upper-case characters in *string* are converted to lower-case
3057     before matching against *exp*;  however, substitutions
3058     specified by *subSpec* use the original unconverted form
3059     of *string*. 
3061 +*-all*+::
3062     All ranges in *string* that match *exp* are found and substitution
3063     is performed for each of these ranges, rather than only the
3064     first. The '&' and '{backslash}*n*' sequences are handled for
3065     each substitution using the information from the corresponding
3066     match.
3068 +*-line*+::
3069     Use newline-sensitive matching. By default, newline
3070     is a completely ordinary character with no special meaning in
3071     either REs or strings.  With this flag, '[^' bracket expressions
3072     and '.' never match newline, a '^' anchor matches the null
3073     string after any newline in the string in addition to its normal
3074     function, and the '$' anchor matches the null string before any
3075     newline in the string in addition to its normal function.
3077 +*-start* 'offset'+::
3078     Specifies a character index offset into the string at which to
3079     start matching the regular expression. *offset* will be
3080     constrained to the bounds of the input string.
3082 +*--*+::
3083     Marks the end of switches. The argument following this one will be
3084     treated as *exp* even if it starts with a +-+.
3088 +*ref* 'string tag ?finalizer?'+
3090 Create a new reference containing *string* of type *tag*.
3091 If *finalizer* is specified, it is a command which will be invoked
3092 when the a garbage collection cycle runs and this reference is
3093 no longer accessible.
3095 The finalizer is invoked as:
3097   +finalizer 'reference string'+
3099 See GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
3101 rename
3102 ~~~~~~
3103 +*rename* 'oldName newName'+
3105 Rename the command that used to be called *oldName* so that it
3106 is now called *newName*.  If *newName* is an empty string
3107 (e.g. {}) then *oldName* is deleted.  The 'rename' command
3108 returns an empty string as result.
3110 return
3111 ~~~~~~
3112 +*return* ?*-code* 'code'? ?*-errorinfo* 'stacktrace'? ?*-level* 'n'? ?'value'?+
3114 Return immediately from the current procedure (or top-level command
3115 or 'source' command), with *value* as the return value.  If *value*
3116 is not specified, an empty string will be returned as result.
3118 If *-code* is specified (as either a number or ok, error, break,
3119 continue, signal, return or exit), this code will be used instead
3120 of +JIM_OK+. This is generally useful when implementing flow of control
3121 commands.
3123 If *-level* is specified and greater than 1, it has the effect of delaying
3124 the new return code from *-code*. This is useful when rethrowing an error
3125 from 'catch'. See the implementation of try/catch in tclcompat.tcl for
3126 an example of how this is done.
3128 If *-errorinfo* is specified (as returned from 'info stacktrace')
3129 it is used to initialize the stacktrace.
3130 scan
3131 ~~~~
3132 +*scan* 'string format varName1 ?varName2 ...?'+
3134 This command parses fields from an input string in the same fashion
3135 as the C 'sscanf' procedure.  *String* gives the input to be parsed
3136 and *format* indicates how to parse it, using '%' fields as in
3137 'sscanf'.  All of the 'sscanf' options are valid; see the 'sscanf'
3138 man page for details.  Each *varName* gives the name of a variable;
3139 when a field is scanned from *string*, the result is converted back
3140 into a string and assigned to the corresponding *varName*.  The
3141 only unusual conversion is for '%c'.  For '%c' conversions a single
3142 character value is converted to a decimal string, which is then
3143 assigned to the corresponding *varName*; no field width may be
3144 specified for this conversion.
3146 seek
3147 ~~~~
3148 +*seek* 'fileId offset ?origin?'+
3150 +'fileId' *seek* 'offset ?origin?'+
3152 Change the current access position for *fileId*.
3153 The *offset* and *origin* arguments specify the position at
3154 which the next read or write will occur for *fileId*.
3155 *offset* must be a number (which may be negative) and *origin*
3156 must be one of the following:
3158 +*start*+::
3159     The new access position will be *offset* bytes from the start
3160     of the file.
3162 +*current*+::
3163     The new access position will be *offset* bytes from the current
3164     access position; a negative *offset* moves the access position
3165     backwards in the file.
3167 +*end*+::
3168     The new access position will be *offset* bytes from the end of
3169     the file.  A negative *offset* places the access position before
3170     the end-of-file, and a positive *offset* places the access position
3171     after the end-of-file.
3173 The *origin* argument defaults to 'start'.
3175 *fileId* must have been the return value from a previous call to
3176 'open', or it may be 'stdin', 'stdout', or 'stderr' to refer to one
3177 of the standard I/O channels.
3179 This command returns an empty string.
3183 +*set* 'varName ?value?'+
3185 Returns the value of variable *varName*.
3187 If *value* is specified, then set the value of *varName* to *value*,
3188 creating a new variable if one doesn't already exist, and return
3189 its value.
3191 If *varName* contains an open parenthesis and ends with a
3192 close parenthesis, then it refers to an array element:  the characters
3193 before the open parenthesis are the name of the array, and the characters
3194 between the parentheses are the index within the array.
3195 Otherwise *varName* refers to a scalar variable.
3197 If no procedure is active, then *varName* refers to a global
3198 variable.
3200 If a procedure is active, then *varName* refers to a parameter
3201 or local variable of the procedure, unless the *global* command
3202 has been invoked to declare *varName* to be global.
3204 The '::' prefix may also be used to explicitly reference a variable
3205 in the global scope.
3207 setref
3208 ~~~~~~
3209 +*setref* 'reference string'+
3211 Store a new string in *reference*, replacing the existing string.
3212 The reference must be a valid reference create with the 'ref'
3213 command.
3215 See GARBAGE COLLECTION, REFERENCES, LAMBDA for more detail.
3217 signal
3218 ~~~~~~
3219 Command for signal handling.
3221 See 'kill' for the different forms which may be used to specify signals.
3223 Commands which return a list of signal names do so using the canonical form:
3224 "+SIGINT SIGTERM+".
3226 +*signal handle* ?'signals ...'?+::
3227     If no signals are given, returns a list of all signals which are currently
3228     being handled.
3229     If signals are specified, these are added to the list of signals currently
3230     being handled.
3232 +*signal ignore* ?'signals ...'?+::
3233     If no signals are given, returns a lists all signals which are currently
3234     being ignored.
3235     If signals are specified, these are added to the list of signals
3236     currently being ignored. These signals are still delivered, but
3237     are not considered by 'catch -signal' or 'try -signal'. Use
3238     'signal check' to determine which signals have occurred but
3239     been ignored.
3241 +*signal default* ?'signals ...'?+::
3242     If no signals are given, returns a lists all signals which are currently have
3243     the default behaviour.
3244     If signals are specified, these are added to the list of signals which have
3245     the default behaviour.
3247 +*signal check ?-clear?* ?'signals ...'?+::
3248     Returns a list of signals which have been delivered to the process
3249     but are 'ignored'. If signals are specified, only that set of signals will
3250     be checked, otherwise all signals will be checked.
3251     If '-clear' is specified, any signals returned are removed and will not be
3252     returned by subsequent calls to 'signal check' unless delivered again.
3254 +*signal throw* ?'signal'?+::
3255     Raises the given signal, which defaults to +SIGINT+ if not specified.
3256     The behaviour is identical to:
3258         kill signal [pid]
3260 Note that 'signal handle' and 'signal ignore' represent two forms of signal
3261 handling. 'signal handle' is used in conjunction with 'catch -signal' or 'try -signal'
3262 to immediately abort execution when the signal is delivered. Alternatively, 'signal ignore'
3263 is used in conjunction with 'signal check' to handle signal synchronously. Consider the
3264 two examples below.
3266 Prevent a processing from taking too long
3268     signal handle SIGALRM
3269     alarm 20
3270     try -signal {
3271         .. possibly long running process ..
3272         alarm 0
3273     } on signal {sig} {
3274         puts stderr "Process took too long"
3275     }
3277 Handle SIGHUP to reconfigure:
3279     signal ignore SIGHUP
3280     while {1} {
3281         ... handle configuration/reconfiguration ...
3282         while {[signal check -clear SIGHUP] eq ""} {
3283             ... do processing ..
3284         }
3285         # Received SIGHUP, so reconfigure
3286     }
3288 sleep
3289 ~~~~~
3290 +*sleep* 'seconds'+
3292 Pauses for the given number of seconds, which may be a floating
3293 point value less than one to sleep for less than a second, or an
3294 integer to sleep for one or more seconds.
3296 source
3297 ~~~~~~
3298 +*source* 'fileName'+
3300 Read file *fileName* and pass the contents to the Tcl interpreter
3301 as a sequence of commands to execute in the normal fashion.  The return
3302 value of 'source' is the return value of the last command executed
3303 from the file.  If an error occurs in executing the contents of the
3304 file, then the 'source' command will return that error.
3306 If a 'return' command is invoked from within the file, the remainder of
3307 the file will be skipped and the 'source' command will return
3308 normally with the result from the 'return' command.
3310 split
3311 ~~~~~
3312 +*split* 'string ?splitChars?'+
3314 Returns a list created by splitting *string* at each character
3315 that is in the *splitChars* argument.
3317 Each element of the result list will consist of the
3318 characters from *string* between instances of the
3319 characters in *splitChars*.
3321 Empty list elements will be generated if *string* contains
3322 adjacent characters in *splitChars*, or if the first or last
3323 character of *string* is in *splitChars*.
3325 If *splitChars* is an empty string then each character of
3326 *string* becomes a separate element of the result list.
3328 *SplitChars* defaults to the standard white-space characters.
3329 For example,
3331     split "comp.unix.misc" .
3333 returns +'"comp unix misc"'+ and
3335     split "Hello world" {}
3337 returns +'"H e l l o { } w o r l d"'+.
3339 stackdump
3340 ~~~~~~~~~
3342 +*stackdump* 'stacktrace'+
3344 Creates a human readable representation of a stack trace.
3346 stacktrace
3347 ~~~~~~~~~~
3349 +*stacktrace*+
3351 Returns a live stack trace as a list of +proc file line proc file line ...+.
3352 Iteratively uses 'info frame' to create the stack trace. This stack trace is in the
3353 same form as produced by 'catch' and 'info stacktrace'
3355 See also 'stackdump'.
3357 string
3358 ~~~~~~
3360 +*string* 'option arg ?arg ...?'+
3362 Perform one of several string operations, depending on *option*.
3363 The legal options (which may be abbreviated) are:
3365 +*string compare ?-nocase?* 'string1 string2'+::
3366     Perform a character-by-character comparison of strings *string1* and
3367     *string2* in the same way as the C 'strcmp' procedure.  Return
3368     -1, 0, or 1, depending on whether *string1* is lexicographically
3369     less than, equal to, or greater than *string2*.
3370     Performs a case-insensitive comparison if '-nocase' is specified.
3372 +*string equal ?-nocase?* 'string1 string2'+::
3373     Returns 1 if the strings are equal, or 0 otherwise.
3374     Performs a case-insensitive comparison if '-nocase' is specified.
3376 +*string first* 'string1 string2 ?firstIndex?'+::
3377     Search *string2* for a sequence of characters that exactly match
3378     the characters in *string1*.  If found, return the index of the
3379     first character in the first such match within *string2*.  If not
3380     found, return -1. If *firstIndex* is specified, matching will start
3381     from *firstIndex* of *string1*.
3382  ::
3383     See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *firstIndex*.
3385 +*string index* 'string charIndex'+::
3386     Returns the *charIndex*'th character of the *string*
3387     argument.  A *charIndex* of 0 corresponds to the first
3388     character of the string.
3389     If *charIndex* is less than 0 or greater than
3390     or equal to the length of the string then an empty string is
3391     returned.
3392  ::
3393     See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *charIndex*.
3395 +*string is* 'class' ?*-strict*? 'string'+::
3396  Returns 1 if *string* is a valid member of the specified character
3397  class, otherwise returns 0. If '-strict' is specified, then an
3398  empty string returns 0, otherwise an empty string will return 1
3399  on any class. The following character classes are recognized
3400  (the class name can be abbreviated):
3401   +alnum+;;  Any alphabet or digit character.
3402   +alpha+;;  Any alphabet character.
3403   +ascii+;;  Any character with a value less than 128 (those that are in the 7-bit ascii range).
3404   +control+;; Any control character.
3405   +digit+;;  Any digit character.
3406   +double+;; Any of the valid forms for a double in Tcl, with optional surrounding whitespace.
3407              In case of under/overflow in the value, 0 is returned.
3408   +graph+;;  Any printing character, except space.
3409   +integer+;; Any of the valid string formats for an integer value in Tcl, with optional surrounding whitespace.
3410   +lower+;;  Any lower case alphabet character.
3411   +print+;;  Any printing character, including space.
3412   +punct+;;  Any punctuation character.
3413   +space+;;  Any space character.
3414   +upper+;;  Any upper case alphabet character.
3415   +xdigit+;; Any hexadecimal digit character ([0-9A-Fa-f]).
3417 +*string last* 'string1 string2 ?lastIndex?'+::
3418     Search *string2* for a sequence of characters that exactly match
3419     the characters in *string1*.  If found, return the index of the
3420     first character in the last such match within *string2*.  If there
3421     is no match, then return -1. If *lastIndex* is specified, only characters
3422     up to *lastIndex* of *string2* will be considered in the match.
3423  ::
3424     See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *lastIndex*.
3426 +*string length* 'string'+::
3427     Returns a decimal string giving the number of characters in *string*.
3429 +*string match ?-nocase?* 'pattern string'+::
3430     See if *pattern* matches *string*; return 1 if it does, 0
3431     if it doesn't.  Matching is done in a fashion similar to that
3432     used by the C-shell.  For the two strings to match, their contents
3433     must be identical except that the following special sequences
3434     may appear in *pattern*:
3436     +*+;;
3437         Matches any sequence of characters in *string*,
3438         including a null string.
3440     +?+;;
3441         Matches any single character in *string*.
3443     +[*chars*]+;;
3444         Matches any character in the set given by *chars*.
3445         If a sequence of the form *x*-*y* appears in *chars*,
3446         then any character between *x* and *y*, inclusive,
3447         will match.
3449     +\x+;;
3450         Matches the single character *x*.  This provides a way of
3451         avoiding the special interpretation of the characters \`\*?[]\`
3452         in **pattern**.
3453  ::
3454     Performs a case-insensitive comparison if '-nocase' is specified.
3456 +*string range* 'string first last'+::
3457     Returns a range of consecutive characters from *string*, starting
3458     with the character whose index is *first* and ending with the
3459     character whose index is *last*.  An index of 0 refers to the
3460     first character of the string.
3461  ::
3462     See STRING AND LIST INDEX SPECIFICATIONS for all allowed forms for *first* and *last*.
3463  ::
3464     If *first* is less than zero then it is treated as if it were zero, and
3465     if *last* is greater than or equal to the length of the string then
3466     it is treated as if it were 'end'.  If *first* is greater than
3467     *last* then an empty string is returned.
3469 +*string repeat* 'string count'+::
3470     Returns a new string consisting of *string* repeated *count* times.
3472 +*string reverse* 'string'+::
3473     Returns a string that is the same length as *string* but
3474     with its characters in the reverse order.
3476 +*string tolower* 'string'+::
3477     Returns a value equal to *string* except that all upper case
3478     letters have been converted to lower case.
3480 +*string toupper* 'string'+::
3481     Returns a value equal to *string* except that all lower case
3482     letters have been converted to upper case.
3484 +*string trim* 'string ?chars?'+::
3485     Returns a value equal to *string* except that any leading
3486     or trailing characters from the set given by *chars* are
3487     removed.
3488     If *chars* is not specified then white space is removed
3489     (spaces, tabs, newlines, and carriage returns).
3491 +*string trimleft* 'string ?chars?'+::
3492     Returns a value equal to *string* except that any
3493     leading characters from the set given by *chars* are
3494     removed.
3495     If *chars* is not specified then white space is removed
3496     (spaces, tabs, newlines, and carriage returns).
3498 +*string trimright* 'string ?chars?'+::
3499     Returns a value equal to *string* except that any
3500     trailing characters from the set given by *chars* are
3501     removed.
3502     If *chars* is not specified then white space is removed
3503     (spaces, tabs, newlines, and carriage returns).
3505 subst
3506 ~~~~~
3507 +*subst ?-nobackslashes? ?-nocommands? ?-novariables?* 'string'+
3509 This command performs variable substitutions, command substitutions,
3510 and backslash substitutions on its string argument and returns the
3511 fully-substituted result. The substitutions are performed in exactly
3512 the same way as for Tcl commands. As a result, the string argument
3513 is actually substituted twice, once by the Tcl parser in the usual
3514 fashion for Tcl commands, and again by the subst command.
3516 If any of the *-nobackslashes*, *-nocommands*, or *-novariables* are
3517 specified, then the corresponding substitutions are not performed.
3518 For example, if *-nocommands* is specified, no command substitution
3519 is performed: open and close brackets are treated as ordinary
3520 characters with no special interpretation.
3522 *Note*: when it performs its substitutions, subst does not give any
3523 special treatment to double quotes or curly braces. For example,
3524 the following script returns 'xyz \{44\}', not 'xyz \{$a\}'.
3526     set a 44
3527     subst {xyz {$a}}
3530 switch
3531 ~~~~~~
3532 +*switch* '?options? string pattern body ?pattern body ...?'+
3534 +*switch* '?options? string {pattern body ?pattern body ...?}'+
3536 The 'switch' command matches its string argument against each of
3537 the pattern arguments in order. As soon as it finds a pattern that
3538 matches string it evaluates the following body and returns the
3539 result of that evaluation. If the last pattern argument is default
3540 then it matches anything. If no pattern argument matches string and
3541 no default is given, then the switch command returns an empty string.
3542 If the initial arguments to switch start with - then they are treated
3543 as options. The following options are currently supported:
3545     +-exact+::
3546         Use exact matching when comparing string to a
3547         pattern. This is the default.
3549     +-glob+::
3550         When matching string to the patterns, use glob-style
3551         matching (i.e. the same as implemented by the string
3552         match command).
3554     +-regexp+::
3555         When matching string to the patterns, use regular
3556         expression matching (i.e. the same as implemented
3557         by the regexp command).
3559     +-command 'commandname'+::
3560         When matching string to the patterns, use the given command, which
3561         must be a single word. The command is invoked as
3562         'commandname pattern string', or 'commandname -nocase pattern string'
3563         and must return 1 if matched, or 0 if not.
3565     +--+::
3566         Marks the end of options. The argument following
3567         this one will be treated as string even if it starts
3568         with a -.
3570 Two syntaxes are provided for the pattern and body arguments. The
3571 first uses a separate argument for each of the patterns and commands;
3572 this form is convenient if substitutions are desired on some of the
3573 patterns or commands. The second form places all of the patterns
3574 and commands together into a single argument; the argument must
3575 have proper list structure, with the elements of the list being the
3576 patterns and commands. The second form makes it easy to construct
3577 multi-line switch commands, since the braces around the whole list
3578 make it unnecessary to include a backslash at the end of each line.
3579 Since the pattern arguments are in braces in the second form, no
3580 command or variable substitutions are performed on them; this makes
3581 the behavior of the second form different than the first form in
3582 some cases.
3584 If a body is specified as '-' it means that the body for the next
3585 pattern should also be used as the body for this pattern (if the
3586 next pattern also has a body of ``-'' then the body after that is
3587 used, and so on). This feature makes it possible to share a single
3588 body among several patterns.
3590 Below are some examples of switch commands:
3592     switch abc a - b {format 1} abc {format 2} default {format 3}
3594 will return 2,
3596     switch -regexp aaab {
3597            ^a.*b$ -
3598            b {format 1}
3599            a* {format 2}
3600            default {format 3}
3601     }
3603 will return 1, and
3605     switch xyz {
3606            a -
3607            b {format 1}
3608            a* {format 2}
3609            default {format 3}
3610     }
3612 will return 3.
3614 tailcall
3615 ~~~~~~~~
3616 +*tailcall* 'cmd ?arg...?'+
3618 The 'tailcall' command provides an optimised way of invoking a command whilst replacing
3619 the current call frame. This is similar to 'exec' in Bourne Shell.
3621 The following are identical except the first immediately replaces the current call frame.
3623   tailcall a b c
3625   return [uplevel 1 a b c]
3627 'tailcall' is useful for a dispatch mechanism:
3629   proc a {cmd args} {
3630     tailcall sub_$cmd {*}$args
3631   }
3632   proc sub_cmd1 ...
3633   proc sub_cmd2 ...
3635 tell
3636 ~~~~
3637 +*tell* 'fileId'+
3639 +'fileId' *tell*+
3641 Returns a decimal string giving the current access position in
3642 *fileId*.
3644 *fileId* must have been the return value from a previous call to
3645 'open', or it may be 'stdin', 'stdout', or 'stderr' to refer to one
3646 of the standard I/O channels.
3648 throw
3649 ~~~~~
3650 +*throw* 'code ?msg?'+
3652 This command throws an exception (return) code along with an optional message.
3653 This command is mostly for convenient usage with 'try'.
3655 The command +throw break+ is equivalent to +break+.
3656 The command +throw 20 message+ can be caught with an +on 20 ...+ clause to 'try'.
3658 time
3659 ~~~~
3660 +*time* 'command ?count?'+
3662 This command will call the Tcl interpreter *count*
3663 times to execute *command* (or once if *count* isn't
3664 specified).  It will then return a string of the form
3666     503 microseconds per iteration
3668 which indicates the average amount of time required per iteration,
3669 in microseconds.
3671 Time is measured in elapsed time, not CPU time.
3675 +*try* '?catchopts? tryscript' ?*on* 'returncodes {?resultvar? ?optsvar?} handlerscript ...'? ?*finally* 'finalscript'?+
3677 The 'try' command is provided as a convenience for exception handling.
3679 This interpeter first evaluates *tryscript* under the effect of the catch
3680 options *catchopts* (e.g. +-signal -noexit --+, see 'catch').
3682 It then evaluates the script for the first matching 'on' handler
3683 (there many be zero or more) based on the return code from the 'try'
3684 section. For example a normal +JIM_ERR+ error will be matched by
3685 an 'on error' handler.
3687 Finally, any *finalscript* is evaluated.
3689 The result of this command is the result of *tryscript*, except in the
3690 case where an exception occurs in a matching 'on' handler script or the 'finally' script,
3691 in which case the result is this new exception.
3693 The specified *returncodes* is a list of return codes either as names ('ok', 'error', 'break', etc.)
3694 or as integers.
3696 If *resultvar* and *optsvar* are specified, they are set as for 'catch' before evaluating
3697 the matching handler.
3699 For example:
3701     set f [open input]
3702     try -signal {
3703         process $f
3704     } on {continue break} {} {
3705         error "Unexpected break/continue"
3706     } on error {msg opts} {
3707         puts "Dealing with error"
3708         return {*}$opts $msg
3709     } on signal sig {
3710         puts "Got signal: $sig"
3711     } finally {
3712         $f close
3713     }
3715 If break, continue or error are raised, they are dealt with by the matching
3716 handler.
3718 In any case, the file will be closed via the 'finally' clause.
3720 See also 'throw', 'catch', 'return', 'error'.
3722 unknown
3723 ~~~~~~~
3724 +*unknown* 'cmdName ?arg arg ...?'+
3726 This command doesn't actually exist as part of Tcl, but Tcl will
3727 invoke it if it does exist.
3729 If the Tcl interpreter encounters a command name for which there
3730 is not a defined command, then Tcl checks for the existence of
3731 a command named 'unknown'.
3733 If there is no such command, then the interpeter returns an
3734 error.
3736 If the 'unknown' command exists, then it is invoked with
3737 arguments consisting of the fully-substituted name and arguments
3738 for the original non-existent command.
3740 The 'unknown' command typically does things like searching
3741 through library directories for a command procedure with the name
3742 *cmdName*, or expanding abbreviated command names to full-length,
3743 or automatically executing unknown commands as UNIX sub-processes.
3745 In some cases (such as expanding abbreviations) 'unknown' will
3746 change the original command slightly and then (re-)execute it.
3747 The result of the 'unknown' command is used as the result for
3748 the original non-existent command.
3750 unset
3751 ~~~~~
3752 +*unset ?-nocomplain? ?--?* '?name name ...?'+
3754 Remove variables.
3755 Each *name* is a variable name, specified in any of the
3756 ways acceptable to the 'set' command.
3758 If a *name* refers to an element of an array, then that
3759 element is removed without affecting the rest of the array.
3761 If a *name* consists of an array name with no parenthesized
3762 index, then the entire array is deleted.
3764 The 'unset' command returns an empty string as result.
3766 An error occurs if any of the variables doesn't exist, unless '-nocomplain'
3767 is specified. The '--' argument may be specified to stop option processing
3768 in case the variable name may be '-nocomplain'.
3770 uplevel
3771 ~~~~~~~
3772 +*uplevel* '?level? command ?command ...?'+
3774 All of the *command* arguments are concatenated as if they had
3775 been passed to 'concat'; the result is then evaluated in the
3776 variable context indicated by *level*.  'Uplevel' returns
3777 the result of that evaluation.  If *level* is an integer, then
3778 it gives a distance (up the procedure calling stack) to move before
3779 executing the command.  If *level* consists of '\#' followed by
3780 a number then the number gives an absolute level number.  If *level*
3781 is omitted then it defaults to '1'.  *Level* cannot be
3782 defaulted if the first *command* argument starts with a digit or '\#'.
3784 For example, suppose that procedure 'a' was invoked
3785 from top-level, and that it called 'b', and that 'b' called 'c'.
3786 Suppose that 'c' invokes the 'uplevel' command.  If *level*
3787 is '1' or '\#2'  or omitted, then the command will be executed
3788 in the variable context of 'b'.  If *level* is '2' or '\#1'
3789 then the command will be executed in the variable context of 'a'.
3791 If *level* is '3' or '\#0' then the command will be executed
3792 at top-level (only global variables will be visible).
3793 The 'uplevel' command causes the invoking procedure to disappear
3794 from the procedure calling stack while the command is being executed.
3795 In the above example, suppose 'c' invokes the command
3797     uplevel 1 {set x 43; d}
3799 where 'd' is another Tcl procedure.  The 'set' command will
3800 modify the variable 'x' in 'b's context, and 'd' will execute
3801 at level 3, as if called from 'b'.  If it in turn executes
3802 the command
3804     uplevel {set x 42}
3806 then the 'set' command will modify the same variable 'x' in 'b's
3807 context:  the procedure 'c' does not appear to be on the call stack
3808 when 'd' is executing.  The command 'info level' may
3809 be used to obtain the level of the current procedure.
3811 'Uplevel' makes it possible to implement new control
3812 constructs as Tcl procedures (for example, 'uplevel' could
3813 be used to implement the 'while' construct as a Tcl procedure).
3815 upvar
3816 ~~~~~
3817 +*upvar* '?level? otherVar myVar ?otherVar myVar ...?'+
3819 This command arranges for one or more local variables in the current
3820 procedure to refer to variables in an enclosing procedure call or
3821 to global variables.
3823 *Level* may have any of the forms permitted for the 'uplevel'
3824 command, and may be omitted if the first letter of the first *otherVar*
3825 isn't '\#' or a digit (it defaults to '1').
3827 For each *otherVar* argument, 'upvar' makes the variable
3828 by that name in the procedure frame given by *level* (or at
3829 global level, if *level* is '\#0') accessible
3830 in the current procedure by the name given in the corresponding
3831 *myVar* argument.
3833 The variable named by *otherVar* need not exist at the time of the
3834 call;  it will be created the first time *myVar* is referenced, just like
3835 an ordinary variable.
3837 'Upvar' may only be invoked from within procedures.
3839 'Upvar' returns an empty string.
3841 The 'upvar' command simplifies the implementation of call-by-name
3842 procedure calling and also makes it easier to build new control constructs
3843 as Tcl procedures.
3844 For example, consider the following procedure:
3846     proc add2 name {
3847         upvar $name x
3848         set x [expr $x+2]
3849     }
3851 'Add2' is invoked with an argument giving the name of a variable,
3852 and it adds two to the value of that variable.
3853 Although 'add2' could have been implemented using 'uplevel'
3854 instead of 'upvar', 'upvar' makes it simpler for 'add2'
3855 to access the variable in the caller's procedure frame.
3857 while
3858 ~~~~~
3859 +*while* 'test body'+
3861 The *while* command evaluates *test* as an expression
3862 (in the same way that 'expr' evaluates its argument).
3863 The value of the expression must be numeric; if it is non-zero
3864 then *body* is executed by passing it to the Tcl interpreter.
3866 Once *body* has been executed then *test* is evaluated
3867 again, and the process repeats until eventually *test*
3868 evaluates to a zero numeric value.  'Continue'
3869 commands may be executed inside *body* to terminate the current
3870 iteration of the loop, and 'break'
3871 commands may be executed inside *body* to cause immediate
3872 termination of the 'while' command.
3874 The 'while' command always returns an empty string.
3876 OPTIONAL-EXTENSIONS
3877 -------------------
3879 The following extensions may or may not be available depending upon
3880 what options were selected when Jim Tcl was built.
3884 The bio command provides a way to read and write binary files
3885 from within Tcl. Note that since Jim supports binary strings, the
3886 main use of this command is 'bio copy' to easily copy between file
3887 descriptors.
3889 +*bio read ?-hex?* 'fd var numbytes'+::
3890     Read bytes from a file descriptor. By default the data is not encoded.
3891     Using *-hex* encodes the data as ascii hex instead. Returns
3892     the number of bytes actually read.
3894 +*bio write ?-hex?* 'fd buf'+::
3895     Write a string to a file descriptor. If *-hex* is specified, the
3896     string is expected to be in ascii hexx format. Returns the number
3897     of bytes actually written.
3899 +*bio copy* 'fromfd tofd ?numbytes?'+::
3900     Copy binary data from the file descriptor *fromfd* to the
3901     file descriptor *tofd*. If *numbytes* is specified, at most that many
3902     bytes will be copied. Otherwise copying continues until the end
3903     of the input file. Returns the number of bytes actually copied.
3905 posix: os.fork, os.wait, os.gethostname, os.getids, os.uptime
3906 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3907 +*os.fork*+::
3908     Invokes 'fork(2)' and returns the result.
3910 +*os.wait -nohang* 'pid'+::
3911     Invokes waitpid(2), with WNOHANG if *-nohang* is specified.
3912     Returns a list of 3 elements.
3914    {0 none 0} if -nohang is specified, and the process is still alive.
3916    {-1 error <error-description>} if the process does not exist or has already been waited for.
3918    {<pid> exit <exit-status>} if the process exited normally.
3920    {<pid> signal <signal-number>} if the process terminated on a signal.
3922    {<pid> other 0} otherwise (core dump, stopped, continued, etc.)
3924 +*os.gethostname*+::
3925     Invokes 'gethostname(3)' and returns the result.
3927 +*os.getids*+::
3928     Returns the various user/group ids for the current process.
3930     jim> os.getids
3931     uid 1000 euid 1000 gid 100 egid 100
3933 +*os.uptime*+::
3934     Returns the number of seconds since system boot. See description of 'uptime' in 'sysinfo(2)'.
3936 ANSI I/O (aio) and EVENTLOOP API
3937 --------------------------------
3938 Jim provides an alternative object-based API for I/O.
3940 See '<<_open,open>>' and '<<_socket,socket>>' for commands which return an I/O handle.
3944 +$handle *read ?-nonewline?* '?len?'+::
3945     Read and return bytes from the stream. To eof if no len.
3947 +$handle *gets* '?var?'+::
3948     Read one line and return it or store it in the var
3950 +$handle *puts ?-nonewline?* 'str'+::
3951     Write the string, with newline unless -nonewline
3953 +$handle *flush*+::
3954     Flush the stream
3956 +$handle *eof*+::
3957     Returns 1 if stream is at eof
3959 +$handle *close*+::
3960     Closes the stream
3962 +$handle *seek* 'offset' *?start|current|end?*+::
3963     Seeks in the stream (default 'current')
3965 +$handle *tell*+::
3966     Returns the current seek position
3968 +$handle *ndelay ?0|1?*+::
3969     Set O_NDELAY (if arg). Returns current/new setting.
3970     Note that in general ANSI I/O interacts badly with non-blocking I/O.
3971     Use with care.
3973 +$handle *accept*+::
3974     Server socket only: Accept a connection and return stream
3976 +$handle *sendto* 'str ?hostname:?port'+::
3977     Sends the string, *str*, to the given address via the socket using sendto(2).
3978     This is intended for udp sockets and may give an error or behave in unintended
3979     ways for other handle types.
3980     Returns the number of bytes written.
3982 +$handle *recvfrom* 'maxlen ?addrvar?'+::
3983     Receives a message from the handle via recvfrom(2) and returns it.
3984     At most *maxlen* bytes are read.
3985     If *addrvar* is specified, the sending address of the message is stored in
3986     the named variable in the form 'addr:port'. See 'socket' for details.
3988 eventloop: after, vwait
3989 ~~~~~~~~~~~~~~~~~~~~~~~
3991 The following commands allow a script to be invoked when the given condition occurs.
3993 +$handle *readable* '?readable-script ?eof-script??'+::
3994     Returns script, or invoke readable-script when readable, eof-script on eof, {} to remove
3996 +$handle *writable* '?writable-script?'+::
3997     Returns script, or invoke writable-script when writable, {} to remove
3999 +$handle *onexception* '?exception-script?'+::
4000     Returns script, or invoke exception-script when oob data, {} to remove
4002 Time-based execution is also available via the eventloop API.
4004 +*after* 'time script'+::
4005         The script is executed after the given number of milliseconds have elapsed.
4006         Returns an event id.
4008 +*after cancel* 'id'+::
4009         Cancels an after event with the given event id.
4011 +*vwait* 'variable'+::
4012         A call to vwait is required to enter the eventloop. 'vwait' processes events until
4013         the named (global) variable changes. The variable need not exist beforehand.
4014         If there are no event handlers defined, 'vwait' returns immediately.
4016 Scripts are executed at the global scope. If an error occurs during a handler script,
4017 an attempt is made to call (the user-defined command) 'bgerror' with the details of the error.
4018 If the 'bgerror' commands does not exist, it is printed to stderr instead.
4020 If a file event handler script generates an error, the handler is automatically removed
4021 to prevent infinite errors. (A time event handler is always removed after execution).
4023 +*bgerror* 'error'+::
4024         Called when an event handler script generates an error.
4025         
4027 socket
4028 ~~~~~~
4029 Various socket types may be created.
4031 +*socket unix* 'path'+::
4032     A unix domain socket client.
4034 +*socket unix.server* 'path'+::
4035     A unix domain socket server.
4037 +*socket ?-ipv6? stream* 'addr:port'+::
4038     A TCP socket client.
4040 +*socket ?-ipv6? stream.server '?addr:?port'+::
4041     A TCP socket server (*addr* defaults to 0.0.0.0 for IPv4 or [::] for IPv6).
4043 +*socket ?-ipv6? dgram* ?'addr:port'?+::
4044     A UDP socket client. If the address is not specified,
4045     the client socket will be unbound and 'sendto' must be used
4046     to indicated the destination.
4048 +*socket ?-ipv6? dgram.server* 'addr:port'+::
4049     A UDP socket server.
4051 +*socket pipe*+::
4052     A pipe. Note that unlike all other socket types, this command returns
4053     a list of two channels: {read write}
4055 This command creates a socket connected (client) or bound (server) to the given
4056 address.
4058 The returned value is channel and may generally be used with the various file I/O
4059 commands (gets, puts, read, etc.), either as object-based syntax or Tcl-compatible syntax.
4061     set f [socket stream www.google.com:80]
4062     aio.sockstream1
4063     $f puts -nonewline "GET / HTTP/1.0\r\n\r\n"
4064     $f gets
4065     HTTP/1.0 302 Found
4066     $f close
4068 Server sockets, however support only 'accept', which is most useful in conjunction with
4069 the EVENTLOOP API.
4071     set f [socket stream.server 80]
4072     $f readable {
4073         set client [$f accept]
4074         $client gets $buf
4075         ...
4076         $client puts -nonewline "HTTP/1.1 404 Not found\r\n"
4077         $client close
4078     }
4079     vwait done
4081 The address, *addr*, can be given in one of the following forms:
4083 1. For IPv4 socket types, an IPv4 address such as 192.168.1.1
4084 2. For IPv6 socket types, an IPv6 address such as [fe80::1234] or [::]
4085 3. A hostname
4087 Note that on many systems, listening on an IPv6 address such as [::] will
4088 also accept requests via IPv4.
4090 Where a hostname is specified, the *first* returned address is used
4091 which matches the socket type is used.
4093 The special type 'pipe' isn't really a socket.
4095     lassign [socket pipe] r w
4097     # Must close $w after exec
4098     exec ps >@$w &
4099     $w close
4101     $r readable ...
4103 syslog
4104 ~~~~~~
4105 +*syslog* '?options? ?priority? message'+
4107 This  command sends message to system syslog facility with given
4108 priority. Valid priorities are:
4110     emerg, alert, crit, err, error, warning, notice, info, debug
4112 If a message is specified, but no priority is specified, then a
4113 priority of info is used.
4115 By default, facility user is used and the value of global tcl variable
4116 argv0 is used as ident string. However, any of the following options
4117 may be specified before priority to control these parameters:
4119 +*-facility* 'value'+::
4120     Use specified facility instead of user. The following
4121     values for facility are recognized:
4123     authpriv, cron, daemon, kernel, lpr, mail, news, syslog, user,
4124     uucp, local0-local7
4126 +*-ident* 'string'+::
4127     Use given string instead of argv0 variable for ident string.
4129 +*-options* 'integer'+::
4130     Set syslog options such as LOG_CONS, LOG_NDELAY You should
4131     use numeric values of those from your system syslog.h file,
4132     because I haven't got time to implement yet another hash
4133     table.
4135 [[BuiltinVariables]]
4136 BUILT-IN VARIABLES
4137 ------------------
4139 The following global variables are created automatically
4140 by the Tcl library.
4142 +*env*+::
4143     This variable is set by Jim as an array
4144     whose elements are the environment variables for the process.
4145     Reading an element will return the value of the corresponding
4146     environment variable.
4147     This array is initialised at startup from the 'env' command.
4149 +*auto_path*+::
4150     This variable contains a list of paths to search for packages.
4151     It contains {. /lib/jim} by default.
4153 The following global variables are set by jimsh.
4155 +*tcl_interactive*+::
4156     This variable is set to 1 if jimsh is started in interactive mode
4157     or 0 otherwise.
4159 +*argv0*+::
4160     If jimsh is invoked to run a script, this variable contains the name
4161     of the script.
4163 +*argv*+::
4164     If jimsh is invoked to run a script, this variable contains a list
4165     of any arguments supplied to the script.
4167 +*jim_argv0*+::
4168     The value of argv[0] when jimsh was invoked.
4170 LICENCE
4171 -------
4173  Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
4174  Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
4175  Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net> 
4176  Copyright 2008 oharboe - Oyvind Harboe - oyvind.harboe@zylin.com
4177  Copyright 2008 Andrew Lunn <andrew@lunn.ch>
4178  Copyright 2008 Duane Ellis <openocd@duaneellis.com>
4179  Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
4180  Copyright 2009 Steve Bennett <steveb@workware.net.au>
4182  The FreeBSD license
4184 [literal] 
4185  Redistribution and use in source and binary forms, with or without
4186  modification, are permitted provided that the following conditions
4187  are met:
4188  1. Redistributions of source code must retain the above copyright
4189     notice, this list of conditions and the following disclaimer.
4190  2. Redistributions in binary form must reproduce the above
4191     copyright notice, this list of conditions and the following
4192     disclaimer in the documentation and/or other materials
4193     provided with the distribution.
4195  THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
4196  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
4197  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
4198  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
4199  JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
4200  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
4201  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4202  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4203  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4204  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4205  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
4206  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4208  The views and conclusions contained in the software and documentation
4209  are those of the authors and should not be interpreted as representing
4210  official policies, either expressed or implied, of the Jim Tcl Project.