Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / lib / readline / funmap.c
1 /* funmap.c -- attach names to functions. */
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 (STATIC_MALLOC)
25 static char *xmalloc (), *xrealloc ();
26 #else
27 extern char *xmalloc (), *xrealloc ();
28 #endif /* STATIC_MALLOC */
29
30 #if !defined (BUFSIZ)
31 #include <stdio.h>
32 #endif /* BUFSIZ */
33
34 #if defined (HAVE_STDLIB_H)
35 #  include <stdlib.h>
36 #else
37 #  include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
39
40 #include "rlconf.h"
41 #include "readline.h"
42
43 static int qsort_string_compare ();
44
45 FUNMAP **funmap = (FUNMAP **)NULL;
46 static int funmap_size = 0;
47 static int funmap_entry = 0;
48
49 /* After initializing the function map, this is the index of the first
50    program specific function. */
51 int funmap_program_specific_entry_start;
52
53 static FUNMAP default_funmap[] = {
54
55   { "abort", rl_abort },
56   { "accept-line", rl_newline },
57   { "arrow-key-prefix", rl_arrow_keys },
58   { "backward-char", rl_backward },
59   { "backward-delete-char", rl_rubout },
60   { "backward-kill-line", rl_backward_kill_line },
61   { "backward-kill-word", rl_backward_kill_word },
62   { "backward-word", rl_backward_word },
63   { "beginning-of-history", rl_beginning_of_history },
64   { "beginning-of-line", rl_beg_of_line },
65   { "call-last-kbd-macro", rl_call_last_kbd_macro },
66   { "capitalize-word", rl_capitalize_word },
67   { "clear-screen", rl_clear_screen },
68   { "complete", rl_complete },
69   { "delete-char", rl_delete },
70   { "delete-horizontal-space", rl_delete_horizontal_space },
71   { "digit-argument", rl_digit_argument },
72   { "do-lowercase-version", rl_do_lowercase_version },
73   { "downcase-word", rl_downcase_word },
74   { "dump-functions", rl_dump_functions },
75   { "emacs-editing-mode", rl_emacs_editing_mode },
76   { "end-kbd-macro", rl_end_kbd_macro },
77   { "end-of-history", rl_end_of_history },
78   { "end-of-line", rl_end_of_line },
79   { "forward-char", rl_forward },
80   { "forward-search-history", rl_forward_search_history },
81   { "forward-word", rl_forward_word },
82   { "history-search-backward", rl_history_search_backward },
83   { "history-search-forward", rl_history_search_forward },
84   { "insert-completions", rl_insert_completions },
85   { "kill-whole-line", rl_kill_full_line },
86   { "kill-line", rl_kill_line },
87   { "kill-word", rl_kill_word },
88   { "next-history", rl_get_next_history },
89   { "non-incremental-forward-search-history", rl_noninc_forward_search },
90   { "non-incremental-reverse-search-history", rl_noninc_reverse_search },
91   { "non-incremental-forward-search-history-again", rl_noninc_forward_search_again },
92   { "non-incremental-reverse-search-history-again", rl_noninc_reverse_search_again },
93   { "possible-completions", rl_possible_completions },
94   { "previous-history", rl_get_previous_history },
95   { "quoted-insert", rl_quoted_insert },
96   { "re-read-init-file", rl_re_read_init_file },
97   { "redraw-current-line", rl_refresh_line},
98   { "reverse-search-history", rl_reverse_search_history },
99   { "revert-line", rl_revert_line },
100   { "self-insert", rl_insert },
101   { "start-kbd-macro", rl_start_kbd_macro },
102   { "tab-insert", rl_tab_insert },
103   { "tilde-expand", rl_tilde_expand },
104   { "transpose-chars", rl_transpose_chars },
105   { "transpose-words", rl_transpose_words },
106   { "tty-status", rl_tty_status },
107   { "undo", rl_undo_command },
108   { "universal-argument", rl_universal_argument },
109   { "unix-line-discard", rl_unix_line_discard },
110   { "unix-word-rubout", rl_unix_word_rubout },
111   { "upcase-word", rl_upcase_word },
112   { "yank", rl_yank },
113   { "yank-last-arg", rl_yank_last_arg },
114   { "yank-nth-arg", rl_yank_nth_arg },
115   { "yank-pop", rl_yank_pop },
116
117 #if defined (VI_MODE)
118   { "vi-append-eol", rl_vi_append_eol },
119   { "vi-append-mode", rl_vi_append_mode },
120   { "vi-arg-digit", rl_vi_arg_digit },
121   { "vi-bWord", rl_vi_bWord },
122   { "vi-bracktype", rl_vi_bracktype },
123   { "vi-bword", rl_vi_bword },
124   { "vi-change-case", rl_vi_change_case },
125   { "vi-change-char", rl_vi_change_char },
126   { "vi-change-to", rl_vi_change_to },
127   { "vi-char-search", rl_vi_char_search },
128   { "vi-column", rl_vi_column },
129   { "vi-comment", rl_vi_comment },
130   { "vi-complete", rl_vi_complete },
131   { "vi-delete", rl_vi_delete },
132   { "vi-delete-to", rl_vi_delete_to },
133   { "vi-eWord", rl_vi_eWord },
134   { "vi-editing-mode", rl_vi_editing_mode },
135   { "vi-end-word", rl_vi_end_word },
136   { "vi-eof-maybe", rl_vi_eof_maybe },
137   { "vi-eword", rl_vi_eword },
138   { "vi-fWord", rl_vi_fWord },
139   { "vi-first-print", rl_vi_first_print },
140   { "vi-fword", rl_vi_fword },
141   { "vi-insert-beg", rl_vi_insert_beg },
142   { "vi-insertion-mode", rl_vi_insertion_mode },
143   { "vi-match", rl_vi_match },
144   { "vi-movement-mode", rl_vi_movement_mode },
145   { "vi-next-word", rl_vi_next_word },
146   { "vi-overstrike", rl_vi_overstrike },
147   { "vi-overstrike-delete", rl_vi_overstrike_delete },
148   { "vi-prev-word", rl_vi_prev_word },
149   { "vi-put", rl_vi_put },
150   { "vi-redo", rl_vi_redo },
151   { "vi-replace", rl_vi_replace },
152   { "vi-search", rl_vi_search },
153   { "vi-search-again", rl_vi_search_again },
154   { "vi-subst", rl_vi_subst },
155   { "vi-tilde-expand", rl_vi_tilde_expand },
156   { "vi-yank-arg", rl_vi_yank_arg },
157   { "vi-yank-to", rl_vi_yank_to },
158 #endif /* VI_MODE */
159
160  {(char *)NULL, (Function *)NULL }
161 };
162
163 rl_add_funmap_entry (name, function)
164      char *name;
165      Function *function;
166 {
167   if (funmap_entry + 2 >= funmap_size)
168     if (!funmap)
169       funmap = (FUNMAP **)xmalloc ((funmap_size = 80) * sizeof (FUNMAP *));
170     else
171       funmap =
172         (FUNMAP **)xrealloc (funmap, (funmap_size += 80) * sizeof (FUNMAP *));
173   
174   funmap[funmap_entry] = (FUNMAP *)xmalloc (sizeof (FUNMAP));
175   funmap[funmap_entry]->name = name;
176   funmap[funmap_entry]->function = function;
177
178   funmap[++funmap_entry] = (FUNMAP *)NULL;
179   return funmap_entry;
180 }
181
182 static int funmap_initialized = 0;
183
184 /* Make the funmap contain all of the default entries. */
185 void
186 rl_initialize_funmap ()
187 {
188   register int i;
189
190   if (funmap_initialized)
191     return;
192
193   for (i = 0; default_funmap[i].name; i++)
194     rl_add_funmap_entry (default_funmap[i].name, default_funmap[i].function);
195
196   funmap_initialized = 1;
197   funmap_program_specific_entry_start = i;
198 }
199
200 /* Produce a NULL terminated array of known function names.  The array
201    is sorted.  The array itself is allocated, but not the strings inside.
202    You should free () the array when you done, but not the pointrs. */
203 char **
204 rl_funmap_names ()
205 {
206   char **result = (char **)NULL;
207   int result_size, result_index;
208
209   result_size = result_index = 0;
210
211   /* Make sure that the function map has been initialized. */
212   rl_initialize_funmap ();
213
214   for (result_index = 0; funmap[result_index]; result_index++)
215     {
216       if (result_index + 2 > result_size)
217         {
218           if (!result)
219             result = (char **)xmalloc ((result_size = 20) * sizeof (char *));
220           else
221             result = (char **)
222               xrealloc (result, (result_size += 20) * sizeof (char *));
223         }
224
225       result[result_index] = funmap[result_index]->name;
226       result[result_index + 1] = (char *)NULL;
227     }
228
229   qsort (result, result_index, sizeof (char *), qsort_string_compare);
230   return (result);
231 }
232
233 /* Stupid comparison routine for qsort () ing strings. */
234 static int
235 qsort_string_compare (s1, s2)
236      register char **s1, **s2;
237 {
238   int r;
239
240   r = **s1 - **s2;
241   if (r == 0)
242     r = strcmp (*s1, *s2);
243   return r;
244 }
245
246 /* Things that mean `Control'. */
247 char *possible_control_prefixes[] = {
248   "Control-", "C-", "CTRL-", (char *)NULL
249 };
250
251 char *possible_meta_prefixes[] = {
252   "Meta", "M-", (char *)NULL
253 };
254
255 #if defined (STATIC_MALLOC)
256 \f
257 /* **************************************************************** */
258 /*                                                                  */
259 /*                      xmalloc and xrealloc ()                     */
260 /*                                                                  */
261 /* **************************************************************** */
262
263 static void memory_error_and_abort ();
264
265 static char *
266 xmalloc (bytes)
267      int bytes;
268 {
269   char *temp = (char *)malloc (bytes);
270
271   if (!temp)
272     memory_error_and_abort ();
273   return (temp);
274 }
275
276 static char *
277 xrealloc (pointer, bytes)
278      char *pointer;
279      int bytes;
280 {
281   char *temp;
282
283   if (!pointer)
284     temp = (char *)malloc (bytes);
285   else
286     temp = (char *)realloc (pointer, bytes);
287
288   if (!temp)
289     memory_error_and_abort ();
290   return (temp);
291 }
292
293 static void
294 memory_error_and_abort ()
295 {
296   fprintf (stderr, "history: Out of virtual memory!\n");
297   abort ();
298 }
299 #endif /* STATIC_MALLOC */