-
Notifications
You must be signed in to change notification settings - Fork 123
/
jim_tcl.txt
6151 lines (4820 loc) · 224 KB
/
jim_tcl.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Jim Tcl(n)
==========
NAME
----
Jim Tcl v0.83+ - reference manual for the Jim Tcl scripting language
SYNOPSIS
--------
cc <source> -ljim
or
jimsh [<scriptfile>|-]
jimsh -e '<immediate-script>'
jimsh --version
jimsh --help
.Quick Index
* <<CommandIndex,Command Reference>>
* <<OperatorPrecedence,Operator Precedence>>
* <<BuiltinVariables, Builtin Variables>>
* <<BackslashSequences, Backslash Sequences>>
INTRODUCTION
------------
Jim Tcl is a small footprint reimplementation of the Tcl scripting language.
The core language engine is compatible with Tcl 8.5+, while implementing
a significant subset of the Tcl 8.6 command set, plus additional features
available only in Jim Tcl.
Some notable differences with Tcl 8.5/8.6/8.7 are:
1. Object-based I/O (aio), but with a Tcl-compatibility layer
2. I/O: Support for sockets and pipes including udp, unix domain sockets and IPv6
3. Integers are 64bit
4. Support for references (`ref`/`getref`/`setref`) and garbage collection
5. Builtin dictionary type (`dict`) with some limitations compared to Tcl 8.6
6. `env` command to access environment variables
7. Operating system features: `os.fork`, `os.uptime`, `wait`, `signal`, `alarm`, `sleep`
8. Much better error reporting. `info stacktrace` as a replacement for '$errorInfo', '$errorCode'
9. Support for "static" variables in procedures
10. Threads and coroutines are not supported
11. Command and variable traces are not supported
12. Built-in command line editing
13. Expression shorthand syntax: +$(...)+
14. Modular build allows many features to be omitted or built as dynamic, loadable modules
15. Highly suitable for use in an embedded environment
16. Support for UDP, IPv6, Unix-Domain sockets in addition to TCP sockets
RECENT CHANGES
--------------
Changes since 0.83
~~~~~~~~~~~~~~~~~~
1. `aio` - support for configurable read and write buffering
Changes between 0.82 and 0.83
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Multi-level `break` and `continue` are now supported
2. `info frame` now only returns 'proc' levels
3. `stacktrace` is now a builtin command
4. The stack trace on error now includes the full stack trace, not just back to where it was caught
5. Improvements with `aio`, related to eventloop and buffering. Add `aio timeout`.
6. `socket` , `open` and `aio accept` now support '-noclose'
7. Add support for hinting with `history hints`
8. Support for `proc` statics by reference (lexical closure) rather than by value
9. `regsub` now supports '-command' (per Tcl 8.7)
10. Add support for `lsort -dict`
Changes between 0.81 and 0.82
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. `try` now supports trap to match on errorcode
2. TIP 603, `aio stat` is now supported to stat a file handle
3. Add support for `socket -async`
4. The handles created by `socket pty` now make the replica name available via 'filename'
5. `info frame` now returns a (largely) Tcl-compatible dictionary, and supports 'info frame 0'
6. `vwait -signal` is now supported
7. ./configure now defaults to '--full'
8. New `timerate` command as an improvement over `time`, somewhat compatible with TIP 527
9. Add `ensemble` command and support for `namespace ensemble` (as an optional extension)
Changes between 0.80 and 0.81
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. TIP 582, comments allowed in expressions
2. Many commands now accept "safe" integer expressions rather than simple integers:
`loop`, `range`, `incr`, `string repeat`, `lrepeat`, `pack`, `unpack`, `rand`
3. String and list indexes now accept integer expressions (<<_string_and_list_index_specifications,STRING AND LIST INDEX SPECIFICATIONS>>)
4. `loop` can now omit the start value
5. Add the `xtrace` command for execution trace support
6. Add `history keep`
7. Add support for `lsearch -index` and `lsearch -stride`, the latter per TIP 351
8. `lsort -index` now supports multiple indices
9. Add support for `lsort -stride`
10. `open` now supports POSIX-style access arguments
11. TIP 526, `expr` now only allows a single argument (unless --compat is enabled)
Changes between 0.79 and 0.80
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. `regsub` now fully supports +{backslash}A+
2. Add `socket pty` to create a pseudo-tty pair
3. Null characters (\x00) are now supported in variable and proc names
4. dictionaries and arrays now preserve insertion order, matching Tcl and the documentation
5. Add `dict getwithdefault` (and the alias `dict getdef`) per TIP 342
6. Add string comparison operators (lt, gt, le, ge) per TIP 461
7. Implement 0d radix prefix for decimal per TIP 472
Changes between 0.78 and 0.79
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Add `file mtimeus` for high resolution file timestamps
2. `aio` now supports datagram Unix-Domain sockets
3. Add support for `aio lock -wait`
4. Add `signal block` to prevent delivery of signals
5. Add support for `file split`
6. Add support for `json::encode` and `json::decode`
7. `aio tty` now allows setting +echo+ without full +raw+ mode
Changes between 0.77 and 0.78
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Add serial/tty support with `aio tty`
2. Add support for 'jimsh -'
3. Add hidden '-commands' option to many commands
4. Add scriptable autocompletion support in interactive mode with `tcl::autocomplete`
5. Add `aio sockopt`
6. Add scriptable autocompletion support with `history completion`
7. Add support for `tree delete`
8. Add support for `defer` and '$jim::defer'
9. Renamed `os.wait` to `wait`, now more Tcl-compatible and compatible with `exec ... &`
10. `pipe` is now a synonym for `socket pipe`
11. Closing a pipe open with `open |...` now returns Tcl-like status
12. It is now possible to used `exec` redirection with a pipe opened with `open |...`
13. Interactive line editing now supports multiline mode if $::history::multiline is set
Changes between 0.76 and 0.77
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. Add support for `aio sync`
2. Add SSL and TLS support in aio
3. Added `zlib`
4. Added support for boolean constants in `expr`
5. `string is` now supports 'boolean' class
6. Add support for `aio lock` and `aio unlock`
7. Add new `interp` command
Changes between 0.75 and 0.76
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. `glob` now supports the +-tails+ option
2. Add support for `string cat`
3. Allow `info source` to add source info
Changes between 0.74 and 0.75
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. `binary`, `pack` and `unpack` now support floating point
2. `file copy` +-force+ handles source and target as the same file
3. `format` now supports +%b+ for binary conversion
4. `lsort` now supports +-unique+ and +-real+
5. Add support for half-close with `aio close` +?r|w?+
6. Add `socket pair` for a bidirectional pipe
7. Add '--random-hash' to randomise hash tables for greater security
8. `dict` now supports 'for', 'values', 'incr', 'append', 'lappend', 'update', 'info' and 'replace'
9. `file stat` no longer requires the variable name
10. Add support for `file link`
TCL INTRODUCTION
-----------------
Tcl stands for 'tool command language' and is pronounced
'http://www.oxforddictionaries.com/definition/american_english/tickle[tickle]'.
It is actually two things: a language and a library.
First, Tcl is a simple textual language, intended primarily for
issuing commands to interactive programs such as text editors,
debuggers, illustrators, and shells. It has a simple syntax and is also
programmable, so Tcl users can write command procedures to provide more
powerful commands than those in the built-in set.
Second, Tcl is a library package that can be embedded in application
programs. The Tcl library consists of a parser for the Tcl language,
routines to implement the Tcl built-in commands, and procedures that
allow each application to extend Tcl with additional commands specific
to that application. The application program generates Tcl commands and
passes them to the Tcl parser for execution. Commands may be generated
by reading characters from an input source, or by associating command
strings with elements of the application's user interface, such as menu
entries, buttons, or keystrokes.
When the Tcl library receives commands it parses them into component
fields and executes built-in commands directly. For commands implemented
by the application, Tcl calls back to the application to execute the
commands. In many cases commands will invoke recursive invocations of the
Tcl interpreter by passing in additional strings to execute (procedures,
looping commands, and conditional commands all work in this way).
An application program gains three advantages by using Tcl for its command
language. First, Tcl provides a standard syntax: once users know Tcl,
they will be able to issue commands easily to any Tcl-based application.
Second, Tcl provides programmability. All a Tcl application needs
to do is to implement a few application-specific low-level commands.
Tcl provides many utility commands plus a general programming interface
for building up complex command procedures. By using Tcl, applications
need not re-implement these features.
Third, Tcl can be used as a common language for communicating between
applications. Inter-application communication is not built into the
Tcl core described here, but various add-on libraries, such as the Tk
toolkit, allow applications to issue commands to each other. This makes
it possible for applications to work together in much more powerful ways
than was previously possible.
Fourth, Jim Tcl includes a command processor, +jimsh+, which can be
used to run standalone Tcl scripts, or to run Tcl commands interactively.
This manual page focuses primarily on the Tcl language. It describes
the language syntax and the built-in commands that will be available
in any application based on Tcl. The individual library procedures are
described in more detail in separate manual pages, one per procedure.
JIMSH COMMAND INTERPRETER
-------------------------
A simple, but powerful command processor, +jimsh+, is part of Jim Tcl.
It may be invoked in interactive mode as:
jimsh
or to process the Tcl script in a file with:
jimsh filename
or to process the Tcl script from standard input:
jimsh -
It may also be invoked to execute an immediate script with:
jimsh -e "script"
Interactive Mode
~~~~~~~~~~~~~~~~
Interactive mode reads Tcl commands from standard input, evaluates
those commands and prints the results.
$ jimsh
Welcome to Jim version 0.73, Copyright (c) 2005-8 Salvatore Sanfilippo
. info version
0.73
. lsort [info commands p*]
package parray pid popen proc puts pwd
. foreach i {a b c} {
{> puts $i
{> }
a
b
c
. bad
invalid command name "bad"
[error] . exit
$
If +jimsh+ is configured with line editing (it is by default) and a VT-100-compatible
terminal is detected, Emacs-style line editing commands are available, including:
arrow keys, +\^W+ to erase a word, +\^U+ to erase the line, +^R+ for reverse incremental search
in history. Additionally, the +h+ command may be used to display the command history.
Command line history is automatically saved and loaded from +~/.jim_history+
In interactive mode, +jimsh+ automatically runs the script +~/.jimrc+ at startup
if it exists.
INTERPRETERS
------------
The central data structure in Tcl is an interpreter (C type 'Jim_Interp').
An interpreter consists of a set of command bindings, a set of variable
values, and a few other miscellaneous pieces of state. Each Tcl command
is interpreted in the context of a particular interpreter.
Some Tcl-based applications will maintain multiple interpreters
simultaneously, each associated with a different widget or portion of
the application. Interpreters are relatively lightweight structures.
They can be created and deleted quickly, so application programmers should
feel free to use multiple interpreters if that simplifies the application.
DATA TYPES
----------
Tcl supports only one type of data: strings. All commands, all arguments
to commands, all command results, and all variable values are strings.
Where commands require numeric arguments or return numeric results,
the arguments and results are passed as strings. Many commands expect
their string arguments to have certain formats, but this interpretation
is up to the individual commands. For example, arguments often contain
Tcl command strings, which may get executed as part of the commands.
The easiest way to understand the Tcl interpreter is to remember that
everything is just an operation on a string. In many cases Tcl constructs
will look similar to more structured constructs from other languages.
However, the Tcl constructs are not structured at all; they are just
strings of characters, and this gives them a different behaviour than
the structures they may look like.
Although the exact interpretation of a Tcl string depends on who is doing
the interpretation, there are three common forms that strings take:
commands, expressions, and lists. The major sections below discuss
these three forms in more detail.
BASIC COMMAND SYNTAX
--------------------
The Tcl language has syntactic similarities to both the Unix shells
and Lisp. However, the interpretation of commands is different
in Tcl than in either of those other two systems.
A Tcl command string consists of one or more commands separated
by newline characters or semi-colons.
Each command consists of a collection of fields separated by
white space (spaces or tabs).
The first field must be the name of a command, and the
additional fields, if any, are arguments that will be passed to
that command. For example, the command:
----
set a 22
----
has three fields: the first, `set`, is the name of a Tcl command, and
the last two, 'a' and '22', will be passed as arguments to
the `set` command. The command name may refer either to a built-in
Tcl command, an application-specific command bound in with the library
procedure 'Jim_CreateCommand', or a command procedure defined with the
`proc` built-in command.
Arguments are passed literally as text strings. Individual commands may
interpret those strings in any fashion they wish. The `set` command,
for example, will treat its first argument as the name of a variable
and its second argument as a string value to assign to that variable.
For other commands arguments may be interpreted as integers, lists,
file names, or Tcl commands.
Command names should normally be typed completely (e.g. no abbreviations).
However, if the Tcl interpreter cannot locate a command it invokes a
special command named `unknown` which attempts to find or create the
command.
For example, at many sites `unknown` will search through library
directories for the desired command and create it as a Tcl procedure if
it is found. The `unknown` command often provides automatic completion
of abbreviated commands, but usually only for commands that were typed
interactively.
It's probably a bad idea to use abbreviations in command scripts and
other forms that will be re-used over time: changes to the command set
may cause abbreviations to become ambiguous, resulting in scripts that
no longer work.
COMMENTS
--------
If the first non-blank character in a command is +\#+, then everything
from the +#+ up through the next newline character is treated as
a comment and ignored. When comments are embedded inside nested
commands (e.g. fields enclosed in braces) they must have properly-matched
braces (this is necessary because when Tcl parses the top-level command
it doesn't yet know that the nested field will be used as a command so
it cannot process the nested comment character as a comment).
GROUPING ARGUMENTS WITH DOUBLE-QUOTES
-------------------------------------
Normally each argument field ends at the next white space, but
double-quotes may be used to create arguments with embedded space.
If an argument field begins with a double-quote, then the argument isn't
terminated by white space (including newlines) or a semi-colon (see below
for information on semi-colons); instead it ends at the next double-quote
character. The double-quotes are not included in the resulting argument.
For example, the command
----
set a "This is a single argument"
----
will pass two arguments to `set`: 'a' and 'This is a single argument'.
Within double-quotes, command substitutions, variable substitutions,
and backslash substitutions still occur, as described below. If the
first character of a command field is not a quote, then quotes receive
no special interpretation in the parsing of that field.
GROUPING ARGUMENTS WITH BRACES
------------------------------
Curly braces may also be used for grouping arguments. They are similar
to quotes except for two differences. First, they nest; this makes them
easier to use for complicated arguments like nested Tcl command strings.
Second, the substitutions described below for commands, variables, and
backslashes do *not* occur in arguments enclosed in braces, so braces
can be used to prevent substitutions where they are undesirable.
If an argument field begins with a left brace, then the argument ends
at the matching right brace. Tcl will strip off the outermost layer
of braces and pass the information between the braces to the command
without any further modification. For example, in the command
----
set a {xyz a {b c d}}
----
the `set` command will receive two arguments: 'a'
and 'xyz a {b c d}'.
When braces or quotes are in effect, the matching brace or quote need
not be on the same line as the starting quote or brace; in this case
the newline will be included in the argument field along with any other
characters up to the matching brace or quote. For example, the `eval`
command takes one argument, which is a command string; `eval` invokes
the Tcl interpreter to execute the command string. The command
----
eval {
set a 22
set b 33
}
----
will assign the value '22' to 'a' and '33' to 'b'.
If the first character of a command field is not a left
brace, then neither left nor right
braces in the field will be treated specially (except as part of
variable substitution; see below).
COMMAND SUBSTITUTION WITH BRACKETS
----------------------------------
If an open bracket occurs in a field of a command, then command
substitution occurs (except for fields enclosed in braces). All of the
text up to the matching close bracket is treated as a Tcl command and
executed immediately. Then the result of that command is substituted
for the bracketed text. For example, consider the command
----
set a [set b]
----
When the `set` command has only a single argument, it is the name of a
variable and `set` returns the contents of that variable. In this case,
if variable 'b' has the value 'foo', then the command above is equivalent
to the command
----
set a foo
----
Brackets can be used in more complex ways. For example, if the variable
'b' has the value 'foo' and the variable 'c' has the value 'gorp',
then the command
----
set a xyz[set b].[set c]
----
is equivalent to the command
----
set a xyzfoo.gorp
----
A bracketed command may contain multiple commands separated by newlines
or semi-colons in the usual fashion. In this case the value of the last
command is used for substitution. For example, the command
----
set a x[set b 22
expr $b+2]x
----
is equivalent to the command
----
set a x24x
----
If a field is enclosed in braces then the brackets and the characters
between them are not interpreted specially; they are passed through to
the argument verbatim.
VARIABLE SUBSTITUTION WITH $
----------------------------
The dollar sign (+$+) may be used as a special shorthand form for
substituting variable values. If +$+ appears in an argument that isn't
enclosed in braces then variable substitution will occur. The characters
after the +$+, up to the first character that isn't a number, letter,
or underscore, are taken as a variable name and the string value of that
variable is substituted for the name.
For example, if variable 'foo' has the value 'test', then the command
----
set a $foo.c
----
is equivalent to the command
----
set a test.c
----
There are two special forms for variable substitution. If the next
character after the name of the variable is an open parenthesis, then
the variable is assumed to be an array name, and all of the characters
between the open parenthesis and the next close parenthesis are taken as
an index into the array. Command substitutions and variable substitutions
are performed on the information between the parentheses before it is
used as an index.
For example, if the variable 'x' is an array with one element named
'first' and value '87' and another element named '14' and value 'more',
then the command
----
set a xyz$x(first)zyx
----
is equivalent to the command
----
set a xyz87zyx
----
If the variable 'index' has the value '14', then the command
----
set a xyz$x($index)zyx
----
is equivalent to the command
----
set a xyzmorezyx
----
For more information on arrays, see <<_variables_scalars_and_arrays,VARIABLES - SCALARS AND ARRAYS>> below.
The second special form for variables occurs when the dollar sign is
followed by an open curly brace. In this case the variable name consists
of all the characters up to the next curly brace.
Array references are not possible in this form: the name between braces
is assumed to refer to a scalar variable. For example, if variable
'foo' has the value 'test', then the command
----
set a abc${foo}bar
----
is equivalent to the command
----
set a abctestbar
----
Variable substitution does not occur in arguments that are enclosed in
braces: the dollar sign and variable name are passed through to the
argument verbatim.
The dollar sign abbreviation is simply a shorthand form. +$a+ is
completely equivalent to +[set a]+; it is provided as a convenience
to reduce typing.
SEPARATING COMMANDS WITH SEMI-COLONS
------------------------------------
Normally, each command occupies one line (the command is terminated by a
newline character). However, semi-colon (+;+) is treated as a command
separator character; multiple commands may be placed on one line by
separating them with a semi-colon. Semi-colons are not treated as
command separators if they appear within curly braces or double-quotes.
BACKSLASH SUBSTITUTION
----------------------
Backslashes may be used to insert non-printing characters into command
fields and also to insert special characters like braces and brackets
into fields without them being interpreted specially as described above.
The backslash sequences understood by the Tcl interpreter are
listed below. In each case, the backslash
sequence is replaced by the given character:
[[BackslashSequences]]
+{backslash}b+::
Backspace (0x8)
+{backslash}f+::
Form feed (0xc)
+{backslash}n+::
Newline (0xa)
+{backslash}r+::
Carriage-return (0xd).
+{backslash}t+::
Tab (0x9).
+{backslash}v+::
Vertical tab (0xb).
+{backslash}{+::
Left brace ({).
+{backslash}}+::
Right brace (}).
+{backslash}[+::
Open bracket ([).
+{backslash}]+::
Close bracket (]).
+{backslash}$+::
Dollar sign ($).
+{backslash}<space>+::
Space ( ): doesn't terminate argument.
+{backslash};+::
Semi-colon: doesn't terminate command.
+{backslash}"+::
Double-quote.
+{backslash}<newline>+::
Nothing: this joins two lines together
into a single line. This backslash feature is unique in that
it will be applied even when the sequence occurs within braces.
+{backslash}{backslash}+::
Backslash ('{backslash}').
+{backslash}ddd+::
The digits +'ddd'+ (one, two, or three of them) give the octal value of
the character. Note that Jim supports null characters in strings.
+{backslash}unnnn+::
+{backslash}u\{nnn\}+::
+{backslash}Unnnnnnnn+::
The UTF-8 encoding of the unicode codepoint represented by the hex digits, +'nnnn'+, is inserted.
The 'u' form allows for one to four hex digits.
The 'U' form allows for one to eight hex digits.
The 'u\{nnn\}' form allows for one to eight hex digits, but makes it easier to insert
characters UTF-8 characters which are followed by a hex digit.
For example, in the command
----
set a \{x\[\ yz\141
----
the second argument to `set` will be +{x[ yza+.
If a backslash is followed by something other than one of the options
described above, then the backslash is transmitted to the argument
field without any special processing, and the Tcl scanner continues
normal processing with the next character. For example, in the
command
----
set \*a \\{foo
----
The first argument to `set` will be +*a+ and the second
argument will be +{foo+.
If an argument is enclosed in braces, then backslash sequences inside
the argument are parsed but no substitution occurs (except for
backslash-newline): the backslash
sequence is passed through to the argument as is, without making
any special interpretation of the characters in the backslash sequence.
In particular, backslashed braces are not counted in locating the
matching right brace that terminates the argument.
For example, in the
command
----
set a {\\{abc}
----
the second argument to `set` will be `\\{abc`.
This backslash mechanism is not sufficient to generate absolutely
any argument structure; it only covers the
most common cases. To produce particularly complicated arguments
it is probably easiest to use the `format` command along with
command substitution.
STRING AND LIST INDEX SPECIFICATIONS
------------------------------------
Many string and list commands take one or more 'index' parameters which
specify a position in the string relative to the start or end of the string/list.
The index may be one of the following forms:
+integer+::
A simple integer, where +0+ refers to the first element of the string
or list.
+integerexpression+::
Any "safe" expression that evaluates to an integer. A "safe" expression does not perform
variable or command subsitution, but is otherwise like a normal expression
(see <<_expressions,EXPRESSIONS>>).
::
For example +1+2*3+ is valid integer expression, but +{$x*2-1}+ is not.
But note that it is possible to use an unbraced expression to allow the Tcl interpreter
to expand variables and commands before being parsed as an integer expression.
::
e.g. +string repeat a $x*2-1+
+*end*+::
The last element of the string or list.
+*end*-integer+::
+*end*-integerexpression+::
+*end*+integerexpression+::
The 'nth-from-last' element of the string or list. Again, a "safe" integer expression
may be used in place of a simple integer. +end-3+ or +end-3+2*$n+. Normally it only makes
sense to use the +*end*-+ form, but if the integer expression is negative, the +*end*++ form
may be used.
COMMAND SUMMARY
---------------
1. A command is just a string.
2. Within a string commands are separated by newlines or semi-colons
(unless the newline or semi-colon is within braces or brackets
or is backslashed).
3. A command consists of fields. The first field is the name of the command.
The other fields are strings that are passed to that command as arguments.
4. Fields are normally separated by white space.
5. Double-quotes allow white space and semi-colons to appear within
a single argument.
Command substitution, variable substitution, and backslash substitution
still occur inside quotes.
6. Braces defer interpretation of special characters.
If a field begins with a left brace, then it consists of everything
between the left brace and the matching right brace. The
braces themselves are not included in the argument.
No further processing is done on the information between the braces
except that backslash-newline sequences are eliminated.
7. If a field doesn't begin with a brace then backslash,
variable, and command substitution are done on the field. Only a
single level of processing is done: the results of one substitution
are not scanned again for further substitutions or any other
special treatment. Substitution can
occur on any field of a command, including the command name
as well as the arguments.
8. If the first non-blank character of a command is a +\#+, everything
from the +#+ up through the next newline is treated as a comment
and ignored.
EXPRESSIONS
-----------
The second major interpretation applied to strings in Tcl is
as expressions. Several commands, such as `expr`, `for`,
and `if`, treat one or more of their arguments as expressions
and call the Tcl expression processors ('Jim_ExprLong',
'Jim_ExprBoolean', etc.) to evaluate them.
The operators permitted in Tcl expressions are a subset of
the operators permitted in C expressions, and they have the
same meaning and precedence as the corresponding C operators.
Expressions almost always yield numeric results
(integer or floating-point values).
For example, the expression
----
8.2 + 6
----
evaluates to 14.2.
Tcl expressions differ from C expressions in the way that
operands are specified, and in that Tcl expressions support
non-numeric operands and string comparisons.
A Tcl expression consists of a combination of operands, operators,
and parentheses.
White space may be used between the operands and operators and
parentheses; it is ignored by the expression processor.
Where possible, operands are interpreted as integer values.
Comments are allowed in expressions, beginning with the '#' character
and continuing until the end of line or end of expression.
Integer values are interpreted as decimal, binary, octal or
hexadecimal if prepended with '0d', '0b', '0o' or '0x'
respectively. Otherwise they are interpreted as decimal by default.
(Jim Tcl does not interpret numbers with leading zeros as octal.)
If an operand does not have one of the integer formats given
above, then it is treated as a floating-point number if that is
possible. Floating-point numbers may be specified in any of the
ways accepted by an ANSI-compliant C compiler (except that the
'f', 'F', 'l', and 'L' suffixes will not be permitted in
most installations). For example, all of the
following are valid floating-point numbers: 2.1, 3., 6e4, 7.91e+16.
If no numeric interpretation is possible, then an operand is left
as a string (and only a limited set of operators may be applied to
it).
String constants representing boolean constants
(+'0'+, +'1'+, +'false'+, +'off'+, +'no'+, +'true'+, +'on'+, +'yes'+)
are also recognized and can be used in logical operations.
Operands may be specified in any of the following ways:
1. As a numeric value, either integer or floating-point.
2. As one of valid boolean constants
3. As a Tcl variable, using standard '$' notation.
The variable's value will be used as the operand.
4. As a string enclosed in double-quotes.
The expression parser will perform backslash, variable, and
command substitutions on the information between the quotes,
and use the resulting value as the operand
5. As a string enclosed in braces.
The characters between the open brace and matching close brace
will be used as the operand without any substitutions.
6. As a Tcl command enclosed in brackets.
The command will be executed and its result will be used as
the operand.
Where substitutions occur above (e.g. inside quoted strings), they
are performed by the expression processor.
However, an additional layer of substitution may already have
been performed by the command parser before the expression
processor was called.
As discussed below, it is usually best to enclose expressions
in braces to prevent the command parser from performing substitutions
on the contents.
For some examples of simple expressions, suppose the variable 'a' has
the value 3 and the variable 'b' has the value 6. Then the expression
on the left side of each of the lines below will evaluate to the value
on the right side of the line:
----
$a + 3.1 6.1
2 + "$a.$b" 5.6
4*[llength "6 2"] 8
{word one} < "word $a" 0
----
The valid operators are listed below, grouped in decreasing order
of precedence:
[[OperatorPrecedence]]
+int() double() round() abs(), rand(), srand()+::
Unary functions (except rand() which takes no arguments)
* +'int()'+ converts the numeric argument to an integer by truncating down.
* +'double()'+ converts the numeric argument to floating point.
* +'round()'+ converts the numeric argument to the closest integer value.
* +'abs()'+ takes the absolute value of the numeric argument.
* +'rand()'+ returns a pseudo-random floating-point value in the range (0,1).
* +'srand()'+ takes an integer argument to (re)seed the random number generator. Returns the first random number from that seed.
+sin() cos() tan() asin() acos() atan() sinh() cosh() tanh() ceil() floor() exp() log() log10() sqrt()+::
Unary math functions.
If Jim is compiled with math support, these functions are available.
+- + ~ !+::
Unary minus, unary plus, bit-wise NOT, logical NOT. None of these operands
may be applied to string operands, and bit-wise NOT may be
applied only to integers.
+** pow(x,y)+::
Power. e.g. 'x^y^'. If Jim is compiled with math support, supports doubles and
integers. Otherwise supports integers only. (Note that the math-function form
has the same highest precedence)
+* / %+::
Multiply, divide, remainder. None of these operands may be
applied to string operands, and remainder may be applied only
to integers.
++ -+::
Add and subtract. Valid for any numeric operands.
+<< >> <<< >>>+::
Left and right shift, left and right rotate. Valid for integer operands only.
+< > \<= >=+::
Boolean less, greater, less than or equal, and greater than or equal.
Each operator produces 1 if the condition is true, 0 otherwise.
These operators may be applied to strings as well as numeric operands,
in which case string comparison is used.
+lt gt le ge+::
Boolean less, greater, less than or equal, and greater than or equal.
Each operator produces 1 if the condition is true, 0 otherwise.
These operators differ from the above in that they use string comparison
for all operands, including numeric.
+== !=+::
Boolean equal and not equal. Each operator produces a zero/one result.
Valid for all operand types. *Note* that values will be converted to integers
if possible, then floating point types, and finally strings will be compared.
It is recommended that 'eq' and 'ne' should be used for string comparison.
+eq ne+::
String equal and not equal. Uses the string value directly without
attempting to convert to a number first.
+in ni+::
String in list and not in list. For 'in', result is 1 if the left operand (as a string)
is contained in the right operand (as a list), or 0 otherwise. The result for
+{$a ni $list}+ is equivalent to +{!($a in $list)}+.
+&+::
Bit-wise AND. Valid for integer operands only.
+|+::
Bit-wise OR. Valid for integer operands only.
+^+::
Bit-wise exclusive OR. Valid for integer operands only.
+&&+::
Logical AND. Produces a 1 result if both operands are non-zero, 0 otherwise.
Valid for numeric operands only (integers or floating-point).
+||+::
Logical OR. Produces a 0 result if both operands are zero, 1 otherwise.
Valid for numeric operands only (integers or floating-point).
+x ? y : z+::
If-then-else, as in C. If +'x'+
evaluates to non-zero, then the result is the value of +'y'+.
Otherwise the result is the value of +'z'+.
The +'x'+ operand must have a numeric value, while +'y'+ and +'z'+ can
be of any type.
See the C manual for more details on the results
produced by each operator.
All of the binary operators group left-to-right within the same
precedence level. For example, the expression
----
4*2 < 7
----
evaluates to 0.
The +&&+, +||+, and +?:+ operators have 'lazy evaluation', just as
in C, which means that operands are not evaluated if they are not
needed to determine the outcome. For example, in
----
$v ? [a] : [b]
----
only one of +[a]+ or +[b]+ will actually be evaluated,
depending on the value of +$v+.
All internal computations involving integers are done with the C
type 'long long' if available, or 'long' otherwise, and all internal
computations involving floating-point are done with the C type
'double'.
When converting a string to floating-point, exponent overflow is
detected and results in a Tcl error.
For conversion to integer from string, detection of overflow depends
on the behaviour of some routines in the local C library, so it should
be regarded as unreliable.
In any case, overflow and underflow are generally not detected
reliably for intermediate results.
Conversion among internal representations for integer, floating-point,
string operands is done automatically as needed.
For arithmetic computations, integers are used until some
floating-point number is introduced, after which floating-point is used.
For example,
----
5 / 4
----
yields the result 1, while
----
5 / 4.0
5 / ( [string length "abcd"] + 0.0 )
----
both yield the result 1.25.
String values may be used as operands of the comparison operators,
although the expression evaluator tries to do comparisons as integer
or floating-point when it can.
If one of the operands of a comparison is a string and the other
has a numeric value, the numeric operand is converted back to
a string using the C 'sprintf' format specifier
'%d' for integers and '%g' for floating-point values.