Source-Changes-HG archive

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

[src/trunk]: src/lib/libcurses * Added ungetch



details:   https://anonhg.NetBSD.org/src/rev/37cd7479fd0f
branches:  trunk
changeset: 485214:37cd7479fd0f
user:      blymn <blymn%NetBSD.org@localhost>
date:      Sat Apr 22 14:32:44 2000 +0000

description:
* Added ungetch
* Converted inkey and getch to use getchar instead of read so ungetch
  will work.

diffstat:

 lib/libcurses/curses.3 |   3 +-
 lib/libcurses/curses.h |   3 +-
 lib/libcurses/getch.c  |  57 +++++++++++++++++++++++++++++++------------------
 3 files changed, 40 insertions(+), 23 deletions(-)

diffs (165 lines):

diff -r 693135fb73fc -r 37cd7479fd0f lib/libcurses/curses.3
--- a/lib/libcurses/curses.3    Sat Apr 22 13:29:01 2000 +0000
+++ b/lib/libcurses/curses.3    Sat Apr 22 14:32:44 2000 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: curses.3,v 1.20 2000/04/22 13:29:01 blymn Exp $
+.\"    $NetBSD: curses.3,v 1.21 2000/04/22 14:32:44 blymn Exp $
 .\"
 .\" Copyright (c) 1985, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -226,6 +226,7 @@
 .Em ch
 .It underend() end underscore mode
 .It underscore()       start underscore mode
+.It ungetch(ch)                Put character back onto input queue.
 .It waddch(win,ch)     add char to
 .Em win
 .It waddstr(win,str)   add string to
diff -r 693135fb73fc -r 37cd7479fd0f lib/libcurses/curses.h
--- a/lib/libcurses/curses.h    Sat Apr 22 13:29:01 2000 +0000
+++ b/lib/libcurses/curses.h    Sat Apr 22 14:32:44 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: curses.h,v 1.40 2000/04/22 13:29:01 blymn Exp $        */
+/*     $NetBSD: curses.h,v 1.41 2000/04/22 14:32:45 blymn Exp $        */
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -556,6 +556,7 @@
 int     touchline(WINDOW *win, int start, int count);
 int     touchoverlap(WINDOW *win1, WINDOW *win2);
 int     touchwin(WINDOW *win);
+int      ungetch(int c);
 int     vwprintw(WINDOW *win, const char *fmt, _BSD_VA_LIST_);
 int     vwscanw(WINDOW *win, const char *fmt, _BSD_VA_LIST_);
 int     waddch(WINDOW *win, chtype ch);
diff -r 693135fb73fc -r 37cd7479fd0f lib/libcurses/getch.c
--- a/lib/libcurses/getch.c     Sat Apr 22 13:29:01 2000 +0000
+++ b/lib/libcurses/getch.c     Sat Apr 22 14:32:44 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getch.c,v 1.21 2000/04/18 21:44:48 jdc Exp $   */
+/*     $NetBSD: getch.c,v 1.22 2000/04/22 14:32:45 blymn Exp $ */
 
 /*
  * Copyright (c) 1981, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)getch.c    8.2 (Berkeley) 5/4/94";
 #else
-__RCSID("$NetBSD: getch.c,v 1.21 2000/04/18 21:44:48 jdc Exp $");
+__RCSID("$NetBSD: getch.c,v 1.22 2000/04/22 14:32:45 blymn Exp $");
 #endif
 #endif                                 /* not lint */
 
@@ -392,8 +392,7 @@
 inkey(int to, int delay)
 {
        wchar_t          k;
-       ssize_t          nchar;
-       unsigned char    c;
+       int              c;
        keymap_t        *current = base_keymap;
 
        for (;;) {              /* loop until we get a complete key sequence */
@@ -401,13 +400,14 @@
                if (state == INKEY_NORM) {
                        if (delay && __timeout(delay) == ERR)
                                return ERR;
-                       if ((nchar = read(STDIN_FILENO, &c, sizeof(char))) < 0)
+                       if ((c = getchar()) == EOF) {
+                               clearerr(stdin);
                                return ERR;
+                       }
+                       
                        if (delay && (__notimeout() == ERR))
                                return ERR;
-                       if (nchar == 0)
-                               return ERR;     /* just in case we are nodelay
-                                                * mode */
+
                        k = (wchar_t) c;
 #ifdef DEBUG
                        __CTRACE("inkey (state normal) got '%s'\n", unctrl(k));
@@ -439,9 +439,13 @@
                                if (to && (__timeout(DEFAULT_DELAY) == ERR))
                                        return ERR;
                        }
-                       if ((nchar = read(STDIN_FILENO, &c,
-                                         sizeof(char))) < 0)
+
+                       c = getchar();
+                       if (ferror(stdin)) {
+                               clearerr(stdin);
                                return ERR;
+                       }
+                       
                        if ((to || delay) && (__notimeout() == ERR))
                                        return ERR;
 
@@ -449,8 +453,9 @@
 #ifdef DEBUG
                        __CTRACE("inkey (state assembling) got '%s'\n", unctrl(k));
 #endif
-                       if (nchar == 0) {       /* inter-char timeout,
+                       if (feof(stdin)) {      /* inter-char timeout,
                                                 * start backing out */
+                               clearerr(stdin);
                                if (start == end)
                                        /* no chars in the buffer, restart */
                                        goto reread;
@@ -556,7 +561,6 @@
 wgetch(WINDOW *win)
 {
        int     inp, weset;
-       ssize_t nchar;
        char    c;
 
        if (!(win->flags & __SCROLLOK) && (win->flags & __FULLWIN)
@@ -611,13 +615,17 @@
                        break;
                }
 
-               if ((nchar = read(STDIN_FILENO, &c, sizeof(char))) < 0) {
+               c = getchar();
+               if (feof(stdin)) {
+                       clearerr(stdin);
+                       __restore_termios();
+                       return ERR;     /* we have timed out */
+               }
+               
+               if (ferror(stdin)) {
+                       clearerr(stdin);
                        inp = ERR;
                } else {
-                       if (nchar == 0) {
-                               __restore_termios();
-                               return ERR;     /* we have timed out */
-                       }
                        inp = (unsigned int) c;
                }
        }
@@ -640,14 +648,21 @@
 
        __restore_termios();
        if (__echoit) {
-/*
-               mvwaddch(curscr,
-                   (int) (win->cury + win->begy), (int) (win->curx + win->begx), (chtype) inp);
-*/
                waddch(win, (chtype) inp);
        }
        if (weset)
                nocbreak();
 
+       wrefresh(win);
        return ((inp < 0) || (inp == ERR) ? ERR : inp);
 }
+
+/*
+ * ungetch --
+ *     Put the character back into the input queue.
+ */
+int
+ungetch(int c)
+{
+       return ((ungetc(c, stdin) == EOF) ? ERR : OK);
+}



Home | Main Index | Thread Index | Old Index