- for (;;) {
- T(("reading %d of %d", (int) count + 1, (int) sizeof(buffer)));
- code = _nc_wgetch(win, &value, TRUE EVENTLIST_2nd((_nc_eventlist *) 0));
- if (code == ERR) {
- break;
- } else if (code == KEY_CODE_YES) {
- /*
- * If we were processing an incomplete multibyte character, return
- * an error since we have a KEY_xxx code which interrupts it. For
- * some cases, we could improve this by writing a new version of
- * lib_getch.c(!), but it is not clear whether the improvement
- * would be worth the effort.
- */
- if (count != 0) {
- ungetch((int) value);
+ _nc_lock_global(curses);
+ sp = _nc_screen_of(win);
+ if (sp != 0) {
+ for (;;) {
+ T(("reading %d of %d", (int) count + 1, (int) sizeof(buffer)));
+ code = _nc_wgetch(win, &value, TRUE EVENTLIST_2nd((_nc_eventlist
+ *) 0));
+ if (code == ERR) {
+ break;
+ } else if (code == KEY_CODE_YES) {
+ /*
+ * If we were processing an incomplete multibyte character,
+ * return an error since we have a KEY_xxx code which
+ * interrupts it. For some cases, we could improve this by
+ * writing a new version of lib_getch.c(!), but it is not clear
+ * whether the improvement would be worth the effort.
+ */
+ if (count != 0) {
+ _nc_ungetch(sp, (int) value);
+ code = ERR;
+ }
+ break;
+ } else if (count + 1 >= sizeof(buffer)) {
+ _nc_ungetch(sp, (int) value);