1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * All rights reserved.
5*0Sstevel@tonic-gate *
6*0Sstevel@tonic-gate * Permission is hereby granted, free of charge, to any person obtaining a
7*0Sstevel@tonic-gate * copy of this software and associated documentation files (the
8*0Sstevel@tonic-gate * "Software"), to deal in the Software without restriction, including
9*0Sstevel@tonic-gate * without limitation the rights to use, copy, modify, merge, publish,
10*0Sstevel@tonic-gate * distribute, and/or sell copies of the Software, and to permit persons
11*0Sstevel@tonic-gate * to whom the Software is furnished to do so, provided that the above
12*0Sstevel@tonic-gate * copyright notice(s) and this permission notice appear in all copies of
13*0Sstevel@tonic-gate * the Software and that both the above copyright notice(s) and this
14*0Sstevel@tonic-gate * permission notice appear in supporting documentation.
15*0Sstevel@tonic-gate *
16*0Sstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*0Sstevel@tonic-gate * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*0Sstevel@tonic-gate * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19*0Sstevel@tonic-gate * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20*0Sstevel@tonic-gate * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
21*0Sstevel@tonic-gate * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
22*0Sstevel@tonic-gate * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
23*0Sstevel@tonic-gate * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
24*0Sstevel@tonic-gate * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25*0Sstevel@tonic-gate *
26*0Sstevel@tonic-gate * Except as contained in this notice, the name of a copyright holder
27*0Sstevel@tonic-gate * shall not be used in advertising or otherwise to promote the sale, use
28*0Sstevel@tonic-gate * or other dealings in this Software without prior written authorization
29*0Sstevel@tonic-gate * of the copyright holder.
30*0Sstevel@tonic-gate */
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <stdlib.h>
36*0Sstevel@tonic-gate #include <string.h>
37*0Sstevel@tonic-gate #include <ctype.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate #include "keytab.h"
41*0Sstevel@tonic-gate #include "strngmem.h"
42*0Sstevel@tonic-gate #include "getline.h"
43*0Sstevel@tonic-gate #include "errmsg.h"
44*0Sstevel@tonic-gate #include "hash.h"
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate * When allocating or reallocating the key-binding table, how
48*0Sstevel@tonic-gate * many entries should be added?
49*0Sstevel@tonic-gate */
50*0Sstevel@tonic-gate #define KT_TABLE_INC 100
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate * Define the size of the hash table that is used to associate action
54*0Sstevel@tonic-gate * names with action functions. This should be a prime number.
55*0Sstevel@tonic-gate */
56*0Sstevel@tonic-gate #define KT_HASH_SIZE 113
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate * Define a binary-symbol-table object.
60*0Sstevel@tonic-gate */
61*0Sstevel@tonic-gate struct KeyTab {
62*0Sstevel@tonic-gate ErrMsg *err; /* Information about the last error */
63*0Sstevel@tonic-gate int size; /* The allocated dimension of table[] */
64*0Sstevel@tonic-gate int nkey; /* The current number of members in the table */
65*0Sstevel@tonic-gate KeySym *table; /* The table of lexically sorted key sequences */
66*0Sstevel@tonic-gate HashTable *actions; /* The hash table of actions */
67*0Sstevel@tonic-gate StringMem *smem; /* Memory for allocating strings */
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate static int _kt_extend_table(KeyTab *kt);
71*0Sstevel@tonic-gate static int _kt_parse_keybinding_string(const char *keyseq,
72*0Sstevel@tonic-gate char *binary, int *nc);
73*0Sstevel@tonic-gate static int _kt_compare_strings(const char *s1, int n1, const char *s2, int n2);
74*0Sstevel@tonic-gate static void _kt_assign_action(KeySym *sym, KtBinder binder, KtKeyFn *keyfn,
75*0Sstevel@tonic-gate void *data);
76*0Sstevel@tonic-gate static char _kt_backslash_escape(const char *string, const char **endp);
77*0Sstevel@tonic-gate static int _kt_is_emacs_meta(const char *string);
78*0Sstevel@tonic-gate static int _kt_is_emacs_ctrl(const char *string);
79*0Sstevel@tonic-gate static KtKeyMatch _kt_locate_keybinding(KeyTab *kt, const char *binary_keyseq,
80*0Sstevel@tonic-gate int nc, int *first, int *last);
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate /*.......................................................................
83*0Sstevel@tonic-gate * Create a new key-binding symbol table.
84*0Sstevel@tonic-gate *
85*0Sstevel@tonic-gate * Output:
86*0Sstevel@tonic-gate * return KeyTab * The new object, or NULL on error.
87*0Sstevel@tonic-gate */
_new_KeyTab(void)88*0Sstevel@tonic-gate KeyTab *_new_KeyTab(void)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate KeyTab *kt; /* The object to be returned */
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate * Allocate the container.
93*0Sstevel@tonic-gate */
94*0Sstevel@tonic-gate kt = (KeyTab *) malloc(sizeof(KeyTab));
95*0Sstevel@tonic-gate if(!kt) {
96*0Sstevel@tonic-gate errno = ENOMEM;
97*0Sstevel@tonic-gate return NULL;
98*0Sstevel@tonic-gate };
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate * Before attempting any operation that might fail, initialize the
101*0Sstevel@tonic-gate * container at least up to the point at which it can safely be passed
102*0Sstevel@tonic-gate * to del_KeyTab().
103*0Sstevel@tonic-gate */
104*0Sstevel@tonic-gate kt->err = NULL;
105*0Sstevel@tonic-gate kt->size = KT_TABLE_INC;
106*0Sstevel@tonic-gate kt->nkey = 0;
107*0Sstevel@tonic-gate kt->table = NULL;
108*0Sstevel@tonic-gate kt->actions = NULL;
109*0Sstevel@tonic-gate kt->smem = NULL;
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate * Allocate a place to record error messages.
112*0Sstevel@tonic-gate */
113*0Sstevel@tonic-gate kt->err = _new_ErrMsg();
114*0Sstevel@tonic-gate if(!kt->err)
115*0Sstevel@tonic-gate return _del_KeyTab(kt);
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate * Allocate the table.
118*0Sstevel@tonic-gate */
119*0Sstevel@tonic-gate kt->table = (KeySym *) malloc(sizeof(kt->table[0]) * kt->size);
120*0Sstevel@tonic-gate if(!kt->table) {
121*0Sstevel@tonic-gate errno = ENOMEM;
122*0Sstevel@tonic-gate return _del_KeyTab(kt);
123*0Sstevel@tonic-gate };
124*0Sstevel@tonic-gate /*
125*0Sstevel@tonic-gate * Allocate a hash table of actions.
126*0Sstevel@tonic-gate */
127*0Sstevel@tonic-gate kt->actions = _new_HashTable(NULL, KT_HASH_SIZE, IGNORE_CASE, NULL, 0);
128*0Sstevel@tonic-gate if(!kt->actions)
129*0Sstevel@tonic-gate return _del_KeyTab(kt);
130*0Sstevel@tonic-gate /*
131*0Sstevel@tonic-gate * Allocate a string allocation object. This allows allocation of
132*0Sstevel@tonic-gate * small strings without fragmenting the heap.
133*0Sstevel@tonic-gate */
134*0Sstevel@tonic-gate kt->smem = _new_StringMem(KT_TABLE_INC);
135*0Sstevel@tonic-gate if(!kt->smem)
136*0Sstevel@tonic-gate return _del_KeyTab(kt);
137*0Sstevel@tonic-gate return kt;
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate /*.......................................................................
141*0Sstevel@tonic-gate * Delete a KeyTab object.
142*0Sstevel@tonic-gate *
143*0Sstevel@tonic-gate * Input:
144*0Sstevel@tonic-gate * kt KeyTab * The object to be deleted.
145*0Sstevel@tonic-gate * Output:
146*0Sstevel@tonic-gate * return KeyTab * The deleted object (always NULL).
147*0Sstevel@tonic-gate */
_del_KeyTab(KeyTab * kt)148*0Sstevel@tonic-gate KeyTab *_del_KeyTab(KeyTab *kt)
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate if(kt) {
151*0Sstevel@tonic-gate if(kt->table)
152*0Sstevel@tonic-gate free(kt->table);
153*0Sstevel@tonic-gate kt->actions = _del_HashTable(kt->actions);
154*0Sstevel@tonic-gate kt->smem = _del_StringMem(kt->smem, 1);
155*0Sstevel@tonic-gate kt->err = _del_ErrMsg(kt->err);
156*0Sstevel@tonic-gate free(kt);
157*0Sstevel@tonic-gate };
158*0Sstevel@tonic-gate return NULL;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /*.......................................................................
162*0Sstevel@tonic-gate * Increase the size of the table to accomodate more keys.
163*0Sstevel@tonic-gate *
164*0Sstevel@tonic-gate * Input:
165*0Sstevel@tonic-gate * kt KeyTab * The table to be extended.
166*0Sstevel@tonic-gate * Output:
167*0Sstevel@tonic-gate * return int 0 - OK.
168*0Sstevel@tonic-gate * 1 - Error.
169*0Sstevel@tonic-gate */
_kt_extend_table(KeyTab * kt)170*0Sstevel@tonic-gate static int _kt_extend_table(KeyTab *kt)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate * Attempt to increase the size of the table.
174*0Sstevel@tonic-gate */
175*0Sstevel@tonic-gate KeySym *newtab = (KeySym *) realloc(kt->table, sizeof(kt->table[0]) *
176*0Sstevel@tonic-gate (kt->size + KT_TABLE_INC));
177*0Sstevel@tonic-gate /*
178*0Sstevel@tonic-gate * Failed?
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate if(!newtab) {
181*0Sstevel@tonic-gate _err_record_msg(kt->err, "Can't extend keybinding table", END_ERR_MSG);
182*0Sstevel@tonic-gate errno = ENOMEM;
183*0Sstevel@tonic-gate return 1;
184*0Sstevel@tonic-gate };
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * Install the resized table.
187*0Sstevel@tonic-gate */
188*0Sstevel@tonic-gate kt->table = newtab;
189*0Sstevel@tonic-gate kt->size += KT_TABLE_INC;
190*0Sstevel@tonic-gate return 0;
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /*.......................................................................
194*0Sstevel@tonic-gate * Add, update or remove a keybinding to the table.
195*0Sstevel@tonic-gate *
196*0Sstevel@tonic-gate * Input:
197*0Sstevel@tonic-gate * kt KeyTab * The table to add the binding to.
198*0Sstevel@tonic-gate * binder KtBinder The source of the binding.
199*0Sstevel@tonic-gate * keyseq const char * The key-sequence to bind.
200*0Sstevel@tonic-gate * action char * The action to associate with the key sequence, or
201*0Sstevel@tonic-gate * NULL to remove the action associated with the
202*0Sstevel@tonic-gate * key sequence.
203*0Sstevel@tonic-gate * Output:
204*0Sstevel@tonic-gate * return int 0 - OK.
205*0Sstevel@tonic-gate * 1 - Error.
206*0Sstevel@tonic-gate */
_kt_set_keybinding(KeyTab * kt,KtBinder binder,const char * keyseq,const char * action)207*0Sstevel@tonic-gate int _kt_set_keybinding(KeyTab *kt, KtBinder binder, const char *keyseq,
208*0Sstevel@tonic-gate const char *action)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate KtKeyFn *keyfn; /* The action function */
211*0Sstevel@tonic-gate void *data; /* The callback data of the action function */
212*0Sstevel@tonic-gate /*
213*0Sstevel@tonic-gate * Check arguments.
214*0Sstevel@tonic-gate */
215*0Sstevel@tonic-gate if(kt==NULL || !keyseq) {
216*0Sstevel@tonic-gate errno = EINVAL;
217*0Sstevel@tonic-gate if(kt)
218*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
219*0Sstevel@tonic-gate return 1;
220*0Sstevel@tonic-gate };
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate * Lookup the function that implements the specified action.
223*0Sstevel@tonic-gate */
224*0Sstevel@tonic-gate if(!action) {
225*0Sstevel@tonic-gate keyfn = 0;
226*0Sstevel@tonic-gate data = NULL;
227*0Sstevel@tonic-gate } else {
228*0Sstevel@tonic-gate Symbol *sym = _find_HashSymbol(kt->actions, action);
229*0Sstevel@tonic-gate if(!sym) {
230*0Sstevel@tonic-gate _err_record_msg(kt->err, "Unknown key-binding action: ", action,
231*0Sstevel@tonic-gate END_ERR_MSG);
232*0Sstevel@tonic-gate errno = EINVAL;
233*0Sstevel@tonic-gate return 1;
234*0Sstevel@tonic-gate };
235*0Sstevel@tonic-gate keyfn = (KtKeyFn *) sym->fn;
236*0Sstevel@tonic-gate data = sym->data;
237*0Sstevel@tonic-gate };
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate * Record the action in the table.
240*0Sstevel@tonic-gate */
241*0Sstevel@tonic-gate return _kt_set_keyfn(kt, binder, keyseq, keyfn, data);
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /*.......................................................................
245*0Sstevel@tonic-gate * Add, update or remove a keybinding to the table, specifying an action
246*0Sstevel@tonic-gate * function directly.
247*0Sstevel@tonic-gate *
248*0Sstevel@tonic-gate * Input:
249*0Sstevel@tonic-gate * kt KeyTab * The table to add the binding to.
250*0Sstevel@tonic-gate * binder KtBinder The source of the binding.
251*0Sstevel@tonic-gate * keyseq char * The key-sequence to bind.
252*0Sstevel@tonic-gate * keyfn KtKeyFn * The action function, or NULL to remove any existing
253*0Sstevel@tonic-gate * action function.
254*0Sstevel@tonic-gate * data void * A pointer to anonymous data to be passed to keyfn
255*0Sstevel@tonic-gate * whenever it is called.
256*0Sstevel@tonic-gate * Output:
257*0Sstevel@tonic-gate * return int 0 - OK.
258*0Sstevel@tonic-gate * 1 - Error.
259*0Sstevel@tonic-gate */
_kt_set_keyfn(KeyTab * kt,KtBinder binder,const char * keyseq,KtKeyFn * keyfn,void * data)260*0Sstevel@tonic-gate int _kt_set_keyfn(KeyTab *kt, KtBinder binder, const char *keyseq,
261*0Sstevel@tonic-gate KtKeyFn *keyfn, void *data)
262*0Sstevel@tonic-gate {
263*0Sstevel@tonic-gate const char *kptr; /* A pointer into keyseq[] */
264*0Sstevel@tonic-gate char *binary; /* The binary version of keyseq[] */
265*0Sstevel@tonic-gate int nc; /* The number of characters in binary[] */
266*0Sstevel@tonic-gate int first,last; /* The first and last entries in the table which */
267*0Sstevel@tonic-gate /* minimally match. */
268*0Sstevel@tonic-gate int size; /* The size to allocate for the binary string */
269*0Sstevel@tonic-gate int i;
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate * Check arguments.
272*0Sstevel@tonic-gate */
273*0Sstevel@tonic-gate if(kt==NULL || !keyseq) {
274*0Sstevel@tonic-gate errno = EINVAL;
275*0Sstevel@tonic-gate if(kt)
276*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
277*0Sstevel@tonic-gate return 1;
278*0Sstevel@tonic-gate };
279*0Sstevel@tonic-gate /*
280*0Sstevel@tonic-gate * Work out a pessimistic estimate of how much space will be needed
281*0Sstevel@tonic-gate * for the binary copy of the string, noting that binary meta characters
282*0Sstevel@tonic-gate * embedded in the input string get split into two characters.
283*0Sstevel@tonic-gate */
284*0Sstevel@tonic-gate for(size=0,kptr = keyseq; *kptr; kptr++)
285*0Sstevel@tonic-gate size += IS_META_CHAR(*kptr) ? 2 : 1;
286*0Sstevel@tonic-gate /*
287*0Sstevel@tonic-gate * Allocate a string that has the length of keyseq[].
288*0Sstevel@tonic-gate */
289*0Sstevel@tonic-gate binary = _new_StringMemString(kt->smem, size + 1);
290*0Sstevel@tonic-gate if(!binary) {
291*0Sstevel@tonic-gate errno = ENOMEM;
292*0Sstevel@tonic-gate _err_record_msg(kt->err, "Insufficient memory to record key sequence",
293*0Sstevel@tonic-gate END_ERR_MSG);
294*0Sstevel@tonic-gate return 1;
295*0Sstevel@tonic-gate };
296*0Sstevel@tonic-gate /*
297*0Sstevel@tonic-gate * Convert control and octal character specifications to binary characters.
298*0Sstevel@tonic-gate */
299*0Sstevel@tonic-gate if(_kt_parse_keybinding_string(keyseq, binary, &nc)) {
300*0Sstevel@tonic-gate binary = _del_StringMemString(kt->smem, binary);
301*0Sstevel@tonic-gate return 1;
302*0Sstevel@tonic-gate };
303*0Sstevel@tonic-gate /*
304*0Sstevel@tonic-gate * Lookup the position in the table at which to insert the binding.
305*0Sstevel@tonic-gate */
306*0Sstevel@tonic-gate switch(_kt_locate_keybinding(kt, binary, nc, &first, &last)) {
307*0Sstevel@tonic-gate /*
308*0Sstevel@tonic-gate * If an exact match for the key-sequence is already in the table,
309*0Sstevel@tonic-gate * simply replace its binding function (or delete the entry if
310*0Sstevel@tonic-gate * the new binding is 0).
311*0Sstevel@tonic-gate */
312*0Sstevel@tonic-gate case KT_EXACT_MATCH:
313*0Sstevel@tonic-gate if(keyfn) {
314*0Sstevel@tonic-gate _kt_assign_action(kt->table + first, binder, keyfn, data);
315*0Sstevel@tonic-gate } else {
316*0Sstevel@tonic-gate _del_StringMemString(kt->smem, kt->table[first].keyseq);
317*0Sstevel@tonic-gate memmove(kt->table + first, kt->table + first + 1,
318*0Sstevel@tonic-gate (kt->nkey - first - 1) * sizeof(kt->table[0]));
319*0Sstevel@tonic-gate kt->nkey--;
320*0Sstevel@tonic-gate };
321*0Sstevel@tonic-gate binary = _del_StringMemString(kt->smem, binary);
322*0Sstevel@tonic-gate break;
323*0Sstevel@tonic-gate /*
324*0Sstevel@tonic-gate * If an ambiguous match has been found and we are installing a
325*0Sstevel@tonic-gate * callback, then our new key-sequence would hide all of the ambiguous
326*0Sstevel@tonic-gate * matches, so we shouldn't allow it.
327*0Sstevel@tonic-gate */
328*0Sstevel@tonic-gate case KT_AMBIG_MATCH:
329*0Sstevel@tonic-gate if(keyfn) {
330*0Sstevel@tonic-gate _err_record_msg(kt->err, "Can't bind \"", keyseq,
331*0Sstevel@tonic-gate "\", because it is a prefix of another binding",
332*0Sstevel@tonic-gate END_ERR_MSG);
333*0Sstevel@tonic-gate binary = _del_StringMemString(kt->smem, binary);
334*0Sstevel@tonic-gate errno = EPERM;
335*0Sstevel@tonic-gate return 1;
336*0Sstevel@tonic-gate };
337*0Sstevel@tonic-gate break;
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate * If the entry doesn't exist, create it.
340*0Sstevel@tonic-gate */
341*0Sstevel@tonic-gate case KT_NO_MATCH:
342*0Sstevel@tonic-gate /*
343*0Sstevel@tonic-gate * Add a new binding?
344*0Sstevel@tonic-gate */
345*0Sstevel@tonic-gate if(keyfn) {
346*0Sstevel@tonic-gate KeySym *sym;
347*0Sstevel@tonic-gate /*
348*0Sstevel@tonic-gate * We will need a new entry, extend the table if needed.
349*0Sstevel@tonic-gate */
350*0Sstevel@tonic-gate if(kt->nkey + 1 > kt->size) {
351*0Sstevel@tonic-gate if(_kt_extend_table(kt)) {
352*0Sstevel@tonic-gate binary = _del_StringMemString(kt->smem, binary);
353*0Sstevel@tonic-gate return 1;
354*0Sstevel@tonic-gate };
355*0Sstevel@tonic-gate };
356*0Sstevel@tonic-gate /*
357*0Sstevel@tonic-gate * Make space to insert the new key-sequence before 'last'.
358*0Sstevel@tonic-gate */
359*0Sstevel@tonic-gate if(last < kt->nkey) {
360*0Sstevel@tonic-gate memmove(kt->table + last + 1, kt->table + last,
361*0Sstevel@tonic-gate (kt->nkey - last) * sizeof(kt->table[0]));
362*0Sstevel@tonic-gate };
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate * Insert the new binding in the vacated position.
365*0Sstevel@tonic-gate */
366*0Sstevel@tonic-gate sym = kt->table + last;
367*0Sstevel@tonic-gate sym->keyseq = binary;
368*0Sstevel@tonic-gate sym->nc = nc;
369*0Sstevel@tonic-gate for(i=0; i<KTB_NBIND; i++) {
370*0Sstevel@tonic-gate KtAction *action = sym->actions + i;
371*0Sstevel@tonic-gate action->fn = 0;
372*0Sstevel@tonic-gate action->data = NULL;
373*0Sstevel@tonic-gate };
374*0Sstevel@tonic-gate sym->binder = -1;
375*0Sstevel@tonic-gate _kt_assign_action(sym, binder, keyfn, data);
376*0Sstevel@tonic-gate kt->nkey++;
377*0Sstevel@tonic-gate };
378*0Sstevel@tonic-gate break;
379*0Sstevel@tonic-gate case KT_BAD_MATCH:
380*0Sstevel@tonic-gate binary = _del_StringMemString(kt->smem, binary);
381*0Sstevel@tonic-gate return 1;
382*0Sstevel@tonic-gate break;
383*0Sstevel@tonic-gate };
384*0Sstevel@tonic-gate return 0;
385*0Sstevel@tonic-gate }
386*0Sstevel@tonic-gate
387*0Sstevel@tonic-gate /*.......................................................................
388*0Sstevel@tonic-gate * Perform a min-match lookup of a key-binding.
389*0Sstevel@tonic-gate *
390*0Sstevel@tonic-gate * Input:
391*0Sstevel@tonic-gate * kt KeyTab * The keybinding table to lookup in.
392*0Sstevel@tonic-gate * binary_keyseq char * The binary key-sequence to lookup.
393*0Sstevel@tonic-gate * nc int the number of characters in keyseq[].
394*0Sstevel@tonic-gate * Input/Output:
395*0Sstevel@tonic-gate * first,last int * If there is an ambiguous or exact match, the indexes
396*0Sstevel@tonic-gate * of the first and last symbols that minimally match
397*0Sstevel@tonic-gate * will be assigned to *first and *last respectively.
398*0Sstevel@tonic-gate * If there is no match, then first and last will
399*0Sstevel@tonic-gate * bracket the location where the symbol should be
400*0Sstevel@tonic-gate * inserted.
401*0Sstevel@tonic-gate * Output:
402*0Sstevel@tonic-gate * return KtKeyMatch One of the following enumerators:
403*0Sstevel@tonic-gate * KT_EXACT_MATCH - An exact match was found.
404*0Sstevel@tonic-gate * KT_AMBIG_MATCH - An ambiguous match was found.
405*0Sstevel@tonic-gate * KT_NO_MATCH - No match was found.
406*0Sstevel@tonic-gate * KT_BAD_MATCH - An error occurred while searching.
407*0Sstevel@tonic-gate */
_kt_locate_keybinding(KeyTab * kt,const char * binary_keyseq,int nc,int * first,int * last)408*0Sstevel@tonic-gate static KtKeyMatch _kt_locate_keybinding(KeyTab *kt, const char *binary_keyseq,
409*0Sstevel@tonic-gate int nc, int *first, int *last)
410*0Sstevel@tonic-gate {
411*0Sstevel@tonic-gate int mid; /* The index at which to bisect the table */
412*0Sstevel@tonic-gate int bot; /* The lowest index of the table not searched yet */
413*0Sstevel@tonic-gate int top; /* The highest index of the table not searched yet */
414*0Sstevel@tonic-gate int test; /* The return value of strcmp() */
415*0Sstevel@tonic-gate /*
416*0Sstevel@tonic-gate * Perform a binary search for the key-sequence.
417*0Sstevel@tonic-gate */
418*0Sstevel@tonic-gate bot = 0;
419*0Sstevel@tonic-gate top = kt->nkey - 1;
420*0Sstevel@tonic-gate while(top >= bot) {
421*0Sstevel@tonic-gate mid = (top + bot)/2;
422*0Sstevel@tonic-gate test = _kt_compare_strings(kt->table[mid].keyseq, kt->table[mid].nc,
423*0Sstevel@tonic-gate binary_keyseq, nc);
424*0Sstevel@tonic-gate if(test > 0)
425*0Sstevel@tonic-gate top = mid - 1;
426*0Sstevel@tonic-gate else if(test < 0)
427*0Sstevel@tonic-gate bot = mid + 1;
428*0Sstevel@tonic-gate else {
429*0Sstevel@tonic-gate *first = *last = mid;
430*0Sstevel@tonic-gate return KT_EXACT_MATCH;
431*0Sstevel@tonic-gate };
432*0Sstevel@tonic-gate };
433*0Sstevel@tonic-gate /*
434*0Sstevel@tonic-gate * An exact match wasn't found, but top is the index just below the
435*0Sstevel@tonic-gate * index where a match would be found, and bot is the index just above
436*0Sstevel@tonic-gate * where the match ought to be found.
437*0Sstevel@tonic-gate */
438*0Sstevel@tonic-gate *first = top;
439*0Sstevel@tonic-gate *last = bot;
440*0Sstevel@tonic-gate /*
441*0Sstevel@tonic-gate * See if any ambiguous matches exist, and if so make *first and *last
442*0Sstevel@tonic-gate * refer to the first and last matches.
443*0Sstevel@tonic-gate */
444*0Sstevel@tonic-gate if(*last < kt->nkey && kt->table[*last].nc > nc &&
445*0Sstevel@tonic-gate _kt_compare_strings(kt->table[*last].keyseq, nc, binary_keyseq, nc)==0) {
446*0Sstevel@tonic-gate *first = *last;
447*0Sstevel@tonic-gate while(*last+1 < kt->nkey && kt->table[*last+1].nc > nc &&
448*0Sstevel@tonic-gate _kt_compare_strings(kt->table[*last+1].keyseq, nc, binary_keyseq, nc)==0)
449*0Sstevel@tonic-gate (*last)++;
450*0Sstevel@tonic-gate return KT_AMBIG_MATCH;
451*0Sstevel@tonic-gate };
452*0Sstevel@tonic-gate /*
453*0Sstevel@tonic-gate * No match.
454*0Sstevel@tonic-gate */
455*0Sstevel@tonic-gate return KT_NO_MATCH;
456*0Sstevel@tonic-gate }
457*0Sstevel@tonic-gate
458*0Sstevel@tonic-gate /*.......................................................................
459*0Sstevel@tonic-gate * Lookup the sub-array of key-bindings who's key-sequences minimally
460*0Sstevel@tonic-gate * match a given key-sequence.
461*0Sstevel@tonic-gate *
462*0Sstevel@tonic-gate * Input:
463*0Sstevel@tonic-gate * kt KeyTab * The keybinding table to lookup in.
464*0Sstevel@tonic-gate * binary_keyseq char * The binary key-sequence to lookup.
465*0Sstevel@tonic-gate * nc int the number of characters in keyseq[].
466*0Sstevel@tonic-gate * Input/Output:
467*0Sstevel@tonic-gate * matches KeySym ** The array of minimally matching symbols
468*0Sstevel@tonic-gate * can be found in (*matches)[0..nmatch-1], unless
469*0Sstevel@tonic-gate * no match was found, in which case *matches will
470*0Sstevel@tonic-gate * be set to NULL.
471*0Sstevel@tonic-gate * nmatch int The number of ambiguously matching symbols. This
472*0Sstevel@tonic-gate * will be 0 if there is no match, 1 for an exact
473*0Sstevel@tonic-gate * match, and a number greater than 1 for an ambiguous
474*0Sstevel@tonic-gate * match.
475*0Sstevel@tonic-gate * Output:
476*0Sstevel@tonic-gate * return KtKeyMatch One of the following enumerators:
477*0Sstevel@tonic-gate * KT_EXACT_MATCH - An exact match was found.
478*0Sstevel@tonic-gate * KT_AMBIG_MATCH - An ambiguous match was found.
479*0Sstevel@tonic-gate * KT_NO_MATCH - No match was found.
480*0Sstevel@tonic-gate * KT_BAD_MATCH - An error occurred while searching.
481*0Sstevel@tonic-gate */
_kt_lookup_keybinding(KeyTab * kt,const char * binary_keyseq,int nc,KeySym ** matches,int * nmatch)482*0Sstevel@tonic-gate KtKeyMatch _kt_lookup_keybinding(KeyTab *kt, const char *binary_keyseq,
483*0Sstevel@tonic-gate int nc, KeySym **matches, int *nmatch)
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate KtKeyMatch status; /* The return status */
486*0Sstevel@tonic-gate int first,last; /* The indexes of the first and last matching entry */
487*0Sstevel@tonic-gate /* in the symbol table. */
488*0Sstevel@tonic-gate /*
489*0Sstevel@tonic-gate * Check the arguments.
490*0Sstevel@tonic-gate */
491*0Sstevel@tonic-gate if(!kt || !binary_keyseq || !matches || !nmatch || nc < 0) {
492*0Sstevel@tonic-gate errno = EINVAL;
493*0Sstevel@tonic-gate if(kt)
494*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
495*0Sstevel@tonic-gate return KT_BAD_MATCH;
496*0Sstevel@tonic-gate };
497*0Sstevel@tonic-gate /*
498*0Sstevel@tonic-gate * Lookup the indexes of the binding-table entries that bracket the
499*0Sstevel@tonic-gate * target key-sequence.
500*0Sstevel@tonic-gate */
501*0Sstevel@tonic-gate status = _kt_locate_keybinding(kt, binary_keyseq, nc, &first, &last);
502*0Sstevel@tonic-gate /*
503*0Sstevel@tonic-gate * Translate the indexes into the corresponding subarray of matching
504*0Sstevel@tonic-gate * table entries.
505*0Sstevel@tonic-gate */
506*0Sstevel@tonic-gate switch(status) {
507*0Sstevel@tonic-gate case KT_EXACT_MATCH:
508*0Sstevel@tonic-gate case KT_AMBIG_MATCH:
509*0Sstevel@tonic-gate *matches = kt->table + first;
510*0Sstevel@tonic-gate *nmatch = last - first + 1;
511*0Sstevel@tonic-gate break;
512*0Sstevel@tonic-gate default:
513*0Sstevel@tonic-gate *matches = NULL;
514*0Sstevel@tonic-gate *nmatch = 0;
515*0Sstevel@tonic-gate break;
516*0Sstevel@tonic-gate };
517*0Sstevel@tonic-gate return status;
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate
520*0Sstevel@tonic-gate /*.......................................................................
521*0Sstevel@tonic-gate * Convert a keybinding string into a uniq binary representation.
522*0Sstevel@tonic-gate *
523*0Sstevel@tonic-gate * Control characters can be given directly in their binary form,
524*0Sstevel@tonic-gate * expressed as either ^ or C-, followed by the character, expressed in
525*0Sstevel@tonic-gate * octal, like \129 or via C-style backslash escapes, with the addition
526*0Sstevel@tonic-gate * of '\E' to denote the escape key. Similarly, meta characters can be
527*0Sstevel@tonic-gate * given directly in binary or expressed as M- followed by the character.
528*0Sstevel@tonic-gate * Meta characters are recorded as two characters in the binary output
529*0Sstevel@tonic-gate * string, the first being the escape key, and the second being the key
530*0Sstevel@tonic-gate * that was modified by the meta key. This means that binding to
531*0Sstevel@tonic-gate * \EA or ^[A or M-A are all equivalent.
532*0Sstevel@tonic-gate *
533*0Sstevel@tonic-gate * Input:
534*0Sstevel@tonic-gate * keyseq char * The key sequence being added.
535*0Sstevel@tonic-gate * Input/Output:
536*0Sstevel@tonic-gate * binary char * The binary version of the key sequence will be
537*0Sstevel@tonic-gate * assigned to binary[], which must have at least
538*0Sstevel@tonic-gate * as many characters as keyseq[] plus the number
539*0Sstevel@tonic-gate * of embedded binary meta characters.
540*0Sstevel@tonic-gate * nc int * The number of characters assigned to binary[]
541*0Sstevel@tonic-gate * will be recorded in *nc.
542*0Sstevel@tonic-gate * Output:
543*0Sstevel@tonic-gate * return int 0 - OK.
544*0Sstevel@tonic-gate * 1 - Error.
545*0Sstevel@tonic-gate */
_kt_parse_keybinding_string(const char * keyseq,char * binary,int * nc)546*0Sstevel@tonic-gate static int _kt_parse_keybinding_string(const char *keyseq, char *binary,
547*0Sstevel@tonic-gate int *nc)
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate const char *iptr = keyseq; /* Pointer into keyseq[] */
550*0Sstevel@tonic-gate char *optr = binary; /* Pointer into binary[] */
551*0Sstevel@tonic-gate char c; /* An intermediate character */
552*0Sstevel@tonic-gate /*
553*0Sstevel@tonic-gate * Parse the input characters until they are exhausted or the
554*0Sstevel@tonic-gate * output string becomes full.
555*0Sstevel@tonic-gate */
556*0Sstevel@tonic-gate while(*iptr) {
557*0Sstevel@tonic-gate /*
558*0Sstevel@tonic-gate * Check for special characters.
559*0Sstevel@tonic-gate */
560*0Sstevel@tonic-gate switch(*iptr) {
561*0Sstevel@tonic-gate case '^': /* A control character specification */
562*0Sstevel@tonic-gate /*
563*0Sstevel@tonic-gate * Convert the caret expression into the corresponding control
564*0Sstevel@tonic-gate * character unless no character follows the caret, in which case
565*0Sstevel@tonic-gate * record a literal caret.
566*0Sstevel@tonic-gate */
567*0Sstevel@tonic-gate if(iptr[1]) {
568*0Sstevel@tonic-gate /*
569*0Sstevel@tonic-gate * Get the next, possibly escaped, character.
570*0Sstevel@tonic-gate */
571*0Sstevel@tonic-gate if(iptr[1] == '\\') {
572*0Sstevel@tonic-gate c = _kt_backslash_escape(iptr+2, &iptr);
573*0Sstevel@tonic-gate } else {
574*0Sstevel@tonic-gate c = iptr[1];
575*0Sstevel@tonic-gate iptr += 2;
576*0Sstevel@tonic-gate };
577*0Sstevel@tonic-gate /*
578*0Sstevel@tonic-gate * Convert the character to a control character.
579*0Sstevel@tonic-gate */
580*0Sstevel@tonic-gate *optr++ = MAKE_CTRL(c);
581*0Sstevel@tonic-gate } else {
582*0Sstevel@tonic-gate *optr++ = *iptr++;
583*0Sstevel@tonic-gate };
584*0Sstevel@tonic-gate break;
585*0Sstevel@tonic-gate /*
586*0Sstevel@tonic-gate * A backslash-escaped character?
587*0Sstevel@tonic-gate */
588*0Sstevel@tonic-gate case '\\':
589*0Sstevel@tonic-gate /*
590*0Sstevel@tonic-gate * Convert the escape sequence to a binary character.
591*0Sstevel@tonic-gate */
592*0Sstevel@tonic-gate *optr++ = _kt_backslash_escape(iptr+1, &iptr);
593*0Sstevel@tonic-gate break;
594*0Sstevel@tonic-gate /*
595*0Sstevel@tonic-gate * Possibly an emacs-style meta character?
596*0Sstevel@tonic-gate */
597*0Sstevel@tonic-gate case 'M':
598*0Sstevel@tonic-gate if(_kt_is_emacs_meta(iptr)) {
599*0Sstevel@tonic-gate *optr++ = GL_ESC_CHAR;
600*0Sstevel@tonic-gate iptr += 2;
601*0Sstevel@tonic-gate } else {
602*0Sstevel@tonic-gate *optr++ = *iptr++;
603*0Sstevel@tonic-gate };
604*0Sstevel@tonic-gate break;
605*0Sstevel@tonic-gate /*
606*0Sstevel@tonic-gate * Possibly an emacs-style control character specification?
607*0Sstevel@tonic-gate */
608*0Sstevel@tonic-gate case 'C':
609*0Sstevel@tonic-gate if(_kt_is_emacs_ctrl(iptr)) {
610*0Sstevel@tonic-gate *optr++ = MAKE_CTRL(iptr[2]);
611*0Sstevel@tonic-gate iptr += 3;
612*0Sstevel@tonic-gate } else {
613*0Sstevel@tonic-gate *optr++ = *iptr++;
614*0Sstevel@tonic-gate };
615*0Sstevel@tonic-gate break;
616*0Sstevel@tonic-gate default:
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gate /*
619*0Sstevel@tonic-gate * Convert embedded meta characters into an escape character followed
620*0Sstevel@tonic-gate * by the meta-unmodified character.
621*0Sstevel@tonic-gate */
622*0Sstevel@tonic-gate if(IS_META_CHAR(*iptr)) {
623*0Sstevel@tonic-gate *optr++ = GL_ESC_CHAR;
624*0Sstevel@tonic-gate *optr++ = META_TO_CHAR(*iptr);
625*0Sstevel@tonic-gate iptr++;
626*0Sstevel@tonic-gate /*
627*0Sstevel@tonic-gate * To allow keysequences that start with printable characters to
628*0Sstevel@tonic-gate * be distinguished from the cursor-key keywords, prepend a backslash
629*0Sstevel@tonic-gate * to the former. This same operation is performed in gl_interpret_char()
630*0Sstevel@tonic-gate * before looking up a keysequence that starts with a printable character.
631*0Sstevel@tonic-gate */
632*0Sstevel@tonic-gate } else if(iptr==keyseq && !IS_CTRL_CHAR(*iptr) &&
633*0Sstevel@tonic-gate strcmp(keyseq, "up") != 0 && strcmp(keyseq, "down") != 0 &&
634*0Sstevel@tonic-gate strcmp(keyseq, "left") != 0 && strcmp(keyseq, "right") != 0) {
635*0Sstevel@tonic-gate *optr++ = '\\';
636*0Sstevel@tonic-gate *optr++ = *iptr++;
637*0Sstevel@tonic-gate } else {
638*0Sstevel@tonic-gate *optr++ = *iptr++;
639*0Sstevel@tonic-gate };
640*0Sstevel@tonic-gate };
641*0Sstevel@tonic-gate };
642*0Sstevel@tonic-gate /*
643*0Sstevel@tonic-gate * How many characters were placed in the output array?
644*0Sstevel@tonic-gate */
645*0Sstevel@tonic-gate *nc = optr - binary;
646*0Sstevel@tonic-gate return 0;
647*0Sstevel@tonic-gate }
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gate /*.......................................................................
650*0Sstevel@tonic-gate * Add, remove or modify an action.
651*0Sstevel@tonic-gate *
652*0Sstevel@tonic-gate * Input:
653*0Sstevel@tonic-gate * kt KeyTab * The key-binding table.
654*0Sstevel@tonic-gate * action char * The name of the action.
655*0Sstevel@tonic-gate * fn KtKeyFn * The function that implements the action, or NULL
656*0Sstevel@tonic-gate * to remove an existing action.
657*0Sstevel@tonic-gate * data void * A pointer to arbitrary callback data to pass to the
658*0Sstevel@tonic-gate * action function whenever it is called.
659*0Sstevel@tonic-gate * Output:
660*0Sstevel@tonic-gate * return int 0 - OK.
661*0Sstevel@tonic-gate * 1 - Error.
662*0Sstevel@tonic-gate */
_kt_set_action(KeyTab * kt,const char * action,KtKeyFn * fn,void * data)663*0Sstevel@tonic-gate int _kt_set_action(KeyTab *kt, const char *action, KtKeyFn *fn, void *data)
664*0Sstevel@tonic-gate {
665*0Sstevel@tonic-gate Symbol *sym; /* The symbol table entry of the action */
666*0Sstevel@tonic-gate /*
667*0Sstevel@tonic-gate * Check the arguments.
668*0Sstevel@tonic-gate */
669*0Sstevel@tonic-gate if(!kt || !action) {
670*0Sstevel@tonic-gate errno = EINVAL;
671*0Sstevel@tonic-gate if(kt)
672*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
673*0Sstevel@tonic-gate return 1;
674*0Sstevel@tonic-gate };
675*0Sstevel@tonic-gate /*
676*0Sstevel@tonic-gate * If no function was provided, delete an existing action.
677*0Sstevel@tonic-gate */
678*0Sstevel@tonic-gate if(!fn) {
679*0Sstevel@tonic-gate sym = _del_HashSymbol(kt->actions, action);
680*0Sstevel@tonic-gate return 0;
681*0Sstevel@tonic-gate };
682*0Sstevel@tonic-gate /*
683*0Sstevel@tonic-gate * If the action already exists, replace its action function.
684*0Sstevel@tonic-gate */
685*0Sstevel@tonic-gate sym = _find_HashSymbol(kt->actions, action);
686*0Sstevel@tonic-gate if(sym) {
687*0Sstevel@tonic-gate sym->fn = (void (*)(void))fn;
688*0Sstevel@tonic-gate sym->data = data;
689*0Sstevel@tonic-gate return 0;
690*0Sstevel@tonic-gate };
691*0Sstevel@tonic-gate /*
692*0Sstevel@tonic-gate * Add a new action.
693*0Sstevel@tonic-gate */
694*0Sstevel@tonic-gate if(!_new_HashSymbol(kt->actions, action, 0, (void (*)(void))fn, data, 0)) {
695*0Sstevel@tonic-gate _err_record_msg(kt->err, "Insufficient memory to record key-binding action",
696*0Sstevel@tonic-gate END_ERR_MSG);
697*0Sstevel@tonic-gate return 1;
698*0Sstevel@tonic-gate };
699*0Sstevel@tonic-gate return 0;
700*0Sstevel@tonic-gate }
701*0Sstevel@tonic-gate
702*0Sstevel@tonic-gate /*.......................................................................
703*0Sstevel@tonic-gate * Compare two strings of specified length which may contain embedded
704*0Sstevel@tonic-gate * ascii NUL's.
705*0Sstevel@tonic-gate *
706*0Sstevel@tonic-gate * Input:
707*0Sstevel@tonic-gate * s1 char * The first of the strings to be compared.
708*0Sstevel@tonic-gate * n1 int The length of the string in s1.
709*0Sstevel@tonic-gate * s2 char * The second of the strings to be compared.
710*0Sstevel@tonic-gate * n2 int The length of the string in s2.
711*0Sstevel@tonic-gate * Output:
712*0Sstevel@tonic-gate * return int < 0 if(s1 < s2)
713*0Sstevel@tonic-gate * 0 if(s1 == s2)
714*0Sstevel@tonic-gate * > 0 if(s1 > s2)
715*0Sstevel@tonic-gate */
_kt_compare_strings(const char * s1,int n1,const char * s2,int n2)716*0Sstevel@tonic-gate static int _kt_compare_strings(const char *s1, int n1, const char *s2, int n2)
717*0Sstevel@tonic-gate {
718*0Sstevel@tonic-gate int i;
719*0Sstevel@tonic-gate /*
720*0Sstevel@tonic-gate * Find the first character where the two strings differ.
721*0Sstevel@tonic-gate */
722*0Sstevel@tonic-gate for(i=0; i<n1 && i<n2 && s1[i]==s2[i]; i++)
723*0Sstevel@tonic-gate ;
724*0Sstevel@tonic-gate /*
725*0Sstevel@tonic-gate * Did we hit the end of either string before finding a difference?
726*0Sstevel@tonic-gate */
727*0Sstevel@tonic-gate if(i==n1 || i==n2) {
728*0Sstevel@tonic-gate if(n1 == n2)
729*0Sstevel@tonic-gate return 0;
730*0Sstevel@tonic-gate else if(n1==i)
731*0Sstevel@tonic-gate return -1;
732*0Sstevel@tonic-gate else
733*0Sstevel@tonic-gate return 1;
734*0Sstevel@tonic-gate };
735*0Sstevel@tonic-gate /*
736*0Sstevel@tonic-gate * Compare the two characters that differed to determine which
737*0Sstevel@tonic-gate * string is greatest.
738*0Sstevel@tonic-gate */
739*0Sstevel@tonic-gate return s1[i] - s2[i];
740*0Sstevel@tonic-gate }
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gate /*.......................................................................
743*0Sstevel@tonic-gate * Assign a given action function to a binding table entry.
744*0Sstevel@tonic-gate *
745*0Sstevel@tonic-gate * Input:
746*0Sstevel@tonic-gate * sym KeySym * The binding table entry to be modified.
747*0Sstevel@tonic-gate * binder KtBinder The source of the binding.
748*0Sstevel@tonic-gate * keyfn KtKeyFn * The action function.
749*0Sstevel@tonic-gate * data void * A pointer to arbitrary callback data to pass to
750*0Sstevel@tonic-gate * the action function whenever it is called.
751*0Sstevel@tonic-gate */
_kt_assign_action(KeySym * sym,KtBinder binder,KtKeyFn * keyfn,void * data)752*0Sstevel@tonic-gate static void _kt_assign_action(KeySym *sym, KtBinder binder, KtKeyFn *keyfn,
753*0Sstevel@tonic-gate void *data)
754*0Sstevel@tonic-gate {
755*0Sstevel@tonic-gate KtAction *action; /* An action function/data pair */
756*0Sstevel@tonic-gate int i;
757*0Sstevel@tonic-gate /*
758*0Sstevel@tonic-gate * Unknown binding source?
759*0Sstevel@tonic-gate */
760*0Sstevel@tonic-gate if(binder < 0 || binder >= KTB_NBIND)
761*0Sstevel@tonic-gate return;
762*0Sstevel@tonic-gate /*
763*0Sstevel@tonic-gate * Record the action according to its source.
764*0Sstevel@tonic-gate */
765*0Sstevel@tonic-gate action = sym->actions + binder;
766*0Sstevel@tonic-gate action->fn = keyfn;
767*0Sstevel@tonic-gate action->data = data;
768*0Sstevel@tonic-gate /*
769*0Sstevel@tonic-gate * Find the highest priority binding source that has supplied an
770*0Sstevel@tonic-gate * action. Note that the actions[] array is ordered in order of
771*0Sstevel@tonic-gate * descreasing priority, so the first entry that contains a function
772*0Sstevel@tonic-gate * is the one to use.
773*0Sstevel@tonic-gate */
774*0Sstevel@tonic-gate for(i=0; i<KTB_NBIND && !sym->actions[i].fn; i++)
775*0Sstevel@tonic-gate ;
776*0Sstevel@tonic-gate /*
777*0Sstevel@tonic-gate * Record the index of this action for use during lookups.
778*0Sstevel@tonic-gate */
779*0Sstevel@tonic-gate sym->binder = i < KTB_NBIND ? i : -1;
780*0Sstevel@tonic-gate return;
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate
783*0Sstevel@tonic-gate /*.......................................................................
784*0Sstevel@tonic-gate * Remove all key bindings that came from a specified source.
785*0Sstevel@tonic-gate *
786*0Sstevel@tonic-gate * Input:
787*0Sstevel@tonic-gate * kt KeyTab * The table of key bindings.
788*0Sstevel@tonic-gate * binder KtBinder The source of the bindings to be cleared.
789*0Sstevel@tonic-gate */
_kt_clear_bindings(KeyTab * kt,KtBinder binder)790*0Sstevel@tonic-gate void _kt_clear_bindings(KeyTab *kt, KtBinder binder)
791*0Sstevel@tonic-gate {
792*0Sstevel@tonic-gate int oldkey; /* The index of a key in the original binding table */
793*0Sstevel@tonic-gate int newkey; /* The index of a key in the updated binding table */
794*0Sstevel@tonic-gate /*
795*0Sstevel@tonic-gate * If there is no table, then no bindings exist to be deleted.
796*0Sstevel@tonic-gate */
797*0Sstevel@tonic-gate if(!kt)
798*0Sstevel@tonic-gate return;
799*0Sstevel@tonic-gate /*
800*0Sstevel@tonic-gate * Clear bindings of the given source.
801*0Sstevel@tonic-gate */
802*0Sstevel@tonic-gate for(oldkey=0; oldkey<kt->nkey; oldkey++)
803*0Sstevel@tonic-gate _kt_assign_action(kt->table + oldkey, binder, 0, NULL);
804*0Sstevel@tonic-gate /*
805*0Sstevel@tonic-gate * Delete entries that now don't have a binding from any source.
806*0Sstevel@tonic-gate */
807*0Sstevel@tonic-gate newkey = 0;
808*0Sstevel@tonic-gate for(oldkey=0; oldkey<kt->nkey; oldkey++) {
809*0Sstevel@tonic-gate KeySym *sym = kt->table + oldkey;
810*0Sstevel@tonic-gate if(sym->binder < 0) {
811*0Sstevel@tonic-gate _del_StringMemString(kt->smem, sym->keyseq);
812*0Sstevel@tonic-gate } else {
813*0Sstevel@tonic-gate if(oldkey != newkey)
814*0Sstevel@tonic-gate kt->table[newkey] = *sym;
815*0Sstevel@tonic-gate newkey++;
816*0Sstevel@tonic-gate };
817*0Sstevel@tonic-gate };
818*0Sstevel@tonic-gate /*
819*0Sstevel@tonic-gate * Record the number of keys that were kept.
820*0Sstevel@tonic-gate */
821*0Sstevel@tonic-gate kt->nkey = newkey;
822*0Sstevel@tonic-gate return;
823*0Sstevel@tonic-gate }
824*0Sstevel@tonic-gate
825*0Sstevel@tonic-gate /*.......................................................................
826*0Sstevel@tonic-gate * Translate a backslash escape sequence to a binary character.
827*0Sstevel@tonic-gate *
828*0Sstevel@tonic-gate * Input:
829*0Sstevel@tonic-gate * string const char * The characters that follow the backslash.
830*0Sstevel@tonic-gate * Input/Output:
831*0Sstevel@tonic-gate * endp const char ** If endp!=NULL, on return *endp will be made to
832*0Sstevel@tonic-gate * point to the character in string[] which follows
833*0Sstevel@tonic-gate * the escape sequence.
834*0Sstevel@tonic-gate * Output:
835*0Sstevel@tonic-gate * return char The binary character.
836*0Sstevel@tonic-gate */
_kt_backslash_escape(const char * string,const char ** endp)837*0Sstevel@tonic-gate static char _kt_backslash_escape(const char *string, const char **endp)
838*0Sstevel@tonic-gate {
839*0Sstevel@tonic-gate char c; /* The output character */
840*0Sstevel@tonic-gate /*
841*0Sstevel@tonic-gate * Is the backslash followed by one or more octal digits?
842*0Sstevel@tonic-gate */
843*0Sstevel@tonic-gate switch(*string) {
844*0Sstevel@tonic-gate case '0': case '1': case '2': case '3':
845*0Sstevel@tonic-gate case '4': case '5': case '6': case '7':
846*0Sstevel@tonic-gate c = strtol(string, (char **)&string, 8);
847*0Sstevel@tonic-gate break;
848*0Sstevel@tonic-gate case 'a':
849*0Sstevel@tonic-gate c = '\a';
850*0Sstevel@tonic-gate string++;
851*0Sstevel@tonic-gate break;
852*0Sstevel@tonic-gate case 'b':
853*0Sstevel@tonic-gate c = '\b';
854*0Sstevel@tonic-gate string++;
855*0Sstevel@tonic-gate break;
856*0Sstevel@tonic-gate case 'e': case 'E': /* Escape */
857*0Sstevel@tonic-gate c = GL_ESC_CHAR;
858*0Sstevel@tonic-gate string++;
859*0Sstevel@tonic-gate break;
860*0Sstevel@tonic-gate case 'f':
861*0Sstevel@tonic-gate c = '\f';
862*0Sstevel@tonic-gate string++;
863*0Sstevel@tonic-gate break;
864*0Sstevel@tonic-gate case 'n':
865*0Sstevel@tonic-gate c = '\n';
866*0Sstevel@tonic-gate string++;
867*0Sstevel@tonic-gate break;
868*0Sstevel@tonic-gate case 'r':
869*0Sstevel@tonic-gate c = '\r';
870*0Sstevel@tonic-gate string++;
871*0Sstevel@tonic-gate break;
872*0Sstevel@tonic-gate case 't':
873*0Sstevel@tonic-gate c = '\t';
874*0Sstevel@tonic-gate string++;
875*0Sstevel@tonic-gate break;
876*0Sstevel@tonic-gate case 'v':
877*0Sstevel@tonic-gate c = '\v';
878*0Sstevel@tonic-gate string++;
879*0Sstevel@tonic-gate break;
880*0Sstevel@tonic-gate case '\0':
881*0Sstevel@tonic-gate c = '\\';
882*0Sstevel@tonic-gate break;
883*0Sstevel@tonic-gate default:
884*0Sstevel@tonic-gate c = *string++;
885*0Sstevel@tonic-gate break;
886*0Sstevel@tonic-gate };
887*0Sstevel@tonic-gate /*
888*0Sstevel@tonic-gate * Report the character which follows the escape sequence.
889*0Sstevel@tonic-gate */
890*0Sstevel@tonic-gate if(endp)
891*0Sstevel@tonic-gate *endp = string;
892*0Sstevel@tonic-gate return c;
893*0Sstevel@tonic-gate }
894*0Sstevel@tonic-gate
895*0Sstevel@tonic-gate /*.......................................................................
896*0Sstevel@tonic-gate * Return non-zero if the next two characters are M- and a third character
897*0Sstevel@tonic-gate * follows. Otherwise return 0.
898*0Sstevel@tonic-gate *
899*0Sstevel@tonic-gate * Input:
900*0Sstevel@tonic-gate * string const char * The sub-string to scan.
901*0Sstevel@tonic-gate * Output:
902*0Sstevel@tonic-gate * return int 1 - The next two characters are M- and these
903*0Sstevel@tonic-gate * are followed by at least one character.
904*0Sstevel@tonic-gate * 0 - The next two characters aren't M- or no
905*0Sstevel@tonic-gate * character follows a M- pair.
906*0Sstevel@tonic-gate */
_kt_is_emacs_meta(const char * string)907*0Sstevel@tonic-gate static int _kt_is_emacs_meta(const char *string)
908*0Sstevel@tonic-gate {
909*0Sstevel@tonic-gate return *string++ == 'M' && *string++ == '-' && *string;
910*0Sstevel@tonic-gate }
911*0Sstevel@tonic-gate
912*0Sstevel@tonic-gate /*.......................................................................
913*0Sstevel@tonic-gate * Return non-zero if the next two characters are C- and a third character
914*0Sstevel@tonic-gate * follows. Otherwise return 0.
915*0Sstevel@tonic-gate *
916*0Sstevel@tonic-gate * Input:
917*0Sstevel@tonic-gate * string const char * The sub-string to scan.
918*0Sstevel@tonic-gate * Output:
919*0Sstevel@tonic-gate * return int 1 - The next two characters are C- and these
920*0Sstevel@tonic-gate * are followed by at least one character.
921*0Sstevel@tonic-gate * 0 - The next two characters aren't C- or no
922*0Sstevel@tonic-gate * character follows a C- pair.
923*0Sstevel@tonic-gate */
_kt_is_emacs_ctrl(const char * string)924*0Sstevel@tonic-gate static int _kt_is_emacs_ctrl(const char *string)
925*0Sstevel@tonic-gate {
926*0Sstevel@tonic-gate return *string++ == 'C' && *string++ == '-' && *string;
927*0Sstevel@tonic-gate }
928*0Sstevel@tonic-gate
929*0Sstevel@tonic-gate /*.......................................................................
930*0Sstevel@tonic-gate * Merge an array of bindings with existing bindings.
931*0Sstevel@tonic-gate *
932*0Sstevel@tonic-gate * Input:
933*0Sstevel@tonic-gate * kt KeyTab * The table of key bindings.
934*0Sstevel@tonic-gate * binder KtBinder The source of the bindings.
935*0Sstevel@tonic-gate * bindings const KtKeyBinding * The array of bindings.
936*0Sstevel@tonic-gate * n int The number of bindings in bindings[].
937*0Sstevel@tonic-gate * Output:
938*0Sstevel@tonic-gate * return int 0 - OK.
939*0Sstevel@tonic-gate * 1 - Error.
940*0Sstevel@tonic-gate */
_kt_add_bindings(KeyTab * kt,KtBinder binder,const KtKeyBinding * bindings,unsigned n)941*0Sstevel@tonic-gate int _kt_add_bindings(KeyTab *kt, KtBinder binder, const KtKeyBinding *bindings,
942*0Sstevel@tonic-gate unsigned n)
943*0Sstevel@tonic-gate {
944*0Sstevel@tonic-gate int i;
945*0Sstevel@tonic-gate /*
946*0Sstevel@tonic-gate * Check the arguments.
947*0Sstevel@tonic-gate */
948*0Sstevel@tonic-gate if(!kt || !bindings) {
949*0Sstevel@tonic-gate errno = EINVAL;
950*0Sstevel@tonic-gate if(kt)
951*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
952*0Sstevel@tonic-gate return 1;
953*0Sstevel@tonic-gate };
954*0Sstevel@tonic-gate /*
955*0Sstevel@tonic-gate * Install the array of bindings.
956*0Sstevel@tonic-gate */
957*0Sstevel@tonic-gate for(i=0; i<n; i++) {
958*0Sstevel@tonic-gate if(_kt_set_keybinding(kt, binder, bindings[i].keyseq, bindings[i].action))
959*0Sstevel@tonic-gate return 1;
960*0Sstevel@tonic-gate };
961*0Sstevel@tonic-gate return 0;
962*0Sstevel@tonic-gate }
963*0Sstevel@tonic-gate
964*0Sstevel@tonic-gate /*.......................................................................
965*0Sstevel@tonic-gate * Lookup the function that implements a given action.
966*0Sstevel@tonic-gate *
967*0Sstevel@tonic-gate * Input:
968*0Sstevel@tonic-gate * kt KeyTab * The table of key bindings.
969*0Sstevel@tonic-gate * action const char * The name of the action to look up.
970*0Sstevel@tonic-gate * Input/Output:
971*0Sstevel@tonic-gate * fn KtKeyFn ** If the action is found, the function that
972*0Sstevel@tonic-gate * implements it will be assigned to *fn. Note
973*0Sstevel@tonic-gate * that fn can be NULL.
974*0Sstevel@tonic-gate * data void ** If the action is found, the callback data
975*0Sstevel@tonic-gate * associated with the action function, will be
976*0Sstevel@tonic-gate * assigned to *data. Note that data can be NULL.
977*0Sstevel@tonic-gate * Output:
978*0Sstevel@tonic-gate * return int 0 - OK.
979*0Sstevel@tonic-gate * 1 - Action not found.
980*0Sstevel@tonic-gate */
_kt_lookup_action(KeyTab * kt,const char * action,KtKeyFn ** fn,void ** data)981*0Sstevel@tonic-gate int _kt_lookup_action(KeyTab *kt, const char *action,
982*0Sstevel@tonic-gate KtKeyFn **fn, void **data)
983*0Sstevel@tonic-gate {
984*0Sstevel@tonic-gate Symbol *sym; /* The symbol table entry of the action */
985*0Sstevel@tonic-gate /*
986*0Sstevel@tonic-gate * Check the arguments.
987*0Sstevel@tonic-gate */
988*0Sstevel@tonic-gate if(!kt || !action) {
989*0Sstevel@tonic-gate errno = EINVAL;
990*0Sstevel@tonic-gate if(kt)
991*0Sstevel@tonic-gate _err_record_msg(kt->err, "NULL argument(s)", END_ERR_MSG);
992*0Sstevel@tonic-gate return 1;
993*0Sstevel@tonic-gate };
994*0Sstevel@tonic-gate /*
995*0Sstevel@tonic-gate * Lookup the symbol table entry of the action.
996*0Sstevel@tonic-gate */
997*0Sstevel@tonic-gate sym = _find_HashSymbol(kt->actions, action);
998*0Sstevel@tonic-gate if(!sym)
999*0Sstevel@tonic-gate return 1;
1000*0Sstevel@tonic-gate /*
1001*0Sstevel@tonic-gate * Return the function and ccallback data associated with the action.
1002*0Sstevel@tonic-gate */
1003*0Sstevel@tonic-gate if(fn)
1004*0Sstevel@tonic-gate *fn = (KtKeyFn *) sym->fn;
1005*0Sstevel@tonic-gate if(data)
1006*0Sstevel@tonic-gate *data = sym->data;
1007*0Sstevel@tonic-gate return 0;
1008*0Sstevel@tonic-gate }
1009*0Sstevel@tonic-gate
1010*0Sstevel@tonic-gate /*.......................................................................
1011*0Sstevel@tonic-gate * Return extra information (ie. in addition to that provided by errno)
1012*0Sstevel@tonic-gate * about the last error to occur in any of the public functions of this
1013*0Sstevel@tonic-gate * module.
1014*0Sstevel@tonic-gate *
1015*0Sstevel@tonic-gate * Input:
1016*0Sstevel@tonic-gate * kt KeyTab * The table of key bindings.
1017*0Sstevel@tonic-gate * Output:
1018*0Sstevel@tonic-gate * return const char * A pointer to the internal buffer in which
1019*0Sstevel@tonic-gate * the error message is temporarily stored.
1020*0Sstevel@tonic-gate */
_kt_last_error(KeyTab * kt)1021*0Sstevel@tonic-gate const char *_kt_last_error(KeyTab *kt)
1022*0Sstevel@tonic-gate {
1023*0Sstevel@tonic-gate return kt ? _err_get_msg(kt->err) : "NULL KeyTab argument";
1024*0Sstevel@tonic-gate }
1025