xref: /titanic_52/usr/src/lib/libcurses/screen/delkey.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 /* Delete keys matching pat or key from the key map. */
50 
51 int
52 delkey(char *sends, int keyval)
53 {
54 	_KEY_MAP	*kp, **kpp = cur_term->_keys, **fpp, **dpp;
55 	int		mask = 0, cmp, numkeys = cur_term->_ksz;
56 	int		counter = 0, i, num_deleted_keys = 0;
57 	short		*lkorder = &(cur_term->_lastkey_ordered),
58 			*first_macro = &(cur_term->_first_macro),
59 			*lmorder = &(cur_term->_lastmacro_ordered);
60 
61 	/* for ease of determination of key to delete */
62 	if (sends)
63 		mask |= 01;
64 	if (keyval >= 0)
65 		mask |= 02;
66 
67 	/* check each key */
68 	while (++counter < numkeys) {
69 		kp = *kpp;
70 		cmp = 0;
71 		if (sends && (strcmp(sends, kp->_sends) == 0))
72 			cmp |= 01;
73 		if (kp->_keyval == keyval)
74 			cmp |= 02;
75 
76 		/* found one to delete */
77 		if (cmp == mask) {
78 			num_deleted_keys++;
79 			/*
80 			 * If it was an externally created key, then the address
81 			 * of the sequence will be right after the structure.
82 			 * See the malloc in newkey.
83 			 */
84 			if (kp->_sends == ((char *)kp + sizeof (_KEY_MAP)))
85 				free(kp);
86 
87 			/* shift left other keys */
88 			i = (numkeys - counter) - 1;
89 			for (fpp = kpp, dpp = kpp + 1; i > 0; i--, fpp++, dpp++)
90 				*fpp = *dpp;
91 			if (counter <= *lmorder) {
92 				if (counter < *first_macro) {
93 					if (counter <= *lkorder)
94 						(*lkorder)--;
95 					(*first_macro)--;
96 				}
97 				(*lmorder)--;
98 			}
99 		} else
100 			kpp++;
101 	}
102 
103 /* Check if we've crossed boundary and/or hit 0 */
104 
105 	if ((cur_term->_ksz -= num_deleted_keys) == 0)
106 		(void) delkeymap(cur_term);
107 	else
108 		cur_term->_keys = (_KEY_MAP **) realloc((char *)
109 		    cur_term->_keys, (unsigned)cur_term->_ksz);
110 
111 	return (num_deleted_keys);
112 }
113