Imported from ../bash-2.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, 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] [-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   -V                 List variable names and values
46   -v                 List variable names and values in a form that can
47                      be reused as input.
48   -S                 List key sequences that invoke macros and their values
49   -s                 List key sequences that invoke macros and their values in
50                      a form that can be reused as input.
51 $END
52
53 #if defined (READLINE)
54
55 #if defined (HAVE_UNISTD_H)
56 #  include <unistd.h>
57 #endif
58
59 #include <stdio.h>
60 #include <errno.h>
61 #if !defined (errno)
62 extern int errno;
63 #endif /* !errno */
64
65 #include <readline/readline.h>
66 #include <readline/history.h>
67
68 #include "../shell.h"
69 #include "../bashline.h"
70 #include "bashgetopt.h"
71 #include "common.h"
72
73 static int query_bindings ();
74
75 extern int no_line_editing;
76
77 #define BIND_RETURN(x)  do { return_code = x; goto bind_exit; } while (0)
78
79 #define LFLAG   0x01
80 #define PFLAG   0x02
81 #define FFLAG   0x04
82 #define VFLAG   0x08
83 #define QFLAG   0x10
84 #define MFLAG   0x20
85 #define RFLAG   0x40
86 #define PPFLAG  0x80
87 #define VVFLAG  0x100
88 #define SFLAG   0x200
89 #define SSFLAG  0x400
90
91 int
92 bind_builtin (list)
93      WORD_LIST *list;
94 {
95   int return_code;
96   FILE *old_rl_outstream;
97   Keymap kmap, saved_keymap;
98   int flags, opt;
99   char *initfile, *map_name, *fun_name, *remove_seq;
100
101   if (no_line_editing)
102     return (EXECUTION_FAILURE);
103
104   kmap = saved_keymap = (Keymap) NULL;
105   flags = 0;
106   initfile = map_name = fun_name = remove_seq = (char *)NULL;
107   return_code = EXECUTION_SUCCESS;
108
109   if (!bash_readline_initialized)
110     initialize_readline ();
111
112   /* Cannot use unwind_protect_pointer () on "FILE *", it is only
113      guaranteed to work for strings. */
114   /* XXX -- see if we can use unwind_protect here */
115   old_rl_outstream = rl_outstream;
116   rl_outstream = stdout;
117
118   reset_internal_getopt ();  
119   while ((opt = internal_getopt (list, "lvpVPsSf:q:m:r:")) != EOF)
120     {
121       switch (opt)
122         {
123         case 'l':
124           flags |= LFLAG;
125           break;
126         case 'v':
127           flags |= VFLAG;
128           break;
129         case 'p':
130           flags |= PFLAG;
131           break;
132         case 'f':
133           flags |= FFLAG;
134           initfile = list_optarg;
135           break;
136         case 'm':
137           flags |= MFLAG;
138           map_name = list_optarg;
139           break;
140         case 'q':
141           flags |= QFLAG;
142           fun_name = list_optarg;
143           break;
144         case 'r':
145           flags |= RFLAG;
146           remove_seq = list_optarg;
147           break;
148         case 'V':
149           flags |= VVFLAG;
150           break;
151         case 'P':
152           flags |= PPFLAG;
153           break;
154         case 's':
155           flags |= SFLAG;
156           break;
157         case 'S':
158           flags |= SSFLAG;
159           break;
160         default:
161           builtin_usage ();
162           BIND_RETURN (EX_USAGE);
163         }
164     }
165
166   list = loptend;
167
168   /* First, see if we need to install a special keymap for this
169      command.  Then start on the arguments. */
170
171   if ((flags & MFLAG) && map_name)
172     {
173       kmap = rl_get_keymap_by_name (map_name);
174       if (!kmap)
175         {
176           builtin_error ("`%s': invalid keymap name", map_name);
177           BIND_RETURN (EXECUTION_FAILURE);
178         }
179     }
180
181   if (kmap)
182     {
183       saved_keymap = rl_get_keymap ();
184       rl_set_keymap (kmap);
185     }
186
187   /* XXX - we need to add exclusive use tests here.  It doesn't make sense
188      to use some of these options together. */
189   /* Now hack the option arguments */
190   if (flags & LFLAG)
191     rl_list_funmap_names ();
192
193   if (flags & PFLAG)
194     rl_function_dumper (1);
195
196   if (flags & PPFLAG)
197     rl_function_dumper (0);
198
199   if (flags & SFLAG)
200     rl_macro_dumper (1);
201
202   if (flags & SSFLAG)
203     rl_macro_dumper (0);
204
205   if (flags & VFLAG)
206     rl_variable_dumper (1);
207
208   if (flags & VVFLAG)
209     rl_variable_dumper (0);
210
211   if ((flags & FFLAG) && initfile)
212     {
213       if (rl_read_init_file (initfile) != 0)
214         {
215           builtin_error ("cannot read %s: %s", initfile, strerror (errno));
216           BIND_RETURN (EXECUTION_FAILURE);
217         }
218     }
219
220   if ((flags & QFLAG) && fun_name)
221     return_code = query_bindings (fun_name);
222
223   if ((flags & RFLAG) && remove_seq)
224     {
225       if (rl_set_key (remove_seq, (Function *)NULL, rl_get_keymap ()) != 0)
226         {
227           builtin_error ("cannot unbind %s", remove_seq);
228           BIND_RETURN (EXECUTION_FAILURE);
229         }
230     }
231
232   /* Process the rest of the arguments as binding specifications. */
233   while (list)
234     {
235       rl_parse_and_bind (list->word->word);
236       list = list->next;
237     }
238
239  bind_exit:
240   if (saved_keymap)
241     rl_set_keymap (saved_keymap);
242
243   rl_outstream = old_rl_outstream;
244   return (return_code);
245 }
246
247 static int
248 query_bindings (name)
249      char *name;
250 {
251   Function *function;
252   char **keyseqs;
253   int j;
254
255   function = rl_named_function (name);
256   if (!function)
257     {
258       builtin_error ("unknown function name `%s'", name);
259       return EXECUTION_FAILURE;
260     }
261
262   keyseqs = rl_invoking_keyseqs (function);
263
264   if (!keyseqs)
265     {
266       printf ("%s is not bound to any keys.\n", name);
267       return EXECUTION_FAILURE;
268     }
269
270   printf ("%s can be invoked via ", name);
271   for (j = 0; j < 5 && keyseqs[j]; j++)
272     printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
273   if (keyseqs[j])
274     printf ("...\n");
275   free_array (keyseqs);
276   return EXECUTION_SUCCESS;
277 }
278 #endif /* READLINE */