xref: /openbsd/lib/libcurses/base/key_defined.c (revision c7ef0cfc)
1*c7ef0cfcSnicm /* $OpenBSD: key_defined.c,v 1.2 2023/10/17 09:52:08 nicm Exp $ */
281d8c4e1Snicm 
381d8c4e1Snicm /****************************************************************************
4*c7ef0cfcSnicm  * Copyright 2020,2023 Thomas E. Dickey                                     *
5*c7ef0cfcSnicm  * Copyright 2003-2006,2009 Free Software Foundation, Inc.                  *
681d8c4e1Snicm  *                                                                          *
781d8c4e1Snicm  * Permission is hereby granted, free of charge, to any person obtaining a  *
881d8c4e1Snicm  * copy of this software and associated documentation files (the            *
981d8c4e1Snicm  * "Software"), to deal in the Software without restriction, including      *
1081d8c4e1Snicm  * without limitation the rights to use, copy, modify, merge, publish,      *
1181d8c4e1Snicm  * distribute, distribute with modifications, sublicense, and/or sell       *
1281d8c4e1Snicm  * copies of the Software, and to permit persons to whom the Software is    *
1381d8c4e1Snicm  * furnished to do so, subject to the following conditions:                 *
1481d8c4e1Snicm  *                                                                          *
1581d8c4e1Snicm  * The above copyright notice and this permission notice shall be included  *
1681d8c4e1Snicm  * in all copies or substantial portions of the Software.                   *
1781d8c4e1Snicm  *                                                                          *
1881d8c4e1Snicm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1981d8c4e1Snicm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
2081d8c4e1Snicm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
2181d8c4e1Snicm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
2281d8c4e1Snicm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2381d8c4e1Snicm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2481d8c4e1Snicm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2581d8c4e1Snicm  *                                                                          *
2681d8c4e1Snicm  * Except as contained in this notice, the name(s) of the above copyright   *
2781d8c4e1Snicm  * holders shall not be used in advertising or otherwise to promote the     *
2881d8c4e1Snicm  * sale, use or other dealings in this Software without prior written       *
2981d8c4e1Snicm  * authorization.                                                           *
3081d8c4e1Snicm  ****************************************************************************/
3181d8c4e1Snicm 
3281d8c4e1Snicm /****************************************************************************
3381d8c4e1Snicm  *  Author: Thomas E. Dickey, 2003                                          *
3481d8c4e1Snicm  ****************************************************************************/
3581d8c4e1Snicm 
3681d8c4e1Snicm #include <curses.priv.h>
37*c7ef0cfcSnicm #include <tic.h>
3881d8c4e1Snicm 
39*c7ef0cfcSnicm MODULE_ID("$Id: key_defined.c,v 1.2 2023/10/17 09:52:08 nicm Exp $")
4081d8c4e1Snicm 
4181d8c4e1Snicm static int
find_definition(TRIES * tree,const char * str)4281d8c4e1Snicm find_definition(TRIES * tree, const char *str)
4381d8c4e1Snicm {
4481d8c4e1Snicm     TRIES *ptr;
4581d8c4e1Snicm     int result = OK;
4681d8c4e1Snicm 
47*c7ef0cfcSnicm     if (VALID_STRING(str) && *str != '\0') {
4881d8c4e1Snicm 	for (ptr = tree; ptr != 0; ptr = ptr->sibling) {
4981d8c4e1Snicm 	    if (UChar(*str) == UChar(ptr->ch)) {
5081d8c4e1Snicm 		if (str[1] == '\0' && ptr->child != 0) {
5181d8c4e1Snicm 		    result = ERR;
5281d8c4e1Snicm 		} else if ((result = find_definition(ptr->child, str + 1))
5381d8c4e1Snicm 			   == OK) {
5481d8c4e1Snicm 		    result = ptr->value;
5581d8c4e1Snicm 		} else if (str[1] == '\0') {
5681d8c4e1Snicm 		    result = ERR;
5781d8c4e1Snicm 		}
5881d8c4e1Snicm 	    }
5981d8c4e1Snicm 	    if (result != OK)
6081d8c4e1Snicm 		break;
6181d8c4e1Snicm 	}
6281d8c4e1Snicm     }
6381d8c4e1Snicm     return (result);
6481d8c4e1Snicm }
6581d8c4e1Snicm 
6681d8c4e1Snicm /*
6781d8c4e1Snicm  * Returns the keycode associated with the given string.  If none is found,
6881d8c4e1Snicm  * return OK.  If the string is only a prefix to other strings, return ERR.
6981d8c4e1Snicm  * Otherwise, return the keycode's value (neither OK/ERR).
7081d8c4e1Snicm  */
7181d8c4e1Snicm NCURSES_EXPORT(int)
NCURSES_SP_NAME(key_defined)72*c7ef0cfcSnicm NCURSES_SP_NAME(key_defined) (NCURSES_SP_DCLx const char *str)
7381d8c4e1Snicm {
7481d8c4e1Snicm     int code = ERR;
7581d8c4e1Snicm 
76*c7ef0cfcSnicm     T((T_CALLED("key_defined(%p, %s)"), (void *) SP_PARM, _nc_visbuf(str)));
77*c7ef0cfcSnicm     if (SP_PARM != 0 && str != 0) {
78*c7ef0cfcSnicm 	code = find_definition(SP_PARM->_keytry, str);
7981d8c4e1Snicm     }
8081d8c4e1Snicm 
8181d8c4e1Snicm     returnCode(code);
8281d8c4e1Snicm }
83*c7ef0cfcSnicm 
84*c7ef0cfcSnicm #if NCURSES_SP_FUNCS
85*c7ef0cfcSnicm NCURSES_EXPORT(int)
key_defined(const char * str)86*c7ef0cfcSnicm key_defined(const char *str)
87*c7ef0cfcSnicm {
88*c7ef0cfcSnicm     return NCURSES_SP_NAME(key_defined) (CURRENT_SCREEN, str);
89*c7ef0cfcSnicm }
90*c7ef0cfcSnicm #endif
91