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
16

DESCRIPTION

18       This command opens a file, serial port, or command pipeline and returns
19       a channel identifier that may be used in future invocations of commands
20       like  read, puts, and close.  If the first character of fileName is not
21       | then the command opens a file: fileName gives the name of the file to
22       open,  and it must conform to the conventions described in the filename
23       manual entry.
24
25       The access argument, if present, indicates the way in  which  the  file
26       (or  command pipeline) is to be accessed.  In the first form access may
27       have any of the following values:
28
29       r              Open the file for reading only; the  file  must  already
30                      exist. This is the default value if access is not speci‐
31                      fied.
32
33       r+             Open the file for both reading  and  writing;  the  file
34                      must already exist.
35
36       w              Open  the  file  for  writing  only.   Truncate it if it
37                      exists.  If it doesn't exist, create a new file.
38
39       w+             Open the file for reading and writing.  Truncate  it  if
40                      it exists.  If it doesn't exist, create a new file.
41
42       a              Open  the  file  for  writing only.  If the file doesn't
43                      exist, create a new empty file.  Set the file pointer to
44                      the end of the file prior to each write.
45
46       a+             Open  the  file  for  reading  and writing.  If the file
47                      doesn't exist, create a new empty file.  Set the initial
48                      access position  to the end of the file.
49
50       In  the  second form, access consists of a list of any of the following
51       flags, all of which have the standard POSIX meanings.  One of the flags
52       must be either RDONLY, WRONLY or RDWR.
53
54       RDONLY         Open the file for reading only.
55
56       WRONLY         Open the file for writing only.
57
58       RDWR           Open the file for both reading and writing.
59
60       APPEND         Set  the  file  pointer  to the end of the file prior to
61                      each write.
62
63       CREAT          Create the file if it  doesn't  already  exist  (without
64                      this flag it is an error for the file not to exist).
65
66       EXCL           If  CREAT is also specified, an error is returned if the
67                      file already exists.
68
69       NOCTTY         If the file is a terminal device, this flag prevents the
70                      file  from  becoming  the  controlling  terminal  of the
71                      process.
72
73       NONBLOCK       Prevents the process from  blocking  while  opening  the
74                      file,  and  possibly  in subsequent I/O operations.  The
75                      exact behavior of this flag is system- and device-depen‐
76                      dent;   its  use is discouraged (it is better to use the
77                      fconfigure command to put a file in  nonblocking  mode).
78                      For  details  refer  to your system documentation on the
79                      open system call's O_NONBLOCK flag.
80
81       TRUNC          If the file exists it is truncated to zero length.
82
83       If a new file is created as part of opening it, permissions  (an  inte‐
84       ger)  is  used  to  set the permissions for the new file in conjunction
85       with the process's file mode creation mask.   Permissions  defaults  to
86       0666.
87
88       Note  that  if  you are going to be reading or writing binary data from
89       the channel created by this command, you should use the fconfigure com‐
90       mand  to change the -translation option of the channel to binary before
91       transferring any binary data.  This is in contrast to the ``b'' charac‐
92       ter  passed  as  part of the equivalent of the access parameter to some
93       versions of the C library fopen() function.
94
95

COMMAND PIPELINES

97       If the first character of fileName is ``|'' then the remaining  charac‐
98       ters  of  fileName  are  treated as a list of arguments that describe a
99       command pipeline to invoke, in the same  style  as  the  arguments  for
100       exec.   In  this  case,  the channel identifier returned by open may be
101       used to write to the command's input pipe or read from its output pipe,
102       depending  on  the value of access.  If write-only access is used (e.g.
103       access is w), then standard output for the pipeline is directed to  the
104       current standard output unless overridden by the command.  If read-only
105       access is used (e.g. access is r), standard input for the  pipeline  is
106       taken from the current standard input unless overridden by the command.
107       The id of the spawned process is accessible through  the  pid  command,
108       using 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

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 │
136              the  number  of  data bits and should be an integer from 5 to 8, │
137              while stop is the number of stop bits and should be the  integer │
138              1 or 2.                                                          │
139
140       -handshake type                                                         │
141              (Windows and Unix). This option is used to setup automatic hand‐ │
142              shake control. Note that not all handshake types maybe supported │
143              by  your  operating  system. The type parameter is case-indepen‐ │
144              dent.                                                            │
145
146              If type is none then any  handshake  is  switched  off.   rtscts 
147              activates  hardware  handshake.  Hardware  handshake signals are │
148              described below.  For software handshake xonxoff  the  handshake │
149              characters can be redefined with -xchar.  An additional hardware │
150              handshake dtrdsr is available only under Windows.  There  is  no │
151              default  handshake  configuration,  the initial value depends on │
152              your operating system settings.  The -handshake option cannot be │
153              queried.                                                         │
154
155       -queue                                                                  
156              (Windows  and  Unix). The -queue option can only be queried.  It │
157              returns a list of two integers representing the  current  number │
158              of bytes in the input and output queue respectively.             │
159
160       -timeout msec                                                           │
161              (Windows  and  Unix). This option is used to set the timeout for │
162              blocking read operations.  It  specifies  the  maximum  interval │
163              between  the  reception  of two bytes in milliseconds.  For Unix │
164              systems the  granularity  is  100  milliseconds.   The  -timeout 
165              option  does  not  affect write operations or nonblocking reads. │
166              This option cannot be queried.                                   │
167
168       -ttycontrol {signal boolean signal boolean ...}
169              (Windows and Unix). This option is used to setup  the  handshake │
170              output lines (see below) permanently or to send a BREAK over the │
171              serial line.  The signal names are case-independent.  {RTS 1 DTR 
172              0}  sets  the RTS output to high and the DTR output to low.  The │
173              BREAK condition (see below) is enabled and disabled with  {BREAK 
174              1}  and  {BREAK 0} respectively.  It's not a good idea to change │
175              the RTS (or DTR) signal with active  hardware  handshake  rtscts 
176              (or  dtrdsr).   The  result  is  unpredictable.  The -ttycontrol 
177              option cannot be queried.                                        │
178
179       -ttystatus                                                              
180              (Windows and Unix). The -ttystatus option can only  be  queried. │
181              It  returns the current modem status and handshake input signals │
182              (see below).  The result is a list of signal,value pairs with  a │
183              fixed  order, e.g. {CTS 1 DSR 0 RING 1 DCD 0}.  The signal names │
184              are returned upper case.                                         │
185
186       -xchar {xonChar xoffChar}
187              (Windows and Unix). This option is used to query or  change  the │
188              software  handshake  characters.  Normally  the operating system │
189              default should be DC1 (0x11) and  DC3  (0x13)  representing  the │
190              ASCII standard XON and XOFF characters.                          │
191
192       -pollinterval msec                                                      │
193              (Windows  only).  This  option  is  used to set the maximum time │
194              between polling for fileevents.  This affects the time  interval │
195              between  checking for events throughout the Tcl interpreter (the │
196              smallest value always wins).  Use this option only if  you  want │
197              to  poll  the  serial  port more or less often than 10 msec (the │
198              default).                                                        │
199
200       -sysbuffer inSize                                                       │
201
202       -sysbuffer {inSize outSize}
203              (Windows only). This option is used to change the size  of  Win‐ │
204              dows  system  buffers for a serial channel. Especially at higher │
205              communication rates the default input buffer size of 4096  bytes │
206              can  overrun  for  latent  systems. The first form specifies the │
207              input buffer size, in the second form both input and output buf‐ │
208              fers are defined.                                                │
209
210       -lasterror                                                              
211              (Windows  only). This option is query only.  In case of a serial │
212              communication error, read or puts returns a general Tcl file I/O │
213              error.   fconfigure  -lasterror  can  be called to get a list of │
214              error details.  See below for  an  explanation  of  the  various │
215              error codes.                                                     │
216
217

SERIAL PORT SIGNALS │

219       RS-232  is  the  most  commonly  used standard electrical interface for │
220       serial communications. A negative voltage  (-3V..-12V)  define  a  mark │
221       (on=1) bit and a positive voltage (+3..+12V) define a space (off=0) bit │
222       (RS-232C).  The following signals are specified for incoming and outgo‐ │
223       ing  data,  status  lines  and handshaking. Here we are using the terms │
224       workstation for your  computer  and  modem  for  the  external  device, │
225       because  some  signal  names (DCD, RI) come from modems. Of course your │
226       external device may use these signal lines for other purposes.          │
227
228
229       TXD(output)                                                             
230              Transmitted Data: Outgoing serial data.                          │
231
232       RXD(input)                                                              
233              Received Data:Incoming serial data.                              │
234
235       RTS(output)                                                             
236              Request To Send: This hardware handshake line informs the  modem │
237              that your workstation is ready to receive data. Your workstation │
238              may automatically reset this signal to indicate that  the  input │
239              buffer is full.                                                  │
240
241       CTS(input)                                                              
242              Clear  To  Send: The complement to RTS. Indicates that the modem │
243              is ready to receive data.                                        │
244
245       DTR(output)                                                             
246              Data Terminal Ready: This signal tells the modem that the  work‐ │
247              station is ready to establish a link. DTR is often enabled auto‐ │
248              matically whenever a serial port is opened.                      │
249
250       DSR(input)                                                              
251              Data Set Ready: The complement to  DTR.  Tells  the  workstation │
252              that the modem is ready to establish a link.                     │
253
254       DCD(input)                                                              
255              Data  Carrier  Detect:  This  line  becomes  active when a modem │
256              detects a "Carrier" signal.                                      │
257
258       RI(input)                                                               
259              Ring Indicator: Goes active when the modem detects  an  incoming │
260              call.                                                            │
261
262       BREAK                                                                   
263              A  BREAK  condition is not a hardware signal line, but a logical │
264              zero on the TXD or RXD lines for a long period of time,  usually │
265              250  to  500  milliseconds.  Normally a receive or transmit data │
266              signal stays at the mark (on=1) voltage until the next character │
267              is  transferred. A BREAK is sometimes used to reset the communi‐ │
268              cations line or change  the  operating  mode  of  communications │
269              hardware.                                                        │
270
271

ERROR CODES (Windows only) │

273       A  lot  of  different errors may occur during serial read operations or │
274       during event polling in background. The external device may  have  been │
275       switched  off,  the data lines may be noisy, system buffers may overrun │
276       or your mode settings may be wrong.  That's  why  a  reliable  software │
277       should  always  catch serial read operations.  In cases of an error Tcl │
278       returns a general file I/O error.  Then fconfigure -lasterror may  help │
279       to locate the problem.  The following error codes may be returned.      │
280
281
282       RXOVER                                                                  
283                 Windows input buffer overrun. The data comes faster than your │
284                 scripts reads it or your system is overloaded. Use fconfigure 
285                 -sysbuffer  to  avoid a temporary bottleneck and/or make your │
286                 script faster.                                                │
287
288       TXFULL                                                                  
289                 Windows output buffer overrun.  Complement  to  RXOVER.  This │
290                 error  should practically not happen, because Tcl cares about │
291                 the output buffer status.                                     │
292
293       OVERRUN                                                                 
294                 UART buffer overrun (hardware)  with  data  lost.   The  data │
295                 comes  faster than the system driver receives it.  Check your │
296                 advanced serial port settings to enable the FIFO (16550) buf‐ │
297                 fer and/or setup a lower(1) interrupt threshold value.        │
298
299       RXPARITY                                                                
300                 A  parity error has been detected by your UART.  Wrong parity │
301                 settings with fconfigure -mode or a noisy data line (RXD) may │
302                 cause this error.                                             │
303
304       FRAME                                                                   
305                 A  stop-bit error has been detected by your UART.  Wrong mode │
306                 settings with fconfigure -mode or a noisy data line (RXD) may │
307                 cause this error.                                             │
308
309       BREAK                                                                   
310                 A BREAK condition has been detected by your UART (see above).
311
312

PORTABILITY ISSUES

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

EXAMPLE

406       Open a command pipeline and catch any errors:
407              set fl [open "| ls this_file_does_not_exist"]
408              set data [read $fl]
409              if {[catch {close $fl} err]} {
410                  puts "ls command failed: $err"
411              }
412
413

SEE ALSO

415       file(n),   close(n),   filename(n),  fconfigure(n),  gets(n),  read(n),
416       puts(n), exec(n), pid(n), fopen(3)
417
418

KEYWORDS

420       access mode, append, create,  file,  non-blocking,  open,  permissions,
421       pipeline, process, serial
422
423
424
425Tcl                                   8.3                              open(n)
Impressum