1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1999-2010,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 #include <curses.priv.h>
31 #include <tic.h>		/* struct tinfo_fkeys */
32 
33 MODULE_ID("$Id: init_keytry.c,v 1.19 2020/02/02 23:34:34 tom Exp $")
34 
35 /*
36 **      _nc_init_keytry()
37 **
38 **      Construct the try for the current terminal's keypad keys.
39 **
40 */
41 
42 /*
43  * Internal entrypoints use SCREEN* parameter to obtain capabilities rather
44  * than cur_term.
45  */
46 #undef CUR
47 #define CUR SP_TERMTYPE
48 
49 #if	BROKEN_LINKER
50 #undef	_nc_tinfo_fkeys
51 #endif
52 
53 /* LINT_PREPRO
54 #if 0*/
55 #include <init_keytry.h>
56 /* LINT_PREPRO
57 #endif*/
58 
59 #if	BROKEN_LINKER
60 const struct tinfo_fkeys *
_nc_tinfo_fkeysf(void)61 _nc_tinfo_fkeysf(void)
62 {
63     return _nc_tinfo_fkeys;
64 }
65 #endif
66 
67 NCURSES_EXPORT(void)
_nc_init_keytry(SCREEN * sp)68 _nc_init_keytry(SCREEN *sp)
69 {
70     /* The sp->_keytry value is initialized in newterm(), where the sp
71      * structure is created, because we can not tell where keypad() or
72      * mouse_activate() (which will call keyok()) are first called.
73      */
74 
75     if (sp != 0) {
76 	unsigned n;
77 
78 	for (n = 0; _nc_tinfo_fkeys[n].code; n++) {
79 	    if (_nc_tinfo_fkeys[n].offset < STRCOUNT) {
80 		(void) _nc_add_to_try(&(sp->_keytry),
81 				      CUR Strings[_nc_tinfo_fkeys[n].offset],
82 				      _nc_tinfo_fkeys[n].code);
83 	    }
84 	}
85 #if NCURSES_XNAMES
86 	/*
87 	 * Add any of the extended strings to the tries if their name begins
88 	 * with 'k', i.e., they follow the convention of other terminfo key
89 	 * names.
90 	 */
91 	{
92 	    TERMTYPE *tp = &(sp->_term->type);
93 	    for (n = STRCOUNT; n < NUM_STRINGS(tp); ++n) {
94 		const char *name = ExtStrname(tp, (int) n, strnames);
95 		char *value = tp->Strings[n];
96 		if (name != 0
97 		    && *name == 'k'
98 		    && value != 0
99 		    && NCURSES_SP_NAME(key_defined) (NCURSES_SP_ARGx
100 						     value) == 0) {
101 		    (void) _nc_add_to_try(&(sp->_keytry),
102 					  value,
103 					  n - STRCOUNT + KEY_MAX);
104 		}
105 	    }
106 	}
107 #endif
108 #ifdef TRACE
109 	_nc_trace_tries(sp->_keytry);
110 #endif
111     }
112 }
113