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 /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
44*7c478bd9Sstevel@tonic-gate #include	<string.h>
45*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate static	short	keycodes[] = {
48*7c478bd9Sstevel@tonic-gate 		    KEY_BACKSPACE,
49*7c478bd9Sstevel@tonic-gate 		    KEY_CATAB,
50*7c478bd9Sstevel@tonic-gate 		    KEY_CLEAR,
51*7c478bd9Sstevel@tonic-gate 		    KEY_CTAB,
52*7c478bd9Sstevel@tonic-gate 		    KEY_DC,
53*7c478bd9Sstevel@tonic-gate 		    KEY_DL,
54*7c478bd9Sstevel@tonic-gate 		    KEY_DOWN,
55*7c478bd9Sstevel@tonic-gate 		    KEY_EIC,
56*7c478bd9Sstevel@tonic-gate 		    KEY_EOL,
57*7c478bd9Sstevel@tonic-gate 		    KEY_EOS,
58*7c478bd9Sstevel@tonic-gate 		    KEY_F(0),
59*7c478bd9Sstevel@tonic-gate 		    KEY_F(1),
60*7c478bd9Sstevel@tonic-gate 		    KEY_F(10),
61*7c478bd9Sstevel@tonic-gate 		    KEY_F(2),
62*7c478bd9Sstevel@tonic-gate 		    KEY_F(3),
63*7c478bd9Sstevel@tonic-gate 		    KEY_F(4),
64*7c478bd9Sstevel@tonic-gate 		    KEY_F(5),
65*7c478bd9Sstevel@tonic-gate 		    KEY_F(6),
66*7c478bd9Sstevel@tonic-gate 		    KEY_F(7),
67*7c478bd9Sstevel@tonic-gate 		    KEY_F(8),
68*7c478bd9Sstevel@tonic-gate 		    KEY_F(9),
69*7c478bd9Sstevel@tonic-gate 		    KEY_HOME,
70*7c478bd9Sstevel@tonic-gate 		    KEY_IC,
71*7c478bd9Sstevel@tonic-gate 		    KEY_IL,
72*7c478bd9Sstevel@tonic-gate 		    KEY_LEFT,
73*7c478bd9Sstevel@tonic-gate 		    KEY_LL,
74*7c478bd9Sstevel@tonic-gate 		    KEY_NPAGE,
75*7c478bd9Sstevel@tonic-gate 		    KEY_PPAGE,
76*7c478bd9Sstevel@tonic-gate 		    KEY_RIGHT,
77*7c478bd9Sstevel@tonic-gate 		    KEY_SF,
78*7c478bd9Sstevel@tonic-gate 		    KEY_SR,
79*7c478bd9Sstevel@tonic-gate 		    KEY_STAB,
80*7c478bd9Sstevel@tonic-gate 		    KEY_UP,
81*7c478bd9Sstevel@tonic-gate 		    KEY_A1,
82*7c478bd9Sstevel@tonic-gate 		    KEY_A3,
83*7c478bd9Sstevel@tonic-gate 		    KEY_B2,
84*7c478bd9Sstevel@tonic-gate 		    KEY_C1,
85*7c478bd9Sstevel@tonic-gate 		    KEY_C3,
86*7c478bd9Sstevel@tonic-gate 		    KEY_BTAB,
87*7c478bd9Sstevel@tonic-gate 		    KEY_BEG,
88*7c478bd9Sstevel@tonic-gate 		    KEY_CANCEL,
89*7c478bd9Sstevel@tonic-gate 		    KEY_CLOSE,
90*7c478bd9Sstevel@tonic-gate 		    KEY_COMMAND,
91*7c478bd9Sstevel@tonic-gate 		    KEY_COPY,
92*7c478bd9Sstevel@tonic-gate 		    KEY_CREATE,
93*7c478bd9Sstevel@tonic-gate 		    KEY_END,
94*7c478bd9Sstevel@tonic-gate 		    KEY_ENTER,
95*7c478bd9Sstevel@tonic-gate 		    KEY_EXIT,
96*7c478bd9Sstevel@tonic-gate 		    KEY_FIND,
97*7c478bd9Sstevel@tonic-gate 		    KEY_HELP,
98*7c478bd9Sstevel@tonic-gate 		    KEY_MARK,
99*7c478bd9Sstevel@tonic-gate 		    KEY_MESSAGE,
100*7c478bd9Sstevel@tonic-gate 		    KEY_MOVE,
101*7c478bd9Sstevel@tonic-gate 		    KEY_NEXT,
102*7c478bd9Sstevel@tonic-gate 		    KEY_OPEN,
103*7c478bd9Sstevel@tonic-gate 		    KEY_OPTIONS,
104*7c478bd9Sstevel@tonic-gate 		    KEY_PREVIOUS,
105*7c478bd9Sstevel@tonic-gate 		    KEY_PRINT,
106*7c478bd9Sstevel@tonic-gate 		    KEY_REDO,
107*7c478bd9Sstevel@tonic-gate 		    KEY_REFERENCE,
108*7c478bd9Sstevel@tonic-gate 		    KEY_REFRESH,
109*7c478bd9Sstevel@tonic-gate 		    KEY_REPLACE,
110*7c478bd9Sstevel@tonic-gate 		    KEY_RESTART,
111*7c478bd9Sstevel@tonic-gate 		    KEY_RESUME,
112*7c478bd9Sstevel@tonic-gate 		    KEY_SAVE,
113*7c478bd9Sstevel@tonic-gate 		    KEY_SUSPEND,
114*7c478bd9Sstevel@tonic-gate 		    KEY_UNDO,
115*7c478bd9Sstevel@tonic-gate 		    KEY_SBEG,
116*7c478bd9Sstevel@tonic-gate 		    KEY_SCANCEL,
117*7c478bd9Sstevel@tonic-gate 		    KEY_SCOMMAND,
118*7c478bd9Sstevel@tonic-gate 		    KEY_SCOPY,
119*7c478bd9Sstevel@tonic-gate 		    KEY_SCREATE,
120*7c478bd9Sstevel@tonic-gate 		    KEY_SDC,
121*7c478bd9Sstevel@tonic-gate 		    KEY_SDL,
122*7c478bd9Sstevel@tonic-gate 		    KEY_SELECT,
123*7c478bd9Sstevel@tonic-gate 		    KEY_SEND,
124*7c478bd9Sstevel@tonic-gate 		    KEY_SEOL,
125*7c478bd9Sstevel@tonic-gate 		    KEY_SEXIT,
126*7c478bd9Sstevel@tonic-gate 		    KEY_SFIND,
127*7c478bd9Sstevel@tonic-gate 		    KEY_SHELP,
128*7c478bd9Sstevel@tonic-gate 		    KEY_SHOME,
129*7c478bd9Sstevel@tonic-gate 		    KEY_SIC,
130*7c478bd9Sstevel@tonic-gate 		    KEY_SLEFT,
131*7c478bd9Sstevel@tonic-gate 		    KEY_SMESSAGE,
132*7c478bd9Sstevel@tonic-gate 		    KEY_SMOVE,
133*7c478bd9Sstevel@tonic-gate 		    KEY_SNEXT,
134*7c478bd9Sstevel@tonic-gate 		    KEY_SOPTIONS,
135*7c478bd9Sstevel@tonic-gate 		    KEY_SPREVIOUS,
136*7c478bd9Sstevel@tonic-gate 		    KEY_SPRINT,
137*7c478bd9Sstevel@tonic-gate 		    KEY_SREDO,
138*7c478bd9Sstevel@tonic-gate 		    KEY_SREPLACE,
139*7c478bd9Sstevel@tonic-gate 		    KEY_SRIGHT,
140*7c478bd9Sstevel@tonic-gate 		    KEY_SRSUME,
141*7c478bd9Sstevel@tonic-gate 		    KEY_SSAVE,
142*7c478bd9Sstevel@tonic-gate 		    KEY_SSUSPEND,
143*7c478bd9Sstevel@tonic-gate 		    KEY_SUNDO,
144*7c478bd9Sstevel@tonic-gate 		    KEY_MOUSE
145*7c478bd9Sstevel@tonic-gate 		};
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate static	_KEY_MAP	*p;
148*7c478bd9Sstevel@tonic-gate static	bool		*funckey;
149*7c478bd9Sstevel@tonic-gate static	short		*codeptr;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate static	void
_laddone(char * txt)152*7c478bd9Sstevel@tonic-gate _laddone(char *txt)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	p->_sends = (txt);
155*7c478bd9Sstevel@tonic-gate 	p->_keyval = *codeptr;
156*7c478bd9Sstevel@tonic-gate 	funckey[(unsigned char)(txt)[0]] |= _KEY;
157*7c478bd9Sstevel@tonic-gate 	p++;
158*7c478bd9Sstevel@tonic-gate }
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate static	void
_keyfunc(char ** keyptr,char ** lastkey)163*7c478bd9Sstevel@tonic-gate _keyfunc(char **keyptr, char **lastkey)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	for (; keyptr <= lastkey; keyptr++, codeptr++)
166*7c478bd9Sstevel@tonic-gate 		if (*keyptr) {
167*7c478bd9Sstevel@tonic-gate 			p->_sends = (*keyptr);
168*7c478bd9Sstevel@tonic-gate 			p->_keyval = *codeptr;
169*7c478bd9Sstevel@tonic-gate 			funckey[(unsigned char)(*keyptr)[0]] |= _KEY;
170*7c478bd9Sstevel@tonic-gate 			p++;
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate /* Map text into num, updating the map structure p. */
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate static	void
_keyfunc2(char ** keyptr,char ** lastkey)177*7c478bd9Sstevel@tonic-gate _keyfunc2(char **keyptr, char **lastkey)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	short code_value = KEY_F(11);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	for (; *keyptr && keyptr <= lastkey; keyptr++, code_value++) {
182*7c478bd9Sstevel@tonic-gate 		p->_sends = *keyptr;
183*7c478bd9Sstevel@tonic-gate 		p->_keyval = (short) code_value;
184*7c478bd9Sstevel@tonic-gate 		funckey[(unsigned char)*keyptr[0]] |= _KEY;
185*7c478bd9Sstevel@tonic-gate 		p++;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate int
setkeymap(void)190*7c478bd9Sstevel@tonic-gate setkeymap(void)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	_KEY_MAP	keymap[((sizeof (keycodes) / sizeof (short)) +
193*7c478bd9Sstevel@tonic-gate 			    ((KEY_F(63) - KEY_F(11)) + 1))], **key_ptrs;
194*7c478bd9Sstevel@tonic-gate 	short		numkeys;
195*7c478bd9Sstevel@tonic-gate 	int		numbytes, key_size = cur_term->_ksz;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	if (cur_term->internal_keys != NULL)
198*7c478bd9Sstevel@tonic-gate 		return (ERR);
199*7c478bd9Sstevel@tonic-gate 	p = keymap;
200*7c478bd9Sstevel@tonic-gate 	codeptr = keycodes;
201*7c478bd9Sstevel@tonic-gate 	funckey = cur_term->funckeystarter;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/* If backspace key sends \b, don't map it. */
204*7c478bd9Sstevel@tonic-gate 	if (key_backspace && strcmp(key_backspace, "\b"))
205*7c478bd9Sstevel@tonic-gate 		_laddone(key_backspace);
206*7c478bd9Sstevel@tonic-gate 	codeptr++;
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_catab, &key_dl);
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/* If down arrow key sends \n, don't map it. */
211*7c478bd9Sstevel@tonic-gate 	if (key_down && strcmp(key_down, "\n"))
212*7c478bd9Sstevel@tonic-gate 		_laddone(key_down);
213*7c478bd9Sstevel@tonic-gate 	codeptr++;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_eic, &key_il);
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	/* If left arrow key sends \b, don't map it. */
218*7c478bd9Sstevel@tonic-gate 	if (key_left && strcmp(key_left, "\b"))
219*7c478bd9Sstevel@tonic-gate 		_laddone(key_left);
220*7c478bd9Sstevel@tonic-gate 	codeptr++;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_ll, &key_up);
223*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_a1, &key_c3);
224*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_btab, &key_btab);
225*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_beg, &key_sundo);
226*7c478bd9Sstevel@tonic-gate 	_keyfunc2(&key_f11, &key_f63);
227*7c478bd9Sstevel@tonic-gate 	_keyfunc(&key_mouse, &key_mouse);
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	/*
230*7c478bd9Sstevel@tonic-gate 	 * malloc returns the address of a list of pointers to
231*7c478bd9Sstevel@tonic-gate 	 * (_KEY_MAP *) structures
232*7c478bd9Sstevel@tonic-gate 	 */
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	if ((key_ptrs = (_KEY_MAP **)
235*7c478bd9Sstevel@tonic-gate 	    /* LINTED */
236*7c478bd9Sstevel@tonic-gate 	    malloc((key_size + (numkeys = (short)(p - keymap))) *
237*7c478bd9Sstevel@tonic-gate 	    sizeof (_KEY_MAP *))) == NULL) {
238*7c478bd9Sstevel@tonic-gate 		goto out;
239*7c478bd9Sstevel@tonic-gate 	}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	/*
242*7c478bd9Sstevel@tonic-gate 	 * Number of bytes needed is the number of structures times their size
243*7c478bd9Sstevel@tonic-gate 	 * malloc room for our array of _KEY_MAP structures
244*7c478bd9Sstevel@tonic-gate 	 */
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if ((p = (_KEY_MAP *) malloc((unsigned)
247*7c478bd9Sstevel@tonic-gate 	    /* LINTED */
248*7c478bd9Sstevel@tonic-gate 	    (numbytes = (int)(sizeof (_KEY_MAP) * numkeys)))) == NULL) {
249*7c478bd9Sstevel@tonic-gate 		/* Can't do it, free list of pointers, indicate */
250*7c478bd9Sstevel@tonic-gate 		/* error upon return. */
251*7c478bd9Sstevel@tonic-gate 		free((char *) key_ptrs);
252*7c478bd9Sstevel@tonic-gate out:
253*7c478bd9Sstevel@tonic-gate 		term_errno = TERM_BAD_MALLOC;
254*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
255*7c478bd9Sstevel@tonic-gate 		strcpy(term_parm_err, "setkeymap");
256*7c478bd9Sstevel@tonic-gate 		termerr();
257*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
258*7c478bd9Sstevel@tonic-gate 		return (ERR);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	if (key_size != 0) {
262*7c478bd9Sstevel@tonic-gate 		(void) memcpy((char *) &(key_ptrs[numkeys]),
263*7c478bd9Sstevel@tonic-gate 		    (char *) cur_term->_keys, (key_size *
264*7c478bd9Sstevel@tonic-gate 		    sizeof (_KEY_MAP *)));
265*7c478bd9Sstevel@tonic-gate 		free(cur_term->_keys);
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 	(void) memcpy((char *) (cur_term->internal_keys = p),
268*7c478bd9Sstevel@tonic-gate 	    (char *) keymap, numbytes);
269*7c478bd9Sstevel@tonic-gate 	cur_term->_keys = key_ptrs;
270*7c478bd9Sstevel@tonic-gate 	cur_term->_ksz += numkeys;
271*7c478bd9Sstevel@tonic-gate 	/*
272*7c478bd9Sstevel@tonic-gate 	 * Reset _lastkey_ordered to -1 since we put the keys read in
273*7c478bd9Sstevel@tonic-gate 	 * from terminfo at the beginning of the keys table.
274*7c478bd9Sstevel@tonic-gate 	 */
275*7c478bd9Sstevel@tonic-gate 	cur_term->_lastkey_ordered = -1;
276*7c478bd9Sstevel@tonic-gate 	cur_term->_lastmacro_ordered += numkeys;
277*7c478bd9Sstevel@tonic-gate 	cur_term->_first_macro += numkeys;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	/* Initialize our pointers to the structures */
280*7c478bd9Sstevel@tonic-gate 	while (numkeys--)
281*7c478bd9Sstevel@tonic-gate 		*key_ptrs++ = p++;
282*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
283*7c478bd9Sstevel@tonic-gate 	if (outf)
284*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "return key structure %x, ending at %x\n",
285*7c478bd9Sstevel@tonic-gate 		    keymap, p);
286*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
287*7c478bd9Sstevel@tonic-gate 	return (OK);
288*7c478bd9Sstevel@tonic-gate }
289