chan configure
Gives or sets the options of a channel.
chan configure CHAN
chan configure CHAN OPTION
chan configure CHAN OPTION VALUE ?OPTION VALUE ...?
Description
With no OPTION it gives a list of each option of CHAN and its value. With one OPTION it gives its value. With pairs of options and values it sets each one, in order. The options are the ones below, and a socket or a serial port has more.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| CHAN | channel | required | The channel. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-blocking | boolean | 1 | Whether a read or a write waits for its data. |
-buffering | full, line, or none | full | When the output buffer goes out. |
-buffersize | integer | none | The size of the buffers in bytes. |
-encoding | encoding name | utf-8 | The encoding of the text of the channel. |
-eofchar | character | empty | A character that ends the input. |
-translation | auto, binary, cr, crlf, or lf | auto for input, lf for output | How the ends of lines change between the channel and the script. |
-profile | strict, replace, or tcl8 | none | What a read or a write does with a byte that the encoding cannot read. Tcl 9.1 only |
-blocking BOOLEAN
Whether a read or a write waits for its data.
With 0, a read gives what is there and a write does not wait; use it with chan event.
% set f [open a.txt]
> chan configure $f -blocking 0
> set r [chan configure $f -blocking]
> chan close $f
> set r
0Each boolean is valid.
-buffering MODE
When the output buffer goes out.
full writes when the buffer is full, line after each newline, and none at once.
Values: full, line, none.
% set f [open a.txt]
> chan configure $f -buffering none
> set r [chan configure $f -buffering]
> chan close $f
> set r
none% set f [open a.txt]
> catch {chan configure $f -buffering bogus} m
> chan close $f
> error $m
bad value for -buffering: must be one of full, line, or none
-errorcode NONE-buffersize SIZE
The size of the buffers in bytes.
The size of the input and output buffers of the channel.
% set f [open a.txt]
> chan configure $f -buffersize 100
> set r [chan configure $f -buffersize]
> chan close $f
> set r
100ztcl keeps each size it gets.
-encoding NAME
The encoding of the text of the channel.
The encoding between the characters of the script and the bytes of the channel. A file has utf-8 by default on each platform. binary is no encoding: one byte for each character.
% set f [open a.txt]
> set c [chan configure $f -encoding]
> chan close $f
> set c
utf-8% set f [open a.txt]
> catch {chan configure $f -encoding bogus} m
> chan close $f
> error $m
unknown encoding "bogus"
-errorcode NONE-eofchar CHAR
A character that ends the input.
A read stops at this character as at the end of the channel. The default is none.
% set f [open a.txt]
> set r [chan configure $f -eofchar]
> chan close $f
> set rztcl keeps each value it gets.
-translation MODE
How the ends of lines change between the channel and the script.
On input, auto reads \n, \r\n, and \r as a newline. On output, lf writes \n, crlf writes \r\n, and cr writes \r. binary changes no byte and sets the encoding to binary.
Values: auto, binary, cr, crlf, lf.
% set f [open a.txt w]
> chan configure $f -translation crlf
> chan puts $f a
> chan close $f
> file size a.txt
3
% set f [open a.txt]
> chan configure $f -translation binary
> set c [chan configure $f -translation]
> chan close $f
> set c
binary# Tcl 8.6 only
% set f [open a.txt]
> catch {chan configure $f -bogus 1} m
> chan close $f
> error $m
bad option "-bogus": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation
-errorcode NONE-profile PROFILE Tcl 9.1 only
What a read or a write does with a byte that the encoding cannot read.
strict makes such a byte an error, replace puts a replacement character in its place, and tcl8 reads it as Tcl 8.6 does.
% set f [open a.txt]
> chan configure $f -profile strict
> set r [chan configure $f -profile]
> chan close $f
> set r
strict% set f [open a.txt]
> catch {chan configure $f -bogus 1} m
> chan close $f
> error $m
bad option "-bogus": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, -profile, or -translation
-errorcode NONEReturns
The list of options and values, one value, or an empty string.
Examples
# Tcl 8.6 only
% set f [open a.txt w]; chan close $f
> set f [open a.txt]
> set r [lsort [dict keys [chan configure $f]]]
> chan close $f
> set r
-blocking -buffering -buffersize -encoding -eofchar -translation
# Tcl 9.1 only
% set f [open a.txt w]; chan close $f
> set f [open a.txt]
> set r [lsort [dict keys [chan configure $f]]]
> chan close $f
> set r
-blocking -buffering -buffersize -encoding -eofchar -profile -translation
Errors
% chan configure nochan
can not find channel named "nochan"
-errorcode TCL LOOKUP CHANNEL nochan
Facts
- Capability
channels. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1