Debian Codegraph @

doc/open.n

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