Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added tcl to libs

File size: 18.2 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: open.n,v 1.34 2007/12/13 15:22:33 dgp Exp $
9'\"
10.so man.macros
11.TH open n 8.3 Tcl "Tcl Built-In Commands"
12.BS
13'\" Note:  do not modify the .SH NAME line immediately below!
14.SH NAME
15open \- Open a file-based or command pipeline channel
16.SH SYNOPSIS
17.sp
18\fBopen \fIfileName\fR
19.br
20\fBopen \fIfileName access\fR
21.br
22\fBopen \fIfileName access permissions\fR
23.BE
24.SH DESCRIPTION
25.PP
26This command opens a file, serial port, or command pipeline and returns a
27channel identifier that may be used in future invocations of commands like
28\fBread\fR, \fBputs\fR, and \fBclose\fR.
29If the first character of \fIfileName\fR is not \fB|\fR then
30the command opens a file:
31\fIfileName\fR gives the name of the file to open, and it must conform to the
32conventions described in the \fBfilename\fR manual entry.
33.PP
34The \fIaccess\fR argument, if present, indicates the way in which the file
35(or command pipeline) is to be accessed.
36In the first form \fIaccess\fR may have any of the following values:
37.TP 15
38\fBr\fR
39Open the file for reading only; the file must already exist. This is the
40default value if \fIaccess\fR is not specified.
41.TP 15
42\fBr+\fR
43Open the file for both reading and writing; the file must
44already exist.
45.TP 15
46\fBw\fR
47Open the file for writing only.  Truncate it if it exists.  If it does not
48exist, create a new file.
49.TP 15
50\fBw+\fR
51Open the file for reading and writing.  Truncate it if it exists.
52If it does not exist, create a new file.
53.TP 15
54\fBa\fR
55Open the file for writing only.  If the file does not exist,
56create a new empty file.
57Set the file pointer to the end of the file prior to each write.
58.TP 15
59\fBa+\fR
60Open the file for reading and writing.  If the file does not exist,
61create a new empty file.
62Set the initial access position  to the end of the file.
63.VS 8.5
64.PP
65All of the legal \fIaccess\fR values above may have the character
66\fBb\fR added as the second or third character in the value to
67indicate that the opened channel should be configured with the
68\fB\-translation binary\fR option, making the channel suitable for
69reading or writing of binary data.
70.VE 8.5
71.PP
72In the second form, \fIaccess\fR consists of a list of any of the
73following flags, all of which have the standard POSIX meanings.
74One of the flags must be either \fBRDONLY\fR, \fBWRONLY\fR or \fBRDWR\fR.
75.TP 15
76\fBRDONLY\fR
77Open the file for reading only.
78.TP 15
79\fBWRONLY\fR
80Open the file for writing only.
81.TP 15
82\fBRDWR\fR
83Open the file for both reading and writing.
84.TP 15
85\fBAPPEND\fR
86Set the file pointer to the end of the file prior to each write.
87.VS 8.5
88.TP 15
89\fBBINARY\fR
90Configure the opened channel with the \fB\-translation binary\fR option.
91.VE 8.5
92.TP 15
93\fBCREAT\fR
94Create the file if it does not already exist (without this flag it
95is an error for the file not to exist).
96.TP 15
97\fBEXCL\fR
98If \fBCREAT\fR is also specified, an error is returned if the
99file already exists.
100.TP 15
101\fBNOCTTY\fR
102If the file is a terminal device, this flag prevents the file from
103becoming the controlling terminal of the process.
104.TP 15
105\fBNONBLOCK\fR
106Prevents the process from blocking while opening the file, and
107possibly in subsequent I/O operations.  The exact behavior of
108this flag is system- and device-dependent;  its use is discouraged
109(it is better to use the \fBfconfigure\fR command to put a file
110in nonblocking mode).
111For details refer to your system documentation on the \fBopen\fR system
112call's \fBO_NONBLOCK\fR flag.
113.TP 15
114\fBTRUNC\fR
115If the file exists it is truncated to zero length.
116.PP
117If a new file is created as part of opening it, \fIpermissions\fR
118(an integer) is used to set the permissions for the new file in
119conjunction with the process's file mode creation mask.
120\fIPermissions\fR defaults to 0666.
121.SH "COMMAND PIPELINES"
122.PP
123If the first character of \fIfileName\fR is
124.QW |
125then the
126remaining characters of \fIfileName\fR are treated as a list of arguments
127that describe a command pipeline to invoke, in the same style as the
128arguments for \fBexec\fR.
129In this case, the channel identifier returned by \fBopen\fR may be used
130to write to the command's input pipe or read from its output pipe,
131depending on the value of \fIaccess\fR.
132If write-only access is used (e.g. \fIaccess\fR is \fBw\fR), then
133standard output for the pipeline is directed to the current standard
134output unless overridden by the command.
135If read-only access is used (e.g. \fIaccess\fR is \fBr\fR),
136standard input for the pipeline is taken from the current standard
137input unless overridden by the command.
138The id of the spawned process is accessible through the \fBpid\fR
139command, using the channel id returned by \fBopen\fR as argument.
140.PP
141If the command (or one of the commands) executed in the command
142pipeline returns an error (according to the definition in \fBexec\fR),
143a Tcl error is generated when \fBclose\fR is called on the channel
144unless the pipeline is in non-blocking mode then no exit status is
145returned (a silent \fBclose\fR with -blocking 0).
146.PP
147It is often useful to use the \fBfileevent\fR command with pipelines
148so other processing may happen at the same time as running the command
149in the background.
150.SH "SERIAL COMMUNICATIONS"
151.PP
152If \fIfileName\fR refers to a serial port, then the specified serial port
153is opened and initialized in a platform-dependent manner.  Acceptable
154values for the \fIfileName\fR to use to open a serial port are described in
155the PORTABILITY ISSUES section.
156.PP
157The \fBfconfigure\fR command can be used to query and set additional
158configuration options specific to serial ports (where supported):
159.TP
160\fB\-mode\fR \fIbaud\fB,\fIparity\fB,\fIdata\fB,\fIstop\fR
161This option is a set of 4 comma-separated values: the baud rate, parity,
162number of data bits, and number of stop bits for this serial port.  The
163\fIbaud\fR rate is a simple integer that specifies the connection speed.
164\fIParity\fR is one of the following letters: \fBn\fR, \fBo\fR, \fBe\fR,
165\fBm\fR, \fBs\fR; respectively signifying the parity options of
166.QW none ,
167.QW odd ,
168.QW even ,
169.QW mark ,
170or
171.QW space .
172\fIData\fR is the number of
173data bits and should be an integer from 5 to 8, while \fIstop\fR is the
174number of stop bits and should be the integer 1 or 2.
175.TP
176\fB\-handshake\fR \fItype\fR
177(Windows and Unix). This option is used to setup automatic handshake
178control. Note that not all handshake types maybe supported by your operating
179system. The \fItype\fR parameter is case-independent.
180.RS
181.PP
182If \fItype\fR is \fBnone\fR then any handshake is switched off.
183\fBrtscts\fR activates hardware handshake. Hardware handshake signals
184are described below.
185For software handshake \fBxonxoff\fR the handshake characters can be redefined
186with \fB\-xchar\fR.
187An additional hardware handshake \fBdtrdsr\fR is available only under Windows.
188There is no default handshake configuration, the initial value depends
189on your operating system settings.
190The \fB\-handshake\fR option cannot be queried.
191.RE
192.TP
193\fB\-queue\fR
194(Windows and Unix). The \fB\-queue\fR option can only be queried.
195It returns a list of two integers representing the current number
196of bytes in the input and output queue respectively.
197.TP
198\fB\-timeout\fR \fImsec\fR
199(Windows and Unix). This option is used to set the timeout for blocking
200read operations. It specifies the maximum interval between the
201reception of two bytes in milliseconds.
202For Unix systems the granularity is 100 milliseconds.
203The \fB\-timeout\fR option does not affect write operations or
204nonblocking reads.
205This option cannot be queried.
206.TP
207\fB\-ttycontrol\fR \fI{signal boolean signal boolean ...}\fR
208(Windows and Unix). This option is used to setup the handshake
209output lines (see below) permanently or to send a BREAK over the serial line.
210The \fIsignal\fR names are case-independent.
211\fB{RTS 1 DTR 0}\fR sets the RTS output to high and the DTR output to low.
212The BREAK condition (see below) is enabled and disabled with \fB{BREAK 1}\fR and
213\fB{BREAK 0}\fR respectively.
214It is not a good idea to change the \fBRTS\fR (or \fBDTR\fR) signal
215with active hardware handshake \fBrtscts\fR (or \fBdtrdsr\fR).
216The result is unpredictable.
217The \fB\-ttycontrol\fR option cannot be queried.
218.TP
219\fB\-ttystatus\fR
220(Windows and Unix). The \fB\-ttystatus\fR option can only be
221queried.  It returns the current modem status and handshake input signals
222(see below).
223The result is a list of signal,value pairs with a fixed order,
224e.g. \fB{CTS 1 DSR 0 RING 1 DCD 0}\fR.
225The \fIsignal\fR names are returned upper case.
226.TP
227\fB\-xchar\fR \fI{xonChar xoffChar}\fR
228(Windows and Unix). This option is used to query or change the software
229handshake characters. Normally the operating system default should be
230DC1 (0x11) and DC3 (0x13) representing the ASCII standard
231XON and XOFF characters.
232.TP
233\fB\-pollinterval\fR \fImsec\fR
234(Windows only). This option is used to set the maximum time between
235polling for fileevents.
236This affects the time interval between checking for events throughout the Tcl
237interpreter (the smallest value always wins).  Use this option only if
238you want to poll the serial port more or less often than 10 msec
239(the default).
240.TP
241\fB\-sysbuffer\fR \fIinSize\fR
242.TP
243\fB\-sysbuffer\fR \fI{inSize outSize}\fR
244(Windows only). This option is used to change the size of Windows
245system buffers for a serial channel. Especially at higher communication
246rates the default input buffer size of 4096 bytes can overrun
247for latent systems. The first form specifies the input buffer size,
248in the second form both input and output buffers are defined.
249.TP
250\fB\-lasterror\fR
251(Windows only). This option is query only.
252In case of a serial communication error, \fBread\fR or \fBputs\fR
253returns a general Tcl file I/O error.
254\fBfconfigure -lasterror\fR can be called to get a list of error details.
255See below for an explanation of the various error codes.
256.SH "SERIAL PORT SIGNALS"
257.PP
258RS-232 is the most commonly used standard electrical interface for serial
259communications. A negative voltage (-3V..-12V) define a mark (on=1) bit and
260a positive voltage (+3..+12V) define a space (off=0) bit (RS-232C).  The
261following signals are specified for incoming and outgoing data, status
262lines and handshaking. Here we are using the terms \fIworkstation\fR for
263your computer and \fImodem\fR for the external device, because some signal
264names (DCD, RI) come from modems. Of course your external device may use
265these signal lines for other purposes.
266.IP \fBTXD(output)\fR
267\fBTransmitted Data:\fR Outgoing serial data.
268.IP \fBRXD(input)\fR
269\fBReceived Data:\fRIncoming serial data.
270.IP \fBRTS(output)\fR
271\fBRequest To Send:\fR This hardware handshake line informs the modem that
272your workstation is ready to receive data. Your workstation may
273automatically reset this signal to indicate that the input buffer is full.
274.IP \fBCTS(input)\fR
275\fBClear To Send:\fR The complement to RTS. Indicates that the modem is
276ready to receive data.
277.IP \fBDTR(output)\fR
278\fBData Terminal Ready:\fR This signal tells the modem that the workstation
279is ready to establish a link. DTR is often enabled automatically whenever a
280serial port is opened.
281.IP \fBDSR(input)\fR
282\fBData Set Ready:\fR The complement to DTR. Tells the workstation that the
283modem is ready to establish a link.
284.IP \fBDCD(input)\fR
285\fBData Carrier Detect:\fR This line becomes active when a modem detects a
286.QW Carrier
287signal.
288.IP \fBRI(input)\fR
289\fBRing Indicator:\fR Goes active when the modem detects an incoming call.
290.IP \fBBREAK\fR
291A BREAK condition is not a hardware signal line, but a logical zero on the
292TXD or RXD lines for a long period of time, usually 250 to 500
293milliseconds.  Normally a receive or transmit data signal stays at the mark
294(on=1) voltage until the next character is transferred. A BREAK is sometimes
295used to reset the communications line or change the operating mode of
296communications hardware.
297.SH "ERROR CODES (Windows only)"
298.PP
299A lot of different errors may occur during serial read operations or during
300event polling in background. The external device may have been switched
301off, the data lines may be noisy, system buffers may overrun or your mode
302settings may be wrong.  That is why a reliable software should always
303\fBcatch\fR serial read operations.  In cases of an error Tcl returns a
304general file I/O error.  Then \fBfconfigure -lasterror\fR may help to
305locate the problem.  The following error codes may be returned.
306.TP 10
307\fBRXOVER\fR
308Windows input buffer overrun. The data comes faster than your scripts reads
309it or your system is overloaded. Use \fBfconfigure -sysbuffer\fR to avoid a
310temporary bottleneck and/or make your script faster.
311.TP 10
312\fBTXFULL\fR
313Windows output buffer overrun. Complement to RXOVER. This error should
314practically not happen, because Tcl cares about the output buffer status.
315.TP 10
316\fBOVERRUN\fR
317UART buffer overrun (hardware) with data lost.
318The data comes faster than the system driver receives it.
319Check your advanced serial port settings to enable the FIFO (16550) buffer
320and/or setup a lower(1) interrupt threshold value.
321.TP 10
322\fBRXPARITY\fR
323A parity error has been detected by your UART.
324Wrong parity settings with \fBfconfigure -mode\fR or a noisy data line (RXD)
325may cause this error.
326.TP 10
327\fBFRAME\fR
328A stop-bit error has been detected by your UART.
329Wrong mode settings with \fBfconfigure -mode\fR or a noisy data line (RXD)
330may cause this error.
331.TP 10
332\fBBREAK\fR
333A BREAK condition has been detected by your UART (see above).
334.SH "PORTABILITY ISSUES"
335.TP
336\fBWindows \fR(all versions)
337Valid values for \fIfileName\fR to open a serial port are of the form
338\fBcom\fIX\fB:\fR, where \fIX\fR is a number, generally from 1 to 4.
339This notation only works for serial ports from 1 to 9, if the system
340happens to have more than four.  An attempt to open a serial port that
341does not exist or has a number greater than 9 will fail.  An alternate
342form of opening serial ports is to use the filename \fB\e\e.\ecomX\fR,
343where X is any number that corresponds to a serial port; please note
344that this method is considerably slower on Windows 95 and Windows 98.
345.TP
346\fBWindows NT\fR
347When running Tcl interactively, there may be some strange interactions
348between the real console, if one is present, and a command pipeline that uses
349standard input or output.  If a command pipeline is opened for reading, some
350of the lines entered at the console will be sent to the command pipeline and
351some will be sent to the Tcl evaluator.  If a command pipeline is opened for
352writing, keystrokes entered into the console are not visible until the
353pipe is closed.  This behavior occurs whether the command pipeline is
354executing 16-bit or 32-bit applications.  These problems only occur because
355both Tcl and the child application are competing for the console at
356the same time.  If the command pipeline is started from a script, so that Tcl
357is not accessing the console, or if the command pipeline does not use
358standard input or output, but is redirected from or to a file, then the
359above problems do not occur. 
360.TP
361\fBWindows 95\fR
362A command pipeline that executes a 16-bit DOS application cannot be opened
363for both reading and writing, since 16-bit DOS applications that receive
364standard input from a pipe and send standard output to a pipe run
365synchronously.  Command pipelines that do not execute 16-bit DOS
366applications run asynchronously and can be opened for both reading and
367writing. 
368.RS
369.PP
370When running Tcl interactively, there may be some strange interactions
371between the real console, if one is present, and a command pipeline that uses
372standard input or output.  If a command pipeline is opened for reading from
373a 32-bit application, some of the keystrokes entered at the console will be
374sent to the command pipeline and some will be sent to the Tcl evaluator.  If
375a command pipeline is opened for writing to a 32-bit application, no output
376is visible on the console until the pipe is closed.  These problems only
377occur because both Tcl and the child application are competing for the
378console at the same time.  If the command pipeline is started from a script,
379so that Tcl is not accessing the console, or if the command pipeline does
380not use standard input or output, but is redirected from or to a file, then
381the above problems do not occur. 
382.PP
383Whether or not Tcl is running interactively, if a command pipeline is opened
384for reading from a 16-bit DOS application, the call to \fBopen\fR will not
385return until end-of-file has been received from the command pipeline's
386standard output.  If a command pipeline is opened for writing to a 16-bit DOS
387application, no data will be sent to the command pipeline's standard output
388until the pipe is actually closed.  This problem occurs because 16-bit DOS
389applications are run synchronously, as described above. 
390.RE
391.TP
392\fBUnix\fR\0\0\0\0\0\0\0
393Valid values for \fIfileName\fR to open a serial port are generally of the
394form \fB/dev/tty\fIX\fR, where \fIX\fR is \fBa\fR or \fBb\fR, but the name
395of any pseudo-file that maps to a serial port may be used.
396Advanced configuration options are only supported for serial ports
397when Tcl is built to use the POSIX serial interface.
398.RS
399.PP
400When running Tcl interactively, there may be some strange interactions
401between the console, if one is present, and a command pipeline that uses
402standard input.  If a command pipeline is opened for reading, some
403of the lines entered at the console will be sent to the command pipeline and
404some will be sent to the Tcl evaluator.  This problem only occurs because
405both Tcl and the child application are competing for the console at the
406same time.  If the command pipeline is started from a script, so that Tcl is
407not accessing the console, or if the command pipeline does not use standard
408input, but is redirected from a file, then the above problem does not occur. 
409.RE
410.PP
411See the \fBPORTABILITY ISSUES\fR section of the \fBexec\fR command for
412additional information not specific to command pipelines about executing
413applications on the various platforms
414.SH "EXAMPLE"
415Open a command pipeline and catch any errors:
416.CS
417set fl [\fBopen\fR "| ls this_file_does_not_exist"]
418set data [read $fl]
419if {[catch {close $fl} err]} {
420    puts "ls command failed: $err"
421}
422.CE
423.SH "SEE ALSO"
424file(n), close(n), filename(n), fconfigure(n), gets(n), read(n),
425puts(n), exec(n), pid(n), fopen(3)
426.SH KEYWORDS
427access mode, append, create, file, non-blocking, open, permissions,
428pipeline, process, serial
Note: See TracBrowser for help on using the repository browser.