Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/io.test @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 218.0 KB
Line 
1# -*- tcl -*-
2# Functionality covered: operation of all IO commands, and all procedures
3# defined in generic/tclIO.c.
4#
5# This file contains a collection of tests for one or more of the Tcl
6# built-in commands.  Sourcing this file into Tcl runs the tests and
7# generates output for errors.  No output means no errors were found.
8#
9# Copyright (c) 1991-1994 The Regents of the University of California.
10# Copyright (c) 1994-1997 Sun Microsystems, Inc.
11# Copyright (c) 1998-1999 by Scriptics Corporation.
12#
13# See the file "license.terms" for information on usage and redistribution
14# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15#
16# RCS: @(#) $Id: io.test,v 1.80 2007/12/13 15:26:06 dgp Exp $
17
18if {[catch {package require tcltest 2}]} {
19    puts stderr "Skipping tests in [info script].  tcltest 2 required."
20    return
21}
22namespace eval ::tcl::test::io {
23    namespace import ::tcltest::*
24
25    variable umaskValue
26    variable path
27    variable f
28    variable i
29    variable n
30    variable v
31    variable msg
32    variable expected
33
34testConstraint testchannel      [llength [info commands testchannel]]
35testConstraint exec             [llength [info commands exec]]
36testConstraint openpipe         1
37testConstraint fileevent        [llength [info commands fileevent]]
38testConstraint fcopy            [llength [info commands fcopy]]
39testConstraint testfevent       [llength [info commands testfevent]]
40testConstraint testchannelevent [llength [info commands testchannelevent]]
41testConstraint testmainthread   [llength [info commands testmainthread]]
42testConstraint testthread       [llength [info commands testthread]]
43
44# You need a *very* special environment to do some tests.  In
45# particular, many file systems do not support large-files...
46testConstraint largefileSupport 0
47
48# some tests can only be run is umask is 2
49# if "umask" cannot be run, the tests will be skipped.
50set umaskValue 0
51testConstraint umask [expr {![catch {set umaskValue [scan [exec /bin/sh -c umask] %o]}]}]
52
53testConstraint makeFileInHome [expr {![file exists ~/_test_] && [file writable ~]}]
54
55# set up a long data file for some of the following tests
56
57set path(longfile) [makeFile {} longfile]
58set f [open $path(longfile) w]
59fconfigure $f -eofchar {} -translation lf
60for { set i 0 } { $i < 100 } { incr i} {
61    puts $f "#123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
62\#123456789abcdef01
63\#"
64    }
65close $f
66
67set path(cat) [makeFile {
68    set f stdin
69    if {$argv != ""} {
70        set f [open [lindex $argv 0]]
71    }
72    fconfigure $f -encoding binary -translation lf -blocking 0 -eofchar \x1a
73    fconfigure stdout -encoding binary -translation lf -buffering none
74    fileevent $f readable "foo $f"
75    proc foo {f} {
76        set x [read $f]
77        catch {puts -nonewline $x}
78        if {[eof $f]} {
79            close $f
80            exit 0
81        }
82    }
83    vwait forever
84} cat]
85
86set thisScript [file join [pwd] [info script]]
87
88proc contents {file} {
89    set f [open $file]
90    fconfigure $f -translation binary
91    set a [read $f]
92    close $f
93    return $a
94}
95
96test io-1.5 {Tcl_WriteChars: CheckChannelErrors} {emptyTest} {
97    # no test, need to cause an async error.
98} {}
99set path(test1) [makeFile {} test1]
100test io-1.6 {Tcl_WriteChars: WriteBytes} {
101    set f [open $path(test1) w]
102    fconfigure $f -encoding binary
103    puts -nonewline $f "a\u4e4d\0"
104    close $f
105    contents $path(test1)
106} "a\x4d\x00"
107test io-1.7 {Tcl_WriteChars: WriteChars} {
108    set f [open $path(test1) w]
109    fconfigure $f -encoding shiftjis
110    puts -nonewline $f "a\u4e4d\0"
111    close $f
112    contents $path(test1)
113} "a\x93\xe1\x00"
114set path(test2) [makeFile {} test2]
115test io-1.8 {Tcl_WriteChars: WriteChars} {
116    # This test written for SF bug #506297.
117    #
118    # Executing this test without the fix for the referenced bug
119    # applied to tcl will cause tcl, more specifically WriteChars, to
120    # go into an infinite loop.
121
122    set f [open $path(test2) w] 
123    fconfigure      $f -encoding iso2022-jp
124    puts -nonewline $f [format %s%c [string repeat " " 4] 12399] 
125    close           $f 
126    contents $path(test2)
127} "    \x1b\$B\$O\x1b(B"
128
129test io-1.9 {Tcl_WriteChars: WriteChars} {
130    # When closing a channel with an encoding that appends
131    # escape bytes, check for the case where the escape
132    # bytes overflow the current IO buffer. The bytes
133    # should be moved into a new buffer.
134
135    set data "1234567890 [format %c 12399]"
136
137    set sizes [list]
138
139    # With default buffer size
140    set f [open $path(test2) w]
141    fconfigure      $f -encoding iso2022-jp
142    puts -nonewline $f $data
143    close           $f
144    lappend sizes [file size $path(test2)]
145
146    # With buffer size equal to the length
147    # of the data, the escape bytes would
148    # go into the next buffer.
149
150    set f [open $path(test2) w]
151    fconfigure      $f -encoding iso2022-jp -buffersize 16
152    puts -nonewline $f $data
153    close           $f
154    lappend sizes [file size $path(test2)]
155
156    # With buffer size that is large enough
157    # to hold 1 byte of escaped data, but
158    # not all 3. This should not write
159    # the escape bytes to the first buffer
160    # and then again to the second buffer.
161
162    set f [open $path(test2) w]
163    fconfigure      $f -encoding iso2022-jp -buffersize 17
164    puts -nonewline $f $data
165    close           $f
166    lappend sizes [file size $path(test2)]
167
168    # With buffer size that can hold 2 out of
169    # 3 bytes of escaped data.
170
171    set f [open $path(test2) w]
172    fconfigure      $f -encoding iso2022-jp -buffersize 18
173    puts -nonewline $f $data
174    close           $f
175    lappend sizes [file size $path(test2)]
176
177    # With buffer size that can hold all the
178    # data and escape bytes.
179
180    set f [open $path(test2) w]
181    fconfigure      $f -encoding iso2022-jp -buffersize 19
182    puts -nonewline $f $data
183    close           $f
184    lappend sizes [file size $path(test2)]
185
186    set sizes
187} {19 19 19 19 19}
188
189test io-2.1 {WriteBytes} {
190    # loop until all bytes are written
191   
192    set f [open $path(test1) w]
193    fconfigure $f  -encoding binary -buffersize 16 -translation crlf
194    puts $f "abcdefghijklmnopqrstuvwxyz"
195    close $f
196    contents $path(test1)
197} "abcdefghijklmnopqrstuvwxyz\r\n"
198test io-2.2 {WriteBytes: savedLF > 0} {
199    # After flushing buffer, there was a \n left over from the last
200    # \n -> \r\n expansion.  It gets stuck at beginning of this buffer.
201
202    set f [open $path(test1) w]
203    fconfigure $f -encoding binary -buffersize 16 -translation crlf
204    puts -nonewline $f "123456789012345\n12"
205    set x [list [contents $path(test1)]]
206    close $f
207    lappend x [contents $path(test1)]
208} [list "123456789012345\r" "123456789012345\r\n12"]
209test io-2.3 {WriteBytes: flush on line} {
210    # Tcl "line" buffering has weird behavior: if current buffer contains
211    # a \n, entire buffer gets flushed.  Logical behavior would be to flush
212    # only up to the \n.
213   
214    set f [open $path(test1) w]
215    fconfigure $f -encoding binary -buffering line -translation crlf
216    puts -nonewline $f "\n12"
217    set x [contents $path(test1)]
218    close $f
219    set x
220} "\r\n12"
221test io-2.4 {WriteBytes: reset sawLF after each buffer} {
222    set f [open $path(test1) w]
223     fconfigure $f -encoding binary -buffering line -translation lf \
224             -buffersize 16
225    puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz"
226    set x [list [contents $path(test1)]]
227    close $f
228    lappend x [contents $path(test1)]
229} [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]
230
231test io-3.1 {WriteChars: compatibility with WriteBytes} {
232    # loop until all bytes are written
233   
234    set f [open $path(test1) w]
235    fconfigure $f -encoding ascii -buffersize 16 -translation crlf
236    puts $f "abcdefghijklmnopqrstuvwxyz"
237    close $f
238    contents $path(test1)
239} "abcdefghijklmnopqrstuvwxyz\r\n"
240test io-3.2 {WriteChars: compatibility with WriteBytes: savedLF > 0} {
241    # After flushing buffer, there was a \n left over from the last
242    # \n -> \r\n expansion.  It gets stuck at beginning of this buffer.
243
244    set f [open $path(test1) w]
245    fconfigure $f -encoding ascii -buffersize 16 -translation crlf
246    puts -nonewline $f "123456789012345\n12"
247    set x [list [contents $path(test1)]]
248    close $f
249    lappend x [contents $path(test1)]
250} [list "123456789012345\r" "123456789012345\r\n12"]
251test io-3.3 {WriteChars: compatibility with WriteBytes: flush on line} {
252    # Tcl "line" buffering has weird behavior: if current buffer contains
253    # a \n, entire buffer gets flushed.  Logical behavior would be to flush
254    # only up to the \n.
255   
256    set f [open $path(test1) w]
257    fconfigure $f -encoding ascii -buffering line -translation crlf
258    puts -nonewline $f "\n12"
259    set x [contents $path(test1)]
260    close $f
261    set x
262} "\r\n12"
263test io-3.4 {WriteChars: loop over stage buffer} {
264    # stage buffer maps to more than can be queued at once.
265
266    set f [open $path(test1) w]
267    fconfigure $f -encoding jis0208 -buffersize 16 
268    puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
269    set x [list [contents $path(test1)]]
270    close $f
271    lappend x [contents $path(test1)]
272} [list "!)!)!)!)!)!)!)!)" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
273test io-3.5 {WriteChars: saved != 0} {
274    # Bytes produced by UtfToExternal from end of last channel buffer
275    # had to be moved to beginning of next channel buffer to preserve
276    # requested buffersize.
277
278    set f [open $path(test1) w]
279    fconfigure $f -encoding jis0208 -buffersize 17 
280    puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
281    set x [list [contents $path(test1)]]
282    close $f
283    lappend x [contents $path(test1)]
284} [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
285test io-3.6 {WriteChars: (stageRead + dstWrote == 0)} {
286    # One incomplete UTF-8 character at end of staging buffer.  Backup
287    # in src to the beginning of that UTF-8 character and try again.
288    #
289    # Translate the first 16 bytes, produce 14 bytes of output, 2 left over
290    # (first two bytes of \uff21 in UTF-8).  Given those two bytes try
291    # translating them again, find that no bytes are read produced, and break
292    # to outer loop where those two bytes will have the remaining 4 bytes
293    # (the last byte of \uff21 plus the all of \uff22) appended.
294
295    set f [open $path(test1) w]
296    fconfigure $f -encoding shiftjis -buffersize 16
297    puts -nonewline $f "12345678901234\uff21\uff22"
298    set x [list [contents $path(test1)]]
299    close $f
300    lappend x [contents $path(test1)]
301} [list "12345678901234\x82\x60" "12345678901234\x82\x60\x82\x61"]
302test io-3.7 {WriteChars: (bufPtr->nextAdded > bufPtr->length)} {
303    # When translating UTF-8 to external, the produced bytes went past end
304    # of the channel buffer.  This is done purpose -- we then truncate the
305    # bytes at the end of the partial character to preserve the requested
306    # blocksize on flush.  The truncated bytes are moved to the beginning
307    # of the next channel buffer.
308
309    set f [open $path(test1) w]
310    fconfigure $f -encoding jis0208 -buffersize 17 
311    puts -nonewline $f "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
312    set x [list [contents $path(test1)]]
313    close $f
314    lappend x [contents $path(test1)]
315} [list "!)!)!)!)!)!)!)!)!" "!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)"]
316test io-3.8 {WriteChars: reset sawLF after each buffer} {
317    set f [open $path(test1) w]
318    fconfigure $f -encoding ascii -buffering line -translation lf \
319             -buffersize 16
320    puts -nonewline $f "abcdefg\nhijklmnopqrstuvwxyz"
321    set x [list [contents $path(test1)]]
322    close $f
323    lappend x [contents $path(test1)]
324} [list "abcdefg\nhijklmno" "abcdefg\nhijklmnopqrstuvwxyz"]
325
326test io-4.1 {TranslateOutputEOL: lf} {
327    # search for \n
328
329    set f [open $path(test1) w]
330    fconfigure $f -buffering line -translation lf
331    puts $f "abcde"
332    set x [list [contents $path(test1)]]
333    close $f
334    lappend x [contents $path(test1)]
335} [list "abcde\n" "abcde\n"]
336test io-4.2 {TranslateOutputEOL: cr} {
337    # search for \n, replace with \r
338
339    set f [open $path(test1) w]
340    fconfigure $f -buffering line -translation cr
341    puts $f "abcde"
342    set x [list [contents $path(test1)]]
343    close $f
344    lappend x [contents $path(test1)]
345} [list "abcde\r" "abcde\r"]
346test io-4.3 {TranslateOutputEOL: crlf} {
347    # simple case: search for \n, replace with \r
348
349    set f [open $path(test1) w]
350    fconfigure $f -buffering line -translation crlf
351    puts $f "abcde"
352    set x [list [contents $path(test1)]]
353    close $f
354    lappend x [contents $path(test1)]
355} [list "abcde\r\n" "abcde\r\n"]
356test io-4.4 {TranslateOutputEOL: crlf} {
357    # keep storing more bytes in output buffer until output buffer is full.
358    # We have 13 bytes initially that would turn into 18 bytes.  Fill
359    # dest buffer while (dstEnd < dstMax).
360
361    set f [open $path(test1) w]
362    fconfigure $f -translation crlf -buffersize 16
363    puts -nonewline $f "1234567\n\n\n\n\nA"
364    set x [list [contents $path(test1)]]
365    close $f
366    lappend x [contents $path(test1)]
367} [list "1234567\r\n\r\n\r\n\r\n\r" "1234567\r\n\r\n\r\n\r\n\r\nA"]
368test io-4.5 {TranslateOutputEOL: crlf} {
369    # Check for overflow of the destination buffer
370
371    set f [open $path(test1) w]
372    fconfigure $f -translation crlf -buffersize 12
373    puts -nonewline $f "12345678901\n456789012345678901234"
374    close $f
375    set x [contents $path(test1)]
376} "12345678901\r\n456789012345678901234"
377
378test io-5.1 {CheckFlush: not full} {
379    set f [open $path(test1) w]
380    fconfigure $f 
381    puts -nonewline $f "12345678901234567890"
382    set x [list [contents $path(test1)]]
383    close $f
384    lappend x [contents $path(test1)]
385} [list "" "12345678901234567890"]
386test io-5.2 {CheckFlush: full} {
387    set f [open $path(test1) w]
388    fconfigure $f -buffersize 16
389    puts -nonewline $f "12345678901234567890"
390    set x [list [contents $path(test1)]]
391    close $f
392    lappend x [contents $path(test1)]
393} [list "1234567890123456" "12345678901234567890"]
394test io-5.3 {CheckFlush: not line} {
395    set f [open $path(test1) w]
396    fconfigure $f -buffering line
397    puts -nonewline $f "12345678901234567890"
398    set x [list [contents $path(test1)]]
399    close $f
400    lappend x [contents $path(test1)]
401} [list "" "12345678901234567890"]
402test io-5.4 {CheckFlush: line} {
403    set f [open $path(test1) w]
404    fconfigure $f -buffering line -translation lf -encoding ascii
405    puts -nonewline $f "1234567890\n1234567890"
406    set x [list [contents $path(test1)]]
407    close $f
408    lappend x [contents $path(test1)]
409} [list "1234567890\n1234567890" "1234567890\n1234567890"]
410test io-5.5 {CheckFlush: none} {
411    set f [open $path(test1) w]
412    fconfigure $f -buffering none
413    puts -nonewline $f "1234567890"
414    set x [list [contents $path(test1)]]
415    close $f
416    lappend x [contents $path(test1)]
417} [list "1234567890" "1234567890"]
418
419test io-6.1 {Tcl_GetsObj: working} {
420    set f [open $path(test1) w]
421    puts $f "foo\nboo"
422    close $f
423    set f [open $path(test1)]
424    set x [gets $f]
425    close $f
426    set x
427} {foo}
428test io-6.2 {Tcl_GetsObj: CheckChannelErrors() != 0} emptyTest {
429    # no test, need to cause an async error.
430} {}
431test io-6.3 {Tcl_GetsObj: how many have we used?} {
432    # if (bufPtr != NULL) {oldRemoved = bufPtr->nextRemoved}
433
434    set f [open $path(test1) w]
435    fconfigure $f -translation crlf
436    puts $f "abc\ndefg"
437    close $f
438    set f [open $path(test1)]
439    set x [list [tell $f] [gets $f line] [tell $f] [gets $f line] $line]
440    close $f
441    set x
442} {0 3 5 4 defg}
443test io-6.4 {Tcl_GetsObj: encoding == NULL} {
444    set f [open $path(test1) w]
445    fconfigure $f -translation binary
446    puts $f "\x81\u1234\0"
447    close $f
448    set f [open $path(test1)]
449    fconfigure $f -translation binary
450    set x [list [gets $f line] $line]
451    close $f
452    set x
453} [list 3 "\x81\x34\x00"]
454test io-6.5 {Tcl_GetsObj: encoding != NULL} {
455    set f [open $path(test1) w]
456    fconfigure $f -translation binary
457    puts $f "\x88\xea\x92\x9a"
458    close $f
459    set f [open $path(test1)]
460    fconfigure $f -encoding shiftjis
461    set x [list [gets $f line] $line]
462    close $f
463    set x
464} [list 2 "\u4e00\u4e01"]
465set a "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
466append a $a
467append a $a
468test io-6.6 {Tcl_GetsObj: loop test} {
469    # if (dst >= dstEnd)
470
471    set f [open $path(test1) w]
472    puts $f $a
473    puts $f hi
474    close $f
475    set f [open $path(test1)]
476    set x [list [gets $f line] $line]
477    close $f
478    set x
479} [list 256 $a]
480test io-6.7 {Tcl_GetsObj: error in input} {stdio openpipe} {
481    # if (FilterInputBytes(chanPtr, &gs) != 0)
482
483    set f [open "|[list [interpreter] $path(cat)]" w+]
484    puts -nonewline $f "hi\nwould"
485    flush $f
486    gets $f
487    fconfigure $f -blocking 0
488    set x [gets $f line]
489    close $f
490    set x
491} {-1}
492test io-6.8 {Tcl_GetsObj: remember if EOF is seen} {
493    set f [open $path(test1) w]
494    puts $f "abcdef\x1aghijk\nwombat"
495    close $f
496    set f [open $path(test1)]
497    fconfigure $f -eofchar \x1a
498    set x [list [gets $f line] $line [gets $f line] $line]
499    close $f
500    set x
501} {6 abcdef -1 {}}
502test io-6.9 {Tcl_GetsObj: remember if EOF is seen} {
503    set f [open $path(test1) w]
504    puts $f "abcdefghijk\nwom\u001abat"
505    close $f
506    set f [open $path(test1)]
507    fconfigure $f -eofchar \x1a
508    set x [list [gets $f line] $line [gets $f line] $line]
509    close $f
510    set x
511} {11 abcdefghijk 3 wom}
512# Comprehensive tests
513test io-6.10 {Tcl_GetsObj: lf mode: no chars} {
514    set f [open $path(test1) w]
515    close $f
516    set f [open $path(test1)]
517    fconfigure $f -translation lf
518    set x [list [gets $f line] $line]
519    close $f
520    set x
521} {-1 {}}
522test io-6.11 {Tcl_GetsObj: lf mode: lone \n} {
523    set f [open $path(test1) w]
524    fconfigure $f -translation lf
525    puts -nonewline $f "\n"
526    close $f
527    set f [open $path(test1)]
528    fconfigure $f -translation lf
529    set x [list [gets $f line] $line [gets $f line] $line]
530    close $f
531    set x
532} {0 {} -1 {}}
533test io-6.12 {Tcl_GetsObj: lf mode: lone \r} {
534    set f [open $path(test1) w]
535    fconfigure $f -translation lf
536    puts -nonewline $f "\r"
537    close $f
538    set f [open $path(test1)]
539    fconfigure $f -translation lf
540    set x [list [gets $f line] $line [gets $f line] $line]
541    close $f
542    set x
543} [list 1 "\r" -1 ""]
544test io-6.13 {Tcl_GetsObj: lf mode: 1 char} {
545    set f [open $path(test1) w]
546    fconfigure $f -translation lf
547    puts -nonewline $f a
548    close $f
549    set f [open $path(test1)]
550    fconfigure $f -translation lf
551    set x [list [gets $f line] $line [gets $f line] $line]
552    close $f
553    set x
554} {1 a -1 {}}
555test io-6.14 {Tcl_GetsObj: lf mode: 1 char followed by EOL} {
556    set f [open $path(test1) w]
557    fconfigure $f -translation lf
558    puts -nonewline $f "a\n"
559    close $f
560    set f [open $path(test1)]
561    fconfigure $f -translation lf
562    set x [list [gets $f line] $line [gets $f line] $line]
563    close $f
564    set x
565} {1 a -1 {}}
566test io-6.15 {Tcl_GetsObj: lf mode: several chars} {
567    set f [open $path(test1) w]
568    fconfigure $f -translation lf
569    puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
570    close $f
571    set f [open $path(test1)]
572    fconfigure $f -translation lf
573    set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line [gets $f line] $line]
574    close $f
575    set x
576} [list 4 "abcd" 10 "efgh\rijkl\r" 4 "mnop" -1 ""]
577test io-6.16 {Tcl_GetsObj: cr mode: no chars} {
578    set f [open $path(test1) w]
579    close $f
580    set f [open $path(test1)]
581    fconfigure $f -translation cr
582    set x [list [gets $f line] $line]
583    close $f
584    set x
585} {-1 {}}
586test io-6.17 {Tcl_GetsObj: cr mode: lone \n} {
587    set f [open $path(test1) w]
588    fconfigure $f -translation lf
589    puts -nonewline $f "\n"
590    close $f
591    set f [open $path(test1)]
592    fconfigure $f -translation cr
593    set x [list [gets $f line] $line [gets $f line] $line]
594    close $f
595    set x
596} [list 1 "\n" -1 ""]
597test io-6.18 {Tcl_GetsObj: cr mode: lone \r} {
598    set f [open $path(test1) w]
599    fconfigure $f -translation lf
600    puts -nonewline $f "\r"
601    close $f
602    set f [open $path(test1)]
603    fconfigure $f -translation cr
604    set x [list [gets $f line] $line [gets $f line] $line]
605    close $f
606    set x
607} {0 {} -1 {}}
608test io-6.19 {Tcl_GetsObj: cr mode: 1 char} {
609    set f [open $path(test1) w]
610    fconfigure $f -translation lf
611    puts -nonewline $f a
612    close $f
613    set f [open $path(test1)]
614    fconfigure $f -translation cr
615    set x [list [gets $f line] $line [gets $f line] $line]
616    close $f
617    set x
618} {1 a -1 {}}
619test io-6.20 {Tcl_GetsObj: cr mode: 1 char followed by EOL} {
620    set f [open $path(test1) w]
621    fconfigure $f -translation lf
622    puts -nonewline $f "a\r"
623    close $f
624    set f [open $path(test1)]
625    fconfigure $f -translation cr
626    set x [list [gets $f line] $line [gets $f line] $line]
627    close $f
628    set x
629} {1 a -1 {}}
630test io-6.21 {Tcl_GetsObj: cr mode: several chars} {
631    set f [open $path(test1) w]
632    fconfigure $f -translation lf
633    puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
634    close $f
635    set f [open $path(test1)]
636    fconfigure $f -translation cr
637    set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line [gets $f line] $line]
638    close $f
639    set x
640} [list 9 "abcd\nefgh" 4 "ijkl" 5 "\nmnop" -1 ""]
641test io-6.22 {Tcl_GetsObj: crlf mode: no chars} {
642    set f [open $path(test1) w]
643    close $f
644    set f [open $path(test1)]
645    fconfigure $f -translation crlf
646    set x [list [gets $f line] $line]
647    close $f
648    set x
649} {-1 {}}
650test io-6.23 {Tcl_GetsObj: crlf mode: lone \n} {
651    set f [open $path(test1) w]
652    fconfigure $f -translation lf
653    puts -nonewline $f "\n"
654    close $f
655    set f [open $path(test1)]
656    fconfigure $f -translation crlf
657    set x [list [gets $f line] $line [gets $f line] $line]
658    close $f
659    set x
660} [list 1 "\n" -1 ""]
661test io-6.24 {Tcl_GetsObj: crlf mode: lone \r} {
662    set f [open $path(test1) w]
663    fconfigure $f -translation lf
664    puts -nonewline $f "\r"
665    close $f
666    set f [open $path(test1)]
667    fconfigure $f -translation crlf
668    set x [list [gets $f line] $line [gets $f line] $line]
669    close $f
670    set x
671} [list 1 "\r" -1 ""]
672test io-6.25 {Tcl_GetsObj: crlf mode: \r\r} {
673    set f [open $path(test1) w]
674    fconfigure $f -translation lf
675    puts -nonewline $f "\r\r"
676    close $f
677    set f [open $path(test1)]
678    fconfigure $f -translation crlf
679    set x [list [gets $f line] $line [gets $f line] $line]
680    close $f
681    set x
682} [list 2 "\r\r" -1 ""]
683test io-6.26 {Tcl_GetsObj: crlf mode: \r\n} {
684    set f [open $path(test1) w]
685    fconfigure $f -translation lf
686    puts -nonewline $f "\r\n"
687    close $f
688    set f [open $path(test1)]
689    fconfigure $f -translation crlf
690    set x [list [gets $f line] $line [gets $f line] $line]
691    close $f
692    set x
693} [list 0 "" -1 ""]
694test io-6.27 {Tcl_GetsObj: crlf mode: 1 char} {
695    set f [open $path(test1) w]
696    fconfigure $f -translation lf
697    puts -nonewline $f a
698    close $f
699    set f [open $path(test1)]
700    fconfigure $f -translation crlf
701    set x [list [gets $f line] $line [gets $f line] $line]
702    close $f
703    set x
704} {1 a -1 {}}
705test io-6.28 {Tcl_GetsObj: crlf mode: 1 char followed by EOL} {
706    set f [open $path(test1) w]
707    fconfigure $f -translation lf
708    puts -nonewline $f "a\r\n"
709    close $f
710    set f [open $path(test1)]
711    fconfigure $f -translation crlf
712    set x [list [gets $f line] $line [gets $f line] $line]
713    close $f
714    set x
715} {1 a -1 {}}
716test io-6.29 {Tcl_GetsObj: crlf mode: several chars} {
717    set f [open $path(test1) w]
718    fconfigure $f -translation lf
719    puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
720    close $f
721    set f [open $path(test1)]
722    fconfigure $f -translation crlf
723    set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
724    close $f
725    set x
726} [list 14 "abcd\nefgh\rijkl" 4 "mnop" -1 ""]
727test io-6.30 {Tcl_GetsObj: crlf mode: buffer exhausted} {testchannel} {
728    # if (eol >= dstEnd)
729
730    set f [open $path(test1) w]
731    fconfigure $f -translation lf
732    puts -nonewline $f "123456789012345\r\nabcdefghijklmnoprstuvwxyz"
733    close $f
734    set f [open $path(test1)]
735    fconfigure $f -translation crlf -buffersize 16
736    set x [list [gets $f line] $line [testchannel inputbuffered $f]]
737    close $f
738    set x
739} [list 15 "123456789012345" 15]
740test io-6.31 {Tcl_GetsObj: crlf mode: buffer exhausted, blocked} {stdio testchannel openpipe fileevent} {
741    # (FilterInputBytes() != 0)
742
743    set f [open "|[list [interpreter] $path(cat)]" w+]
744    fconfigure $f -translation {crlf lf} -buffering none
745    puts -nonewline $f "bbbbbbbbbbbbbb\r\n123456789012345\r"
746    fconfigure $f -buffersize 16
747    set x [gets $f]
748    fconfigure $f -blocking 0
749    lappend x [gets $f line] $line [fblocked $f] [testchannel inputbuffered $f]
750    close $f
751    set x
752} [list "bbbbbbbbbbbbbb" -1 "" 1 16]
753test io-6.32 {Tcl_GetsObj: crlf mode: buffer exhausted, more data} {testchannel} {
754    # not (FilterInputBytes() != 0)
755
756    set f [open $path(test1) w]
757    fconfigure $f -translation lf
758    puts -nonewline $f "123456789012345\r\n123"
759    close $f
760    set f [open $path(test1)]
761    fconfigure $f -translation crlf -buffersize 16
762    set x [list [gets $f line] $line [tell $f] [testchannel inputbuffered $f]]
763    close $f
764    set x
765} [list 15 "123456789012345" 17 3]
766test io-6.33 {Tcl_GetsObj: crlf mode: buffer exhausted, at eof} {
767    # eol still equals dstEnd
768   
769    set f [open $path(test1) w]
770    fconfigure $f -translation lf
771    puts -nonewline $f "123456789012345\r"
772    close $f
773    set f [open $path(test1)]
774    fconfigure $f -translation crlf -buffersize 16
775    set x [list [gets $f line] $line [eof $f]]
776    close $f
777    set x
778} [list 16 "123456789012345\r" 1]
779test io-6.34 {Tcl_GetsObj: crlf mode: buffer exhausted, not followed by \n} {
780    # not (*eol == '\n')
781   
782    set f [open $path(test1) w]
783    fconfigure $f -translation lf
784    puts -nonewline $f "123456789012345\rabcd\r\nefg"
785    close $f
786    set f [open $path(test1)]
787    fconfigure $f -translation crlf -buffersize 16
788    set x [list [gets $f line] $line [tell $f]]
789    close $f
790    set x
791} [list 20 "123456789012345\rabcd" 22]
792test io-6.35 {Tcl_GetsObj: auto mode: no chars} {
793    set f [open $path(test1) w]
794    close $f
795    set f [open $path(test1)]
796    fconfigure $f -translation auto
797    set x [list [gets $f line] $line]
798    close $f
799    set x
800} {-1 {}}
801test io-6.36 {Tcl_GetsObj: auto mode: lone \n} {
802    set f [open $path(test1) w]
803    fconfigure $f -translation lf
804    puts -nonewline $f "\n"
805    close $f
806    set f [open $path(test1)]
807    fconfigure $f -translation auto
808    set x [list [gets $f line] $line [gets $f line] $line]
809    close $f
810    set x
811} [list 0 "" -1 ""]
812test io-6.37 {Tcl_GetsObj: auto mode: lone \r} {
813    set f [open $path(test1) w]
814    fconfigure $f -translation lf
815    puts -nonewline $f "\r"
816    close $f
817    set f [open $path(test1)]
818    fconfigure $f -translation auto
819    set x [list [gets $f line] $line [gets $f line] $line]
820    close $f
821    set x
822} [list 0 "" -1 ""]
823test io-6.38 {Tcl_GetsObj: auto mode: \r\r} {
824    set f [open $path(test1) w]
825    fconfigure $f -translation lf
826    puts -nonewline $f "\r\r"
827    close $f
828    set f [open $path(test1)]
829    fconfigure $f -translation auto
830    set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
831    close $f
832    set x
833} [list 0 "" 0 "" -1 ""]
834test io-6.39 {Tcl_GetsObj: auto mode: \r\n} {
835    set f [open $path(test1) w]
836    fconfigure $f -translation lf
837    puts -nonewline $f "\r\n"
838    close $f
839    set f [open $path(test1)]
840    fconfigure $f -translation auto
841    set x [list [gets $f line] $line [gets $f line] $line]
842    close $f
843    set x
844} [list 0 "" -1 ""]
845test io-6.40 {Tcl_GetsObj: auto mode: 1 char} {
846    set f [open $path(test1) w]
847    fconfigure $f -translation lf
848    puts -nonewline $f a
849    close $f
850    set f [open $path(test1)]
851    fconfigure $f -translation auto
852    set x [list [gets $f line] $line [gets $f line] $line]
853    close $f
854    set x
855} {1 a -1 {}}
856test io-6.41 {Tcl_GetsObj: auto mode: 1 char followed by EOL} {
857    set f [open $path(test1) w]
858    fconfigure $f -translation lf
859    puts -nonewline $f "a\r\n"
860    close $f
861    set f [open $path(test1)]
862    fconfigure $f -translation auto
863    set x [list [gets $f line] $line [gets $f line] $line]
864    close $f
865    set x
866} {1 a -1 {}}
867test io-6.42 {Tcl_GetsObj: auto mode: several chars} {
868    set f [open $path(test1) w]
869    fconfigure $f -translation lf
870    puts -nonewline $f "abcd\nefgh\rijkl\r\nmnop"
871    close $f
872    set f [open $path(test1)]
873    fconfigure $f -translation auto
874    set x [list [gets $f line] $line [gets $f line] $line]
875    lappend x [gets $f line] $line [gets $f line] $line [gets $f line] $line
876    close $f
877    set x
878} [list 4 "abcd" 4 "efgh" 4 "ijkl" 4 "mnop" -1 ""]
879test io-6.43 {Tcl_GetsObj: input saw cr} {stdio testchannel openpipe fileevent} {
880    # if (chanPtr->flags & INPUT_SAW_CR)
881
882    set f [open "|[list [interpreter] $path(cat)]" w+]
883    fconfigure $f -translation {auto lf} -buffering none
884    puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
885    fconfigure $f -buffersize 16
886    set x [list [gets $f]]
887    fconfigure $f -blocking 0
888    lappend x [gets $f line] $line [testchannel queuedcr $f] 
889    fconfigure $f -blocking 1
890    puts -nonewline $f "\nabcd\refg\x1a"
891    lappend x [gets $f line] $line [testchannel queuedcr $f]
892    lappend x [gets $f line] $line
893    close $f
894    set x
895} [list "bbbbbbbbbbbbbbb" 15 "123456789abcdef" 1 4 "abcd" 0 3 "efg"]
896test io-6.44 {Tcl_GetsObj: input saw cr, not followed by cr} {stdio testchannel openpipe fileevent} {
897    # not (*eol == '\n')
898
899    set f [open "|[list [interpreter] $path(cat)]" w+]
900    fconfigure $f -translation {auto lf} -buffering none
901    puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
902    fconfigure $f -buffersize 16
903    set x [list [gets $f]]
904    fconfigure $f -blocking 0
905    lappend x [gets $f line] $line [testchannel queuedcr $f] 
906    fconfigure $f -blocking 1
907    puts -nonewline $f "abcd\refg\x1a"
908    lappend x [gets $f line] $line [testchannel queuedcr $f]
909    lappend x [gets $f line] $line
910    close $f
911    set x
912} [list "bbbbbbbbbbbbbbb" 15 "123456789abcdef" 1 4 "abcd" 0 3 "efg"]
913test io-6.45 {Tcl_GetsObj: input saw cr, skip right number of bytes} {stdio testchannel openpipe fileevent} {
914    # Tcl_ExternalToUtf()
915
916    set f [open "|[list [interpreter] $path(cat)]" w+]
917    fconfigure $f -translation {auto lf} -buffering none
918    fconfigure $f -encoding unicode
919    puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
920    fconfigure $f -buffersize 16
921    gets $f
922    fconfigure $f -blocking 0
923    set x [list [gets $f line] $line [testchannel queuedcr $f]]
924    fconfigure $f -blocking 1
925    puts -nonewline $f "\nabcd\refg"
926    lappend x [gets $f line] $line [testchannel queuedcr $f]
927    close $f
928    set x
929} [list 15 "123456789abcdef" 1 4 "abcd" 0]
930test io-6.46 {Tcl_GetsObj: input saw cr, followed by just \n should give eof} {stdio testchannel openpipe fileevent} {
931    # memmove()
932
933    set f [open "|[list [interpreter] $path(cat)]" w+]
934    fconfigure $f -translation {auto lf} -buffering none
935    puts -nonewline $f "bbbbbbbbbbbbbbb\n123456789abcdef\r"
936    fconfigure $f -buffersize 16
937    gets $f
938    fconfigure $f -blocking 0
939    set x [list [gets $f line] $line [testchannel queuedcr $f]]
940    fconfigure $f -blocking 1
941    puts -nonewline $f "\n\x1a"
942    lappend x [gets $f line] $line [testchannel queuedcr $f]
943    close $f
944    set x
945} [list 15 "123456789abcdef" 1 -1 "" 0]
946test io-6.47 {Tcl_GetsObj: auto mode: \r at end of buffer, peek for \n} {testchannel} {
947    # (eol == dstEnd)
948
949    set f [open $path(test1) w]
950    fconfigure $f -translation lf
951    puts -nonewline $f "123456789012345\r\nabcdefghijklmnopq"
952    close $f
953    set f [open $path(test1)]
954    fconfigure $f -translation auto -buffersize 16
955    set x [list [gets $f] [testchannel inputbuffered $f]]
956    close $f
957    set x
958} [list "123456789012345" 15]   
959test io-6.48 {Tcl_GetsObj: auto mode: \r at end of buffer, no more avail} {testchannel} {
960    # PeekAhead() did not get any, so (eol >= dstEnd)
961   
962    set f [open $path(test1) w]
963    fconfigure $f -translation lf
964    puts -nonewline $f "123456789012345\r"
965    close $f
966    set f [open $path(test1)]
967    fconfigure $f -translation auto -buffersize 16
968    set x [list [gets $f] [testchannel queuedcr $f]]
969    close $f
970    set x
971} [list "123456789012345" 1]
972test io-6.49 {Tcl_GetsObj: auto mode: \r followed by \n} {testchannel} {
973    # if (*eol == '\n') {skip++}
974   
975    set f [open $path(test1) w]
976    fconfigure $f -translation lf
977    puts -nonewline $f "123456\r\n78901"
978    close $f
979    set f [open $path(test1)]
980    set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
981    close $f
982    set x
983} [list "123456" 0 8 "78901"]
984test io-6.50 {Tcl_GetsObj: auto mode: \r not followed by \n} {testchannel} {
985    # not (*eol == '\n')
986   
987    set f [open $path(test1) w]
988    fconfigure $f -translation lf
989    puts -nonewline $f "123456\r78901"
990    close $f
991    set f [open $path(test1)]
992    set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
993    close $f
994    set x
995} [list "123456" 0 7 "78901"]
996test io-6.51 {Tcl_GetsObj: auto mode: \n} {
997    # else if (*eol == '\n') {goto gotoeol;}
998   
999    set f [open $path(test1) w]
1000    fconfigure $f -translation lf
1001    puts -nonewline $f "123456\n78901"
1002    close $f
1003    set f [open $path(test1)]
1004    set x [list [gets $f] [tell $f] [gets $f]]
1005    close $f
1006    set x
1007} [list "123456" 7 "78901"]
1008test io-6.52 {Tcl_GetsObj: saw EOF character} {testchannel} {
1009    # if (eof != NULL)
1010
1011    set f [open $path(test1) w]
1012    fconfigure $f -translation lf
1013    puts -nonewline $f "123456\x1ak9012345\r"
1014    close $f
1015    set f [open $path(test1)]
1016    fconfigure $f -eofchar \x1a
1017    set x [list [gets $f] [testchannel queuedcr $f] [tell $f] [gets $f]]
1018    close $f
1019    set x
1020} [list "123456" 0 6 ""]
1021test io-6.53 {Tcl_GetsObj: device EOF} {
1022    # didn't produce any bytes
1023
1024    set f [open $path(test1) w]
1025    close $f
1026    set f [open $path(test1)]
1027    set x [list [gets $f line] $line [eof $f]]
1028    close $f
1029    set x
1030} {-1 {} 1}
1031test io-6.54 {Tcl_GetsObj: device EOF} {
1032    # got some bytes before EOF.
1033
1034    set f [open $path(test1) w]
1035    puts -nonewline $f abc
1036    close $f
1037    set f [open $path(test1)]
1038    set x [list [gets $f line] $line [eof $f]]
1039    close $f
1040    set x
1041} {3 abc 1}
1042test io-6.55 {Tcl_GetsObj: overconverted} {
1043    # Tcl_ExternalToUtf(), make sure state updated
1044
1045    set f [open $path(test1) w]
1046    fconfigure $f -encoding iso2022-jp
1047    puts $f "there\u4e00ok\n\u4e01more bytes\nhere"
1048    close $f
1049    set f [open $path(test1)]
1050    fconfigure $f -encoding iso2022-jp
1051    set x [list [gets $f line] $line [gets $f line] $line [gets $f line] $line]
1052    close $f
1053    set x
1054} [list 8 "there\u4e00ok" 11 "\u4e01more bytes" 4 "here"]
1055test io-6.56 {Tcl_GetsObj: incomplete lines should disable file events} {stdio openpipe fileevent} {
1056    update
1057    set f [open "|[list [interpreter] $path(cat)]" w+]
1058    fconfigure $f -buffering none
1059    puts -nonewline $f "foobar"
1060    fconfigure $f -blocking 0
1061    variable x {}
1062    after 500 [namespace code { lappend x timeout }]
1063    fileevent $f readable [namespace code { lappend x [gets $f] }]
1064    vwait [namespace which -variable x]
1065    vwait [namespace which -variable x]
1066    fconfigure $f -blocking 1
1067    puts -nonewline $f "baz\n"
1068    after 500 [namespace code { lappend x timeout }]
1069    fconfigure $f -blocking 0
1070    vwait [namespace which -variable x]
1071    vwait [namespace which -variable x]
1072    close $f
1073    set x
1074} {{} timeout foobarbaz timeout}
1075
1076test io-7.1 {FilterInputBytes: split up character at end of buffer} {
1077    # (result == TCL_CONVERT_MULTIBYTE)
1078
1079    set f [open $path(test1) w]
1080    fconfigure $f -encoding shiftjis
1081    puts $f "1234567890123\uff10\uff11\uff12\uff13\uff14\nend"
1082    close $f
1083    set f [open $path(test1)]
1084    fconfigure $f -encoding shiftjis -buffersize 16
1085    set x [gets $f]
1086    close $f
1087    set x
1088} "1234567890123\uff10\uff11\uff12\uff13\uff14"
1089test io-7.2 {FilterInputBytes: split up character in middle of buffer} {
1090    # (bufPtr->nextAdded < bufPtr->bufLength)
1091   
1092    set f [open $path(test1) w]
1093    fconfigure $f -encoding binary
1094    puts -nonewline $f "1234567890\n123\x82\x4f\x82\x50\x82"
1095    close $f
1096    set f [open $path(test1)]
1097    fconfigure $f -encoding shiftjis
1098    set x [list [gets $f line] $line [eof $f]]
1099    close $f
1100    set x
1101} [list 10 "1234567890" 0]
1102test io-7.3 {FilterInputBytes: split up character at EOF} {testchannel} {
1103    set f [open $path(test1) w]
1104    fconfigure $f -encoding binary
1105    puts -nonewline $f "1234567890123\x82\x4f\x82\x50\x82"
1106    close $f
1107    set f [open $path(test1)]
1108    fconfigure $f -encoding shiftjis
1109    set x [list [gets $f line] $line]
1110    lappend x [tell $f] [testchannel inputbuffered $f] [eof $f]
1111    lappend x [gets $f line] $line
1112    close $f
1113    set x
1114} [list 15 "1234567890123\uff10\uff11" 18 0 1 -1 ""]
1115test io-7.4 {FilterInputBytes: recover from split up character} {stdio openpipe fileevent} {
1116    set f [open "|[list [interpreter] $path(cat)]" w+]
1117    fconfigure $f -encoding binary -buffering none
1118    puts -nonewline $f "1234567890123\x82\x4f\x82\x50\x82"
1119    fconfigure $f -encoding shiftjis -blocking 0
1120    fileevent $f read [namespace code "ready $f"]
1121    variable x {}
1122    proc ready {f} {
1123        variable x
1124        lappend x [gets $f line] $line [fblocked $f]
1125    }
1126    vwait [namespace which -variable x]
1127    fconfigure $f -encoding binary -blocking 1
1128    puts $f "\x51\x82\x52"
1129    fconfigure $f -encoding shiftjis
1130    vwait [namespace which -variable x]
1131    close $f
1132    set x
1133} [list -1 "" 1 17 "1234567890123\uff10\uff11\uff12\uff13" 0]
1134
1135test io-8.1 {PeekAhead: only go to device if no more cached data} {testchannel} {
1136    # (bufPtr->nextPtr == NULL)
1137
1138    set f [open $path(test1) w]
1139    fconfigure $f -encoding ascii -translation lf
1140    puts -nonewline $f "123456789012345\r\n2345678"
1141    close $f
1142    set f [open $path(test1)]
1143    fconfigure $f -encoding ascii -translation auto -buffersize 16
1144    # here
1145    gets $f
1146    set x [testchannel inputbuffered $f]
1147    close $f
1148    set x
1149} "7"
1150test io-8.2 {PeekAhead: only go to device if no more cached data} {stdio testchannel openpipe fileevent} {
1151    # not (bufPtr->nextPtr == NULL)
1152
1153    set f [open "|[list [interpreter] $path(cat)]" w+]
1154    fconfigure $f -translation lf -encoding ascii -buffering none
1155    puts -nonewline $f "123456789012345\r\nbcdefghijklmnopqrstuvwxyz"
1156    variable x {}
1157    fileevent $f read [namespace code "ready $f"]
1158    proc ready {f} {
1159        variable x
1160        lappend x [gets $f line] $line [testchannel inputbuffered $f]
1161    }
1162    fconfigure $f -encoding unicode -buffersize 16 -blocking 0
1163    vwait [namespace which -variable x]
1164    fconfigure $f -translation auto -encoding ascii -blocking 1
1165    # here
1166    vwait [namespace which -variable x]
1167    close $f
1168    set x
1169} [list -1 "" 42 15 "123456789012345" 25]
1170test io-8.3 {PeekAhead: no cached data available} {stdio testchannel openpipe fileevent} {
1171    # (bytesLeft == 0)
1172
1173    set f [open "|[list [interpreter] $path(cat)]" w+]
1174    fconfigure $f -translation {auto binary}
1175    puts -nonewline $f "abcdefghijklmno\r"
1176    flush $f
1177    set x [list [gets $f line] $line [testchannel queuedcr $f]]
1178    close $f
1179    set x
1180} [list 15 "abcdefghijklmno" 1]
1181set a "123456789012345678901234567890"
1182append a "123456789012345678901234567890"
1183append a "1234567890123456789012345678901"
1184test io-8.4 {PeekAhead: cached data available in this buffer} {
1185    # not (bytesLeft == 0)
1186
1187    set f [open $path(test1) w+]
1188    fconfigure $f -translation binary
1189    puts $f "${a}\r\nabcdef"
1190    close $f
1191    set f [open $path(test1)]
1192    fconfigure $f -encoding binary -translation auto
1193
1194    # "${a}\r" was converted in one operation (because ENCODING_LINESIZE
1195    # is 30).  To check if "\n" follows, calls PeekAhead and determines
1196    # that cached data is available in buffer w/o having to call driver.
1197
1198    set x [gets $f]
1199    close $f
1200    set x   
1201} $a
1202unset a
1203test io-8.5 {PeekAhead: don't peek if last read was short} {stdio testchannel openpipe fileevent} {
1204    # (bufPtr->nextAdded < bufPtr->length)
1205
1206    set f [open "|[list [interpreter] $path(cat)]" w+]
1207    fconfigure $f -translation {auto binary}
1208    puts -nonewline $f "abcdefghijklmno\r"
1209    flush $f
1210    # here
1211    set x [list [gets $f line] $line [testchannel queuedcr $f]]
1212    close $f
1213    set x
1214} {15 abcdefghijklmno 1}
1215test io-8.6 {PeekAhead: change to non-blocking mode} {stdio testchannel openpipe fileevent} {
1216    # ((chanPtr->flags & CHANNEL_NONBLOCKING) == 0)
1217
1218    set f [open "|[list [interpreter] $path(cat)]" w+]
1219    fconfigure $f -translation {auto binary} -buffersize 16
1220    puts -nonewline $f "abcdefghijklmno\r"
1221    flush $f
1222    # here
1223    set x [list [gets $f line] $line [testchannel queuedcr $f]]
1224    close $f
1225    set x
1226} {15 abcdefghijklmno 1}
1227test io-8.7 {PeekAhead: cleanup} {stdio testchannel openpipe fileevent} {
1228    # Make sure bytes are removed from buffer.
1229
1230    set f [open "|[list [interpreter] $path(cat)]" w+]
1231    fconfigure $f -translation {auto binary} -buffering none
1232    puts -nonewline $f "abcdefghijklmno\r"
1233    # here
1234    set x [list [gets $f line] $line [testchannel queuedcr $f]]
1235    puts -nonewline $f "\x1a"
1236    lappend x [gets $f line] $line
1237    close $f
1238    set x
1239} {15 abcdefghijklmno 1 -1 {}}
1240
1241test io-9.1 {CommonGetsCleanup} emptyTest {
1242} {}
1243
1244test io-10.1 {Tcl_ReadChars: CheckChannelErrors} emptyTest {
1245    # no test, need to cause an async error.
1246} {}
1247test io-10.2 {Tcl_ReadChars: loop until enough copied} {
1248    # one time
1249    # for (copied = 0; (unsigned) toRead > 0; )
1250
1251    set f [open $path(test1) w]
1252    puts $f abcdefghijklmnop
1253    close $f
1254
1255    set f [open $path(test1)]
1256    set x [read $f 5]
1257    close $f
1258    set x
1259} {abcde}
1260test io-10.3 {Tcl_ReadChars: loop until enough copied} {
1261    # multiple times
1262    # for (copied = 0; (unsigned) toRead > 0; )
1263
1264    set f [open $path(test1) w]
1265    puts $f abcdefghijklmnopqrstuvwxyz
1266    close $f
1267
1268    set f [open $path(test1)]
1269    fconfigure $f -buffersize 16
1270    # here
1271    set x [read $f 19]
1272    close $f
1273    set x
1274} {abcdefghijklmnopqrs}
1275test io-10.4 {Tcl_ReadChars: no more in channel buffer} {
1276    # (copiedNow < 0)
1277
1278    set f [open $path(test1) w]
1279    puts -nonewline $f abcdefghijkl
1280    close $f
1281
1282    set f [open $path(test1)]
1283    # here
1284    set x [read $f 1000]
1285    close $f
1286    set x
1287} {abcdefghijkl}
1288test io-10.5 {Tcl_ReadChars: stop on EOF} {
1289    # (chanPtr->flags & CHANNEL_EOF)
1290
1291    set f [open $path(test1) w]
1292    puts -nonewline $f abcdefghijkl
1293    close $f
1294
1295    set f [open $path(test1)]
1296    # here
1297    set x [read $f 1000]
1298    close $f
1299    set x
1300} {abcdefghijkl}
1301
1302test io-11.1 {ReadBytes: want to read a lot} {
1303    # ((unsigned) toRead > (unsigned) srcLen)
1304
1305    set f [open $path(test1) w]
1306    puts -nonewline $f abcdefghijkl
1307    close $f
1308    set f [open $path(test1)]
1309    fconfigure $f -encoding binary
1310    # here
1311    set x [read $f 1000]
1312    close $f
1313    set x
1314} {abcdefghijkl}
1315test io-11.2 {ReadBytes: want to read all} {
1316    # ((unsigned) toRead > (unsigned) srcLen)
1317
1318    set f [open $path(test1) w]
1319    puts -nonewline $f abcdefghijkl
1320    close $f
1321    set f [open $path(test1)]
1322    fconfigure $f -encoding binary
1323    # here
1324    set x [read $f]
1325    close $f
1326    set x
1327} {abcdefghijkl}
1328test io-11.3 {ReadBytes: allocate more space} {
1329    # (toRead > length - offset - 1)
1330
1331    set f [open $path(test1) w]
1332    puts -nonewline $f abcdefghijklmnopqrstuvwxyz
1333    close $f
1334    set f [open $path(test1)]
1335    fconfigure $f -buffersize 16 -encoding binary
1336    # here
1337    set x [read $f]
1338    close $f
1339    set x
1340} {abcdefghijklmnopqrstuvwxyz}
1341test io-11.4 {ReadBytes: EOF char found} {
1342    # (TranslateInputEOL() != 0)
1343
1344    set f [open $path(test1) w]
1345    puts $f abcdefghijklmnopqrstuvwxyz
1346    close $f
1347    set f [open $path(test1)]
1348    fconfigure $f -eofchar m -encoding binary
1349    # here
1350    set x [list [read $f] [eof $f] [read $f] [eof $f]]
1351    close $f
1352    set x
1353} [list "abcdefghijkl" 1 "" 1]
1354
1355test io-12.1 {ReadChars: want to read a lot} {
1356    # ((unsigned) toRead > (unsigned) srcLen)
1357
1358    set f [open $path(test1) w]
1359    puts -nonewline $f abcdefghijkl
1360    close $f
1361    set f [open $path(test1)]
1362    # here
1363    set x [read $f 1000]
1364    close $f
1365    set x
1366} {abcdefghijkl}
1367test io-12.2 {ReadChars: want to read all} {
1368    # ((unsigned) toRead > (unsigned) srcLen)
1369
1370    set f [open $path(test1) w]
1371    puts -nonewline $f abcdefghijkl
1372    close $f
1373    set f [open $path(test1)]
1374    # here
1375    set x [read $f]
1376    close $f
1377    set x
1378} {abcdefghijkl}
1379test io-12.3 {ReadChars: allocate more space} {
1380    # (toRead > length - offset - 1)
1381
1382    set f [open $path(test1) w]
1383    puts -nonewline $f abcdefghijklmnopqrstuvwxyz
1384    close $f
1385    set f [open $path(test1)]
1386    fconfigure $f -buffersize 16
1387    # here
1388    set x [read $f]
1389    close $f
1390    set x
1391} {abcdefghijklmnopqrstuvwxyz}
1392test io-12.4 {ReadChars: split-up char} {stdio testchannel openpipe fileevent} {
1393    # (srcRead == 0)
1394
1395    set f [open "|[list [interpreter] $path(cat)]" w+]
1396    fconfigure $f -encoding binary -buffering none -buffersize 16
1397    puts -nonewline $f "123456789012345\x96"
1398    fconfigure $f -encoding shiftjis -blocking 0
1399
1400    fileevent $f read [namespace code "ready $f"]
1401    proc ready {f} {
1402        variable x
1403        lappend x [read $f] [testchannel inputbuffered $f]
1404    }
1405    variable x {}
1406
1407    fconfigure $f -encoding shiftjis
1408    vwait [namespace which -variable x]
1409    fconfigure $f -encoding binary -blocking 1
1410    puts -nonewline $f "\x7b"
1411    after 500                   ;# Give the cat process time to catch up
1412    fconfigure $f -encoding shiftjis -blocking 0
1413    vwait [namespace which -variable x]
1414    close $f
1415    set x
1416} [list "123456789012345" 1 "\u672c" 0]
1417test io-12.5 {ReadChars: fileevents on partial characters} {stdio openpipe fileevent} {
1418    set path(test1) [makeFile {
1419        fconfigure stdout -encoding binary -buffering none
1420        gets stdin; puts -nonewline "\xe7"
1421        gets stdin; puts -nonewline "\x89"
1422        gets stdin; puts -nonewline "\xa6"
1423    } test1]
1424    set f [open "|[list [interpreter] $path(test1)]" r+]
1425    fileevent $f readable [namespace code {
1426        lappend x [read $f]
1427        if {[eof $f]} {
1428            lappend x eof
1429        }
1430    }]
1431    puts $f "go1"
1432    flush $f
1433    fconfigure $f -blocking 0 -encoding utf-8
1434    variable x {}
1435    vwait [namespace which -variable x]
1436    after 500 [namespace code { lappend x timeout }]
1437    vwait [namespace which -variable x]
1438    puts $f "go2"
1439    flush $f
1440    vwait [namespace which -variable x]
1441    after 500 [namespace code { lappend x timeout }]
1442    vwait [namespace which -variable x]
1443    puts $f "go3"
1444    flush $f
1445    vwait [namespace which -variable x]
1446    vwait [namespace which -variable x]
1447    lappend x [catch {close $f} msg] $msg
1448    set x
1449} "{} timeout {} timeout \u7266 {} eof 0 {}"
1450
1451test io-13.1 {TranslateInputEOL: cr mode} {} {
1452    set f [open $path(test1) w]
1453    fconfigure $f -translation lf
1454    puts -nonewline $f "abcd\rdef\r"
1455    close $f
1456    set f [open $path(test1)]
1457    fconfigure $f -translation cr
1458    set x [read $f]
1459    close $f
1460    set x
1461} "abcd\ndef\n"
1462test io-13.2 {TranslateInputEOL: crlf mode} {
1463    set f [open $path(test1) w]
1464    fconfigure $f -translation lf
1465    puts -nonewline $f "abcd\r\ndef\r\n"
1466    close $f
1467    set f [open $path(test1)]
1468    fconfigure $f -translation crlf
1469    set x [read $f]
1470    close $f
1471    set x
1472} "abcd\ndef\n"
1473test io-13.3 {TranslateInputEOL: crlf mode: naked cr} {
1474    # (src >= srcMax)
1475
1476    set f [open $path(test1) w]
1477    fconfigure $f -translation lf
1478    puts -nonewline $f "abcd\r\ndef\r"
1479    close $f
1480    set f [open $path(test1)]
1481    fconfigure $f -translation crlf
1482    set x [read $f]
1483    close $f
1484    set x
1485} "abcd\ndef\r"
1486test io-13.4 {TranslateInputEOL: crlf mode: cr followed by not \n} {
1487    # (src >= srcMax)
1488
1489    set f [open $path(test1) w]
1490    fconfigure $f -translation lf
1491    puts -nonewline $f "abcd\r\ndef\rfgh"
1492    close $f
1493    set f [open $path(test1)]
1494    fconfigure $f -translation crlf
1495    set x [read $f]
1496    close $f
1497    set x
1498} "abcd\ndef\rfgh"
1499test io-13.5 {TranslateInputEOL: crlf mode: naked lf} {
1500    # (src >= srcMax)
1501
1502    set f [open $path(test1) w]
1503    fconfigure $f -translation lf
1504    puts -nonewline $f "abcd\r\ndef\nfgh"
1505    close $f
1506    set f [open $path(test1)]
1507    fconfigure $f -translation crlf
1508    set x [read $f]
1509    close $f
1510    set x
1511} "abcd\ndef\nfgh"
1512test io-13.6 {TranslateInputEOL: auto mode: saw cr in last segment} {stdio testchannel openpipe fileevent} {
1513    # (chanPtr->flags & INPUT_SAW_CR)
1514    # This test may fail on slower machines.
1515
1516    set f [open "|[list [interpreter] $path(cat)]" w+]
1517    fconfigure $f -blocking 0 -buffering none -translation {auto lf}
1518
1519    fileevent $f read [namespace code "ready $f"]
1520    proc ready {f} {
1521        variable x
1522        lappend x [read $f] [testchannel queuedcr $f]
1523    }
1524    variable x {}
1525    variable y {}
1526
1527    puts -nonewline $f "abcdefghj\r"
1528    after 500 [namespace code {set y ok}]
1529    vwait [namespace which -variable y]
1530
1531    puts -nonewline $f "\n01234"
1532    after 500 [namespace code {set y ok}]
1533    vwait [namespace which -variable y]
1534
1535    close $f
1536    set x
1537} [list "abcdefghj\n" 1 "01234" 0]
1538test io-13.7 {TranslateInputEOL: auto mode: naked \r} {testchannel openpipe} {
1539    # (src >= srcMax)
1540
1541    set f [open $path(test1) w]
1542    fconfigure $f -translation lf
1543    puts -nonewline $f "abcd\r"
1544    close $f
1545    set f [open $path(test1)]
1546    fconfigure $f -translation auto
1547    set x [list [read $f] [testchannel queuedcr $f]]
1548    close $f
1549    set x
1550} [list "abcd\n" 1]
1551test io-13.8 {TranslateInputEOL: auto mode: \r\n} {
1552    # (*src == '\n')
1553
1554    set f [open $path(test1) w]
1555    fconfigure $f -translation lf
1556    puts -nonewline $f "abcd\r\ndef"
1557    close $f
1558    set f [open $path(test1)]
1559    fconfigure $f -translation auto
1560    set x [read $f]
1561    close $f
1562    set x
1563} "abcd\ndef"
1564test io-13.9 {TranslateInputEOL: auto mode: \r followed by not \n} {
1565    set f [open $path(test1) w]
1566    fconfigure $f -translation lf
1567    puts -nonewline $f "abcd\rdef"
1568    close $f
1569    set f [open $path(test1)]
1570    fconfigure $f -translation auto
1571    set x [read $f]
1572    close $f
1573    set x
1574} "abcd\ndef"
1575test io-13.10 {TranslateInputEOL: auto mode: \n} {
1576    # not (*src == '\r')
1577
1578    set f [open $path(test1) w]
1579    fconfigure $f -translation lf
1580    puts -nonewline $f "abcd\ndef"
1581    close $f
1582    set f [open $path(test1)]
1583    fconfigure $f -translation auto
1584    set x [read $f]
1585    close $f
1586    set x
1587} "abcd\ndef"
1588test io-13.11 {TranslateInputEOL: EOF char} {
1589    # (*chanPtr->inEofChar != '\0')
1590
1591    set f [open $path(test1) w]
1592    fconfigure $f -translation lf
1593    puts -nonewline $f "abcd\ndefgh"
1594    close $f
1595    set f [open $path(test1)]
1596    fconfigure $f -translation auto -eofchar e
1597    set x [read $f]
1598    close $f
1599    set x
1600} "abcd\nd"
1601test io-13.12 {TranslateInputEOL: find EOF char in src} {
1602    # (*chanPtr->inEofChar != '\0')
1603
1604    set f [open $path(test1) w]
1605    fconfigure $f -translation lf
1606    puts -nonewline $f "\r\n\r\n\r\nab\r\n\r\ndef\r\n\r\n\r\n"
1607    close $f
1608    set f [open $path(test1)]
1609    fconfigure $f -translation auto -eofchar e
1610    set x [read $f]
1611    close $f
1612    set x
1613} "\n\n\nab\n\nd"
1614
1615# Test standard handle management. The functions tested are
1616# Tcl_SetStdChannel and Tcl_GetStdChannel. Incidentally we are
1617# also testing channel table management.
1618
1619if {[info commands testchannel] != ""} {
1620    set consoleFileNames [lsort [testchannel open]]
1621} else {
1622    # just to avoid an error
1623    set consoleFileNames [list]
1624}
1625
1626test io-14.1 {Tcl_SetStdChannel and Tcl_GetStdChannel} {testchannel} {
1627    set l ""
1628    lappend l [fconfigure stdin -buffering]
1629    lappend l [fconfigure stdout -buffering]
1630    lappend l [fconfigure stderr -buffering]
1631    lappend l [lsort [testchannel open]]
1632    set l
1633} [list line line none $consoleFileNames]
1634test io-14.2 {Tcl_SetStdChannel and Tcl_GetStdChannel} {
1635    interp create x
1636    set l ""
1637    lappend l [x eval {fconfigure stdin -buffering}]
1638    lappend l [x eval {fconfigure stdout -buffering}]
1639    lappend l [x eval {fconfigure stderr -buffering}]
1640    interp delete x
1641    set l
1642} {line line none}
1643set path(test3) [makeFile {} test3]
1644test io-14.3 {Tcl_SetStdChannel & Tcl_GetStdChannel} {exec openpipe} {
1645    set f [open $path(test1) w]
1646    puts -nonewline $f {
1647        close stdin
1648        close stdout
1649        close stderr
1650        set f  [}
1651    puts $f [list open $path(test1) r]]
1652    puts $f "set f2 \[[list open $path(test2) w]]"
1653    puts $f "set f3 \[[list open $path(test3) w]]"
1654    puts $f {   puts stdout [gets stdin]
1655        puts stdout out
1656        puts stderr err
1657        close $f
1658        close $f2
1659        close $f3
1660    }
1661    close $f
1662    set result [exec [interpreter] $path(test1)]
1663    set f  [open $path(test2) r]
1664    set f2 [open $path(test3) r]
1665    lappend result [read $f] [read $f2]
1666    close $f
1667    close $f2
1668    set result
1669} {{
1670out
1671} {err
1672}}
1673# This test relies on the fact that the smallest available fd is used first.
1674test io-14.4 {Tcl_SetStdChannel & Tcl_GetStdChannel} {exec unix} {
1675    set f [open $path(test1) w]
1676    puts -nonewline $f { close stdin
1677        close stdout
1678        close stderr
1679        set f  [}
1680    puts $f [list open $path(test1) r]]
1681    puts $f "set f2 \[[list open $path(test2) w]]"
1682    puts $f "set f3 \[[list open $path(test3) w]]"
1683    puts $f {   puts stdout [gets stdin]
1684        puts stdout $f2
1685        puts stderr $f3
1686        close $f
1687        close $f2
1688        close $f3
1689    }
1690    close $f
1691    set result [exec [interpreter] $path(test1)]
1692    set f  [open $path(test2) r]
1693    set f2 [open $path(test3) r]
1694    lappend result [read $f] [read $f2]
1695    close $f
1696    close $f2
1697    set result
1698} {{ close stdin
1699file1
1700} {file2
1701}}
1702catch {interp delete z}
1703test io-14.5 {Tcl_GetChannel: stdio name translation} {
1704    interp create z
1705    eof stdin
1706    catch {z eval flush stdin} msg1
1707    catch {z eval close stdin} msg2
1708    catch {z eval flush stdin} msg3
1709    set result [list $msg1 $msg2 $msg3]
1710    interp delete z
1711    set result
1712} {{channel "stdin" wasn't opened for writing} {} {can not find channel named "stdin"}}
1713test io-14.6 {Tcl_GetChannel: stdio name translation} {
1714    interp create z
1715    eof stdout
1716    catch {z eval flush stdout} msg1
1717    catch {z eval close stdout} msg2
1718    catch {z eval flush stdout} msg3
1719    set result [list $msg1 $msg2 $msg3]
1720    interp delete z
1721    set result
1722} {{} {} {can not find channel named "stdout"}}
1723test io-14.7 {Tcl_GetChannel: stdio name translation} {
1724    interp create z
1725    eof stderr
1726    catch {z eval flush stderr} msg1
1727    catch {z eval close stderr} msg2
1728    catch {z eval flush stderr} msg3
1729    set result [list $msg1 $msg2 $msg3]
1730    interp delete z
1731    set result
1732} {{} {} {can not find channel named "stderr"}}
1733set path(script) [makeFile {} script]
1734test io-14.8 {reuse of stdio special channels} {stdio openpipe} {
1735    file delete $path(script)
1736    file delete $path(test1)
1737    set f [open $path(script) w]
1738    puts -nonewline $f {
1739        close stderr
1740        set f [}
1741    puts $f [list open $path(test1) w]]
1742    puts -nonewline $f {
1743        puts stderr hello
1744        close $f
1745        set f [}
1746    puts $f [list open $path(test1) r]]
1747    puts $f {
1748        puts [gets $f]
1749    }
1750    close $f
1751    set f [open "|[list [interpreter] $path(script)]" r]
1752    set c [gets $f]
1753    close $f
1754    set c
1755} hello
1756test io-14.9 {reuse of stdio special channels} {stdio openpipe fileevent} {
1757    file delete $path(script)
1758    file delete $path(test1)
1759    set f [open $path(script) w]
1760    puts $f {
1761        array set path [lindex $argv 0]
1762        set f [open $path(test1) w]
1763        puts $f hello
1764        close $f
1765        close stderr
1766        set f [open "|[list [info nameofexecutable] $path(cat) $path(test1)]" r]
1767        puts [gets $f]
1768    }
1769    close $f
1770    set f [open "|[list [interpreter] $path(script) [array get path]]" r]
1771    set c [gets $f]
1772    close $f
1773    # Added delay to give Windows time to stop the spawned process and clean
1774    # up its grip on the file test1. Added delete as proper test cleanup.
1775    # The failing tests were 18.1 and 18.2 as first re-users of file "test1".
1776    after 10000
1777    file delete $path(script)
1778    file delete $path(test1)
1779    set c
1780} hello
1781
1782test io-15.1 {Tcl_CreateCloseHandler} emptyTest {
1783} {}
1784
1785test io-16.1 {Tcl_DeleteCloseHandler} emptyTest {
1786} {}
1787
1788# Test channel table management. The functions tested are
1789# GetChannelTable, DeleteChannelTable, Tcl_RegisterChannel,
1790# Tcl_UnregisterChannel, Tcl_GetChannel and Tcl_CreateChannel.
1791#
1792# These functions use "eof stdin" to ensure that the standard
1793# channels are added to the channel table of the interpreter.
1794
1795test io-17.1 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1796    set l1 [testchannel refcount stdin]
1797    eof stdin
1798    interp create x
1799    set l ""
1800    lappend l [expr [testchannel refcount stdin] - $l1]
1801    x eval {eof stdin}
1802    lappend l [expr [testchannel refcount stdin] - $l1]
1803    interp delete x
1804    lappend l [expr [testchannel refcount stdin] - $l1]
1805    set l
1806} {0 1 0}
1807test io-17.2 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1808    set l1 [testchannel refcount stdout]
1809    eof stdin
1810    interp create x
1811    set l ""
1812    lappend l [expr [testchannel refcount stdout] - $l1]
1813    x eval {eof stdout}
1814    lappend l [expr [testchannel refcount stdout] - $l1]
1815    interp delete x
1816    lappend l [expr [testchannel refcount stdout] - $l1]
1817    set l
1818} {0 1 0}
1819test io-17.3 {GetChannelTable, DeleteChannelTable on std handles} {testchannel} {
1820    set l1 [testchannel refcount stderr]
1821    eof stdin
1822    interp create x
1823    set l ""
1824    lappend l [expr [testchannel refcount stderr] - $l1]
1825    x eval {eof stderr}
1826    lappend l [expr [testchannel refcount stderr] - $l1]
1827    interp delete x
1828    lappend l [expr [testchannel refcount stderr] - $l1]
1829    set l
1830} {0 1 0}
1831
1832test io-18.1 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
1833    file delete -force $path(test1)
1834    set l ""
1835    set f [open $path(test1) w]
1836    lappend l [lindex [testchannel info $f] 15]
1837    close $f
1838    if {[catch {lindex [testchannel info $f] 15} msg]} {
1839        lappend l $msg
1840    } else {
1841        lappend l "very broken: $f found after being closed"
1842    }
1843    string compare [string tolower $l] \
1844        [list 1 [format "can not find channel named \"%s\"" $f]]
1845} 0
1846test io-18.2 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
1847    file delete -force $path(test1)
1848    set l ""
1849    set f [open $path(test1) w]
1850    lappend l [lindex [testchannel info $f] 15]
1851    interp create x
1852    interp share "" $f x
1853    lappend l [lindex [testchannel info $f] 15]
1854    x eval close $f
1855    lappend l [lindex [testchannel info $f] 15]
1856    interp delete x
1857    lappend l [lindex [testchannel info $f] 15]
1858    close $f
1859    if {[catch {lindex [testchannel info $f] 15} msg]} {
1860        lappend l $msg
1861    } else {
1862        lappend l "very broken: $f found after being closed"
1863    }
1864    string compare [string tolower $l] \
1865        [list 1 2 1 1 [format "can not find channel named \"%s\"" $f]]
1866} 0
1867test io-18.3 {Tcl_RegisterChannel, Tcl_UnregisterChannel} {testchannel} {
1868    file delete $path(test1)
1869    set l ""
1870    set f [open $path(test1) w]
1871    lappend l [lindex [testchannel info $f] 15]
1872    interp create x
1873    interp share "" $f x
1874    lappend l [lindex [testchannel info $f] 15]
1875    interp delete x
1876    lappend l [lindex [testchannel info $f] 15]
1877    close $f
1878    if {[catch {lindex [testchannel info $f] 15} msg]} {
1879        lappend l $msg
1880    } else {
1881        lappend l "very broken: $f found after being closed"
1882    }
1883    string compare [string tolower $l] \
1884        [list 1 2 1 [format "can not find channel named \"%s\"" $f]]
1885} 0
1886
1887test io-19.1 {Tcl_GetChannel->Tcl_GetStdChannel, standard handles} {
1888    eof stdin
1889} 0
1890test io-19.2 {testing Tcl_GetChannel, user opened handle} {
1891    file delete $path(test1)
1892    set f [open $path(test1) w]
1893    set x [eof $f]
1894    close $f
1895    set x
1896} 0
1897test io-19.3 {Tcl_GetChannel, channel not found} {
1898    list [catch {eof file34} msg] $msg
1899} {1 {can not find channel named "file34"}}
1900test io-19.4 {Tcl_CreateChannel, insertion into channel table} {testchannel} {
1901    file delete $path(test1)
1902    set f [open $path(test1) w]
1903    set l ""
1904    lappend l [eof $f]
1905    close $f
1906    if {[catch {lindex [testchannel info $f] 15} msg]} {
1907        lappend l $msg
1908    } else {
1909        lappend l "very broken: $f found after being closed"
1910    }
1911    string compare [string tolower $l] \
1912        [list 0 [format "can not find channel named \"%s\"" $f]]
1913} 0
1914
1915test io-20.1 {Tcl_CreateChannel: initial settings} {
1916        set a [open $path(test2) w]
1917    set old [encoding system]
1918    encoding system ascii
1919    set f [open $path(test1) w]
1920    set x [fconfigure $f -encoding]
1921    close $f
1922    encoding system $old
1923        close $a
1924    set x
1925} {ascii}   
1926test io-20.2 {Tcl_CreateChannel: initial settings} {win} {
1927    set f [open $path(test1) w+]
1928    set x [list [fconfigure $f -eofchar] [fconfigure $f -translation]]
1929    close $f
1930    set x
1931} [list [list \x1a ""] {auto crlf}]
1932test io-20.3 {Tcl_CreateChannel: initial settings} {unix} {
1933    set f [open $path(test1) w+]
1934    set x [list [fconfigure $f -eofchar] [fconfigure $f -translation]]
1935    close $f
1936    set x
1937} {{{} {}} {auto lf}}
1938set path(stdout) [makeFile {} stdout]
1939test io-20.5 {Tcl_CreateChannel: install channel in empty slot} {stdio openpipe} {
1940    set f [open $path(script) w]
1941    puts -nonewline $f {
1942        close stdout
1943        set f1 [}
1944    puts $f [list open $path(stdout) w]]
1945    puts $f {
1946        fconfigure $f1 -buffersize 777
1947        puts stderr [fconfigure stdout -buffersize]
1948    }
1949    close $f
1950    set f [open "|[list [interpreter] $path(script)]"]
1951    catch {close $f} msg
1952    set msg
1953} {777}
1954
1955test io-21.1 {CloseChannelsOnExit} emptyTest {
1956} {}
1957
1958# Test management of attributes associated with a channel, such as
1959# its default translation, its name and type, etc. The functions
1960# tested in this group are Tcl_GetChannelName,
1961# Tcl_GetChannelType and Tcl_GetChannelFile. Tcl_GetChannelInstanceData
1962# not tested because files do not use the instance data.
1963
1964test io-22.1 {Tcl_GetChannelMode} emptyTest {
1965    # Not used anywhere in Tcl.
1966} {}
1967
1968test io-23.1 {Tcl_GetChannelName} {testchannel} {
1969    file delete $path(test1)
1970    set f [open $path(test1) w]
1971    set n [testchannel name $f]
1972    close $f
1973    string compare $n $f
1974} 0
1975
1976test io-24.1 {Tcl_GetChannelType} {testchannel} {
1977    file delete $path(test1)
1978    set f [open $path(test1) w]
1979    set t [testchannel type $f]
1980    close $f
1981    string compare $t file
1982} 0
1983
1984test io-25.1 {Tcl_GetChannelHandle, input} {testchannel} {
1985    set f [open $path(test1) w]
1986    fconfigure $f -translation lf -eofchar {}
1987    puts $f "1234567890\n098765432"
1988    close $f
1989    set f [open $path(test1) r]
1990    gets $f
1991    set l ""
1992    lappend l [testchannel inputbuffered $f]
1993    lappend l [tell $f]
1994    close $f
1995    set l
1996} {10 11}
1997test io-25.2 {Tcl_GetChannelHandle, output} {testchannel} {
1998    file delete $path(test1)
1999    set f [open $path(test1) w]
2000    fconfigure $f -translation lf
2001    puts $f hello
2002    set l ""
2003    lappend l [testchannel outputbuffered $f]
2004    lappend l [tell $f]
2005    flush $f
2006    lappend l [testchannel outputbuffered $f]
2007    lappend l [tell $f]
2008    close $f
2009    file delete $path(test1)
2010    set l
2011} {6 6 0 6}
2012
2013test io-26.1 {Tcl_GetChannelInstanceData} {stdio openpipe} {
2014    # "pid" command uses Tcl_GetChannelInstanceData
2015    # Don't care what pid is (but must be a number), just want to exercise it.
2016
2017    set f [open "|[list [interpreter] << exit]"]
2018    expr [pid $f]
2019    close $f
2020} {}   
2021
2022# Test flushing. The functions tested here are FlushChannel.
2023
2024test io-27.1 {FlushChannel, no output buffered} {
2025    file delete $path(test1)
2026    set f [open $path(test1) w]
2027    flush $f
2028    set s [file size $path(test1)]
2029    close $f
2030    set s
2031} 0
2032test io-27.2 {FlushChannel, some output buffered} {
2033    file delete $path(test1)
2034    set f [open $path(test1) w]
2035    fconfigure $f -translation lf -eofchar {}
2036    set l ""
2037    puts $f hello
2038    lappend l [file size $path(test1)]
2039    flush $f
2040    lappend l [file size $path(test1)]
2041    close $f
2042    lappend l [file size $path(test1)]
2043    set l
2044} {0 6 6}
2045test io-27.3 {FlushChannel, implicit flush on close} {
2046    file delete $path(test1)
2047    set f [open $path(test1) w]
2048    fconfigure $f -translation lf -eofchar {}
2049    set l ""
2050    puts $f hello
2051    lappend l [file size $path(test1)]
2052    close $f
2053    lappend l [file size $path(test1)]
2054    set l
2055} {0 6}
2056test io-27.4 {FlushChannel, implicit flush when buffer fills} {
2057    file delete $path(test1)
2058    set f [open $path(test1) w]
2059    fconfigure $f -translation lf -eofchar {}
2060    fconfigure $f -buffersize 60
2061    set l ""
2062    lappend l [file size $path(test1)]
2063    for {set i 0} {$i < 12} {incr i} {
2064        puts $f hello
2065    }
2066    lappend l [file size $path(test1)]
2067    flush $f
2068    lappend l [file size $path(test1)]
2069    close $f
2070    set l
2071} {0 60 72}
2072test io-27.5 {FlushChannel, implicit flush when buffer fills and on close} \
2073        {unixOrPc} {
2074    file delete $path(test1)
2075    set f [open $path(test1) w]
2076    fconfigure $f -translation lf -buffersize 60 -eofchar {}
2077    set l ""
2078    lappend l [file size $path(test1)]
2079    for {set i 0} {$i < 12} {incr i} {
2080        puts $f hello
2081    }
2082    lappend l [file size $path(test1)]
2083    close $f
2084    lappend l [file size $path(test1)]
2085    set l
2086} {0 60 72}
2087set path(pipe)   [makeFile {} pipe]
2088set path(output) [makeFile {} output]
2089test io-27.6 {FlushChannel, async flushing, async close} \
2090        {stdio asyncPipeClose openpipe} {
2091    file delete $path(pipe)
2092    file delete $path(output)
2093    set f [open $path(pipe) w]
2094    puts $f "set f \[[list open $path(output) w]]"
2095    puts $f {
2096        fconfigure $f -translation lf -buffering none -eofchar {}
2097        while {![eof stdin]} {
2098            after 20
2099            puts -nonewline $f [read stdin 1024]
2100        }
2101        close $f
2102    }
2103    close $f
2104    set x 01234567890123456789012345678901
2105    for {set i 0} {$i < 11} {incr i} {
2106        set x "$x$x"
2107    }
2108    set f [open $path(output) w]
2109    close $f
2110    set f [open "|[list [interpreter] $path(pipe)]" w]
2111    fconfigure $f -blocking off
2112    puts -nonewline $f $x
2113    close $f
2114    set counter 0
2115    while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2116        after 20 [list incr [namespace which -variable counter]]
2117        vwait [namespace which -variable counter]
2118    }
2119    if {$counter == 1000} {
2120        set result "file size only [file size $path(output)]"
2121    } else {
2122        set result ok
2123    }
2124} ok
2125
2126# Tests closing a channel. The functions tested are CloseChannel and Tcl_Close.
2127
2128test io-28.1 {CloseChannel called when all references are dropped} {testchannel} {
2129    file delete $path(test1)
2130    set f [open $path(test1) w]
2131    interp create x
2132    interp share "" $f x
2133    set l ""
2134    lappend l [testchannel refcount $f]
2135    x eval close $f
2136    interp delete x
2137    lappend l [testchannel refcount $f]
2138    close $f
2139    set l
2140} {2 1}
2141test io-28.2 {CloseChannel called when all references are dropped} {
2142    file delete $path(test1)
2143    set f [open $path(test1) w]
2144    interp create x
2145    interp share "" $f x
2146    puts -nonewline $f abc
2147    close $f
2148    x eval puts $f def
2149    x eval close $f
2150    interp delete x
2151    set f [open $path(test1) r]
2152    set l [gets $f]
2153    close $f
2154    set l
2155} abcdef
2156test io-28.3 {CloseChannel, not called before output queue is empty} \
2157        {stdio asyncPipeClose nonPortable openpipe} {
2158    file delete $path(pipe)
2159    file delete $path(output)
2160    set f [open $path(pipe) w]
2161    puts $f {
2162
2163        # Need to not have eof char appended on close, because the other
2164        # side of the pipe already closed, so that writing would cause an
2165        # error "invalid file".
2166
2167        fconfigure stdout -eofchar {}
2168        fconfigure stderr -eofchar {}
2169
2170        set f [open $path(output) w]
2171        fconfigure $f -translation lf -buffering none
2172        for {set x 0} {$x < 20} {incr x} {
2173            after 20
2174            puts -nonewline $f [read stdin 1024]
2175        }
2176        close $f
2177    }
2178    close $f
2179    set x 01234567890123456789012345678901
2180    for {set i 0} {$i < 11} {incr i} {
2181        set x "$x$x"
2182    }
2183    set f [open $path(output) w]
2184    close $f
2185    set f [open "|[list [interpreter] pipe]" r+]
2186    fconfigure $f -blocking off -eofchar {}
2187
2188    puts -nonewline $f $x
2189    close $f
2190    set counter 0
2191    while {([file size $path(output)] < 20480) && ($counter < 1000)} {
2192        after 20 [list incr [namespace which -variable counter]]
2193        vwait [namespace which -variable counter]
2194    }
2195    if {$counter == 1000} {
2196        set result probably_broken
2197    } else {
2198        set result ok
2199    }
2200} ok
2201test io-28.4 {Tcl_Close} {testchannel} {
2202    file delete $path(test1)
2203    set l ""
2204    lappend l [lsort [testchannel open]]
2205    set f [open $path(test1) w]
2206    lappend l [lsort [testchannel open]]
2207    close $f
2208    lappend l [lsort [testchannel open]]
2209    set x [list $consoleFileNames \
2210                [lsort [list {*}$consoleFileNames $f]] \
2211                $consoleFileNames]
2212    string compare $l $x
2213} 0
2214test io-28.5 {Tcl_Close vs standard handles} {stdio unix testchannel openpipe} {
2215    file delete $path(script)
2216    set f [open $path(script) w]
2217    puts $f {
2218        close stdin
2219        puts [testchannel open]
2220    }
2221    close $f
2222    set f [open "|[list [interpreter] $path(script)]" r]
2223    set l [gets $f]
2224    close $f
2225    set l
2226} {file1 file2}
2227
2228test io-29.1 {Tcl_WriteChars, channel not writable} {
2229    list [catch {puts stdin hello} msg] $msg
2230} {1 {channel "stdin" wasn't opened for writing}}
2231test io-29.2 {Tcl_WriteChars, empty string} {
2232    file delete $path(test1)
2233    set f [open $path(test1) w]
2234    fconfigure $f -eofchar {}
2235    puts -nonewline $f ""
2236    close $f
2237    file size $path(test1)
2238} 0
2239test io-29.3 {Tcl_WriteChars, nonempty string} {
2240    file delete $path(test1)
2241    set f [open $path(test1) w]
2242    fconfigure $f -eofchar {}
2243    puts -nonewline $f hello
2244    close $f
2245    file size $path(test1)
2246} 5
2247test io-29.4 {Tcl_WriteChars, buffering in full buffering mode} {testchannel} {
2248    file delete $path(test1)
2249    set f [open $path(test1) w]
2250    fconfigure $f -translation lf -buffering full -eofchar {}
2251    puts $f hello
2252    set l ""
2253    lappend l [testchannel outputbuffered $f]
2254    lappend l [file size $path(test1)]
2255    flush $f
2256    lappend l [testchannel outputbuffered $f]
2257    lappend l [file size $path(test1)]
2258    close $f
2259    set l
2260} {6 0 0 6}
2261test io-29.5 {Tcl_WriteChars, buffering in line buffering mode} {testchannel} {
2262    file delete $path(test1)
2263    set f [open $path(test1) w]
2264    fconfigure $f -translation lf -buffering line -eofchar {}
2265    puts -nonewline $f hello
2266    set l ""
2267    lappend l [testchannel outputbuffered $f]
2268    lappend l [file size $path(test1)]
2269    puts $f hello
2270    lappend l [testchannel outputbuffered $f]
2271    lappend l [file size $path(test1)]
2272    close $f
2273    set l
2274} {5 0 0 11}
2275test io-29.6 {Tcl_WriteChars, buffering in no buffering mode} {testchannel} {
2276    file delete $path(test1)
2277    set f [open $path(test1) w]
2278    fconfigure $f -translation lf -buffering none -eofchar {}
2279    puts -nonewline $f hello
2280    set l ""
2281    lappend l [testchannel outputbuffered $f]
2282    lappend l [file size $path(test1)]
2283    puts $f hello
2284    lappend l [testchannel outputbuffered $f]
2285    lappend l [file size $path(test1)]
2286    close $f
2287    set l
2288} {0 5 0 11}
2289test io-29.7 {Tcl_Flush, full buffering} {testchannel} {
2290    file delete $path(test1)
2291    set f [open $path(test1) w]
2292    fconfigure $f -translation lf -buffering full -eofchar {}
2293    puts -nonewline $f hello
2294    set l ""
2295    lappend l [testchannel outputbuffered $f]
2296    lappend l [file size $path(test1)]
2297    puts $f hello
2298    lappend l [testchannel outputbuffered $f]
2299    lappend l [file size $path(test1)]
2300    flush $f
2301    lappend l [testchannel outputbuffered $f]
2302    lappend l [file size $path(test1)]
2303    close $f
2304    set l
2305} {5 0 11 0 0 11}
2306test io-29.8 {Tcl_Flush, full buffering} {testchannel} {
2307    file delete $path(test1)
2308    set f [open $path(test1) w]
2309    fconfigure $f -translation lf -buffering line
2310    puts -nonewline $f hello
2311    set l ""
2312    lappend l [testchannel outputbuffered $f]
2313    lappend l [file size $path(test1)]
2314    flush $f
2315    lappend l [testchannel outputbuffered $f]
2316    lappend l [file size $path(test1)]
2317    puts $f hello
2318    lappend l [testchannel outputbuffered $f]
2319    lappend l [file size $path(test1)]
2320    flush $f
2321    lappend l [testchannel outputbuffered $f]
2322    lappend l [file size $path(test1)]
2323    close $f
2324    set l
2325} {5 0 0 5 0 11 0 11}
2326test io-29.9 {Tcl_Flush, channel not writable} {
2327    list [catch {flush stdin} msg] $msg
2328} {1 {channel "stdin" wasn't opened for writing}}
2329test io-29.10 {Tcl_WriteChars, looping and buffering} {
2330    file delete $path(test1)
2331    set f1 [open $path(test1) w]
2332    fconfigure $f1 -translation lf -eofchar {}
2333    set f2 [open $path(longfile) r]
2334    for {set x 0} {$x < 10} {incr x} {
2335        puts $f1 [gets $f2]
2336    }
2337    close $f2
2338    close $f1
2339    file size $path(test1)
2340} 387
2341test io-29.11 {Tcl_WriteChars, no newline, implicit flush} {
2342    file delete $path(test1)
2343    set f1 [open $path(test1) w]
2344    fconfigure $f1 -eofchar {}
2345    set f2 [open $path(longfile) r]
2346    for {set x 0} {$x < 10} {incr x} {
2347        puts -nonewline $f1 [gets $f2]
2348    }
2349    close $f1
2350    close $f2
2351    file size $path(test1)
2352} 377
2353test io-29.12 {Tcl_WriteChars on a pipe} {stdio openpipe} {
2354    file delete $path(test1)
2355    file delete $path(pipe)
2356    set f1 [open $path(pipe) w]
2357    puts $f1 "set f1 \[[list open $path(longfile) r]]"
2358    puts $f1 {
2359        for {set x 0} {$x < 10} {incr x} {
2360            puts [gets $f1]
2361        }
2362    }
2363    close $f1
2364    set f1 [open "|[list [interpreter] $path(pipe)]" r]
2365    set f2 [open $path(longfile) r]
2366    set y ok
2367    for {set x 0} {$x < 10} {incr x} {
2368        set l1 [gets $f1]
2369        set l2 [gets $f2]
2370        if {"$l1" != "$l2"} {
2371            set y broken
2372        }
2373    }
2374    close $f1
2375    close $f2
2376    set y
2377} ok
2378test io-29.13 {Tcl_WriteChars to a pipe, line buffered} {stdio openpipe} {
2379    file delete $path(test1)
2380    file delete $path(pipe)
2381    set f1 [open $path(pipe) w]
2382    puts $f1 {
2383        puts [gets stdin]
2384        puts [gets stdin]
2385    }
2386    close $f1
2387    set y ok
2388    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2389    fconfigure $f1 -buffering line
2390    set f2 [open $path(longfile) r]
2391    set line [gets $f2]
2392    puts $f1 $line
2393    set backline [gets $f1]
2394    if {"$line" != "$backline"} {
2395        set y broken
2396    }
2397    set line [gets $f2]
2398    puts $f1 $line
2399    set backline [gets $f1]
2400    if {"$line" != "$backline"} {
2401        set y broken
2402    }
2403    close $f1
2404    close $f2
2405    set y
2406} ok
2407test io-29.14 {Tcl_WriteChars, buffering and implicit flush at close} {
2408    file delete $path(test3)
2409    set f [open $path(test3) w]
2410    puts -nonewline $f "Text1"
2411    puts -nonewline $f " Text 2"
2412    puts $f " Text 3"
2413    close $f
2414    set f [open $path(test3) r]
2415    set x [gets $f]
2416    close $f
2417    set x
2418} {Text1 Text 2 Text 3}
2419test io-29.15 {Tcl_Flush, channel not open for writing} {
2420    file delete $path(test1)
2421    set fd [open $path(test1) w]
2422    close $fd
2423    set fd [open $path(test1) r]
2424    set x [list [catch {flush $fd} msg] $msg]
2425    close $fd
2426    string compare $x \
2427        [list 1 "channel \"$fd\" wasn't opened for writing"]
2428} 0
2429test io-29.16 {Tcl_Flush on pipe opened only for reading} {stdio openpipe} {
2430    set fd [open "|[list [interpreter] cat longfile]" r]
2431    set x [list [catch {flush $fd} msg] $msg]
2432    catch {close $fd}
2433    string compare $x \
2434        [list 1 "channel \"$fd\" wasn't opened for writing"]
2435} 0
2436test io-29.17 {Tcl_WriteChars buffers, then Tcl_Flush flushes} {
2437    file delete $path(test1)
2438    set f1 [open $path(test1) w]
2439    fconfigure $f1 -translation lf
2440    puts $f1 hello
2441    puts $f1 hello
2442    puts $f1 hello
2443    flush $f1
2444    set x [file size $path(test1)]
2445    close $f1
2446    set x
2447} 18
2448test io-29.18 {Tcl_WriteChars and Tcl_Flush intermixed} {
2449    file delete $path(test1)
2450    set x ""
2451    set f1 [open $path(test1) w]
2452    fconfigure $f1 -translation lf
2453    puts $f1 hello
2454    puts $f1 hello
2455    puts $f1 hello
2456    flush $f1
2457    lappend x [file size $path(test1)]
2458    puts $f1 hello
2459    flush $f1
2460    lappend x [file size $path(test1)]
2461    puts $f1 hello
2462    flush $f1
2463    lappend x [file size $path(test1)]
2464    close $f1
2465    set x
2466} {18 24 30}
2467test io-29.19 {Explicit and implicit flushes} {
2468    file delete $path(test1)
2469    set f1 [open $path(test1) w]
2470    fconfigure $f1 -translation lf -eofchar {}
2471    set x ""
2472    puts $f1 hello
2473    puts $f1 hello
2474    puts $f1 hello
2475    flush $f1
2476    lappend x [file size $path(test1)]
2477    puts $f1 hello
2478    flush $f1
2479    lappend x [file size $path(test1)]
2480    puts $f1 hello
2481    close $f1
2482    lappend x [file size $path(test1)]
2483    set x
2484} {18 24 30}
2485test io-29.20 {Implicit flush when buffer is full} {
2486    file delete $path(test1)
2487    set f1 [open $path(test1) w]
2488    fconfigure $f1 -translation lf -eofchar {}
2489    set line "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2490    for {set x 0} {$x < 100} {incr x} {
2491      puts $f1 $line
2492    }
2493    set z ""
2494    lappend z [file size $path(test1)]
2495    for {set x 0} {$x < 100} {incr x} {
2496        puts $f1 $line
2497    }
2498    lappend z [file size $path(test1)]
2499    close $f1
2500    lappend z [file size $path(test1)]
2501    set z
2502} {4096 12288 12600}
2503test io-29.21 {Tcl_Flush to pipe} {stdio openpipe} {
2504    file delete $path(pipe)
2505    set f1 [open $path(pipe) w]
2506    puts $f1 {set x [read stdin 6]}
2507    puts $f1 {set cnt [string length $x]}
2508    puts $f1 {puts "read $cnt characters"}
2509    close $f1
2510    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2511    puts $f1 hello
2512    flush $f1
2513    set x [gets $f1]
2514    catch {close $f1}
2515    set x
2516} "read 6 characters"
2517test io-29.22 {Tcl_Flush called at other end of pipe} {stdio openpipe} {
2518    file delete $path(pipe)
2519    set f1 [open $path(pipe) w]
2520    puts $f1 {
2521        fconfigure stdout -buffering full
2522        puts hello
2523        puts hello
2524        flush stdout
2525        gets stdin
2526        puts bye
2527        flush stdout
2528    }
2529    close $f1
2530    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2531    set x ""
2532    lappend x [gets $f1]
2533    lappend x [gets $f1]
2534    puts $f1 hello
2535    flush $f1
2536    lappend x [gets $f1]
2537    close $f1
2538    set x
2539} {hello hello bye}
2540test io-29.23 {Tcl_Flush and line buffering at end of pipe} {stdio openpipe} {
2541    file delete $path(pipe)
2542    set f1 [open $path(pipe) w]
2543    puts $f1 {
2544        puts hello
2545        puts hello
2546        gets stdin
2547        puts bye
2548    }
2549    close $f1
2550    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
2551    set x ""
2552    lappend x [gets $f1]
2553    lappend x [gets $f1]
2554    puts $f1 hello
2555    flush $f1
2556    lappend x [gets $f1]
2557    close $f1
2558    set x
2559} {hello hello bye}
2560test io-29.24 {Tcl_WriteChars and Tcl_Flush move end of file} {
2561    set f [open $path(test3) w]
2562    puts $f "Line 1"
2563    puts $f "Line 2"
2564    set f2 [open $path(test3)]
2565    set x {}
2566    lappend x [read -nonewline $f2]
2567    close $f2
2568    flush $f
2569    set f2 [open $path(test3)]
2570    lappend x [read -nonewline $f2]
2571    close $f2
2572    close $f
2573    set x
2574} "{} {Line 1\nLine 2}"
2575test io-29.25 {Implicit flush with Tcl_Flush to command pipelines} {stdio openpipe fileevent} {
2576    file delete $path(test3)
2577    set f [open "|[list [interpreter] $path(cat) | [interpreter] $path(cat) > $path(test3)]" w]
2578    puts $f "Line 1"
2579    puts $f "Line 2"
2580    close $f
2581    after 100
2582    set f [open $path(test3) r]
2583    set x [read $f]
2584    close $f
2585    set x
2586} "Line 1\nLine 2\n"
2587test io-29.26 {Tcl_Flush, Tcl_Write on bidirectional pipelines} {stdio unixExecs openpipe} {
2588    set f [open "|[list cat -u]" r+]
2589    puts $f "Line1"
2590    flush $f
2591    set x [gets $f]
2592    close $f
2593    set x
2594} {Line1}
2595test io-29.27 {Tcl_Flush on closed pipeline} {stdio openpipe} {
2596    file delete $path(pipe)
2597    set f [open $path(pipe) w]
2598    puts $f {exit}
2599    close $f
2600    set f [open "|[list [interpreter] $path(pipe)]" r+]
2601    gets $f
2602    puts $f output
2603    after 50
2604    #
2605    # The flush below will get a SIGPIPE. This is an expected part of
2606    # test and indicates that the test operates correctly. If you run
2607    # this test under a debugger, the signal will by intercepted unless
2608    # you disable the debugger's signal interception.
2609    #
2610    if {[catch {flush $f} msg]} {
2611        set x [list 1 $msg $::errorCode]
2612        catch {close $f}
2613    } else {
2614        if {[catch {close $f} msg]} {
2615            set x [list 1 $msg $::errorCode]
2616        } else {
2617            set x {this was supposed to fail and did not}
2618        }
2619    }
2620    regsub {".*":} $x {"":} x
2621    string tolower $x
2622} {1 {error flushing "": broken pipe} {posix epipe {broken pipe}}}
2623test io-29.28 {Tcl_WriteChars, lf mode} {
2624    file delete $path(test1)
2625    set f [open $path(test1) w]
2626    fconfigure $f -translation lf -eofchar {}
2627    puts $f hello\nthere\nand\nhere
2628    flush $f
2629    set s [file size $path(test1)]
2630    close $f
2631    set s
2632} 21
2633test io-29.29 {Tcl_WriteChars, cr mode} {
2634    file delete $path(test1)
2635    set f [open $path(test1) w]
2636    fconfigure $f -translation cr -eofchar {}
2637    puts $f hello\nthere\nand\nhere
2638    close $f
2639    file size $path(test1)
2640} 21
2641test io-29.30 {Tcl_WriteChars, crlf mode} {
2642    file delete $path(test1)
2643    set f [open $path(test1) w]
2644    fconfigure $f -translation crlf -eofchar {}
2645    puts $f hello\nthere\nand\nhere
2646    close $f
2647    file size $path(test1)
2648} 25
2649test io-29.31 {Tcl_WriteChars, background flush} {stdio openpipe} {
2650    file delete $path(pipe)
2651    file delete $path(output)
2652    set f [open $path(pipe) w]
2653    puts $f "set f \[[list open $path(output)  w]]"
2654    puts $f {fconfigure $f -translation lf}
2655    set x [list while {![eof stdin]}]
2656    set x "$x {"
2657    puts $f $x
2658    puts $f {  puts -nonewline $f [read stdin 4096]}
2659    puts $f {  flush $f}
2660    puts $f "}"
2661    puts $f {close $f}
2662    close $f
2663    set x 01234567890123456789012345678901
2664    for {set i 0} {$i < 11} {incr i} {
2665        set x "$x$x"
2666    }
2667    set f [open $path(output) w]
2668    close $f
2669    set f [open "|[list [interpreter] $path(pipe)]" r+]
2670    fconfigure $f -blocking off
2671    puts -nonewline $f $x
2672    close $f
2673    set counter 0
2674    while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2675        after 10 [list incr [namespace which -variable counter]]
2676        vwait [namespace which -variable counter]
2677    }
2678    if {$counter == 1000} {
2679        set result "file size only [file size $path(output)]"
2680    } else {
2681        set result ok
2682    }
2683    # allow a little time for the background process to close.
2684    # otherwise, the following test fails on the [file delete $path(output)
2685    # on Windows because a process still has the file open.
2686    after 100 set v 1; vwait v
2687    set result
2688} ok
2689test io-29.32 {Tcl_WriteChars, background flush to slow reader} \
2690        {stdio asyncPipeClose openpipe} {
2691    file delete $path(pipe)
2692    file delete $path(output)
2693    set f [open $path(pipe) w]
2694    puts $f "set f \[[list open $path(output) w]]"
2695    puts $f {fconfigure $f -translation lf}
2696    set x [list while {![eof stdin]}]
2697    set x "$x \{"
2698    puts $f $x
2699    puts $f {  after 20}
2700    puts $f {  puts -nonewline $f [read stdin 1024]}
2701    puts $f {  flush $f}
2702    puts $f "\}"
2703    puts $f {close $f}
2704    close $f
2705    set x 01234567890123456789012345678901
2706    for {set i 0} {$i < 11} {incr i} {
2707        set x "$x$x"
2708    }
2709    set f [open $path(output) w]
2710    close $f
2711    set f [open "|[list [interpreter] $path(pipe)]" r+]
2712    fconfigure $f -blocking off
2713    puts -nonewline $f $x
2714    close $f
2715    set counter 0
2716    while {([file size $path(output)] < 65536) && ($counter < 1000)} {
2717        after 20 [list incr [namespace which -variable counter]]
2718        vwait [namespace which -variable counter]
2719    }
2720    if {$counter == 1000} {
2721        set result "file size only [file size $path(output)]"
2722    } else {
2723        set result ok
2724    }
2725} ok
2726test io-29.33 {Tcl_Flush, implicit flush on exit} {exec} {
2727    set f [open $path(script) w]
2728    puts $f "set f \[[list open $path(test1) w]]"
2729    puts $f {fconfigure $f -translation lf
2730        puts $f hello
2731        puts $f bye
2732        puts $f strange
2733    }
2734    close $f
2735    exec [interpreter] $path(script)
2736    set f [open $path(test1) r]
2737    set r [read $f]
2738    close $f
2739    set r
2740} "hello\nbye\nstrange\n"
2741test io-29.34 {Tcl_Close, async flush on close, using sockets} {socket tempNotMac fileevent} {
2742    variable c 0
2743    variable x running
2744    set l abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
2745    proc writelots {s l} {
2746        for {set i 0} {$i < 2000} {incr i} {
2747            puts $s $l
2748        }
2749    }
2750    proc accept {s a p} {
2751        variable x
2752        fileevent $s readable [namespace code [list readit $s]]
2753        fconfigure $s -blocking off
2754        set x accepted
2755    }
2756    proc readit {s} {
2757        variable c
2758        variable x
2759        set l [gets $s]
2760
2761        if {[eof $s]} {
2762            close $s
2763            set x done
2764        } elseif {([string length $l] > 0) || ![fblocked $s]} {
2765            incr c
2766        }
2767    }
2768    set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
2769    set cs [socket 127.0.0.1 [lindex [fconfigure $ss -sockname] 2]]
2770    vwait [namespace which -variable x]
2771    fconfigure $cs -blocking off
2772    writelots $cs $l
2773    close $cs
2774    close $ss
2775    vwait [namespace which -variable x]
2776    set c
2777} 2000
2778test io-29.35 {Tcl_Close vs fileevent vs multiple interpreters} {socket tempNotMac fileevent} {
2779    # On Mac, this test screws up sockets such that subsequent tests using port 2828
2780    # either cause errors or panic().
2781
2782    catch {interp delete x}
2783    catch {interp delete y}
2784    interp create x
2785    interp create y
2786    set s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
2787    proc accept {s a p} {
2788        puts $s hello
2789        close $s
2790    }
2791    set c [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
2792    interp share {} $c x
2793    interp share {} $c y
2794    close $c
2795    x eval {
2796        proc readit {s} {
2797            gets $s
2798            if {[eof $s]} {
2799                close $s
2800            }
2801        }
2802    }
2803    y eval {
2804        proc readit {s} {
2805            gets $s
2806            if {[eof $s]} {
2807                close $s
2808            }
2809        }
2810    }
2811    x eval "fileevent $c readable \{readit $c\}"
2812    y eval "fileevent $c readable \{readit $c\}"
2813    y eval [list close $c]
2814    update
2815    close $s
2816    interp delete x
2817    interp delete y
2818} ""
2819
2820# Test end of line translations. Procedures tested are Tcl_Write, Tcl_Read.
2821
2822test io-30.1 {Tcl_Write lf, Tcl_Read lf} {
2823    file delete $path(test1)
2824    set f [open $path(test1) w]
2825    fconfigure $f -translation lf
2826    puts $f hello\nthere\nand\nhere
2827    close $f
2828    set f [open $path(test1) r]
2829    fconfigure $f -translation lf
2830    set x [read $f]
2831    close $f
2832    set x
2833} "hello\nthere\nand\nhere\n"
2834test io-30.2 {Tcl_Write lf, Tcl_Read cr} {
2835    file delete $path(test1)
2836    set f [open $path(test1) w]
2837    fconfigure $f -translation lf
2838    puts $f hello\nthere\nand\nhere
2839    close $f
2840    set f [open $path(test1) r]
2841    fconfigure $f -translation cr
2842    set x [read $f]
2843    close $f
2844    set x
2845} "hello\nthere\nand\nhere\n"
2846test io-30.3 {Tcl_Write lf, Tcl_Read crlf} {
2847    file delete $path(test1)
2848    set f [open $path(test1) w]
2849    fconfigure $f -translation lf
2850    puts $f hello\nthere\nand\nhere
2851    close $f
2852    set f [open $path(test1) r]
2853    fconfigure $f -translation crlf
2854    set x [read $f]
2855    close $f
2856    set x
2857} "hello\nthere\nand\nhere\n"
2858test io-30.4 {Tcl_Write cr, Tcl_Read cr} {
2859    file delete $path(test1)
2860    set f [open $path(test1) w]
2861    fconfigure $f -translation cr
2862    puts $f hello\nthere\nand\nhere
2863    close $f
2864    set f [open $path(test1) r]
2865    fconfigure $f -translation cr
2866    set x [read $f]
2867    close $f
2868    set x
2869} "hello\nthere\nand\nhere\n"
2870test io-30.5 {Tcl_Write cr, Tcl_Read lf} {
2871    file delete $path(test1)
2872    set f [open $path(test1) w]
2873    fconfigure $f -translation cr
2874    puts $f hello\nthere\nand\nhere
2875    close $f
2876    set f [open $path(test1) r]
2877    fconfigure $f -translation lf
2878    set x [read $f]
2879    close $f
2880    set x
2881} "hello\rthere\rand\rhere\r"
2882test io-30.6 {Tcl_Write cr, Tcl_Read crlf} {
2883    file delete $path(test1)
2884    set f [open $path(test1) w]
2885    fconfigure $f -translation cr
2886    puts $f hello\nthere\nand\nhere
2887    close $f
2888    set f [open $path(test1) r]
2889    fconfigure $f -translation crlf
2890    set x [read $f]
2891    close $f
2892    set x
2893} "hello\rthere\rand\rhere\r"
2894test io-30.7 {Tcl_Write crlf, Tcl_Read crlf} {
2895    file delete $path(test1)
2896    set f [open $path(test1) w]
2897    fconfigure $f -translation crlf
2898    puts $f hello\nthere\nand\nhere
2899    close $f
2900    set f [open $path(test1) r]
2901    fconfigure $f -translation crlf
2902    set x [read $f]
2903    close $f
2904    set x
2905} "hello\nthere\nand\nhere\n"
2906test io-30.8 {Tcl_Write crlf, Tcl_Read lf} {
2907    file delete $path(test1)
2908    set f [open $path(test1) w]
2909    fconfigure $f -translation crlf
2910    puts $f hello\nthere\nand\nhere
2911    close $f
2912    set f [open $path(test1) r]
2913    fconfigure $f -translation lf
2914    set x [read $f]
2915    close $f
2916    set x
2917} "hello\r\nthere\r\nand\r\nhere\r\n"
2918test io-30.9 {Tcl_Write crlf, Tcl_Read cr} {
2919    file delete $path(test1)
2920    set f [open $path(test1) w]
2921    fconfigure $f -translation crlf
2922    puts $f hello\nthere\nand\nhere
2923    close $f
2924    set f [open $path(test1) r]
2925    fconfigure $f -translation cr
2926    set x [read $f]
2927    close $f
2928    set x
2929} "hello\n\nthere\n\nand\n\nhere\n\n"
2930test io-30.10 {Tcl_Write lf, Tcl_Read auto} {
2931    file delete $path(test1)
2932    set f [open $path(test1) w]
2933    fconfigure $f -translation lf
2934    puts $f hello\nthere\nand\nhere
2935    close $f
2936    set f [open $path(test1) r]
2937    set c [read $f]
2938    set x [fconfigure $f -translation]
2939    close $f
2940    list $c $x
2941} {{hello
2942there
2943and
2944here
2945} auto}
2946test io-30.11 {Tcl_Write cr, Tcl_Read auto} {
2947    file delete $path(test1)
2948    set f [open $path(test1) w]
2949    fconfigure $f -translation cr
2950    puts $f hello\nthere\nand\nhere
2951    close $f
2952    set f [open $path(test1) r]
2953    set c [read $f]
2954    set x [fconfigure $f -translation]
2955    close $f
2956    list $c $x
2957} {{hello
2958there
2959and
2960here
2961} auto}
2962test io-30.12 {Tcl_Write crlf, Tcl_Read auto} {
2963    file delete $path(test1)
2964    set f [open $path(test1) w]
2965    fconfigure $f -translation crlf
2966    puts $f hello\nthere\nand\nhere
2967    close $f
2968    set f [open $path(test1) r]
2969    set c [read $f]
2970    set x [fconfigure $f -translation]
2971    close $f
2972    list $c $x
2973} {{hello
2974there
2975and
2976here
2977} auto}
2978test io-30.13 {Tcl_Write crlf on block boundary, Tcl_Read auto} {
2979    file delete $path(test1)
2980    set f [open $path(test1) w]
2981    fconfigure $f -translation crlf
2982    set line "123456789ABCDE"   ;# 14 char plus crlf
2983    puts -nonewline $f x        ;# shift crlf across block boundary
2984    for {set i 0} {$i < 700} {incr i} {
2985        puts $f $line
2986    }
2987    close $f
2988    set f [open $path(test1) r]
2989    fconfigure $f -translation auto
2990    set c [read $f]
2991    close $f
2992    string length $c
2993} [expr 700*15+1]
2994test io-30.14 {Tcl_Write crlf on block boundary, Tcl_Read crlf} {
2995    file delete $path(test1)
2996    set f [open $path(test1) w]
2997    fconfigure $f -translation crlf
2998    set line "123456789ABCDE"   ;# 14 char plus crlf
2999    puts -nonewline $f x        ;# shift crlf across block boundary
3000    for {set i 0} {$i < 700} {incr i} {
3001        puts $f $line
3002    }
3003    close $f
3004    set f [open $path(test1) r]
3005    fconfigure $f -translation crlf
3006    set c [read $f]
3007    close $f
3008    string length $c
3009} [expr 700*15+1]
3010test io-30.15 {Tcl_Write mixed, Tcl_Read auto} {
3011    file delete $path(test1)
3012    set f [open $path(test1) w]
3013    fconfigure $f -translation lf
3014    puts $f hello\nthere\nand\rhere
3015    close $f
3016    set f [open $path(test1) r]
3017    fconfigure $f -translation auto
3018    set c [read $f]
3019    close $f
3020    set c
3021} {hello
3022there
3023and
3024here
3025}
3026test io-30.16 {Tcl_Write ^Z at end, Tcl_Read auto} {
3027    file delete $path(test1)
3028    set f [open $path(test1) w]
3029    fconfigure $f -translation lf
3030    puts -nonewline $f hello\nthere\nand\rhere\n\x1a
3031    close $f
3032    set f [open $path(test1) r]
3033    fconfigure $f -eofchar \x1a -translation auto
3034    set c [read $f]
3035    close $f
3036    set c
3037} {hello
3038there
3039and
3040here
3041}
3042test io-30.17 {Tcl_Write, implicit ^Z at end, Tcl_Read auto} {win} {
3043    file delete $path(test1)
3044    set f [open $path(test1) w]
3045    fconfigure $f -eofchar \x1a -translation lf
3046    puts $f hello\nthere\nand\rhere
3047    close $f
3048    set f [open $path(test1) r]
3049    fconfigure $f -eofchar \x1a -translation auto
3050    set c [read $f]
3051    close $f
3052    set c
3053} {hello
3054there
3055and
3056here
3057}
3058test io-30.18 {Tcl_Write, ^Z in middle, Tcl_Read auto} {
3059    file delete $path(test1)
3060    set f [open $path(test1) w]
3061    fconfigure $f -translation lf
3062    set s [format "abc\ndef\n%cghi\nqrs" 26]
3063    puts $f $s
3064    close $f
3065    set f [open $path(test1) r]
3066    fconfigure $f -eofchar \x1a -translation auto
3067    set l ""
3068    lappend l [gets $f]
3069    lappend l [gets $f]
3070    lappend l [eof $f]
3071    lappend l [gets $f]
3072    lappend l [eof $f]
3073    lappend l [gets $f]
3074    lappend l [eof $f]
3075    close $f
3076    set l
3077} {abc def 0 {} 1 {} 1}
3078test io-30.19 {Tcl_Write, ^Z no newline in middle, Tcl_Read auto} {
3079    file delete $path(test1)
3080    set f [open $path(test1) w]
3081    fconfigure $f -translation lf
3082    set s [format "abc\ndef\n%cghi\nqrs" 26]
3083    puts $f $s
3084    close $f
3085    set f [open $path(test1) r]
3086    fconfigure $f -eofchar \x1a -translation auto
3087    set l ""
3088    lappend l [gets $f]
3089    lappend l [gets $f]
3090    lappend l [eof $f]
3091    lappend l [gets $f]
3092    lappend l [eof $f]
3093    lappend l [gets $f]
3094    lappend l [eof $f]
3095    close $f
3096    set l
3097} {abc def 0 {} 1 {} 1}
3098test io-30.20 {Tcl_Write, ^Z in middle ignored, Tcl_Read lf} {
3099    file delete $path(test1)
3100    set f [open $path(test1) w]
3101    fconfigure $f -translation lf -eofchar {}
3102    set s [format "abc\ndef\n%cghi\nqrs" 26]
3103    puts $f $s
3104    close $f
3105    set f [open $path(test1) r]
3106    fconfigure $f -translation lf -eofchar {}
3107    set l ""
3108    lappend l [gets $f]
3109    lappend l [gets $f]
3110    lappend l [eof $f]
3111    lappend l [gets $f]
3112    lappend l [eof $f]
3113    lappend l [gets $f]
3114    lappend l [eof $f]
3115    lappend l [gets $f]
3116    lappend l [eof $f]
3117    close $f
3118    set l
3119} "abc def 0 \x1aghi 0 qrs 0 {} 1"
3120test io-30.21 {Tcl_Write, ^Z in middle ignored, Tcl_Read cr} {
3121    file delete $path(test1)
3122    set f [open $path(test1) w]
3123    fconfigure $f -translation lf -eofchar {}
3124    set s [format "abc\ndef\n%cghi\nqrs" 26]
3125    puts $f $s
3126    close $f
3127    set f [open $path(test1) r]
3128    fconfigure $f -translation cr -eofchar {}
3129    set l ""
3130    set x [gets $f]
3131    lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs\n"]
3132    lappend l [eof $f]
3133    lappend l [gets $f]
3134    lappend l [eof $f]
3135    close $f
3136    set l
3137} {0 1 {} 1}
3138test io-30.22 {Tcl_Write, ^Z in middle ignored, Tcl_Read crlf} {
3139    file delete $path(test1)
3140    set f [open $path(test1) w]
3141    fconfigure $f -translation lf -eofchar {}
3142    set s [format "abc\ndef\n%cghi\nqrs" 26]
3143    puts $f $s
3144    close $f
3145    set f [open $path(test1) r]
3146    fconfigure $f -translation crlf -eofchar {}
3147    set l ""
3148    set x [gets $f]
3149    lappend l [string compare $x "abc\ndef\n\x1aghi\nqrs\n"]
3150    lappend l [eof $f]
3151    lappend l [gets $f]
3152    lappend l [eof $f]
3153    close $f
3154    set l
3155} {0 1 {} 1}
3156test io-30.23 {Tcl_Write lf, ^Z in middle, Tcl_Read auto} {
3157    file delete $path(test1)
3158    set f [open $path(test1) w]
3159    fconfigure $f -translation lf
3160    set c [format abc\ndef\n%cqrs\ntuv 26]
3161    puts $f $c
3162    close $f
3163    set f [open $path(test1) r]
3164    fconfigure $f -translation auto -eofchar \x1a
3165    set c [string length [read $f]]
3166    set e [eof $f]
3167    close $f
3168    list $c $e
3169} {8 1}
3170test io-30.24 {Tcl_Write lf, ^Z in middle, Tcl_Read lf} {
3171    file delete $path(test1)
3172    set f [open $path(test1) w]
3173    fconfigure $f -translation lf
3174    set c [format abc\ndef\n%cqrs\ntuv 26]
3175    puts $f $c
3176    close $f
3177    set f [open $path(test1) r]
3178    fconfigure $f -translation lf -eofchar \x1a
3179    set c [string length [read $f]]
3180    set e [eof $f]
3181    close $f
3182    list $c $e
3183} {8 1}
3184test io-30.25 {Tcl_Write cr, ^Z in middle, Tcl_Read auto} {
3185    file delete $path(test1)
3186    set f [open $path(test1) w]
3187    fconfigure $f -translation cr
3188    set c [format abc\ndef\n%cqrs\ntuv 26]
3189    puts $f $c
3190    close $f
3191    set f [open $path(test1) r]
3192    fconfigure $f -translation auto -eofchar \x1a
3193    set c [string length [read $f]]
3194    set e [eof $f]
3195    close $f
3196    list $c $e
3197} {8 1}
3198test io-30.26 {Tcl_Write cr, ^Z in middle, Tcl_Read cr} {
3199    file delete $path(test1)
3200    set f [open $path(test1) w]
3201    fconfigure $f -translation cr
3202    set c [format abc\ndef\n%cqrs\ntuv 26]
3203    puts $f $c
3204    close $f
3205    set f [open $path(test1) r]
3206    fconfigure $f -translation cr -eofchar \x1a
3207    set c [string length [read $f]]
3208    set e [eof $f]
3209    close $f
3210    list $c $e
3211} {8 1}
3212test io-30.27 {Tcl_Write crlf, ^Z in middle, Tcl_Read auto} {
3213    file delete $path(test1)
3214    set f [open $path(test1) w]
3215    fconfigure $f -translation crlf
3216    set c [format abc\ndef\n%cqrs\ntuv 26]
3217    puts $f $c
3218    close $f
3219    set f [open $path(test1) r]
3220    fconfigure $f -translation auto -eofchar \x1a
3221    set c [string length [read $f]]
3222    set e [eof $f]
3223    close $f
3224    list $c $e
3225} {8 1}
3226test io-30.28 {Tcl_Write crlf, ^Z in middle, Tcl_Read crlf} {
3227    file delete $path(test1)
3228    set f [open $path(test1) w]
3229    fconfigure $f -translation crlf
3230    set c [format abc\ndef\n%cqrs\ntuv 26]
3231    puts $f $c
3232    close $f
3233    set f [open $path(test1) r]
3234    fconfigure $f -translation crlf -eofchar \x1a
3235    set c [string length [read $f]]
3236    set e [eof $f]
3237    close $f
3238    list $c $e
3239} {8 1}
3240
3241# Test end of line translations. Functions tested are Tcl_Write and Tcl_Gets.
3242
3243test io-31.1 {Tcl_Write lf, Tcl_Gets auto} {
3244    file delete $path(test1)
3245    set f [open $path(test1) w]
3246    fconfigure $f -translation lf
3247    puts $f hello\nthere\nand\nhere
3248    close $f
3249    set f [open $path(test1) r]
3250    set l ""
3251    lappend l [gets $f]
3252    lappend l [tell $f]
3253    lappend l [fconfigure $f -translation]
3254    lappend l [gets $f]
3255    lappend l [tell $f]
3256    lappend l [fconfigure $f -translation]
3257    close $f
3258    set l
3259} {hello 6 auto there 12 auto}
3260test io-31.2 {Tcl_Write cr, Tcl_Gets auto} {
3261    file delete $path(test1)
3262    set f [open $path(test1) w]
3263    fconfigure $f -translation cr
3264    puts $f hello\nthere\nand\nhere
3265    close $f
3266    set f [open $path(test1) r]
3267    set l ""
3268    lappend l [gets $f]
3269    lappend l [tell $f]
3270    lappend l [fconfigure $f -translation]
3271    lappend l [gets $f]
3272    lappend l [tell $f]
3273    lappend l [fconfigure $f -translation]
3274    close $f
3275    set l
3276} {hello 6 auto there 12 auto}
3277test io-31.3 {Tcl_Write crlf, Tcl_Gets auto} {
3278    file delete $path(test1)
3279    set f [open $path(test1) w]
3280    fconfigure $f -translation crlf
3281    puts $f hello\nthere\nand\nhere
3282    close $f
3283    set f [open $path(test1) r]
3284    set l ""
3285    lappend l [gets $f]
3286    lappend l [tell $f]
3287    lappend l [fconfigure $f -translation]
3288    lappend l [gets $f]
3289    lappend l [tell $f]
3290    lappend l [fconfigure $f -translation]
3291    close $f
3292    set l
3293} {hello 7 auto there 14 auto}
3294test io-31.4 {Tcl_Write lf, Tcl_Gets lf} {
3295    file delete $path(test1)
3296    set f [open $path(test1) w]
3297    fconfigure $f -translation lf
3298    puts $f hello\nthere\nand\nhere
3299    close $f
3300    set f [open $path(test1) r]
3301    fconfigure $f -translation lf
3302    set l ""
3303    lappend l [gets $f]
3304    lappend l [tell $f]
3305    lappend l [fconfigure $f -translation]
3306    lappend l [gets $f]
3307    lappend l [tell $f]
3308    lappend l [fconfigure $f -translation]
3309    close $f
3310    set l
3311} {hello 6 lf there 12 lf}
3312test io-31.5 {Tcl_Write lf, Tcl_Gets cr} {
3313    file delete $path(test1)
3314    set f [open $path(test1) w]
3315    fconfigure $f -translation lf
3316    puts $f hello\nthere\nand\nhere
3317    close $f
3318    set f [open $path(test1) r]
3319    fconfigure $f -translation cr
3320    set l ""
3321    lappend l [string length [gets $f]]
3322    lappend l [tell $f]
3323    lappend l [fconfigure $f -translation]
3324    lappend l [eof $f]
3325    lappend l [gets $f]
3326    lappend l [tell $f]
3327    lappend l [fconfigure $f -translation]
3328    lappend l [eof $f]
3329    close $f
3330    set l
3331} {21 21 cr 1 {} 21 cr 1}
3332test io-31.6 {Tcl_Write lf, Tcl_Gets crlf} {
3333    file delete $path(test1)
3334    set f [open $path(test1) w]
3335    fconfigure $f -translation lf
3336    puts $f hello\nthere\nand\nhere
3337    close $f
3338    set f [open $path(test1) r]
3339    fconfigure $f -translation crlf
3340    set l ""
3341    lappend l [string length [gets $f]]
3342    lappend l [tell $f]
3343    lappend l [fconfigure $f -translation]
3344    lappend l [eof $f]
3345    lappend l [gets $f]
3346    lappend l [tell $f]
3347    lappend l [fconfigure $f -translation]
3348    lappend l [eof $f]
3349    close $f
3350    set l
3351} {21 21 crlf 1 {} 21 crlf 1}
3352test io-31.7 {Tcl_Write cr, Tcl_Gets cr} {
3353    file delete $path(test1)
3354    set f [open $path(test1) w]
3355    fconfigure $f -translation cr
3356    puts $f hello\nthere\nand\nhere
3357    close $f
3358    set f [open $path(test1) r]
3359    fconfigure $f -translation cr
3360    set l ""
3361    lappend l [gets $f]
3362    lappend l [tell $f]
3363    lappend l [fconfigure $f -translation]
3364    lappend l [eof $f]
3365    lappend l [gets $f]
3366    lappend l [tell $f]
3367    lappend l [fconfigure $f -translation]
3368    lappend l [eof $f]
3369    close $f
3370    set l
3371} {hello 6 cr 0 there 12 cr 0}
3372test io-31.8 {Tcl_Write cr, Tcl_Gets lf} {
3373    file delete $path(test1)
3374    set f [open $path(test1) w]
3375    fconfigure $f -translation cr
3376    puts $f hello\nthere\nand\nhere
3377    close $f
3378    set f [open $path(test1) r]
3379    fconfigure $f -translation lf
3380    set l ""
3381    lappend l [string length [gets $f]]
3382    lappend l [tell $f]
3383    lappend l [fconfigure $f -translation]
3384    lappend l [eof $f]
3385    lappend l [gets $f]
3386    lappend l [tell $f]
3387    lappend l [fconfigure $f -translation]
3388    lappend l [eof $f]
3389    close $f
3390    set l
3391} {21 21 lf 1 {} 21 lf 1}
3392test io-31.9 {Tcl_Write cr, Tcl_Gets crlf} {
3393    file delete $path(test1)
3394    set f [open $path(test1) w]
3395    fconfigure $f -translation cr
3396    puts $f hello\nthere\nand\nhere
3397    close $f
3398    set f [open $path(test1) r]
3399    fconfigure $f -translation crlf
3400    set l ""
3401    lappend l [string length [gets $f]]
3402    lappend l [tell $f]
3403    lappend l [fconfigure $f -translation]
3404    lappend l [eof $f]
3405    lappend l [gets $f]
3406    lappend l [tell $f]
3407    lappend l [fconfigure $f -translation]
3408    lappend l [eof $f]
3409    close $f
3410    set l
3411} {21 21 crlf 1 {} 21 crlf 1}
3412test io-31.10 {Tcl_Write crlf, Tcl_Gets crlf} {
3413    file delete $path(test1)
3414    set f [open $path(test1) w]
3415    fconfigure $f -translation crlf
3416    puts $f hello\nthere\nand\nhere
3417    close $f
3418    set f [open $path(test1) r]
3419    fconfigure $f -translation crlf
3420    set l ""
3421    lappend l [gets $f]
3422    lappend l [tell $f]
3423    lappend l [fconfigure $f -translation]
3424    lappend l [eof $f]
3425    lappend l [gets $f]
3426    lappend l [tell $f]
3427    lappend l [fconfigure $f -translation]
3428    lappend l [eof $f]
3429    close $f
3430    set l
3431} {hello 7 crlf 0 there 14 crlf 0}
3432test io-31.11 {Tcl_Write crlf, Tcl_Gets cr} {
3433    file delete $path(test1)
3434    set f [open $path(test1) w]
3435    fconfigure $f -translation crlf
3436    puts $f hello\nthere\nand\nhere
3437    close $f
3438    set f [open $path(test1) r]
3439    fconfigure $f -translation cr
3440    set l ""
3441    lappend l [gets $f]
3442    lappend l [tell $f]
3443    lappend l [fconfigure $f -translation]
3444    lappend l [eof $f]
3445    lappend l [string length [gets $f]]
3446    lappend l [tell $f]
3447    lappend l [fconfigure $f -translation]
3448    lappend l [eof $f]
3449    close $f
3450    set l
3451} {hello 6 cr 0 6 13 cr 0}
3452test io-31.12 {Tcl_Write crlf, Tcl_Gets lf} {
3453    file delete $path(test1)
3454    set f [open $path(test1) w]
3455    fconfigure $f -translation crlf
3456    puts $f hello\nthere\nand\nhere
3457    close $f
3458    set f [open $path(test1) r]
3459    fconfigure $f -translation lf
3460    set l ""
3461    lappend l [string length [gets $f]]
3462    lappend l [tell $f]
3463    lappend l [fconfigure $f -translation]
3464    lappend l [eof $f]
3465    lappend l [string length [gets $f]]
3466    lappend l [tell $f]
3467    lappend l [fconfigure $f -translation]
3468    lappend l [eof $f]
3469    close $f
3470    set l
3471} {6 7 lf 0 6 14 lf 0}
3472test io-31.13 {binary mode is synonym of lf mode} {
3473    file delete $path(test1)
3474    set f [open $path(test1) w]
3475    fconfigure $f -translation binary
3476    set x [fconfigure $f -translation]
3477    close $f
3478    set x
3479} lf
3480#
3481# Test io-9.14 has been removed because "auto" output translation mode is
3482# not supoprted.
3483#
3484test io-31.14 {Tcl_Write mixed, Tcl_Gets auto} {
3485    file delete $path(test1)
3486    set f [open $path(test1) w]
3487    fconfigure $f -translation lf
3488    puts $f hello\nthere\rand\r\nhere
3489    close $f
3490    set f [open $path(test1) r]
3491    fconfigure $f -translation auto
3492    set l ""
3493    lappend l [gets $f]
3494    lappend l [gets $f]
3495    lappend l [gets $f]
3496    lappend l [gets $f]
3497    lappend l [eof $f]
3498    lappend l [gets $f]
3499    lappend l [eof $f]
3500    close $f
3501    set l
3502} {hello there and here 0 {} 1}
3503test io-31.15 {Tcl_Write mixed, Tcl_Gets auto} {
3504    file delete $path(test1)
3505    set f [open $path(test1) w]
3506    fconfigure $f -translation lf
3507    puts -nonewline $f hello\nthere\rand\r\nhere\r
3508    close $f
3509    set f [open $path(test1) r]
3510    fconfigure $f -translation auto
3511    set l ""
3512    lappend l [gets $f]
3513    lappend l [gets $f]
3514    lappend l [gets $f]
3515    lappend l [gets $f]
3516    lappend l [eof $f]
3517    lappend l [gets $f]
3518    lappend l [eof $f]
3519    close $f
3520    set l
3521} {hello there and here 0 {} 1}
3522test io-31.16 {Tcl_Write mixed, Tcl_Gets auto} {
3523    file delete $path(test1)
3524    set f [open $path(test1) w]
3525    fconfigure $f -translation lf
3526    puts -nonewline $f hello\nthere\rand\r\nhere\n
3527    close $f
3528    set f [open $path(test1) r]
3529    set l ""
3530    lappend l [gets $f]
3531    lappend l [gets $f]
3532    lappend l [gets $f]
3533    lappend l [gets $f]
3534    lappend l [eof $f]
3535    lappend l [gets $f]
3536    lappend l [eof $f]
3537    close $f
3538    set l
3539} {hello there and here 0 {} 1}
3540test io-31.17 {Tcl_Write mixed, Tcl_Gets auto} {
3541    file delete $path(test1)
3542    set f [open $path(test1) w]
3543    fconfigure $f -translation lf
3544    puts -nonewline $f hello\nthere\rand\r\nhere\r\n
3545    close $f
3546    set f [open $path(test1) r]
3547    fconfigure $f -translation auto
3548    set l ""
3549    lappend l [gets $f]
3550    lappend l [gets $f]
3551    lappend l [gets $f]
3552    lappend l [gets $f]
3553    lappend l [eof $f]
3554    lappend l [gets $f]
3555    lappend l [eof $f]
3556    close $f
3557    set l
3558} {hello there and here 0 {} 1}
3559test io-31.18 {Tcl_Write ^Z at end, Tcl_Gets auto} {
3560    file delete $path(test1)
3561    set f [open $path(test1) w]
3562    fconfigure $f -translation lf
3563    set s [format "hello\nthere\nand\rhere\n\%c" 26]
3564    puts $f $s
3565    close $f
3566    set f [open $path(test1) r]
3567    fconfigure $f -eofchar \x1a -translation auto
3568    set l ""
3569    lappend l [gets $f]
3570    lappend l [gets $f]
3571    lappend l [gets $f]
3572    lappend l [gets $f]
3573    lappend l [eof $f]
3574    lappend l [gets $f]
3575    lappend l [eof $f]
3576    close $f
3577    set l
3578} {hello there and here 0 {} 1}
3579test io-31.19 {Tcl_Write, implicit ^Z at end, Tcl_Gets auto} {
3580    file delete $path(test1)
3581    set f [open $path(test1) w]
3582    fconfigure $f -eofchar \x1a -translation lf
3583    puts $f hello\nthere\nand\rhere
3584    close $f
3585    set f [open $path(test1) r]
3586    fconfigure $f -eofchar \x1a -translation auto
3587    set l ""
3588    lappend l [gets $f]
3589    lappend l [gets $f]
3590    lappend l [gets $f]
3591    lappend l [gets $f]
3592    lappend l [eof $f]
3593    lappend l [gets $f]
3594    lappend l [eof $f]
3595    close $f
3596    set l
3597} {hello there and here 0 {} 1}
3598test io-31.20 {Tcl_Write, ^Z in middle, Tcl_Gets auto, eofChar} {
3599    file delete $path(test1)
3600    set f [open $path(test1) w]
3601    fconfigure $f -translation lf
3602    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3603    puts $f $s
3604    close $f
3605    set f [open $path(test1) r]
3606    fconfigure $f -eofchar \x1a
3607    fconfigure $f -translation auto
3608    set l ""
3609    lappend l [gets $f]
3610    lappend l [gets $f]
3611    lappend l [eof $f]
3612    lappend l [gets $f]
3613    lappend l [eof $f]
3614    close $f
3615    set l
3616} {abc def 0 {} 1}
3617test io-31.21 {Tcl_Write, no newline ^Z in middle, Tcl_Gets auto, eofChar} {
3618    file delete $path(test1)
3619    set f [open $path(test1) w]
3620    fconfigure $f -translation lf
3621    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3622    puts $f $s
3623    close $f
3624    set f [open $path(test1) r]
3625    fconfigure $f -eofchar \x1a -translation auto
3626    set l ""
3627    lappend l [gets $f]
3628    lappend l [gets $f]
3629    lappend l [eof $f]
3630    lappend l [gets $f]
3631    lappend l [eof $f]
3632    close $f
3633    set l
3634} {abc def 0 {} 1}
3635test io-31.22 {Tcl_Write, ^Z in middle ignored, Tcl_Gets lf} {
3636    file delete $path(test1)
3637    set f [open $path(test1) w]
3638    fconfigure $f -translation lf -eofchar {}
3639    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3640    puts $f $s
3641    close $f
3642    set f [open $path(test1) r]
3643    fconfigure $f -translation lf -eofchar {}
3644    set l ""
3645    lappend l [gets $f]
3646    lappend l [gets $f]
3647    lappend l [eof $f]
3648    lappend l [gets $f]
3649    lappend l [eof $f]
3650    lappend l [gets $f]
3651    lappend l [eof $f]
3652    lappend l [gets $f]
3653    lappend l [eof $f]
3654    close $f
3655    set l
3656} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
3657test io-31.23 {Tcl_Write, ^Z in middle ignored, Tcl_Gets cr} {
3658    file delete $path(test1)
3659    set f [open $path(test1) w]
3660    fconfigure $f -translation cr -eofchar {}
3661    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3662    puts $f $s
3663    close $f
3664    set f [open $path(test1) r]
3665    fconfigure $f -translation cr -eofchar {}
3666    set l ""
3667    lappend l [gets $f]
3668    lappend l [gets $f]
3669    lappend l [eof $f]
3670    lappend l [gets $f]
3671    lappend l [eof $f]
3672    lappend l [gets $f]
3673    lappend l [eof $f]
3674    lappend l [gets $f]
3675    lappend l [eof $f]
3676    close $f
3677    set l
3678} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
3679test io-31.24 {Tcl_Write, ^Z in middle ignored, Tcl_Gets crlf} {
3680    file delete $path(test1)
3681    set f [open $path(test1) w]
3682    fconfigure $f -translation crlf -eofchar {}
3683    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3684    puts $f $s
3685    close $f
3686    set f [open $path(test1) r]
3687    fconfigure $f -translation crlf -eofchar {}
3688    set l ""
3689    lappend l [gets $f]
3690    lappend l [gets $f]
3691    lappend l [eof $f]
3692    lappend l [gets $f]
3693    lappend l [eof $f]
3694    lappend l [gets $f]
3695    lappend l [eof $f]
3696    lappend l [gets $f]
3697    lappend l [eof $f]
3698    close $f
3699    set l
3700} "abc def 0 \x1aqrs 0 tuv 0 {} 1"
3701test io-31.25 {Tcl_Write lf, ^Z in middle, Tcl_Gets auto} {
3702    file delete $path(test1)
3703    set f [open $path(test1) w]
3704    fconfigure $f -translation lf
3705    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3706    puts $f $s
3707    close $f
3708    set f [open $path(test1) r]
3709    fconfigure $f -translation auto -eofchar \x1a
3710    set l ""
3711    lappend l [gets $f]
3712    lappend l [gets $f]
3713    lappend l [eof $f]
3714    lappend l [gets $f]
3715    lappend l [eof $f]
3716    close $f
3717    set l
3718} {abc def 0 {} 1}
3719test io-31.26 {Tcl_Write lf, ^Z in middle, Tcl_Gets lf} {
3720    file delete $path(test1)
3721    set f [open $path(test1) w]
3722    fconfigure $f -translation lf
3723    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3724    puts $f $s
3725    close $f
3726    set f [open $path(test1) r]
3727    fconfigure $f -translation lf -eofchar \x1a
3728    set l ""
3729    lappend l [gets $f]
3730    lappend l [gets $f]
3731    lappend l [eof $f]
3732    lappend l [gets $f]
3733    lappend l [eof $f]
3734    close $f
3735    set l
3736} {abc def 0 {} 1}
3737test io-31.27 {Tcl_Write cr, ^Z in middle, Tcl_Gets auto} {
3738    file delete $path(test1)
3739    set f [open $path(test1) w]
3740    fconfigure $f -translation cr -eofchar {}
3741    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3742    puts $f $s
3743    close $f
3744    set f [open $path(test1) r]
3745    fconfigure $f -translation auto -eofchar \x1a
3746    set l ""
3747    lappend l [gets $f]
3748    lappend l [gets $f]
3749    lappend l [eof $f]
3750    lappend l [gets $f]
3751    lappend l [eof $f]
3752    close $f
3753    set l
3754} {abc def 0 {} 1}
3755test io-31.28 {Tcl_Write cr, ^Z in middle, Tcl_Gets cr} {
3756    file delete $path(test1)
3757    set f [open $path(test1) w]
3758    fconfigure $f -translation cr -eofchar {}
3759    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3760    puts $f $s
3761    close $f
3762    set f [open $path(test1) r]
3763    fconfigure $f -translation cr -eofchar \x1a
3764    set l ""
3765    lappend l [gets $f]
3766    lappend l [gets $f]
3767    lappend l [eof $f]
3768    lappend l [gets $f]
3769    lappend l [eof $f]
3770    close $f
3771    set l
3772} {abc def 0 {} 1}
3773test io-31.29 {Tcl_Write crlf, ^Z in middle, Tcl_Gets auto} {
3774    file delete $path(test1)
3775    set f [open $path(test1) w]
3776    fconfigure $f -translation crlf -eofchar {}
3777    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3778    puts $f $s
3779    close $f
3780    set f [open $path(test1) r]
3781    fconfigure $f -translation auto -eofchar \x1a
3782    set l ""
3783    lappend l [gets $f]
3784    lappend l [gets $f]
3785    lappend l [eof $f]
3786    lappend l [gets $f]
3787    lappend l [eof $f]
3788    close $f
3789    set l
3790} {abc def 0 {} 1}
3791test io-31.30 {Tcl_Write crlf, ^Z in middle, Tcl_Gets crlf} {
3792    file delete $path(test1)
3793    set f [open $path(test1) w]
3794    fconfigure $f -translation crlf -eofchar {}
3795    set s [format "abc\ndef\n%cqrs\ntuv" 26]
3796    puts $f $s
3797    close $f
3798    set f [open $path(test1) r]
3799    fconfigure $f -translation crlf -eofchar \x1a
3800    set l ""
3801    lappend l [gets $f]
3802    lappend l [gets $f]
3803    lappend l [eof $f]
3804    lappend l [gets $f]
3805    lappend l [eof $f]
3806    close $f
3807    set l
3808} {abc def 0 {} 1}
3809test io-31.31 {Tcl_Write crlf on block boundary, Tcl_Gets crlf} {
3810    file delete $path(test1)
3811    set f [open $path(test1) w]
3812    fconfigure $f -translation crlf
3813    set line "123456789ABCDE"   ;# 14 char plus crlf
3814    puts -nonewline $f x        ;# shift crlf across block boundary
3815    for {set i 0} {$i < 700} {incr i} {
3816        puts $f $line
3817    }
3818    close $f
3819    set f [open $path(test1) r]
3820    fconfigure $f -translation crlf
3821    set c ""
3822    while {[gets $f line] >= 0} {
3823        append c $line\n
3824    }
3825    close $f
3826    string length $c
3827} [expr 700*15+1]
3828test io-31.32 {Tcl_Write crlf on block boundary, Tcl_Gets auto} {
3829    file delete $path(test1)
3830    set f [open $path(test1) w]
3831    fconfigure $f -translation crlf
3832    set line "123456789ABCDE"   ;# 14 char plus crlf
3833    puts -nonewline $f x        ;# shift crlf across block boundary
3834    for {set i 0} {$i < 700} {incr i} {
3835        puts $f $line
3836    }
3837    close $f
3838    set f [open $path(test1) r]
3839    fconfigure $f -translation auto
3840    set c ""
3841    while {[gets $f line] >= 0} {
3842        append c $line\n
3843    }
3844    close $f
3845    string length $c
3846} [expr 700*15+1]
3847
3848# Test Tcl_Read and buffering.
3849
3850test io-32.1 {Tcl_Read, channel not readable} {
3851    list [catch {read stdout} msg] $msg
3852} {1 {channel "stdout" wasn't opened for reading}}
3853test io-32.2 {Tcl_Read, zero byte count} {
3854    read stdin 0
3855} ""
3856test io-32.3 {Tcl_Read, negative byte count} {
3857    set f [open $path(longfile) r]
3858    set l [list [catch {read $f -1} msg] $msg]
3859    close $f
3860    set l
3861} {1 {bad argument "-1": should be "nonewline"}}
3862test io-32.4 {Tcl_Read, positive byte count} {
3863    set f [open $path(longfile) r]
3864    set x [read $f 1024]
3865    set s [string length $x]
3866    unset x
3867    close $f
3868    set s
3869} 1024
3870test io-32.5 {Tcl_Read, multiple buffers} {
3871    set f [open $path(longfile) r]
3872    fconfigure $f -buffersize 100
3873    set x [read $f 1024]
3874    set s [string length $x]
3875    unset x
3876    close $f
3877    set s
3878} 1024
3879test io-32.6 {Tcl_Read, very large read} {
3880    set f1 [open $path(longfile) r]
3881    set z [read $f1 1000000]
3882    close $f1
3883    set l [string length $z]
3884    set x ok
3885    set z [file size $path(longfile)]
3886    if {$z != $l} {
3887        set x broken
3888    }
3889    set x
3890} ok
3891test io-32.7 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
3892    set f1 [open $path(longfile) r]
3893    fconfigure $f1 -blocking off
3894    set z [read $f1 20]
3895    close $f1
3896    set l [string length $z]
3897    set x ok
3898    if {$l != 20} {
3899        set x broken
3900    }
3901    set x
3902} ok
3903test io-32.8 {Tcl_Read, nonblocking, file} {nonBlockFiles} {
3904    set f1 [open $path(longfile) r]
3905    fconfigure $f1 -blocking off
3906    set z [read $f1 1000000]
3907    close $f1
3908    set x ok
3909    set l [string length $z]
3910    set z [file size $path(longfile)]
3911    if {$z != $l} {
3912        set x broken
3913    }
3914    set x
3915} ok
3916test io-32.9 {Tcl_Read, read to end of file} {
3917    set f1 [open $path(longfile) r]
3918    set z [read $f1]
3919    close $f1
3920    set l [string length $z]
3921    set x ok
3922    set z [file size $path(longfile)]
3923    if {$z != $l} {
3924        set x broken
3925    }
3926    set x
3927} ok
3928test io-32.10 {Tcl_Read from a pipe} {stdio openpipe} {
3929    file delete $path(pipe)
3930    set f1 [open $path(pipe) w]
3931    puts $f1 {puts [gets stdin]}
3932    close $f1
3933    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
3934    puts $f1 hello
3935    flush $f1
3936    set x [read $f1]
3937    close $f1
3938    set x
3939} "hello\n"
3940test io-32.11 {Tcl_Read from a pipe} {stdio openpipe} {
3941    file delete $path(pipe)
3942    set f1 [open $path(pipe) w]
3943    puts $f1 {puts [gets stdin]}
3944    puts $f1 {puts [gets stdin]}
3945    close $f1
3946    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
3947    puts $f1 hello
3948    flush $f1
3949    set x ""
3950    lappend x [read $f1 6]
3951    puts $f1 hello
3952    flush $f1
3953    lappend x [read $f1]
3954    close $f1
3955    set x
3956} {{hello
3957} {hello
3958}}
3959test io-32.12 {Tcl_Read, -nonewline} {
3960    file delete $path(test1)
3961    set f1 [open $path(test1) w]
3962    puts $f1 hello
3963    puts $f1 bye
3964    close $f1
3965    set f1 [open $path(test1) r]
3966    set c [read -nonewline $f1]
3967    close $f1
3968    set c
3969} {hello
3970bye}
3971test io-32.13 {Tcl_Read, -nonewline} {
3972    file delete $path(test1)
3973    set f1 [open $path(test1) w]
3974    puts $f1 hello
3975    puts $f1 bye
3976    close $f1
3977    set f1 [open $path(test1) r]
3978    set c [read -nonewline $f1]
3979    close $f1
3980    list [string length $c] $c
3981} {9 {hello
3982bye}}
3983test io-32.14 {Tcl_Read, reading in small chunks} {
3984    file delete $path(test1)
3985    set f [open $path(test1) w]
3986    puts $f "Two lines: this one"
3987    puts $f "and this one"
3988    close $f
3989    set f [open $path(test1)]
3990    set x [list [read $f 1] [read $f 2] [read $f]]
3991    close $f
3992    set x
3993} {T wo { lines: this one
3994and this one
3995}}
3996test io-32.15 {Tcl_Read, asking for more input than available} {
3997    file delete $path(test1)
3998    set f [open $path(test1) w]
3999    puts $f "Two lines: this one"
4000    puts $f "and this one"
4001    close $f
4002    set f [open $path(test1)]
4003    set x [read $f 100]
4004    close $f
4005    set x
4006} {Two lines: this one
4007and this one
4008}
4009test io-32.16 {Tcl_Read, read to end of file with -nonewline} {
4010    file delete $path(test1)
4011    set f [open $path(test1) w]
4012    puts $f "Two lines: this one"
4013    puts $f "and this one"
4014    close $f
4015    set f [open $path(test1)]
4016    set x [read -nonewline $f]
4017    close $f
4018    set x
4019} {Two lines: this one
4020and this one}
4021
4022# Test Tcl_Gets.
4023
4024test io-33.1 {Tcl_Gets, reading what was written} {
4025    file delete $path(test1)
4026    set f1 [open $path(test1) w]
4027    set y "first line"
4028    puts $f1 $y
4029    close $f1
4030    set f1 [open $path(test1) r]
4031    set x [gets $f1]
4032    set z ok
4033    if {"$x" != "$y"} {
4034        set z broken
4035    }
4036    close $f1
4037    set z
4038} ok
4039test io-33.2 {Tcl_Gets into variable} {
4040    set f1 [open $path(longfile) r]
4041    set c [gets $f1 x]
4042    set l [string length x]
4043    set z ok
4044    if {$l != $l} {
4045        set z broken
4046    }
4047    close $f1
4048    set z
4049} ok
4050test io-33.3 {Tcl_Gets from pipe} {stdio openpipe} {
4051    file delete $path(pipe)
4052    set f1 [open $path(pipe) w]
4053    puts $f1 {puts [gets stdin]}
4054    close $f1
4055    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4056    puts $f1 hello
4057    flush $f1
4058    set x [gets $f1]
4059    close $f1
4060    set z ok
4061    if {"$x" != "hello"} {
4062        set z broken
4063    }
4064    set z
4065} ok
4066test io-33.4 {Tcl_Gets with long line} {
4067    file delete $path(test3)
4068    set f [open $path(test3) w]
4069    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4070    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4071    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4072    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4073    puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4074    close $f
4075    set f [open $path(test3)]
4076    set x [gets $f]
4077    close $f
4078    set x
4079} {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
4080test io-33.5 {Tcl_Gets with long line} {
4081    set f [open $path(test3)]
4082    set x [gets $f y]
4083    close $f
4084    list $x $y
4085} {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
4086test io-33.6 {Tcl_Gets and end of file} {
4087    file delete $path(test3)
4088    set f [open $path(test3) w]
4089    puts -nonewline $f "Test1\nTest2"
4090    close $f
4091    set f [open $path(test3)]
4092    set x {}
4093    set y {}
4094    lappend x [gets $f y] $y
4095    set y {}
4096    lappend x [gets $f y] $y
4097    set y {}
4098    lappend x [gets $f y] $y
4099    close $f
4100    set x
4101} {5 Test1 5 Test2 -1 {}}
4102test io-33.7 {Tcl_Gets and bad variable} {
4103    set f [open $path(test3) w]
4104    puts $f "Line 1"
4105    puts $f "Line 2"
4106    close $f
4107    catch {unset x}
4108    set x 24
4109    set f [open $path(test3) r]
4110    set result [list [catch {gets $f x(0)} msg] $msg]
4111    close $f
4112    set result
4113} {1 {can't set "x(0)": variable isn't array}}
4114test io-33.8 {Tcl_Gets, exercising double buffering} {
4115    set f [open $path(test3) w]
4116    fconfigure $f -translation lf -eofchar {}
4117    set x ""
4118    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4119    for {set y 0} {$y < 100} {incr y} {puts $f $x}
4120    close $f
4121    set f [open $path(test3) r]
4122    fconfigure $f -translation lf
4123    for {set y 0} {$y < 100} {incr y} {gets $f}
4124    close $f
4125    set y
4126} 100
4127test io-33.9 {Tcl_Gets, exercising double buffering} {
4128    set f [open $path(test3) w]
4129    fconfigure $f -translation lf -eofchar {}
4130    set x ""
4131    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4132    for {set y 0} {$y < 200} {incr y} {puts $f $x}
4133    close $f
4134    set f [open $path(test3) r]
4135    fconfigure $f -translation lf
4136    for {set y 0} {$y < 200} {incr y} {gets $f}
4137    close $f
4138    set y
4139} 200
4140test io-33.10 {Tcl_Gets, exercising double buffering} {
4141    set f [open $path(test3) w]
4142    fconfigure $f -translation lf -eofchar {}
4143    set x ""
4144    for {set y 0} {$y < 99} {incr y} {set x "a$x"}
4145    for {set y 0} {$y < 300} {incr y} {puts $f $x}
4146    close $f
4147    set f [open $path(test3) r]
4148    fconfigure $f -translation lf
4149    for {set y 0} {$y < 300} {incr y} {gets $f}
4150    close $f
4151    set y
4152} 300
4153
4154# Test Tcl_Seek and Tcl_Tell.
4155
4156test io-34.1 {Tcl_Seek to current position at start of file} {
4157    set f1 [open $path(longfile) r]
4158    seek $f1 0 current
4159    set c [tell $f1]
4160    close $f1
4161    set c
4162} 0
4163test io-34.2 {Tcl_Seek to offset from start} {
4164    file delete $path(test1)
4165    set f1 [open $path(test1) w]
4166    fconfigure $f1 -translation lf -eofchar {}
4167    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4168    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4169    close $f1
4170    set f1 [open $path(test1) r]
4171    seek $f1 10 start
4172    set c [tell $f1]
4173    close $f1
4174    set c
4175} 10
4176test io-34.3 {Tcl_Seek to end of file} {
4177    file delete $path(test1)
4178    set f1 [open $path(test1) w]
4179    fconfigure $f1 -translation lf -eofchar {}
4180    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4181    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4182    close $f1
4183    set f1 [open $path(test1) r]
4184    seek $f1 0 end
4185    set c [tell $f1]
4186    close $f1
4187    set c
4188} 54
4189test io-34.4 {Tcl_Seek to offset from end of file} {
4190    file delete $path(test1)
4191    set f1 [open $path(test1) w]
4192    fconfigure $f1 -translation lf -eofchar {}
4193    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4194    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4195    close $f1
4196    set f1 [open $path(test1) r]
4197    seek $f1 -10 end
4198    set c [tell $f1]
4199    close $f1
4200    set c
4201} 44
4202test io-34.5 {Tcl_Seek to offset from current position} {
4203    file delete $path(test1)
4204    set f1 [open $path(test1) w]
4205    fconfigure $f1 -translation lf -eofchar {}
4206    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4207    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4208    close $f1
4209    set f1 [open $path(test1) r]
4210    seek $f1 10 current
4211    seek $f1 10 current
4212    set c [tell $f1]
4213    close $f1
4214    set c
4215} 20
4216test io-34.6 {Tcl_Seek to offset from end of file} {
4217    file delete $path(test1)
4218    set f1 [open $path(test1) w]
4219    fconfigure $f1 -translation lf -eofchar {}
4220    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4221    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4222    close $f1
4223    set f1 [open $path(test1) r]
4224    seek $f1 -10 end
4225    set c [tell $f1]
4226    set r [read $f1]
4227    close $f1
4228    list $c $r
4229} {44 {rstuvwxyz
4230}}
4231test io-34.7 {Tcl_Seek to offset from end of file, then to current position} {
4232    file delete $path(test1)
4233    set f1 [open $path(test1) w]
4234    fconfigure $f1 -translation lf -eofchar {}
4235    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4236    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4237    close $f1
4238    set f1 [open $path(test1) r]
4239    seek $f1 -10 end
4240    set c1 [tell $f1]
4241    set r1 [read $f1 5]
4242    seek $f1 0 current
4243    set c2 [tell $f1]
4244    close $f1
4245    list $c1 $r1 $c2
4246} {44 rstuv 49}
4247test io-34.8 {Tcl_Seek on pipes: not supported} {stdio openpipe} {
4248    set f1 [open "|[list [interpreter]]" r+]
4249    set x [list [catch {seek $f1 0 current} msg] $msg]
4250    close $f1
4251    regsub {".*":} $x {"":} x
4252    string tolower $x
4253} {1 {error during seek on "": invalid argument}}
4254test io-34.9 {Tcl_Seek, testing buffered input flushing} {
4255    file delete $path(test3)
4256    set f [open $path(test3) w]
4257    fconfigure $f -eofchar {}
4258    puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
4259    close $f
4260    set f [open $path(test3) RDWR]
4261    set x [read $f 1]
4262    seek $f 3
4263    lappend x [read $f 1]
4264    seek $f 0 start
4265    lappend x [read $f 1]
4266    seek $f 10 current
4267    lappend x [read $f 1]
4268    seek $f -2 end
4269    lappend x [read $f 1]
4270    seek $f 50 end
4271    lappend x [read $f 1]
4272    seek $f 1
4273    lappend x [read $f 1]
4274    close $f
4275    set x
4276} {a d a l Y {} b}
4277set path(test3) [makeFile {} test3]
4278test io-34.10 {Tcl_Seek testing flushing of buffered input} {
4279    set f [open $path(test3) w]
4280    fconfigure $f -translation lf
4281    puts $f xyz\n123
4282    close $f
4283    set f [open $path(test3) r+]
4284    fconfigure $f -translation lf
4285    set x [gets $f]
4286    seek $f 0 current
4287    puts $f 456
4288    close $f
4289    list $x [viewFile test3]
4290} "xyz {xyz
4291456}"
4292test io-34.11 {Tcl_Seek testing flushing of buffered output} {
4293    set f [open $path(test3) w]
4294    puts $f xyz\n123
4295    close $f
4296    set f [open $path(test3) w+]
4297    puts $f xyzzy
4298    seek $f 2
4299    set x [gets $f]
4300    close $f
4301    list $x [viewFile test3]
4302} "zzy xyzzy"
4303test io-34.12 {Tcl_Seek testing combination of write, seek back and read} {
4304    set f [open $path(test3) w]
4305    fconfigure $f -translation lf -eofchar {}
4306    puts $f xyz\n123
4307    close $f
4308    set f [open $path(test3) a+]
4309    fconfigure $f -translation lf -eofchar {}
4310    puts $f xyzzy
4311    flush $f
4312    set x [tell $f]
4313    seek $f -4 cur
4314    set y [gets $f]
4315    close $f
4316    list $x [viewFile test3] $y
4317} {14 {xyz
4318123
4319xyzzy} zzy}
4320test io-34.13 {Tcl_Tell at start of file} {
4321    file delete $path(test1)
4322    set f1 [open $path(test1) w]
4323    set p [tell $f1]
4324    close $f1
4325    set p
4326} 0
4327test io-34.14 {Tcl_Tell after seek to end of file} {
4328    file delete $path(test1)
4329    set f1 [open $path(test1) w]
4330    fconfigure $f1 -translation lf -eofchar {}
4331    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4332    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4333    close $f1
4334    set f1 [open $path(test1) r]
4335    seek $f1 0 end
4336    set c1 [tell $f1]
4337    close $f1
4338    set c1
4339} 54
4340test io-34.15 {Tcl_Tell combined with seeking} {
4341    file delete $path(test1)
4342    set f1 [open $path(test1) w]
4343    fconfigure $f1 -translation lf -eofchar {}
4344    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4345    puts $f1 "abcdefghijklmnopqrstuvwxyz"
4346    close $f1
4347    set f1 [open $path(test1) r]
4348    seek $f1 10 start
4349    set c1 [tell $f1]
4350    seek $f1 10 current
4351    set c2 [tell $f1]
4352    close $f1
4353    list $c1 $c2
4354} {10 20}
4355test io-34.16 {Tcl_Tell on pipe: always -1} {stdio openpipe} {
4356    set f1 [open "|[list [interpreter]]" r+]
4357    set c [tell $f1]
4358    close $f1
4359    set c
4360} -1
4361test io-34.17 {Tcl_Tell on pipe: always -1} {stdio openpipe} {
4362    set f1 [open "|[list [interpreter]]" r+]
4363    puts $f1 {puts hello}
4364    flush $f1
4365    set c [tell $f1]
4366    gets $f1
4367    close $f1
4368    set c
4369} -1
4370test io-34.18 {Tcl_Tell combined with seeking and reading} {
4371    file delete $path(test2)
4372    set f [open $path(test2) w]
4373    fconfigure $f -translation lf -eofchar {}
4374    puts -nonewline $f "line1\nline2\nline3\nline4\nline5\n"
4375    close $f
4376    set f [open $path(test2)]
4377    fconfigure $f -translation lf
4378    set x [tell $f]
4379    read $f 3
4380    lappend x [tell $f]
4381    seek $f 2
4382    lappend x [tell $f]
4383    seek $f 10 current
4384    lappend x [tell $f]
4385    seek $f 0 end
4386    lappend x [tell $f]
4387    close $f
4388    set x
4389} {0 3 2 12 30}
4390test io-34.19 {Tcl_Tell combined with opening in append mode} {
4391    set f [open $path(test3) w]
4392    fconfigure $f -translation lf -eofchar {}
4393    puts $f "abcdefghijklmnopqrstuvwxyz"
4394    puts $f "abcdefghijklmnopqrstuvwxyz"
4395    close $f
4396    set f [open $path(test3) a]
4397    set c [tell $f]
4398    close $f
4399    set c
4400} 54
4401test io-34.20 {Tcl_Tell combined with writing} {
4402    set f [open $path(test3) w]
4403    set l ""
4404    seek $f 29 start
4405    lappend l [tell $f]
4406    puts -nonewline $f a
4407    seek $f 39 start
4408    lappend l [tell $f]
4409    puts -nonewline $f a
4410    lappend l [tell $f]
4411    seek $f 407 end
4412    lappend l [tell $f]
4413    close $f
4414    set l
4415} {29 39 40 447}
4416test io-34.21 {Tcl_Seek and Tcl_Tell on large files} {largefileSupport} {
4417    file delete $path(test3)
4418    set f [open $path(test3) w]
4419    fconfigure $f -encoding binary
4420    set l ""
4421    lappend l [tell $f]
4422    puts -nonewline $f abcdef
4423    lappend l [tell $f]
4424    flush $f
4425    lappend l [tell $f]
4426    # 4GB offset!
4427    seek $f 0x100000000
4428    lappend l [tell $f]
4429    puts -nonewline $f abcdef
4430    lappend l [tell $f]
4431    close $f
4432    lappend l [file size $f]
4433    # truncate...
4434    close [open $path(test3) w]
4435    lappend l [file size $f]
4436    set l
4437} {0 6 6 4294967296 4294967302 4294967302 0}
4438
4439# Test Tcl_Eof
4440
4441test io-35.1 {Tcl_Eof} {
4442    file delete $path(test1)
4443    set f [open $path(test1) w]
4444    puts $f hello
4445    puts $f hello
4446    close $f
4447    set f [open $path(test1)]
4448    set x [eof $f]
4449    lappend x [eof $f]
4450    gets $f
4451    lappend x [eof $f]
4452    gets $f
4453    lappend x [eof $f]
4454    gets $f
4455    lappend x [eof $f]
4456    lappend x [eof $f]
4457    close $f
4458    set x
4459} {0 0 0 0 1 1}
4460test io-35.2 {Tcl_Eof with pipe} {stdio openpipe} {
4461    file delete $path(pipe)
4462    set f1 [open $path(pipe) w]
4463    puts $f1 {gets stdin}
4464    puts $f1 {puts hello}
4465    close $f1
4466    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4467    puts $f1 hello
4468    set x [eof $f1]
4469    flush $f1
4470    lappend x [eof $f1]
4471    gets $f1
4472    lappend x [eof $f1]
4473    gets $f1
4474    lappend x [eof $f1]
4475    close $f1
4476    set x
4477} {0 0 0 1}
4478test io-35.3 {Tcl_Eof with pipe} {stdio openpipe} {
4479    file delete $path(pipe)
4480    set f1 [open $path(pipe) w]
4481    puts $f1 {gets stdin}
4482    puts $f1 {puts hello}
4483    close $f1
4484    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4485    puts $f1 hello
4486    set x [eof $f1]
4487    flush $f1
4488    lappend x [eof $f1]
4489    gets $f1
4490    lappend x [eof $f1]
4491    gets $f1
4492    lappend x [eof $f1]
4493    gets $f1
4494    lappend x [eof $f1]
4495    gets $f1
4496    lappend x [eof $f1]
4497    close $f1
4498    set x
4499} {0 0 0 1 1 1}
4500test io-35.4 {Tcl_Eof, eof detection on nonblocking file} {nonBlockFiles} {
4501    file delete $path(test1)
4502    set f [open $path(test1) w]
4503    close $f
4504    set f [open $path(test1) r]
4505    fconfigure $f -blocking off
4506    set l ""
4507    lappend l [gets $f]
4508    lappend l [eof $f]
4509    close $f
4510    set l
4511} {{} 1}
4512test io-35.5 {Tcl_Eof, eof detection on nonblocking pipe} {stdio openpipe} {
4513    file delete $path(pipe)
4514    set f [open $path(pipe) w]
4515    puts $f {
4516        exit
4517    }
4518    close $f
4519    set f [open "|[list [interpreter] $path(pipe)]" r]
4520    set l ""
4521    lappend l [gets $f]
4522    lappend l [eof $f]
4523    close $f
4524    set l
4525} {{} 1}
4526test io-35.6 {Tcl_Eof, eof char, lf write, auto read} {
4527    file delete $path(test1)
4528    set f [open $path(test1) w]
4529    fconfigure $f -translation lf -eofchar \x1a
4530    puts $f abc\ndef
4531    close $f
4532    set s [file size $path(test1)]
4533    set f [open $path(test1) r]
4534    fconfigure $f -translation auto -eofchar \x1a
4535    set l [string length [read $f]]
4536    set e [eof $f]
4537    close $f
4538    list $s $l $e
4539} {9 8 1}
4540test io-35.7 {Tcl_Eof, eof char, lf write, lf read} {
4541    file delete $path(test1)
4542    set f [open $path(test1) w]
4543    fconfigure $f -translation lf -eofchar \x1a
4544    puts $f abc\ndef
4545    close $f
4546    set s [file size $path(test1)]
4547    set f [open $path(test1) r]
4548    fconfigure $f -translation lf -eofchar \x1a
4549    set l [string length [read $f]]
4550    set e [eof $f]
4551    close $f
4552    list $s $l $e
4553} {9 8 1}
4554test io-35.8 {Tcl_Eof, eof char, cr write, auto read} {
4555    file delete $path(test1)
4556    set f [open $path(test1) w]
4557    fconfigure $f -translation cr -eofchar \x1a
4558    puts $f abc\ndef
4559    close $f
4560    set s [file size $path(test1)]
4561    set f [open $path(test1) r]
4562    fconfigure $f -translation auto -eofchar \x1a
4563    set l [string length [read $f]]
4564    set e [eof $f]
4565    close $f
4566    list $s $l $e
4567} {9 8 1}
4568test io-35.9 {Tcl_Eof, eof char, cr write, cr read} {
4569    file delete $path(test1)
4570    set f [open $path(test1) w]
4571    fconfigure $f -translation cr -eofchar \x1a
4572    puts $f abc\ndef
4573    close $f
4574    set s [file size $path(test1)]
4575    set f [open $path(test1) r]
4576    fconfigure $f -translation cr -eofchar \x1a
4577    set l [string length [read $f]]
4578    set e [eof $f]
4579    close $f
4580    list $s $l $e
4581} {9 8 1}
4582test io-35.10 {Tcl_Eof, eof char, crlf write, auto read} {
4583    file delete $path(test1)
4584    set f [open $path(test1) w]
4585    fconfigure $f -translation crlf -eofchar \x1a
4586    puts $f abc\ndef
4587    close $f
4588    set s [file size $path(test1)]
4589    set f [open $path(test1) r]
4590    fconfigure $f -translation auto -eofchar \x1a
4591    set l [string length [read $f]]
4592    set e [eof $f]
4593    close $f
4594    list $s $l $e
4595} {11 8 1}
4596test io-35.11 {Tcl_Eof, eof char, crlf write, crlf read} {
4597    file delete $path(test1)
4598    set f [open $path(test1) w]
4599    fconfigure $f -translation crlf -eofchar \x1a
4600    puts $f abc\ndef
4601    close $f
4602    set s [file size $path(test1)]
4603    set f [open $path(test1) r]
4604    fconfigure $f -translation crlf -eofchar \x1a
4605    set l [string length [read $f]]
4606    set e [eof $f]
4607    close $f
4608    list $s $l $e
4609} {11 8 1}
4610test io-35.12 {Tcl_Eof, eof char in middle, lf write, auto read} {
4611    file delete $path(test1)
4612    set f [open $path(test1) w]
4613    fconfigure $f -translation lf -eofchar {}
4614    set i [format abc\ndef\n%cqrs\nuvw 26]
4615    puts $f $i
4616    close $f
4617    set c [file size $path(test1)]
4618    set f [open $path(test1) r]
4619    fconfigure $f -translation auto -eofchar \x1a
4620    set l [string length [read $f]]
4621    set e [eof $f]
4622    close $f
4623    list $c $l $e
4624} {17 8 1}
4625test io-35.13 {Tcl_Eof, eof char in middle, lf write, lf read} {
4626    file delete $path(test1)
4627    set f [open $path(test1) w]
4628    fconfigure $f -translation lf -eofchar {}
4629    set i [format abc\ndef\n%cqrs\nuvw 26]
4630    puts $f $i
4631    close $f
4632    set c [file size $path(test1)]
4633    set f [open $path(test1) r]
4634    fconfigure $f -translation lf -eofchar \x1a
4635    set l [string length [read $f]]
4636    set e [eof $f]
4637    close $f
4638    list $c $l $e
4639} {17 8 1}
4640test io-35.14 {Tcl_Eof, eof char in middle, cr write, auto read} {
4641    file delete $path(test1)
4642    set f [open $path(test1) w]
4643    fconfigure $f -translation cr -eofchar {}
4644    set i [format abc\ndef\n%cqrs\nuvw 26]
4645    puts $f $i
4646    close $f
4647    set c [file size $path(test1)]
4648    set f [open $path(test1) r]
4649    fconfigure $f -translation auto -eofchar \x1a
4650    set l [string length [read $f]]
4651    set e [eof $f]
4652    close $f
4653    list $c $l $e
4654} {17 8 1}
4655test io-35.15 {Tcl_Eof, eof char in middle, cr write, cr read} {
4656    file delete $path(test1)
4657    set f [open $path(test1) w]
4658    fconfigure $f -translation cr -eofchar {}
4659    set i [format abc\ndef\n%cqrs\nuvw 26]
4660    puts $f $i
4661    close $f
4662    set c [file size $path(test1)]
4663    set f [open $path(test1) r]
4664    fconfigure $f -translation cr -eofchar \x1a
4665    set l [string length [read $f]]
4666    set e [eof $f]
4667    close $f
4668    list $c $l $e
4669} {17 8 1}
4670test io-35.16 {Tcl_Eof, eof char in middle, crlf write, auto read} {
4671    file delete $path(test1)
4672    set f [open $path(test1) w]
4673    fconfigure $f -translation crlf -eofchar {}
4674    set i [format abc\ndef\n%cqrs\nuvw 26]
4675    puts $f $i
4676    close $f
4677    set c [file size $path(test1)]
4678    set f [open $path(test1) r]
4679    fconfigure $f -translation auto -eofchar \x1a
4680    set l [string length [read $f]]
4681    set e [eof $f]
4682    close $f
4683    list $c $l $e
4684} {21 8 1}
4685test io-35.17 {Tcl_Eof, eof char in middle, crlf write, crlf read} {
4686    file delete $path(test1)
4687    set f [open $path(test1) w]
4688    fconfigure $f -translation crlf -eofchar {}
4689    set i [format abc\ndef\n%cqrs\nuvw 26]
4690    puts $f $i
4691    close $f
4692    set c [file size $path(test1)]
4693    set f [open $path(test1) r]
4694    fconfigure $f -translation crlf -eofchar \x1a
4695    set l [string length [read $f]]
4696    set e [eof $f]
4697    close $f
4698    list $c $l $e
4699} {21 8 1}
4700
4701# Test Tcl_InputBlocked
4702
4703test io-36.1 {Tcl_InputBlocked on nonblocking pipe} {stdio openpipe} {
4704    set f1 [open "|[list [interpreter]]" r+]
4705    puts $f1 {puts hello_from_pipe}
4706    flush $f1
4707    gets $f1
4708    fconfigure $f1 -blocking off -buffering full
4709    puts $f1 {puts hello}
4710    set x ""
4711    lappend x [gets $f1]
4712    lappend x [fblocked $f1]
4713    flush $f1
4714    after 200
4715    lappend x [gets $f1]
4716    lappend x [fblocked $f1]
4717    lappend x [gets $f1]
4718    lappend x [fblocked $f1]
4719    close $f1
4720    set x
4721} {{} 1 hello 0 {} 1}
4722test io-36.2 {Tcl_InputBlocked on blocking pipe} {stdio openpipe} {
4723    set f1 [open "|[list [interpreter]]" r+]
4724    fconfigure $f1 -buffering line
4725    puts $f1 {puts hello_from_pipe}
4726    set x ""
4727    lappend x [gets $f1]
4728    lappend x [fblocked $f1]
4729    puts $f1 {exit}
4730    lappend x [gets $f1]
4731    lappend x [fblocked $f1]
4732    lappend x [eof $f1]
4733    close $f1
4734    set x
4735} {hello_from_pipe 0 {} 0 1}
4736test io-36.3 {Tcl_InputBlocked vs files, short read} {
4737    file delete $path(test1)
4738    set f [open $path(test1) w]
4739    puts $f abcdefghijklmnop
4740    close $f
4741    set f [open $path(test1) r]
4742    set l ""
4743    lappend l [fblocked $f]
4744    lappend l [read $f 3]
4745    lappend l [fblocked $f]
4746    lappend l [read -nonewline $f]
4747    lappend l [fblocked $f]
4748    lappend l [eof $f]
4749    close $f
4750    set l
4751} {0 abc 0 defghijklmnop 0 1}
4752test io-36.4 {Tcl_InputBlocked vs files, event driven read} {fileevent} {
4753    proc in {f} {
4754        variable l
4755        variable x
4756        lappend l [read $f 3]
4757        if {[eof $f]} {lappend l eof; close $f; set x done}
4758    }
4759    file delete $path(test1)
4760    set f [open $path(test1) w]
4761    puts $f abcdefghijklmnop
4762    close $f
4763    set f [open $path(test1) r]
4764    set l ""
4765    fileevent $f readable [namespace code [list in $f]]
4766    variable x
4767    vwait [namespace which -variable x]
4768    set l
4769} {abc def ghi jkl mno {p
4770} eof}
4771test io-36.5 {Tcl_InputBlocked vs files, short read, nonblocking} {nonBlockFiles} {
4772    file delete $path(test1)
4773    set f [open $path(test1) w]
4774    puts $f abcdefghijklmnop
4775    close $f
4776    set f [open $path(test1) r]
4777    fconfigure $f -blocking off
4778    set l ""
4779    lappend l [fblocked $f]
4780    lappend l [read $f 3]
4781    lappend l [fblocked $f]
4782    lappend l [read -nonewline $f]
4783    lappend l [fblocked $f]
4784    lappend l [eof $f]
4785    close $f
4786    set l
4787} {0 abc 0 defghijklmnop 0 1}
4788test io-36.6 {Tcl_InputBlocked vs files, event driven read} {nonBlockFiles fileevent} {
4789    proc in {f} {
4790        variable l
4791        variable x
4792        lappend l [read $f 3]
4793        if {[eof $f]} {lappend l eof; close $f; set x done}
4794    }
4795    file delete $path(test1)
4796    set f [open $path(test1) w]
4797    puts $f abcdefghijklmnop
4798    close $f
4799    set f [open $path(test1) r]
4800    fconfigure $f -blocking off
4801    set l ""
4802    fileevent $f readable [namespace code [list in $f]]
4803    variable x
4804    vwait [namespace which -variable x]
4805    set l
4806} {abc def ghi jkl mno {p
4807} eof}
4808
4809# Test Tcl_InputBuffered
4810
4811test io-37.1 {Tcl_InputBuffered} {testchannel} {
4812    set f [open $path(longfile) r]
4813    fconfigure $f -buffersize 4096
4814    read $f 3
4815    set l ""
4816    lappend l [testchannel inputbuffered $f]
4817    lappend l [tell $f]
4818    close $f
4819    set l
4820} {4093 3}
4821test io-37.2 {Tcl_InputBuffered, test input flushing on seek} {testchannel} {
4822    set f [open $path(longfile) r]
4823    fconfigure $f -buffersize 4096
4824    read $f 3
4825    set l ""
4826    lappend l [testchannel inputbuffered $f]
4827    lappend l [tell $f]
4828    seek $f 0 current
4829    lappend l [testchannel inputbuffered $f]
4830    lappend l [tell $f]
4831    close $f
4832    set l
4833} {4093 3 0 3}
4834
4835# Test Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize
4836
4837test io-38.1 {Tcl_GetChannelBufferSize, default buffer size} {
4838    set f [open $path(longfile) r]
4839    set s [fconfigure $f -buffersize]
4840    close $f
4841    set s
4842} 4096
4843test io-38.2 {Tcl_SetChannelBufferSize, Tcl_GetChannelBufferSize} {
4844    set f [open $path(longfile) r]
4845    set l ""
4846    lappend l [fconfigure $f -buffersize]
4847    fconfigure $f -buffersize 10000
4848    lappend l [fconfigure $f -buffersize]
4849    fconfigure $f -buffersize 1
4850    lappend l [fconfigure $f -buffersize]
4851    fconfigure $f -buffersize -1
4852    lappend l [fconfigure $f -buffersize]
4853    fconfigure $f -buffersize 0
4854    lappend l [fconfigure $f -buffersize]
4855    fconfigure $f -buffersize 100000
4856    lappend l [fconfigure $f -buffersize]
4857    fconfigure $f -buffersize 10000000
4858    lappend l [fconfigure $f -buffersize]
4859    close $f
4860    set l
4861} {4096 10000 1 1 1 100000 100000}
4862test io-38.3 {Tcl_SetChannelBufferSize, changing buffersize between reads} {
4863    # This test crashes the interp if Bug #427196 is not fixed
4864
4865    set chan [open [info script] r]
4866    fconfigure $chan -buffersize 10
4867    set var [read $chan 2]
4868    fconfigure $chan -buffersize 32
4869    append var [read $chan]
4870    close $chan
4871} {}
4872
4873# Test Tcl_SetChannelOption, Tcl_GetChannelOption
4874
4875test io-39.1 {Tcl_GetChannelOption} {
4876    file delete $path(test1)
4877    set f1 [open $path(test1) w]
4878    set x [fconfigure $f1 -blocking]
4879    close $f1
4880    set x
4881} 1
4882#
4883# Test 17.2 was removed.
4884#
4885test io-39.2 {Tcl_GetChannelOption} {
4886    file delete $path(test1)
4887    set f1 [open $path(test1) w]
4888    set x [fconfigure $f1 -buffering]
4889    close $f1
4890    set x
4891} full
4892test io-39.3 {Tcl_GetChannelOption} {
4893    file delete $path(test1)
4894    set f1 [open $path(test1) w]
4895    fconfigure $f1 -buffering line
4896    set x [fconfigure $f1 -buffering]
4897    close $f1
4898    set x
4899} line
4900test io-39.4 {Tcl_GetChannelOption, Tcl_SetChannelOption} {
4901    file delete $path(test1)
4902    set f1 [open $path(test1) w]
4903    set l ""
4904    lappend l [fconfigure $f1 -buffering]
4905    fconfigure $f1 -buffering line
4906    lappend l [fconfigure $f1 -buffering]
4907    fconfigure $f1 -buffering none
4908    lappend l [fconfigure $f1 -buffering]
4909    fconfigure $f1 -buffering line
4910    lappend l [fconfigure $f1 -buffering]
4911    fconfigure $f1 -buffering full
4912    lappend l [fconfigure $f1 -buffering]
4913    close $f1
4914    set l
4915} {full line none line full}
4916test io-39.5 {Tcl_GetChannelOption, invariance} {
4917    file delete $path(test1)
4918    set f1 [open $path(test1) w]
4919    set l ""
4920    lappend l [fconfigure $f1 -buffering]
4921    lappend l [list [catch {fconfigure $f1 -buffering green} msg] $msg]
4922    lappend l [fconfigure $f1 -buffering]
4923    close $f1
4924    set l
4925} {full {1 {bad value for -buffering: must be one of full, line, or none}} full}
4926test io-39.6 {Tcl_SetChannelOption, multiple options} {
4927    file delete $path(test1)
4928    set f1 [open $path(test1) w]
4929    fconfigure $f1 -translation lf -buffering line
4930    puts $f1 hello
4931    puts $f1 bye
4932    set x [file size $path(test1)]
4933    close $f1
4934    set x
4935} 10
4936test io-39.7 {Tcl_SetChannelOption, buffering, translation} {
4937    file delete $path(test1)
4938    set f1 [open $path(test1) w]
4939    fconfigure $f1 -translation lf
4940    puts $f1 hello
4941    puts $f1 bye
4942    set x ""
4943    fconfigure $f1 -buffering line
4944    lappend x [file size $path(test1)]
4945    puts $f1 really_bye
4946    lappend x [file size $path(test1)]
4947    close $f1
4948    set x
4949} {0 21}
4950test io-39.8 {Tcl_SetChannelOption, different buffering options} {
4951    file delete $path(test1)
4952    set f1 [open $path(test1) w]
4953    set l ""
4954    fconfigure $f1 -translation lf -buffering none -eofchar {}
4955    puts -nonewline $f1 hello
4956    lappend l [file size $path(test1)]
4957    puts -nonewline $f1 hello
4958    lappend l [file size $path(test1)]
4959    fconfigure $f1 -buffering full
4960    puts -nonewline $f1 hello
4961    lappend l [file size $path(test1)]
4962    fconfigure $f1 -buffering none
4963    lappend l [file size $path(test1)]
4964    puts -nonewline $f1 hello
4965    lappend l [file size $path(test1)]
4966    close $f1
4967    lappend l [file size $path(test1)]
4968    set l
4969} {5 10 10 10 20 20}
4970test io-39.9 {Tcl_SetChannelOption, blocking mode} {nonBlockFiles} {
4971    file delete $path(test1)
4972    set f1 [open $path(test1) w]
4973    close $f1
4974    set f1 [open $path(test1) r]
4975    set x ""
4976    lappend x [fconfigure $f1 -blocking]
4977    fconfigure $f1 -blocking off
4978    lappend x [fconfigure $f1 -blocking]
4979    lappend x [gets $f1]
4980    lappend x [read $f1 1000]
4981    lappend x [fblocked $f1]
4982    lappend x [eof $f1]
4983    close $f1
4984    set x
4985} {1 0 {} {} 0 1}
4986test io-39.10 {Tcl_SetChannelOption, blocking mode} {stdio openpipe} {
4987    file delete $path(pipe)
4988    set f1 [open $path(pipe) w]
4989    puts $f1 {
4990        gets stdin
4991        after 100
4992        puts hi
4993        gets stdin
4994    }
4995    close $f1
4996    set x ""
4997    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
4998    fconfigure $f1 -blocking off -buffering line
4999    lappend x [fconfigure $f1 -blocking]
5000    lappend x [gets $f1]
5001    lappend x [fblocked $f1]
5002    fconfigure $f1 -blocking on
5003    puts $f1 hello
5004    fconfigure $f1 -blocking off
5005    lappend x [gets $f1]
5006    lappend x [fblocked $f1]
5007    fconfigure $f1 -blocking on
5008    puts $f1 bye
5009    fconfigure $f1 -blocking off
5010    lappend x [gets $f1]
5011    lappend x [fblocked $f1]
5012    fconfigure $f1 -blocking on
5013    lappend x [fconfigure $f1 -blocking]
5014    lappend x [gets $f1]
5015    lappend x [fblocked $f1]
5016    lappend x [eof $f1]
5017    lappend x [gets $f1]
5018    lappend x [eof $f1]
5019    close $f1
5020    set x
5021} {0 {} 1 {} 1 {} 1 1 hi 0 0 {} 1}
5022test io-39.11 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
5023    file delete $path(test1)
5024    set f [open $path(test1) w]
5025    fconfigure $f -buffersize -10
5026    set x [fconfigure $f -buffersize]
5027    close $f
5028    set x
5029} 4096
5030test io-39.12 {Tcl_SetChannelOption, Tcl_GetChannelOption buffer size} {
5031    file delete $path(test1)
5032    set f [open $path(test1) w]
5033    fconfigure $f -buffersize 10000000
5034    set x [fconfigure $f -buffersize]
5035    close $f
5036    set x
5037} 4096
5038test io-39.13 {Tcl_SetChannelOption, Tcl_GetChannelOption, buffer size} {
5039    file delete $path(test1)
5040    set f [open $path(test1) w]
5041    fconfigure $f -buffersize 40000
5042    set x [fconfigure $f -buffersize]
5043    close $f
5044    set x
5045} 40000
5046test io-39.14 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
5047    file delete $path(test1)
5048    set f [open $path(test1) w]
5049    fconfigure $f -encoding {} 
5050    puts -nonewline $f \xe7\x89\xa6
5051    close $f
5052    set f [open $path(test1) r]
5053    fconfigure $f -encoding utf-8
5054    set x [read $f]
5055    close $f
5056    set x
5057} \u7266
5058test io-39.15 {Tcl_SetChannelOption: -encoding, binary & utf-8} {
5059    file delete $path(test1)
5060    set f [open $path(test1) w]
5061    fconfigure $f -encoding binary
5062    puts -nonewline $f \xe7\x89\xa6
5063    close $f
5064    set f [open $path(test1) r]
5065    fconfigure $f -encoding utf-8
5066    set x [read $f]
5067    close $f
5068    set x
5069} \u7266
5070test io-39.16 {Tcl_SetChannelOption: -encoding, errors} {
5071    file delete $path(test1)
5072    set f [open $path(test1) w]
5073    set result [list [catch {fconfigure $f -encoding foobar} msg] $msg]
5074    close $f
5075    set result
5076} {1 {unknown encoding "foobar"}}
5077test io-39.17 {Tcl_SetChannelOption: -encoding, clearing CHANNEL_NEED_MORE_DATA} {stdio openpipe fileevent} {
5078    set f [open "|[list [interpreter] $path(cat)]" r+]
5079    fconfigure $f -encoding binary
5080    puts -nonewline $f "\xe7"
5081    flush $f
5082    fconfigure $f -encoding utf-8 -blocking 0
5083    variable x {}
5084    fileevent $f readable [namespace code { lappend x [read $f] }]
5085    vwait [namespace which -variable x]
5086    after 300 [namespace code { lappend x timeout }]
5087    vwait [namespace which -variable x]
5088    fconfigure $f -encoding utf-8
5089    vwait [namespace which -variable x]
5090    after 300 [namespace code { lappend x timeout }]
5091    vwait [namespace which -variable x]
5092    fconfigure $f -encoding binary
5093    vwait [namespace which -variable x]
5094    after 300 [namespace code { lappend x timeout }]
5095    vwait [namespace which -variable x]
5096    close $f
5097    set x
5098} "{} timeout {} timeout \xe7 timeout"
5099test io-39.18 {Tcl_SetChannelOption, setting read mode independently} \
5100        {socket} {
5101    proc accept {s a p} {close $s}
5102    set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5103    set port [lindex [fconfigure $s1 -sockname] 2]
5104    set s2 [socket 127.0.0.1 $port]
5105    update
5106    fconfigure $s2 -translation {auto lf}
5107    set modes [fconfigure $s2 -translation]
5108    close $s1
5109    close $s2
5110    set modes
5111} {auto lf}
5112test io-39.19 {Tcl_SetChannelOption, setting read mode independently} \
5113        {socket} {
5114    proc accept {s a p} {close $s}
5115    set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5116    set port [lindex [fconfigure $s1 -sockname] 2]
5117    set s2 [socket 127.0.0.1 $port]
5118    update
5119    fconfigure $s2 -translation {auto crlf}
5120    set modes [fconfigure $s2 -translation]
5121    close $s1
5122    close $s2
5123    set modes
5124} {auto crlf}
5125test io-39.20 {Tcl_SetChannelOption, setting read mode independently} \
5126        {socket} {
5127    proc accept {s a p} {close $s}
5128    set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5129    set port [lindex [fconfigure $s1 -sockname] 2]
5130    set s2 [socket 127.0.0.1 $port]
5131    update
5132    fconfigure $s2 -translation {auto cr}
5133    set modes [fconfigure $s2 -translation]
5134    close $s1
5135    close $s2
5136    set modes
5137} {auto cr}
5138test io-39.21 {Tcl_SetChannelOption, setting read mode independently} \
5139        {socket} {
5140    proc accept {s a p} {close $s}
5141    set s1 [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5142    set port [lindex [fconfigure $s1 -sockname] 2]
5143    set s2 [socket 127.0.0.1 $port]
5144    update
5145    fconfigure $s2 -translation {auto auto}
5146    set modes [fconfigure $s2 -translation]
5147    close $s1
5148    close $s2
5149    set modes
5150} {auto crlf}
5151test io-39.22 {Tcl_SetChannelOption, invariance} {unix} {
5152    file delete $path(test1)
5153    set f1 [open $path(test1) w+]
5154    set l ""
5155    lappend l [fconfigure $f1 -eofchar]
5156    fconfigure $f1 -eofchar {ON GO}
5157    lappend l [fconfigure $f1 -eofchar]
5158    fconfigure $f1 -eofchar D
5159    lappend l [fconfigure $f1 -eofchar]
5160    close $f1
5161    set l
5162} {{{} {}} {O G} {D D}}
5163test io-39.22a {Tcl_SetChannelOption, invariance} {
5164    file delete $path(test1)
5165    set f1 [open $path(test1) w+]
5166    set l [list]
5167    fconfigure $f1 -eofchar {ON GO}
5168    lappend l [fconfigure $f1 -eofchar]
5169    fconfigure $f1 -eofchar D
5170    lappend l [fconfigure $f1 -eofchar]
5171    lappend l [list [catch {fconfigure $f1 -eofchar {1 2 3}} msg] $msg]
5172    close $f1
5173    set l
5174} {{O G} {D D} {1 {bad value for -eofchar: should be a list of zero, one, or two elements}}}
5175test io-39.23 {Tcl_GetChannelOption, server socket is not readable or
5176        writeable, it should still have valid -eofchar and -translation options } {
5177    set l [list]
5178    set sock [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5179    lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
5180    close $sock
5181    set l
5182} {{{}} auto}
5183test io-39.24 {Tcl_SetChannelOption, server socket is not readable or
5184        writable so we can't change -eofchar or -translation } {
5185    set l [list]
5186    set sock [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
5187    fconfigure $sock -eofchar D -translation lf
5188    lappend l [fconfigure $sock -eofchar] [fconfigure $sock -translation]
5189    close $sock
5190    set l
5191} {{{}} auto}
5192
5193test io-40.1 {POSIX open access modes: RDWR} {
5194    file delete $path(test3)
5195    set f [open $path(test3) w]
5196    puts $f xyzzy
5197    close $f
5198    set f [open $path(test3) RDWR]
5199    puts -nonewline $f "ab"
5200    seek $f 0 current
5201    set x [gets $f]
5202    close $f
5203    set f [open $path(test3) r]
5204    lappend x [gets $f]
5205    close $f
5206    set x
5207} {zzy abzzy}
5208test io-40.2 {POSIX open access modes: CREAT} {unix} {
5209    file delete $path(test3)
5210    set f [open $path(test3) {WRONLY CREAT} 0600]
5211    file stat $path(test3) stats
5212    set x [format "0%o" [expr $stats(mode)&0o777]]
5213    puts $f "line 1"
5214    close $f
5215    set f [open $path(test3) r]
5216    lappend x [gets $f]
5217    close $f
5218    set x
5219} {0600 {line 1}}
5220test io-40.3 {POSIX open access modes: CREAT} {unix umask} {
5221    # This test only works if your umask is 2, like ouster's.
5222    file delete $path(test3)
5223    set f [open $path(test3) {WRONLY CREAT}]
5224    close $f
5225    file stat $path(test3) stats
5226    format "0%o" [expr $stats(mode)&0o777]
5227} [format %04o [expr {0o666 & ~ $umaskValue}]]
5228test io-40.4 {POSIX open access modes: CREAT} {
5229    file delete $path(test3)
5230    set f [open $path(test3) w]
5231    fconfigure $f -eofchar {}
5232    puts $f xyzzy
5233    close $f
5234    set f [open $path(test3) {WRONLY CREAT}]
5235    fconfigure $f -eofchar {}
5236    puts -nonewline $f "ab"
5237    close $f
5238    set f [open $path(test3) r]
5239    set x [gets $f]
5240    close $f
5241    set x
5242} abzzy
5243test io-40.5 {POSIX open access modes: APPEND} {
5244    file delete $path(test3)
5245    set f [open $path(test3) w]
5246    fconfigure $f -translation lf -eofchar {}
5247    puts $f xyzzy
5248    close $f
5249    set f [open $path(test3) {WRONLY APPEND}]
5250    fconfigure $f -translation lf
5251    puts $f "new line"
5252    seek $f 0
5253    puts $f "abc"
5254    close $f
5255    set f [open $path(test3) r]
5256    fconfigure $f -translation lf
5257    set x ""
5258    seek $f 6 current
5259    lappend x [gets $f]
5260    lappend x [gets $f]
5261    close $f
5262    set x
5263} {{new line} abc}
5264test io-40.6 {POSIX open access modes: EXCL} -match regexp -body {
5265    file delete $path(test3)
5266    set f [open $path(test3) w]
5267    puts $f xyzzy
5268    close $f
5269    open $path(test3) {WRONLY CREAT EXCL}
5270} -returnCodes error -result {(?i)couldn't open ".*test3": file (already )?exists}
5271test io-40.7 {POSIX open access modes: EXCL} {
5272    file delete $path(test3)
5273    set f [open $path(test3) {WRONLY CREAT EXCL}]
5274    fconfigure $f -eofchar {}
5275    puts $f "A test line"
5276    close $f
5277    viewFile test3
5278} {A test line}
5279test io-40.8 {POSIX open access modes: TRUNC} {
5280    file delete $path(test3)
5281    set f [open $path(test3) w]
5282    puts $f xyzzy
5283    close $f
5284    set f [open $path(test3) {WRONLY TRUNC}]
5285    puts $f abc
5286    close $f
5287    set f [open $path(test3) r]
5288    set x [gets $f]
5289    close $f
5290    set x
5291} abc
5292test io-40.9 {POSIX open access modes: NONBLOCK} {nonPortable unix} {
5293    file delete $path(test3)
5294    set f [open $path(test3) {WRONLY NONBLOCK CREAT}]
5295    puts $f "NONBLOCK test"
5296    close $f
5297    set f [open $path(test3) r]
5298    set x [gets $f]
5299    close $f
5300    set x
5301} {NONBLOCK test}
5302test io-40.10 {POSIX open access modes: RDONLY} {
5303    set f [open $path(test1) w]
5304    puts $f "two lines: this one"
5305    puts $f "and this"
5306    close $f
5307    set f [open $path(test1) RDONLY]
5308    set x [list [gets $f] [catch {puts $f Test} msg] $msg]
5309    close $f
5310    string compare [string tolower $x] \
5311        [list {two lines: this one} 1 \
5312                [format "channel \"%s\" wasn't opened for writing" $f]]
5313} 0
5314test io-40.11 {POSIX open access modes: RDONLY} -match regexp -body {
5315    file delete $path(test3)
5316    open $path(test3) RDONLY
5317} -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5318test io-40.12 {POSIX open access modes: WRONLY} -match regexp -body {
5319    file delete $path(test3)
5320    open $path(test3) WRONLY
5321} -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5322test io-40.13 {POSIX open access modes: WRONLY} {
5323    makeFile xyzzy test3
5324    set f [open $path(test3) WRONLY]
5325    fconfigure $f -eofchar {}
5326    puts -nonewline $f "ab"
5327    seek $f 0 current
5328    set x [list [catch {gets $f} msg] $msg]
5329    close $f
5330    lappend x [viewFile test3]
5331    string compare [string tolower $x] \
5332        [list 1 "channel \"$f\" wasn't opened for reading" abzzy]
5333} 0
5334test io-40.14 {POSIX open access modes: RDWR} -match regexp -body {
5335    file delete $path(test3)
5336    open $path(test3) RDWR
5337} -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
5338test io-40.15 {POSIX open access modes: RDWR} {
5339    makeFile xyzzy test3
5340    set f [open $path(test3) RDWR]
5341    puts -nonewline $f "ab"
5342    seek $f 0 current
5343    set x [gets $f]
5344    close $f
5345    lappend x [viewFile test3]
5346} {zzy abzzy}
5347test io-40.16 {tilde substitution in open} -constraints makeFileInHome -setup {
5348    makeFile {Some text} _test_ ~
5349} -body {
5350    file exists [file join $::env(HOME) _test_]
5351} -cleanup {
5352    removeFile _test_ ~
5353} -result 1
5354test io-40.17 {tilde substitution in open} {
5355    set home $::env(HOME)
5356    unset ::env(HOME)
5357    set x [list [catch {open ~/foo} msg] $msg]
5358    set ::env(HOME) $home
5359    set x
5360} {1 {couldn't find HOME environment variable to expand path}}
5361
5362test io-41.1 {Tcl_FileeventCmd: errors} {fileevent} {
5363    list [catch {fileevent foo} msg] $msg
5364} {1 {wrong # args: should be "fileevent channelId event ?script?"}}
5365test io-41.2 {Tcl_FileeventCmd: errors} {fileevent} {
5366    list [catch {fileevent foo bar baz q} msg] $msg
5367} {1 {wrong # args: should be "fileevent channelId event ?script?"}}
5368test io-41.3 {Tcl_FileeventCmd: errors} {fileevent} {
5369    list [catch {fileevent gorp readable} msg] $msg
5370} {1 {can not find channel named "gorp"}}
5371test io-41.4 {Tcl_FileeventCmd: errors} {fileevent} {
5372    list [catch {fileevent gorp writable} msg] $msg
5373} {1 {can not find channel named "gorp"}}
5374test io-41.5 {Tcl_FileeventCmd: errors} {fileevent} {
5375    list [catch {fileevent gorp who-knows} msg] $msg
5376} {1 {bad event name "who-knows": must be readable or writable}}
5377
5378#
5379# Test fileevent on a file
5380#
5381
5382set path(foo) [makeFile {} foo]
5383set f [open $path(foo) w+]
5384
5385test io-42.1 {Tcl_FileeventCmd: creating, deleting, querying} {fileevent} {
5386    list [fileevent $f readable] [fileevent $f writable]
5387} {{} {}}
5388test io-42.2 {Tcl_FileeventCmd: replacing} {fileevent} {
5389    set result {}
5390    fileevent $f r "first script"
5391    lappend result [fileevent $f readable]
5392    fileevent $f r "new script"
5393    lappend result [fileevent $f readable]
5394    fileevent $f r "yet another"
5395    lappend result [fileevent $f readable]
5396    fileevent $f r ""
5397    lappend result [fileevent $f readable]
5398} {{first script} {new script} {yet another} {}}
5399test io-42.3 {Tcl_FileeventCmd: replacing, with NULL chars in script} {fileevent} {
5400    set result {}
5401    fileevent $f r "first scr\0ipt"
5402    lappend result [string length [fileevent $f readable]]
5403    fileevent $f r "new scr\0ipt"
5404    lappend result [string length [fileevent $f readable]]
5405    fileevent $f r "yet ano\0ther"
5406    lappend result [string length [fileevent $f readable]]
5407    fileevent $f r ""
5408    lappend result [fileevent $f readable]
5409} {13 11 12 {}}
5410
5411
5412test io-43.1 {Tcl_FileeventCmd: creating, deleting, querying} {stdio unixExecs fileevent} {
5413    set result {}
5414    fileevent $f readable "script 1"
5415    lappend result [fileevent $f readable] [fileevent $f writable]
5416    fileevent $f writable "write script"
5417    lappend result [fileevent $f readable] [fileevent $f writable]
5418    fileevent $f readable {}
5419    lappend result [fileevent $f readable] [fileevent $f writable]
5420    fileevent $f writable {}
5421    lappend result [fileevent $f readable] [fileevent $f writable]
5422} {{script 1} {} {script 1} {write script} {} {write script} {} {}}
5423test io-43.2 {Tcl_FileeventCmd: deleting when many present} -setup {
5424    set f2 [open "|[list cat -u]" r+]
5425    set f3 [open "|[list cat -u]" r+]
5426} -constraints {stdio unixExecs fileevent openpipe} -body {
5427    set result {}
5428    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5429    fileevent $f r "read f"
5430    fileevent $f2 r "read f2"
5431    fileevent $f3 r "read f3"
5432    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5433    fileevent $f2 r {}
5434    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5435    fileevent $f3 r {}
5436    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5437    fileevent $f r {}
5438    lappend result [fileevent $f r] [fileevent $f2 r] [fileevent $f3 r]
5439} -cleanup {
5440    catch {close $f2}
5441    catch {close $f3}
5442} -result {{} {} {} {read f} {read f2} {read f3} {read f} {} {read f3} {read f} {} {} {} {} {}}
5443
5444test io-44.1 {FileEventProc procedure: normal read event} -setup {
5445    set f2 [open "|[list cat -u]" r+]
5446    set f3 [open "|[list cat -u]" r+]
5447} -constraints {stdio unixExecs fileevent openpipe} -body {
5448    fileevent $f2 readable [namespace code {
5449        set x [gets $f2]; fileevent $f2 readable {}
5450    }]
5451    puts $f2 text; flush $f2
5452    variable x initial
5453    vwait [namespace which -variable x]
5454    set x
5455} -cleanup {
5456    catch {close $f2}
5457    catch {close $f3}
5458} -result {text}
5459test io-44.2 {FileEventProc procedure: error in read event} -constraints {
5460    stdio unixExecs fileevent openpipe
5461} -setup {
5462    set f2 [open "|[list cat -u]" r+]
5463    set f3 [open "|[list cat -u]" r+]
5464    proc myHandler {msg options} {
5465        variable x $msg
5466    }
5467    set handler [interp bgerror {}]
5468    interp bgerror {} [namespace which myHandler]
5469} -body {
5470    fileevent $f2 readable {error bogus}
5471    puts $f2 text; flush $f2
5472    variable x initial
5473    vwait [namespace which -variable x]
5474    list $x [fileevent $f2 readable]
5475} -cleanup {
5476    interp bgerror {} $handler
5477    catch {close $f2}
5478    catch {close $f3}
5479} -result {bogus {}}
5480test io-44.3 {FileEventProc procedure: normal write event} -setup {
5481    set f2 [open "|[list cat -u]" r+]
5482    set f3 [open "|[list cat -u]" r+]
5483} -constraints {stdio unixExecs fileevent openpipe} -body {
5484    fileevent $f2 writable [namespace code {
5485        lappend x "triggered"
5486        incr count -1
5487        if {$count <= 0} {
5488            fileevent $f2 writable {}
5489        }
5490    }]
5491    variable x initial
5492    set count 3
5493    vwait [namespace which -variable x]
5494    vwait [namespace which -variable x]
5495    vwait [namespace which -variable x]
5496    set x
5497} -cleanup {
5498    catch {close $f2}
5499    catch {close $f3}
5500} -result {initial triggered triggered triggered}
5501test io-44.4 {FileEventProc procedure: eror in write event} -constraints {
5502    stdio unixExecs fileevent openpipe
5503} -setup {
5504    set f2 [open "|[list cat -u]" r+]
5505    set f3 [open "|[list cat -u]" r+]
5506    proc myHandler {msg options} {
5507        variable x $msg
5508    }
5509    set handler [interp bgerror {}]
5510    interp bgerror {} [namespace which myHandler]
5511} -body {
5512    fileevent $f2 writable {error bad-write}
5513    variable x initial
5514    vwait [namespace which -variable x]
5515    list $x [fileevent $f2 writable]
5516} -cleanup {
5517    interp bgerror {} $handler
5518    catch {close $f2}
5519    catch {close $f3}
5520} -result {bad-write {}}
5521test io-44.5 {FileEventProc procedure: end of file} {stdio unixExecs openpipe fileevent} {
5522    set f4 [open "|[list [interpreter] $path(cat) << foo]" r]
5523    fileevent $f4 readable [namespace code {
5524        if {[gets $f4 line] < 0} {
5525            lappend x eof
5526            fileevent $f4 readable {}
5527        } else {
5528            lappend x $line
5529        }
5530    }]
5531    variable x initial
5532    vwait [namespace which -variable x]
5533    vwait [namespace which -variable x]
5534    close $f4
5535    set x
5536} {initial foo eof}
5537
5538close $f
5539makeFile "foo bar" foo
5540
5541test io-45.1 {DeleteFileEvent, cleanup on close} {fileevent} {
5542    set f [open $path(foo) r]
5543    fileevent $f readable [namespace code {
5544        lappend x "binding triggered: \"[gets $f]\""
5545        fileevent $f readable {}
5546    }]
5547    close $f
5548    set x initial
5549    after 100 [namespace code { set y done }]
5550    variable y
5551    vwait [namespace which -variable y]
5552    set x
5553} {initial}
5554test io-45.2 {DeleteFileEvent, cleanup on close} {fileevent} {
5555    set f  [open $path(foo) r]
5556    set f2 [open $path(foo) r]
5557    fileevent $f readable [namespace code {
5558            lappend x "f triggered: \"[gets $f]\""
5559            fileevent $f readable {}
5560        }]
5561    fileevent $f2 readable [namespace code {
5562        lappend x "f2 triggered: \"[gets $f2]\""
5563        fileevent $f2 readable {}
5564    }]
5565    close $f
5566    variable x initial
5567    vwait [namespace which -variable x]
5568    close $f2
5569    set x
5570} {initial {f2 triggered: "foo bar"}}
5571test io-45.3 {DeleteFileEvent, cleanup on close} {fileevent} {
5572    set f  [open $path(foo) r]
5573    set f2 [open $path(foo) r]
5574    set f3 [open $path(foo) r]
5575    fileevent $f readable {f script}
5576    fileevent $f2 readable {f2 script}
5577    fileevent $f3 readable {f3 script}
5578    set x {}
5579    close $f2
5580    lappend x [catch {fileevent $f readable} msg] $msg \
5581            [catch {fileevent $f2 readable}] \
5582            [catch {fileevent $f3 readable} msg] $msg
5583    close $f3
5584    lappend x [catch {fileevent $f readable} msg] $msg \
5585            [catch {fileevent $f2 readable}] \
5586            [catch {fileevent $f3 readable}]
5587    close $f
5588    lappend x [catch {fileevent $f readable}] \
5589            [catch {fileevent $f2 readable}] \
5590            [catch {fileevent $f3 readable}]
5591} {0 {f script} 1 0 {f3 script} 0 {f script} 1 1 1 1 1}
5592
5593# Execute these tests only if the "testfevent" command is present.
5594
5595test io-46.1 {Tcl event loop vs multiple interpreters} {testfevent fileevent} {
5596    testfevent create
5597    set script "set f \[[list open $path(foo) r]]\n"
5598    append script {
5599        set x "no event"
5600        fileevent $f readable [namespace code {
5601            set x "f triggered: [gets $f]"
5602            fileevent $f readable {}
5603        }]
5604    }
5605    testfevent cmd $script
5606    after 1     ;# We must delay because Windows takes a little time to notice
5607    update
5608    testfevent cmd {close $f}
5609    list [testfevent cmd {set x}] [testfevent cmd {info commands after}]
5610} {{f triggered: foo bar} after}
5611test io-46.2 {Tcl event loop vs multiple interpreters} testfevent {
5612    testfevent create
5613    testfevent cmd {
5614        variable x 0
5615        after 100 {set x triggered}
5616        vwait [namespace which -variable x]
5617        set x
5618    }
5619} {triggered}
5620test io-46.3 {Tcl event loop vs multiple interpreters} testfevent {
5621    testfevent create
5622    testfevent cmd {
5623        set x 0
5624        after 10 {lappend x timer}
5625        after 30
5626        set result $x
5627        update idletasks
5628        lappend result $x
5629        update
5630        lappend result $x
5631    }
5632} {0 0 {0 timer}}
5633
5634test io-47.1 {fileevent vs multiple interpreters} {testfevent fileevent} {
5635    set f  [open $path(foo) r]
5636    set f2 [open $path(foo) r]
5637    set f3 [open $path(foo) r]
5638    fileevent $f readable {script 1}
5639    testfevent create
5640    testfevent share $f2
5641    testfevent cmd "fileevent $f2 readable {script 2}"
5642    fileevent $f3 readable {sript 3}
5643    set x {}
5644    lappend x [fileevent $f2 readable]
5645    testfevent delete
5646    lappend x [fileevent $f readable] [fileevent $f2 readable] \
5647        [fileevent $f3 readable]
5648    close $f
5649    close $f2
5650    close $f3
5651    set x
5652} {{} {script 1} {} {sript 3}}
5653test io-47.2 {deleting fileevent on interpreter delete} {testfevent fileevent} {
5654    set f  [open $path(foo) r]
5655    set f2 [open $path(foo) r]
5656    set f3 [open $path(foo) r]
5657    set f4 [open $path(foo) r]
5658    fileevent $f readable {script 1}
5659    testfevent create
5660    testfevent share $f2
5661    testfevent share $f3
5662    testfevent cmd "fileevent $f2 readable {script 2}
5663        fileevent $f3 readable {script 3}"
5664    fileevent $f4 readable {script 4}
5665    testfevent delete
5666    set x [list [fileevent $f readable] [fileevent $f2 readable] \
5667                [fileevent $f3 readable] [fileevent $f4 readable]]
5668    close $f
5669    close $f2
5670    close $f3
5671    close $f4
5672    set x
5673} {{script 1} {} {} {script 4}}
5674test io-47.3 {deleting fileevent on interpreter delete} {testfevent fileevent} {
5675    set f  [open $path(foo) r]
5676    set f2 [open $path(foo) r]
5677    set f3 [open $path(foo) r]
5678    set f4 [open $path(foo) r]
5679    testfevent create
5680    testfevent share $f3
5681    testfevent share $f4
5682    fileevent $f readable {script 1}
5683    fileevent $f2 readable {script 2}
5684    testfevent cmd "fileevent $f3 readable {script 3}
5685      fileevent $f4 readable {script 4}"
5686    testfevent delete
5687    set x [list [fileevent $f readable] [fileevent $f2 readable] \
5688                [fileevent $f3 readable] [fileevent $f4 readable]]
5689    close $f
5690    close $f2
5691    close $f3
5692    close $f4
5693    set x
5694} {{script 1} {script 2} {} {}}
5695test io-47.4 {file events on shared files and multiple interpreters} {testfevent fileevent} {
5696    set f  [open $path(foo) r]
5697    set f2 [open $path(foo) r]
5698    testfevent create
5699    testfevent share $f
5700    testfevent cmd "fileevent $f readable {script 1}"
5701    fileevent $f readable {script 2}
5702    fileevent $f2 readable {script 3}
5703    set x [list [fileevent $f2 readable] \
5704                [testfevent cmd "fileevent $f readable"] \
5705                [fileevent $f readable]]
5706    testfevent delete
5707    close $f
5708    close $f2
5709    set x
5710} {{script 3} {script 1} {script 2}}
5711test io-47.5 {file events on shared files, deleting file events} {testfevent fileevent} {
5712    set f [open $path(foo) r]
5713    testfevent create
5714    testfevent share $f
5715    testfevent cmd "fileevent $f readable {script 1}"
5716    fileevent $f readable {script 2}
5717    testfevent cmd "fileevent $f readable {}"
5718    set x [list [testfevent cmd "fileevent $f readable"] \
5719                [fileevent $f readable]]
5720    testfevent delete
5721    close $f
5722    set x
5723} {{} {script 2}}
5724test io-47.6 {file events on shared files, deleting file events} {testfevent fileevent} {
5725    set f [open $path(foo) r]
5726    testfevent create
5727    testfevent share $f
5728    testfevent cmd "fileevent $f readable {script 1}"
5729    fileevent $f readable {script 2}
5730    fileevent $f readable {}
5731    set x [list [testfevent cmd "fileevent $f readable"] \
5732                [fileevent $f readable]]
5733    testfevent delete
5734    close $f
5735    set x
5736} {{script 1} {}}
5737
5738set path(bar) [makeFile {} bar]
5739
5740test io-48.1 {testing readability conditions} {fileevent} {
5741    set f [open $path(bar) w]
5742    puts $f abcdefg
5743    puts $f abcdefg
5744    puts $f abcdefg
5745    puts $f abcdefg
5746    puts $f abcdefg
5747    close $f
5748    set f [open $path(bar) r]
5749    fileevent $f readable [namespace code [list consume $f]]
5750    proc consume {f} {
5751        variable l
5752        variable x
5753        lappend l called
5754        if {[eof $f]} {
5755            close $f
5756            set x done
5757        } else {
5758            gets $f
5759        }
5760    }
5761    set l ""
5762    variable x not_done
5763    vwait [namespace which -variable x]
5764    list $x $l
5765} {done {called called called called called called called}}
5766test io-48.2 {testing readability conditions} {nonBlockFiles fileevent} {
5767    set f [open $path(bar) w]
5768    puts $f abcdefg
5769    puts $f abcdefg
5770    puts $f abcdefg
5771    puts $f abcdefg
5772    puts $f abcdefg
5773    close $f
5774    set f [open $path(bar) r]
5775    fileevent $f readable [namespace code [list consume $f]]
5776    fconfigure $f -blocking off
5777    proc consume {f} {
5778        variable x
5779        variable l
5780        lappend l called
5781        if {[eof $f]} {
5782            close $f
5783            set x done
5784        } else {
5785            gets $f
5786        }
5787    }
5788    set l ""
5789    variable x not_done
5790    vwait [namespace which -variable x]
5791    list $x $l
5792} {done {called called called called called called called}}
5793set path(my_script) [makeFile {} my_script]
5794test io-48.3 {testing readability conditions} {stdio unix nonBlockFiles openpipe fileevent} {
5795    set f [open $path(bar) w]
5796    puts $f abcdefg
5797    puts $f abcdefg
5798    puts $f abcdefg
5799    puts $f abcdefg
5800    puts $f abcdefg
5801    close $f
5802    set f [open $path(my_script) w]
5803    puts $f {
5804        proc copy_slowly {f} {
5805            while {![eof $f]} {
5806                puts [gets $f]
5807                after 200
5808            }
5809            close $f
5810        }
5811    }
5812    close $f
5813    set f [open "|[list [interpreter]]" r+]
5814    fileevent  $f readable [namespace code [list consume $f]]
5815    fconfigure $f -buffering line
5816    fconfigure $f -blocking off
5817    proc consume {f} {
5818        variable l
5819        variable x
5820        if {[eof $f]} {
5821            set x done
5822        } else {
5823            gets $f
5824            lappend l [fblocked $f]
5825            gets $f
5826            lappend l [fblocked $f]
5827        }
5828    }
5829    set l ""
5830    variable x not_done
5831    puts $f [list source $path(my_script)]
5832    puts $f "set f \[[list open $path(bar) r]]"
5833    puts $f {copy_slowly $f}
5834    puts $f {exit}
5835    vwait [namespace which -variable x]
5836    close $f
5837    list $x $l
5838} {done {0 1 0 1 0 1 0 1 0 1 0 1 0 0}}
5839test io-48.4 {lf write, testing readability, ^Z termination, auto read mode} {fileevent} {
5840    file delete $path(test1)
5841    set f [open $path(test1) w]
5842    fconfigure $f -translation lf
5843    variable c [format "abc\ndef\n%c" 26]
5844    puts -nonewline $f $c
5845    close $f
5846    proc consume {f} {
5847        variable l
5848        variable c
5849        variable x
5850        if {[eof $f]} {
5851           set x done
5852           close $f
5853        } else {
5854           lappend l [gets $f]
5855           incr c
5856        }
5857    }
5858    set c 0
5859    set l ""
5860    set f [open $path(test1) r]
5861    fconfigure $f -translation auto -eofchar \x1a
5862    fileevent $f readable [namespace code [list consume $f]]
5863    variable x
5864    vwait [namespace which -variable x]
5865    list $c $l
5866} {3 {abc def {}}}
5867test io-48.5 {lf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
5868    file delete $path(test1)
5869    set f [open $path(test1) w]
5870    fconfigure $f -translation lf
5871    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
5872    puts -nonewline $f $c
5873    close $f
5874    proc consume {f} {
5875        variable l
5876        variable x
5877        variable c
5878        if {[eof $f]} {
5879           set x done
5880           close $f
5881        } else {
5882           lappend l [gets $f]
5883           incr c
5884        }
5885    }
5886    set c 0
5887    set l ""
5888    set f [open $path(test1) r]
5889    fconfigure $f -eofchar \x1a -translation auto
5890    fileevent $f readable [namespace code [list consume $f]]
5891    variable x
5892    vwait [namespace which -variable x]
5893    list $c $l
5894} {3 {abc def {}}}
5895test io-48.6 {cr write, testing readability, ^Z termination, auto read mode} {fileevent} {
5896    file delete $path(test1)
5897    set f [open $path(test1) w]
5898    fconfigure $f -translation cr
5899    set c [format "abc\ndef\n%c" 26]
5900    puts -nonewline $f $c
5901    close $f
5902    proc consume {f} {
5903        variable l
5904        variable x
5905        variable c
5906        if {[eof $f]} {
5907           set x done
5908           close $f
5909        } else {
5910           lappend l [gets $f]
5911           incr c
5912        }
5913    }
5914    set c 0
5915    set l ""
5916    set f [open $path(test1) r]
5917    fconfigure $f -translation auto -eofchar \x1a
5918    fileevent $f readable [namespace code [list consume $f]]
5919    variable x
5920    vwait [namespace which -variable x]
5921    list $c $l
5922} {3 {abc def {}}}
5923test io-48.7 {cr write, testing readability, ^Z in middle, auto read mode} {fileevent} {
5924    file delete $path(test1)
5925    set f [open $path(test1) w]
5926    fconfigure $f -translation cr
5927    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
5928    puts -nonewline $f $c
5929    close $f
5930    proc consume {f} {
5931        variable l
5932        variable c
5933        variable x
5934        if {[eof $f]} {
5935           set x done
5936           close $f
5937        } else {
5938           lappend l [gets $f]
5939           incr c
5940        }
5941    }
5942    set c 0
5943    set l ""
5944    set f [open $path(test1) r]
5945    fconfigure $f -eofchar \x1a -translation auto
5946    fileevent $f readable [namespace code [list consume $f]]
5947    variable x
5948    vwait [namespace which -variable x]
5949    list $c $l
5950} {3 {abc def {}}}
5951test io-48.8 {crlf write, testing readability, ^Z termination, auto read mode} {fileevent} {
5952    file delete $path(test1)
5953    set f [open $path(test1) w]
5954    fconfigure $f -translation crlf
5955    set c [format "abc\ndef\n%c" 26]
5956    puts -nonewline $f $c
5957    close $f
5958    proc consume {f} {
5959        variable l
5960        variable x
5961        variable c
5962        if {[eof $f]} {
5963           set x done
5964           close $f
5965        } else {
5966           lappend l [gets $f]
5967           incr c
5968        }
5969    }
5970    set c 0
5971    set l ""
5972    set f [open $path(test1) r]
5973    fconfigure $f -translation auto -eofchar \x1a
5974    fileevent $f readable [namespace code [list consume $f]]
5975    variable x
5976    vwait [namespace which -variable x]
5977    list $c $l
5978} {3 {abc def {}}}
5979test io-48.9 {crlf write, testing readability, ^Z in middle, auto read mode} {fileevent} {
5980    file delete $path(test1)
5981    set f [open $path(test1) w]
5982    fconfigure $f -translation crlf
5983    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
5984    puts -nonewline $f $c
5985    close $f
5986    proc consume {f} {
5987        variable l
5988        variable c
5989        variable x
5990        if {[eof $f]} {
5991           set x done
5992           close $f
5993        } else {
5994           lappend l [gets $f]
5995           incr c
5996        }
5997    }
5998    set c 0
5999    set l ""
6000    set f [open $path(test1) r]
6001    fconfigure $f -eofchar \x1a -translation auto
6002    fileevent $f readable [namespace code [list consume $f]]
6003    variable x
6004    vwait [namespace which -variable x]
6005    list $c $l
6006} {3 {abc def {}}}
6007test io-48.10 {lf write, testing readability, ^Z in middle, lf read mode} {fileevent} {
6008    file delete $path(test1)
6009    set f [open $path(test1) w]
6010    fconfigure $f -translation lf
6011    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6012    puts -nonewline $f $c
6013    close $f
6014    proc consume {f} {
6015        variable l
6016        variable c
6017        variable x
6018        if {[eof $f]} {
6019           set x done
6020           close $f
6021        } else {
6022           lappend l [gets $f]
6023           incr c
6024        }
6025    }
6026    set c 0
6027    set l ""
6028    set f [open $path(test1) r]
6029    fconfigure $f -eofchar \x1a -translation lf
6030    fileevent $f readable [namespace code [list consume $f]]
6031    variable x
6032    vwait [namespace which -variable x]
6033    list $c $l
6034} {3 {abc def {}}}
6035test io-48.11 {lf write, testing readability, ^Z termination, lf read mode} {fileevent} {
6036    file delete $path(test1)
6037    set f [open $path(test1) w]
6038    fconfigure $f -translation lf
6039    set c [format "abc\ndef\n%c" 26]
6040    puts -nonewline $f $c
6041    close $f
6042    proc consume {f} {
6043        variable l
6044        variable x
6045        variable c
6046        if {[eof $f]} {
6047           set x done
6048           close $f
6049        } else {
6050           lappend l [gets $f]
6051           incr c
6052        }
6053    }
6054    set c 0
6055    set l ""
6056    set f [open $path(test1) r]
6057    fconfigure $f -translation lf -eofchar \x1a
6058    fileevent $f readable [namespace code [list consume $f]]
6059    variable x
6060    vwait [namespace which -variable x]
6061    list $c $l
6062} {3 {abc def {}}}
6063test io-48.12 {cr write, testing readability, ^Z in middle, cr read mode} {fileevent} {
6064    file delete $path(test1)
6065    set f [open $path(test1) w]
6066    fconfigure $f -translation cr
6067    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6068    puts -nonewline $f $c
6069    close $f
6070    proc consume {f} {
6071        variable l
6072        variable x
6073        variable c
6074        if {[eof $f]} {
6075           set x done
6076           close $f
6077        } else {
6078           lappend l [gets $f]
6079           incr c
6080        }
6081    }
6082    set c 0
6083    set l ""
6084    set f [open $path(test1) r]
6085    fconfigure $f -eofchar \x1a -translation cr
6086    fileevent $f readable [namespace code [list consume $f]]
6087    variable x
6088    vwait [namespace which -variable x]
6089    list $c $l
6090} {3 {abc def {}}}
6091test io-48.13 {cr write, testing readability, ^Z termination, cr read mode} {fileevent} {
6092    file delete $path(test1)
6093    set f [open $path(test1) w]
6094    fconfigure $f -translation cr
6095    set c [format "abc\ndef\n%c" 26]
6096    puts -nonewline $f $c
6097    close $f
6098    proc consume {f} {
6099        variable c
6100        variable x
6101        variable l
6102        if {[eof $f]} {
6103           set x done
6104           close $f
6105        } else {
6106           lappend l [gets $f]
6107           incr c
6108        }
6109    }
6110    set c 0
6111    set l ""
6112    set f [open $path(test1) r]
6113    fconfigure $f -translation cr -eofchar \x1a
6114    fileevent $f readable [namespace code [list consume $f]]
6115    variable x
6116    vwait [namespace which -variable x]
6117    list $c $l
6118} {3 {abc def {}}}
6119test io-48.14 {crlf write, testing readability, ^Z in middle, crlf read mode} {fileevent} {
6120    file delete $path(test1)
6121    set f [open $path(test1) w]
6122    fconfigure $f -translation crlf
6123    set c [format "abc\ndef\n%cfoo\nbar\n" 26]
6124    puts -nonewline $f $c
6125    close $f
6126    proc consume {f} {
6127        variable c
6128        variable x
6129        variable l
6130        if {[eof $f]} {
6131           set x done
6132           close $f
6133        } else {
6134           lappend l [gets $f]
6135           incr c
6136        }
6137    }
6138    set c 0
6139    set l ""
6140    set f [open $path(test1) r]
6141    fconfigure $f -eofchar \x1a -translation crlf
6142    fileevent $f readable [namespace code [list consume $f]]
6143    variable x
6144    vwait [namespace which -variable x]
6145    list $c $l
6146} {3 {abc def {}}}
6147test io-48.15 {crlf write, testing readability, ^Z termi, crlf read mode} {fileevent} {
6148    file delete $path(test1)
6149    set f [open $path(test1) w]
6150    fconfigure $f -translation crlf
6151    set c [format "abc\ndef\n%c" 26]
6152    puts -nonewline $f $c
6153    close $f
6154    proc consume {f} {
6155        variable c
6156        variable x
6157        variable l
6158        if {[eof $f]} {
6159           set x done
6160           close $f
6161        } else {
6162           lappend l [gets $f]
6163           incr c
6164        }
6165    }
6166    set c 0
6167    set l ""
6168    set f [open $path(test1) r]
6169    fconfigure $f -translation crlf -eofchar \x1a
6170    fileevent $f readable [namespace code [list consume $f]]
6171    variable x
6172    vwait [namespace which -variable x]
6173    list $c $l
6174} {3 {abc def {}}}
6175
6176test io-49.1 {testing crlf reading, leftover cr disgorgment} {
6177    file delete $path(test1)
6178    set f [open $path(test1) w]
6179    fconfigure $f -translation lf
6180    puts -nonewline $f "a\rb\rc\r\n"
6181    close $f
6182    set f [open $path(test1) r]
6183    set l ""
6184    lappend l [file size $path(test1)]
6185    fconfigure $f -translation crlf
6186    lappend l [read $f 1]
6187    lappend l [tell $f]
6188    lappend l [read $f 1]
6189    lappend l [tell $f]
6190    lappend l [read $f 1]
6191    lappend l [tell $f]
6192    lappend l [read $f 1]
6193    lappend l [tell $f]
6194    lappend l [read $f 1]
6195    lappend l [tell $f]
6196    lappend l [read $f 1]
6197    lappend l [tell $f]
6198    lappend l [eof $f]
6199    lappend l [read $f 1]
6200    lappend l [eof $f]
6201    close $f
6202    set l
6203} "7 a 1 [list \r] 2 b 3 [list \r] 4 c 5 {
6204} 7 0 {} 1"
6205test io-49.2 {testing crlf reading, leftover cr disgorgment} {
6206    file delete $path(test1)
6207    set f [open $path(test1) w]
6208    fconfigure $f -translation lf
6209    puts -nonewline $f "a\rb\rc\r\n"
6210    close $f
6211    set f [open $path(test1) r]
6212    set l ""
6213    lappend l [file size $path(test1)]
6214    fconfigure $f -translation crlf
6215    lappend l [read $f 2]
6216    lappend l [tell $f]
6217    lappend l [read $f 2]
6218    lappend l [tell $f]
6219    lappend l [read $f 2]
6220    lappend l [tell $f]
6221    lappend l [eof $f]
6222    lappend l [read $f 2]
6223    lappend l [tell $f]
6224    lappend l [eof $f]
6225    close $f
6226    set l
6227} "7 [list a\r] 2 [list b\r] 4 [list c\n] 7 0 {} 7 1"
6228test io-49.3 {testing crlf reading, leftover cr disgorgment} {
6229    file delete $path(test1)
6230    set f [open $path(test1) w]
6231    fconfigure $f -translation lf
6232    puts -nonewline $f "a\rb\rc\r\n"
6233    close $f
6234    set f [open $path(test1) r]
6235    set l ""
6236    lappend l [file size $path(test1)]
6237    fconfigure $f -translation crlf
6238    lappend l [read $f 3]
6239    lappend l [tell $f]
6240    lappend l [read $f 3]
6241    lappend l [tell $f]
6242    lappend l [eof $f]
6243    lappend l [read $f 3]
6244    lappend l [tell $f]
6245    lappend l [eof $f]
6246    close $f
6247    set l
6248} "7 [list a\rb] 3 [list \rc\n] 7 0 {} 7 1"
6249test io-49.4 {testing crlf reading, leftover cr disgorgment} {
6250    file delete $path(test1)
6251    set f [open $path(test1) w]
6252    fconfigure $f -translation lf
6253    puts -nonewline $f "a\rb\rc\r\n"
6254    close $f
6255    set f [open $path(test1) r]
6256    set l ""
6257    lappend l [file size $path(test1)]
6258    fconfigure $f -translation crlf
6259    lappend l [read $f 3]
6260    lappend l [tell $f]
6261    lappend l [gets $f]
6262    lappend l [tell $f]
6263    lappend l [eof $f]
6264    lappend l [gets $f]
6265    lappend l [tell $f]
6266    lappend l [eof $f]
6267    close $f
6268    set l
6269} "7 [list a\rb] 3 [list \rc] 7 0 {} 7 1"
6270test io-49.5 {testing crlf reading, leftover cr disgorgment} {
6271    file delete $path(test1)
6272    set f [open $path(test1) w]
6273    fconfigure $f -translation lf
6274    puts -nonewline $f "a\rb\rc\r\n"
6275    close $f
6276    set f [open $path(test1) r]
6277    set l ""
6278    lappend l [file size $path(test1)]
6279    fconfigure $f -translation crlf
6280    lappend l [set x [gets $f]]
6281    lappend l [tell $f]
6282    lappend l [gets $f]
6283    lappend l [tell $f]
6284    lappend l [eof $f]
6285    close $f
6286    set l
6287} [list 7 a\rb\rc 7 {} 7 1]
6288
6289test io-50.1 {testing handler deletion} {testchannelevent} {
6290    file delete $path(test1)
6291    set f [open $path(test1) w]
6292    close $f
6293    set f [open $path(test1) r]
6294    testchannelevent $f add readable [namespace code [list delhandler $f]]
6295    proc delhandler {f} {
6296        variable z
6297        set z called
6298        testchannelevent $f delete 0
6299    }
6300    set z not_called
6301    update
6302    close $f
6303    set z
6304} called
6305test io-50.2 {testing handler deletion with multiple handlers} {testchannelevent} {
6306    file delete $path(test1)
6307    set f [open $path(test1) w]
6308    close $f
6309    set f [open $path(test1) r]
6310    testchannelevent $f add readable [namespace code [list delhandler $f 1]]
6311    testchannelevent $f add readable [namespace code [list delhandler $f 0]]
6312    proc delhandler {f i} {
6313        variable z
6314        lappend z "called delhandler $f $i"
6315        testchannelevent $f delete 0
6316    }
6317    set z ""
6318    update
6319    close $f
6320    string compare [string tolower $z] \
6321        [list [list called delhandler $f 0] [list called delhandler $f 1]]
6322} 0
6323test io-50.3 {testing handler deletion with multiple handlers} {testchannelevent} {
6324    file delete $path(test1)
6325    set f [open $path(test1) w]
6326    close $f
6327    set f [open $path(test1) r]
6328    testchannelevent $f add readable [namespace code [list notcalled $f 1]]
6329    testchannelevent $f add readable [namespace code [list delhandler $f 0]]
6330    set z ""
6331    proc notcalled {f i} {
6332        variable z
6333        lappend z "notcalled was called!! $f $i"
6334    }
6335    proc delhandler {f i} {
6336        variable z
6337        testchannelevent $f delete 1
6338        lappend z "delhandler $f $i called"
6339        testchannelevent $f delete 0
6340        lappend z "delhandler $f $i deleted myself"
6341    }
6342    set z ""
6343    update
6344    close $f
6345    string compare [string tolower $z] \
6346        [list [list delhandler $f 0 called] \
6347              [list delhandler $f 0 deleted myself]]
6348} 0
6349test io-50.4 {testing handler deletion vs reentrant calls} {testchannelevent} {
6350    file delete $path(test1)
6351    set f [open $path(test1) w]
6352    close $f
6353    set f [open $path(test1) r]
6354    testchannelevent $f add readable [namespace code [list delrecursive $f]]
6355    proc delrecursive {f} {
6356        variable z
6357        variable u
6358        if {"$u" == "recursive"} {
6359            testchannelevent $f delete 0
6360            lappend z "delrecursive deleting recursive"
6361        } else {
6362            lappend z "delrecursive calling recursive"
6363            set u recursive
6364            update
6365        }
6366    }
6367    variable u toplevel
6368    variable z ""
6369    update
6370    close $f
6371    string compare [string tolower $z] \
6372        {{delrecursive calling recursive} {delrecursive deleting recursive}}
6373} 0
6374test io-50.5 {testing handler deletion vs reentrant calls} {testchannelevent} {
6375    file delete $path(test1)
6376    set f [open $path(test1) w]
6377    close $f
6378    set f [open $path(test1) r]
6379    testchannelevent $f add readable [namespace code [list notcalled $f]]
6380    testchannelevent $f add readable [namespace code [list del $f]]
6381    proc notcalled {f} {
6382        variable z
6383        lappend z "notcalled was called!! $f"
6384    }
6385    proc del {f} {
6386        variable u
6387        variable z
6388        if {"$u" == "recursive"} {
6389            testchannelevent $f delete 1
6390            testchannelevent $f delete 0
6391            lappend z "del deleted notcalled"
6392            lappend z "del deleted myself"
6393        } else {
6394            set u recursive
6395            lappend z "del calling recursive"
6396            update
6397            lappend z "del after update"
6398        }
6399    }
6400    set z ""
6401    set u toplevel
6402    update
6403    close $f
6404    string compare [string tolower $z] \
6405        [list {del calling recursive} {del deleted notcalled} \
6406              {del deleted myself} {del after update}]
6407} 0
6408test io-50.6 {testing handler deletion vs reentrant calls} {testchannelevent} {
6409    file delete $path(test1)
6410    set f [open $path(test1) w]
6411    close $f
6412    set f [open $path(test1) r]
6413    testchannelevent $f add readable [namespace code [list second $f]]
6414    testchannelevent $f add readable [namespace code [list first $f]]
6415    proc first {f} {
6416        variable u
6417        variable z
6418        if {"$u" == "toplevel"} {
6419            lappend z "first called"
6420            set u first
6421            update
6422            lappend z "first after update"
6423        } else {
6424            lappend z "first called not toplevel"
6425        }
6426    }
6427    proc second {f} {
6428        variable u
6429        variable z
6430        if {"$u" == "first"} {
6431            lappend z "second called, first time"
6432            set u second
6433            testchannelevent $f delete 0
6434        } elseif {"$u" == "second"} {
6435            lappend z "second called, second time"
6436            testchannelevent $f delete 0
6437        } else {
6438            lappend z "second called, cannot happen!"
6439            testchannelevent $f removeall
6440        }
6441    }
6442    set z ""
6443    set u toplevel
6444    update
6445    close $f
6446    string compare [string tolower $z] \
6447        [list {first called} {first called not toplevel} \
6448              {second called, first time} {second called, second time} \
6449              {first after update}]
6450} 0
6451
6452test io-51.1 {Test old socket deletion on Macintosh} {socket} {
6453    set x 0
6454    set result ""
6455    proc accept {s a p} {
6456        variable x
6457        variable wait
6458        fconfigure $s -blocking off
6459        puts $s "sock[incr x]"
6460        close $s
6461        set wait done
6462    }
6463    set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
6464    set port [lindex [fconfigure $ss -sockname] 2]
6465
6466    variable wait ""
6467    set cs [socket 127.0.0.1 $port]
6468    vwait [namespace which -variable wait]
6469    lappend result [gets $cs]
6470    close $cs
6471
6472    set wait ""
6473    set cs [socket 127.0.0.1 $port]
6474    vwait [namespace which -variable wait]
6475    lappend result [gets $cs]
6476    close $cs
6477
6478    set wait ""
6479    set cs [socket 127.0.0.1 $port]
6480    vwait [namespace which -variable wait]
6481    lappend result [gets $cs]
6482    close $cs
6483
6484    set wait ""
6485    set cs [socket 127.0.0.1 $port]
6486    vwait [namespace which -variable wait]
6487    lappend result [gets $cs]
6488    close $cs
6489    close $ss
6490    set result
6491} {sock1 sock2 sock3 sock4}
6492
6493test io-52.1 {TclCopyChannel} {fcopy} {
6494    file delete $path(test1)
6495    set f1 [open $thisScript]
6496    set f2 [open $path(test1) w]
6497    fcopy $f1 $f2 -command { # }
6498    catch { fcopy $f1 $f2 } msg
6499    close $f1
6500    close $f2
6501    string compare $msg "channel \"$f1\" is busy"
6502} {0}
6503test io-52.2 {TclCopyChannel} {fcopy} {
6504    file delete $path(test1)
6505    set f1 [open $thisScript]
6506    set f2 [open $path(test1) w]
6507    set f3 [open $thisScript]
6508    fcopy $f1 $f2 -command { # }
6509    catch { fcopy $f3 $f2 } msg
6510    close $f1
6511    close $f2
6512    close $f3
6513    string compare $msg "channel \"$f2\" is busy"
6514} {0}
6515test io-52.3 {TclCopyChannel} {fcopy} {
6516    file delete $path(test1)
6517    set f1 [open $thisScript]
6518    set f2 [open $path(test1) w]
6519    fconfigure $f1 -translation lf -blocking 0
6520    fconfigure $f2 -translation cr -blocking 0
6521    set s0 [fcopy $f1 $f2]
6522    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6523    close $f1
6524    close $f2
6525    set s1 [file size $thisScript]
6526    set s2 [file size $path(test1)]
6527    if {("$s1" == "$s2") && ($s0 == $s1)} {
6528        lappend result ok
6529    }
6530    set result
6531} {0 0 ok}
6532test io-52.4 {TclCopyChannel} {fcopy} {
6533    file delete $path(test1)
6534    set f1 [open $thisScript]
6535    set f2 [open $path(test1) w]
6536    fconfigure $f1 -translation lf -blocking 0
6537    fconfigure $f2 -translation cr -blocking 0
6538    fcopy $f1 $f2 -size 40
6539    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6540    close $f1
6541    close $f2
6542    lappend result [file size $path(test1)]
6543} {0 0 40}
6544test io-52.5 {TclCopyChannel} {fcopy} {
6545    file delete $path(test1)
6546    set f1 [open $thisScript]
6547    set f2 [open $path(test1) w]
6548    fconfigure $f1 -translation lf -blocking 0
6549    fconfigure $f2 -translation lf -blocking 0
6550    fcopy $f1 $f2 -size -1
6551    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6552    close $f1
6553    close $f2
6554    set s1 [file size $thisScript]
6555    set s2 [file size $path(test1)]
6556    if {"$s1" == "$s2"} {
6557        lappend result ok
6558    }
6559    set result
6560} {0 0 ok}
6561test io-52.6 {TclCopyChannel} {fcopy} {
6562    file delete $path(test1)
6563    set f1 [open $thisScript]
6564    set f2 [open $path(test1) w]
6565    fconfigure $f1 -translation lf -blocking 0
6566    fconfigure $f2 -translation lf -blocking 0
6567    set s0 [fcopy $f1 $f2 -size [expr [file size $thisScript] + 5]]
6568    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6569    close $f1
6570    close $f2
6571    set s1 [file size $thisScript]
6572    set s2 [file size $path(test1)]
6573    if {("$s1" == "$s2") && ($s0 == $s1)} {
6574        lappend result ok
6575    }
6576    set result
6577} {0 0 ok}
6578test io-52.7 {TclCopyChannel} {fcopy} {
6579    file delete $path(test1)
6580    set f1 [open $thisScript]
6581    set f2 [open $path(test1) w]
6582    fconfigure $f1 -translation lf -blocking 0
6583    fconfigure $f2 -translation lf -blocking 0
6584    fcopy $f1 $f2
6585    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6586    set s1 [file size $thisScript]
6587    set s2 [file size $path(test1)]
6588    close $f1
6589    close $f2
6590    if {"$s1" == "$s2"} {
6591        lappend result ok
6592    }
6593    set result
6594} {0 0 ok}
6595test io-52.8 {TclCopyChannel} {stdio openpipe fcopy} {
6596    file delete $path(test1)
6597    file delete $path(pipe)
6598    set f1 [open $path(pipe) w]
6599    fconfigure $f1 -translation lf
6600    puts $f1 "
6601        puts ready
6602        gets stdin
6603        set f1 \[open [list $thisScript] r\]
6604        fconfigure \$f1 -translation lf
6605        puts \[read \$f1 100\]
6606        close \$f1
6607    "
6608    close $f1
6609    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
6610    fconfigure $f1 -translation lf
6611    gets $f1
6612    puts $f1 ready
6613    flush $f1
6614    set f2 [open $path(test1) w]
6615    fconfigure $f2 -translation lf
6616    set s0 [fcopy $f1 $f2 -size 40]
6617    catch {close $f1}
6618    close $f2
6619    list $s0 [file size $path(test1)]
6620} {40 40}
6621# Empty files, to register them with the test facility
6622set path(kyrillic.txt)   [makeFile {} kyrillic.txt]
6623set path(utf8-fcopy.txt) [makeFile {} utf8-fcopy.txt]
6624set path(utf8-rp.txt)    [makeFile {} utf8-rp.txt]
6625# Create kyrillic file, use lf translation to avoid os eol issues
6626set out [open $path(kyrillic.txt) w]
6627fconfigure $out -encoding koi8-r -translation lf
6628puts       $out "\u0410\u0410"
6629close      $out
6630test io-52.9 {TclCopyChannel & encodings} {fcopy} {
6631    # Copy kyrillic to UTF-8, using fcopy.
6632
6633    set in  [open $path(kyrillic.txt) r]
6634    set out [open $path(utf8-fcopy.txt) w]
6635
6636    fconfigure $in  -encoding koi8-r -translation lf
6637    fconfigure $out -encoding utf-8 -translation lf
6638
6639    fcopy $in $out
6640    close $in
6641    close $out
6642
6643    # Do the same again, but differently (read/puts).
6644
6645    set in  [open $path(kyrillic.txt) r]
6646    set out [open $path(utf8-rp.txt) w]
6647
6648    fconfigure $in  -encoding koi8-r -translation lf
6649    fconfigure $out -encoding utf-8 -translation lf
6650
6651    puts -nonewline $out [read $in]
6652
6653    close $in
6654    close $out
6655
6656    list [file size $path(kyrillic.txt)] \
6657            [file size $path(utf8-fcopy.txt)] \
6658            [file size $path(utf8-rp.txt)]
6659} {3 5 5}
6660test io-52.10 {TclCopyChannel & encodings} {fcopy} {
6661    # encoding to binary (=> implies that the
6662    # internal utf-8 is written)
6663
6664    set in  [open $path(kyrillic.txt) r]
6665    set out [open $path(utf8-fcopy.txt) w]
6666
6667    fconfigure $in  -encoding koi8-r -translation lf
6668    # -translation binary is also -encoding binary
6669    fconfigure $out -translation binary
6670
6671    fcopy $in $out
6672    close $in
6673    close $out
6674
6675    file size $path(utf8-fcopy.txt)
6676} 5
6677test io-52.11 {TclCopyChannel & encodings} {fcopy} {
6678    # binary to encoding => the input has to be
6679    # in utf-8 to make sense to the encoder
6680
6681    set in  [open $path(utf8-fcopy.txt) r]
6682    set out [open $path(kyrillic.txt) w]
6683
6684    # -translation binary is also -encoding binary
6685    fconfigure $in  -translation binary
6686    fconfigure $out -encoding koi8-r -translation lf
6687
6688    fcopy $in $out
6689    close $in
6690    close $out
6691
6692    file size $path(kyrillic.txt)
6693} 3
6694
6695test io-53.1 {CopyData} {fcopy} {
6696    file delete $path(test1)
6697    set f1 [open $thisScript]
6698    set f2 [open $path(test1) w]
6699    fconfigure $f1 -translation lf -blocking 0
6700    fconfigure $f2 -translation cr -blocking 0
6701    fcopy $f1 $f2 -size 0
6702    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6703    close $f1
6704    close $f2
6705    lappend result [file size $path(test1)]
6706} {0 0 0}
6707test io-53.2 {CopyData} {fcopy} {
6708    file delete $path(test1)
6709    set f1 [open $thisScript]
6710    set f2 [open $path(test1) w]
6711    fconfigure $f1 -translation lf -blocking 0
6712    fconfigure $f2 -translation cr -blocking 0
6713    fcopy $f1 $f2 -command [namespace code {set s0}]
6714    set result [list [fconfigure $f1 -blocking] [fconfigure $f2 -blocking]]
6715    variable s0
6716    vwait [namespace which -variable s0]
6717    close $f1
6718    close $f2
6719    set s1 [file size $thisScript]
6720    set s2 [file size $path(test1)]
6721    if {("$s1" == "$s2") && ($s0 == $s1)} {
6722        lappend result ok
6723    }
6724    set result
6725} {0 0 ok}
6726test io-53.3 {CopyData: background read underflow} {stdio unix openpipe fcopy} {
6727    file delete $path(test1)
6728    file delete $path(pipe)
6729    set f1 [open $path(pipe) w]
6730    puts -nonewline $f1 {
6731        puts ready
6732        flush stdout                            ;# Don't assume line buffered!
6733        fcopy stdin stdout -command { set x }
6734        vwait x
6735        set f [}
6736    puts $f1 [list open $path(test1) w]]
6737    puts $f1 {
6738        fconfigure $f -translation lf
6739        puts $f "done"
6740        close $f
6741    }
6742    close $f1
6743    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
6744    set result [gets $f1]
6745    puts $f1 line1
6746    flush $f1
6747    lappend result [gets $f1]
6748    puts $f1 line2
6749    flush $f1
6750    lappend result [gets $f1]
6751    close $f1
6752    after 500
6753    set f [open $path(test1)]
6754    lappend result [read $f]
6755    close $f
6756    set result
6757} "ready line1 line2 {done\n}"
6758test io-53.4 {CopyData: background write overflow} {stdio unix openpipe fileevent fcopy} {
6759    set big bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n
6760    variable x
6761    for {set x 0} {$x < 12} {incr x} {
6762        append big $big
6763    }
6764    file delete $path(test1)
6765    file delete $path(pipe)
6766    set f1 [open $path(pipe) w]
6767    puts $f1 {
6768        puts ready
6769        fcopy stdin stdout -command { set x }
6770        vwait x
6771        set f [open $path(test1) w]
6772        fconfigure $f -translation lf
6773        puts $f "done"
6774        close $f
6775    }
6776    close $f1
6777    set f1 [open "|[list [interpreter] $path(pipe)]" r+]
6778    set result [gets $f1]
6779    fconfigure $f1 -blocking 0
6780    puts $f1 $big
6781    flush $f1
6782    after 500
6783    set result ""
6784    fileevent $f1 read [namespace code {
6785        append result [read $f1 1024]
6786        if {[string length $result] >= [string length $big]} {
6787            set x done
6788        }
6789    }]
6790    vwait [namespace which -variable x]
6791    close $f1
6792    set big {}
6793    set x
6794} done
6795set result {}
6796proc FcopyTestAccept {sock args} {
6797    after 1000 "close $sock"
6798}
6799proc FcopyTestDone {bytes {error {}}} {
6800    variable fcopyTestDone
6801    if {[string length $error]} {
6802        set fcopyTestDone 1
6803    } else {
6804        set fcopyTestDone 0
6805    }
6806}
6807test io-53.5 {CopyData: error during fcopy} {socket fcopy} {
6808    variable fcopyTestDone
6809    set listen [socket -server [namespace code FcopyTestAccept] -myaddr 127.0.0.1 0]
6810    set in [open $thisScript]   ;# 126 K
6811    set out [socket 127.0.0.1 [lindex [fconfigure $listen -sockname] 2]]
6812    catch {unset fcopyTestDone}
6813    close $listen       ;# This means the socket open never really succeeds
6814    fcopy $in $out -command [namespace code FcopyTestDone]
6815    variable fcopyTestDone
6816    if ![info exists fcopyTestDone] {
6817        vwait [namespace which -variable fcopyTestDone]         ;# The error occurs here in the b.g.
6818    }
6819    close $in
6820    close $out
6821    set fcopyTestDone   ;# 1 for error condition
6822} 1
6823test io-53.6 {CopyData: error during fcopy} {stdio openpipe fcopy} {
6824    variable fcopyTestDone
6825    file delete $path(pipe)
6826    file delete $path(test1)
6827    catch {unset fcopyTestDone}
6828    set f1 [open $path(pipe) w]
6829    puts $f1 "exit 1"
6830    close $f1
6831    set in [open "|[list [interpreter] $path(pipe)]" r+]
6832    set out [open $path(test1) w]
6833    fcopy $in $out -command [namespace code FcopyTestDone]
6834    variable fcopyTestDone
6835    if ![info exists fcopyTestDone] {
6836        vwait [namespace which -variable fcopyTestDone]
6837    }
6838    catch {close $in}
6839    close $out
6840    set fcopyTestDone   ;# 0 for plain end of file
6841} {0}
6842proc doFcopy {in out {bytes 0} {error {}}} {
6843    variable fcopyTestDone
6844    variable fcopyTestCount
6845    incr fcopyTestCount $bytes
6846    if {[string length $error]} {
6847        set fcopyTestDone 1
6848    } elseif {[eof $in]} {
6849        set fcopyTestDone 0
6850    } else {
6851        # Delay next fcopy to wait for size>0 input bytes
6852        after 100 [list fcopy $in $out -size 1000 \
6853                -command [namespace code [list doFcopy $in $out]]]
6854    }
6855}
6856test io-53.7 {CopyData: Flooding fcopy from pipe} {stdio openpipe fcopy} {
6857    variable fcopyTestDone
6858    file delete $path(pipe)
6859    catch {unset fcopyTestDone}
6860    set fcopyTestCount 0
6861    set f1 [open $path(pipe) w]
6862    puts $f1 {
6863        # Write  10 bytes / 10 msec
6864        proc Write {count} {
6865            puts -nonewline "1234567890"
6866            if {[incr count -1]} {
6867                after 10 [list Write $count]
6868            } else {
6869                set ::ready 1
6870            }
6871        }
6872        fconfigure stdout -buffering none
6873        Write 345 ;# 3450 bytes ~3.45 sec
6874        vwait ready
6875        exit 0
6876    }
6877    close $f1
6878    set in [open "|[list [interpreter] $path(pipe) &]" r+]
6879    set out [open $path(test1) w]
6880    doFcopy $in $out
6881    variable fcopyTestDone
6882    if ![info exists fcopyTestDone] {
6883        vwait [namespace which -variable fcopyTestDone]
6884    }
6885    catch {close $in}
6886    close $out
6887    # -1=error 0=script error N=number of bytes
6888    expr ($fcopyTestDone == 0) ? $fcopyTestCount : -1
6889} {3450}
6890
6891test io-54.1 {Recursive channel events} {socket fileevent} {
6892    # This test checks to see if file events are delivered during recursive
6893    # event loops when there is buffered data on the channel.
6894
6895    proc accept {s a p} {
6896        variable as
6897        fconfigure $s -translation lf
6898        puts $s "line 1\nline2\nline3"
6899        flush $s
6900        set as $s
6901    }
6902    proc readit {s next} {
6903        variable x
6904        variable result
6905        lappend result $next
6906        if {$next == 1} {
6907            fileevent $s readable [namespace code [list readit $s 2]]
6908            vwait [namespace which -variable x]
6909        }
6910        incr x
6911    }
6912    set ss [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
6913
6914    # We need to delay on some systems until the creation of the
6915    # server socket completes.
6916
6917    set done 0
6918    for {set i 0} {$i < 10} {incr i} {
6919        if {![catch {set cs [socket 127.0.0.1 [lindex [fconfigure $ss -sockname] 2]]}]} {
6920            set done 1
6921            break
6922        }
6923        after 100
6924    }
6925    if {$done == 0} {
6926        close $ss
6927        error "failed to connect to server"
6928    }
6929    variable result {}
6930    variable x 0
6931    variable as
6932    vwait [namespace which -variable as]
6933    fconfigure $cs -translation lf
6934    lappend result [gets $cs]
6935    fconfigure $cs -blocking off
6936    fileevent $cs readable [namespace code [list readit $cs 1]]
6937    set a [after 2000 [namespace code { set x failure }]]
6938    vwait [namespace which -variable x]
6939    after cancel $a
6940    close $as
6941    close $ss
6942    close $cs
6943    list $result $x
6944} {{{line 1} 1 2} 2}
6945test io-54.2 {Testing for busy-wait in recursive channel events} {socket fileevent} {
6946    set accept {}
6947    set after {}
6948    variable s [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
6949    proc accept {s a p} {
6950        variable counter
6951        variable accept
6952
6953        set accept $s
6954        set counter 0
6955        fconfigure $s -blocking off -buffering line -translation lf
6956        fileevent $s readable [namespace code "doit $s"]
6957    }
6958    proc doit {s} {
6959        variable counter
6960        variable after
6961
6962        incr counter
6963        set l [gets $s]
6964        if {"$l" == ""} {
6965            fileevent $s readable [namespace code "doit1 $s"]
6966            set after [after 1000 [namespace code newline]]
6967        }
6968    }
6969    proc doit1 {s} {
6970        variable counter
6971        variable accept
6972
6973        incr counter
6974        set l [gets $s]
6975        close $s
6976        set accept {}
6977    }
6978    proc producer {} {
6979        variable s
6980        variable writer
6981
6982        set writer [socket 127.0.0.1 [lindex [fconfigure $s -sockname] 2]]
6983        fconfigure $writer -buffering line
6984        puts -nonewline $writer hello
6985        flush $writer
6986    }
6987    proc newline {} {
6988        variable done
6989        variable writer
6990
6991        puts $writer hello
6992        flush $writer
6993        set done 1
6994    }
6995    producer
6996    variable done
6997    vwait [namespace which -variable done]
6998    close $writer
6999    close $s
7000    after cancel $after
7001    if {$accept != {}} {close $accept}
7002    set counter
7003} 1
7004
7005set path(fooBar) [makeFile {} fooBar]
7006
7007test io-55.1 {ChannelEventScriptInvoker: deletion} -constraints {
7008    fileevent
7009} -setup {
7010    variable x
7011    proc eventScript {fd} {
7012        variable x
7013        close $fd
7014        error "planned error"
7015        set x whoops
7016    }
7017    proc myHandler args {
7018        variable x got_error
7019    }
7020    set handler [interp bgerror {}]
7021    interp bgerror {} [namespace which myHandler]
7022} -body {
7023    set f [open $path(fooBar) w]
7024    fileevent $f writable [namespace code [list eventScript $f]]
7025    variable x not_done
7026    vwait [namespace which -variable x]
7027    set x
7028} -cleanup {
7029    interp bgerror {} $handler
7030} -result {got_error}
7031
7032test io-56.1 {ChannelTimerProc} {testchannelevent} {
7033    set f [open $path(fooBar) w]
7034    puts $f "this is a test"
7035    close $f
7036    set f [open $path(fooBar) r]
7037    testchannelevent $f add readable [namespace code {
7038        read $f 1
7039        incr x
7040    }]
7041    variable x 0
7042    vwait [namespace which -variable x]
7043    vwait [namespace which -variable x]
7044    set result $x
7045    testchannelevent $f set 0 none
7046    after idle [namespace code {set y done}]
7047    variable y
7048    vwait [namespace which -variable y]
7049    close $f
7050    lappend result $y
7051} {2 done}
7052
7053test io-57.1 {buffered data and file events, gets} {fileevent} {
7054    proc accept {sock args} {
7055        variable s2
7056        set s2 $sock
7057    }
7058    set server [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
7059    set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
7060    variable s2
7061    vwait [namespace which -variable s2]
7062    update
7063    fileevent $s2 readable [namespace code {lappend result readable}]
7064    puts $s "12\n34567890"
7065    flush $s
7066    variable result [gets $s2]
7067    after 1000 [namespace code {lappend result timer}]
7068    vwait [namespace which -variable result]
7069    lappend result [gets $s2]
7070    vwait [namespace which -variable result]
7071    close $s
7072    close $s2
7073    close $server
7074    set result
7075} {12 readable 34567890 timer}
7076test io-57.2 {buffered data and file events, read} {fileevent} {
7077    proc accept {sock args} {
7078        variable s2
7079        set s2 $sock
7080    }
7081    set server [socket -server [namespace code accept] -myaddr 127.0.0.1 0]
7082    set s [socket 127.0.0.1 [lindex [fconfigure $server -sockname] 2]]
7083    variable s2
7084    vwait [namespace which -variable s2]
7085    update
7086    fileevent $s2 readable [namespace code {lappend result readable}]
7087    puts -nonewline $s "1234567890"
7088    flush $s
7089    variable result [read $s2 1]
7090    after 1000 [namespace code {lappend result timer}]
7091    vwait [namespace which -variable result]
7092    lappend result [read $s2 9]
7093    vwait [namespace which -variable result]
7094    close $s
7095    close $s2
7096    close $server
7097    set result
7098} {1 readable 234567890 timer}
7099
7100test io-58.1 {Tcl_NotifyChannel and error when closing} {stdio unixOrPc openpipe fileevent} {
7101    set out [open $path(script) w]
7102    puts $out {
7103        puts "normal message from pipe"
7104        puts stderr "error message from pipe"
7105        exit 1
7106    }
7107    proc readit {pipe} {
7108        variable x
7109        variable result
7110        if {[eof $pipe]} {
7111            set x [catch {close $pipe} line]
7112            lappend result catch $line
7113        } else {
7114            gets $pipe line
7115            lappend result gets $line
7116        }
7117    }
7118    close $out
7119    set pipe [open "|[list [interpreter] $path(script)]" r]
7120    fileevent $pipe readable [namespace code [list readit $pipe]]
7121    variable x ""
7122    set result ""
7123    vwait [namespace which -variable x]
7124    list $x $result
7125} {1 {gets {normal message from pipe} gets {} catch {error message from pipe}}}
7126
7127test io-59.1 {Thread reference of channels} {testmainthread testchannel} {
7128    # TIP #10
7129    # More complicated tests (like that the reference changes as a
7130    # channel is moved from thread to thread) can be done only in the
7131    # extension which fully implements the moving of channels between
7132    # threads, i.e. 'Threads'. Or we have to extend [testthread] as well.
7133
7134    set f [open $path(longfile) r]
7135    set result [testchannel mthread $f]
7136    close $f
7137    string equal $result [testmainthread]
7138} {1}
7139
7140test io-60.1 {writing illegal utf sequences} {openpipe fileevent} {
7141    # This test will hang in older revisions of the core.
7142
7143    set out [open $path(script) w]
7144    puts $out {
7145        puts [encoding convertfrom identity \xe2]
7146        exit 1
7147    }
7148    proc readit {pipe} {
7149        variable x
7150        variable result
7151        if {[eof $pipe]} {
7152            set x [catch {close $pipe} line]
7153            lappend result catch $line
7154        } else {
7155            gets $pipe line
7156            lappend result gets $line
7157        }
7158    }
7159    close $out
7160    set pipe [open "|[list [interpreter] $path(script)]" r]
7161    fileevent $pipe readable [namespace code [list readit $pipe]]
7162    variable x ""
7163    set result ""
7164    vwait [namespace which -variable x]
7165
7166    # cut of the remainder of the error stack, especially the filename
7167    set result [lreplace $result 3 3 [lindex [split [lindex $result 3] \n] 0]]
7168    list $x $result
7169} {1 {gets {} catch {error writing "stdout": invalid argument}}}
7170
7171test io-61.1 {Reset eof state after changing the eof char} -setup {
7172    set datafile [makeFile {} eofchar]
7173    set f [open $datafile w]
7174    fconfigure $f -translation binary
7175    puts -nonewline $f [string repeat "Ho hum\n" 11]
7176    puts $f =
7177    set line [string repeat "Ge gla " 4]
7178    puts -nonewline $f [string repeat [string trimright $line]\n 834]
7179    close $f
7180} -body {
7181    set f [open $datafile r]
7182    fconfigure $f -eofchar =
7183    set res {}
7184    lappend res [read $f; tell $f]
7185    fconfigure $f -eofchar {}
7186    lappend res [read $f 1]
7187    lappend res [read $f; tell $f]
7188    # Any seek zaps the internals into a good state.
7189    #seek $f 0 start
7190    #seek $f 0 current
7191    #lappend res [read $f; tell $f]
7192    close $f
7193    set res
7194} -cleanup {
7195    removeFile eofchar
7196} -result {77 = 23431}
7197
7198
7199# Test the cutting and splicing of channels, this is incidentially the
7200# attach/detach facility of package Thread, but __without any
7201# safeguards__. It can also be used to emulate transfer of channels
7202# between threads, and is used for that here.
7203
7204test io-70.0 {Cutting & Splicing channels} {testchannel} {
7205    set f [makeFile {... dummy ...} cutsplice]
7206    set c [open $f r]
7207
7208    set     res {}
7209    lappend res [catch {seek $c 0 start}]
7210    testchannel cut $c
7211
7212    lappend res [catch {seek $c 0 start}]
7213    testchannel splice $c
7214
7215    lappend res [catch {seek $c 0 start}]
7216    close $c
7217
7218    removeFile cutsplice
7219
7220    set res
7221} {0 1 0}
7222
7223
7224# Duplicate of code in "thread.test". Find a better way of doing this
7225# without duplication. Maybe placement into a proc which transforms to
7226# nop after the first call, and placement of its defintion in a
7227# central location.
7228
7229if {[testConstraint testthread]} {
7230    testthread errorproc ThreadError
7231
7232    proc ThreadError {id info} {
7233        global threadError
7234        set threadError $info
7235    }
7236
7237    proc ThreadNullError {id info} {
7238        # ignore
7239    }
7240}
7241
7242test io-70.1 {Transfer channel} {testchannel testthread} {
7243    set f [makeFile {... dummy ...} cutsplice]
7244    set c [open $f r]
7245
7246    set     res {}
7247    lappend res [catch {seek $c 0 start}]
7248    testchannel cut $c
7249    lappend res [catch {seek $c 0 start}]
7250
7251    set tid [testthread create]
7252    testthread send $tid [list set c $c]
7253    lappend res [testthread send $tid {
7254        testchannel splice $c
7255        set res [catch {seek $c 0 start}]
7256        close $c
7257        set res
7258    }]
7259
7260    tcltest::threadReap
7261    removeFile cutsplice
7262
7263    set res
7264} {0 1 0}
7265
7266# ### ### ### ######### ######### #########
7267
7268foreach {n msg expected} {
7269     0 {}                                 {}
7270     1 {{message only}}                   {{message only}}
7271     2 {-options x}                       {-options x}
7272     3 {-options {x y} {the message}}     {-options {x y} {the message}}
7273
7274     4 {-code 1     -level 0 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7275     5 {-code 0     -level 0 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7276     6 {-code 1     -level 5 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7277     7 {-code 0     -level 5 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7278     8 {-code error -level 0 -f ba snarf} {-code error -level 0 -f ba snarf}
7279     9 {-code ok    -level 0 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7280    10 {-code error -level 5 -f ba snarf} {-code error -level 0 -f ba snarf}
7281    11 {-code ok    -level 5 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7282    12 {-code boss  -level 0 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7283    13 {-code boss  -level 5 -f ba snarf} {-code 1     -level 0 -f ba snarf}
7284    14 {-code 1     -level 0 -f ba}       {-code 1     -level 0 -f ba}
7285    15 {-code 0     -level 0 -f ba}       {-code 1     -level 0 -f ba}
7286    16 {-code 1     -level 5 -f ba}       {-code 1     -level 0 -f ba}
7287    17 {-code 0     -level 5 -f ba}       {-code 1     -level 0 -f ba}
7288    18 {-code error -level 0 -f ba}       {-code error -level 0 -f ba}
7289    19 {-code ok    -level 0 -f ba}       {-code 1     -level 0 -f ba}
7290    20 {-code error -level 5 -f ba}       {-code error -level 0 -f ba}
7291    21 {-code ok    -level 5 -f ba}       {-code 1     -level 0 -f ba}
7292    22 {-code boss  -level 0 -f ba}       {-code 1     -level 0 -f ba}
7293    23 {-code boss  -level 5 -f ba}       {-code 1     -level 0 -f ba}
7294    24 {-code 1     -level X -f ba snarf} {-code 1     -level 0 -f ba snarf}
7295    25 {-code 0     -level X -f ba snarf} {-code 1     -level 0 -f ba snarf}
7296    26 {-code error -level X -f ba snarf} {-code error -level 0 -f ba snarf}
7297    27 {-code ok    -level X -f ba snarf} {-code 1     -level 0 -f ba snarf}
7298    28 {-code boss  -level X -f ba snarf} {-code 1     -level 0 -f ba snarf}
7299    29 {-code 1     -level X -f ba}       {-code 1     -level 0 -f ba}
7300    30 {-code 0     -level X -f ba}       {-code 1     -level 0 -f ba}
7301    31 {-code error -level X -f ba}       {-code error -level 0 -f ba}
7302    32 {-code ok    -level X -f ba}       {-code 1     -level 0 -f ba}
7303    33 {-code boss  -level X -f ba}       {-code 1     -level 0 -f ba}
7304
7305    34 {-code 1 -code 1     -level 0 -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7306    35 {-code 1 -code 0     -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7307    36 {-code 1 -code 1     -level 5 -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7308    37 {-code 1 -code 0     -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7309    38 {-code 1 -code error -level 0 -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
7310    39 {-code 1 -code ok    -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7311    40 {-code 1 -code error -level 5 -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
7312    41 {-code 1 -code ok    -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7313    42 {-code 1 -code boss  -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7314    43 {-code 1 -code boss  -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7315    44 {-code 1 -code 1     -level 0 -f ba}       {-code 1 -code 1     -level 0 -f ba}
7316    45 {-code 1 -code 0     -level 0 -f ba}       {-code 1             -level 0 -f ba}
7317    46 {-code 1 -code 1     -level 5 -f ba}       {-code 1 -code 1     -level 0 -f ba}
7318    47 {-code 1 -code 0     -level 5 -f ba}       {-code 1             -level 0 -f ba}
7319    48 {-code 1 -code error -level 0 -f ba}       {-code 1 -code error -level 0 -f ba}
7320    49 {-code 1 -code ok    -level 0 -f ba}       {-code 1             -level 0 -f ba}
7321    50 {-code 1 -code error -level 5 -f ba}       {-code 1 -code error -level 0 -f ba}
7322    51 {-code 1 -code ok    -level 5 -f ba}       {-code 1             -level 0 -f ba}
7323    52 {-code 1 -code boss  -level 0 -f ba}       {-code 1             -level 0 -f ba}
7324    53 {-code 1 -code boss  -level 5 -f ba}       {-code 1             -level 0 -f ba}
7325    54 {-code 1 -code 1     -level X -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7326    55 {-code 1 -code 0     -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7327    56 {-code 1 -code error -level X -f ba snarf} {-code 1 -code error -level 0 -f ba snarf}
7328    57 {-code 1 -code ok    -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7329    58 {-code 1 -code boss  -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7330    59 {-code 1 -code 1     -level X -f ba}       {-code 1 -code 1     -level 0 -f ba}
7331    60 {-code 1 -code 0     -level X -f ba}       {-code 1             -level 0 -f ba}
7332    61 {-code 1 -code error -level X -f ba}       {-code 1 -code error -level 0 -f ba}
7333    62 {-code 1 -code ok    -level X -f ba}       {-code 1             -level 0 -f ba}
7334    63 {-code 1 -code boss  -level X -f ba}       {-code 1             -level 0 -f ba}
7335
7336    64 {-code 0 -code 1     -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7337    65 {-code 0 -code 0     -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7338    66 {-code 0 -code 1     -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7339    67 {-code 0 -code 0     -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7340    68 {-code 0 -code error -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7341    69 {-code 0 -code ok    -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7342    70 {-code 0 -code error -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7343    71 {-code 0 -code ok    -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7344    72 {-code 0 -code boss  -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7345    73 {-code 0 -code boss  -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7346    74 {-code 0 -code 1     -level 0 -f ba}       {-code 1 -level 0 -f ba}
7347    75 {-code 0 -code 0     -level 0 -f ba}       {-code 1 -level 0 -f ba}
7348    76 {-code 0 -code 1     -level 5 -f ba}       {-code 1 -level 0 -f ba}
7349    77 {-code 0 -code 0     -level 5 -f ba}       {-code 1 -level 0 -f ba}
7350    78 {-code 0 -code error -level 0 -f ba}       {-code 1 -level 0 -f ba}
7351    79 {-code 0 -code ok    -level 0 -f ba}       {-code 1 -level 0 -f ba}
7352    80 {-code 0 -code error -level 5 -f ba}       {-code 1 -level 0 -f ba}
7353    81 {-code 0 -code ok    -level 5 -f ba}       {-code 1 -level 0 -f ba}
7354    82 {-code 0 -code boss  -level 0 -f ba}       {-code 1 -level 0 -f ba}
7355    83 {-code 0 -code boss  -level 5 -f ba}       {-code 1 -level 0 -f ba}
7356    84 {-code 0 -code 1     -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7357    85 {-code 0 -code 0     -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7358    86 {-code 0 -code error -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7359    87 {-code 0 -code ok    -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7360    88 {-code 0 -code boss  -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7361    89 {-code 0 -code 1     -level X -f ba}       {-code 1 -level 0 -f ba}
7362    90 {-code 0 -code 0     -level X -f ba}       {-code 1 -level 0 -f ba}
7363    91 {-code 0 -code error -level X -f ba}       {-code 1 -level 0 -f ba}
7364    92 {-code 0 -code ok    -level X -f ba}       {-code 1 -level 0 -f ba}
7365    93 {-code 0 -code boss  -level X -f ba}       {-code 1 -level 0 -f ba}
7366
7367    94 {-code 1     -code 1 -level 0 -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7368    95 {-code 0     -code 1 -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7369    96 {-code 1     -code 1 -level 5 -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7370    97 {-code 0     -code 1 -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7371    98 {-code error -code 1 -level 0 -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
7372    99 {-code ok    -code 1 -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7373    a0 {-code error -code 1 -level 5 -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
7374    a1 {-code ok    -code 1 -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7375    a2 {-code boss  -code 1 -level 0 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7376    a3 {-code boss  -code 1 -level 5 -f ba snarf} {-code 1             -level 0 -f ba snarf}
7377    a4 {-code 1     -code 1 -level 0 -f ba}       {-code 1 -code 1     -level 0 -f ba}
7378    a5 {-code 0     -code 1 -level 0 -f ba}       {-code 1             -level 0 -f ba}
7379    a6 {-code 1     -code 1 -level 5 -f ba}       {-code 1 -code 1     -level 0 -f ba}
7380    a7 {-code 0     -code 1 -level 5 -f ba}       {-code 1             -level 0 -f ba}
7381    a8 {-code error -code 1 -level 0 -f ba}       {-code error -code 1 -level 0 -f ba}
7382    a9 {-code ok    -code 1 -level 0 -f ba}       {-code 1             -level 0 -f ba}
7383    b0 {-code error -code 1 -level 5 -f ba}       {-code error -code 1 -level 0 -f ba}
7384    b1 {-code ok    -code 1 -level 5 -f ba}       {-code 1             -level 0 -f ba}
7385    b2 {-code boss  -code 1 -level 0 -f ba}       {-code 1             -level 0 -f ba}
7386    b3 {-code boss  -code 1 -level 5 -f ba}       {-code 1             -level 0 -f ba}
7387    b4 {-code 1     -code 1 -level X -f ba snarf} {-code 1 -code 1     -level 0 -f ba snarf}
7388    b5 {-code 0     -code 1 -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7389    b6 {-code error -code 1 -level X -f ba snarf} {-code error -code 1 -level 0 -f ba snarf}
7390    b7 {-code ok    -code 1 -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7391    b8 {-code boss  -code 1 -level X -f ba snarf} {-code 1             -level 0 -f ba snarf}
7392    b9 {-code 1     -code 1 -level X -f ba}       {-code 1 -code 1     -level 0 -f ba}
7393    c0 {-code 0     -code 1 -level X -f ba}       {-code 1             -level 0 -f ba}
7394    c1 {-code error -code 1 -level X -f ba}       {-code error -code 1 -level 0 -f ba}
7395    c2 {-code ok    -code 1 -level X -f ba}       {-code 1             -level 0 -f ba}
7396    c3 {-code boss  -code 1 -level X -f ba}       {-code 1             -level 0 -f ba}
7397
7398    c4 {-code 1     -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7399    c5 {-code 0     -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7400    c6 {-code 1     -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7401    c7 {-code 0     -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7402    c8 {-code error -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7403    c9 {-code ok    -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7404    d0 {-code error -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7405    d1 {-code ok    -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7406    d2 {-code boss  -code 0 -level 0 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7407    d3 {-code boss  -code 0 -level 5 -f ba snarf} {-code 1 -level 0 -f ba snarf}
7408    d4 {-code 1     -code 0 -level 0 -f ba}       {-code 1 -level 0 -f ba}
7409    d5 {-code 0     -code 0 -level 0 -f ba}       {-code 1 -level 0 -f ba}
7410    d6 {-code 1     -code 0 -level 5 -f ba}       {-code 1 -level 0 -f ba}
7411    d7 {-code 0     -code 0 -level 5 -f ba}       {-code 1 -level 0 -f ba}
7412    d8 {-code error -code 0 -level 0 -f ba}       {-code 1 -level 0 -f ba}
7413    d9 {-code ok    -code 0 -level 0 -f ba}       {-code 1 -level 0 -f ba}
7414    e0 {-code error -code 0 -level 5 -f ba}       {-code 1 -level 0 -f ba}
7415    e1 {-code ok    -code 0 -level 5 -f ba}       {-code 1 -level 0 -f ba}
7416    e2 {-code boss  -code 0 -level 0 -f ba}       {-code 1 -level 0 -f ba}
7417    e3 {-code boss  -code 0 -level 5 -f ba}       {-code 1 -level 0 -f ba}
7418    e4 {-code 1     -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7419    e5 {-code 0     -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7420    e6 {-code error -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7421    e7 {-code ok    -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7422    e8 {-code boss  -code 0 -level X -f ba snarf} {-code 1 -level 0 -f ba snarf}
7423    e9 {-code 1     -code 0 -level X -f ba}       {-code 1 -level 0 -f ba}
7424    f0 {-code 0     -code 0 -level X -f ba}       {-code 1 -level 0 -f ba}
7425    f1 {-code error -code 0 -level X -f ba}       {-code 1 -level 0 -f ba}
7426    f2 {-code ok    -code 0 -level X -f ba}       {-code 1 -level 0 -f ba}
7427    f3 {-code boss  -code 0 -level X -f ba}       {-code 1 -level 0 -f ba}
7428} {
7429    test io-71.$n {Tcl_SetChannelError} {testchannel} {
7430
7431        set f [makeFile {... dummy ...} cutsplice]
7432        set c [open $f r]
7433
7434        set res [testchannel setchannelerror $c [lrange $msg 0 end]]
7435        close $c
7436        removeFile cutsplice
7437
7438        set res
7439    } [lrange $expected 0 end]
7440
7441    test io-72.$n {Tcl_SetChannelErrorInterp} {testchannel} {
7442
7443        set f [makeFile {... dummy ...} cutsplice]
7444        set c [open $f r]
7445
7446        set res [testchannel setchannelerrorinterp $c [lrange $msg 0 end]]
7447        close $c
7448        removeFile cutsplice
7449
7450        set res
7451    } [lrange $expected 0 end]
7452}
7453
7454test io-73.1 {channel Tcl_Obj SetChannelFromAny} {} {
7455    # Test for Bug 1847044 - don't spoil type unless we have a valid channel
7456    catch {close [lreplace [list a] 0 end]}
7457} {1}
7458
7459# ### ### ### ######### ######### #########
7460
7461# cleanup
7462foreach file [list fooBar longfile script output test1 pipe my_script foo \
7463        bar test2 test3 cat stdout kyrillic.txt utf8-fcopy.txt utf8-rp.txt] {
7464    removeFile $file
7465}
7466cleanupTests
7467}
7468namespace delete ::tcl::test::io
7469return
Note: See TracBrowser for help on using the repository browser.