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