OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tcl8.6.12 / doc / close.n
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 close n 7.5 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 close \- Close an open channel
14 .SH SYNOPSIS
15 \fBclose \fIchannelId\fR ?r(ead)|w(rite)?
16 .BE
17 .SH DESCRIPTION
18 .PP
19 Closes or half-closes the channel given by \fIchannelId\fR.
20 .PP
21 \fIChannelId\fR must be an identifier for an open channel such as a
22 Tcl standard channel (\fBstdin\fR, \fBstdout\fR, or \fBstderr\fR),
23 the return value from an invocation of \fBopen\fR or \fBsocket\fR, or
24 the result of a channel creation command provided by a Tcl extension.
25 .PP
26 The single-argument form is a simple
27 .QW "full-close" :
28 all buffered output is flushed to the channel's output device,
29 any buffered input is discarded, the underlying file or device is closed,
30 and \fIchannelId\fR becomes unavailable for use.
31 .PP
32 If the channel is blocking, the command does not return until all output
33 is flushed.
34 If the channel is nonblocking and there is unflushed output, the
35 channel remains open and the command
36 returns immediately; output will be flushed in the background and the
37 channel will be closed when all the flushing is complete.
38 .PP
39 If \fIchannelId\fR is a blocking channel for a command pipeline then
40 \fBclose\fR waits for the child processes to complete.
41 .PP
42 If the channel is shared between interpreters, then \fBclose\fR
43 makes \fIchannelId\fR unavailable in the invoking interpreter but has no
44 other effect until all of the sharing interpreters have closed the
45 channel.
46 When the last interpreter in which the channel is registered invokes
47 \fBclose\fR, the cleanup actions described above occur. See the
48 \fBinterp\fR command for a description of channel sharing.
49 .PP
50 Channels are automatically closed when an interpreter is destroyed and
51 when the process exits.
52 .VS 8.6
53 From 8.6 on (TIP#398), nonblocking channels are no longer switched to blocking mode when exiting; this guarantees a timely exit even when the peer or a communication channel is stalled. To ensure proper flushing of stalled nonblocking channels on exit, one must now either (a) actively switch them back to blocking or (b) use the environment variable TCL_FLUSH_NONBLOCKING_ON_EXIT,  which when set and not equal to "0" restores the previous behavior.
54 .VE 8.6
55 .PP
56 The command returns an empty string, and may generate an error if
57 an error occurs while flushing output.  If a command in a command
58 pipeline created with \fBopen\fR returns an error, \fBclose\fR
59 generates an error (similar to the \fBexec\fR command.)
60 .PP
61 .VS 8.6
62 The two-argument form is a
63 .QW "half-close" :
64 given a bidirectional channel like a
65 socket or command pipeline and a (possibly abbreviated) direction, it closes
66 only the sub-stream going in that direction. This means a shutdown() on a
67 socket, and a close() of one end of a pipe for a command pipeline. Then, the
68 Tcl-level channel data structure is either kept or freed depending on whether
69 the other direction is still open.
70 .PP
71 A single-argument close on an already half-closed bidirectional channel is
72 defined to just
73 .QW "finish the job" .
74 A half-close on an already closed half, or on a wrong-sided unidirectional
75 channel, raises an error.
76 .PP
77 In the case of a command pipeline, the child-reaping duty falls upon the
78 shoulders of the last close or half-close, which is thus allowed to report an
79 abnormal exit error.
80 .PP
81 Currently only sockets and command pipelines support half-close. A future
82 extension will allow reflected and stacked channels to do so.
83 .VE 8.6
84 .SH EXAMPLE
85 .PP
86 This illustrates how you can use Tcl to ensure that files get closed
87 even when errors happen by combining \fBcatch\fR, \fBclose\fR and
88 \fBreturn\fR:
89 .PP
90 .CS
91 proc withOpenFile {filename channelVar script} {
92     upvar 1 $channelVar chan
93     set chan [open $filename]
94     catch {
95         uplevel 1 $script
96     } result options
97     \fBclose\fR $chan
98     return -options $options $result
99 }
100 .CE
101 .SH "SEE ALSO"
102 file(n), open(n), socket(n), eof(n), Tcl_StandardChannels(3)
103 .SH KEYWORDS
104 blocking, channel, close, nonblocking, half-close
105 '\" Local Variables:
106 '\" mode: nroff
107 '\" fill-column: 78
108 '\" End: