home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / readline / funmap.c < prev    next >
C/C++ Source or Header  |  1994-01-26  |  9KB  |  289 lines

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