xref: /titanic_51/usr/src/lib/libcurses/screen/newkey.c (revision 148434217c040ea38dc844384f6ba68d9b325906)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<stdlib.h>
45 #include	<string.h>
46 #include	<sys/types.h>
47 #include	"curses_inc.h"
48 
49 /*
50  * Set a new key or a new macro.
51  *
52  * rcvchars: the pattern identifying the key
53  * keyval: the value to return when the key is recognized
54  * macro: if this is not a function key but a macro,
55  * 	tgetch() will block on macros.
56  */
57 
58 int
59 newkey(char *rcvchars, short keyval, bool macro)
60 {
61 	_KEY_MAP	**keys, *key_info,
62 					**prev_keys = cur_term->_keys;
63 	short		*numkeys = &cur_term->_ksz;
64 	char		*str;
65 	size_t		len;
66 
67 	if ((!rcvchars) || (*rcvchars == '\0') || (keyval < 0) ||
68 	    (((keys = (_KEY_MAP **) malloc(sizeof (_KEY_MAP *) *
69 	    (*numkeys + 1))) == NULL))) {
70 		goto bad;
71 	}
72 
73 	len = strlen(rcvchars) + 1;
74 
75 	if ((key_info = (_KEY_MAP *) malloc(sizeof (_KEY_MAP) + len)) ==
76 	    NULL) {
77 		free(keys);
78 bad :
79 		term_errno = TERM_BAD_MALLOC;
80 #ifdef	DEBUG
81 		strcpy(term_parm_err, "newkey");
82 #endif	/* DEBUG */
83 		return (ERR);
84 	}
85 
86 	if (macro) {
87 		(void) memcpy((char *) keys, (char *) prev_keys,
88 		    (*numkeys * sizeof (_KEY_MAP *)));
89 		keys[*numkeys] = key_info;
90 	} else {
91 		short	*first = &(cur_term->_first_macro);
92 
93 		(void) memcpy((char *) keys, (char *) prev_keys,
94 		    (*first * sizeof (_KEY_MAP *)));
95 		(void) memcpy((char *) &(keys[*first + 1]),
96 		    (char *) &(prev_keys[*first]),
97 		    ((*numkeys - *first) * sizeof (_KEY_MAP *)));
98 		keys[(*first)++] = key_info;
99 		cur_term->_lastmacro_ordered++;
100 	}
101 	if (prev_keys != NULL)
102 		free(prev_keys);
103 	cur_term->_keys = keys;
104 
105 	(*numkeys)++;
106 	key_info->_sends = str = (char *) key_info + sizeof (_KEY_MAP);
107 	(void) memcpy(str, rcvchars, len);
108 	key_info->_keyval = keyval;
109 	cur_term->funckeystarter[*str] |= (macro ? _MACRO : _KEY);
110 
111 	return (OK);
112 }
113