Module Name: src
Committed By: roy
Date: Thu Sep 27 14:05:26 UTC 2018
Modified Files:
src/lib/libcurses: curses_private.h get_wch.c getch.c
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/lib/libcurses/curses_private.h
cvs rdiff -u -r1.16 -r1.17 src/lib/libcurses/get_wch.c
cvs rdiff -u -r1.67 -r1.68 src/lib/libcurses/getch.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libcurses/curses_private.h
diff -u src/lib/libcurses/curses_private.h:1.62 src/lib/libcurses/curses_private.h:1.63
--- src/lib/libcurses/curses_private.h:1.62 Tue Jan 31 09:17:53 2017
+++ src/lib/libcurses/curses_private.h Thu Sep 27 14:05:26 2018
@@ -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_win_free_nsp(WINDOW *);
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);
Index: src/lib/libcurses/get_wch.c
diff -u src/lib/libcurses/get_wch.c:1.16 src/lib/libcurses/get_wch.c:1.17
--- src/lib/libcurses/get_wch.c:1.16 Wed Sep 26 14:42:22 2018
+++ src/lib/libcurses/get_wch.c Thu Sep 27 14:05:26 2018
@@ -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 @@ extern short state; /* storage declared
/* 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 @@ inkey(wchar_t *wc, int to, int delay)
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 @@ inkey(wchar_t *wc, int to, int delay)
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 @@ inkey(wchar_t *wc, int to, int delay)
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 @@ wget_wch(WINDOW *win, wint_t *ch)
if ( ret == ERR )
return ERR;
} else {
+ bool resized;
+
switch (win->delay) {
case -1:
break;
@@ -596,17 +591,11 @@ wget_wch(WINDOW *win, wint_t *ch)
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 @@ unget_wch(const wchar_t c)
{
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
Index: src/lib/libcurses/getch.c
diff -u src/lib/libcurses/getch.c:1.67 src/lib/libcurses/getch.c:1.68
--- src/lib/libcurses/getch.c:1.67 Wed Sep 26 14:42:22 2018
+++ src/lib/libcurses/getch.c Thu Sep 27 14:05:26 2018
@@ -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 @@ reread:
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 @@ reread:
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 @@ wgetch(WINDOW *win)
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 @@ set_escdelay(int escdelay)
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;
+}