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