xref: /titanic_50/usr/src/lib/libxcurses2/src/libc/xcurses/keypad.c (revision 8b464eb836173b92f2b7a65623cd06c8c3c59289)
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-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * keypad.c
33  *
34  * XCurses Library
35  *
36  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37  *
38  */
39 
40 #ifdef M_RCSID
41 #ifndef lint
42 static char rcsID[] =
43 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
44 "libxcurses/src/libc/xcurses/rcs/keypad.c 1.6 1998/06/03 12:56:59 "
45 "cbates Exp $";
46 #endif
47 #endif
48 
49 #include <private.h>
50 #include <stdlib.h>
51 
52 /*
53  * Add a function key string to the decode tree.
54  * Return -1 on error, else the length of the key sequence.
55  */
56 static int
57 decode_add(t_decode **root, const char *str, short code)
58 {
59 	const char	*start;
60 	t_decode	*node, *saved;
61 
62 	if (root == NULL)
63 		return (-1);
64 
65 	if (str == NULL)
66 		return (0);
67 
68 	start = str;
69 	saved = NULL;
70 
71 	if (*root == NULL) {
72 		/* First node of tree. */
73 		node = (t_decode *) malloc(sizeof (*node));
74 		if (node == NULL)
75 			return (-1);
76 
77 		*root = saved = node;
78 
79 		node->child = node->sibling = NULL;
80 		node->ch = *str++;
81 		node->key = 0;
82 	} else {
83 		/* Find node to insert function key sequence into the tree. */
84 		for (node = *root; *str != '\0'; ++str, node = node->child) {
85 			while (node->ch != *str &&
86 				node->sibling != NULL)
87 				node = node->sibling;
88 
89 			if (node->ch != *str) {
90 				node->sibling = (t_decode *)
91 					malloc(sizeof (*node));
92 				if (node->sibling == NULL)
93 					return (-1);
94 
95 				saved = node = node->sibling;
96 				node->child = node->sibling = NULL;
97 				node->ch = *str++;
98 				node->key = 0;
99 				break;
100 			}
101 
102 			if (node->child == NULL)
103 				break;
104 		}
105 	}
106 
107 	/* Insert string into the tree; node->child == null. */
108 	while (*str != '\0') {
109 		node->child = (t_decode *) malloc(sizeof (*node));
110 		if (node->child == NULL) {
111 			__m_decode_free(&saved);
112 			return (-1);
113 		}
114 
115 		node = node->child;
116 		node->child = node->sibling = NULL;
117 		node->ch = *str++;
118 		node->key = 0;
119 	}
120 
121 	node->key = code;
122 
123 	/* (str - start) should be enough small to fit in "int" */
124 	return ((int)(str - start));
125 }
126 
127 
128 void
129 __m_decode_free(t_decode **tree)
130 {
131 	if (*tree != NULL) {
132 		__m_decode_free(&(*tree)->sibling);
133 		__m_decode_free(&(*tree)->child);
134 		free(*tree);
135 		*tree = NULL;
136 	}
137 }
138 
139 /*
140  * Initialise the function key decode tree.
141  */
142 int
143 __m_decode_init(t_decode **tree)
144 {
145 	int	max, len;
146 	const short	(*p)[2];
147 
148 	*tree = NULL;
149 
150 	for (max = -1, p = __m_keyindex; **p != -1; ++p) {
151 		len = decode_add(tree, cur_term->_str[**p], (*p)[1]);
152 		if (len < 0)
153 			return (-1);
154 		if (max < len)
155 			max = len;
156 	}
157 
158 	return (max);
159 }
160 
161 /*
162  * When true for a given window, then multibyte function key processing
163  * is done for all input throough that window, see wgetch().
164  */
165 int
166 keypad(WINDOW *w, bool bf)
167 {
168 	if (bf) {
169 		if (keypad_xmit)
170 			(void) TPUTS(keypad_xmit, 1, __m_outc);
171 		w->_flags |= W_USE_KEYPAD;
172 	} else {
173 		if (keypad_local)
174 			(void) TPUTS(keypad_local, 1, __m_outc);
175 		w->_flags &= ~W_USE_KEYPAD;
176 	}
177 	return (OK);
178 }
179