1166124Srafan/****************************************************************************
2174993Srafan * Copyright (c) 2003,2006 Free Software Foundation, Inc.                   *
3166124Srafan *                                                                          *
4166124Srafan * Permission is hereby granted, free of charge, to any person obtaining a  *
5166124Srafan * copy of this software and associated documentation files (the            *
6166124Srafan * "Software"), to deal in the Software without restriction, including      *
7166124Srafan * without limitation the rights to use, copy, modify, merge, publish,      *
8166124Srafan * distribute, distribute with modifications, sublicense, and/or sell       *
9166124Srafan * copies of the Software, and to permit persons to whom the Software is    *
10166124Srafan * furnished to do so, subject to the following conditions:                 *
11166124Srafan *                                                                          *
12166124Srafan * The above copyright notice and this permission notice shall be included  *
13166124Srafan * in all copies or substantial portions of the Software.                   *
14166124Srafan *                                                                          *
15166124Srafan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16166124Srafan * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17166124Srafan * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18166124Srafan * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19166124Srafan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20166124Srafan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21166124Srafan * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22166124Srafan *                                                                          *
23166124Srafan * Except as contained in this notice, the name(s) of the above copyright   *
24166124Srafan * holders shall not be used in advertising or otherwise to promote the     *
25166124Srafan * sale, use or other dealings in this Software without prior written       *
26166124Srafan * authorization.                                                           *
27166124Srafan ****************************************************************************/
28166124Srafan
29166124Srafan/****************************************************************************
30166124Srafan *  Author: Thomas E. Dickey, 2003                                          *
31166124Srafan ****************************************************************************/
32166124Srafan
33166124Srafan#include <curses.priv.h>
34166124Srafan
35174993SrafanMODULE_ID("$Id: key_defined.c,v 1.6 2006/12/30 23:22:55 tom Exp $")
36166124Srafan
37166124Srafanstatic int
38174993Srafanfind_definition(TRIES * tree, const char *str)
39166124Srafan{
40174993Srafan    TRIES *ptr;
41174993Srafan    int result = OK;
42166124Srafan
43166124Srafan    if (str != 0 && *str != '\0') {
44166124Srafan	for (ptr = tree; ptr != 0; ptr = ptr->sibling) {
45166124Srafan	    if (UChar(*str) == UChar(ptr->ch)) {
46166124Srafan		if (str[1] == '\0' && ptr->child != 0) {
47174993Srafan		    result = ERR;
48174993Srafan		} else if ((result = find_definition(ptr->child, str + 1))
49174993Srafan			   == OK) {
50166124Srafan		    result = ptr->value;
51166124Srafan		} else if (str[1] == '\0') {
52174993Srafan		    result = ERR;
53166124Srafan		}
54166124Srafan	    }
55174993Srafan	    if (result != OK)
56166124Srafan		break;
57166124Srafan	}
58166124Srafan    }
59166124Srafan    return (result);
60166124Srafan}
61166124Srafan
62166124Srafan/*
63166124Srafan * Returns the keycode associated with the given string.  If none is found,
64174993Srafan * return OK.  If the string is only a prefix to other strings, return ERR.
65174993Srafan * Otherwise, return the keycode's value (neither OK/ERR).
66166124Srafan */
67166124SrafanNCURSES_EXPORT(int)
68166124Srafankey_defined(const char *str)
69166124Srafan{
70166124Srafan    int code = ERR;
71166124Srafan
72166124Srafan    T((T_CALLED("key_defined(%s)"), _nc_visbuf(str)));
73166124Srafan    if (SP != 0 && str != 0) {
74166124Srafan	code = find_definition(SP->_keytry, str);
75166124Srafan    }
76166124Srafan
77166124Srafan    returnCode(code);
78166124Srafan}
79