]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_ungetch.c
ncurses 6.2 - patch 20200718
[ncurses.git] / ncurses / base / lib_ungetch.c
index 2eda99017bed066b226bd0c51998fee640aacf6f..fcbe6f4a90c1fe3c32f79af2504bccc11ab249c3 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
+ * Copyright 2020 Thomas E. Dickey                                          *
+ * Copyright 1998-2011,2012 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -30,6 +31,7 @@
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Juergen Pfeifer                         2009                    *
  ****************************************************************************/
 
 /*
@@ -41,7 +43,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_ungetch.c,v 1.10 2008/05/03 20:20:58 tom Exp $")
+MODULE_ID("$Id: lib_ungetch.c,v 1.17 2020/02/02 23:34:34 tom Exp $")
 
 #include <fifo_defs.h>
 
@@ -52,25 +54,28 @@ _nc_fifo_dump(SCREEN *sp)
     int i;
     T(("head = %d, tail = %d, peek = %d", head, tail, peek));
     for (i = 0; i < 10; i++)
-       T(("char %d = %s", i, _tracechar(sp->_fifo[i])));
+       T(("char %d = %s", i, _nc_tracechar(sp, sp->_fifo[i])));
 }
 #endif /* TRACE */
 
 NCURSES_EXPORT(int)
-_nc_ungetch(SCREEN *sp, int ch)
+safe_ungetch(SCREEN *sp, int ch)
 {
     int rc = ERR;
 
-    if (tail != -1) {
-       if (head == -1) {
+    T((T_CALLED("ungetch(%p,%s)"), (void *) sp, _nc_tracechar(sp, ch)));
+
+    if (sp != 0 && tail >= 0) {
+       if (head < 0) {
            head = 0;
            t_inc();
            peek = tail;        /* no raw keys */
-       } else
+       } else {
            h_dec();
+       }
 
        sp->_fifo[head] = ch;
-       T(("ungetch %s ok", _tracechar(ch)));
+       T(("ungetch %s ok", _nc_tracechar(sp, ch)));
 #ifdef TRACE
        if (USE_TRACEF(TRACE_IEVENT)) {
            _nc_fifo_dump(sp);
@@ -79,12 +84,11 @@ _nc_ungetch(SCREEN *sp, int ch)
 #endif
        rc = OK;
     }
-    return rc;
+    returnCode(rc);
 }
 
 NCURSES_EXPORT(int)
 ungetch(int ch)
 {
-    T((T_CALLED("ungetch(%s)"), _tracechar(ch)));
-    returnCode(_nc_ungetch(SP, ch));
+    return safe_ungetch(CURRENT_SCREEN, ch);
 }