17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright (c) 1995, by Sun Microsystems, Inc.
247c478bdstevel@tonic-gate * All rights reserved.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/*
307c478bdstevel@tonic-gate * keypad.c
317c478bdstevel@tonic-gate *
327c478bdstevel@tonic-gate * XCurses Library
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
357c478bdstevel@tonic-gate *
367c478bdstevel@tonic-gate */
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#ifdef M_RCSID
397c478bdstevel@tonic-gate#ifndef lint
407c478bdstevel@tonic-gatestatic char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/keypad.c 1.3 1995/05/24 19:43:46 ant Exp $";
417c478bdstevel@tonic-gate#endif
427c478bdstevel@tonic-gate#endif
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate#include <private.h>
457c478bdstevel@tonic-gate#include <stdlib.h>
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate/*f
487c478bdstevel@tonic-gate * Add a function key string to the decode tree.
497c478bdstevel@tonic-gate * Return -1 on error, else the length of the key sequence.
507c478bdstevel@tonic-gate */
517c478bdstevel@tonic-gatestatic int
527c478bdstevel@tonic-gatedecode_add(root, str, code)
537c478bdstevel@tonic-gatet_decode **root;
547c478bdstevel@tonic-gateconst char *str;
557c478bdstevel@tonic-gateshort code;
567c478bdstevel@tonic-gate{
577c478bdstevel@tonic-gate	const char *start;
587c478bdstevel@tonic-gate	t_decode *node, *saved;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	if (root == (t_decode **) 0)
617c478bdstevel@tonic-gate		return -1;
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	if (str == (char *) 0)
647c478bdstevel@tonic-gate		return 0;
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate	start = str;
677c478bdstevel@tonic-gate	saved = (t_decode *) 0;
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate	if (*root == (t_decode *) 0) {
707c478bdstevel@tonic-gate		/* First node of tree. */
717c478bdstevel@tonic-gate		node = (t_decode *) malloc(sizeof *node);
727c478bdstevel@tonic-gate		if (node == (t_decode *) 0)
737c478bdstevel@tonic-gate			return -1;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate		*root = saved = node;
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate		node->child = node->sibling = (t_decode *) 0;
787c478bdstevel@tonic-gate		node->ch = *str++;
797c478bdstevel@tonic-gate		node->key = 0;
807c478bdstevel@tonic-gate	} else {
817c478bdstevel@tonic-gate		/* Find node to insert function key sequence into the tree. */
827c478bdstevel@tonic-gate		for (node = *root; *str != '\0'; ++str, node = node->child) {
837c478bdstevel@tonic-gate			while (node->ch != *str
847c478bdstevel@tonic-gate			&& node->sibling != (t_decode *)0)
857c478bdstevel@tonic-gate				node = node->sibling;
867c478bdstevel@tonic-gate
877c478bdstevel@tonic-gate			if (node->ch != *str) {
887c478bdstevel@tonic-gate				node->sibling = (t_decode *) malloc(
897c478bdstevel@tonic-gate					sizeof *node
907c478bdstevel@tonic-gate				);
917c478bdstevel@tonic-gate				if (node->sibling == (t_decode *) 0)
927c478bdstevel@tonic-gate					return -1;
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate				saved = node = node->sibling;
957c478bdstevel@tonic-gate				node->child = node->sibling = (t_decode *) 0;
967c478bdstevel@tonic-gate				node->ch = *str++;
977c478bdstevel@tonic-gate				node->key = 0;
987c478bdstevel@tonic-gate				break;
997c478bdstevel@tonic-gate			}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate			if (node->child == (t_decode *) 0)
1027c478bdstevel@tonic-gate				break;
1037c478bdstevel@tonic-gate		}
1047c478bdstevel@tonic-gate	}
1057c478bdstevel@tonic-gate
1067c478bdstevel@tonic-gate	/* Insert string into the tree; node->child == null. */
1077c478bdstevel@tonic-gate	while (*str != '\0') {
1087c478bdstevel@tonic-gate		node->child = (t_decode *) malloc(sizeof *node);
1097c478bdstevel@tonic-gate		if (node->child == (t_decode *) 0) {
1107c478bdstevel@tonic-gate			__m_decode_free(&saved);
1117c478bdstevel@tonic-gate			return -1;
1127c478bdstevel@tonic-gate		}
1137c478bdstevel@tonic-gate
1147c478bdstevel@tonic-gate		node = node->child;
1157c478bdstevel@tonic-gate		node->child = node->sibling = (t_decode *) 0;
1167c478bdstevel@tonic-gate		node->ch = *str++;
1177c478bdstevel@tonic-gate		node->key = 0;
1187c478bdstevel@tonic-gate	}
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate	node->key = code;
1217c478bdstevel@tonic-gate
1227c478bdstevel@tonic-gate	return (int) (str - start);
1237c478bdstevel@tonic-gate}
1247c478bdstevel@tonic-gate
1257c478bdstevel@tonic-gatevoid
1267c478bdstevel@tonic-gate__m_decode_free(tree)
1277c478bdstevel@tonic-gatet_decode **tree;
1287c478bdstevel@tonic-gate{
1297c478bdstevel@tonic-gate	if (*tree != (t_decode *) 0) {
1307c478bdstevel@tonic-gate		__m_decode_free(&(*tree)->sibling);
1317c478bdstevel@tonic-gate		__m_decode_free(&(*tree)->child);
1327c478bdstevel@tonic-gate		free(*tree);
1337c478bdstevel@tonic-gate		*tree = (t_decode *) 0;
1347c478bdstevel@tonic-gate	}
1357c478bdstevel@tonic-gate}
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate/*f
1387c478bdstevel@tonic-gate * Initialise the function key decode tree.
1397c478bdstevel@tonic-gate */
1407c478bdstevel@tonic-gateint
1417c478bdstevel@tonic-gate__m_decode_init(tree)
1427c478bdstevel@tonic-gatet_decode **tree;
1437c478bdstevel@tonic-gate{
1447c478bdstevel@tonic-gate	int max, len;
1457c478bdstevel@tonic-gate	short (*p)[2];
1467c478bdstevel@tonic-gate
1477c478bdstevel@tonic-gate	*tree = (t_decode *) 0;
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gate	for (max = -1, p = __m_keyindex; **p != -1; ++p) {
1507c478bdstevel@tonic-gate		len = decode_add(tree, cur_term->_str[**p], (*p)[1]);
1517c478bdstevel@tonic-gate		if (len < 0)
1527c478bdstevel@tonic-gate			return -1;
1537c478bdstevel@tonic-gate		if (max < len)
1547c478bdstevel@tonic-gate			max = len;
1557c478bdstevel@tonic-gate	}
1567c478bdstevel@tonic-gate
1577c478bdstevel@tonic-gate	return max;
1587c478bdstevel@tonic-gate}
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate/*f
1617c478bdstevel@tonic-gate * When true for a given window, then multibyte function key processing
1627c478bdstevel@tonic-gate * is done for all input throough that window, see wgetch().
1637c478bdstevel@tonic-gate */
1647c478bdstevel@tonic-gateint
1657c478bdstevel@tonic-gatekeypad(WINDOW *w, bool bf)
1667c478bdstevel@tonic-gate{
1677c478bdstevel@tonic-gate#ifdef M_CURSES_TRACE
1687c478bdstevel@tonic-gate	__m_trace("keypad(%p, %d)", w, bf);
1697c478bdstevel@tonic-gate#endif
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	w->_flags &= ~W_USE_KEYPAD;
1727c478bdstevel@tonic-gate
1737c478bdstevel@tonic-gate	if (bf)
1747c478bdstevel@tonic-gate		w->_flags |= W_USE_KEYPAD;
1757c478bdstevel@tonic-gate
1767c478bdstevel@tonic-gate	return __m_return_code("keypad", OK);
1777c478bdstevel@tonic-gate}
1787c478bdstevel@tonic-gate
179