Re: gets, fconfigure question



Stuart <bigdakine@xxxxxxx> wrote:


Hello,

I have Tcl/Tk scrip that sleeps fro 20s and wakes up to read a log
file.

THis log file is being written to quite often, so that sometimes when
the Tcl script wakes up, it may read to the end of file, before reading
the end of line. According to the man pages, if one uses fconfigure on
the channel ID to make the channel non-blocking, then in the case gets
reads to the end of file before end of line, it will return -1. In
blocking mode it should give a positive result.

I've examined the behavior with blocking turned off.

It does not appear to do what the documentation suggest it should do.
gets still stuffs an incomplete line into the variable. And yes a
variable name is specified with the gets command.

Perhaps I've misunderstood the documentation.

At any rate, I'd rather gets return a -1 if the EOL occurs before EOF.

Any comments appreciated,

Stuart


I think what you are seeing is that files don't really support the
non-blocking mode -- this is an operating system thing.

So if you read the documentation again, what you are seeing is the
EOF action, not the short read action that would be seen on a channel
that supports non-blocking.

Your trick, in other words, would work fine on a tcp channel.

Lets set up an experiment: first of all, the file "lines" contains

line 1\nline 2

that is, line 2 is a partial line. Then we have the following script
in "read.tcl"

fconfigure stdin -blocking 0
while true {
set l [gets stdin v]
puts "$l $v"
if {[eof stdin]} break
after 1000
}

Let's try some commands from the Unix shell:

[~/junk] $ tcl read.tcl <lines
6 line 1
6 line 2

OK, files don't support non-blocking.

[~/junk] $ cat lines | tcl read.tcl
6 line 1
6 line 2

But pipes do; however, in this case EOF precludes the short read.

[~/junk] $ (cat lines; sleep 3) | tcl read.tcl
6 line 1
-1
-1
6 line 2

Here we delay EOF by 3 seconds, and you see the short read semantics.

[~/junk] $ (cat lines; sleep 3; cat lines) | tcl read.tcl
6 line 1
-1
-1
12 line 2line 1
6 line 2

Here we simulate extending the file, and all is well.

.



Relevant Pages