1/* $OpenBSD: keyok.c,v 1.6 2023/10/17 09:52:08 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2019-2020,2021 Thomas E. Dickey                                *
5 * Copyright 1998-2012,2014 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: Thomas E. Dickey                        1997-on                 *
34 *     and: Juergen Pfeifer                         2009                    *
35 ****************************************************************************/
36
37#include <curses.priv.h>
38
39MODULE_ID("$Id: keyok.c,v 1.6 2023/10/17 09:52:08 nicm Exp $")
40
41/*
42 * Enable (or disable) ncurses' interpretation of a keycode by adding (or
43 * removing) the corresponding 'tries' entry.
44 *
45 * Do this by storing a second tree of tries, which records the disabled keys.
46 * The simplest way to copy is to make a function that returns the string (with
47 * nulls set to 0200), then use that to reinsert the string into the
48 * corresponding tree.
49 */
50
51NCURSES_EXPORT(int)
52NCURSES_SP_NAME(keyok) (NCURSES_SP_DCLx int c, bool flag)
53{
54    int code = ERR;
55
56    if (HasTerminal(SP_PARM)) {
57	T((T_CALLED("keyok(%p, %d,%d)"), (void *) SP_PARM, c, flag));
58#ifdef USE_TERM_DRIVER
59	code = CallDriver_2(sp, td_kyOk, c, flag);
60#else
61	if (c >= 0) {
62	    int count = 0;
63	    char *s;
64	    unsigned ch = (unsigned) c;
65
66	    if (flag) {
67		while ((s = _nc_expand_try(SP_PARM->_key_ok,
68					   ch, &count, (size_t) 0)) != 0) {
69		    if (_nc_remove_key(&(SP_PARM->_key_ok), ch)) {
70			code = _nc_add_to_try(&(SP_PARM->_keytry), s, ch);
71			free(s);
72			count = 0;
73			if (code != OK)
74			    break;
75		    } else {
76			free(s);
77		    }
78		}
79	    } else {
80		while ((s = _nc_expand_try(SP_PARM->_keytry,
81					   ch, &count, (size_t) 0)) != 0) {
82		    if (_nc_remove_key(&(SP_PARM->_keytry), ch)) {
83			code = _nc_add_to_try(&(SP_PARM->_key_ok), s, ch);
84			free(s);
85			count = 0;
86			if (code != OK)
87			    break;
88		    } else {
89			free(s);
90		    }
91		}
92	    }
93	}
94#endif
95    }
96    returnCode(code);
97}
98
99#if NCURSES_SP_FUNCS
100NCURSES_EXPORT(int)
101keyok(int c, bool flag)
102{
103    return NCURSES_SP_NAME(keyok) (CURRENT_SCREEN, c, flag);
104}
105#endif
106