octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Re: New function proposal


From: michael . goffioul
Subject: RE: Re: New function proposal
Date: Thu, 15 Feb 2007 14:14:05 +0100

> Ok, I'm now compiling this. However, I'd like to add a unit test to the
> popen2 code. Taking the example code from popen2 and modifying it
> slightly I have a test like
[snip]
> However, this of course won't work on MSVC..
 
Actually, there's a "sort.exe" program in C:\\WINDOWS\\system32.
I don't know if it's standard, but with its location, I'd guess it is. The
difference is of course with the option flags: /R would probably be OK.
 
However, while trying this, I noticed that Win32 does not handle non-
blocking pipes as UNIX does, especially regarding error reporting:
when a read error occurs, it always looks like an EOF and errno is
set to EINVAL, whether it's because there's nothing to read on the
pipe or the pipe is broken. Up to now, I didn't find a reliable way to
distinguish both situations; from octave:
errno -> EINVAL
feof -> true
ferror -> "fgets: end-of-file detected"
 
Looking on MSDN, this looks like the MSVC-way to handle things.
You can have a look at these links
http://msdn2.microsoft.com/fr-fr/library/kt0etdcs(VS.80).aspx
http://msdn2.microsoft.com/fr-fr/library/ksazx244(VS.80).aspx
 
One way to work-around this would be to use blocking pipe instead
(I added a 3rd argument to popen2). This works for me:
[in, out, pid] = popen2 ("sort", "/R", 1);
fputs (in, "these\\nare\\nsome\\nstrings\\n");
fclose (in);
EAGAIN = errno ("EAGAIN");
done = false;
str = {};
idx= 0;
do
    s = fgets (out);
    if (ischar (s))
        idx++;
        str{idx} = s;
    elseif (errno () == EAGAIN)
        sleep (0.1);
        fclear (out);
    else
        done = true;
    endif
until (done)
fclose (out);
assert(str,{"these\\r\\n","strings\\r\\n","some\\r\\n","are\\r\\n"})

Note however the change in new-line characters. This is due to the
fact that in my patch, I opened the pipes in binary mode. Maybe this
could also be made configurable (either 3rd argument of popen2
becomes a bit-set, or a 4th argument).
 
Michael.
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]