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