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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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