Source-Changes-HG archive

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

[src/trunk]: src/lib/libcurses curses: unify resize handling in getch



details:   https://anonhg.NetBSD.org/src/rev/d18ea501d154
branches:  trunk
changeset: 993732:d18ea501d154
user:      roy <roy%NetBSD.org@localhost>
date:      Thu Sep 27 14:05:26 2018 +0000

description:
curses: unify resize handling in getch

Instead of testing each fgetc call for resize event, add the wrapper
__fgetc_resize to simplify the logic.
While here, ensure that get_wch uses the correct input stream which
may or may not be stdin.

diffstat:

 lib/libcurses/curses_private.h |   3 +-
 lib/libcurses/get_wch.c        |  66 +++++++++++++++++++++++++----------------
 lib/libcurses/getch.c          |  62 +++++++++++++++++++++------------------
 3 files changed, 75 insertions(+), 56 deletions(-)

diffs (256 lines):

diff -r d943615a5cc3 -r d18ea501d154 lib/libcurses/curses_private.h
--- a/lib/libcurses/curses_private.h    Thu Sep 27 13:04:21 2018 +0000
+++ b/lib/libcurses/curses_private.h    Thu Sep 27 14:05:26 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: curses_private.h,v 1.62 2017/01/31 09:17:53 roy Exp $  */
+/*     $NetBSD: curses_private.h,v 1.63 2018/09/27 14:05:26 roy Exp $  */
 
 /*-
  * Copyright (c) 1998-2000 Brett Lymn
@@ -365,6 +365,7 @@
 void   __cursesi_putnsp(nschar_t *, const int, const int);
 void   __cursesi_chtype_to_cchar(chtype, cchar_t *);
 #endif /* HAVE_WCHAR */
+int     __fgetc_resize(FILE *);
 int     __unget(wint_t);
 int     __mvcur(int, int, int, int, int);
 WINDOW  *__newwin(SCREEN *, int, int, int, int, int);
diff -r d943615a5cc3 -r d18ea501d154 lib/libcurses/get_wch.c
--- a/lib/libcurses/get_wch.c   Thu Sep 27 13:04:21 2018 +0000
+++ b/lib/libcurses/get_wch.c   Thu Sep 27 14:05:26 2018 +0000
@@ -1,4 +1,4 @@
-/*   $NetBSD: get_wch.c,v 1.16 2018/09/26 14:42:22 kamil Exp $ */
+/*   $NetBSD: get_wch.c,v 1.17 2018/09/27 14:05:26 roy Exp $ */
 
 /*
  * Copyright (c) 2005 The NetBSD Foundation Inc.
@@ -36,7 +36,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: get_wch.c,v 1.16 2018/09/26 14:42:22 kamil Exp $");
+__RCSID("$NetBSD: get_wch.c,v 1.17 2018/09/27 14:05:26 roy Exp $");
 #endif                                           /* not lint */
 
 #include <errno.h>
@@ -56,6 +56,7 @@
 /* prototypes for private functions */
 #ifdef HAVE_WCHAR
 static int inkey(wchar_t *wc, int to, int delay);
+static wint_t __fgetwc_resize(FILE *infd, bool *resized);
 #endif /* HAVE_WCHAR */
 
 #ifdef HAVE_WCHAR
@@ -100,14 +101,10 @@
                if (wstate == INKEY_NORM) {
                        if (delay && __timeout(delay) == ERR)
                                return ERR;
-                       c = fgetc(infd);
-                       if (c == WEOF) {
+                       c = __fgetc_resize(infd);
+                       if (c == ERR || c == KEY_RESIZE) {
                                clearerr(infd);
-                               if (errno == EINTR && _cursesi_screen->resized) {
-                                       _cursesi_screen->resized = 0;
-                                       return KEY_RESIZE;
-                               } else
-                                       return ERR;
+                               return c;
                        }
 
                        if (delay && (__notimeout() == ERR))
@@ -152,14 +149,10 @@
                                        return ERR;
                        }
 
-                       c = fgetc(infd);
+                       c = __fgetc_resize(infd);
                        if (ferror(infd)) {
                                clearerr(infd);
-                               if (errno == EINTR && _cursesi_screen->resized) {
-                                       _cursesi_screen->resized = 0;
-                                       return KEY_RESIZE;
-                               } else
-                                       return ERR;
+                               return c;
                        }
 
                        if ((to || delay) && (__notimeout() == ERR))
@@ -206,10 +199,10 @@
                                        return ERR;
                        }
 
-                       c = fgetc(infd);
+                       c = __fgetc_resize(infd);
                        if (ferror(infd)) {
                                clearerr(infd);
-                               return ERR;
+                               return c;
                        }
 
                        if ((to || delay) && (__notimeout() == ERR))
@@ -583,6 +576,8 @@
                if ( ret == ERR )
                        return ERR;
        } else {
+               bool resized;
+
                switch (win->delay) {
                        case -1:
                                break;
@@ -596,17 +591,11 @@
                                break;
                }
 
-               c = getwchar();
-               if (feof(infd)) {
+               c = __fgetwc_resize(infd, &resized);
+               if (c == WEOF) {
                        clearerr(infd);
                        __restore_termios();
-                       return ERR;     /* we have timed out */
-               }
-
-               if (ferror(infd)) {
-                       clearerr(infd);
-                       if (errno == EINTR && _cursesi_screen->resized) {
-                               _cursesi_screen->resized = 0;
+                       if (resized) {
                                *ch = KEY_RESIZE;
                                return KEY_CODE_YES;
                        } else
@@ -674,3 +663,28 @@
 {
        return __unget((wint_t)c);
 }
+
+#ifdef HAVE_WCHAR
+/*
+ * __fgetwc_resize --
+ *    Any call to fgetwc(3) should use this function instead.
+ */
+static wint_t
+__fgetwc_resize(FILE *infd, bool *resized)
+{
+       wint_t c;
+
+       c = fgetwc(infd);
+       if (c != WEOF)
+               return c;
+
+       if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized)
+               return ERR;
+#ifdef DEBUG
+       __CTRACE(__CTRACE_INPUT, "__fgetwc_resize returning KEY_RESIZE\n");
+#endif
+       _cursesi_screen->resized = 0;
+       *resized = true;
+       return c;
+}
+#endif
diff -r d943615a5cc3 -r d18ea501d154 lib/libcurses/getch.c
--- a/lib/libcurses/getch.c     Thu Sep 27 13:04:21 2018 +0000
+++ b/lib/libcurses/getch.c     Thu Sep 27 14:05:26 2018 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getch.c,v 1.67 2018/09/26 14:42:22 kamil Exp $ */
+/*     $NetBSD: getch.c,v 1.68 2018/09/27 14:05:26 roy Exp $   */
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)getch.c    8.2 (Berkeley) 5/4/94";
 #else
-__RCSID("$NetBSD: getch.c,v 1.67 2018/09/26 14:42:22 kamil Exp $");
+__RCSID("$NetBSD: getch.c,v 1.68 2018/09/27 14:05:26 roy Exp $");
 #endif
 #endif                                 /* not lint */
 
@@ -560,14 +560,10 @@
                if (state == INKEY_NORM) {
                        if (delay && __timeout(delay) == ERR)
                                return ERR;
-                       c = fgetc(infd);
-                       if (c == EOF) {
+                       c = __fgetc_resize(infd);
+                       if (c == ERR || c == KEY_RESIZE) {
                                clearerr(infd);
-                               if (errno == EINTR && _cursesi_screen->resized) {
-                                       _cursesi_screen->resized = 0;
-                                       return KEY_RESIZE;
-                               } else
-                                       return ERR;
+                               return c;
                        }
 
                        if (delay && (__notimeout() == ERR))
@@ -606,14 +602,10 @@
                                        return ERR;
                        }
 
-                       c = fgetc(infd);
+                       c = __fgetc_resize(infd);
                        if (ferror(infd)) {
                                clearerr(infd);
-                               if (errno == EINTR && _cursesi_screen->resized) {
-                                       _cursesi_screen->resized = 0;
-                                       return KEY_RESIZE;
-                               } else
-                                       return ERR;
+                               return c;
                        }
 
                        if ((to || delay) && (__notimeout() == ERR))
@@ -889,22 +881,11 @@
                        break;
                }
 
-               c = fgetc(infd);
-               if (feof(infd)) {
+               inp = __fgetc_resize(infd);
+               if (inp == ERR || inp == KEY_RESIZE) {
                        clearerr(infd);
                        __restore_termios();
-                       return ERR;     /* we have timed out */
-               }
-
-               if (ferror(infd)) {
-                       clearerr(infd);
-                       if (errno == EINTR && _cursesi_screen->resized) {
-                               _cursesi_screen->resized = 0;
-                               inp = KEY_RESIZE;
-                       } else
-                               inp = ERR;
-               } else {
-                       inp = c;
+                       return inp;
                }
        }
 #ifdef DEBUG
@@ -1011,3 +992,26 @@
        ESCDELAY = escdelay;
        return OK;
 }
+
+/*
+ * __fgetc_resize --
+ *    Any call to fgetc(3) should use this function instead
+ *    and test for the return value of KEY_RESIZE as well as ERR.
+ */
+int
+__fgetc_resize(FILE *infd)
+{
+       int c;
+
+       c = fgetc(infd);
+       if (c != EOF)
+               return c;
+
+       if (!ferror(infd) || errno != EINTR || !_cursesi_screen->resized)
+               return ERR;
+#ifdef DEBUG
+       __CTRACE(__CTRACE_INPUT, "__fgetc_resize returning KEY_RESIZE\n");
+#endif
+       _cursesi_screen->resized = 0;
+       return KEY_RESIZE;
+}



Home | Main Index | Thread Index | Old Index