]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/bind.def
6b8ac56b805eefecfd4bffe65eaee95a872c027f
[thirdparty/bash.git] / builtins / bind.def
1 This file is bind.def, from which is created bind.c.
2 It implements the builtin "bind" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES bind.c
23
24 #include <config.h>
25
26 $BUILTIN bind
27 $DEPENDS_ON READLINE
28 $FUNCTION bind_builtin
29 $SHORT_DOC bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [keyseq:readline-function]
30 Bind a key sequence to a Readline function, or to a macro. The
31 syntax is equivalent to that found in ~/.inputrc, but must be
32 passed as a single argument: bind '"\C-x\C-r": re-read-init-file'.
33 Arguments we accept:
34 -m keymap Use `keymap' as the keymap for the duration of this
35 command. Acceptable keymap names are emacs,
36 emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
37 vi-command, and vi-insert.
38 -l List names of functions.
39 -P List function names and bindings.
40 -p List functions and bindings in a form that can be
41 reused as input.
42 -r keyseq Remove the binding for KEYSEQ.
43 -f filename Read key bindings from FILENAME.
44 -q function-name Query about which keys invoke the named function.
45 -u function-name Unbind all keys which are bound to the named function.
46 -V List variable names and values
47 -v List variable names and values in a form that can
48 be reused as input.
49 -S List key sequences that invoke macros and their values
50 -s List key sequences that invoke macros and their values in
51 a form that can be reused as input.
52 $END
53
54 #if defined (READLINE)
55
56 #if defined (HAVE_UNISTD_H)
57 # ifdef _MINIX
58 # include <sys/types.h>
59 # endif
60 # include <unistd.h>
61 #endif
62
63 #include <stdio.h>
64 #include <errno.h>
65 #if !defined (errno)
66 extern int errno;
67 #endif /* !errno */
68
69 #include <readline/readline.h>
70 #include <readline/history.h>
71
72 #include "../shell.h"
73 #include "../bashline.h"
74 #include "bashgetopt.h"
75 #include "common.h"
76
77 static int query_bindings ();
78 static int unbind_command ();
79
80 extern int no_line_editing;
81
82 #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
83
84 #define LFLAG 0x0001
85 #define PFLAG 0x0002
86 #define FFLAG 0x0004
87 #define VFLAG 0x0008
88 #define QFLAG 0x0010
89 #define MFLAG 0x0020
90 #define RFLAG 0x0040
91 #define PPFLAG 0x0080
92 #define VVFLAG 0x0100
93 #define SFLAG 0x0200
94 #define SSFLAG 0x0400
95 #define UFLAG 0x0800
96
97 int
98 bind_builtin (list)
99 WORD_LIST *list;
100 {
101 int return_code;
102 FILE *old_rl_outstream;
103 Keymap kmap, saved_keymap;
104 int flags, opt;
105 char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq;
106
107 if (no_line_editing)
108 return (EXECUTION_FAILURE);
109
110 kmap = saved_keymap = (Keymap) NULL;
111 flags = 0;
112 initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
113 return_code = EXECUTION_SUCCESS;
114
115 if (!bash_readline_initialized)
116 initialize_readline ();
117
118 /* Cannot use unwind_protect_pointer () on "FILE *", it is only
119 guaranteed to work for strings. */
120 /* XXX -- see if we can use unwind_protect here */
121 old_rl_outstream = rl_outstream;
122 rl_outstream = stdout;
123
124 reset_internal_getopt ();
125 while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:")) != EOF)
126 {
127 switch (opt)
128 {
129 case 'l':
130 flags |= LFLAG;
131 break;
132 case 'v':
133 flags |= VFLAG;
134 break;
135 case 'p':
136 flags |= PFLAG;
137 break;
138 case 'f':
139 flags |= FFLAG;
140 initfile = list_optarg;
141 break;
142 case 'm':
143 flags |= MFLAG;
144 map_name = list_optarg;
145 break;
146 case 'q':
147 flags |= QFLAG;
148 fun_name = list_optarg;
149 break;
150 case 'u':
151 flags |= UFLAG;
152 unbind_name = list_optarg;
153 break;
154 case 'r':
155 flags |= RFLAG;
156 remove_seq = list_optarg;
157 break;
158 case 'V':
159 flags |= VVFLAG;
160 break;
161 case 'P':
162 flags |= PPFLAG;
163 break;
164 case 's':
165 flags |= SFLAG;
166 break;
167 case 'S':
168 flags |= SSFLAG;
169 break;
170 default:
171 builtin_usage ();
172 BIND_RETURN (EX_USAGE);
173 }
174 }
175
176 list = loptend;
177
178 /* First, see if we need to install a special keymap for this
179 command. Then start on the arguments. */
180
181 if ((flags & MFLAG) && map_name)
182 {
183 kmap = rl_get_keymap_by_name (map_name);
184 if (!kmap)
185 {
186 builtin_error ("`%s': invalid keymap name", map_name);
187 BIND_RETURN (EXECUTION_FAILURE);
188 }
189 }
190
191 if (kmap)
192 {
193 saved_keymap = rl_get_keymap ();
194 rl_set_keymap (kmap);
195 }
196
197 /* XXX - we need to add exclusive use tests here. It doesn't make sense
198 to use some of these options together. */
199 /* Now hack the option arguments */
200 if (flags & LFLAG)
201 rl_list_funmap_names ();
202
203 if (flags & PFLAG)
204 rl_function_dumper (1);
205
206 if (flags & PPFLAG)
207 rl_function_dumper (0);
208
209 if (flags & SFLAG)
210 rl_macro_dumper (1);
211
212 if (flags & SSFLAG)
213 rl_macro_dumper (0);
214
215 if (flags & VFLAG)
216 rl_variable_dumper (1);
217
218 if (flags & VVFLAG)
219 rl_variable_dumper (0);
220
221 if ((flags & FFLAG) && initfile)
222 {
223 if (rl_read_init_file (initfile) != 0)
224 {
225 builtin_error ("cannot read %s: %s", initfile, strerror (errno));
226 BIND_RETURN (EXECUTION_FAILURE);
227 }
228 }
229
230 if ((flags & QFLAG) && fun_name)
231 return_code = query_bindings (fun_name);
232
233 if ((flags & UFLAG) && unbind_name)
234 return_code = unbind_command (unbind_name);
235
236 if ((flags & RFLAG) && remove_seq)
237 {
238 if (rl_set_key (remove_seq, (Function *)NULL, rl_get_keymap ()) != 0)
239 {
240 builtin_error ("cannot unbind %s", remove_seq);
241 BIND_RETURN (EXECUTION_FAILURE);
242 }
243 }
244
245 /* Process the rest of the arguments as binding specifications. */
246 while (list)
247 {
248 rl_parse_and_bind (list->word->word);
249 list = list->next;
250 }
251
252 bind_exit:
253 if (saved_keymap)
254 rl_set_keymap (saved_keymap);
255
256 rl_outstream = old_rl_outstream;
257 return (return_code);
258 }
259
260 static int
261 query_bindings (name)
262 char *name;
263 {
264 Function *function;
265 char **keyseqs;
266 int j;
267
268 function = rl_named_function (name);
269 if (function == 0)
270 {
271 builtin_error ("unknown function name `%s'", name);
272 return EXECUTION_FAILURE;
273 }
274
275 keyseqs = rl_invoking_keyseqs (function);
276
277 if (!keyseqs)
278 {
279 printf ("%s is not bound to any keys.\n", name);
280 return EXECUTION_FAILURE;
281 }
282
283 printf ("%s can be invoked via ", name);
284 for (j = 0; j < 5 && keyseqs[j]; j++)
285 printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
286 if (keyseqs[j])
287 printf ("...\n");
288 free_array (keyseqs);
289 return EXECUTION_SUCCESS;
290 }
291
292 static int
293 unbind_command (name)
294 char *name;
295 {
296 Function *function;
297
298 function = rl_named_function (name);
299 if (function == 0)
300 {
301 builtin_error ("unknown function name `%s'", name);
302 return EXECUTION_FAILURE;
303 }
304
305 rl_unbind_function_in_map (function, rl_get_keymap ());
306 return EXECUTION_SUCCESS;
307 }
308 #endif /* READLINE */