1fconfigure(n) Tcl Built-In Commands fconfigure(n)
2
3
4
5______________________________________________________________________________
6
8 fconfigure - Set and get options on a channel
9
11 fconfigure channelId
12 fconfigure channelId name
13 fconfigure channelId name value ?name value ...?
14_________________________________________________________________
15
17 The fconfigure command sets and retrieves options for channels.
18
19 ChannelId identifies the channel for which to set or query an option
20 and must refer to an open channel such as a Tcl standard channel
21 (stdin, stdout, or stderr), the return value from an invocation of open
22 or socket, or the result of a channel creation command provided by a
23 Tcl extension.
24
25 If no name or value arguments are supplied, the command returns a list
26 containing alternating option names and values for the channel. If
27 name is supplied but no value then the command returns the current
28 value of the given option. If one or more pairs of name and value are
29 supplied, the command sets each of the named options to the correspond‐
30 ing value; in this case the return value is an empty string.
31
32 The options described below are supported for all channels. In addi‐
33 tion, each channel type may add options that only it supports. See the
34 manual entry for the command that creates each type of channels for the
35 options that that specific type of channel supports. For example, see
36 the manual entry for the socket command for its additional options.
37
38 -blocking boolean
39 The -blocking option determines whether I/O operations on the
40 channel can cause the process to block indefinitely. The value
41 of the option must be a proper boolean value. Channels are nor‐
42 mally in blocking mode; if a channel is placed into nonblocking
43 mode it will affect the operation of the gets, read, puts,
44 flush, and close commands by allowing them to operate asyn‐
45 chronously; see the documentation for those commands for
46 details. For nonblocking mode to work correctly, the applica‐
47 tion must be using the Tcl event loop (e.g. by calling
48 Tcl_DoOneEvent or invoking the vwait command).
49
50 -buffering newValue
51 If newValue is full then the I/O system will buffer output until
52 its internal buffer is full or until the flush command is
53 invoked. If newValue is line, then the I/O system will automati‐
54 cally flush output for the channel whenever a newline character
55 is output. If newValue is none, the I/O system will flush auto‐
56 matically after every output operation. The default is for
57 -buffering to be set to full except for channels that connect to
58 terminal-like devices; for these channels the initial setting is
59 line. Additionally, stdin and stdout are initially set to line,
60 and stderr is set to none.
61
62 -buffersize newSize
63 Newvalue must be an integer; its value is used to set the size
64 of buffers, in bytes, subsequently allocated for this channel to
65 store input or output. Newvalue must be between ten and one mil‐
66 lion, allowing buffers of ten to one million bytes in size.
67
68 -encoding name
69 This option is used to specify the encoding of the channel, so
70 that the data can be converted to and from Unicode for use in
71 Tcl. For instance, in order for Tcl to read characters from a
72 Japanese file in shiftjis and properly process and display the
73 contents, the encoding would be set to shiftjis. Thereafter,
74 when reading from the channel, the bytes in the Japanese file
75 would be converted to Unicode as they are read. Writing is also
76 supported - as Tcl strings are written to the channel they will
77 automatically be converted to the specified encoding on output.
78
79 If a file contains pure binary data (for instance, a JPEG
80 image), the encoding for the channel should be configured to be
81 binary. Tcl will then assign no interpretation to the data in
82 the file and simply read or write raw bytes. The Tcl binary
83 command can be used to manipulate this byte-oriented data. It
84 is usually better to set the -translation option to binary when
85 you want to transfer binary data, as this turns off the other
86 automatic interpretations of the bytes in the stream as well.
87
88 The default encoding for newly opened channels is the same plat‐
89 form- and locale-dependent system encoding used for interfacing
90 with the operating system, as returned by encoding system.
91
92 -eofchar char
93
94 -eofchar {inChar outChar}
95 This option supports DOS file systems that use Control-z (\x1a)
96 as an end of file marker. If char is not an empty string, then
97 this character signals end-of-file when it is encountered during
98 input. For output, the end-of-file character is output when the
99 channel is closed. If char is the empty string, then there is
100 no special end of file character marker. For read-write chan‐
101 nels, a two-element list specifies the end of file marker for
102 input and output, respectively. As a convenience, when setting
103 the end-of-file character for a read-write channel you can spec‐
104 ify a single value that will apply to both reading and writing.
105 When querying the end-of-file character of a read-write channel,
106 a two-element list will always be returned. The default value
107 for -eofchar is the empty string in all cases except for files
108 under Windows. In that case the -eofchar is Control-z (\x1a)
109 for reading and the empty string for writing. The acceptable
110 range for -eofchar values is \x01 - \x7f; attempting to set
111 -eofchar to a value outside of this range will generate an
112 error.
113
114 -translation mode
115
116 -translation {inMode outMode}
117 In Tcl scripts the end of a line is always represented using a
118 single newline character (\n). However, in actual files and
119 devices the end of a line may be represented differently on dif‐
120 ferent platforms, or even for different devices on the same
121 platform. For example, under UNIX newlines are used in files,
122 whereas carriage-return-linefeed sequences are normally used in
123 network connections. On input (i.e., with gets and read) the
124 Tcl I/O system automatically translates the external end-of-line
125 representation into newline characters. Upon output (i.e., with
126 puts), the I/O system translates newlines to the external end-
127 of-line representation. The default translation mode, auto,
128 handles all the common cases automatically, but the -translation
129 option provides explicit control over the end of line transla‐
130 tions.
131
132 The value associated with -translation is a single item for
133 read-only and write-only channels. The value is a two-element
134 list for read-write channels; the read translation mode is the
135 first element of the list, and the write translation mode is the
136 second element. As a convenience, when setting the translation
137 mode for a read-write channel you can specify a single value
138 that will apply to both reading and writing. When querying the
139 translation mode of a read-write channel, a two-element list
140 will always be returned. The following values are currently
141 supported:
142
143 auto As the input translation mode, auto treats any of newline
144 (lf), carriage return (cr), or carriage return followed
145 by a newline (crlf) as the end of line representation.
146 The end of line representation can even change from line-
147 to-line, and all cases are translated to a newline. As
148 the output translation mode, auto chooses a platform spe‐
149 cific representation; for sockets on all platforms Tcl
150 chooses crlf, for all Unix flavors, it chooses lf, and
151 for the various flavors of Windows it chooses crlf. The
152 default setting for -translation is auto for both input
153 and output.
154
155 binary No end-of-line translations are performed. This is
156 nearly identical to lf mode, except that in addition
157 binary mode also sets the end-of-file character to the
158 empty string (which disables it) and sets the encoding to
159 binary (which disables encoding filtering). See the
160 description of -eofchar and -encoding for more informa‐
161 tion.
162
163 Internally, i.e. when it comes to the actual behaviour of
164 the translator this value is identical to lf and is
165 therefore reported as such when queried. Even if binary
166 was used to set the translation.
167
168 cr The end of a line in the underlying file or device is
169 represented by a single carriage return character. As
170 the input translation mode, cr mode converts carriage
171 returns to newline characters. As the output translation
172 mode, cr mode translates newline characters to carriage
173 returns.
174
175 crlf The end of a line in the underlying file or device is
176 represented by a carriage return character followed by a
177 linefeed character. As the input translation mode, crlf
178 mode converts carriage-return-linefeed sequences to new‐
179 line characters. As the output translation mode, crlf
180 mode translates newline characters to carriage-return-
181 linefeed sequences. This mode is typically used on Win‐
182 dows platforms and for network connections.
183
184 lf The end of a line in the underlying file or device is
185 represented by a single newline (linefeed) character. In
186 this mode no translations occur during either input or
187 output. This mode is typically used on UNIX platforms.
188
190 The Tcl standard channels (stdin, stdout, and stderr) can be configured
191 through this command like every other channel opened by the Tcl
192 library. Beyond the standard options described above they will also
193 support any special option according to their current type. If, for
194 example, a Tcl application is started by the inet super-server common
195 on Unix system its Tcl standard channels will be sockets and thus sup‐
196 port the socket options.
197
199 Instruct Tcl to always send output to stdout immediately, whether or
200 not it is to a terminal:
201 fconfigure stdout -buffering none
202
203 Open a socket and read lines from it without ever blocking the process‐
204 ing of other events:
205 set s [socket some.where.com 12345]
206 fconfigure $s -blocking 0
207 fileevent $s readable "readMe $s"
208 proc readMe chan {
209 if {[gets $chan line] < 0} {
210 if {[eof $chan]} {
211 close $chan
212 return
213 }
214 # Could not read a complete line this time; Tcl's
215 # internal buffering will hold the partial line for us
216 # until some more data is available over the socket.
217 } else {
218 puts stdout $line
219 }
220 }
221
222 Read a PPM-format image from a file:
223 # Open the file and put it into Unix ASCII mode
224 set f [open teapot.ppm]
225 fconfigure $f -encoding ascii -translation lf
226
227 # Get the header
228 if {[gets $f] ne "P6"} {
229 error "not a raw-bits PPM"
230 }
231
232 # Read lines until we have got non-comment lines
233 # that supply us with three decimal values.
234 set words {}
235 while {[llength $words] < 3} {
236 gets $f line
237 if {[string match "#*" $line]} continue
238 lappend words {*}[join [scan $line %d%d%d]]
239 }
240
241 # Those words supply the size of the image and its
242 # overall depth per channel. Assign to variables.
243 lassign $words xSize ySize depth
244
245 # Now switch to binary mode to pull in the data,
246 # one byte per channel (red,green,blue) per pixel.
247 fconfigure $f -translation binary
248 set numDataBytes [expr {3 * $xSize * $ySize}]
249 set data [read $f $numDataBytes]
250
251 close $f
252
253
255 close(n), flush(n), gets(n), open(n), puts(n), read(n), socket(n),
256 Tcl_StandardChannels(3)
257
258
260 blocking, buffering, carriage return, end of line, flushing, linemode,
261 newline, nonblocking, platform, translation, encoding, filter, byte
262 array, binary
263
264
265
266Tcl 8.3 fconfigure(n)