| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 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 fcopy n 8.0 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 |
fcopy \- Copy data from one channel to another |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBfcopy \fIinchan\fR \fIoutchan\fR ?\fB\-size \fIsize\fR? ?\fB\-command \fIcallback\fR? |
| 16 |
.BE |
| 17 |
|
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
The \fBfcopy\fR command copies data from one I/O channel, \fIinchan\fR to another I/O channel, \fIoutchan\fR. |
| 21 |
The \fBfcopy\fR command leverages the buffering in the Tcl I/O system to |
| 22 |
avoid extra copies and to avoid buffering too much data in |
| 23 |
main memory when copying large files to slow destinations like |
| 24 |
network sockets. |
| 25 |
.PP |
| 26 |
The \fBfcopy\fR |
| 27 |
command transfers data from \fIinchan\fR until end of file |
| 28 |
or \fIsize\fR bytes or characters have been |
| 29 |
transferred; \fIsize\fR is in bytes if the two channels are using the |
| 30 |
same encoding, and is in characters otherwise. |
| 31 |
If no \fB\-size\fR argument is given, |
| 32 |
then the copy goes until end of file. |
| 33 |
All the data read from \fIinchan\fR is copied to \fIoutchan\fR. |
| 34 |
Without the \fB\-command\fR option, \fBfcopy\fR blocks until the copy is complete |
| 35 |
and returns the number of bytes or characters (using the same rules as |
| 36 |
for the \fB\-size\fR option) written to \fIoutchan\fR. |
| 37 |
.PP |
| 38 |
The \fB\-command\fR argument makes \fBfcopy\fR work in the background. |
| 39 |
In this case it returns immediately and the \fIcallback\fR is invoked |
| 40 |
later when the copy completes. |
| 41 |
The \fIcallback\fR is called with |
| 42 |
one or two additional |
| 43 |
arguments that indicates how many bytes were written to \fIoutchan\fR. |
| 44 |
If an error occurred during the background copy, the second argument is the |
| 45 |
error string associated with the error. |
| 46 |
With a background copy, |
| 47 |
it is not necessary to put \fIinchan\fR or \fIoutchan\fR into |
| 48 |
non-blocking mode; the \fBfcopy\fR command takes care of that automatically. |
| 49 |
However, it is necessary to enter the event loop by using |
| 50 |
the \fBvwait\fR command or by using Tk. |
| 51 |
.PP |
| 52 |
You are not allowed to do other input operations with \fIinchan\fR, or |
| 53 |
output operations with \fIoutchan\fR, during a background |
| 54 |
\fBfcopy\fR. The converse is entirely legitimate, as exhibited by the |
| 55 |
bidirectional fcopy example below. |
| 56 |
.PP |
| 57 |
If either \fIinchan\fR or \fIoutchan\fR get closed |
| 58 |
while the copy is in progress, the current copy is stopped |
| 59 |
and the command callback is \fInot\fR made. |
| 60 |
If \fIinchan\fR is closed, |
| 61 |
then all data already queued for \fIoutchan\fR is written out. |
| 62 |
.PP |
| 63 |
Note that \fIinchan\fR can become readable during a background copy. |
| 64 |
You should turn off any \fBfileevent\fR handlers during a background |
| 65 |
copy so those handlers do not interfere with the copy. |
| 66 |
Any wrong-sided I/O attempted (by a \fBfileevent\fR handler or otherwise) will get a |
| 67 |
.QW "channel busy" |
| 68 |
error. |
| 69 |
.PP |
| 70 |
\fBFcopy\fR translates end-of-line sequences in \fIinchan\fR and \fIoutchan\fR |
| 71 |
according to the \fB\-translation\fR option |
| 72 |
for these channels. |
| 73 |
See the manual entry for \fBfconfigure\fR for details on the |
| 74 |
\fB\-translation\fR option. |
| 75 |
The translations mean that the number of bytes read from \fIinchan\fR |
| 76 |
can be different than the number of bytes written to \fIoutchan\fR. |
| 77 |
Only the number of bytes written to \fIoutchan\fR is reported, |
| 78 |
either as the return value of a synchronous \fBfcopy\fR or |
| 79 |
as the argument to the callback for an asynchronous \fBfcopy\fR. |
| 80 |
.PP |
| 81 |
\fBFcopy\fR obeys the encodings and character translations configured |
| 82 |
for the channels. This |
| 83 |
means that the incoming characters are converted internally first |
| 84 |
UTF-8 and then into the encoding of the channel \fBfcopy\fR writes |
| 85 |
to. See the manual entry for \fBfconfigure\fR for details on the |
| 86 |
\fB\-encoding\fR and \fB\-translation\fR options. No conversion is |
| 87 |
done if both channels are |
| 88 |
set to encoding |
| 89 |
.QW binary |
| 90 |
and have matching translations. If only the output channel is set to encoding |
| 91 |
.QW binary |
| 92 |
the system will write the internal UTF-8 representation of the incoming |
| 93 |
characters. If only the input channel is set to encoding |
| 94 |
.QW binary |
| 95 |
the system will assume that the incoming |
| 96 |
bytes are valid UTF-8 characters and convert them according to the |
| 97 |
output encoding. The behaviour of the system for bytes which are not |
| 98 |
valid UTF-8 characters is undefined in this case. |
| 99 |
.SH EXAMPLES |
| 100 |
.PP |
| 101 |
The first example transfers the contents of one channel exactly to |
| 102 |
another. Note that when copying one file to another, it is better to |
| 103 |
use \fBfile copy\fR which also copies file metadata (e.g. the file |
| 104 |
access permissions) where possible. |
| 105 |
.PP |
| 106 |
.CS |
| 107 |
fconfigure $in -translation binary |
| 108 |
fconfigure $out -translation binary |
| 109 |
\fBfcopy\fR $in $out |
| 110 |
.CE |
| 111 |
.PP |
| 112 |
This second example shows how the callback gets |
| 113 |
passed the number of bytes transferred. |
| 114 |
It also uses vwait to put the application into the event loop. |
| 115 |
Of course, this simplified example could be done without the command |
| 116 |
callback. |
| 117 |
.PP |
| 118 |
.CS |
| 119 |
proc Cleanup {in out bytes {error {}}} { |
| 120 |
global total |
| 121 |
set total $bytes |
| 122 |
close $in |
| 123 |
close $out |
| 124 |
if {[string length $error] != 0} { |
| 125 |
# error occurred during the copy |
| 126 |
} |
| 127 |
} |
| 128 |
set in [open $file1] |
| 129 |
set out [socket $server $port] |
| 130 |
\fBfcopy\fR $in $out -command [list Cleanup $in $out] |
| 131 |
vwait total |
| 132 |
.CE |
| 133 |
.PP |
| 134 |
The third example copies in chunks and tests for end of file |
| 135 |
in the command callback. |
| 136 |
.PP |
| 137 |
.CS |
| 138 |
proc CopyMore {in out chunk bytes {error {}}} { |
| 139 |
global total done |
| 140 |
incr total $bytes |
| 141 |
if {([string length $error] != 0) || [eof $in]} { |
| 142 |
set done $total |
| 143 |
close $in |
| 144 |
close $out |
| 145 |
} else { |
| 146 |
\fBfcopy\fR $in $out -size $chunk \e |
| 147 |
-command [list CopyMore $in $out $chunk] |
| 148 |
} |
| 149 |
} |
| 150 |
set in [open $file1] |
| 151 |
set out [socket $server $port] |
| 152 |
set chunk 1024 |
| 153 |
set total 0 |
| 154 |
\fBfcopy\fR $in $out -size $chunk \e |
| 155 |
-command [list CopyMore $in $out $chunk] |
| 156 |
vwait done |
| 157 |
.CE |
| 158 |
.PP |
| 159 |
The fourth example starts an asynchronous, bidirectional fcopy between |
| 160 |
two sockets. Those could also be pipes from two [open "|hal 9000" r+] |
| 161 |
(though their conversation would remain secret to the script, since |
| 162 |
all four fileevent slots are busy). |
| 163 |
.PP |
| 164 |
.CS |
| 165 |
set flows 2 |
| 166 |
proc Done {dir args} { |
| 167 |
global flows done |
| 168 |
puts "$dir is over." |
| 169 |
incr flows -1 |
| 170 |
if {$flows<=0} {set done 1} |
| 171 |
} |
| 172 |
\fBfcopy\fR $sok1 $sok2 -command [list Done UP] |
| 173 |
\fBfcopy\fR $sok2 $sok1 -command [list Done DOWN] |
| 174 |
vwait done |
| 175 |
.CE |
| 176 |
.SH "SEE ALSO" |
| 177 |
eof(n), fblocked(n), fconfigure(n), file(n) |
| 178 |
.SH KEYWORDS |
| 179 |
blocking, channel, end of line, end of file, nonblocking, read, translation |
| 180 |
'\" Local Variables: |
| 181 |
'\" mode: nroff |
| 182 |
'\" End: |