open.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:18k
- '"
- '" Copyright (c) 1993 The Regents of the University of California.
- '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: open.n,v 1.16.2.4 2006/03/16 21:11:57 andreas_kupries Exp $
- '"
- .so man.macros
- .TH open n 8.3 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- open - Open a file-based or command pipeline channel
- .SH SYNOPSIS
- .sp
- fBopen fIfileNamefR
- .br
- fBopen fIfileName accessfR
- .br
- fBopen fIfileName access permissionsfR
- .BE
- .SH DESCRIPTION
- .PP
- This command opens a file, serial port, or command pipeline and returns a
- channel identifier that may be used in future invocations of commands like
- fBreadfR, fBputsfR, and fBclosefR.
- If the first character of fIfileNamefR is not fB|fR then
- the command opens a file:
- fIfileNamefR gives the name of the file to open, and it must conform to the
- conventions described in the fBfilenamefR manual entry.
- .PP
- The fIaccessfR argument, if present, indicates the way in which the file
- (or command pipeline) is to be accessed.
- In the first form fIaccessfR may have any of the following values:
- .TP 15
- fBrfR
- Open the file for reading only; the file must already exist. This is the
- default value if fIaccessfR is not specified.
- .TP 15
- fBr+fR
- Open the file for both reading and writing; the file must
- already exist.
- .TP 15
- fBwfR
- Open the file for writing only. Truncate it if it exists. If it doesn't
- exist, create a new file.
- .TP 15
- fBw+fR
- Open the file for reading and writing. Truncate it if it exists.
- If it doesn't exist, create a new file.
- .TP 15
- fBafR
- Open the file for writing only. If the file doesn't exist,
- create a new empty file.
- Set the file pointer to the end of the file prior to each write.
- .TP 15
- fBa+fR
- Open the file for reading and writing. If the file doesn't exist,
- create a new empty file.
- Set the initial access position to the end of the file.
- .PP
- In the second form, fIaccessfR consists of a list of any of the
- following flags, all of which have the standard POSIX meanings.
- One of the flags must be either fBRDONLYfR, fBWRONLYfR or fBRDWRfR.
- .TP 15
- fBRDONLYfR
- Open the file for reading only.
- .TP 15
- fBWRONLYfR
- Open the file for writing only.
- .TP 15
- fBRDWRfR
- Open the file for both reading and writing.
- .TP 15
- fBAPPENDfR
- Set the file pointer to the end of the file prior to each write.
- .TP 15
- fBCREATfR
- Create the file if it doesn't already exist (without this flag it
- is an error for the file not to exist).
- .TP 15
- fBEXCLfR
- If fBCREATfR is also specified, an error is returned if the
- file already exists.
- .TP 15
- fBNOCTTYfR
- If the file is a terminal device, this flag prevents the file from
- becoming the controlling terminal of the process.
- .TP 15
- fBNONBLOCKfR
- Prevents the process from blocking while opening the file, and
- possibly in subsequent I/O operations. The exact behavior of
- this flag is system- and device-dependent; its use is discouraged
- (it is better to use the fBfconfigurefR command to put a file
- in nonblocking mode).
- For details refer to your system documentation on the fBopenfR system
- call's fBO_NONBLOCKfR flag.
- .TP 15
- fBTRUNCfR
- If the file exists it is truncated to zero length.
- .PP
- If a new file is created as part of opening it, fIpermissionsfR
- (an integer) is used to set the permissions for the new file in
- conjunction with the process's file mode creation mask.
- fIPermissionsfR defaults to 0666.
- .PP
- Note that if you are going to be reading or writing binary data from
- the channel created by this command, you should use the
- fBfconfigurefR command to change the fB-translationfR option of
- the channel to fBbinaryfR before transferring any binary data. This
- is in contrast to the ``b'' character passed as part of the equivalent
- of the fIaccessfR parameter to some versions of the C library
- fIfopen()fR function.
- .SH "COMMAND PIPELINES"
- .PP
- If the first character of fIfileNamefR is ``|'' then the
- remaining characters of fIfileNamefR are treated as a list of arguments
- that describe a command pipeline to invoke, in the same style as the
- arguments for fBexecfR.
- In this case, the channel identifier returned by fBopenfR may be used
- to write to the command's input pipe or read from its output pipe,
- depending on the value of fIaccessfR.
- If write-only access is used (e.g. fIaccessfR is fBwfR), then
- standard output for the pipeline is directed to the current standard
- output unless overridden by the command.
- If read-only access is used (e.g. fIaccessfR is fBrfR),
- standard input for the pipeline is taken from the current standard
- input unless overridden by the command.
- The id of the spawned process is accessible through the fBpidfR
- command, using the channel id returned by fBopenfR as argument.
- .PP
- If the command (or one of the commands) executed in the command
- pipeline returns an error (according to the definition in fBexecfR),
- a Tcl error is generated when fBclosefR is called on the channel
- unless the pipeline is in non-blocking mode then no exit status is
- returned (a silent fBclosefR with -blocking 0).
- .PP
- It is often useful to use the fBfileeventfR command with pipelines
- so other processing may happen at the same time as running the command
- in the background.
- .VS 8.4
- .SH "SERIAL COMMUNICATIONS"
- .PP
- If fIfileNamefR refers to a serial port, then the specified serial port
- is opened and initialized in a platform-dependent manner. Acceptable
- values for the fIfileNamefR to use to open a serial port are described in
- the PORTABILITY ISSUES section.
- .PP
- The fBfconfigurefR command can be used to query and set additional
- configuration options specific to serial ports (where supported):
- .TP
- fB-modefR fIbaudfB,fIparityfB,fIdatafB,fIstopfR
- This option is a set of 4 comma-separated values: the baud rate, parity,
- number of data bits, and number of stop bits for this serial port. The
- fIbaudfR rate is a simple integer that specifies the connection speed.
- fIParityfR is one of the following letters: fBnfR, fBofR, fBefR,
- fBmfR, fBsfR; respectively signifying the parity options of ``none'',
- ``odd'', ``even'', ``mark'', or ``space''. fIDatafR is the number of
- data bits and should be an integer from 5 to 8, while fIstopfR is the
- number of stop bits and should be the integer 1 or 2.
- .TP
- fB-handshakefR fItypefR
- (Windows and Unix). This option is used to setup automatic handshake
- control. Note that not all handshake types maybe supported by your operating
- system. The fItypefR parameter is case-independent.
- .sp
- If fItypefR is fBnonefR then any handshake is switched off.
- fBrtsctsfR activates hardware handshake. Hardware handshake signals
- are described below.
- For software handshake fBxonxofffR the handshake characters can be redefined
- with fB-xcharfR.
- An additional hardware handshake fBdtrdsrfR is available only under Windows.
- There is no default handshake configuration, the initial value depends
- on your operating system settings.
- The fB-handshakefR option cannot be queried.
- .TP
- fB-queuefR
- (Windows and Unix). The fB-queuefR option can only be queried.
- It returns a list of two integers representing the current number
- of bytes in the input and output queue respectively.
- .TP
- fB-timeoutfR fImsecfR
- (Windows and Unix). This option is used to set the timeout for blocking
- read operations. It specifies the maximum interval between the
- reception of two bytes in milliseconds.
- For Unix systems the granularity is 100 milliseconds.
- The fB-timeoutfR option does not affect write operations or
- nonblocking reads.
- This option cannot be queried.
- .TP
- fB-ttycontrolfR fI{signal boolean signal boolean ...}fR
- (Windows and Unix). This option is used to setup the handshake
- output lines (see below) permanently or to send a BREAK over the serial line.
- The fIsignalfR names are case-independent.
- fB{RTS 1 DTR 0}fR sets the RTS output to high and the DTR output to low.
- The BREAK condition (see below) is enabled and disabled with fB{BREAK 1}fR and
- fB{BREAK 0}fR respectively.
- It's not a good idea to change the fBRTSfR (or fBDTRfR) signal
- with active hardware handshake fBrtsctsfR (or fBdtrdsrfR).
- The result is unpredictable.
- The fB-ttycontrolfR option cannot be queried.
- .TP
- fB-ttystatusfR
- (Windows and Unix). The fB-ttystatusfR option can only be
- queried. It returns the current modem status and handshake input signals
- (see below).
- The result is a list of signal,value pairs with a fixed order,
- e.g. fB{CTS 1 DSR 0 RING 1 DCD 0}fR.
- The fIsignalfR names are returned upper case.
- .TP
- fB-xcharfR fI{xonChar xoffChar}fR
- (Windows and Unix). This option is used to query or change the software
- handshake characters. Normally the operating system default should be
- DC1 (0x11) and DC3 (0x13) representing the ASCII standard
- XON and XOFF characters.
- .TP
- fB-pollintervalfR fImsecfR
- (Windows only). This option is used to set the maximum time between
- polling for fileevents.
- This affects the time interval between checking for events throughout the Tcl
- interpreter (the smallest value always wins). Use this option only if
- you want to poll the serial port more or less often than 10 msec
- (the default).
- .TP
- fB-sysbufferfR fIinSizefR
- .TP
- fB-sysbufferfR fI{inSize outSize}fR
- (Windows only). This option is used to change the size of Windows
- system buffers for a serial channel. Especially at higher communication
- rates the default input buffer size of 4096 bytes can overrun
- for latent systems. The first form specifies the input buffer size,
- in the second form both input and output buffers are defined.
- .TP
- fB-lasterrorfR
- (Windows only). This option is query only.
- In case of a serial communication error, fBreadfR or fBputsfR
- returns a general Tcl file I/O error.
- fBfconfigure -lasterrorfR can be called to get a list of error details.
- See below for an explanation of the various error codes.
- .SH "SERIAL PORT SIGNALS"
- .PP
- RS-232 is the most commonly used standard electrical interface for serial
- communications. A negative voltage (-3V..-12V) define a mark (on=1) bit and
- a positive voltage (+3..+12V) define a space (off=0) bit (RS-232C). The
- following signals are specified for incoming and outgoing data, status
- lines and handshaking. Here we are using the terms fIworkstationfR for
- your computer and fImodemfR for the external device, because some signal
- names (DCD, RI) come from modems. Of course your external device may use
- these signal lines for other purposes.
- .IP fBTXD(output)fR
- fBTransmitted Data:fR Outgoing serial data.
- .IP fBRXD(input)fR
- fBReceived Data:fRIncoming serial data.
- .IP fBRTS(output)fR
- fBRequest To Send:fR This hardware handshake line informs the modem that
- your workstation is ready to receive data. Your workstation may
- automatically reset this signal to indicate that the input buffer is full.
- .IP fBCTS(input)fR
- fBClear To Send:fR The complement to RTS. Indicates that the modem is
- ready to receive data.
- .IP fBDTR(output)fR
- fBData Terminal Ready:fR This signal tells the modem that the workstation
- is ready to establish a link. DTR is often enabled automatically whenever a
- serial port is opened.
- .IP fBDSR(input)fR
- fBData Set Ready:fR The complement to DTR. Tells the workstation that the
- modem is ready to establish a link.
- .IP fBDCD(input)fR
- fBData Carrier Detect:fR This line becomes active when a modem detects
- a "Carrier" signal.
- .IP fBRI(input)fR
- fBRing Indicator:fR Goes active when the modem detects an incoming call.
- .IP fBBREAKfR
- A BREAK condition is not a hardware signal line, but a logical zero on the
- TXD or RXD lines for a long period of time, usually 250 to 500
- milliseconds. Normally a receive or transmit data signal stays at the mark
- (on=1) voltage until the next character is transferred. A BREAK is sometimes
- used to reset the communications line or change the operating mode of
- communications hardware.
- .SH "ERROR CODES (Windows only)"
- .PP
- A lot of different errors may occur during serial read operations or during
- event polling in background. The external device may have been switched
- off, the data lines may be noisy, system buffers may overrun or your mode
- settings may be wrong. That's why a reliable software should always
- fBcatchfR serial read operations. In cases of an error Tcl returns a
- general file I/O error. Then fBfconfigure -lasterrorfR may help to
- locate the problem. The following error codes may be returned.
- .TP 10
- fBRXOVERfR
- Windows input buffer overrun. The data comes faster than your scripts reads
- it or your system is overloaded. Use fBfconfigure -sysbufferfR to avoid a
- temporary bottleneck and/or make your script faster.
- .TP 10
- fBTXFULLfR
- Windows output buffer overrun. Complement to RXOVER. This error should
- practically not happen, because Tcl cares about the output buffer status.
- .TP 10
- fBOVERRUNfR
- UART buffer overrun (hardware) with data lost.
- The data comes faster than the system driver receives it.
- Check your advanced serial port settings to enable the FIFO (16550) buffer
- and/or setup a lower(1) interrupt threshold value.
- .TP 10
- fBRXPARITYfR
- A parity error has been detected by your UART.
- Wrong parity settings with fBfconfigure -modefR or a noisy data line (RXD)
- may cause this error.
- .TP 10
- fBFRAMEfR
- A stop-bit error has been detected by your UART.
- Wrong mode settings with fBfconfigure -modefR or a noisy data line (RXD)
- may cause this error.
- .TP 10
- fBBREAKfR
- A BREAK condition has been detected by your UART (see above).
- .VE
- .SH "PORTABILITY ISSUES"
- .TP
- fBWindows fR(all versions)
- Valid values for fIfileNamefR to open a serial port are of the form
- fBcomfIXfB:fR, where fIXfR is a number, generally from 1 to 4.
- This notation only works for serial ports from 1 to 9, if the system
- happens to have more than four. An attempt to open a serial port that
- does not exist or has a number greater than 9 will fail. An alternate
- form of opening serial ports is to use the filename fBee.ecomXfR,
- where X is any number that corresponds to a serial port; please note
- that this method is considerably slower on Windows 95 and Windows 98.
- .TP
- fBWindows NTfR
- When running Tcl interactively, there may be some strange interactions
- between the real console, if one is present, and a command pipeline that uses
- standard input or output. If a command pipeline is opened for reading, some
- of the lines entered at the console will be sent to the command pipeline and
- some will be sent to the Tcl evaluator. If a command pipeline is opened for
- writing, keystrokes entered into the console are not visible until the
- pipe is closed. This behavior occurs whether the command pipeline is
- executing 16-bit or 32-bit applications. These problems only occur because
- both Tcl and the child application are competing for the console at
- the same time. If the command pipeline is started from a script, so that Tcl
- is not accessing the console, or if the command pipeline does not use
- standard input or output, but is redirected from or to a file, then the
- above problems do not occur.
- .TP
- fBWindows 95fR
- A command pipeline that executes a 16-bit DOS application cannot be opened
- for both reading and writing, since 16-bit DOS applications that receive
- standard input from a pipe and send standard output to a pipe run
- synchronously. Command pipelines that do not execute 16-bit DOS
- applications run asynchronously and can be opened for both reading and
- writing.
- .sp
- When running Tcl interactively, there may be some strange interactions
- between the real console, if one is present, and a command pipeline that uses
- standard input or output. If a command pipeline is opened for reading from
- a 32-bit application, some of the keystrokes entered at the console will be
- sent to the command pipeline and some will be sent to the Tcl evaluator. If
- a command pipeline is opened for writing to a 32-bit application, no output
- is visible on the console until the pipe is closed. These problems only
- occur because both Tcl and the child application are competing for the
- console at the same time. If the command pipeline is started from a script,
- so that Tcl is not accessing the console, or if the command pipeline does
- not use standard input or output, but is redirected from or to a file, then
- the above problems do not occur.
- .sp
- Whether or not Tcl is running interactively, if a command pipeline is opened
- for reading from a 16-bit DOS application, the call to fBopenfR will not
- return until end-of-file has been received from the command pipeline's
- standard output. If a command pipeline is opened for writing to a 16-bit DOS
- application, no data will be sent to the command pipeline's standard output
- until the pipe is actually closed. This problem occurs because 16-bit DOS
- applications are run synchronously, as described above.
- .TP
- fBMacintoshfR
- Opening a serial port is not currently implemented under Macintosh.
- .sp
- Opening a command pipeline is not supported under Macintosh, since
- applications do not support the concept of standard input or output.
- .TP
- fBUnixfR
- Valid values for fIfileNamefR to open a serial port are generally of the
- form fB/dev/ttyfIXfR, where fIXfR is fBafR or fBbfR, but the name
- of any pseudo-file that maps to a serial port may be used.
- .VS 8.4
- Advanced configuration options are only supported for serial ports
- when Tcl is built to use the POSIX serial interface.
- .VE 8.4
- .sp
- When running Tcl interactively, there may be some strange interactions
- between the console, if one is present, and a command pipeline that uses
- standard input. If a command pipeline is opened for reading, some
- of the lines entered at the console will be sent to the command pipeline and
- some will be sent to the Tcl evaluator. This problem only occurs because
- both Tcl and the child application are competing for the console at the
- same time. If the command pipeline is started from a script, so that Tcl is
- not accessing the console, or if the command pipeline does not use standard
- input, but is redirected from a file, then the above problem does not occur.
- .LP
- See the PORTABILITY ISSUES section of the fBexecfR command for additional
- information not specific to command pipelines about executing
- applications on the various platforms
- .SH "EXAMPLE"
- Open a command pipeline and catch any errors:
- .CS
- set fl [fBopenfR "| ls this_file_does_not_exist"]
- set data [read $fl]
- if {[catch {close $fl} err]} {
- puts "ls command failed: $err"
- }
- .CE
- .SH "SEE ALSO"
- file(n), close(n), filename(n), fconfigure(n), gets(n), read(n),
- puts(n), exec(n), pid(n), fopen(3)
- .SH KEYWORDS
- access mode, append, create, file, non-blocking, open, permissions,
- pipeline, process, serial