bug-ncurses
[Top][All Lists]
Advanced

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

Re: ABI/API wgetch: indiscernible return (timer/EOF)


From: Thomas Dickey
Subject: Re: ABI/API wgetch: indiscernible return (timer/EOF)
Date: Wed, 12 Aug 2020 17:37:11 -0400
User-agent: NeoMutt/20170113 (1.7.2)

On Tue, Aug 11, 2020 at 09:09:12AM +0200, Leon Winter wrote:
> Hi Thomas,
> 
> the function wgetch returns ERR and errno set to zero if the timer expired.
> Unfortunately the same the return happens when EOF is reached on stdin :(
> (Actually errno is not set to zero but rather not set at all, but best 
> practise
> dictates one zeroes it before calling errno-setting functions)

maybe - actually common practice is to only look at errno if one
calls a function which returns a value which is documented to set
errno, e.g., checking after read returns a -1, but never checking
if read returns a 0.

That's done so that errno can be propagated up a few levels.

Now... wgetch is only documented to set errno to EINTR

There's no applicable errno for end-of-file (that's why there's feof).

There's a couple of timer-related errno's defined, but they seem to
be for different purposes:

       ETIME           Timer expired.  (POSIX.1 (XSI STREAMS option))

                       (POSIX.1 says "STREAM ioctl(2) timeout")

       ETIMEDOUT       Connection timed out (POSIX.1).

fwiw, SVr4 curses doesn't set errno, except for some (undocumented...)
case in setupterm.  It does have its own curs_errno and term_errno,
which of course aren't in X/Open Curses.

> 
> See the following example:
> 
> // gcc main.c -lncurses
> #include <curses.h>
> #include <errno.h>
> #include <unistd.h>
> 
> void
> read_key (WINDOW *const w)
> { errno = 0;
>   const int key = wgetch (w);
>   if (key == ERR) fprintf (stderr, "wgetch = ERR, errno = %d\n", errno);
> }
> 
> int
> main (int argc, char **argv)
> { initscr ();
>   cbreak ();
>   noecho ();
> 
>   WINDOW *const w = newwin (1, 1, 0, 0);
>   keypad (w, true);
> 
>   int fd[2];
>   if (pipe (fd) == -1) return 1;
>   if (dup2 (fd[0], 0) == -1) return 1;
> 
>   wtimeout (w, 1);
>   read_key (w);
> 
>   wtimeout (w, -1);
>   close (fd[1]);
>   read_key (w);
> 
>   endwin ();
>   return 0;
> }
> 
> I would be quite nice for the caller to able to tell them apart.
> 
> Regards,
> Leon
> 

-- 
Thomas E. Dickey <dickey@invisible-island.net>
https://invisible-island.net
ftp://ftp.invisible-island.net

Attachment: signature.asc
Description: PGP signature


reply via email to

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