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