xref: /titanic_41/usr/src/lib/libcurses/screen/setkeymap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
46*7c478bd9Sstevel@tonic-gate #include	<string.h>
47*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate static	short	keycodes[] = {
50*7c478bd9Sstevel@tonic-gate 		    KEY_BACKSPACE,
51*7c478bd9Sstevel@tonic-gate 		    KEY_CATAB,
52*7c478bd9Sstevel@tonic-gate 		    KEY_CLEAR,
53*7c478bd9Sstevel@tonic-gate 		    KEY_CTAB,
54*7c478bd9Sstevel@tonic-gate 		    KEY_DC,
55*7c478bd9Sstevel@tonic-gate 		    KEY_DL,
56*7c478bd9Sstevel@tonic-gate 		    KEY_DOWN,
57*7c478bd9Sstevel@tonic-gate 		    KEY_EIC,
58*7c478bd9Sstevel@tonic-gate 		    KEY_EOL,
59*7c478bd9Sstevel@tonic-gate 		    KEY_EOS,
60*7c478bd9Sstevel@tonic-gate 		    KEY_F(0),
61*7c478bd9Sstevel@tonic-gate 		    KEY_F(1),
62*7c478bd9Sstevel@tonic-gate 		    KEY_F(10),
63*7c478bd9Sstevel@tonic-gate 		    KEY_F(2),
64*7c478bd9Sstevel@tonic-gate 		    KEY_F(3),
65*7c478bd9Sstevel@tonic-gate 		    KEY_F(4),
66*7c478bd9Sstevel@tonic-gate 		    KEY_F(5),
67*7c478bd9Sstevel@tonic-gate 		    KEY_F(6),
68*7c478bd9Sstevel@tonic-gate 		    KEY_F(7),
69*7c478bd9Sstevel@tonic-gate 		    KEY_F(8),
70*7c478bd9Sstevel@tonic-gate 		    KEY_F(9),
71*7c478bd9Sstevel@tonic-gate 		    KEY_HOME,
72*7c478bd9Sstevel@tonic-gate 		    KEY_IC,
73*7c478bd9Sstevel@tonic-gate 		    KEY_IL,
74*7c478bd9Sstevel@tonic-gate 		    KEY_LEFT,
75*7c478bd9Sstevel@tonic-gate 		    KEY_LL,
76*7c478bd9Sstevel@tonic-gate 		    KEY_NPAGE,
77*7c478bd9Sstevel@tonic-gate 		    KEY_PPAGE,
78*7c478bd9Sstevel@tonic-gate 		    KEY_RIGHT,
79*7c478bd9Sstevel@tonic-gate 		    KEY_SF,
80*7c478bd9Sstevel@tonic-gate 		    KEY_SR,
81*7c478bd9Sstevel@tonic-gate 		    KEY_STAB,
82*7c478bd9Sstevel@tonic-gate 		    KEY_UP,
83*7c478bd9Sstevel@tonic-gate 		    KEY_A1,
84*7c478bd9Sstevel@tonic-gate 		    KEY_A3,
85*7c478bd9Sstevel@tonic-gate 		    KEY_B2,
86*7c478bd9Sstevel@tonic-gate 		    KEY_C1,
87*7c478bd9Sstevel@tonic-gate 		    KEY_C3,
88*7c478bd9Sstevel@tonic-gate 		    KEY_BTAB,
89*7c478bd9Sstevel@tonic-gate 		    KEY_BEG,
90*7c478bd9Sstevel@tonic-gate 		    KEY_CANCEL,
91*7c478bd9Sstevel@tonic-gate 		    KEY_CLOSE,
92*7c478bd9Sstevel@tonic-gate 		    KEY_COMMAND,
93*7c478bd9Sstevel@tonic-gate 		    KEY_COPY,
94*7c478bd9Sstevel@tonic-gate 		    KEY_CREATE,
95*7c478bd9Sstevel@tonic-gate 		    KEY_END,
96*7c478bd9Sstevel@tonic-gate 		    KEY_ENTER,
97*7c478bd9Sstevel@tonic-gate 		    KEY_EXIT,
98*7c478bd9Sstevel@tonic-gate 		    KEY_FIND,
99*7c478bd9Sstevel@tonic-gate 		    KEY_HELP,
100*7c478bd9Sstevel@tonic-gate 		    KEY_MARK,
101*7c478bd9Sstevel@tonic-gate 		    KEY_MESSAGE,
102*7c478bd9Sstevel@tonic-gate 		    KEY_MOVE,
103*7c478bd9Sstevel@tonic-gate 		    KEY_NEXT,
104*7c478bd9Sstevel@tonic-gate 		    KEY_OPEN,
105*7c478bd9Sstevel@tonic-gate 		    KEY_OPTIONS,
106*7c478bd9Sstevel@tonic-gate 		    KEY_PREVIOUS,
107*7c478bd9Sstevel@tonic-gate 		    KEY_PRINT,
108*7c478bd9Sstevel@tonic-gate 		    KEY_REDO,
109*7c478bd9Sstevel@tonic-gate 		    KEY_REFERENCE,
110*7c478bd9Sstevel@tonic-gate 		    KEY_REFRESH,
111*7c478bd9Sstevel@tonic-gate 		    KEY_REPLACE,
112*7c478bd9Sstevel@tonic-gate 		    KEY_RESTART,
113*7c478bd9Sstevel@tonic-gate 		    KEY_RESUME,
114*7c478bd9Sstevel@tonic-gate 		    KEY_SAVE,
115*7c478bd9Sstevel@tonic-gate 		    KEY_SUSPEND,
116*7c478bd9Sstevel@tonic-gate 		    KEY_UNDO,
117*7c478bd9Sstevel@tonic-gate 		    KEY_SBEG,
118*7c478bd9Sstevel@tonic-gate 		    KEY_SCANCEL,
119*7c478bd9Sstevel@tonic-gate 		    KEY_SCOMMAND,
120*7c478bd9Sstevel@tonic-gate 		    KEY_SCOPY,
121*7c478bd9Sstevel@tonic-gate 		    KEY_SCREATE,
122*7c478bd9Sstevel@tonic-gate 		    KEY_SDC,
123*7c478bd9Sstevel@tonic-gate 		    KEY_SDL,
124*7c478bd9Sstevel@tonic-gate 		    KEY_SELECT,
125*7c478bd9Sstevel@tonic-gate 		    KEY_SEND,
126*7c478bd9Sstevel@tonic-gate 		    KEY_SEOL,
127*7c478bd9Sstevel@tonic-gate 		    KEY_SEXIT,
128*7c478bd9Sstevel@tonic-gate 		    KEY_SFIND,
129*7c478bd9Sstevel@tonic-gate 		    KEY_SHELP,
130*7c478bd9Sstevel@tonic-gate 		    KEY_SHOME,
131*7c478bd9Sstevel@tonic-gate 		    KEY_SIC,
132*7c478bd9Sstevel@tonic-gate 		    KEY_SLEFT,
133*7c478bd9Sstevel@tonic-gate 		    KEY_SMESSAGE,
134*7c478bd9Sstevel@tonic-gate 		    KEY_SMOVE,
135*7c478bd9Sstevel@tonic-gate 		    KEY_SNEXT,
136*7c478bd9Sstevel@tonic-gate 		    KEY_SOPTIONS,
137*7c478bd9Sstevel@tonic-gate 		    KEY_SPREVIOUS,
138*7c478bd9Sstevel@tonic-gate 		    KEY_SPRINT,
139*7c478bd9Sstevel@tonic-gate 		    KEY_SREDO,
140*7c478bd9Sstevel@tonic-gate 		    KEY_SREPLACE,
141*7c478bd9Sstevel@tonic-gate 		    KEY_SRIGHT,
142*7c478bd9Sstevel@tonic-gate 		    KEY_SRSUME,
143*7c478bd9Sstevel@tonic-gate 		    KEY_SSAVE,
144*7c478bd9Sstevel@tonic-gate 		    KEY_SSUSPEND,
145*7c478bd9Sstevel@tonic-gate 		    KEY_SUNDO,
146*7c478bd9Sstevel@tonic-gate 		    KEY_MOUSE
147*7c478bd9Sstevel@tonic-gate 		};
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate static	_KEY_MAP	*p;
150*7c478bd9Sstevel@tonic-gate static	bool		*funckey;
151*7c478bd9Sstevel@tonic-gate static	short		*codeptr;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate static	void
_laddone(char * txt)154*7c478bd9Sstevel@tonic-gate _laddone(char *txt)
155*7c478bd9Sstevel@tonic-gate {
156*7c478bd9Sstevel@tonic-gate 	p->_sends = (txt);
157*7c478bd9Sstevel@tonic-gate 	p->_keyval = *codeptr;
158*7c478bd9Sstevel@tonic-gate 	funckey[(unsigned char)(txt)[0]] |= _KEY;
159*7c478bd9Sstevel@tonic-gate 	p++;
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate static	void
_keyfunc(char ** keyptr,char ** lastkey)165*7c478bd9Sstevel@tonic-gate _keyfunc(char **keyptr, char **lastkey)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	for (; keyptr <= lastkey; keyptr++, codeptr++)
168*7c478bd9Sstevel@tonic-gate 		if (*keyptr) {
169*7c478bd9Sstevel@tonic-gate 			p->_sends = (*keyptr);
170*7c478bd9Sstevel@tonic-gate 			p->_keyval = *codeptr;
171*7c478bd9Sstevel@tonic-gate 			funckey[(unsigned char)(*keyptr)[0]] |= _KEY;
172*7c478bd9Sstevel@tonic-gate 			p++;
173*7c478bd9Sstevel@tonic-gate 		}
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate static	void
_keyfunc2(char ** keyptr,char ** lastkey)179*7c478bd9Sstevel@tonic-gate _keyfunc2(char **keyptr, char **lastkey)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	short code_value = KEY_F(11);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	for (; *keyptr && keyptr <= lastkey; keyptr++, code_value++) {
184*7c478bd9Sstevel@tonic-gate 		p->_sends = *keyptr;
185*7c478bd9Sstevel@tonic-gate 		p->_keyval = (short) code_value;
186*7c478bd9Sstevel@tonic-gate 		funckey[(unsigned char)*keyptr[0]] |= _KEY;
187*7c478bd9Sstevel@tonic-gate 		p++;
188*7c478bd9Sstevel@tonic-gate 	}
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate int
setkeymap(void)192*7c478bd9Sstevel@tonic-gate setkeymap(void)
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	_KEY_MAP	keymap[((sizeof (keycodes) / sizeof (short)) +
195*7c478bd9Sstevel@tonic-gate 			    ((KEY_F(63) - KEY_F(11)) + 1))], **key_ptrs;
196*7c478bd9Sstevel@tonic-gate 	short		numkeys;
197*7c478bd9Sstevel@tonic-gate 	int		numbytes, key_size = cur_term->_ksz;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (cur_term->internal_keys != NULL)
200*7c478bd9Sstevel@tonic-gate 		return (ERR);
201*7c478bd9Sstevel@tonic-gate 	p = keymap;
202*7c478bd9Sstevel@tonic-gate 	codeptr = keycodes;
203*7c478bd9Sstevel@tonic-gate 	funckey = cur_term->funckeystarter;
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	/* If backspace key sends \b, don't map it. */
206*7c478bd9Sstevel@tonic-gate 	if (key_backspace && strcmp(key_backspace, "\b"))
207*7c478bd9Sstevel@tonic-gate 		_laddone(key_backspace);
208*7c478bd9Sstevel@tonic-gate 	codeptr++;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_catab, &key_dl);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	/* If down arrow key sends \n, don't map it. */
213*7c478bd9Sstevel@tonic-gate 	if (key_down && strcmp(key_down, "\n"))
214*7c478bd9Sstevel@tonic-gate 		_laddone(key_down);
215*7c478bd9Sstevel@tonic-gate 	codeptr++;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_eic, &key_il);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	/* If left arrow key sends \b, don't map it. */
220*7c478bd9Sstevel@tonic-gate 	if (key_left && strcmp(key_left, "\b"))
221*7c478bd9Sstevel@tonic-gate 		_laddone(key_left);
222*7c478bd9Sstevel@tonic-gate 	codeptr++;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_ll, &key_up);
225*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_a1, &key_c3);
226*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_btab, &key_btab);
227*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_beg, &key_sundo);
228*7c478bd9Sstevel@tonic-gate 	_keyfunc2(&key_f11, &key_f63);
229*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_mouse, &key_mouse);
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	/*
232*7c478bd9Sstevel@tonic-gate 	 * malloc returns the address of a list of pointers to
233*7c478bd9Sstevel@tonic-gate 	 * (_KEY_MAP *) structures
234*7c478bd9Sstevel@tonic-gate 	 */
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	if ((key_ptrs = (_KEY_MAP **)
237*7c478bd9Sstevel@tonic-gate 	    /* LINTED */
238*7c478bd9Sstevel@tonic-gate 	    malloc((key_size + (numkeys = (short)(p - keymap))) *
239*7c478bd9Sstevel@tonic-gate 	    sizeof (_KEY_MAP *))) == NULL) {
240*7c478bd9Sstevel@tonic-gate 		goto out;
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate 	/*
244*7c478bd9Sstevel@tonic-gate 	 * Number of bytes needed is the number of structures times their size
245*7c478bd9Sstevel@tonic-gate 	 * malloc room for our array of _KEY_MAP structures
246*7c478bd9Sstevel@tonic-gate 	 */
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	if ((p = (_KEY_MAP *) malloc((unsigned)
249*7c478bd9Sstevel@tonic-gate 	    /* LINTED */
250*7c478bd9Sstevel@tonic-gate 	    (numbytes = (int)(sizeof (_KEY_MAP) * numkeys)))) == NULL) {
251*7c478bd9Sstevel@tonic-gate 		/* Can't do it, free list of pointers, indicate */
252*7c478bd9Sstevel@tonic-gate 		/* error upon return. */
253*7c478bd9Sstevel@tonic-gate 		free((char *) key_ptrs);
254*7c478bd9Sstevel@tonic-gate out:
255*7c478bd9Sstevel@tonic-gate 		term_errno = TERM_BAD_MALLOC;
256*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
257*7c478bd9Sstevel@tonic-gate 		strcpy(term_parm_err, "setkeymap");
258*7c478bd9Sstevel@tonic-gate 		termerr();
259*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
260*7c478bd9Sstevel@tonic-gate 		return (ERR);
261*7c478bd9Sstevel@tonic-gate 	}
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	if (key_size != 0) {
264*7c478bd9Sstevel@tonic-gate 		(void) memcpy((char *) &(key_ptrs[numkeys]),
265*7c478bd9Sstevel@tonic-gate 		    (char *) cur_term->_keys, (key_size *
266*7c478bd9Sstevel@tonic-gate 		    sizeof (_KEY_MAP *)));
267*7c478bd9Sstevel@tonic-gate 		free(cur_term->_keys);
268*7c478bd9Sstevel@tonic-gate 	}
269*7c478bd9Sstevel@tonic-gate 	(void) memcpy((char *) (cur_term->internal_keys = p),
270*7c478bd9Sstevel@tonic-gate 	    (char *) keymap, numbytes);
271*7c478bd9Sstevel@tonic-gate 	cur_term->_keys = key_ptrs;
272*7c478bd9Sstevel@tonic-gate 	cur_term->_ksz += numkeys;
273*7c478bd9Sstevel@tonic-gate 	/*
274*7c478bd9Sstevel@tonic-gate 	 * Reset _lastkey_ordered to -1 since we put the keys read in
275*7c478bd9Sstevel@tonic-gate 	 * from terminfo at the beginning of the keys table.
276*7c478bd9Sstevel@tonic-gate 	 */
277*7c478bd9Sstevel@tonic-gate 	cur_term->_lastkey_ordered = -1;
278*7c478bd9Sstevel@tonic-gate 	cur_term->_lastmacro_ordered += numkeys;
279*7c478bd9Sstevel@tonic-gate 	cur_term->_first_macro += numkeys;
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	/* Initialize our pointers to the structures */
282*7c478bd9Sstevel@tonic-gate 	while (numkeys--)
283*7c478bd9Sstevel@tonic-gate 		*key_ptrs++ = p++;
284*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
285*7c478bd9Sstevel@tonic-gate 	if (outf)
286*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "return key structure %x, ending at %x\n",
287*7c478bd9Sstevel@tonic-gate 		    keymap, p);
288*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
289*7c478bd9Sstevel@tonic-gate 	return (OK);
290*7c478bd9Sstevel@tonic-gate }
291