1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * keypad.c
29  *
30  * XCurses Library
31  *
32  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/keypad.c 1.3 1995/05/24 19:43:46 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 #include <stdlib.h>
44 
45 /*f
46  * Add a function key string to the decode tree.
47  * Return -1 on error, else the length of the key sequence.
48  */
49 static int
decode_add(root,str,code)50 decode_add(root, str, code)
51 t_decode **root;
52 const char *str;
53 short code;
54 {
55 	const char *start;
56 	t_decode *node, *saved;
57 
58 	if (root == (t_decode **) 0)
59 		return -1;
60 
61 	if (str == (char *) 0)
62 		return 0;
63 
64 	start = str;
65 	saved = (t_decode *) 0;
66 
67 	if (*root == (t_decode *) 0) {
68 		/* First node of tree. */
69 		node = (t_decode *) malloc(sizeof *node);
70 		if (node == (t_decode *) 0)
71 			return -1;
72 
73 		*root = saved = node;
74 
75 		node->child = node->sibling = (t_decode *) 0;
76 		node->ch = *str++;
77 		node->key = 0;
78 	} else {
79 		/* Find node to insert function key sequence into the tree. */
80 		for (node = *root; *str != '\0'; ++str, node = node->child) {
81 			while (node->ch != *str
82 			&& node->sibling != (t_decode *)0)
83 				node = node->sibling;
84 
85 			if (node->ch != *str) {
86 				node->sibling = (t_decode *) malloc(
87 					sizeof *node
88 				);
89 				if (node->sibling == (t_decode *) 0)
90 					return -1;
91 
92 				saved = node = node->sibling;
93 				node->child = node->sibling = (t_decode *) 0;
94 				node->ch = *str++;
95 				node->key = 0;
96 				break;
97 			}
98 
99 			if (node->child == (t_decode *) 0)
100 				break;
101 		}
102 	}
103 
104 	/* Insert string into the tree; node->child == null. */
105 	while (*str != '\0') {
106 		node->child = (t_decode *) malloc(sizeof *node);
107 		if (node->child == (t_decode *) 0) {
108 			__m_decode_free(&saved);
109 			return -1;
110 		}
111 
112 		node = node->child;
113 		node->child = node->sibling = (t_decode *) 0;
114 		node->ch = *str++;
115 		node->key = 0;
116 	}
117 
118 	node->key = code;
119 
120 	return (int) (str - start);
121 }
122 
123 void
__m_decode_free(tree)124 __m_decode_free(tree)
125 t_decode **tree;
126 {
127 	if (*tree != (t_decode *) 0) {
128 		__m_decode_free(&(*tree)->sibling);
129 		__m_decode_free(&(*tree)->child);
130 		free(*tree);
131 		*tree = (t_decode *) 0;
132 	}
133 }
134 
135 /*f
136  * Initialise the function key decode tree.
137  */
138 int
__m_decode_init(tree)139 __m_decode_init(tree)
140 t_decode **tree;
141 {
142 	int max, len;
143 	short (*p)[2];
144 
145 	*tree = (t_decode *) 0;
146 
147 	for (max = -1, p = __m_keyindex; **p != -1; ++p) {
148 		len = decode_add(tree, cur_term->_str[**p], (*p)[1]);
149 		if (len < 0)
150 			return -1;
151 		if (max < len)
152 			max = len;
153 	}
154 
155 	return max;
156 }
157 
158 /*f
159  * When true for a given window, then multibyte function key processing
160  * is done for all input throough that window, see wgetch().
161  */
162 int
keypad(WINDOW * w,bool bf)163 keypad(WINDOW *w, bool bf)
164 {
165 #ifdef M_CURSES_TRACE
166 	__m_trace("keypad(%p, %d)", w, bf);
167 #endif
168 
169 	w->_flags &= ~W_USE_KEYPAD;
170 
171 	if (bf)
172 		w->_flags |= W_USE_KEYPAD;
173 
174 	return __m_return_code("keypad", OK);
175 }
176 
177