1open(n) Tcl Built-In Commands open(n)
2
3
4
5______________________________________________________________________________
6
8 open - Open a file-based or command pipeline channel
9
11 open fileName
12 open fileName access
13 open fileName access permissions
14______________________________________________________________________________
15
17 This command opens a file, serial port, or command pipeline and returns
18 a channel identifier that may be used in future invocations of commands
19 like read, puts, and close. If the first character of fileName is not
20 | then the command opens a file: fileName gives the name of the file to
21 open, and it must conform to the conventions described in the filename
22 manual entry.
23
24 The access argument, if present, indicates the way in which the file
25 (or command pipeline) is to be accessed. In the first form access may
26 have any of the following values:
27
28 r Open the file for reading only; the file must already
29 exist. This is the default value if access is not speci‐
30 fied.
31
32 r+ Open the file for both reading and writing; the file
33 must already exist.
34
35 w Open the file for writing only. Truncate it if it
36 exists. If it does not exist, create a new file.
37
38 w+ Open the file for reading and writing. Truncate it if
39 it exists. If it does not exist, create a new file.
40
41 a Open the file for writing only. If the file does not
42 exist, create a new empty file. Set the file pointer to
43 the end of the file prior to each write.
44
45 a+ Open the file for reading and writing. If the file does
46 not exist, create a new empty file. Set the initial
47 access position to the end of the file.
48
49 All of the legal access values above may have the character b added as
50 the second or third character in the value to indicate that the opened
51 channel should be configured as if with the fconfigure -translation
52 binary option, making the channel suitable for reading or writing of
53 binary data.
54
55 In the second form, access consists of a list of any of the following
56 flags, all of which have the standard POSIX meanings. One of the flags
57 must be either RDONLY, WRONLY or RDWR.
58
59 RDONLY Open the file for reading only.
60
61 WRONLY Open the file for writing only.
62
63 RDWR Open the file for both reading and writing.
64
65 APPEND Set the file pointer to the end of the file prior to
66 each write.
67
68 BINARY Configure the opened channel with the -translation
69 binary option.
70
71 CREAT Create the file if it does not already exist (without
72 this flag it is an error for the file not to exist).
73
74 EXCL If CREAT is also specified, an error is returned if the
75 file already exists.
76
77 NOCTTY If the file is a terminal device, this flag prevents the
78 file from becoming the controlling terminal of the
79 process.
80
81 NONBLOCK Prevents the process from blocking while opening the
82 file, and possibly in subsequent I/O operations. The
83 exact behavior of this flag is system- and device-depen‐
84 dent; its use is discouraged (it is better to use the
85 fconfigure command to put a file in nonblocking mode).
86 For details refer to your system documentation on the
87 open system call's O_NONBLOCK flag.
88
89 TRUNC If the file exists it is truncated to zero length.
90
91 If a new file is created as part of opening it, permissions (an inte‐
92 ger) is used to set the permissions for the new file in conjunction
93 with the process's file mode creation mask. Permissions defaults to
94 0666.
95
97 If the first character of fileName is “|” then the remaining characters
98 of fileName are treated as a list of arguments that describe a command
99 pipeline to invoke, in the same style as the arguments for exec. In
100 this case, the channel identifier returned by open may be used to write
101 to the command's input pipe or read from its output pipe, depending on
102 the value of access. If write-only access is used (e.g. access is
103 “w”), then standard output for the pipeline is directed to the current
104 standard output unless overridden by the command. If read-only access
105 is used (e.g. access is “r”), standard input for the pipeline is taken
106 from the current standard input unless overridden by the command. The
107 id of the spawned process is accessible through the pid command, using
108 the channel id returned by open as argument.
109
110 If the command (or one of the commands) executed in the command pipe‐
111 line returns an error (according to the definition in exec), a Tcl
112 error is generated when close is called on the channel unless the pipe‐
113 line is in non-blocking mode then no exit status is returned (a silent
114 close with -blocking 0).
115
116 It is often useful to use the fileevent command with pipelines so other
117 processing may happen at the same time as running the command in the
118 background.
119
121 If fileName refers to a serial port, then the specified serial port is
122 opened and initialized in a platform-dependent manner. Acceptable val‐
123 ues for the fileName to use to open a serial port are described in the
124 PORTABILITY ISSUES section.
125
126 The fconfigure command can be used to query and set additional configu‐
127 ration options specific to serial ports (where supported):
128
129 -mode baud,parity,data,stop
130 This option is a set of 4 comma-separated values: the baud rate,
131 parity, number of data bits, and number of stop bits for this
132 serial port. The baud rate is a simple integer that specifies
133 the connection speed. Parity is one of the following letters:
134 n, o, e, m, s; respectively signifying the parity options of
135 “none”, “odd”, “even”, “mark”, or “space”. Data is the number
136 of data bits and should be an integer from 5 to 8, while stop is
137 the number of stop bits and should be the integer 1 or 2.
138
139 -handshake type
140 (Windows and Unix). This option is used to setup automatic hand‐
141 shake control. Note that not all handshake types maybe supported
142 by your operating system. The type parameter is case-indepen‐
143 dent.
144
145 If type is none then any handshake is switched off. rtscts
146 activates hardware handshake. Hardware handshake signals are
147 described below. For software handshake xonxoff the handshake
148 characters can be redefined with -xchar. An additional hardware
149 handshake dtrdsr is available only under Windows. There is no
150 default handshake configuration, the initial value depends on
151 your operating system settings. The -handshake option cannot be
152 queried.
153
154 -queue (Windows and Unix). The -queue option can only be queried. It
155 returns a list of two integers representing the current number
156 of bytes in the input and output queue respectively.
157
158 -timeout msec
159 (Windows and Unix). This option is used to set the timeout for
160 blocking read operations. It specifies the maximum interval
161 between the reception of two bytes in milliseconds. For Unix
162 systems the granularity is 100 milliseconds. The -timeout
163 option does not affect write operations or nonblocking reads.
164 This option cannot be queried.
165
166 -ttycontrol {signal boolean signal boolean ...}
167 (Windows and Unix). This option is used to setup the handshake
168 output lines (see below) permanently or to send a BREAK over the
169 serial line. The signal names are case-independent. {RTS 1 DTR
170 0} sets the RTS output to high and the DTR output to low. The
171 BREAK condition (see below) is enabled and disabled with {BREAK
172 1} and {BREAK 0} respectively. It is not a good idea to change
173 the RTS (or DTR) signal with active hardware handshake rtscts
174 (or dtrdsr). The result is unpredictable. The -ttycontrol
175 option cannot be queried.
176
177 -ttystatus
178 (Windows and Unix). The -ttystatus option can only be queried.
179 It returns the current modem status and handshake input signals
180 (see below). The result is a list of signal,value pairs with a
181 fixed order, e.g. {CTS 1 DSR 0 RING 1 DCD 0}. The signal names
182 are returned upper case.
183
184 -xchar {xonChar xoffChar}
185 (Windows and Unix). This option is used to query or change the
186 software handshake characters. Normally the operating system
187 default should be DC1 (0x11) and DC3 (0x13) representing the
188 ASCII standard XON and XOFF characters.
189
190 -pollinterval msec
191 (Windows only). This option is used to set the maximum time
192 between polling for fileevents. This affects the time interval
193 between checking for events throughout the Tcl interpreter (the
194 smallest value always wins). Use this option only if you want
195 to poll the serial port more or less often than 10 msec (the
196 default).
197
198 -sysbuffer inSize
199
200 -sysbuffer {inSize outSize}
201 (Windows only). This option is used to change the size of Win‐
202 dows system buffers for a serial channel. Especially at higher
203 communication rates the default input buffer size of 4096 bytes
204 can overrun for latent systems. The first form specifies the
205 input buffer size, in the second form both input and output buf‐
206 fers are defined.
207
208 -lasterror
209 (Windows only). This option is query only. In case of a serial
210 communication error, read or puts returns a general Tcl file I/O
211 error. fconfigure -lasterror can be called to get a list of
212 error details. See below for an explanation of the various
213 error codes.
214
216 RS-232 is the most commonly used standard electrical interface for
217 serial communications. A negative voltage (-3V..-12V) define a mark
218 (on=1) bit and a positive voltage (+3..+12V) define a space (off=0) bit
219 (RS-232C). The following signals are specified for incoming and outgo‐
220 ing data, status lines and handshaking. Here we are using the terms
221 workstation for your computer and modem for the external device,
222 because some signal names (DCD, RI) come from modems. Of course your
223 external device may use these signal lines for other purposes.
224
225 TXD(output)
226 Transmitted Data: Outgoing serial data.
227
228 RXD(input)
229 Received Data:Incoming serial data.
230
231 RTS(output)
232 Request To Send: This hardware handshake line informs the modem
233 that your workstation is ready to receive data. Your workstation
234 may automatically reset this signal to indicate that the input
235 buffer is full.
236
237 CTS(input)
238 Clear To Send: The complement to RTS. Indicates that the modem
239 is ready to receive data.
240
241 DTR(output)
242 Data Terminal Ready: This signal tells the modem that the work‐
243 station is ready to establish a link. DTR is often enabled auto‐
244 matically whenever a serial port is opened.
245
246 DSR(input)
247 Data Set Ready: The complement to DTR. Tells the workstation
248 that the modem is ready to establish a link.
249
250 DCD(input)
251 Data Carrier Detect: This line becomes active when a modem
252 detects a “Carrier” signal.
253
254 RI(input)
255 Ring Indicator: Goes active when the modem detects an incoming
256 call.
257
258 BREAK A BREAK condition is not a hardware signal line, but a logical
259 zero on the TXD or RXD lines for a long period of time, usually
260 250 to 500 milliseconds. Normally a receive or transmit data
261 signal stays at the mark (on=1) voltage until the next character
262 is transferred. A BREAK is sometimes used to reset the communi‐
263 cations line or change the operating mode of communications
264 hardware.
265
267 A lot of different errors may occur during serial read operations or
268 during event polling in background. The external device may have been
269 switched off, the data lines may be noisy, system buffers may overrun
270 or your mode settings may be wrong. That is why a reliable software
271 should always catch serial read operations. In cases of an error Tcl
272 returns a general file I/O error. Then fconfigure -lasterror may help
273 to locate the problem. The following error codes may be returned.
274
275 RXOVER Windows input buffer overrun. The data comes faster than your
276 scripts reads it or your system is overloaded. Use fconfigure
277 -sysbuffer to avoid a temporary bottleneck and/or make your
278 script faster.
279
280 TXFULL Windows output buffer overrun. Complement to RXOVER. This
281 error should practically not happen, because Tcl cares about
282 the output buffer status.
283
284 OVERRUN UART buffer overrun (hardware) with data lost. The data
285 comes faster than the system driver receives it. Check your
286 advanced serial port settings to enable the FIFO (16550) buf‐
287 fer and/or setup a lower(1) interrupt threshold value.
288
289 RXPARITY A parity error has been detected by your UART. Wrong parity
290 settings with fconfigure -mode or a noisy data line (RXD) may
291 cause this error.
292
293 FRAME A stop-bit error has been detected by your UART. Wrong mode
294 settings with fconfigure -mode or a noisy data line (RXD) may
295 cause this error.
296
297 BREAK A BREAK condition has been detected by your UART (see above).
298
300 Windows
301 Valid values for fileName to open a serial port are of the form
302 comX, where X is a number, generally from 1 to 9. A legacy form
303 accepted as well is comX:. This notation only works for serial
304 ports from 1 to 9. An attempt to open a serial port that does
305 not exist or has a number greater than 9 will fail. An alter‐
306 nate form of opening serial ports is to use the filename
307 //./comX, where X is any number that corresponds to a serial
308 port.
309
310 When running Tcl interactively, there may be some strange inter‐
311 actions between the real console, if one is present, and a com‐
312 mand pipeline that uses standard input or output. If a command
313 pipeline is opened for reading, some of the lines entered at the
314 console will be sent to the command pipeline and some will be
315 sent to the Tcl evaluator. If a command pipeline is opened for
316 writing, keystrokes entered into the console are not visible
317 until the pipe is closed. These problems only occur because
318 both Tcl and the child application are competing for the console
319 at the same time. If the command pipeline is started from a
320 script, so that Tcl is not accessing the console, or if the com‐
321 mand pipeline does not use standard input or output, but is
322 redirected from or to a file, then the above problems do not
323 occur.
324
325 Unix
326 Valid values for fileName to open a serial port are generally of
327 the form /dev/ttyX, where X is a or b, but the name of any
328 pseudo-file that maps to a serial port may be used. Advanced
329 configuration options are only supported for serial ports when
330 Tcl is built to use the POSIX serial interface.
331
332 When running Tcl interactively, there may be some strange inter‐
333 actions between the console, if one is present, and a command
334 pipeline that uses standard input. If a command pipeline is
335 opened for reading, some of the lines entered at the console
336 will be sent to the command pipeline and some will be sent to
337 the Tcl evaluator. This problem only occurs because both Tcl
338 and the child application are competing for the console at the
339 same time. If the command pipeline is started from a script, so
340 that Tcl is not accessing the console, or if the command pipe‐
341 line does not use standard input, but is redirected from a file,
342 then the above problem does not occur.
343
344 See the PORTABILITY ISSUES section of the exec command for additional
345 information not specific to command pipelines about executing applica‐
346 tions on the various platforms
347
349 Open a command pipeline and catch any errors:
350
351 set fl [open "| ls this_file_does_not_exist"]
352 set data [read $fl]
353 if {[catch {close $fl} err]} {
354 puts "ls command failed: $err"
355 }
356
358 file(n), close(n), filename(n), fconfigure(n), gets(n), read(n),
359 puts(n), exec(n), pid(n), fopen(3)
360
362 access mode, append, create, file, non-blocking, open, permissions,
363 pipeline, process, serial
364
365
366
367Tcl 8.3 open(n)