1/* $OpenBSD: init_keytry.c,v 1.7 2023/10/17 09:52:09 nicm Exp $ */
2
3/****************************************************************************
4 * Copyright 2020 Thomas E. Dickey                                          *
5 * Copyright 1999-2010,2016 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#include <curses.priv.h>
33#include <tic.h>		/* struct tinfo_fkeys */
34
35MODULE_ID("$Id: init_keytry.c,v 1.7 2023/10/17 09:52:09 nicm Exp $")
36
37/*
38**      _nc_init_keytry()
39**
40**      Construct the try for the current terminal's keypad keys.
41**
42*/
43
44/*
45 * Internal entrypoints use SCREEN* parameter to obtain capabilities rather
46 * than cur_term.
47 */
48#undef CUR
49#define CUR SP_TERMTYPE
50
51#if	BROKEN_LINKER
52#undef	_nc_tinfo_fkeys
53#endif
54
55/* LINT_PREPRO
56#if 0*/
57#include <init_keytry.h>
58/* LINT_PREPRO
59#endif*/
60
61#if	BROKEN_LINKER
62const struct tinfo_fkeys *
63_nc_tinfo_fkeysf(void)
64{
65    return _nc_tinfo_fkeys;
66}
67#endif
68
69NCURSES_EXPORT(void)
70_nc_init_keytry(SCREEN *sp)
71{
72    /* The sp->_keytry value is initialized in newterm(), where the sp
73     * structure is created, because we can not tell where keypad() or
74     * mouse_activate() (which will call keyok()) are first called.
75     */
76
77    if (sp != 0) {
78	unsigned n;
79
80	for (n = 0; _nc_tinfo_fkeys[n].code; n++) {
81	    if (_nc_tinfo_fkeys[n].offset < STRCOUNT) {
82		(void) _nc_add_to_try(&(sp->_keytry),
83				      CUR Strings[_nc_tinfo_fkeys[n].offset],
84				      _nc_tinfo_fkeys[n].code);
85	    }
86	}
87#if NCURSES_XNAMES
88	/*
89	 * Add any of the extended strings to the tries if their name begins
90	 * with 'k', i.e., they follow the convention of other terminfo key
91	 * names.
92	 */
93	{
94	    TERMTYPE *tp = &(sp->_term->type);
95	    for (n = STRCOUNT; n < NUM_STRINGS(tp); ++n) {
96		const char *name = ExtStrname(tp, (int) n, strnames);
97		char *value = tp->Strings[n];
98		if (name != 0
99		    && *name == 'k'
100		    && value != 0
101		    && NCURSES_SP_NAME(key_defined) (NCURSES_SP_ARGx
102						     value) == 0) {
103		    (void) _nc_add_to_try(&(sp->_keytry),
104					  value,
105					  n - STRCOUNT + KEY_MAX);
106		}
107	    }
108	}
109#endif
110#ifdef TRACE
111	_nc_trace_tries(sp->_keytry);
112#endif
113    }
114}
115