OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [readline/] [bind.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 106 markom
/* bind.c -- key binding and startup file support for the readline library. */
2
 
3
/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4
 
5
   This file is part of the GNU Readline Library, a library for
6
   reading lines of text with interactive input and history editing.
7
 
8
   The GNU Readline Library is free software; you can redistribute it
9
   and/or modify it under the terms of the GNU General Public License
10
   as published by the Free Software Foundation; either version 1, or
11
   (at your option) any later version.
12
 
13
   The GNU Readline Library is distributed in the hope that it will be
14
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15
   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   The GNU General Public License is often shipped with GNU software, and
19
   is generally kept in a file called COPYING or LICENSE.  If you do not
20
   have a copy of the license, write to the Free Software Foundation,
21
   675 Mass Ave, Cambridge, MA 02139, USA. */
22
#define READLINE_LIBRARY
23
 
24
#if defined (HAVE_CONFIG_H)
25
#  include <config.h>
26
#endif
27
 
28
#include <stdio.h>
29
#include <sys/types.h>
30
#include <fcntl.h>
31
#if defined (HAVE_SYS_FILE_H)
32
#  include <sys/file.h>
33
#endif /* HAVE_SYS_FILE_H */
34
 
35
#if defined (HAVE_UNISTD_H)
36
#  include <unistd.h>
37
#endif /* HAVE_UNISTD_H */
38
 
39
#if defined (HAVE_STDLIB_H)
40
#  include <stdlib.h>
41
#else
42
#  include "ansi_stdlib.h"
43
#endif /* HAVE_STDLIB_H */
44
 
45
#include <signal.h>
46
#include <errno.h>
47
 
48
#if !defined (errno)
49
extern int errno;
50
#endif /* !errno */
51
 
52
#include "posixstat.h"
53
 
54
/* System-specific feature definitions and include files. */
55
#include "rldefs.h"
56
 
57
/* Some standard library routines. */
58
#include "readline.h"
59
#include "history.h"
60
 
61
#if !defined (strchr) && !defined (__STDC__)
62
extern char *strchr (), *strrchr ();
63
#endif /* !strchr && !__STDC__ */
64
 
65
#ifndef O_BINARY
66
# define O_BINARY 0
67
#endif
68
 
69
extern int _rl_horizontal_scroll_mode;
70
extern int _rl_mark_modified_lines;
71
extern int _rl_bell_preference;
72
extern int _rl_meta_flag;
73
extern int _rl_convert_meta_chars_to_ascii;
74
extern int _rl_output_meta_chars;
75
extern int _rl_complete_show_all;
76
extern int _rl_complete_mark_directories;
77
extern int _rl_print_completions_horizontally;
78
extern int _rl_completion_case_fold;
79
extern int _rl_enable_keypad;
80
#if defined (PAREN_MATCHING)
81
extern int rl_blink_matching_paren;
82
#endif /* PAREN_MATCHING */
83
#if defined (VISIBLE_STATS)
84
extern int rl_visible_stats;
85
#endif /* VISIBLE_STATS */
86
extern int rl_complete_with_tilde_expansion;
87
extern int rl_completion_query_items;
88
extern int rl_inhibit_completion;
89
extern char *_rl_comment_begin;
90
extern unsigned char *_rl_isearch_terminators;
91
 
92
extern int rl_explicit_arg;
93
extern int rl_editing_mode;
94
extern unsigned char _rl_parsing_conditionalized_out;
95
extern Keymap _rl_keymap;
96
 
97
extern char *possible_control_prefixes[], *possible_meta_prefixes[];
98
 
99
/* Functions imported from funmap.c */
100
extern char **rl_funmap_names ();
101
extern int rl_add_funmap_entry ();
102
 
103
/* Functions imported from util.c */
104
extern char *_rl_strindex ();
105
 
106
/* Functions imported from shell.c */
107
extern char *get_env_value ();
108
 
109
/* Variables exported by this file. */
110
Keymap rl_binding_keymap;
111
 
112
/* Forward declarations */
113
void rl_set_keymap_from_edit_mode ();
114
 
115
static int _rl_read_init_file ();
116
static int glean_key_from_name ();
117
static int substring_member_of_array ();
118
 
119
extern char *xmalloc (), *xrealloc ();
120
 
121
/* **************************************************************** */
122
/*                                                                  */
123
/*                      Binding keys                                */
124
/*                                                                  */
125
/* **************************************************************** */
126
 
127
/* rl_add_defun (char *name, Function *function, int key)
128
   Add NAME to the list of named functions.  Make FUNCTION be the function
129
   that gets called.  If KEY is not -1, then bind it. */
130
int
131
rl_add_defun (name, function, key)
132
     char *name;
133
     Function *function;
134
     int key;
135
{
136
  if (key != -1)
137
    rl_bind_key (key, function);
138
  rl_add_funmap_entry (name, function);
139
  return 0;
140
}
141
 
142
/* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
143
int
144
rl_bind_key (key, function)
145
     int key;
146
     Function *function;
147
{
148
  if (key < 0)
149
    return (key);
150
 
151
  if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
152
    {
153
      if (_rl_keymap[ESC].type == ISKMAP)
154
        {
155
          Keymap escmap;
156
 
157
          escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
158
          key = UNMETA (key);
159
          escmap[key].type = ISFUNC;
160
          escmap[key].function = function;
161
          return (0);
162
        }
163
      return (key);
164
    }
165
 
166
  _rl_keymap[key].type = ISFUNC;
167
  _rl_keymap[key].function = function;
168
  rl_binding_keymap = _rl_keymap;
169
  return (0);
170
}
171
 
172
/* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
173
   KEY. */
174
int
175
rl_bind_key_in_map (key, function, map)
176
     int key;
177
     Function *function;
178
     Keymap map;
179
{
180
  int result;
181
  Keymap oldmap;
182
 
183
  oldmap = _rl_keymap;
184
  _rl_keymap = map;
185
  result = rl_bind_key (key, function);
186
  _rl_keymap = oldmap;
187
  return (result);
188
}
189
 
190
/* Make KEY do nothing in the currently selected keymap.
191
   Returns non-zero in case of error. */
192
int
193
rl_unbind_key (key)
194
     int key;
195
{
196
  return (rl_bind_key (key, (Function *)NULL));
197
}
198
 
199
/* Make KEY do nothing in MAP.
200
   Returns non-zero in case of error. */
201
int
202
rl_unbind_key_in_map (key, map)
203
     int key;
204
     Keymap map;
205
{
206
  return (rl_bind_key_in_map (key, (Function *)NULL, map));
207
}
208
 
209
/* Unbind all keys bound to FUNCTION in MAP. */
210
int
211
rl_unbind_function_in_map (func, map)
212
     Function *func;
213
     Keymap map;
214
{
215
  register int i, rval;
216
 
217
  for (i = rval = 0; i < KEYMAP_SIZE; i++)
218
    {
219
      if (map[i].type == ISFUNC && map[i].function == func)
220
        {
221
          map[i].function = (Function *)NULL;
222
          rval = 1;
223
        }
224
    }
225
  return rval;
226
}
227
 
228
int
229
rl_unbind_command_in_map (command, map)
230
     char *command;
231
     Keymap map;
232
{
233
  Function *func;
234
 
235
  func = rl_named_function (command);
236
  if (func == 0)
237
    return 0;
238
  return (rl_unbind_function_in_map (func, map));
239
}
240
 
241
/* Bind the key sequence represented by the string KEYSEQ to
242
   FUNCTION.  This makes new keymaps as necessary.  The initial
243
   place to do bindings is in MAP. */
244
int
245
rl_set_key (keyseq, function, map)
246
     char *keyseq;
247
     Function *function;
248
     Keymap map;
249
{
250
  return (rl_generic_bind (ISFUNC, keyseq, (char *)function, map));
251
}
252
 
253
/* Bind the key sequence represented by the string KEYSEQ to
254
   the string of characters MACRO.  This makes new keymaps as
255
   necessary.  The initial place to do bindings is in MAP. */
256
int
257
rl_macro_bind (keyseq, macro, map)
258
     char *keyseq, *macro;
259
     Keymap map;
260
{
261
  char *macro_keys;
262
  int macro_keys_len;
263
 
264
  macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
265
 
266
  if (rl_translate_keyseq (macro, macro_keys, &macro_keys_len))
267
    {
268
      free (macro_keys);
269
      return -1;
270
    }
271
  rl_generic_bind (ISMACR, keyseq, macro_keys, map);
272
  return 0;
273
}
274
 
275
/* Bind the key sequence represented by the string KEYSEQ to
276
   the arbitrary pointer DATA.  TYPE says what kind of data is
277
   pointed to by DATA, right now this can be a function (ISFUNC),
278
   a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
279
   as necessary.  The initial place to do bindings is in MAP. */
280
int
281
rl_generic_bind (type, keyseq, data, map)
282
     int type;
283
     char *keyseq, *data;
284
     Keymap map;
285
{
286
  char *keys;
287
  int keys_len;
288
  register int i;
289
 
290
  /* If no keys to bind to, exit right away. */
291
  if (!keyseq || !*keyseq)
292
    {
293
      if (type == ISMACR)
294
        free (data);
295
      return -1;
296
    }
297
 
298
  keys = xmalloc (1 + (2 * strlen (keyseq)));
299
 
300
  /* Translate the ASCII representation of KEYSEQ into an array of
301
     characters.  Stuff the characters into KEYS, and the length of
302
     KEYS into KEYS_LEN. */
303
  if (rl_translate_keyseq (keyseq, keys, &keys_len))
304
    {
305
      free (keys);
306
      return -1;
307
    }
308
 
309
  /* Bind keys, making new keymaps as necessary. */
310
  for (i = 0; i < keys_len; i++)
311
    {
312
      int ic = (int) ((unsigned char)keys[i]);
313
 
314
      if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
315
        {
316
          ic = UNMETA (ic);
317
          if (map[ESC].type == ISKMAP)
318
            map = FUNCTION_TO_KEYMAP (map, ESC);
319
        }
320
 
321
      if ((i + 1) < keys_len)
322
        {
323
          if (map[ic].type != ISKMAP)
324
            {
325
              if (map[ic].type == ISMACR)
326
                free ((char *)map[ic].function);
327
 
328
              map[ic].type = ISKMAP;
329
              map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
330
            }
331
          map = FUNCTION_TO_KEYMAP (map, ic);
332
        }
333
      else
334
        {
335
          if (map[ic].type == ISMACR)
336
            free ((char *)map[ic].function);
337
 
338
          map[ic].function = KEYMAP_TO_FUNCTION (data);
339
          map[ic].type = type;
340
        }
341
 
342
      rl_binding_keymap = map;
343
    }
344
  free (keys);
345
  return 0;
346
}
347
 
348
/* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
349
   an array of characters.  LEN gets the final length of ARRAY.  Return
350
   non-zero if there was an error parsing SEQ. */
351
int
352
rl_translate_keyseq (seq, array, len)
353
     char *seq, *array;
354
     int *len;
355
{
356
  register int i, c, l, temp;
357
 
358
  for (i = l = 0; c = seq[i]; i++)
359
    {
360
      if (c == '\\')
361
        {
362
          c = seq[++i];
363
 
364
          if (c == 0)
365
            break;
366
 
367
          /* Handle \C- and \M- prefixes. */
368
          if ((c == 'C' || c == 'M') && seq[i + 1] == '-')
369
            {
370
              /* Handle special case of backwards define. */
371
              if (strncmp (&seq[i], "C-\\M-", 5) == 0)
372
                {
373
                  array[l++] = ESC;
374
                  i += 5;
375
                  array[l++] = CTRL (_rl_to_upper (seq[i]));
376
                  if (seq[i] == '\0')
377
                    i--;
378
                }
379
              else if (c == 'M')
380
                {
381
                  i++;
382
                  array[l++] = ESC;     /* XXX */
383
                }
384
              else if (c == 'C')
385
                {
386
                  i += 2;
387
                  /* Special hack for C-?... */
388
                  array[l++] = (seq[i] == '?') ? RUBOUT : CTRL (_rl_to_upper (seq[i]));
389
                }
390
              continue;
391
            }
392
 
393
          /* Translate other backslash-escaped characters.  These are the
394
             same escape sequences that bash's `echo' and `printf' builtins
395
             handle, with the addition of \d -> RUBOUT.  A backslash
396
             preceding a character that is not special is stripped. */
397
          switch (c)
398
            {
399
            case 'a':
400
              array[l++] = '\007';
401
              break;
402
            case 'b':
403
              array[l++] = '\b';
404
              break;
405
            case 'd':
406
              array[l++] = RUBOUT;      /* readline-specific */
407
              break;
408
            case 'e':
409
              array[l++] = ESC;
410
              break;
411
            case 'f':
412
              array[l++] = '\f';
413
              break;
414
            case 'n':
415
              array[l++] = NEWLINE;
416
              break;
417
            case 'r':
418
              array[l++] = RETURN;
419
              break;
420
            case 't':
421
              array[l++] = TAB;
422
              break;
423
            case 'v':
424
              array[l++] = 0x0B;
425
              break;
426
            case '\\':
427
              array[l++] = '\\';
428
              break;
429
            case '0': case '1': case '2': case '3':
430
            case '4': case '5': case '6': case '7':
431
              i++;
432
              for (temp = 2, c -= '0'; ISOCTAL (seq[i]) && temp--; i++)
433
                c = (c * 8) + OCTVALUE (seq[i]);
434
              i--;      /* auto-increment in for loop */
435
              array[l++] = c % (largest_char + 1);
436
              break;
437
            case 'x':
438
              i++;
439
              for (temp = 3, c = 0; isxdigit (seq[i]) && temp--; i++)
440
                c = (c * 16) + HEXVALUE (seq[i]);
441
              if (temp == 3)
442
                c = 'x';
443
              i--;      /* auto-increment in for loop */
444
              array[l++] = c % (largest_char + 1);
445
              break;
446
            default:    /* backslashes before non-special chars just add the char */
447
              array[l++] = c;
448
              break;    /* the backslash is stripped */
449
            }
450
          continue;
451
        }
452
 
453
      array[l++] = c;
454
    }
455
 
456
  *len = l;
457
  array[l] = '\0';
458
  return (0);
459
}
460
 
461
char *
462
rl_untranslate_keyseq (seq)
463
     int seq;
464
{
465
  static char kseq[16];
466
  int i, c;
467
 
468
  i = 0;
469
  c = seq;
470
  if (META_CHAR (c))
471
    {
472
      kseq[i++] = '\\';
473
      kseq[i++] = 'M';
474
      kseq[i++] = '-';
475
      c = UNMETA (c);
476
    }
477
  else if (CTRL_CHAR (c))
478
    {
479
      kseq[i++] = '\\';
480
      kseq[i++] = 'C';
481
      kseq[i++] = '-';
482
      c = _rl_to_lower (UNCTRL (c));
483
    }
484
  else if (c == RUBOUT)
485
    {
486
      kseq[i++] = '\\';
487
      kseq[i++] = 'C';
488
      kseq[i++] = '-';
489
      c = '?';
490
    }
491
 
492
  if (c == ESC)
493
    {
494
      kseq[i++] = '\\';
495
      c = 'e';
496
    }
497
  else if (c == '\\' || c == '"')
498
    {
499
      kseq[i++] = '\\';
500
    }
501
 
502
  kseq[i++] = (unsigned char) c;
503
  kseq[i] = '\0';
504
  return kseq;
505
}
506
 
507
static char *
508
_rl_untranslate_macro_value (seq)
509
     char *seq;
510
{
511
  char *ret, *r, *s;
512
  int c;
513
 
514
  r = ret = xmalloc (7 * strlen (seq) + 1);
515
  for (s = seq; *s; s++)
516
    {
517
      c = *s;
518
      if (META_CHAR (c))
519
        {
520
          *r++ = '\\';
521
          *r++ = 'M';
522
          *r++ = '-';
523
          c = UNMETA (c);
524
        }
525
      else if (CTRL_CHAR (c) && c != ESC)
526
        {
527
          *r++ = '\\';
528
          *r++ = 'C';
529
          *r++ = '-';
530
          c = _rl_to_lower (UNCTRL (c));
531
        }
532
      else if (c == RUBOUT)
533
        {
534
          *r++ = '\\';
535
          *r++ = 'C';
536
          *r++ = '-';
537
          c = '?';
538
        }
539
 
540
      if (c == ESC)
541
        {
542
          *r++ = '\\';
543
          c = 'e';
544
        }
545
      else if (c == '\\' || c == '"')
546
        *r++ = '\\';
547
 
548
      *r++ = (unsigned char)c;
549
    }
550
  *r = '\0';
551
  return ret;
552
}
553
 
554
/* Return a pointer to the function that STRING represents.
555
   If STRING doesn't have a matching function, then a NULL pointer
556
   is returned. */
557
Function *
558
rl_named_function (string)
559
     char *string;
560
{
561
  register int i;
562
 
563
  rl_initialize_funmap ();
564
 
565
  for (i = 0; funmap[i]; i++)
566
    if (_rl_stricmp (funmap[i]->name, string) == 0)
567
      return (funmap[i]->function);
568
  return ((Function *)NULL);
569
}
570
 
571
/* Return the function (or macro) definition which would be invoked via
572
   KEYSEQ if executed in MAP.  If MAP is NULL, then the current keymap is
573
   used.  TYPE, if non-NULL, is a pointer to an int which will receive the
574
   type of the object pointed to.  One of ISFUNC (function), ISKMAP (keymap),
575
   or ISMACR (macro). */
576
Function *
577
rl_function_of_keyseq (keyseq, map, type)
578
     char *keyseq;
579
     Keymap map;
580
     int *type;
581
{
582
  register int i;
583
 
584
  if (!map)
585
    map = _rl_keymap;
586
 
587
  for (i = 0; keyseq && keyseq[i]; i++)
588
    {
589
      int ic = keyseq[i];
590
 
591
      if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
592
        {
593
          if (map[ESC].type != ISKMAP)
594
            {
595
              if (type)
596
                *type = map[ESC].type;
597
 
598
              return (map[ESC].function);
599
            }
600
          else
601
            {
602
              map = FUNCTION_TO_KEYMAP (map, ESC);
603
              ic = UNMETA (ic);
604
            }
605
        }
606
 
607
      if (map[ic].type == ISKMAP)
608
        {
609
          /* If this is the last key in the key sequence, return the
610
             map. */
611
          if (!keyseq[i + 1])
612
            {
613
              if (type)
614
                *type = ISKMAP;
615
 
616
              return (map[ic].function);
617
            }
618
          else
619
            map = FUNCTION_TO_KEYMAP (map, ic);
620
        }
621
      else
622
        {
623
          if (type)
624
            *type = map[ic].type;
625
 
626
          return (map[ic].function);
627
        }
628
    }
629
  return ((Function *) NULL);
630
}
631
 
632
/* The last key bindings file read. */
633
static char *last_readline_init_file = (char *)NULL;
634
 
635
/* The file we're currently reading key bindings from. */
636
static char *current_readline_init_file;
637
static int current_readline_init_include_level;
638
static int current_readline_init_lineno;
639
 
640
/* Read FILENAME into a locally-allocated buffer and return the buffer.
641
   The size of the buffer is returned in *SIZEP.  Returns NULL if any
642
   errors were encountered. */
643
static char *
644
_rl_read_file (filename, sizep)
645
     char *filename;
646
     size_t *sizep;
647
{
648
  struct stat finfo;
649
  size_t file_size;
650
  char *buffer;
651
  int i, file;
652
 
653
  if ((stat (filename, &finfo) < 0) || (file = open (filename, O_RDONLY | O_BINARY, 0666)) < 0)
654
    return ((char *)NULL);
655
 
656
  file_size = (size_t)finfo.st_size;
657
 
658
  /* check for overflow on very large files */
659
  if (file_size != finfo.st_size || file_size + 1 < file_size)
660
    {
661
      if (file >= 0)
662
        close (file);
663
#if defined (EFBIG)
664
      errno = EFBIG;
665
#endif
666
      return ((char *)NULL);
667
    }
668
 
669
  /* Read the file into BUFFER. */
670
  buffer = (char *)xmalloc (file_size + 1);
671
  i = read (file, buffer, file_size);
672
  close (file);
673
 
674
#if 1
675
  if (i < file_size)
676
#else
677
  if (i < 0)
678
#endif
679
    {
680
      free (buffer);
681
      return ((char *)NULL);
682
    }
683
 
684
  buffer[file_size] = '\0';
685
 
686
#if O_BINARY
687
  {
688
    /* Systems which distinguish between text and binary files need
689
       to strip the CR characters before each Newline, otherwise the
690
       parsing functions won't work.  */
691
    char *s, *d;
692
    size_t removed = 0;
693
 
694
    for (s = buffer, d = buffer; s < buffer + file_size; s++)
695
      {
696
        if (removed)
697
          *d = *s;
698
        if (*s != '\r' || s[1] != '\n')
699
          d++;
700
        else
701
          removed++;
702
      }
703
 
704
    file_size -= removed;
705
    buffer[file_size] = '\0';
706
  }
707
#endif
708
 
709
  if (sizep)
710
    *sizep = file_size;
711
  return (buffer);
712
}
713
 
714
/* Re-read the current keybindings file. */
715
int
716
rl_re_read_init_file (count, ignore)
717
     int count, ignore;
718
{
719
  int r;
720
  r = rl_read_init_file ((char *)NULL);
721
  rl_set_keymap_from_edit_mode ();
722
  return r;
723
}
724
 
725
/* Do key bindings from a file.  If FILENAME is NULL it defaults
726
   to the first non-null filename from this list:
727
     1. the filename used for the previous call
728
     2. the value of the shell variable `INPUTRC'
729
     3. ~/.inputrc
730
     4. (for __MSDOS__ only) ~/_inputrc
731
   If the file existed and could be opened and read, 0 is returned,
732
   otherwise errno is returned. */
733
int
734
rl_read_init_file (filename)
735
     char *filename;
736
{
737
  /* Default the filename. */
738
  if (filename == 0)
739
    {
740
      filename = last_readline_init_file;
741
      if (filename == 0)
742
        filename = get_env_value ("INPUTRC");
743
      if (filename == 0)
744
        filename = DEFAULT_INPUTRC;
745
    }
746
 
747
  if (*filename == 0)
748
    filename = DEFAULT_INPUTRC;
749
 
750
#ifdef __MSDOS__
751
  {
752
    /* DOS doesn't allow leading dots in file names.  If the original
753
       name fails (it could work if we are on Windows), fall back to
754
       ~/_inputrc.  */
755
    int retval = _rl_read_init_file (filename, 0);
756
 
757
    if (retval == 0)
758
      return retval;
759
    else if (strcmp (filename, "~/.inputrc") == 0)
760
      filename = "~/_inputrc";
761
  }
762
#endif
763
 
764
  return (_rl_read_init_file (filename, 0));
765
}
766
 
767
static int
768
_rl_read_init_file (filename, include_level)
769
     char *filename;
770
     int include_level;
771
{
772
  register int i;
773
  char *buffer, *openname, *line, *end;
774
  size_t file_size;
775
 
776
  current_readline_init_file = filename;
777
  current_readline_init_include_level = include_level;
778
 
779
  openname = tilde_expand (filename);
780
  buffer = _rl_read_file (openname, &file_size);
781
  free (openname);
782
 
783
  if (buffer == 0)
784
    return (errno);
785
 
786
  if (include_level == 0 && filename != last_readline_init_file)
787
    {
788
      FREE (last_readline_init_file);
789
      last_readline_init_file = savestring (filename);
790
    }
791
 
792
  /* Loop over the lines in the file.  Lines that start with `#' are
793
     comments; all other lines are commands for readline initialization. */
794
  current_readline_init_lineno = 1;
795
  line = buffer;
796
  end = buffer + file_size;
797
  while (line < end)
798
    {
799
      /* Find the end of this line. */
800
      for (i = 0; line + i != end && line[i] != '\n'; i++);
801
 
802
      /* Mark end of line. */
803
      line[i] = '\0';
804
 
805
      /* Skip leading whitespace. */
806
      while (*line && whitespace (*line))
807
        {
808
          line++;
809
          i--;
810
        }
811
 
812
      /* If the line is not a comment, then parse it. */
813
      if (*line && *line != '#')
814
        rl_parse_and_bind (line);
815
 
816
      /* Move to the next line. */
817
      line += i + 1;
818
      current_readline_init_lineno++;
819
    }
820
 
821
  free (buffer);
822
  return (0);
823
}
824
 
825
static void
826
_rl_init_file_error (msg)
827
     char *msg;
828
{
829
  fprintf (stderr, "readline: %s: line %d: %s\n", current_readline_init_file,
830
                   current_readline_init_lineno,
831
                   msg);
832
}
833
 
834
/* **************************************************************** */
835
/*                                                                  */
836
/*                      Parser Directives                           */
837
/*                                                                  */
838
/* **************************************************************** */
839
 
840
/* Conditionals. */
841
 
842
/* Calling programs set this to have their argv[0]. */
843
char *rl_readline_name = "other";
844
 
845
/* Stack of previous values of parsing_conditionalized_out. */
846
static unsigned char *if_stack = (unsigned char *)NULL;
847
static int if_stack_depth;
848
static int if_stack_size;
849
 
850
/* Push _rl_parsing_conditionalized_out, and set parser state based
851
   on ARGS. */
852
static int
853
parser_if (args)
854
     char *args;
855
{
856
  register int i;
857
 
858
  /* Push parser state. */
859
  if (if_stack_depth + 1 >= if_stack_size)
860
    {
861
      if (!if_stack)
862
        if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
863
      else
864
        if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
865
    }
866
  if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
867
 
868
  /* If parsing is turned off, then nothing can turn it back on except
869
     for finding the matching endif.  In that case, return right now. */
870
  if (_rl_parsing_conditionalized_out)
871
    return 0;
872
 
873
  /* Isolate first argument. */
874
  for (i = 0; args[i] && !whitespace (args[i]); i++);
875
 
876
  if (args[i])
877
    args[i++] = '\0';
878
 
879
  /* Handle "$if term=foo" and "$if mode=emacs" constructs.  If this
880
     isn't term=foo, or mode=emacs, then check to see if the first
881
     word in ARGS is the same as the value stored in rl_readline_name. */
882
  if (rl_terminal_name && _rl_strnicmp (args, "term=", 5) == 0)
883
    {
884
      char *tem, *tname;
885
 
886
      /* Terminals like "aaa-60" are equivalent to "aaa". */
887
      tname = savestring (rl_terminal_name);
888
      tem = strchr (tname, '-');
889
      if (tem)
890
        *tem = '\0';
891
 
892
      /* Test the `long' and `short' forms of the terminal name so that
893
         if someone has a `sun-cmd' and does not want to have bindings
894
         that will be executed if the terminal is a `sun', they can put
895
         `$if term=sun-cmd' into their .inputrc. */
896
      _rl_parsing_conditionalized_out = _rl_stricmp (args + 5, tname) &&
897
                                        _rl_stricmp (args + 5, rl_terminal_name);
898
      free (tname);
899
    }
900
#if defined (VI_MODE)
901
  else if (_rl_strnicmp (args, "mode=", 5) == 0)
902
    {
903
      int mode;
904
 
905
      if (_rl_stricmp (args + 5, "emacs") == 0)
906
        mode = emacs_mode;
907
      else if (_rl_stricmp (args + 5, "vi") == 0)
908
        mode = vi_mode;
909
      else
910
        mode = no_mode;
911
 
912
      _rl_parsing_conditionalized_out = mode != rl_editing_mode;
913
    }
914
#endif /* VI_MODE */
915
  /* Check to see if the first word in ARGS is the same as the
916
     value stored in rl_readline_name. */
917
  else if (_rl_stricmp (args, rl_readline_name) == 0)
918
    _rl_parsing_conditionalized_out = 0;
919
  else
920
    _rl_parsing_conditionalized_out = 1;
921
  return 0;
922
}
923
 
924
/* Invert the current parser state if there is anything on the stack. */
925
static int
926
parser_else (args)
927
     char *args;
928
{
929
  register int i;
930
 
931
  if (if_stack_depth == 0)
932
    {
933
      _rl_init_file_error ("$else found without matching $if");
934
      return 0;
935
    }
936
 
937
  /* Check the previous (n - 1) levels of the stack to make sure that
938
     we haven't previously turned off parsing. */
939
  for (i = 0; i < if_stack_depth - 1; i++)
940
    if (if_stack[i] == 1)
941
      return 0;
942
 
943
  /* Invert the state of parsing if at top level. */
944
  _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
945
  return 0;
946
}
947
 
948
/* Terminate a conditional, popping the value of
949
   _rl_parsing_conditionalized_out from the stack. */
950
static int
951
parser_endif (args)
952
     char *args;
953
{
954
  if (if_stack_depth)
955
    _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
956
  else
957
    _rl_init_file_error ("$endif without matching $if");
958
  return 0;
959
}
960
 
961
static int
962
parser_include (args)
963
     char *args;
964
{
965
  char *old_init_file, *e;
966
  int old_line_number, old_include_level, r;
967
 
968
  if (_rl_parsing_conditionalized_out)
969
    return (0);
970
 
971
  old_init_file = current_readline_init_file;
972
  old_line_number = current_readline_init_lineno;
973
  old_include_level = current_readline_init_include_level;
974
 
975
  e = strchr (args, '\n');
976
  if (e)
977
    *e = '\0';
978
  r = _rl_read_init_file (args, old_include_level + 1);
979
 
980
  current_readline_init_file = old_init_file;
981
  current_readline_init_lineno = old_line_number;
982
  current_readline_init_include_level = old_include_level;
983
 
984
  return r;
985
}
986
 
987
/* Associate textual names with actual functions. */
988
static struct {
989
  char *name;
990
  Function *function;
991
} parser_directives [] = {
992
  { "if", parser_if },
993
  { "endif", parser_endif },
994
  { "else", parser_else },
995
  { "include", parser_include },
996
  { (char *)0x0, (Function *)0x0 }
997
};
998
 
999
/* Handle a parser directive.  STATEMENT is the line of the directive
1000
   without any leading `$'. */
1001
static int
1002
handle_parser_directive (statement)
1003
     char *statement;
1004
{
1005
  register int i;
1006
  char *directive, *args;
1007
 
1008
  /* Isolate the actual directive. */
1009
 
1010
  /* Skip whitespace. */
1011
  for (i = 0; whitespace (statement[i]); i++);
1012
 
1013
  directive = &statement[i];
1014
 
1015
  for (; statement[i] && !whitespace (statement[i]); i++);
1016
 
1017
  if (statement[i])
1018
    statement[i++] = '\0';
1019
 
1020
  for (; statement[i] && whitespace (statement[i]); i++);
1021
 
1022
  args = &statement[i];
1023
 
1024
  /* Lookup the command, and act on it. */
1025
  for (i = 0; parser_directives[i].name; i++)
1026
    if (_rl_stricmp (directive, parser_directives[i].name) == 0)
1027
      {
1028
        (*parser_directives[i].function) (args);
1029
        return (0);
1030
      }
1031
 
1032
  /* display an error message about the unknown parser directive */
1033
  _rl_init_file_error ("unknown parser directive");
1034
  return (1);
1035
}
1036
 
1037
/* Read the binding command from STRING and perform it.
1038
   A key binding command looks like: Keyname: function-name\0,
1039
   a variable binding command looks like: set variable value.
1040
   A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
1041
int
1042
rl_parse_and_bind (string)
1043
     char *string;
1044
{
1045
  char *funname, *kname;
1046
  register int c, i;
1047
  int key, equivalency;
1048
 
1049
  while (string && whitespace (*string))
1050
    string++;
1051
 
1052
  if (!string || !*string || *string == '#')
1053
    return 0;
1054
 
1055
  /* If this is a parser directive, act on it. */
1056
  if (*string == '$')
1057
    {
1058
      handle_parser_directive (&string[1]);
1059
      return 0;
1060
    }
1061
 
1062
  /* If we aren't supposed to be parsing right now, then we're done. */
1063
  if (_rl_parsing_conditionalized_out)
1064
    return 0;
1065
 
1066
  i = 0;
1067
  /* If this keyname is a complex key expression surrounded by quotes,
1068
     advance to after the matching close quote.  This code allows the
1069
     backslash to quote characters in the key expression. */
1070
  if (*string == '"')
1071
    {
1072
      int passc = 0;
1073
 
1074
      for (i = 1; c = string[i]; i++)
1075
        {
1076
          if (passc)
1077
            {
1078
              passc = 0;
1079
              continue;
1080
            }
1081
 
1082
          if (c == '\\')
1083
            {
1084
              passc++;
1085
              continue;
1086
            }
1087
 
1088
          if (c == '"')
1089
            break;
1090
        }
1091
      /* If we didn't find a closing quote, abort the line. */
1092
      if (string[i] == '\0')
1093
        {
1094
          _rl_init_file_error ("no closing `\"' in key binding");
1095
          return 1;
1096
        }
1097
    }
1098
 
1099
  /* Advance to the colon (:) or whitespace which separates the two objects. */
1100
  for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
1101
 
1102
  equivalency = (c == ':' && string[i + 1] == '=');
1103
 
1104
  /* Mark the end of the command (or keyname). */
1105
  if (string[i])
1106
    string[i++] = '\0';
1107
 
1108
  /* If doing assignment, skip the '=' sign as well. */
1109
  if (equivalency)
1110
    string[i++] = '\0';
1111
 
1112
  /* If this is a command to set a variable, then do that. */
1113
  if (_rl_stricmp (string, "set") == 0)
1114
    {
1115
      char *var = string + i;
1116
      char *value;
1117
 
1118
      /* Make VAR point to start of variable name. */
1119
      while (*var && whitespace (*var)) var++;
1120
 
1121
      /* Make value point to start of value string. */
1122
      value = var;
1123
      while (*value && !whitespace (*value)) value++;
1124
      if (*value)
1125
        *value++ = '\0';
1126
      while (*value && whitespace (*value)) value++;
1127
 
1128
      rl_variable_bind (var, value);
1129
      return 0;
1130
    }
1131
 
1132
  /* Skip any whitespace between keyname and funname. */
1133
  for (; string[i] && whitespace (string[i]); i++);
1134
  funname = &string[i];
1135
 
1136
  /* Now isolate funname.
1137
     For straight function names just look for whitespace, since
1138
     that will signify the end of the string.  But this could be a
1139
     macro definition.  In that case, the string is quoted, so skip
1140
     to the matching delimiter.  We allow the backslash to quote the
1141
     delimiter characters in the macro body. */
1142
  /* This code exists to allow whitespace in macro expansions, which
1143
     would otherwise be gobbled up by the next `for' loop.*/
1144
  /* XXX - it may be desirable to allow backslash quoting only if " is
1145
     the quoted string delimiter, like the shell. */
1146
  if (*funname == '\'' || *funname == '"')
1147
    {
1148
      int delimiter = string[i++], passc;
1149
 
1150
      for (passc = 0; c = string[i]; i++)
1151
        {
1152
          if (passc)
1153
            {
1154
              passc = 0;
1155
              continue;
1156
            }
1157
 
1158
          if (c == '\\')
1159
            {
1160
              passc = 1;
1161
              continue;
1162
            }
1163
 
1164
          if (c == delimiter)
1165
            break;
1166
        }
1167
      if (c)
1168
        i++;
1169
    }
1170
 
1171
  /* Advance to the end of the string.  */
1172
  for (; string[i] && !whitespace (string[i]); i++);
1173
 
1174
  /* No extra whitespace at the end of the string. */
1175
  string[i] = '\0';
1176
 
1177
  /* Handle equivalency bindings here.  Make the left-hand side be exactly
1178
     whatever the right-hand evaluates to, including keymaps. */
1179
  if (equivalency)
1180
    {
1181
      return 0;
1182
    }
1183
 
1184
  /* If this is a new-style key-binding, then do the binding with
1185
     rl_set_key ().  Otherwise, let the older code deal with it. */
1186
  if (*string == '"')
1187
    {
1188
      char *seq;
1189
      register int j, k, passc;
1190
 
1191
      seq = xmalloc (1 + strlen (string));
1192
      for (j = 1, k = passc = 0; string[j]; j++)
1193
        {
1194
          /* Allow backslash to quote characters, but leave them in place.
1195
             This allows a string to end with a backslash quoting another
1196
             backslash, or with a backslash quoting a double quote.  The
1197
             backslashes are left in place for rl_translate_keyseq (). */
1198
          if (passc || (string[j] == '\\'))
1199
            {
1200
              seq[k++] = string[j];
1201
              passc = !passc;
1202
              continue;
1203
            }
1204
 
1205
          if (string[j] == '"')
1206
            break;
1207
 
1208
          seq[k++] = string[j];
1209
        }
1210
      seq[k] = '\0';
1211
 
1212
      /* Binding macro? */
1213
      if (*funname == '\'' || *funname == '"')
1214
        {
1215
          j = strlen (funname);
1216
 
1217
          /* Remove the delimiting quotes from each end of FUNNAME. */
1218
          if (j && funname[j - 1] == *funname)
1219
            funname[j - 1] = '\0';
1220
 
1221
          rl_macro_bind (seq, &funname[1], _rl_keymap);
1222
        }
1223
      else
1224
        rl_set_key (seq, rl_named_function (funname), _rl_keymap);
1225
 
1226
      free (seq);
1227
      return 0;
1228
    }
1229
 
1230
  /* Get the actual character we want to deal with. */
1231
  kname = strrchr (string, '-');
1232
  if (!kname)
1233
    kname = string;
1234
  else
1235
    kname++;
1236
 
1237
  key = glean_key_from_name (kname);
1238
 
1239
  /* Add in control and meta bits. */
1240
  if (substring_member_of_array (string, possible_control_prefixes))
1241
    key = CTRL (_rl_to_upper (key));
1242
 
1243
  if (substring_member_of_array (string, possible_meta_prefixes))
1244
    key = META (key);
1245
 
1246
  /* Temporary.  Handle old-style keyname with macro-binding. */
1247
  if (*funname == '\'' || *funname == '"')
1248
    {
1249
      unsigned char useq[2];
1250
      int fl = strlen (funname);
1251
 
1252
      useq[0] = key; useq[1] = '\0';
1253
      if (fl && funname[fl - 1] == *funname)
1254
        funname[fl - 1] = '\0';
1255
 
1256
      rl_macro_bind (useq, &funname[1], _rl_keymap);
1257
    }
1258
#if defined (PREFIX_META_HACK)
1259
  /* Ugly, but working hack to keep prefix-meta around. */
1260
  else if (_rl_stricmp (funname, "prefix-meta") == 0)
1261
    {
1262
      char seq[2];
1263
 
1264
      seq[0] = key;
1265
      seq[1] = '\0';
1266
      rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
1267
    }
1268
#endif /* PREFIX_META_HACK */
1269
  else
1270
    rl_bind_key (key, rl_named_function (funname));
1271
  return 0;
1272
}
1273
 
1274
/* Simple structure for boolean readline variables (i.e., those that can
1275
   have one of two values; either "On" or 1 for truth, or "Off" or 0 for
1276
   false. */
1277
 
1278
static struct {
1279
  char *name;
1280
  int *value;
1281
} boolean_varlist [] = {
1282
#if defined (PAREN_MATCHING)
1283
  { "blink-matching-paren",     &rl_blink_matching_paren },
1284
#endif
1285
  { "completion-ignore-case",   &_rl_completion_case_fold },
1286
  { "convert-meta",             &_rl_convert_meta_chars_to_ascii },
1287
  { "disable-completion",       &rl_inhibit_completion },
1288
  { "enable-keypad",            &_rl_enable_keypad },
1289
  { "expand-tilde",             &rl_complete_with_tilde_expansion },
1290
  { "horizontal-scroll-mode",   &_rl_horizontal_scroll_mode },
1291
  { "input-meta",               &_rl_meta_flag },
1292
  { "mark-directories",         &_rl_complete_mark_directories },
1293
  { "mark-modified-lines",      &_rl_mark_modified_lines },
1294
  { "meta-flag",                &_rl_meta_flag },
1295
  { "output-meta",              &_rl_output_meta_chars },
1296
  { "print-completions-horizontally", &_rl_print_completions_horizontally },
1297
  { "show-all-if-ambiguous",    &_rl_complete_show_all },
1298
#if defined (VISIBLE_STATS)
1299
  { "visible-stats",            &rl_visible_stats },
1300
#endif /* VISIBLE_STATS */
1301
  { (char *)NULL, (int *)NULL }
1302
};
1303
 
1304
int
1305
rl_variable_bind (name, value)
1306
     char *name, *value;
1307
{
1308
  register int i;
1309
 
1310
  /* Check for simple variables first. */
1311
  for (i = 0; boolean_varlist[i].name; i++)
1312
    {
1313
      if (_rl_stricmp (name, boolean_varlist[i].name) == 0)
1314
        {
1315
          /* A variable is TRUE if the "value" is "on", "1" or "". */
1316
          *boolean_varlist[i].value = *value == 0 ||
1317
                                      _rl_stricmp (value, "on") == 0 ||
1318
                                      (value[0] == '1' && value[1] == '\0');
1319
          return 0;
1320
        }
1321
    }
1322
 
1323
  /* Not a boolean variable, so check for specials. */
1324
 
1325
  /* Editing mode change? */
1326
  if (_rl_stricmp (name, "editing-mode") == 0)
1327
    {
1328
      if (_rl_strnicmp (value, "vi", 2) == 0)
1329
        {
1330
#if defined (VI_MODE)
1331
          _rl_keymap = vi_insertion_keymap;
1332
          rl_editing_mode = vi_mode;
1333
#endif /* VI_MODE */
1334
        }
1335
      else if (_rl_strnicmp (value, "emacs", 5) == 0)
1336
        {
1337
          _rl_keymap = emacs_standard_keymap;
1338
          rl_editing_mode = emacs_mode;
1339
        }
1340
    }
1341
 
1342
  /* Comment string change? */
1343
  else if (_rl_stricmp (name, "comment-begin") == 0)
1344
    {
1345
      if (*value)
1346
        {
1347
          if (_rl_comment_begin)
1348
            free (_rl_comment_begin);
1349
 
1350
          _rl_comment_begin = savestring (value);
1351
        }
1352
    }
1353
  else if (_rl_stricmp (name, "completion-query-items") == 0)
1354
    {
1355
      int nval = 100;
1356
      if (*value)
1357
        {
1358
          nval = atoi (value);
1359
          if (nval < 0)
1360
            nval = 0;
1361
        }
1362
      rl_completion_query_items = nval;
1363
    }
1364
  else if (_rl_stricmp (name, "keymap") == 0)
1365
    {
1366
      Keymap kmap;
1367
      kmap = rl_get_keymap_by_name (value);
1368
      if (kmap)
1369
        rl_set_keymap (kmap);
1370
    }
1371
  else if (_rl_stricmp (name, "bell-style") == 0)
1372
    {
1373
      if (!*value)
1374
        _rl_bell_preference = AUDIBLE_BELL;
1375
      else
1376
        {
1377
          if (_rl_stricmp (value, "none") == 0 || _rl_stricmp (value, "off") == 0)
1378
            _rl_bell_preference = NO_BELL;
1379
          else if (_rl_stricmp (value, "audible") == 0 || _rl_stricmp (value, "on") == 0)
1380
            _rl_bell_preference = AUDIBLE_BELL;
1381
          else if (_rl_stricmp (value, "visible") == 0)
1382
            _rl_bell_preference = VISIBLE_BELL;
1383
        }
1384
    }
1385
  else if (_rl_stricmp (name, "prefer-visible-bell") == 0)
1386
    {
1387
      /* Backwards compatibility. */
1388
      if (*value && (_rl_stricmp (value, "on") == 0 ||
1389
                     (*value == '1' && !value[1])))
1390
        _rl_bell_preference = VISIBLE_BELL;
1391
      else
1392
        _rl_bell_preference = AUDIBLE_BELL;
1393
    }
1394
  else if (_rl_stricmp (name, "isearch-terminators") == 0)
1395
    {
1396
      /* Isolate the value and translate it into a character string. */
1397
      int beg, end;
1398
      char *v;
1399
 
1400
      v = savestring (value);
1401
      FREE (_rl_isearch_terminators);
1402
      if (v[0] == '"' || v[0] == '\'')
1403
        {
1404
          int delim = v[0];
1405
          for (beg = end = 1; v[end] && v[end] != delim; end++)
1406
            ;
1407
        }
1408
      else
1409
        {
1410
          for (beg = end = 0; whitespace (v[end]) == 0; end++)
1411
            ;
1412
        }
1413
 
1414
      v[end] = '\0';
1415
      /* The value starts at v + beg.  Translate it into a character string. */
1416
      _rl_isearch_terminators = (unsigned char *)xmalloc (2 * strlen (v) + 1);
1417
      rl_translate_keyseq (v + beg, _rl_isearch_terminators, &end);
1418
      _rl_isearch_terminators[end] = '\0';
1419
      free (v);
1420
    }
1421
 
1422
  /* For the time being, unknown variable names are simply ignored. */
1423
  return 0;
1424
}
1425
 
1426
/* Return the character which matches NAME.
1427
   For example, `Space' returns ' '. */
1428
 
1429
typedef struct {
1430
  char *name;
1431
  int value;
1432
} assoc_list;
1433
 
1434
static assoc_list name_key_alist[] = {
1435
  { "DEL", 0x7f },
1436
  { "ESC", '\033' },
1437
  { "Escape", '\033' },
1438
  { "LFD", '\n' },
1439
  { "Newline", '\n' },
1440
  { "RET", '\r' },
1441
  { "Return", '\r' },
1442
  { "Rubout", 0x7f },
1443
  { "SPC", ' ' },
1444
  { "Space", ' ' },
1445
  { "Tab", 0x09 },
1446
  { (char *)0x0, 0 }
1447
};
1448
 
1449
static int
1450
glean_key_from_name (name)
1451
     char *name;
1452
{
1453
  register int i;
1454
 
1455
  for (i = 0; name_key_alist[i].name; i++)
1456
    if (_rl_stricmp (name, name_key_alist[i].name) == 0)
1457
      return (name_key_alist[i].value);
1458
 
1459
  return (*(unsigned char *)name);      /* XXX was return (*name) */
1460
}
1461
 
1462
/* Auxiliary functions to manage keymaps. */
1463
static struct {
1464
  char *name;
1465
  Keymap map;
1466
} keymap_names[] = {
1467
  { "emacs", emacs_standard_keymap },
1468
  { "emacs-standard", emacs_standard_keymap },
1469
  { "emacs-meta", emacs_meta_keymap },
1470
  { "emacs-ctlx", emacs_ctlx_keymap },
1471
#if defined (VI_MODE)
1472
  { "vi", vi_movement_keymap },
1473
  { "vi-move", vi_movement_keymap },
1474
  { "vi-command", vi_movement_keymap },
1475
  { "vi-insert", vi_insertion_keymap },
1476
#endif /* VI_MODE */
1477
  { (char *)0x0, (Keymap)0x0 }
1478
};
1479
 
1480
Keymap
1481
rl_get_keymap_by_name (name)
1482
     char *name;
1483
{
1484
  register int i;
1485
 
1486
  for (i = 0; keymap_names[i].name; i++)
1487
    if (strcmp (name, keymap_names[i].name) == 0)
1488
      return (keymap_names[i].map);
1489
  return ((Keymap) NULL);
1490
}
1491
 
1492
char *
1493
rl_get_keymap_name (map)
1494
     Keymap map;
1495
{
1496
  register int i;
1497
  for (i = 0; keymap_names[i].name; i++)
1498
    if (map == keymap_names[i].map)
1499
      return (keymap_names[i].name);
1500
  return ((char *)NULL);
1501
}
1502
 
1503
void
1504
rl_set_keymap (map)
1505
     Keymap map;
1506
{
1507
  if (map)
1508
    _rl_keymap = map;
1509
}
1510
 
1511
Keymap
1512
rl_get_keymap ()
1513
{
1514
  return (_rl_keymap);
1515
}
1516
 
1517
void
1518
rl_set_keymap_from_edit_mode ()
1519
{
1520
  if (rl_editing_mode == emacs_mode)
1521
    _rl_keymap = emacs_standard_keymap;
1522
#if defined (VI_MODE)
1523
  else if (rl_editing_mode == vi_mode)
1524
    _rl_keymap = vi_insertion_keymap;
1525
#endif /* VI_MODE */
1526
}
1527
 
1528
char *
1529
rl_get_keymap_name_from_edit_mode ()
1530
{
1531
  if (rl_editing_mode == emacs_mode)
1532
    return "emacs";
1533
#if defined (VI_MODE)
1534
  else if (rl_editing_mode == vi_mode)
1535
    return "vi";
1536
#endif /* VI_MODE */
1537
  else
1538
    return "none";
1539
}
1540
 
1541
/* **************************************************************** */
1542
/*                                                                  */
1543
/*                Key Binding and Function Information              */
1544
/*                                                                  */
1545
/* **************************************************************** */
1546
 
1547
/* Each of the following functions produces information about the
1548
   state of keybindings and functions known to Readline.  The info
1549
   is always printed to rl_outstream, and in such a way that it can
1550
   be read back in (i.e., passed to rl_parse_and_bind (). */
1551
 
1552
/* Print the names of functions known to Readline. */
1553
void
1554
rl_list_funmap_names ()
1555
{
1556
  register int i;
1557
  char **funmap_names;
1558
 
1559
  funmap_names = rl_funmap_names ();
1560
 
1561
  if (!funmap_names)
1562
    return;
1563
 
1564
  for (i = 0; funmap_names[i]; i++)
1565
    fprintf (rl_outstream, "%s\n", funmap_names[i]);
1566
 
1567
  free (funmap_names);
1568
}
1569
 
1570
static char *
1571
_rl_get_keyname (key)
1572
     int key;
1573
{
1574
  char *keyname;
1575
  int i, c;
1576
 
1577
  keyname = (char *)xmalloc (8);
1578
 
1579
  c = key;
1580
  /* Since this is going to be used to write out keysequence-function
1581
     pairs for possible inclusion in an inputrc file, we don't want to
1582
     do any special meta processing on KEY. */
1583
 
1584
#if 0
1585
  /* We might want to do this, but the old version of the code did not. */
1586
 
1587
  /* If this is an escape character, we don't want to do any more processing.
1588
     Just add the special ESC key sequence and return. */
1589
  if (c == ESC)
1590
    {
1591
      keyseq[0] = '\\';
1592
      keyseq[1] = 'e';
1593
      keyseq[2] = '\0';
1594
      return keyseq;
1595
    }
1596
#endif
1597
 
1598
  /* RUBOUT is translated directly into \C-? */
1599
  if (key == RUBOUT)
1600
    {
1601
      keyname[0] = '\\';
1602
      keyname[1] = 'C';
1603
      keyname[2] = '-';
1604
      keyname[3] = '?';
1605
      keyname[4] = '\0';
1606
      return keyname;
1607
    }
1608
 
1609
  i = 0;
1610
  /* Now add special prefixes needed for control characters.  This can
1611
     potentially change C. */
1612
  if (CTRL_CHAR (c))
1613
    {
1614
      keyname[i++] = '\\';
1615
      keyname[i++] = 'C';
1616
      keyname[i++] = '-';
1617
      c = _rl_to_lower (UNCTRL (c));
1618
    }
1619
 
1620
  /* XXX experimental code.  Turn the characters that are not ASCII or
1621
     ISO Latin 1 (128 - 159) into octal escape sequences (\200 - \237).
1622
     This changes C. */
1623
  if (c >= 128 && c <= 159)
1624
    {
1625
      keyname[i++] = '\\';
1626
      keyname[i++] = '2';
1627
      c -= 128;
1628
      keyname[i++] = (c / 8) + '0';
1629
      c = (c % 8) + '0';
1630
    }
1631
 
1632
  /* Now, if the character needs to be quoted with a backslash, do that. */
1633
  if (c == '\\' || c == '"')
1634
    keyname[i++] = '\\';
1635
 
1636
  /* Now add the key, terminate the string, and return it. */
1637
  keyname[i++] = (char) c;
1638
  keyname[i] = '\0';
1639
 
1640
  return keyname;
1641
}
1642
 
1643
/* Return a NULL terminated array of strings which represent the key
1644
   sequences that are used to invoke FUNCTION in MAP. */
1645
char **
1646
rl_invoking_keyseqs_in_map (function, map)
1647
     Function *function;
1648
     Keymap map;
1649
{
1650
  register int key;
1651
  char **result;
1652
  int result_index, result_size;
1653
 
1654
  result = (char **)NULL;
1655
  result_index = result_size = 0;
1656
 
1657
  for (key = 0; key < KEYMAP_SIZE; key++)
1658
    {
1659
      switch (map[key].type)
1660
        {
1661
        case ISMACR:
1662
          /* Macros match, if, and only if, the pointers are identical.
1663
             Thus, they are treated exactly like functions in here. */
1664
        case ISFUNC:
1665
          /* If the function in the keymap is the one we are looking for,
1666
             then add the current KEY to the list of invoking keys. */
1667
          if (map[key].function == function)
1668
            {
1669
              char *keyname;
1670
 
1671
              keyname = _rl_get_keyname (key);
1672
 
1673
              if (result_index + 2 > result_size)
1674
                {
1675
                  result_size += 10;
1676
                  result = (char **) xrealloc (result, result_size * sizeof (char *));
1677
                }
1678
 
1679
              result[result_index++] = keyname;
1680
              result[result_index] = (char *)NULL;
1681
            }
1682
          break;
1683
 
1684
        case ISKMAP:
1685
          {
1686
            char **seqs;
1687
            register int i;
1688
 
1689
            /* Find the list of keyseqs in this map which have FUNCTION as
1690
               their target.  Add the key sequences found to RESULT. */
1691
            if (map[key].function)
1692
              seqs =
1693
                rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
1694
            else
1695
              break;
1696
 
1697
            if (seqs == 0)
1698
              break;
1699
 
1700
            for (i = 0; seqs[i]; i++)
1701
              {
1702
                char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
1703
 
1704
                if (key == ESC)
1705
                  sprintf (keyname, "\\e");
1706
                else if (CTRL_CHAR (key))
1707
                  sprintf (keyname, "\\C-%c", _rl_to_lower (UNCTRL (key)));
1708
                else if (key == RUBOUT)
1709
                  sprintf (keyname, "\\C-?");
1710
                else if (key == '\\' || key == '"')
1711
                  {
1712
                    keyname[0] = '\\';
1713
                    keyname[1] = (char) key;
1714
                    keyname[2] = '\0';
1715
                  }
1716
                else
1717
                  {
1718
                    keyname[0] = (char) key;
1719
                    keyname[1] = '\0';
1720
                  }
1721
 
1722
                strcat (keyname, seqs[i]);
1723
                free (seqs[i]);
1724
 
1725
                if (result_index + 2 > result_size)
1726
                  {
1727
                    result_size += 10;
1728
                    result = (char **) xrealloc (result, result_size * sizeof (char *));
1729
                  }
1730
 
1731
                result[result_index++] = keyname;
1732
                result[result_index] = (char *)NULL;
1733
              }
1734
 
1735
            free (seqs);
1736
          }
1737
          break;
1738
        }
1739
    }
1740
  return (result);
1741
}
1742
 
1743
/* Return a NULL terminated array of strings which represent the key
1744
   sequences that can be used to invoke FUNCTION using the current keymap. */
1745
char **
1746
rl_invoking_keyseqs (function)
1747
     Function *function;
1748
{
1749
  return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
1750
}
1751
 
1752
/* Print all of the functions and their bindings to rl_outstream.  If
1753
   PRINT_READABLY is non-zero, then print the output in such a way
1754
   that it can be read back in. */
1755
void
1756
rl_function_dumper (print_readably)
1757
     int print_readably;
1758
{
1759
  register int i;
1760
  char **names;
1761
  char *name;
1762
 
1763
  names = rl_funmap_names ();
1764
 
1765
  fprintf (rl_outstream, "\n");
1766
 
1767
  for (i = 0; name = names[i]; i++)
1768
    {
1769
      Function *function;
1770
      char **invokers;
1771
 
1772
      function = rl_named_function (name);
1773
      invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
1774
 
1775
      if (print_readably)
1776
        {
1777
          if (!invokers)
1778
            fprintf (rl_outstream, "# %s (not bound)\n", name);
1779
          else
1780
            {
1781
              register int j;
1782
 
1783
              for (j = 0; invokers[j]; j++)
1784
                {
1785
                  fprintf (rl_outstream, "\"%s\": %s\n",
1786
                           invokers[j], name);
1787
                  free (invokers[j]);
1788
                }
1789
 
1790
              free (invokers);
1791
            }
1792
        }
1793
      else
1794
        {
1795
          if (!invokers)
1796
            fprintf (rl_outstream, "%s is not bound to any keys\n",
1797
                     name);
1798
          else
1799
            {
1800
              register int j;
1801
 
1802
              fprintf (rl_outstream, "%s can be found on ", name);
1803
 
1804
              for (j = 0; invokers[j] && j < 5; j++)
1805
                {
1806
                  fprintf (rl_outstream, "\"%s\"%s", invokers[j],
1807
                           invokers[j + 1] ? ", " : ".\n");
1808
                }
1809
 
1810
              if (j == 5 && invokers[j])
1811
                fprintf (rl_outstream, "...\n");
1812
 
1813
              for (j = 0; invokers[j]; j++)
1814
                free (invokers[j]);
1815
 
1816
              free (invokers);
1817
            }
1818
        }
1819
    }
1820
}
1821
 
1822
/* Print all of the current functions and their bindings to
1823
   rl_outstream.  If an explicit argument is given, then print
1824
   the output in such a way that it can be read back in. */
1825
int
1826
rl_dump_functions (count, key)
1827
     int count, key;
1828
{
1829
  if (rl_dispatching)
1830
    fprintf (rl_outstream, "\r\n");
1831
  rl_function_dumper (rl_explicit_arg);
1832
  rl_on_new_line ();
1833
  return (0);
1834
}
1835
 
1836
static void
1837
_rl_macro_dumper_internal (print_readably, map, prefix)
1838
     int print_readably;
1839
     Keymap map;
1840
     char *prefix;
1841
{
1842
  register int key;
1843
  char *keyname, *out;
1844
  int prefix_len;
1845
 
1846
  for (key = 0; key < KEYMAP_SIZE; key++)
1847
    {
1848
      switch (map[key].type)
1849
        {
1850
        case ISMACR:
1851
          keyname = _rl_get_keyname (key);
1852
#if 0
1853
          out = (char *)map[key].function;
1854
#else
1855
          out = _rl_untranslate_macro_value ((char *)map[key].function);
1856
#endif
1857
          if (print_readably)
1858
            fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
1859
                                                         keyname,
1860
                                                         out ? out : "");
1861
          else
1862
            fprintf (rl_outstream, "%s%s outputs %s\n", prefix ? prefix : "",
1863
                                                        keyname,
1864
                                                        out ? out : "");
1865
          free (keyname);
1866
#if 1
1867
          free (out);
1868
#endif
1869
          break;
1870
        case ISFUNC:
1871
          break;
1872
        case ISKMAP:
1873
          prefix_len = prefix ? strlen (prefix) : 0;
1874
          if (key == ESC)
1875
            {
1876
              keyname = xmalloc (3 + prefix_len);
1877
              if (prefix)
1878
                strcpy (keyname, prefix);
1879
              keyname[prefix_len] = '\\';
1880
              keyname[prefix_len + 1] = 'e';
1881
              keyname[prefix_len + 2] = '\0';
1882
            }
1883
          else
1884
            {
1885
              keyname = _rl_get_keyname (key);
1886
              if (prefix)
1887
                {
1888
                  out = xmalloc (strlen (keyname) + prefix_len + 1);
1889
                  strcpy (out, prefix);
1890
                  strcpy (out + prefix_len, keyname);
1891
                  free (keyname);
1892
                  keyname = out;
1893
                }
1894
            }
1895
 
1896
          _rl_macro_dumper_internal (print_readably, FUNCTION_TO_KEYMAP (map, key), keyname);
1897
          free (keyname);
1898
          break;
1899
        }
1900
    }
1901
}
1902
 
1903
void
1904
rl_macro_dumper (print_readably)
1905
     int print_readably;
1906
{
1907
  _rl_macro_dumper_internal (print_readably, _rl_keymap, (char *)NULL);
1908
}
1909
 
1910
int
1911
rl_dump_macros (count, key)
1912
     int count, key;
1913
{
1914
  if (rl_dispatching)
1915
    fprintf (rl_outstream, "\r\n");
1916
  rl_macro_dumper (rl_explicit_arg);
1917
  rl_on_new_line ();
1918
  return (0);
1919
}
1920
 
1921
void
1922
rl_variable_dumper (print_readably)
1923
     int print_readably;
1924
{
1925
  int i;
1926
  char *kname;
1927
 
1928
  for (i = 0; boolean_varlist[i].name; i++)
1929
    {
1930
      if (print_readably)
1931
        fprintf (rl_outstream, "set %s %s\n", boolean_varlist[i].name,
1932
                               *boolean_varlist[i].value ? "on" : "off");
1933
      else
1934
        fprintf (rl_outstream, "%s is set to `%s'\n", boolean_varlist[i].name,
1935
                               *boolean_varlist[i].value ? "on" : "off");
1936
    }
1937
 
1938
  /* bell-style */
1939
  switch (_rl_bell_preference)
1940
    {
1941
    case NO_BELL:
1942
      kname = "none"; break;
1943
    case VISIBLE_BELL:
1944
      kname = "visible"; break;
1945
    case AUDIBLE_BELL:
1946
    default:
1947
      kname = "audible"; break;
1948
    }
1949
  if (print_readably)
1950
    fprintf (rl_outstream, "set bell-style %s\n", kname);
1951
  else
1952
    fprintf (rl_outstream, "bell-style is set to `%s'\n", kname);
1953
 
1954
  /* comment-begin */
1955
  if (print_readably)
1956
    fprintf (rl_outstream, "set comment-begin %s\n", _rl_comment_begin ? _rl_comment_begin : RL_COMMENT_BEGIN_DEFAULT);
1957
  else
1958
    fprintf (rl_outstream, "comment-begin is set to `%s'\n", _rl_comment_begin ? _rl_comment_begin : "");
1959
 
1960
  /* completion-query-items */
1961
  if (print_readably)
1962
    fprintf (rl_outstream, "set completion-query-items %d\n", rl_completion_query_items);
1963
  else
1964
    fprintf (rl_outstream, "completion-query-items is set to `%d'\n", rl_completion_query_items);
1965
 
1966
  /* editing-mode */
1967
  if (print_readably)
1968
    fprintf (rl_outstream, "set editing-mode %s\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1969
  else
1970
    fprintf (rl_outstream, "editing-mode is set to `%s'\n", (rl_editing_mode == emacs_mode) ? "emacs" : "vi");
1971
 
1972
  /* keymap */
1973
  kname = rl_get_keymap_name (_rl_keymap);
1974
  if (kname == 0)
1975
    kname = rl_get_keymap_name_from_edit_mode ();
1976
  if (print_readably)
1977
    fprintf (rl_outstream, "set keymap %s\n", kname ? kname : "none");
1978
  else
1979
    fprintf (rl_outstream, "keymap is set to `%s'\n", kname ? kname : "none");
1980
 
1981
  /* isearch-terminators */
1982
  if (_rl_isearch_terminators)
1983
    {
1984
      char *disp;
1985
 
1986
      disp = _rl_untranslate_macro_value (_rl_isearch_terminators);
1987
 
1988
      if (print_readably)
1989
        fprintf (rl_outstream, "set isearch-terminators \"%s\"\n", disp);
1990
      else
1991
        fprintf (rl_outstream, "isearch-terminators is set to \"%s\"\n", disp);
1992
 
1993
      free (disp);
1994
    }
1995
}
1996
 
1997
/* Print all of the current variables and their values to
1998
   rl_outstream.  If an explicit argument is given, then print
1999
   the output in such a way that it can be read back in. */
2000
int
2001
rl_dump_variables (count, key)
2002
     int count, key;
2003
{
2004
  if (rl_dispatching)
2005
    fprintf (rl_outstream, "\r\n");
2006
  rl_variable_dumper (rl_explicit_arg);
2007
  rl_on_new_line ();
2008
  return (0);
2009
}
2010
 
2011
/* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
2012
void
2013
_rl_bind_if_unbound (keyseq, default_func)
2014
     char *keyseq;
2015
     Function *default_func;
2016
{
2017
  Function *func;
2018
 
2019
  if (keyseq)
2020
    {
2021
      func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
2022
      if (!func || func == rl_do_lowercase_version)
2023
        rl_set_key (keyseq, default_func, _rl_keymap);
2024
    }
2025
}
2026
 
2027
/* Return non-zero if any members of ARRAY are a substring in STRING. */
2028
static int
2029
substring_member_of_array (string, array)
2030
     char *string, **array;
2031
{
2032
  while (*array)
2033
    {
2034
      if (_rl_strindex (string, *array))
2035
        return (1);
2036
      array++;
2037
    }
2038
  return (0);
2039
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.