Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/fconfigure.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 18 years ago

added tcl to libs

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