150276Speter/****************************************************************************
2166124Srafan * Copyright (c) 1998-2000,2006 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30174993Srafan *  Author: Thomas E. Dickey            1997-on                             *
3150276Speter ****************************************************************************/
3250276Speter
3350276Speter#include <curses.priv.h>
3450276Speter
35174993SrafanMODULE_ID("$Id: keyok.c,v 1.7 2006/12/30 16:22:33 tom Exp $")
3650276Speter
3750276Speter/*
3850276Speter * Enable (or disable) ncurses' interpretation of a keycode by adding (or
3950276Speter * removing) the corresponding 'tries' entry.
4050276Speter *
4150276Speter * Do this by storing a second tree of tries, which records the disabled keys.
4250276Speter * The simplest way to copy is to make a function that returns the string (with
4350276Speter * nulls set to 0200), then use that to reinsert the string into the
4450276Speter * corresponding tree.
4550276Speter */
4650276Speter
4776726SpeterNCURSES_EXPORT(int)
4876726Speterkeyok(int c, bool flag)
4950276Speter{
5076726Speter    int code = ERR;
5176726Speter    int count = 0;
5276726Speter    char *s;
5350276Speter
5476726Speter    T((T_CALLED("keyok(%d,%d)"), c, flag));
55166124Srafan    if (c >= 0) {
56166124Srafan	unsigned ch = (unsigned) c;
57166124Srafan	if (flag) {
58166124Srafan	    while ((s = _nc_expand_try(SP->_key_ok, ch, &count, 0)) != 0
59166124Srafan		   && _nc_remove_key(&(SP->_key_ok), ch)) {
60174993Srafan		code = _nc_add_to_try(&(SP->_keytry), s, ch);
61166124Srafan		free(s);
62166124Srafan		count = 0;
63174993Srafan		if (code != OK)
64174993Srafan		    break;
65166124Srafan	    }
66166124Srafan	} else {
67166124Srafan	    while ((s = _nc_expand_try(SP->_keytry, ch, &count, 0)) != 0
68166124Srafan		   && _nc_remove_key(&(SP->_keytry), ch)) {
69174993Srafan		code = _nc_add_to_try(&(SP->_key_ok), s, ch);
70166124Srafan		free(s);
71166124Srafan		count = 0;
72174993Srafan		if (code != OK)
73174993Srafan		    break;
74166124Srafan	    }
7550276Speter	}
7676726Speter    }
7776726Speter    returnCode(code);
7850276Speter}
79