-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.cc
2940 lines (2559 loc) · 75.1 KB
/
bind.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* bind.c -- key binding and startup file support for the readline library.
*/
/* Copyright (C) 1987-2017 Free Software Foundation, Inc.
This file is part of the GNU Readline Library (Readline), a library
for reading lines of text with interactive input and history editing.
Readline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Readline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Readline. If not, see <http://www.gnu.org/licenses/>.
*/
#define READLINE_LIBRARY
#if defined(__TANDEM)
#include <floss.h>
#endif
#if defined(HAVE_CONFIG_H)
#include <config.hh>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#if defined(HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif /* HAVE_SYS_FILE_H */
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#else
#include "ansi_stdlib.hh"
#endif /* HAVE_STDLIB_H */
#include <errno.h>
#if !defined(errno)
extern int errno;
#endif /* !errno */
#include "posixstat.hh"
/* System-specific feature definitions and include files. */
#include "rldefs.hh"
/* Some standard library routines. */
#include "history.hh"
#include "readline.hh"
#include "rlprivate.hh"
#include "rlshell.hh"
#include "xmalloc.hh"
#if !defined(strchr) && !defined(__STDC__)
extern char *strchr(), *strrchr();
#endif /* !strchr && !__STDC__ */
/* Variables exported by this file. */
Keymap rl_binding_keymap;
static int _rl_skip_to_delim PARAMS((char*, int, int));
#if defined(USE_VARARGS) && defined(PREFER_STDARG)
static void _rl_init_file_error(const char*, ...)
__attribute__((__format__(printf, 1, 2)));
#else
static void _rl_init_file_error();
#endif
static rl_command_func_t* _rl_function_of_keyseq_internal
PARAMS((const char*, size_t, Keymap, int*));
static char* _rl_read_file PARAMS((char*, size_t*));
static int _rl_read_init_file PARAMS((const char*, int));
static int glean_key_from_name PARAMS((char*));
static int find_boolean_var PARAMS((const char*));
static int find_string_var PARAMS((const char*));
static const char* boolean_varname PARAMS((int));
static const char* string_varname PARAMS((int));
static char* _rl_get_string_variable_value PARAMS((const char*));
static int substring_member_of_array PARAMS((const char*,
const char* const*));
static int _rl_get_keymap_by_name PARAMS((const char*));
static int _rl_get_keymap_by_map PARAMS((Keymap));
static int currently_reading_init_file;
/* used only in this file */
static int _rl_prefer_visible_bell= 1;
#define OP_EQ 1
#define OP_NE 2
#define OP_GT 3
#define OP_GE 4
#define OP_LT 5
#define OP_LE 6
#define OPSTART(c) ((c) == '=' || (c) == '!' || (c) == '<' || (c) == '>')
#define CMPSTART(c) ((c) == '=' || (c) == '!')
/* **************************************************************** */
/* */
/* Binding keys */
/* */
/* **************************************************************** */
/* rl_add_defun (char *name, rl_command_func_t *function, int key)
Add NAME to the list of named functions. Make FUNCTION be the function
that gets called. If KEY is not -1, then bind it. */
int rl_add_defun(const char* name, rl_command_func_t* function, int key)
{
if (key != -1)
rl_bind_key(key, function);
rl_add_funmap_entry(name, function);
return 0;
}
/* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
int rl_bind_key(int key, rl_command_func_t* function)
{
char keyseq[3];
int l;
if (key < 0)
return (key);
if (META_CHAR(key) && _rl_convert_meta_chars_to_ascii)
{
if (_rl_keymap[ESC].type == ISKMAP)
{
Keymap escmap;
escmap = FUNCTION_TO_KEYMAP(_rl_keymap, ESC);
key = UNMETA(key);
escmap[key].type = ISFUNC;
escmap[key].function= function;
return (0);
}
return (key);
}
/* If it's bound to a function or macro, just overwrite. Otherwise we
have to treat it as a key sequence so rl_generic_bind handles shadow
keymaps for us. If we are binding '\' make sure to escape it so it
makes it through the call to rl_translate_keyseq. */
if (_rl_keymap[key].type != ISKMAP)
{
_rl_keymap[key].type = ISFUNC;
_rl_keymap[key].function= function;
}
else
{
l= 0;
if (key == '\\')
keyseq[l++]= '\\';
keyseq[l++]= key;
keyseq[l] = '\0';
rl_bind_keyseq(keyseq, function);
}
rl_binding_keymap= _rl_keymap;
return (0);
}
/* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
KEY. */
int rl_bind_key_in_map(int key, rl_command_func_t* function, Keymap map)
{
int result;
Keymap oldmap;
oldmap = _rl_keymap;
_rl_keymap= map;
result = rl_bind_key(key, function);
_rl_keymap= oldmap;
return (result);
}
/* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
now, this is always used to attempt to bind the arrow keys. */
int rl_bind_key_if_unbound_in_map(int key,
rl_command_func_t* default_func,
Keymap kmap)
{
char* keyseq;
keyseq= rl_untranslate_keyseq((unsigned char)key);
return (rl_bind_keyseq_if_unbound_in_map(keyseq, default_func, kmap));
}
int rl_bind_key_if_unbound(int key, rl_command_func_t* default_func)
{
char* keyseq;
keyseq= rl_untranslate_keyseq((unsigned char)key);
return (rl_bind_keyseq_if_unbound_in_map(
keyseq, default_func, _rl_keymap));
}
/* Make KEY do nothing in the currently selected keymap.
Returns non-zero in case of error. This is not the same as self-insert;
this makes it a dead key. */
int rl_unbind_key(int key)
{
return (rl_bind_key(key, (rl_command_func_t*)NULL));
}
/* Make KEY do nothing in MAP. Returns non-zero in case of error. */
int rl_unbind_key_in_map(int key, Keymap map)
{
return (rl_bind_key_in_map(key, (rl_command_func_t*)NULL, map));
}
/* Unbind all keys bound to FUNCTION in MAP. */
int rl_unbind_function_in_map(rl_command_func_t* func, Keymap map)
{
int i, rval;
for (i= rval= 0; i < KEYMAP_SIZE; i++)
{
if (map[i].type == ISFUNC && map[i].function == func)
{
map[i].function= (rl_command_func_t*)NULL;
rval = 1;
}
}
return rval;
}
/* Unbind all keys bound to COMMAND, which is a bindable command name, in
* MAP */
int rl_unbind_command_in_map(const char* command, Keymap map)
{
rl_command_func_t* func;
func= rl_named_function(command);
if (func == 0)
return 0;
return (rl_unbind_function_in_map(func, map));
}
/* Bind the key sequence represented by the string KEYSEQ to
FUNCTION, starting in the current keymap. This makes new
keymaps as necessary. */
int rl_bind_keyseq(const char* keyseq, rl_command_func_t* function)
{
return (rl_generic_bind(ISFUNC, keyseq, (char*)function, _rl_keymap));
}
/* Bind the key sequence represented by the string KEYSEQ to
FUNCTION. This makes new keymaps as necessary. The initial
place to do bindings is in MAP. */
int rl_bind_keyseq_in_map(const char* keyseq,
rl_command_func_t* function,
Keymap map)
{
return (rl_generic_bind(ISFUNC, keyseq, (char*)function, map));
}
/* Backwards compatibility; equivalent to rl_bind_keyseq_in_map() */
int rl_set_key(const char* keyseq, rl_command_func_t* function, Keymap map)
{
return (rl_generic_bind(ISFUNC, keyseq, (char*)function, map));
}
/* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. Right
now, this is always used to attempt to bind the arrow keys, hence the
check for rl_vi_movement_mode. */
int rl_bind_keyseq_if_unbound_in_map(const char* keyseq,
rl_command_func_t* default_func,
Keymap kmap)
{
rl_command_func_t* func;
char* keys;
int keys_len;
if (keyseq)
{
/* Handle key sequences that require translations and `raw' ones that
don't. This might be a problem with backslashes. */
keys= (char*)xmalloc(1 + (2 * strlen(keyseq)));
if (rl_translate_keyseq(keyseq, keys, &keys_len))
{
xfree(keys);
return -1;
}
func= rl_function_of_keyseq_len(keys, keys_len, kmap, (int*)NULL);
xfree(keys);
#if defined(VI_MODE)
if (!func || func == rl_do_lowercase_version ||
func == rl_vi_movement_mode)
#else
if (!func || func == rl_do_lowercase_version)
#endif
return (rl_bind_keyseq_in_map(keyseq, default_func, kmap));
else
return 1;
}
return 0;
}
int rl_bind_keyseq_if_unbound(const char* keyseq,
rl_command_func_t* default_func)
{
return (rl_bind_keyseq_if_unbound_in_map(
keyseq, default_func, _rl_keymap));
}
/* Bind the key sequence represented by the string KEYSEQ to
the string of characters MACRO. This makes new keymaps as
necessary. The initial place to do bindings is in MAP. */
int rl_macro_bind(const char* keyseq, const char* macro, Keymap map)
{
char* macro_keys;
int macro_keys_len;
macro_keys= (char*)xmalloc((2 * strlen(macro)) + 1);
if (rl_translate_keyseq(macro, macro_keys, ¯o_keys_len))
{
xfree(macro_keys);
return -1;
}
rl_generic_bind(ISMACR, keyseq, macro_keys, map);
return 0;
}
/* Bind the key sequence represented by the string KEYSEQ to
the arbitrary pointer DATA. TYPE says what kind of data is
pointed to by DATA, right now this can be a function (ISFUNC),
a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
as necessary. The initial place to do bindings is in MAP. */
int rl_generic_bind(int type, const char* keyseq, char* data, Keymap map)
{
char* keys;
int keys_len, prevkey;
int i;
KEYMAP_ENTRY k;
Keymap prevmap;
k.function= 0;
/* If no keys to bind to, exit right away. */
if (keyseq == 0 || *keyseq == 0)
{
if (type == ISMACR)
xfree(data);
return -1;
}
keys= (char*)xmalloc(1 + (2 * strlen(keyseq)));
/* Translate the ASCII representation of KEYSEQ into an array of
characters. Stuff the characters into KEYS, and the length of
KEYS into KEYS_LEN. */
if (rl_translate_keyseq(keyseq, keys, &keys_len))
{
xfree(keys);
return -1;
}
prevmap= map;
prevkey= keys[0];
/* Bind keys, making new keymaps as necessary. */
for (i= 0; i < keys_len; i++)
{
unsigned char uc= keys[i];
int ic;
if (i > 0)
prevkey= ic;
ic= uc;
if (ic < 0 || ic >= KEYMAP_SIZE)
{
xfree(keys);
return -1;
}
if (META_CHAR(ic) && _rl_convert_meta_chars_to_ascii)
{
ic= UNMETA(ic);
if (map[ESC].type == ISKMAP)
{
prevmap= map;
map = FUNCTION_TO_KEYMAP(map, ESC);
}
}
if ((i + 1) < keys_len)
{
if (map[ic].type != ISKMAP)
{
/* We allow subsequences of keys. If a keymap is being
created that will `shadow' an existing function or macro
key binding, we save that keybinding into the ANYOTHERKEY
index in the new map. The dispatch code will look there
to find the function to execute if the subsequence is not
matched. ANYOTHERKEY was chosen to be greater than
UCHAR_MAX. */
k= map[ic];
map[ic].type = ISKMAP;
map[ic].function= KEYMAP_TO_FUNCTION(rl_make_bare_keymap());
}
prevmap= map;
map = FUNCTION_TO_KEYMAP(map, ic);
/* The dispatch code will return this function if no matching
key sequence is found in the keymap. This (with a little
help from the dispatch code in readline.c) allows `a' to be
mapped to something, `abc' to be mapped to something else,
and the function bound to `a' to be executed when the user
types `abx', leaving `bx' in the input queue. */
if (k.function &&
((k.type == ISFUNC && k.function != rl_do_lowercase_version) ||
k.type == ISMACR))
{
map[ANYOTHERKEY]= k;
k.function = 0;
}
}
else
{
if (map[ic].type == ISMACR)
xfree((char*)map[ic].function);
else if (map[ic].type == ISKMAP)
{
prevmap= map;
map = FUNCTION_TO_KEYMAP(map, ic);
ic = ANYOTHERKEY;
/* If we're trying to override a keymap with a null function
(e.g., trying to unbind it), we can't use a null pointer
here because that's indistinguishable from having not been
overridden. We use a special bindable function that does
nothing. */
if (type == ISFUNC && data == 0)
data= (char*)_rl_null_function;
}
map[ic].function= KEYMAP_TO_FUNCTION(data);
map[ic].type = type;
}
rl_binding_keymap= map;
}
/* If we unbound a key (type == ISFUNC, data == 0), and the prev keymap
points to the keymap where we unbound the key (sanity check), and the
current binding keymap is empty (rl_empty_keymap() returns non-zero),
and the binding keymap has ANYOTHERKEY set with type == ISFUNC
(overridden function), delete the now-empty keymap, take the
previously- overridden function and remove the override. */
/* Right now, this only works one level back. */
if (type == ISFUNC && data == 0 && prevmap[prevkey].type == ISKMAP &&
(FUNCTION_TO_KEYMAP(prevmap, prevkey) == rl_binding_keymap) &&
rl_binding_keymap[ANYOTHERKEY].type == ISFUNC &&
rl_empty_keymap(rl_binding_keymap))
{
prevmap[prevkey].type = rl_binding_keymap[ANYOTHERKEY].type;
prevmap[prevkey].function= rl_binding_keymap[ANYOTHERKEY].function;
rl_discard_keymap(rl_binding_keymap);
rl_binding_keymap= prevmap;
}
xfree(keys);
return 0;
}
/* Translate the ASCII representation of SEQ, stuffing the values into
ARRAY, an array of characters. LEN gets the final length of ARRAY.
Return non-zero if there was an error parsing SEQ. */
int rl_translate_keyseq(const char* seq, char* array, int* len)
{
int i, c, l, temp;
for (i= l= 0; (c= seq[i]); i++)
{
if (c == '\\')
{
c= seq[++i];
if (c == 0)
{
array[l++]= '\\'; /* preserve trailing backslash */
break;
}
/* Handle \C- and \M- prefixes. */
if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
{
/* Handle special case of backwards define. */
if (strncmp(&seq[i], "C-\\M-", 5) == 0)
{
array[l++]= ESC; /* ESC is meta-prefix */
i+= 5;
array[l++]= CTRL(_rl_to_upper(seq[i]));
}
else if (c == 'M')
{
i++; /* seq[i] == '-' */
/* XXX - obey convert-meta setting */
if (_rl_convert_meta_chars_to_ascii &&
_rl_keymap[ESC].type == ISKMAP)
array[l++]= ESC; /* ESC is meta-prefix */
else if (seq[i + 1] == '\\' && seq[i + 2] == 'C' &&
seq[i + 3] == '-')
{
i+= 4;
temp= (seq[i] == '?') ? RUBOUT : CTRL(_rl_to_upper(seq[i]));
array[l++]= META(temp);
}
else
{
/* This doesn't yet handle things like \M-\a, which may
or may not have any reasonable meaning. You're
probably better off using straight octal or hex. */
i++;
array[l++]= META(seq[i]);
}
}
else if (c == 'C')
{
i+= 2;
/* Special hack for C-?... */
array[l++]=
(seq[i] == '?') ? RUBOUT : CTRL(_rl_to_upper(seq[i]));
}
if (seq[i] == '\0')
break;
continue;
}
/* Translate other backslash-escaped characters. These are the
same escape sequences that bash's `echo' and `printf' builtins
handle, with the addition of \d -> RUBOUT. A backslash
preceding a character that is not special is stripped. */
switch (c)
{
case 'a':
array[l++]= '\007';
break;
case 'b':
array[l++]= '\b';
break;
case 'd':
array[l++]= RUBOUT; /* readline-specific */
break;
case 'e':
array[l++]= ESC;
break;
case 'f':
array[l++]= '\f';
break;
case 'n':
array[l++]= NEWLINE;
break;
case 'r':
array[l++]= RETURN;
break;
case 't':
array[l++]= TAB;
break;
case 'v':
array[l++]= 0x0B;
break;
case '\\':
array[l++]= '\\';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
i++;
for (temp= 2, c-= '0'; ISOCTAL((unsigned char)seq[i]) && temp--;
i++)
c= (c * 8) + OCTVALUE(seq[i]);
i--; /* auto-increment in for loop */
array[l++]= c & largest_char;
break;
case 'x':
i++;
for (temp= 2, c= 0; ISXDIGIT((unsigned char)seq[i]) && temp--;
i++)
c= (c * 16) + HEXVALUE(seq[i]);
if (temp == 2)
c= 'x';
i--; /* auto-increment in for loop */
array[l++]= c & largest_char;
break;
default
: /* backslashes before non-special chars just add the char */
array[l++]= c;
break; /* the backslash is stripped */
}
continue;
}
array[l++]= c;
}
*len = l;
array[l]= '\0';
return (0);
}
static int _rl_isescape(int c)
{
switch (c)
{
case '\007':
case '\b':
case '\f':
case '\n':
case '\r':
case TAB:
case 0x0b:
return (1);
default:
return (0);
}
}
static int _rl_escchar(int c)
{
switch (c)
{
case '\007':
return ('a');
case '\b':
return ('b');
case '\f':
return ('f');
case '\n':
return ('n');
case '\r':
return ('r');
case TAB:
return ('t');
case 0x0b:
return ('v');
default:
return (c);
}
}
char* rl_untranslate_keyseq(int seq)
{
static char kseq[16];
int i, c;
i= 0;
c= seq;
if (META_CHAR(c))
{
kseq[i++]= '\\';
kseq[i++]= 'M';
kseq[i++]= '-';
c = UNMETA(c);
}
else if (c == ESC)
{
kseq[i++]= '\\';
c = 'e';
}
else if (CTRL_CHAR(c))
{
kseq[i++]= '\\';
kseq[i++]= 'C';
kseq[i++]= '-';
c = _rl_to_lower(UNCTRL(c));
}
else if (c == RUBOUT)
{
kseq[i++]= '\\';
kseq[i++]= 'C';
kseq[i++]= '-';
c = '?';
}
if (c == ESC)
{
kseq[i++]= '\\';
c = 'e';
}
else if (c == '\\' || c == '"')
{
kseq[i++]= '\\';
}
kseq[i++]= (unsigned char)c;
kseq[i] = '\0';
return kseq;
}
char* _rl_untranslate_macro_value(char* seq, int use_escapes)
{
char *ret, *r, *s;
int c;
r= ret= (char*)xmalloc(7 * strlen(seq) + 1);
for (s= seq; *s; s++)
{
c= *s;
if (META_CHAR(c))
{
*r++= '\\';
*r++= 'M';
*r++= '-';
c = UNMETA(c);
}
else if (c == ESC)
{
*r++= '\\';
c = 'e';
}
else if (CTRL_CHAR(c))
{
*r++= '\\';
if (use_escapes && _rl_isescape(c))
c= _rl_escchar(c);
else
{
*r++= 'C';
*r++= '-';
c = _rl_to_lower(UNCTRL(c));
}
}
else if (c == RUBOUT)
{
*r++= '\\';
*r++= 'C';
*r++= '-';
c = '?';
}
if (c == ESC)
{
*r++= '\\';
c = 'e';
}
else if (c == '\\' || c == '"')
*r++= '\\';
*r++= (unsigned char)c;
}
*r= '\0';
return ret;
}
/* Return a pointer to the function that STRING represents.
If STRING doesn't have a matching function, then a NULL pointer
is returned. */
rl_command_func_t* rl_named_function(const char* string)
{
int i;
rl_initialize_funmap();
for (i= 0; funmap[i]; i++)
if (_rl_stricmp(funmap[i]->name, string) == 0)
return (funmap[i]->function);
return ((rl_command_func_t*)NULL);
}
/* Return the function (or macro) definition which would be invoked via
KEYSEQ if executed in MAP. If MAP is NULL, then the current keymap is
used. TYPE, if non-NULL, is a pointer to an int which will receive the
type of the object pointed to. One of ISFUNC (function), ISKMAP
(keymap), or ISMACR (macro). */
static rl_command_func_t* _rl_function_of_keyseq_internal(
const char* keyseq,
size_t len,
Keymap map,
int* type)
{
int i;
if (map == 0)
map= _rl_keymap;
for (i= 0; keyseq && i < len; i++)
{
unsigned char ic= keyseq[i];
if (META_CHAR(ic) && _rl_convert_meta_chars_to_ascii)
{
if (map[ESC].type == ISKMAP)
{
map= FUNCTION_TO_KEYMAP(map, ESC);
ic = UNMETA(ic);
}
/* XXX - should we just return NULL here, since this obviously
doesn't match? */
else
{
if (type)
*type= map[ESC].type;
return (map[ESC].function);
}
}
if (map[ic].type == ISKMAP)
{
/* If this is the last key in the key sequence, return the
map. */
if (keyseq[i + 1] == '\0')
{
if (type)
*type= ISKMAP;
return (map[ic].function);
}
else
map= FUNCTION_TO_KEYMAP(map, ic);
}
/* If we're not at the end of the key sequence, and the current key
is bound to something other than a keymap, then the entire key
sequence is not bound. */
else if (map[ic].type != ISKMAP && keyseq[i + 1])
return ((rl_command_func_t*)NULL);
else /* map[ic].type != ISKMAP && keyseq[i+1] == 0 */
{
if (type)
*type= map[ic].type;
return (map[ic].function);
}
}
return ((rl_command_func_t*)NULL);
}
rl_command_func_t* rl_function_of_keyseq(const char* keyseq,
Keymap map,
int* type)
{
return _rl_function_of_keyseq_internal(
keyseq, strlen(keyseq), map, type);
}
rl_command_func_t* rl_function_of_keyseq_len(const char* keyseq,
size_t len,
Keymap map,
int* type)
{
return _rl_function_of_keyseq_internal(keyseq, len, map, type);
}
/* The last key bindings file read. */
static char* last_readline_init_file= (char*)NULL;
/* The file we're currently reading key bindings from. */
static const char* current_readline_init_file;
static int current_readline_init_include_level;
static int current_readline_init_lineno;
/* Read FILENAME into a locally-allocated buffer and return the buffer.
The size of the buffer is returned in *SIZEP. Returns NULL if any
errors were encountered. */
static char* _rl_read_file(char* filename, size_t* sizep)
{
struct stat finfo;
size_t file_size;
char* buffer;
int i, file;
file= -1;
if (((file= open(filename, O_RDONLY, 0666)) < 0) ||
(fstat(file, &finfo) < 0))
{
if (file >= 0)
close(file);
return ((char*)NULL);
}
file_size= (size_t)finfo.st_size;
/* check for overflow on very large files */
if (file_size != finfo.st_size || file_size + 1 < file_size)
{
if (file >= 0)
close(file);
#if defined(EFBIG)
errno= EFBIG;
#endif
return ((char*)NULL);
}
/* Read the file into BUFFER. */
buffer= (char*)xmalloc(file_size + 1);
i = read(file, buffer, file_size);
close(file);
if (i < 0)
{
xfree(buffer);
return ((char*)NULL);
}
RL_CHECK_SIGNALS();
buffer[i]= '\0';
if (sizep)
*sizep= i;
return (buffer);
}
/* Re-read the current keybindings file. */
int rl_re_read_init_file(int count, int ignore)
{
int r;
r= rl_read_init_file((const char*)NULL);
rl_set_keymap_from_edit_mode();
return r;
}
/* Do key bindings from a file. If FILENAME is NULL it defaults
to the first non-null filename from this list:
1. the filename used for the previous call
2. the value of the shell variable `INPUTRC'
3. ~/.inputrc
4. /etc/inputrc
If the file existed and could be opened and read, 0 is returned,
otherwise errno is returned. */
int rl_read_init_file(const char* filename)
{
/* Default the filename. */
if (filename == 0)
filename= last_readline_init_file;
if (filename == 0)
filename= sh_get_env_value("INPUTRC");
if (filename == 0 || *filename == 0)
{
filename= DEFAULT_INPUTRC;
/* Try to read DEFAULT_INPUTRC; fall back to SYS_INPUTRC on failure */
if (_rl_read_init_file(filename, 0) == 0)
return 0;
filename= SYS_INPUTRC;
}
#if defined(__MSDOS__)
if (_rl_read_init_file(filename, 0) == 0)
return 0;
filename= "~/_inputrc";
#endif
return (_rl_read_init_file(filename, 0));
}
static int _rl_read_init_file(const char* filename, int include_level)
{
int i;
char * buffer, *openname, *line, *end;
size_t file_size;
current_readline_init_file = filename;
current_readline_init_include_level= include_level;
openname= tilde_expand(filename);
buffer = _rl_read_file(openname, &file_size);
xfree(openname);
RL_CHECK_SIGNALS();
if (buffer == 0)
return (errno);
if (include_level == 0 && filename != last_readline_init_file)
{
free(last_readline_init_file);
last_readline_init_file= savestring(filename);
}
currently_reading_init_file= 1;
/* Loop over the lines in the file. Lines that start with `#' are
comments; all other lines are commands for readline initialization. */
current_readline_init_lineno= 1;