This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / readline / keymaps.c
1 /* keymaps.c -- Functions and keymaps for the GNU Readline library. */
2
3 /* Copyright (C) 1988,1989 Free Software Foundation, Inc.
4
5    This file is part of GNU Readline, a library for reading lines
6    of text with interactive input and history editing.
7
8    Readline is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by the
10    Free Software Foundation; either version 1, or (at your option) any
11    later version.
12
13    Readline is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Readline; see the file COPYING.  If not, write to the Free
20    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21
22 #include "keymaps.h"
23 #include "emacs_keymap.c"
24
25 #ifdef VI_MODE
26 #include "vi_keymap.c"
27 #endif
28
29 /* Remove these declarations when we have a complete libgnu.a. */
30 /* #define STATIC_MALLOC */
31 #if !defined (STATIC_MALLOC)
32 extern char *xmalloc (), *xrealloc ();
33 #else
34 static char *xmalloc (), *xrealloc ();
35 #endif /* STATIC_MALLOC */
36
37 /* **************************************************************** */
38 /*                                                                  */
39 /*                    Functions for manipulating Keymaps.           */
40 /*                                                                  */
41 /* **************************************************************** */
42
43
44 /* Return a new, empty keymap.
45    Free it with free() when you are done. */
46 Keymap
47 rl_make_bare_keymap ()
48 {
49   register int i;
50   Keymap keymap = (Keymap)xmalloc (128 * sizeof (KEYMAP_ENTRY));
51
52   for (i = 0; i < 128; i++)
53     {
54       keymap[i].type = ISFUNC;
55       keymap[i].function = (Function *)NULL;
56     }
57
58   for (i = 'A'; i < ('Z' + 1); i++)
59     {
60       keymap[i].type = ISFUNC;
61       keymap[i].function = rl_do_lowercase_version;
62     }
63
64   return (keymap);
65 }
66
67 /* Return a new keymap which is a copy of MAP. */
68 Keymap
69 rl_copy_keymap (map)
70      Keymap map;
71 {
72   register int i;
73   Keymap temp = rl_make_bare_keymap ();
74
75   for (i = 0; i < 128; i++)
76     {
77       temp[i].type = map[i].type;
78       temp[i].function = map[i].function;
79     }
80   return (temp);
81 }
82
83 /* Return a new keymap with the printing characters bound to rl_insert,
84    the uppercase Meta characters bound to run their lowercase equivalents,
85    and the Meta digits bound to produce numeric arguments. */
86 Keymap
87 rl_make_keymap ()
88 {
89   extern rl_insert (), rl_rubout ();
90   register int i;
91   Keymap newmap;
92
93   newmap = rl_make_bare_keymap ();
94
95   /* All printing characters are self-inserting. */
96   for (i = ' '; i < 126; i++)
97     newmap[i].function = rl_insert;
98
99   newmap[TAB].function = rl_insert;
100   newmap[RUBOUT].function = rl_rubout;
101   newmap[CTRL('H')].function = rl_rubout;
102
103   return (newmap);
104 }
105
106 /* Free the storage associated with MAP. */
107 rl_discard_keymap (map)
108      Keymap (map);
109 {
110   int i;
111
112   if (!map)
113     return;
114
115   for (i = 0; i < 128; i++)
116     {
117       switch (map[i].type)
118         {
119         case ISFUNC:
120           break;
121
122         case ISKMAP:
123           rl_discard_keymap ((Keymap)map[i].function);
124           break;
125
126         case ISMACR:
127           free ((char *)map[i].function);
128           break;
129         }
130     }
131 }
132
133 #ifdef STATIC_MALLOC
134 \f
135 /* **************************************************************** */
136 /*                                                                  */
137 /*                      xmalloc and xrealloc ()                     */
138 /*                                                                  */
139 /* **************************************************************** */
140
141 static void memory_error_and_abort ();
142
143 static char *
144 xmalloc (bytes)
145      int bytes;
146 {
147   char *temp = (char *)malloc (bytes);
148
149   if (!temp)
150     memory_error_and_abort ();
151   return (temp);
152 }
153
154 static char *
155 xrealloc (pointer, bytes)
156      char *pointer;
157      int bytes;
158 {
159   char *temp;
160
161   if (!pointer)
162     temp = (char *)malloc (bytes);
163   else
164     temp = (char *)realloc (pointer, bytes);
165
166   if (!temp)
167     memory_error_and_abort ();
168   return (temp);
169 }
170
171 static void
172 memory_error_and_abort ()
173 {
174   fprintf (stderr, "readline: Out of virtual memory!\n");
175   abort ();
176 }
177 #endif /* STATIC_MALLOC */