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 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 #include	<stdlib.h>
43 #include	<string.h>
44 #include	<sys/types.h>
45 #include	"curses_inc.h"
46 
47 /*
48  * Set a new key or a new macro.
49  *
50  * rcvchars: the pattern identifying the key
51  * keyval: the value to return when the key is recognized
52  * macro: if this is not a function key but a macro,
53  * 	tgetch() will block on macros.
54  */
55 
56 int
57 newkey(char *rcvchars, short keyval, bool macro)
58 {
59 	_KEY_MAP	**keys, *key_info,
60 					**prev_keys = cur_term->_keys;
61 	short		*numkeys = &cur_term->_ksz;
62 	char		*str;
63 	size_t		len;
64 
65 	if ((!rcvchars) || (*rcvchars == '\0') || (keyval < 0) ||
66 	    (((keys = (_KEY_MAP **) malloc(sizeof (_KEY_MAP *) *
67 	    (*numkeys + 1))) == NULL))) {
68 		goto bad;
69 	}
70 
71 	len = strlen(rcvchars) + 1;
72 
73 	if ((key_info = (_KEY_MAP *) malloc(sizeof (_KEY_MAP) + len)) ==
74 	    NULL) {
75 		free(keys);
76 bad :
77 		term_errno = TERM_BAD_MALLOC;
78 #ifdef	DEBUG
79 		strcpy(term_parm_err, "newkey");
80 #endif	/* DEBUG */
81 		return (ERR);
82 	}
83 
84 	if (macro) {
85 		(void) memcpy((char *) keys, (char *) prev_keys,
86 		    (*numkeys * sizeof (_KEY_MAP *)));
87 		keys[*numkeys] = key_info;
88 	} else {
89 		short	*first = &(cur_term->_first_macro);
90 
91 		(void) memcpy((char *) keys, (char *) prev_keys,
92 		    (*first * sizeof (_KEY_MAP *)));
93 		(void) memcpy((char *) &(keys[*first + 1]),
94 		    (char *) &(prev_keys[*first]),
95 		    ((*numkeys - *first) * sizeof (_KEY_MAP *)));
96 		keys[(*first)++] = key_info;
97 		cur_term->_lastmacro_ordered++;
98 	}
99 	if (prev_keys != NULL)
100 		free(prev_keys);
101 	cur_term->_keys = keys;
102 
103 	(*numkeys)++;
104 	key_info->_sends = str = (char *) key_info + sizeof (_KEY_MAP);
105 	(void) memcpy(str, rcvchars, len);
106 	key_info->_keyval = keyval;
107 	cur_term->funckeystarter[*str] |= (macro ? _MACRO : _KEY);
108 
109 	return (OK);
110 }
111