1*c7ef0cfcSnicm /* $OpenBSD: lib_ungetch.c,v 1.4 2023/10/17 09:52:09 nicm Exp $ */
292dd1ec0Smillert
392dd1ec0Smillert /****************************************************************************
4*c7ef0cfcSnicm * Copyright 2020 Thomas E. Dickey *
5*c7ef0cfcSnicm * Copyright 1998-2011,2012 Free Software Foundation, Inc. *
692dd1ec0Smillert * *
792dd1ec0Smillert * Permission is hereby granted, free of charge, to any person obtaining a *
892dd1ec0Smillert * copy of this software and associated documentation files (the *
992dd1ec0Smillert * "Software"), to deal in the Software without restriction, including *
1092dd1ec0Smillert * without limitation the rights to use, copy, modify, merge, publish, *
1192dd1ec0Smillert * distribute, distribute with modifications, sublicense, and/or sell *
1292dd1ec0Smillert * copies of the Software, and to permit persons to whom the Software is *
1392dd1ec0Smillert * furnished to do so, subject to the following conditions: *
1492dd1ec0Smillert * *
1592dd1ec0Smillert * The above copyright notice and this permission notice shall be included *
1692dd1ec0Smillert * in all copies or substantial portions of the Software. *
1792dd1ec0Smillert * *
1892dd1ec0Smillert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
1992dd1ec0Smillert * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
2092dd1ec0Smillert * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
2192dd1ec0Smillert * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
2292dd1ec0Smillert * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
2392dd1ec0Smillert * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
2492dd1ec0Smillert * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
2592dd1ec0Smillert * *
2692dd1ec0Smillert * Except as contained in this notice, the name(s) of the above copyright *
2792dd1ec0Smillert * holders shall not be used in advertising or otherwise to promote the *
2892dd1ec0Smillert * sale, use or other dealings in this Software without prior written *
2992dd1ec0Smillert * authorization. *
3092dd1ec0Smillert ****************************************************************************/
3192dd1ec0Smillert
3292dd1ec0Smillert /****************************************************************************
3392dd1ec0Smillert * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
3492dd1ec0Smillert * and: Eric S. Raymond <esr@snark.thyrsus.com> *
3581d8c4e1Snicm * and: Thomas E. Dickey 1996-on *
36*c7ef0cfcSnicm * and: Juergen Pfeifer 2009 *
3792dd1ec0Smillert ****************************************************************************/
3892dd1ec0Smillert
3992dd1ec0Smillert /*
4092dd1ec0Smillert ** lib_ungetch.c
4192dd1ec0Smillert **
4292dd1ec0Smillert ** The routine ungetch().
4392dd1ec0Smillert **
4492dd1ec0Smillert */
4592dd1ec0Smillert
4692dd1ec0Smillert #include <curses.priv.h>
4792dd1ec0Smillert
48*c7ef0cfcSnicm MODULE_ID("$Id: lib_ungetch.c,v 1.4 2023/10/17 09:52:09 nicm Exp $")
4992dd1ec0Smillert
5092dd1ec0Smillert #include <fifo_defs.h>
5192dd1ec0Smillert
5292dd1ec0Smillert #ifdef TRACE
NCURSES_EXPORT(void)5384af20ceSmillert NCURSES_EXPORT(void)
5481d8c4e1Snicm _nc_fifo_dump(SCREEN *sp)
5592dd1ec0Smillert {
5692dd1ec0Smillert int i;
5792dd1ec0Smillert T(("head = %d, tail = %d, peek = %d", head, tail, peek));
5892dd1ec0Smillert for (i = 0; i < 10; i++)
5981d8c4e1Snicm T(("char %d = %s", i, _nc_tracechar(sp, sp->_fifo[i])));
6092dd1ec0Smillert }
6192dd1ec0Smillert #endif /* TRACE */
6292dd1ec0Smillert
6384af20ceSmillert NCURSES_EXPORT(int)
safe_ungetch(SCREEN * sp,int ch)64*c7ef0cfcSnicm safe_ungetch(SCREEN *sp, int ch)
6592dd1ec0Smillert {
6681d8c4e1Snicm int rc = ERR;
6781d8c4e1Snicm
68*c7ef0cfcSnicm T((T_CALLED("ungetch(%p,%s)"), (void *) sp, _nc_tracechar(sp, ch)));
69*c7ef0cfcSnicm
70*c7ef0cfcSnicm if (sp != 0 && tail >= 0) {
71*c7ef0cfcSnicm if (head < 0) {
7292dd1ec0Smillert head = 0;
7381d8c4e1Snicm t_inc();
7492dd1ec0Smillert peek = tail; /* no raw keys */
75*c7ef0cfcSnicm } else {
7692dd1ec0Smillert h_dec();
77*c7ef0cfcSnicm }
7892dd1ec0Smillert
7981d8c4e1Snicm sp->_fifo[head] = ch;
8081d8c4e1Snicm T(("ungetch %s ok", _nc_tracechar(sp, ch)));
8192dd1ec0Smillert #ifdef TRACE
8281d8c4e1Snicm if (USE_TRACEF(TRACE_IEVENT)) {
8381d8c4e1Snicm _nc_fifo_dump(sp);
8481d8c4e1Snicm _nc_unlock_global(tracef);
8581d8c4e1Snicm }
8692dd1ec0Smillert #endif
8781d8c4e1Snicm rc = OK;
8881d8c4e1Snicm }
89*c7ef0cfcSnicm returnCode(rc);
9081d8c4e1Snicm }
9181d8c4e1Snicm
9281d8c4e1Snicm NCURSES_EXPORT(int)
ungetch(int ch)9381d8c4e1Snicm ungetch(int ch)
9481d8c4e1Snicm {
95*c7ef0cfcSnicm return safe_ungetch(CURRENT_SCREEN, ch);
9692dd1ec0Smillert }
97