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