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