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