1open(n)                      Tcl Built-In Commands                     open(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       open - Open a file-based or command pipeline channel
9

SYNOPSIS

11       open fileName
12       open fileName access
13       open fileName access permissions
14______________________________________________________________________________
15

DESCRIPTION

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 ex‐
36                      ists.  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 ac‐
47                      cess 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 bi‐
52       nary option, making the channel suitable for reading or writing of  bi‐
53       nary data.
54
55       In  the  second form, access consists of a list of any of the following
56       flags, most of which have the standard  POSIX  meanings.   One  of  the
57       flags 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  bi‐
69                      nary 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

COMMAND PIPELINES

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
103w”), 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  er‐
112       ror  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

SERIAL COMMUNICATIONS

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 ac‐
146              tivates hardware handshake. Hardware handshake signals  are  de‐
147              scribed  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 be‐
161              tween the reception of two bytes in milliseconds.  For Unix sys‐
162              tems  the  granularity is 100 milliseconds.  The -timeout option
163              does not affect write operations or nonblocking reads.  This op‐
164              tion 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  op‐
175              tion 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 de‐
187              fault should be DC1 (0x11) and DC3 (0x13) representing the ASCII
188              standard XON and XOFF characters.
189
190       -pollinterval msec
191              (Windows only). This option is used to set the maximum time  be‐
192              tween  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 de‐
196              fault).
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 in‐
205              put  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 er‐
212              ror  details.  See below for an explanation of the various error
213              codes.
214

SERIAL PORT SIGNALS

216       RS-232 is the most commonly used standard electrical interface for  se‐
217       rial  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, be‐
222       cause some signal names (DCD, RI) come from modems. Of course your  ex‐
223       ternal 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  de‐
252              tects 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

ERROR CODES (Windows only)

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 er‐
281                 ror 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

PORTABILITY ISSUES

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 un‐
317              til the pipe is closed.  These problems only occur because  both
318              Tcl  and  the child application are competing for the console at
319              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 oc‐
323              cur.
324
325              Files opened in the “a” mode or with the APPEND flag set are im‐
326              plemented by seeking immediately before each write, which is not
327              an atomic operation and does not carry the guarantee  of  strict
328              appending that is present on POSIX platforms.
329
330       Unix
331              Valid values for fileName to open a serial port are generally of
332              the form /dev/ttyX, where X is a or  b,  but  the  name  of  any
333              pseudo-file  that  maps  to a serial port may be used.  Advanced
334              configuration options are only supported for serial  ports  when
335              Tcl is built to use the POSIX serial interface.
336
337              When running Tcl interactively, there may be some strange inter‐
338              actions between the console, if one is present,  and  a  command
339              pipeline  that  uses  standard  input.  If a command pipeline is
340              opened for reading, some of the lines  entered  at  the  console
341              will  be  sent  to the command pipeline and some will be sent to
342              the Tcl evaluator.  This problem only occurs  because  both  Tcl
343              and  the  child application are competing for the console at the
344              same time.  If the command pipeline is started from a script, so
345              that  Tcl  is not accessing the console, or if the command pipe‐
346              line does not use standard input, but is redirected from a file,
347              then the above problem does not occur.
348
349       See  the  PORTABILITY ISSUES section of the exec command for additional
350       information not specific to command pipelines about executing  applica‐
351       tions on the various platforms
352

EXAMPLES

354       Open  a file for writing, forcing it to be created and raising an error
355       if it already exists.
356
357              set myNewFile [open filename.txt {WRONLY CREAT EXCL}]
358
359       Open a file for writing as a log file.
360
361              set myLogFile [open filename.log "a"]
362              fconfigure $myLogFile -buffering line
363
364       Open a command pipeline and catch any errors:
365
366              set fl [open "| ls this_file_does_not_exist"]
367              set data [read $fl]
368              if {[catch {close $fl} err]} {
369                  puts "ls command failed: $err"
370              }
371
372       Open a command pipeline and read binary data from it. Note the  unusual
373       form  with  “|[list” that handles non-trivial edge cases with arguments
374       that potentially have spaces in.
375
376              set fl [open |[list create_image_data $input] "rb"]
377              set binData [read $fl]
378              close $fl
379

SEE ALSO

381       file(n),  close(n),  filename(n),  fconfigure(n),   gets(n),   read(n),
382       puts(n), exec(n), pid(n), fopen(3)
383

KEYWORDS

385       access  mode,  append,  create,  file, non-blocking, open, permissions,
386       pipeline, process, serial
387
388
389
390Tcl                                   8.3                              open(n)
Impressum