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
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 with the -translation binary option, mak‐ │
52       ing the channel suitable for reading or writing of binary data.
53
54       In the second form, access consists of a list of any of  the  following
55       flags, all of which have the standard POSIX meanings.  One of the flags
56       must be either RDONLY, WRONLY or RDWR.
57
58       RDONLY         Open the file for reading only.
59
60       WRONLY         Open the file for writing only.
61
62       RDWR           Open the file for both reading and writing.
63
64       APPEND         Set the file pointer to the end of  the  file  prior  to
65                      each write.                                              │
66
67       BINARY                                                                  
68                      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

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  w),
103       then  standard output for the pipeline is directed to the current stan‐
104       dard output unless overridden by the command.  If read-only  access  is
105       used  (e.g. access is r), standard input for the pipeline is taken from
106       the current standard input unless overridden by the command.  The id of
107       the  spawned  process  is accessible through the pid command, using the
108       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

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
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

SERIAL PORT SIGNALS

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

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
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

PORTABILITY ISSUES

300       Windows (all versions)
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 4.   This  nota‐
303              tion only works for serial ports from 1 to 9, if the system hap‐
304              pens to have more than four.  An attempt to open a  serial  port
305              that  does  not  exist or has a number greater than 9 will fail.
306              An alternate form of opening serial ports is to use the filename
307              \\.\comX,  where  X  is  any number that corresponds to a serial
308              port; please note that this method  is  considerably  slower  on
309              Windows 95 and Windows 98.
310
311       Windows NT
312              When running Tcl interactively, there may be some strange inter‐
313              actions between the real console, if one is present, and a  com‐
314              mand  pipeline that uses standard input or output.  If a command
315              pipeline is opened for reading, some of the lines entered at the
316              console  will  be  sent to the command pipeline and some will be
317              sent to the Tcl evaluator.  If a command pipeline is opened  for
318              writing,  keystrokes  entered  into  the console are not visible
319              until the pipe is closed.  This behavior occurs whether the com‐
320              mand pipeline is executing 16-bit or 32-bit applications.  These
321              problems only occur because both Tcl and the  child  application
322              are  competing for the console at the same time.  If the command
323              pipeline is started from a script, so that Tcl is not  accessing
324              the  console,  or  if the command pipeline does not use standard
325              input or output, but is redirected from or to a file,  then  the
326              above problems do not occur.
327
328       Windows 95
329              A command pipeline that executes a 16-bit DOS application cannot
330              be opened for both reading and writing, since 16-bit DOS  appli‐
331              cations  that  receive standard input from a pipe and send stan‐
332              dard output to a pipe run synchronously.  Command pipelines that
333              do  not  execute  16-bit DOS applications run asynchronously and
334              can be opened for both reading and writing.
335
336              When running Tcl interactively, there may be some strange inter‐
337              actions  between the real console, if one is present, and a com‐
338              mand pipeline that uses standard input or output.  If a  command
339              pipeline  is  opened for reading from a 32-bit application, some
340              of the keystrokes entered at the console will  be  sent  to  the
341              command pipeline and some will be sent to the Tcl evaluator.  If
342              a command pipeline is opened for writing to  a  32-bit  applica‐
343              tion,  no  output  is  visible  on the console until the pipe is
344              closed.  These problems only occur  because  both  Tcl  and  the
345              child  application  are  competing  for  the console at the same
346              time.  If the command pipeline is started from a script, so that
347              Tcl  is  not  accessing  the console, or if the command pipeline
348              does not use standard input or output, but is redirected from or
349              to a file, then the above problems do not occur.
350
351              Whether  or not Tcl is running interactively, if a command pipe‐
352              line is opened for reading from a 16-bit  DOS  application,  the
353              call to open will not return until end-of-file has been received
354              from the command pipeline's standard output.  If a command pipe‐
355              line  is opened for writing to a 16-bit DOS application, no data
356              will be sent to the command pipeline's standard output until the
357              pipe is actually closed.  This problem occurs because 16-bit DOS
358              applications are run synchronously, as described above.
359
360       Unix
361              Valid values for fileName to open a serial port are generally of
362              the  form  /dev/ttyX,  where  X  is  a or b, but the name of any
363              pseudo-file that maps to a serial port may  be  used.   Advanced
364              configuration  options  are only supported for serial ports when
365              Tcl is built to use the POSIX serial interface.
366
367              When running Tcl interactively, there may be some strange inter‐
368              actions  between  the  console, if one is present, and a command
369              pipeline that uses standard input.  If  a  command  pipeline  is
370              opened  for  reading,  some  of the lines entered at the console
371              will be sent to the command pipeline and some will  be  sent  to
372              the  Tcl  evaluator.   This problem only occurs because both Tcl
373              and the child application are competing for the console  at  the
374              same time.  If the command pipeline is started from a script, so
375              that Tcl is not accessing the console, or if the  command  pipe‐
376              line does not use standard input, but is redirected from a file,
377              then the above problem does not occur.
378
379       See the PORTABILITY ISSUES section of the exec command  for  additional
380       information  not specific to command pipelines about executing applica‐
381       tions on the various platforms
382

EXAMPLE

384       Open a command pipeline and catch any errors:
385              set fl [open "| ls this_file_does_not_exist"]
386              set data [read $fl]
387              if {[catch {close $fl} err]} {
388                  puts "ls command failed: $err"
389              }
390

SEE ALSO

392       file(n),  close(n),  filename(n),  fconfigure(n),   gets(n),   read(n),
393       puts(n), exec(n), pid(n), fopen(3)
394

KEYWORDS

396       access  mode,  append,  create,  file, non-blocking, open, permissions,
397       pipeline, process, serial
398
399
400
401Tcl                                   8.3                              open(n)
Impressum