e1be552cdcc297ac0c8ac9e01139779d4e477e71
[platform/upstream/bash.git] / lib / 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 #define READLINE_LIBRARY
22
23 #if defined (HAVE_CONFIG_H)
24 #  include "config.h"
25 #endif
26
27 #if defined (HAVE_STDLIB_H)
28 #  include <stdlib.h>
29 #else
30 #  include "ansi_stdlib.h"
31 #endif /* HAVE_STDLIB_H */
32
33 #include "rlconf.h"
34 #include "keymaps.h"
35 #include "emacs_keymap.c"
36
37 #if defined (VI_MODE)
38 #include "vi_keymap.c"
39 #endif
40
41 extern int rl_do_lowercase_version ();
42 extern int rl_rubout (), rl_insert ();
43
44 #if defined (STATIC_MALLOC)
45 static char *xmalloc (), *xrealloc ();
46 #else
47 extern char *xmalloc (), *xrealloc ();
48 #endif /* STATIC_MALLOC */
49
50 /* **************************************************************** */
51 /*                                                                  */
52 /*                    Functions for manipulating Keymaps.           */
53 /*                                                                  */
54 /* **************************************************************** */
55
56
57 /* Return a new, empty keymap.
58    Free it with free() when you are done. */
59 Keymap
60 rl_make_bare_keymap ()
61 {
62   register int i;
63   Keymap keymap = (Keymap)xmalloc (KEYMAP_SIZE * sizeof (KEYMAP_ENTRY));
64
65   for (i = 0; i < KEYMAP_SIZE; i++)
66     {
67       keymap[i].type = ISFUNC;
68       keymap[i].function = (Function *)NULL;
69     }
70
71   for (i = 'A'; i < ('Z' + 1); i++)
72     {
73       keymap[i].type = ISFUNC;
74       keymap[i].function = rl_do_lowercase_version;
75     }
76
77   return (keymap);
78 }
79
80 /* Return a new keymap which is a copy of MAP. */
81 Keymap
82 rl_copy_keymap (map)
83      Keymap map;
84 {
85   register int i;
86   Keymap temp = rl_make_bare_keymap ();
87
88   for (i = 0; i < KEYMAP_SIZE; i++)
89     {
90       temp[i].type = map[i].type;
91       temp[i].function = map[i].function;
92     }
93   return (temp);
94 }
95
96 /* Return a new keymap with the printing characters bound to rl_insert,
97    the uppercase Meta characters bound to run their lowercase equivalents,
98    and the Meta digits bound to produce numeric arguments. */
99 Keymap
100 rl_make_keymap ()
101 {
102   register int i;
103   Keymap newmap;
104
105   newmap = rl_make_bare_keymap ();
106
107   /* All ASCII printing characters are self-inserting. */
108   for (i = ' '; i < 127; i++)
109     newmap[i].function = rl_insert;
110
111   newmap[TAB].function = rl_insert;
112   newmap[RUBOUT].function = rl_rubout;
113   newmap[CTRL('H')].function = rl_rubout;
114
115 #if KEYMAP_SIZE > 128
116   /* Printing characters in some 8-bit character sets. */
117   for (i = 128; i < 160; i++)
118     newmap[i].function = rl_insert;
119
120   /* ISO Latin-1 printing characters should self-insert. */
121   for (i = 160; i < 256; i++)
122     newmap[i].function = rl_insert;
123 #endif /* KEYMAP_SIZE > 128 */
124
125   return (newmap);
126 }
127
128 /* Free the storage associated with MAP. */
129 void
130 rl_discard_keymap (map)
131      Keymap (map);
132 {
133   int i;
134
135   if (!map)
136     return;
137
138   for (i = 0; i < KEYMAP_SIZE; i++)
139     {
140       switch (map[i].type)
141         {
142         case ISFUNC:
143           break;
144
145         case ISKMAP:
146           rl_discard_keymap ((Keymap)map[i].function);
147           break;
148
149         case ISMACR:
150           free ((char *)map[i].function);
151           break;
152         }
153     }
154 }
155
156 #if defined (STATIC_MALLOC)
157 \f
158 /* **************************************************************** */
159 /*                                                                  */
160 /*                      xmalloc and xrealloc ()                     */
161 /*                                                                  */
162 /* **************************************************************** */
163
164 static void memory_error_and_abort ();
165
166 static char *
167 xmalloc (bytes)
168      int bytes;
169 {
170   char *temp = (char *)malloc (bytes);
171
172   if (!temp)
173     memory_error_and_abort ();
174   return (temp);
175 }
176
177 static char *
178 xrealloc (pointer, bytes)
179      char *pointer;
180      int bytes;
181 {
182   char *temp;
183
184   if (!pointer)
185     temp = (char *)malloc (bytes);
186   else
187     temp = (char *)realloc (pointer, bytes);
188
189   if (!temp)
190     memory_error_and_abort ();
191   return (temp);
192 }
193
194 static void
195 memory_error_and_abort ()
196 {
197   fprintf (stderr, "readline: Out of virtual memory!\n");
198   abort ();
199 }
200 #endif /* STATIC_MALLOC */