1 /**************************************************************************** 2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 * and: Thomas E. Dickey 1996-on * 33 * and: Juergen Pfeifer 2009 * 34 ****************************************************************************/ 35 36 /* 37 ** lib_ungetch.c 38 ** 39 ** The routine ungetch(). 40 ** 41 */ 42 43 #include <curses.priv.h> 44 45 MODULE_ID("$Id: lib_ungetch.c,v 1.16 2012/08/04 17:38:53 tom Exp $") 46 47 #include <fifo_defs.h> 48 49 #ifdef TRACE 50 NCURSES_EXPORT(void) 51 _nc_fifo_dump(SCREEN *sp) 52 { 53 int i; 54 T(("head = %d, tail = %d, peek = %d", head, tail, peek)); 55 for (i = 0; i < 10; i++) 56 T(("char %d = %s", i, _nc_tracechar(sp, sp->_fifo[i]))); 57 } 58 #endif /* TRACE */ 59 60 NCURSES_EXPORT(int) 61 safe_ungetch(SCREEN *sp, int ch) 62 { 63 int rc = ERR; 64 65 T((T_CALLED("ungetch(%p,%s)"), (void *) sp, _nc_tracechar(sp, ch))); 66 67 if (sp != 0 && tail >= 0) { 68 if (head < 0) { 69 head = 0; 70 t_inc(); 71 peek = tail; /* no raw keys */ 72 } else { 73 h_dec(); 74 } 75 76 sp->_fifo[head] = ch; 77 T(("ungetch %s ok", _nc_tracechar(sp, ch))); 78 #ifdef TRACE 79 if (USE_TRACEF(TRACE_IEVENT)) { 80 _nc_fifo_dump(sp); 81 _nc_unlock_global(tracef); 82 } 83 #endif 84 rc = OK; 85 } 86 returnCode(rc); 87 } 88 89 NCURSES_EXPORT(int) 90 ungetch(int ch) 91 { 92 return safe_ungetch(CURRENT_SCREEN, ch); 93 } 94