Imported from ../bash-1.14.7.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 $BUILTIN bind
25 $DEPENDS_ON READLINE
26 $FUNCTION bind_builtin
27 $SHORT_DOC bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline-function]
28 Bind a key sequence to a Readline function, or to a macro.  The
29 syntax is equivalent to that found in ~/.inputrc, but must be
30 passed as a single argument: bind '"\C-x\C-r": re-read-init-file'.
31 Arguments we accept:
32   -m  keymap         Use `keymap' as the keymap for the duration of this
33                      command.  Acceptable keymap names are emacs,
34                      emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
35                      vi-command, and vi-insert.
36   -l                 List names of functions.
37   -v                 List function names and bindings.
38   -d                 Dump functions and bindings such that they
39                      can be read back in.
40   -f  filename       Read key bindings from FILENAME.
41   -q  function-name  Query about which keys invoke the named function.
42 $END
43
44 #include <stdio.h>
45 #include "../shell.h"
46 #if defined (READLINE)
47 #include <errno.h>
48 #if !defined (errno)
49 extern int errno;
50 #endif /* !errno */
51 #include <readline/readline.h>
52 #include <readline/history.h>
53 #include "bashgetopt.h"
54
55 static int query_bindings ();
56
57 extern int bash_readline_initialized;
58 extern int no_line_editing;
59
60 #define BIND_RETURN(x)  do { return_code = x; goto bind_exit; } while (0)
61
62 #define USAGE "usage: bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline_func]"
63
64 int
65 bind_builtin (list)
66      WORD_LIST *list;
67 {
68   int return_code = EXECUTION_SUCCESS;
69   FILE *old_rl_outstream;
70   Keymap kmap, saved_keymap;
71   int lflag, dflag, fflag, vflag, qflag, mflag, opt;
72   char *initfile, *map_name, *fun_name;
73
74   if (no_line_editing)
75     return (EXECUTION_FAILURE);
76
77   kmap = saved_keymap = (Keymap) NULL;
78   lflag = dflag = vflag = fflag = qflag = mflag = 0;
79   initfile = map_name = fun_name = (char *)NULL;
80
81   if (!bash_readline_initialized)
82     initialize_readline ();
83
84   /* Cannot use unwind_protect_pointer () on "FILE *", it is only
85      guaranteed to work for strings. */
86   /* XXX -- see if we can use unwind_protect here */
87   old_rl_outstream = rl_outstream;
88   rl_outstream = stdout;
89
90   reset_internal_getopt ();  
91   while ((opt = internal_getopt (list, "lvdf:q:m:")) != EOF)
92     {
93       switch (opt)
94         {
95         case 'l':
96           lflag++;
97           break;
98
99         case 'v':
100           vflag++;
101           break;
102
103         case 'd':
104           dflag++;
105           break;
106
107         case 'f':
108           fflag++;
109           initfile = list_optarg;
110           break;
111
112         case 'm':
113           mflag++;
114           map_name = list_optarg;
115           break;
116
117         case 'q':
118           qflag++;
119           fun_name = list_optarg;
120           break;
121
122         default:
123           builtin_error (USAGE);
124           BIND_RETURN (EX_USAGE);
125         }
126     }
127
128   list = loptend;
129
130   /* First, see if we need to install a special keymap for this
131      command.  Then start on the arguments. */
132
133   if (mflag && map_name)
134     {
135       kmap = rl_get_keymap_by_name (map_name);
136       if (!kmap)
137         {
138           builtin_error ("`%s': illegal keymap name", map_name);
139           BIND_RETURN (EXECUTION_FAILURE);
140         }
141     }
142
143   if (kmap)
144     {
145       saved_keymap = rl_get_keymap ();
146       rl_set_keymap (kmap);
147     }
148
149   /* XXX - we need to add exclusive use tests here.  It doesn't make sense
150      to use some of these options together. */
151   /* Now hack the option arguments */
152   if (lflag)
153     rl_list_funmap_names (0);
154
155   if (vflag)
156     rl_function_dumper (0);
157
158   if (dflag)
159     rl_function_dumper (1);
160
161   if (fflag && initfile)
162     {
163       if (rl_read_init_file (initfile) != 0)
164         {
165           builtin_error ("cannot read %s: %s", initfile, strerror (errno));
166           BIND_RETURN (EXECUTION_FAILURE);
167         }
168     }
169
170   if (qflag && fun_name)
171     return_code = query_bindings (fun_name);
172
173   /* Process the rest of the arguments as binding specifications. */
174   while (list)
175     {
176       rl_parse_and_bind (list->word->word);
177       list = list->next;
178     }
179
180  bind_exit:
181   if (saved_keymap)
182     rl_set_keymap (saved_keymap);
183
184   rl_outstream = old_rl_outstream;
185   return (return_code);
186 }
187
188 static int
189 query_bindings (name)
190      char *name;
191 {
192   Function *function;
193   char **keyseqs;
194   int j;
195
196   function = rl_named_function (name);
197   if (!function)
198     {
199       builtin_error ("unknown function name `%s'", name);
200       return EXECUTION_FAILURE;
201     }
202
203   keyseqs = rl_invoking_keyseqs (function);
204
205   if (!keyseqs)
206     {
207       printf ("%s is not bound to any keys.\n", name);
208       return EXECUTION_FAILURE;
209     }
210
211   printf ("%s can be invoked via ", name);
212   for (j = 0; j < 5 && keyseqs[j]; j++)
213     printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
214   if (keyseqs[j])
215     printf ("...\n");
216   free_array (keyseqs);
217   return EXECUTION_SUCCESS;
218 }
219 #endif /* READLINE */