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-1998 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/* LINTLIBRARY */
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * keypad.c
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * XCurses Library
357c478bdstevel@tonic-gate *
367c478bdstevel@tonic-gate * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
377c478bdstevel@tonic-gate *
387c478bdstevel@tonic-gate */
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#ifdef M_RCSID
417c478bdstevel@tonic-gate#ifndef lint
427c478bdstevel@tonic-gatestatic char rcsID[] =
437c478bdstevel@tonic-gate"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
447c478bdstevel@tonic-gate"libxcurses/src/libc/xcurses/rcs/keypad.c 1.6 1998/06/03 12:56:59 "
457c478bdstevel@tonic-gate"cbates Exp $";
467c478bdstevel@tonic-gate#endif
477c478bdstevel@tonic-gate#endif
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#include <private.h>
507c478bdstevel@tonic-gate#include <stdlib.h>
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate/*
537c478bdstevel@tonic-gate * Add a function key string to the decode tree.
547c478bdstevel@tonic-gate * Return -1 on error, else the length of the key sequence.
557c478bdstevel@tonic-gate */
567c478bdstevel@tonic-gatestatic int
577c478bdstevel@tonic-gatedecode_add(t_decode **root, const char *str, short code)
587c478bdstevel@tonic-gate{
597c478bdstevel@tonic-gate	const char	*start;
607c478bdstevel@tonic-gate	t_decode	*node, *saved;
617c478bdstevel@tonic-gate
627c478bdstevel@tonic-gate	if (root == NULL)
637c478bdstevel@tonic-gate		return (-1);
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate	if (str == NULL)
667c478bdstevel@tonic-gate		return (0);
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate	start = str;
697c478bdstevel@tonic-gate	saved = NULL;
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate	if (*root == NULL) {
727c478bdstevel@tonic-gate		/* First node of tree. */
737c478bdstevel@tonic-gate		node = (t_decode *) malloc(sizeof (*node));
747c478bdstevel@tonic-gate		if (node == NULL)
757c478bdstevel@tonic-gate			return (-1);
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate		*root = saved = node;
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate		node->child = node->sibling = NULL;
807c478bdstevel@tonic-gate		node->ch = *str++;
817c478bdstevel@tonic-gate		node->key = 0;
827c478bdstevel@tonic-gate	} else {
837c478bdstevel@tonic-gate		/* Find node to insert function key sequence into the tree. */
847c478bdstevel@tonic-gate		for (node = *root; *str != '\0'; ++str, node = node->child) {
857c478bdstevel@tonic-gate			while (node->ch != *str &&
867c478bdstevel@tonic-gate				node->sibling != NULL)
877c478bdstevel@tonic-gate				node = node->sibling;
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate			if (node->ch != *str) {
907c478bdstevel@tonic-gate				node->sibling = (t_decode *)
917c478bdstevel@tonic-gate					malloc(sizeof (*node));
927c478bdstevel@tonic-gate				if (node->sibling == NULL)
937c478bdstevel@tonic-gate					return (-1);
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate				saved = node = node->sibling;
967c478bdstevel@tonic-gate				node->child = node->sibling = NULL;
977c478bdstevel@tonic-gate				node->ch = *str++;
987c478bdstevel@tonic-gate				node->key = 0;
997c478bdstevel@tonic-gate				break;
1007c478bdstevel@tonic-gate			}
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate			if (node->child == NULL)
1037c478bdstevel@tonic-gate				break;
1047c478bdstevel@tonic-gate		}
1057c478bdstevel@tonic-gate	}
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	/* Insert string into the tree; node->child == null. */
1087c478bdstevel@tonic-gate	while (*str != '\0') {
1097c478bdstevel@tonic-gate		node->child = (t_decode *) malloc(sizeof (*node));
1107c478bdstevel@tonic-gate		if (node->child == NULL) {
1117c478bdstevel@tonic-gate			__m_decode_free(&saved);
1127c478bdstevel@tonic-gate			return (-1);
1137c478bdstevel@tonic-gate		}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate		node = node->child;
1167c478bdstevel@tonic-gate		node->child = node->sibling = NULL;
1177c478bdstevel@tonic-gate		node->ch = *str++;
1187c478bdstevel@tonic-gate		node->key = 0;
1197c478bdstevel@tonic-gate	}
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate	node->key = code;
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate	/* (str - start) should be enough small to fit in "int" */
1247c478bdstevel@tonic-gate	return ((int)(str - start));
1257c478bdstevel@tonic-gate}
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gatevoid
1297c478bdstevel@tonic-gate__m_decode_free(t_decode **tree)
1307c478bdstevel@tonic-gate{
1317c478bdstevel@tonic-gate	if (*tree != NULL) {
1327c478bdstevel@tonic-gate		__m_decode_free(&(*tree)->sibling);
1337c478bdstevel@tonic-gate		__m_decode_free(&(*tree)->child);
1347c478bdstevel@tonic-gate		free(*tree);
1357c478bdstevel@tonic-gate		*tree = NULL;
1367c478bdstevel@tonic-gate	}
1377c478bdstevel@tonic-gate}
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gate/*
1407c478bdstevel@tonic-gate * Initialise the function key decode tree.
1417c478bdstevel@tonic-gate */
1427c478bdstevel@tonic-gateint
1437c478bdstevel@tonic-gate__m_decode_init(t_decode **tree)
1447c478bdstevel@tonic-gate{
1457c478bdstevel@tonic-gate	int	max, len;
1467c478bdstevel@tonic-gate	const short	(*p)[2];
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate	*tree = NULL;
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate	for (max = -1, p = __m_keyindex; **p != -1; ++p) {
1517c478bdstevel@tonic-gate		len = decode_add(tree, cur_term->_str[**p], (*p)[1]);
1527c478bdstevel@tonic-gate		if (len < 0)
1537c478bdstevel@tonic-gate			return (-1);
1547c478bdstevel@tonic-gate		if (max < len)
1557c478bdstevel@tonic-gate			max = len;
1567c478bdstevel@tonic-gate	}
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate	return (max);
1597c478bdstevel@tonic-gate}
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate/*
1627c478bdstevel@tonic-gate * When true for a given window, then multibyte function key processing
1637c478bdstevel@tonic-gate * is done for all input throough that window, see wgetch().
1647c478bdstevel@tonic-gate */
1657c478bdstevel@tonic-gateint
1667c478bdstevel@tonic-gatekeypad(WINDOW *w, bool bf)
1677c478bdstevel@tonic-gate{
1687c478bdstevel@tonic-gate	if (bf) {
1697c478bdstevel@tonic-gate		if (keypad_xmit)
1707c478bdstevel@tonic-gate			(void) TPUTS(keypad_xmit, 1, __m_outc);
1717c478bdstevel@tonic-gate		w->_flags |= W_USE_KEYPAD;
1727c478bdstevel@tonic-gate	} else {
1737c478bdstevel@tonic-gate		if (keypad_local)
1747c478bdstevel@tonic-gate			(void) TPUTS(keypad_local, 1, __m_outc);
1757c478bdstevel@tonic-gate		w->_flags &= ~W_USE_KEYPAD;
1767c478bdstevel@tonic-gate	}
1777c478bdstevel@tonic-gate	return (OK);
1787c478bdstevel@tonic-gate}
179