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