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