xref: /openbsd-src/gnu/lib/libreadline/funmap.c (revision 9704b281e65e1189747652d0ba55eee892cff5f7)
11acd27e7Smillert /* funmap.c -- attach names to functions. */
21acd27e7Smillert 
31acd27e7Smillert /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
41acd27e7Smillert 
51acd27e7Smillert    This file is part of the GNU Readline Library, a library for
61acd27e7Smillert    reading lines of text with interactive input and history editing.
71acd27e7Smillert 
81acd27e7Smillert    The GNU Readline Library is free software; you can redistribute it
91acd27e7Smillert    and/or modify it under the terms of the GNU General Public License
101acd27e7Smillert    as published by the Free Software Foundation; either version 2, or
111acd27e7Smillert    (at your option) any later version.
121acd27e7Smillert 
131acd27e7Smillert    The GNU Readline Library is distributed in the hope that it will be
141acd27e7Smillert    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
151acd27e7Smillert    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161acd27e7Smillert    GNU General Public License for more details.
171acd27e7Smillert 
181acd27e7Smillert    The GNU General Public License is often shipped with GNU software, and
191acd27e7Smillert    is generally kept in a file called COPYING or LICENSE.  If you do not
201acd27e7Smillert    have a copy of the license, write to the Free Software Foundation,
211acd27e7Smillert    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
221acd27e7Smillert #define READLINE_LIBRARY
231acd27e7Smillert 
241acd27e7Smillert #if defined (HAVE_CONFIG_H)
251acd27e7Smillert #  include <config.h>
261acd27e7Smillert #endif
271acd27e7Smillert 
281acd27e7Smillert #if !defined (BUFSIZ)
291acd27e7Smillert #include <stdio.h>
301acd27e7Smillert #endif /* BUFSIZ */
311acd27e7Smillert 
321acd27e7Smillert #if defined (HAVE_STDLIB_H)
331acd27e7Smillert #  include <stdlib.h>
341acd27e7Smillert #else
351acd27e7Smillert #  include "ansi_stdlib.h"
361acd27e7Smillert #endif /* HAVE_STDLIB_H */
371acd27e7Smillert 
381acd27e7Smillert #include "rlconf.h"
391acd27e7Smillert #include "readline.h"
401acd27e7Smillert 
411acd27e7Smillert #include "xmalloc.h"
421acd27e7Smillert 
431acd27e7Smillert #ifdef __STDC__
441acd27e7Smillert typedef int QSFUNC (const void *, const void *);
451acd27e7Smillert #else
461acd27e7Smillert typedef int QSFUNC ();
471acd27e7Smillert #endif
481acd27e7Smillert 
49*15b117eaSkettenis extern int _rl_qsort_string_compare PARAMS((char **, char **));
501acd27e7Smillert 
511acd27e7Smillert FUNMAP **funmap;
521acd27e7Smillert static int funmap_size;
531acd27e7Smillert static int funmap_entry;
541acd27e7Smillert 
551acd27e7Smillert /* After initializing the function map, this is the index of the first
561acd27e7Smillert    program specific function. */
571acd27e7Smillert int funmap_program_specific_entry_start;
581acd27e7Smillert 
591acd27e7Smillert static FUNMAP default_funmap[] = {
601acd27e7Smillert   { "abort", rl_abort },
611acd27e7Smillert   { "accept-line", rl_newline },
621acd27e7Smillert   { "arrow-key-prefix", rl_arrow_keys },
63*15b117eaSkettenis   { "backward-byte", rl_backward_byte },
64*15b117eaSkettenis   { "backward-char", rl_backward_char },
651acd27e7Smillert   { "backward-delete-char", rl_rubout },
661acd27e7Smillert   { "backward-kill-line", rl_backward_kill_line },
671acd27e7Smillert   { "backward-kill-word", rl_backward_kill_word },
681acd27e7Smillert   { "backward-word", rl_backward_word },
691acd27e7Smillert   { "beginning-of-history", rl_beginning_of_history },
701acd27e7Smillert   { "beginning-of-line", rl_beg_of_line },
711acd27e7Smillert   { "call-last-kbd-macro", rl_call_last_kbd_macro },
721acd27e7Smillert   { "capitalize-word", rl_capitalize_word },
731acd27e7Smillert   { "character-search", rl_char_search },
741acd27e7Smillert   { "character-search-backward", rl_backward_char_search },
751acd27e7Smillert   { "clear-screen", rl_clear_screen },
761acd27e7Smillert   { "complete", rl_complete },
771acd27e7Smillert   { "copy-backward-word", rl_copy_backward_word },
781acd27e7Smillert   { "copy-forward-word", rl_copy_forward_word },
791acd27e7Smillert   { "copy-region-as-kill", rl_copy_region_to_kill },
801acd27e7Smillert   { "delete-char", rl_delete },
811acd27e7Smillert   { "delete-char-or-list", rl_delete_or_show_completions },
821acd27e7Smillert   { "delete-horizontal-space", rl_delete_horizontal_space },
831acd27e7Smillert   { "digit-argument", rl_digit_argument },
841acd27e7Smillert   { "do-lowercase-version", rl_do_lowercase_version },
851acd27e7Smillert   { "downcase-word", rl_downcase_word },
861acd27e7Smillert   { "dump-functions", rl_dump_functions },
871acd27e7Smillert   { "dump-macros", rl_dump_macros },
881acd27e7Smillert   { "dump-variables", rl_dump_variables },
891acd27e7Smillert   { "emacs-editing-mode", rl_emacs_editing_mode },
901acd27e7Smillert   { "end-kbd-macro", rl_end_kbd_macro },
911acd27e7Smillert   { "end-of-history", rl_end_of_history },
921acd27e7Smillert   { "end-of-line", rl_end_of_line },
931acd27e7Smillert   { "exchange-point-and-mark", rl_exchange_point_and_mark },
941acd27e7Smillert   { "forward-backward-delete-char", rl_rubout_or_delete },
95*15b117eaSkettenis   { "forward-byte", rl_forward_byte },
96*15b117eaSkettenis   { "forward-char", rl_forward_char },
971acd27e7Smillert   { "forward-search-history", rl_forward_search_history },
981acd27e7Smillert   { "forward-word", rl_forward_word },
991acd27e7Smillert   { "history-search-backward", rl_history_search_backward },
1001acd27e7Smillert   { "history-search-forward", rl_history_search_forward },
1011acd27e7Smillert   { "insert-comment", rl_insert_comment },
1021acd27e7Smillert   { "insert-completions", rl_insert_completions },
1031acd27e7Smillert   { "kill-whole-line", rl_kill_full_line },
1041acd27e7Smillert   { "kill-line", rl_kill_line },
1051acd27e7Smillert   { "kill-region", rl_kill_region },
1061acd27e7Smillert   { "kill-word", rl_kill_word },
1071acd27e7Smillert   { "menu-complete", rl_menu_complete },
1081acd27e7Smillert   { "next-history", rl_get_next_history },
1091acd27e7Smillert   { "non-incremental-forward-search-history", rl_noninc_forward_search },
1101acd27e7Smillert   { "non-incremental-reverse-search-history", rl_noninc_reverse_search },
1111acd27e7Smillert   { "non-incremental-forward-search-history-again", rl_noninc_forward_search_again },
1121acd27e7Smillert   { "non-incremental-reverse-search-history-again", rl_noninc_reverse_search_again },
113*15b117eaSkettenis   { "overwrite-mode", rl_overwrite_mode },
114*15b117eaSkettenis #ifdef __CYGWIN__
1151acd27e7Smillert   { "paste-from-clipboard", rl_paste_from_clipboard },
1161acd27e7Smillert #endif
1171acd27e7Smillert   { "possible-completions", rl_possible_completions },
1181acd27e7Smillert   { "previous-history", rl_get_previous_history },
1191acd27e7Smillert   { "quoted-insert", rl_quoted_insert },
1201acd27e7Smillert   { "re-read-init-file", rl_re_read_init_file },
1211acd27e7Smillert   { "redraw-current-line", rl_refresh_line},
1221acd27e7Smillert   { "reverse-search-history", rl_reverse_search_history },
1231acd27e7Smillert   { "revert-line", rl_revert_line },
1241acd27e7Smillert   { "self-insert", rl_insert },
1251acd27e7Smillert   { "set-mark", rl_set_mark },
1261acd27e7Smillert   { "start-kbd-macro", rl_start_kbd_macro },
1271acd27e7Smillert   { "tab-insert", rl_tab_insert },
1281acd27e7Smillert   { "tilde-expand", rl_tilde_expand },
1291acd27e7Smillert   { "transpose-chars", rl_transpose_chars },
1301acd27e7Smillert   { "transpose-words", rl_transpose_words },
1311acd27e7Smillert   { "tty-status", rl_tty_status },
1321acd27e7Smillert   { "undo", rl_undo_command },
1331acd27e7Smillert   { "universal-argument", rl_universal_argument },
1341acd27e7Smillert   { "unix-line-discard", rl_unix_line_discard },
1351acd27e7Smillert   { "unix-word-rubout", rl_unix_word_rubout },
1361acd27e7Smillert   { "upcase-word", rl_upcase_word },
1371acd27e7Smillert   { "yank", rl_yank },
1381acd27e7Smillert   { "yank-last-arg", rl_yank_last_arg },
1391acd27e7Smillert   { "yank-nth-arg", rl_yank_nth_arg },
1401acd27e7Smillert   { "yank-pop", rl_yank_pop },
1411acd27e7Smillert 
1421acd27e7Smillert #if defined (VI_MODE)
1431acd27e7Smillert   { "vi-append-eol", rl_vi_append_eol },
1441acd27e7Smillert   { "vi-append-mode", rl_vi_append_mode },
1451acd27e7Smillert   { "vi-arg-digit", rl_vi_arg_digit },
1461acd27e7Smillert   { "vi-back-to-indent", rl_vi_back_to_indent },
1471acd27e7Smillert   { "vi-bWord", rl_vi_bWord },
1481acd27e7Smillert   { "vi-bword", rl_vi_bword },
1491acd27e7Smillert   { "vi-change-case", rl_vi_change_case },
1501acd27e7Smillert   { "vi-change-char", rl_vi_change_char },
1511acd27e7Smillert   { "vi-change-to", rl_vi_change_to },
1521acd27e7Smillert   { "vi-char-search", rl_vi_char_search },
1531acd27e7Smillert   { "vi-column", rl_vi_column },
1541acd27e7Smillert   { "vi-complete", rl_vi_complete },
1551acd27e7Smillert   { "vi-delete", rl_vi_delete },
1561acd27e7Smillert   { "vi-delete-to", rl_vi_delete_to },
1571acd27e7Smillert   { "vi-eWord", rl_vi_eWord },
1581acd27e7Smillert   { "vi-editing-mode", rl_vi_editing_mode },
1591acd27e7Smillert   { "vi-end-word", rl_vi_end_word },
1601acd27e7Smillert   { "vi-eof-maybe", rl_vi_eof_maybe },
1611acd27e7Smillert   { "vi-eword", rl_vi_eword },
1621acd27e7Smillert   { "vi-fWord", rl_vi_fWord },
1631acd27e7Smillert   { "vi-fetch-history", rl_vi_fetch_history },
1641acd27e7Smillert   { "vi-first-print", rl_vi_first_print },
1651acd27e7Smillert   { "vi-fword", rl_vi_fword },
1661acd27e7Smillert   { "vi-goto-mark", rl_vi_goto_mark },
1671acd27e7Smillert   { "vi-insert-beg", rl_vi_insert_beg },
1681acd27e7Smillert   { "vi-insertion-mode", rl_vi_insertion_mode },
1691acd27e7Smillert   { "vi-match", rl_vi_match },
1701acd27e7Smillert   { "vi-movement-mode", rl_vi_movement_mode },
1711acd27e7Smillert   { "vi-next-word", rl_vi_next_word },
1721acd27e7Smillert   { "vi-overstrike", rl_vi_overstrike },
1731acd27e7Smillert   { "vi-overstrike-delete", rl_vi_overstrike_delete },
1741acd27e7Smillert   { "vi-prev-word", rl_vi_prev_word },
1751acd27e7Smillert   { "vi-put", rl_vi_put },
1761acd27e7Smillert   { "vi-redo", rl_vi_redo },
1771acd27e7Smillert   { "vi-replace", rl_vi_replace },
1781acd27e7Smillert   { "vi-search", rl_vi_search },
1791acd27e7Smillert   { "vi-search-again", rl_vi_search_again },
1801acd27e7Smillert   { "vi-set-mark", rl_vi_set_mark },
1811acd27e7Smillert   { "vi-subst", rl_vi_subst },
1821acd27e7Smillert   { "vi-tilde-expand", rl_vi_tilde_expand },
1831acd27e7Smillert   { "vi-yank-arg", rl_vi_yank_arg },
1841acd27e7Smillert   { "vi-yank-to", rl_vi_yank_to },
1851acd27e7Smillert #endif /* VI_MODE */
1861acd27e7Smillert 
187*15b117eaSkettenis  {(char *)NULL, (rl_command_func_t *)NULL }
1881acd27e7Smillert };
1891acd27e7Smillert 
1901acd27e7Smillert int
rl_add_funmap_entry(name,function)1911acd27e7Smillert rl_add_funmap_entry (name, function)
192*15b117eaSkettenis      const char *name;
193*15b117eaSkettenis      rl_command_func_t *function;
1941acd27e7Smillert {
1951acd27e7Smillert   if (funmap_entry + 2 >= funmap_size)
1961acd27e7Smillert     {
1971acd27e7Smillert       funmap_size += 64;
1981acd27e7Smillert       funmap = (FUNMAP **)xrealloc (funmap, funmap_size * sizeof (FUNMAP *));
1991acd27e7Smillert     }
2001acd27e7Smillert 
2011acd27e7Smillert   funmap[funmap_entry] = (FUNMAP *)xmalloc (sizeof (FUNMAP));
2021acd27e7Smillert   funmap[funmap_entry]->name = name;
2031acd27e7Smillert   funmap[funmap_entry]->function = function;
2041acd27e7Smillert 
2051acd27e7Smillert   funmap[++funmap_entry] = (FUNMAP *)NULL;
2061acd27e7Smillert   return funmap_entry;
2071acd27e7Smillert }
2081acd27e7Smillert 
2091acd27e7Smillert static int funmap_initialized;
2101acd27e7Smillert 
2111acd27e7Smillert /* Make the funmap contain all of the default entries. */
2121acd27e7Smillert void
rl_initialize_funmap()2131acd27e7Smillert rl_initialize_funmap ()
2141acd27e7Smillert {
2151acd27e7Smillert   register int i;
2161acd27e7Smillert 
2171acd27e7Smillert   if (funmap_initialized)
2181acd27e7Smillert     return;
2191acd27e7Smillert 
2201acd27e7Smillert   for (i = 0; default_funmap[i].name; i++)
2211acd27e7Smillert     rl_add_funmap_entry (default_funmap[i].name, default_funmap[i].function);
2221acd27e7Smillert 
2231acd27e7Smillert   funmap_initialized = 1;
2241acd27e7Smillert   funmap_program_specific_entry_start = i;
2251acd27e7Smillert }
2261acd27e7Smillert 
2271acd27e7Smillert /* Produce a NULL terminated array of known function names.  The array
2281acd27e7Smillert    is sorted.  The array itself is allocated, but not the strings inside.
2291acd27e7Smillert    You should free () the array when you done, but not the pointrs. */
230*15b117eaSkettenis const char **
rl_funmap_names()2311acd27e7Smillert rl_funmap_names ()
2321acd27e7Smillert {
233*15b117eaSkettenis   const char **result;
2341acd27e7Smillert   int result_size, result_index;
2351acd27e7Smillert 
2361acd27e7Smillert   /* Make sure that the function map has been initialized. */
2371acd27e7Smillert   rl_initialize_funmap ();
2381acd27e7Smillert 
239*15b117eaSkettenis   for (result_index = result_size = 0, result = (const char **)NULL; funmap[result_index]; result_index++)
2401acd27e7Smillert     {
2411acd27e7Smillert       if (result_index + 2 > result_size)
2421acd27e7Smillert 	{
2431acd27e7Smillert 	  result_size += 20;
244*15b117eaSkettenis 	  result = (const char **)xrealloc (result, result_size * sizeof (char *));
2451acd27e7Smillert 	}
2461acd27e7Smillert 
2471acd27e7Smillert       result[result_index] = funmap[result_index]->name;
2481acd27e7Smillert       result[result_index + 1] = (char *)NULL;
2491acd27e7Smillert     }
2501acd27e7Smillert 
2511acd27e7Smillert   qsort (result, result_index, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
2521acd27e7Smillert   return (result);
2531acd27e7Smillert }
254