bug-ncurses
[Top][All Lists]
Advanced

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

Re: raw() ignored while in getnstr()


From: Thomas Dickey
Subject: Re: raw() ignored while in getnstr()
Date: Sat, 15 May 2021 11:10:17 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Fri, May 14, 2021 at 11:24:59PM -0400, Bill Gray wrote:
>    Not sure if this is a bug,  peculiarity,  or feature.
> 
>    At least on my Xubuntu 20.04 box,  raw() ensures that Ctrl-C is
> not caught as an interrupt causing immediate shutdown.  The exception
> is when we're in getnstr().  Run the following minimal example,  and
> you can hit keys and get their ASCII values on the screen,  including
> Ctrl-C,  with no shutdown.
> 
>    Hit 'q',  and the program asks you to enter some text,  using
> getnstr().  At this point,  even though raw() is set,  Ctrl-C aborts.
> This happens in xterm and the Linux console.
> 
>    PDCurses doesn't do this (raw() means raw(),  even in getnstr()).
> Is there some reason ncurses behaves this way?

ncurses does this because it handles the left-cursor key (KEY_LEFT):

       The  user's  erase and kill characters are interpreted.  If keypad mode
       is on for the window, KEY_LEFT and KEY_BACKSPACE  are  both  considered
       equivalent to the user's kill character.
 
which agrees with the description here:

        https://pubs.opengroup.org/onlinepubs/7908799/xcurses/getnstr.html

        The user's erase and kill characters are interpreted, as well as any
        special keys (such as function keys, home key, clear key, and so on).

The short answer is the special keys have gotten all of the attention in this
area, and that because raw mode is associated with no timeout, which interferes
with interpreting special keys, it was never an issue.  (After all, X/Open
says that erase (^H) and kill (^U) are interpreted, then why not the interrupt
and quit characters).

In a quick check with Solaris 10, its X/Open curses does not interpret special
keys with/without raw mode.  However, the SVr4 (default) curses does this:

        a) when raw mode is enabled, the cursor-keys return control characters
           from getch:
                up = ^C
                down = ^B
                left = ^D
                right = ^E
           
           (which ncurses does...), and in wgetnstr (it has this rather than
           "getnstr"), the left-cursor key erases the last character in the
           buffer (which is essentially what ncurses does).

           However, ^C does not kill the program (unlike ncurses).

        b) when raw mode is not enabled, SVr4 curses behaves the same way
           for the wgetnstr call.  The getch calls return the individual
           characters (e.g., giving "A" rather than "^C" for the display
           of up-cursor in that loop).

Checking a little further, HPUX gives the same result in the loop as Solaris,
(up=^C, etc), but AIX does not (it shows me the final character of the key
sequence either way).  But both of those agree with the two Solaris curses
libraries by not stopping the program when ^C is typed in wgetnstr.  AIX
and HPUX have only the X/Open curses, but there's some difference in how
they got there.

Finally, NetBSD sort-of agrees with the others (does not quit on ^C).
But it doesn't echo ^C or other control characters as the others do.
So I'd have to regard this as just a neglected area of the library
rather than done intentionally.

> Thanks!           -- Bill
> 
> #include <stdlib.h>
> #include <string.h>
> #include <curses.h>
> 
> int main( const int argc, const char *argv[])
> {
>     char text[80], c = 0;
> 
>     initscr();
>     cbreak( );
>     noecho( );
>     clear( );
>     raw( );
>     keypad( stdscr, 1);
>     mvaddstr( 4, 2, "Hit Ctrl-C and the program won't abort.");
>     while( c != 'q')
>         {
>         c = getch( );
>         sprintf( text, "%d (%c)  ", c, c);
>         mvaddstr( 5, 2, text);
>         }
>     mvaddstr( 4, 2, "Hit Ctrl-C and the program will now abort.");
>     mvaddstr( 5, 2, "Enter some text below:");
>     move( 6, 2);
>     echo( );
>     getnstr( text, sizeof( text) - 1);
>     endwin( );
>     return( 0);
> }
> 

-- 
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]