home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / D / CLISP / CLISPSRC.TAR / clisp-1995-01-01 / src / readline / funmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-31  |  9.1 KB  |  276 lines

  1. /* funmap.c -- changed by Bruno Haible, 31 October 1993 */
  2.  
  3. /* funmap.c -- attach names to functions. */
  4.  
  5. /* Copyright (C) 1988, 1989, 1991 Free Software Foundation, Inc.
  6.  
  7.    This file is part of GNU Readline, a library for reading lines
  8.    of text with interactive input and history editing.
  9.  
  10.    Readline is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 2 of the License, or
  13.    (at your option) any later version.
  14.  
  15.    Readline is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.    You should have received a copy of the GNU General Public License
  21.    along with this program; if not, write to the Free Software
  22.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. #include "sysdep.h"
  25.  
  26. /* #define STATIC_MALLOC */
  27. #if !defined (STATIC_MALLOC)
  28. extern char *xmalloc RL((int bytes));
  29. extern char *xrealloc RL((void *pointer, int bytes));
  30. #else
  31. static char *xmalloc RL((int bytes));
  32. static char *xrealloc RL((void *pointer, int bytes));
  33. #endif /* STATIC_MALLOC */
  34.  
  35. #include <stdio.h>
  36. #include "readline.h"
  37.  
  38. FUNMAP **funmap = (FUNMAP **)NULL;
  39. static int funmap_size = 0;
  40. static int funmap_entry = 0;
  41.  
  42. /* After initializing the function map, this is the index of the first
  43.    program specific function. */
  44. int funmap_program_specific_entry_start;
  45.  
  46. static FUNMAP default_funmap[] = {
  47.  
  48.   { "abort", (Function *) rl_abort },
  49.   { "accept-line", (Function *) rl_newline },
  50.   { "arrow-key-prefix", (Function *) rl_arrow_keys },
  51.   { "backward-char", (Function *) rl_backward },
  52.   { "backward-delete-char", (Function *) rl_rubout },
  53.   { "backward-kill-line", (Function *) rl_backward_kill_line },
  54.   { "backward-kill-word", (Function *) rl_backward_kill_word },
  55.   { "backward-word", (Function *) rl_backward_word },
  56.   { "beginning-of-history", (Function *) rl_beginning_of_history },
  57.   { "beginning-of-line", (Function *) rl_beg_of_line },
  58.   { "call-last-kbd-macro", (Function *) rl_call_last_kbd_macro },
  59.   { "capitalize-word", (Function *) rl_capitalize_word },
  60.   { "clear-screen", (Function *) rl_clear_screen },
  61.   { "complete", (Function *) rl_complete },
  62.   { "delete-char", (Function *) rl_delete },
  63.   { "digit-argument", (Function *) rl_digit_argument },
  64.   { "do-lowercase-version", (Function *) rl_do_lowercase_version },
  65.   { "downcase-word", (Function *) rl_downcase_word },
  66.   { "dump-functions", (Function *) rl_dump_functions },
  67.   { "end-kbd-macro", (Function *) rl_end_kbd_macro },
  68.   { "end-of-history", (Function *) rl_end_of_history },
  69.   { "end-of-line", (Function *) rl_end_of_line },
  70.   { "forward-char", (Function *) rl_forward },
  71.   { "forward-search-history", (Function *) rl_forward_search_history },
  72.   { "forward-word", (Function *) rl_forward_word },
  73.   { "kill-line", (Function *) rl_kill_line },
  74.   { "kill-word", (Function *) rl_kill_word },
  75.   { "next-history", (Function *) rl_get_next_history },
  76.   { "possible-completions", (Function *) rl_possible_completions },
  77.   { "previous-history", (Function *) rl_get_previous_history },
  78.   { "quoted-insert", (Function *) rl_quoted_insert },
  79.   { "re-read-init-file", (Function *) rl_re_read_init_file },
  80.   { "redraw-current-line", (Function *) rl_refresh_line},
  81.   { "reverse-search-history", (Function *) rl_reverse_search_history },
  82.   { "revert-line", (Function *) rl_revert_line },
  83.   { "self-insert", (Function *) rl_insert },
  84.   { "start-kbd-macro", (Function *) rl_start_kbd_macro },
  85.   { "tab-insert", (Function *) rl_tab_insert },
  86.   { "transpose-chars", (Function *) rl_transpose_chars },
  87.   { "transpose-words", (Function *) rl_transpose_words },
  88.   { "undo", (Function *) rl_undo_command },
  89.   { "universal-argument", (Function *) rl_universal_argument },
  90.   { "unix-line-discard", (Function *) rl_unix_line_discard },
  91.   { "unix-word-rubout", (Function *) rl_unix_word_rubout },
  92.   { "upcase-word", (Function *) rl_upcase_word },
  93.   { "yank", (Function *) rl_yank },
  94.   { "yank-nth-arg", (Function *) rl_yank_nth_arg },
  95.   { "yank-pop", (Function *) rl_yank_pop },
  96.  
  97. #if defined (VI_MODE)
  98.  
  99.   { "vi-append-eol", (Function *) rl_vi_append_eol },
  100.   { "vi-append-mode", (Function *) rl_vi_append_mode },
  101.   { "vi-arg-digit", (Function *) rl_vi_arg_digit },
  102.   { "vi-bWord", (Function *) rl_vi_bWord },
  103.   { "vi-bracktype", (Function *) rl_vi_bracktype },
  104.   { "vi-bword", (Function *) rl_vi_bword },
  105.   { "vi-change-case", (Function *) rl_vi_change_case },
  106.   { "vi-change-char", (Function *) rl_vi_change_char },
  107.   { "vi-change-to", (Function *) rl_vi_change_to },
  108.   { "vi-char-search", (Function *) rl_vi_char_search },
  109.   { "vi-column", (Function *) rl_vi_column },
  110.   { "vi-comment", (Function *) rl_vi_comment },
  111.   { "vi-complete", (Function *) rl_vi_complete },
  112.   { "vi-delete", (Function *) rl_vi_delete },
  113.   { "vi-delete-to", (Function *) rl_vi_delete_to },
  114.   { "vi-dosearch", (Function *) rl_vi_dosearch },
  115.   { "vi-eWord", (Function *) rl_vi_eWord },
  116.   { "vi-editing-mode", (Function *) rl_vi_editing_mode },
  117.   { "vi-end-word", (Function *) rl_vi_end_word },
  118.   { "vi-eof-maybe", (Function *) rl_vi_eof_maybe },
  119.   { "vi-eword", (Function *) rl_vi_eword },
  120.   { "vi-fWord", (Function *) rl_vi_fWord },
  121.   { "vi-first-print", (Function *) rl_vi_first_print },
  122.   { "vi-fword", (Function *) rl_vi_fword },
  123.   { "vi-insert-beg", (Function *) rl_vi_insert_beg },
  124.   { "vi-insertion-mode", (Function *) rl_vi_insertion_mode },
  125.   { "vi-match", (Function *) rl_vi_match },
  126.   { "vi-movement-mode", (Function *) rl_vi_movement_mode },
  127.   { "vi-next-word", (Function *) rl_vi_next_word },
  128.   { "vi-overstrike", (Function *) rl_vi_overstrike },
  129.   { "vi-overstrike-delete", (Function *) rl_vi_overstrike_delete },
  130.   { "vi-prev-word", (Function *) rl_vi_prev_word },
  131.   { "vi-put", (Function *) rl_vi_put },
  132.   { "vi-replace", (Function *) rl_vi_replace },
  133.   { "vi-search", (Function *) rl_vi_search },
  134.   { "vi-search-again", (Function *) rl_vi_search_again },
  135.   { "vi-subst", (Function *) rl_vi_subst },
  136.   { "vi-yank-arg", (Function *) rl_vi_yank_arg },
  137.   { "vi-yank-to", (Function *) rl_vi_yank_to },
  138.  
  139. #endif /* VI_MODE */
  140.  
  141.  {(char *)NULL, (Function *)NULL }
  142. };
  143.  
  144. void
  145. rl_add_funmap_entry (name, function)
  146.      char *name;
  147.      Function *function;
  148. {
  149.   if (funmap_entry + 2 >= funmap_size)
  150.     if (!funmap)
  151.       funmap = (FUNMAP **)xmalloc ((funmap_size = 80) * sizeof (FUNMAP *));
  152.     else
  153.       funmap =
  154.     (FUNMAP **)xrealloc (funmap, (funmap_size += 80) * sizeof (FUNMAP *));
  155.   
  156.   funmap[funmap_entry] = (FUNMAP *)xmalloc (sizeof (FUNMAP));
  157.   funmap[funmap_entry]->name = name;
  158.   funmap[funmap_entry]->function = function;
  159.  
  160.   funmap[++funmap_entry] = (FUNMAP *)NULL;
  161. }
  162.  
  163. static int funmap_initialized = 0;
  164.  
  165. /* Make the funmap contain all of the default entries. */
  166. void
  167. rl_initialize_funmap ()
  168. {
  169.   register int i;
  170.  
  171.   if (funmap_initialized)
  172.     return;
  173.  
  174.   for (i = 0; default_funmap[i].name; i++)
  175.     rl_add_funmap_entry (default_funmap[i].name, default_funmap[i].function);
  176.  
  177.   funmap_initialized = 1;
  178.   funmap_program_specific_entry_start = i;
  179. }
  180.  
  181. /* Stupid comparison routine for qsort () ing strings. */
  182. static int
  183. qsort_string_compare (s1, s2)
  184.      register char **s1, **s2;
  185. {
  186.   return (strcmp (*s1, *s2));
  187. }
  188.  
  189. /* Produce a NULL terminated array of known function names.  The array
  190.    is sorted.  The array itself is allocated, but not the strings inside.
  191.    You should free () the array when you done, but not the pointrs. */
  192. char **
  193. rl_funmap_names ()
  194. {
  195.   char **result = (char **)NULL;
  196.   int result_size, result_index;
  197.  
  198.   result_size = result_index = 0;
  199.  
  200.   /* Make sure that the function map has been initialized. */
  201.   rl_initialize_funmap ();
  202.  
  203.   for (result_index = 0; funmap[result_index]; result_index++)
  204.     {
  205.       if (result_index + 2 > result_size)
  206.     {
  207.       if (!result)
  208.         result = (char **)xmalloc ((result_size = 20) * sizeof (char *));
  209.       else
  210.         result = (char **)
  211.           xrealloc (result, (result_size += 20) * sizeof (char *));
  212.     }
  213.  
  214.       result[result_index] = funmap[result_index]->name;
  215.       result[result_index + 1] = (char *)NULL;
  216.     }
  217.  
  218.   qsort (result, result_index, sizeof (char *), qsort_string_compare);
  219.   return (result);
  220. }
  221.  
  222. /* Things that mean `Control'. */
  223. char *possible_control_prefixes[] = {
  224.   "Control-", "C-", "CTRL-", (char *)NULL
  225. };
  226.  
  227. char *possible_meta_prefixes[] = {
  228.   "Meta", "M-", (char *)NULL
  229. };
  230.  
  231. #if defined (STATIC_MALLOC)
  232.  
  233. /* **************************************************************** */
  234. /*                                    */
  235. /*            xmalloc and xrealloc ()                     */
  236. /*                                    */
  237. /* **************************************************************** */
  238.  
  239. static void memory_error_and_abort RL((void));
  240.  
  241. static char *
  242. xmalloc (bytes)
  243.      int bytes;
  244. {
  245.   char *temp = (char *)malloc (bytes);
  246.  
  247.   if (!temp)
  248.     memory_error_and_abort ();
  249.   return (temp);
  250. }
  251.  
  252. static char *
  253. xrealloc (pointer, bytes)
  254.      char *pointer;
  255.      int bytes;
  256. {
  257.   char *temp;
  258.  
  259.   if (!pointer)
  260.     temp = (char *)malloc (bytes);
  261.   else
  262.     temp = (char *)realloc (pointer, bytes);
  263.  
  264.   if (!temp)
  265.     memory_error_and_abort ();
  266.   return (temp);
  267. }
  268.  
  269. static void
  270. memory_error_and_abort ()
  271. {
  272.   fprintf (stderr, "history: Out of virtual memory!\n");
  273.   abort ();
  274. }
  275. #endif /* STATIC_MALLOC */
  276.