1135446Strhodes/****************************************************************************
2223812Sdougb * Copyright (c) 2003,2006 Free Software Foundation, Inc.                   *
3135446Strhodes *                                                                          *
4135446Strhodes * Permission is hereby granted, free of charge, to any person obtaining a  *
5193149Sdougb * copy of this software and associated documentation files (the            *
6135446Strhodes * "Software"), to deal in the Software without restriction, including      *
7135446Strhodes * without limitation the rights to use, copy, modify, merge, publish,      *
8135446Strhodes * distribute, distribute with modifications, sublicense, and/or sell       *
9135446Strhodes * copies of the Software, and to permit persons to whom the Software is    *
10135446Strhodes * furnished to do so, subject to the following conditions:                 *
11135446Strhodes *                                                                          *
12135446Strhodes * The above copyright notice and this permission notice shall be included  *
13135446Strhodes * in all copies or substantial portions of the Software.                   *
14135446Strhodes *                                                                          *
15135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16135446Strhodes * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17135446Strhodes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18225361Sdougb * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19135446Strhodes * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20135446Strhodes * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21135446Strhodes * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22135446Strhodes *                                                                          *
23135446Strhodes * Except as contained in this notice, the name(s) of the above copyright   *
24135446Strhodes * holders shall not be used in advertising or otherwise to promote the     *
25135446Strhodes * sale, use or other dealings in this Software without prior written       *
26135446Strhodes * authorization.                                                           *
27193149Sdougb ****************************************************************************/
28170222Sdougb
29135446Strhodes/****************************************************************************
30135446Strhodes *  Author: Thomas E. Dickey, 2003                                          *
31135446Strhodes ****************************************************************************/
32135446Strhodes
33135446Strhodes#include <curses.priv.h>
34135446Strhodes
35135446StrhodesMODULE_ID("$Id: key_defined.c,v 1.6 2006/12/30 23:22:55 tom Exp $")
36170222Sdougb
37135446Strhodesstatic int
38135446Strhodesfind_definition(TRIES * tree, const char *str)
39170222Sdougb{
40135446Strhodes    TRIES *ptr;
41135446Strhodes    int result = OK;
42170222Sdougb
43135446Strhodes    if (str != 0 && *str != '\0') {
44135446Strhodes	for (ptr = tree; ptr != 0; ptr = ptr->sibling) {
45170222Sdougb	    if (UChar(*str) == UChar(ptr->ch)) {
46135446Strhodes		if (str[1] == '\0' && ptr->child != 0) {
47135446Strhodes		    result = ERR;
48170222Sdougb		} else if ((result = find_definition(ptr->child, str + 1))
49135446Strhodes			   == OK) {
50135446Strhodes		    result = ptr->value;
51170222Sdougb		} else if (str[1] == '\0') {
52135446Strhodes		    result = ERR;
53135446Strhodes		}
54135446Strhodes	    }
55135446Strhodes	    if (result != OK)
56170222Sdougb		break;
57135446Strhodes	}
58135446Strhodes    }
59135446Strhodes    return (result);
60135446Strhodes}
61135446Strhodes
62170222Sdougb/*
63170222Sdougb * Returns the keycode associated with the given string.  If none is found,
64170222Sdougb * return OK.  If the string is only a prefix to other strings, return ERR.
65170222Sdougb * Otherwise, return the keycode's value (neither OK/ERR).
66170222Sdougb */
67170222SdougbNCURSES_EXPORT(int)
68135446Strhodeskey_defined(const char *str)
69135446Strhodes{
70135446Strhodes    int code = ERR;
71135446Strhodes
72135446Strhodes    T((T_CALLED("key_defined(%s)"), _nc_visbuf(str)));
73135446Strhodes    if (SP != 0 && str != 0) {
74135446Strhodes	code = find_definition(SP->_keytry, str);
75135446Strhodes    }
76135446Strhodes
77135446Strhodes    returnCode(code);
78135446Strhodes}
79135446Strhodes