Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/bind.def |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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-2015 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 | - | ||||||||||||
9 | it under the terms of the GNU General Public License as published by | - | ||||||||||||
10 | the Free Software Foundation, either version 3 of the License, or | - | ||||||||||||
11 | (at your option) any later version. | - | ||||||||||||
12 | - | |||||||||||||
13 | Bash is distributed in the hope that it will be useful, | - | ||||||||||||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | - | ||||||||||||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | - | ||||||||||||
16 | GNU General Public License for more details. | - | ||||||||||||
17 | - | |||||||||||||
18 | You should have received a copy of the GNU General Public License | - | ||||||||||||
19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | - | ||||||||||||
20 | - | |||||||||||||
21 | $PRODUCES bind.c | - | ||||||||||||
22 | - | |||||||||||||
23 | #include <config.h> | - | ||||||||||||
24 | - | |||||||||||||
25 | $BUILTIN bind | - | ||||||||||||
26 | $DEPENDS_ON READLINE | - | ||||||||||||
27 | $FUNCTION bind_builtin | - | ||||||||||||
28 | $SHORT_DOC bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command] | - | ||||||||||||
29 | Set Readline key bindings and variables. | - | ||||||||||||
30 | - | |||||||||||||
31 | Bind a key sequence to a Readline function or a macro, or set a | - | ||||||||||||
32 | Readline variable. The non-option argument syntax is equivalent to | - | ||||||||||||
33 | that found in ~/.inputrc, but must be passed as a single argument: | - | ||||||||||||
34 | e.g., bind '"\C-x\C-r": re-read-init-file'. | - | ||||||||||||
35 | - | |||||||||||||
36 | Options: | - | ||||||||||||
37 | -m keymap Use KEYMAP as the keymap for the duration of this | - | ||||||||||||
38 | command. Acceptable keymap names are emacs, | - | ||||||||||||
39 | emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move, | - | ||||||||||||
40 | vi-command, and vi-insert. | - | ||||||||||||
41 | -l List names of functions. | - | ||||||||||||
42 | -P List function names and bindings. | - | ||||||||||||
43 | -p List functions and bindings in a form that can be | - | ||||||||||||
44 | reused as input. | - | ||||||||||||
45 | -S List key sequences that invoke macros and their values | - | ||||||||||||
46 | -s List key sequences that invoke macros and their values | - | ||||||||||||
47 | in a form that can be reused as input. | - | ||||||||||||
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 | -q function-name Query about which keys invoke the named function. | - | ||||||||||||
52 | -u function-name Unbind all keys which are bound to the named function. | - | ||||||||||||
53 | -r keyseq Remove the binding for KEYSEQ. | - | ||||||||||||
54 | -f filename Read key bindings from FILENAME. | - | ||||||||||||
55 | -x keyseq:shell-command Cause SHELL-COMMAND to be executed when | - | ||||||||||||
56 | KEYSEQ is entered. | - | ||||||||||||
57 | -X List key sequences bound with -x and associated commands | - | ||||||||||||
58 | in a form that can be reused as input. | - | ||||||||||||
59 | - | |||||||||||||
60 | Exit Status: | - | ||||||||||||
61 | bind returns 0 unless an unrecognized option is given or an error occurs. | - | ||||||||||||
62 | $END | - | ||||||||||||
63 | - | |||||||||||||
64 | #if defined (READLINE) | - | ||||||||||||
65 | - | |||||||||||||
66 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||
67 | # ifdef _MINIX | - | ||||||||||||
68 | # include <sys/types.h> | - | ||||||||||||
69 | # endif | - | ||||||||||||
70 | # include <unistd.h> | - | ||||||||||||
71 | #endif | - | ||||||||||||
72 | - | |||||||||||||
73 | #include <stdio.h> | - | ||||||||||||
74 | #include <errno.h> | - | ||||||||||||
75 | #if !defined (errno) | - | ||||||||||||
76 | extern int errno; | - | ||||||||||||
77 | #endif /* !errno */ | - | ||||||||||||
78 | - | |||||||||||||
79 | #include <readline/readline.h> | - | ||||||||||||
80 | #include <readline/history.h> | - | ||||||||||||
81 | - | |||||||||||||
82 | #include "../bashintl.h" | - | ||||||||||||
83 | - | |||||||||||||
84 | #include "../shell.h" | - | ||||||||||||
85 | #include "../bashline.h" | - | ||||||||||||
86 | #include "bashgetopt.h" | - | ||||||||||||
87 | #include "common.h" | - | ||||||||||||
88 | - | |||||||||||||
89 | static int query_bindings __P((char *)); | - | ||||||||||||
90 | static int unbind_command __P((char *)); | - | ||||||||||||
91 | static int unbind_keyseq __P((char *)); | - | ||||||||||||
92 | - | |||||||||||||
93 | #define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0) | - | ||||||||||||
94 | - | |||||||||||||
95 | #define LFLAG 0x0001 | - | ||||||||||||
96 | #define PFLAG 0x0002 | - | ||||||||||||
97 | #define FFLAG 0x0004 | - | ||||||||||||
98 | #define VFLAG 0x0008 | - | ||||||||||||
99 | #define QFLAG 0x0010 | - | ||||||||||||
100 | #define MFLAG 0x0020 | - | ||||||||||||
101 | #define RFLAG 0x0040 | - | ||||||||||||
102 | #define PPFLAG 0x0080 | - | ||||||||||||
103 | #define VVFLAG 0x0100 | - | ||||||||||||
104 | #define SFLAG 0x0200 | - | ||||||||||||
105 | #define SSFLAG 0x0400 | - | ||||||||||||
106 | #define UFLAG 0x0800 | - | ||||||||||||
107 | #define XFLAG 0x1000 | - | ||||||||||||
108 | #define XXFLAG 0x2000 | - | ||||||||||||
109 | - | |||||||||||||
110 | int | - | ||||||||||||
111 | bind_builtin (list) | - | ||||||||||||
112 | WORD_LIST *list; | - | ||||||||||||
113 | { | - | ||||||||||||
114 | int return_code; | - | ||||||||||||
115 | Keymap kmap, saved_keymap; | - | ||||||||||||
116 | int flags, opt; | - | ||||||||||||
117 | char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq, *t; | - | ||||||||||||
118 | - | |||||||||||||
119 | if (no_line_editing)
| 0 | ||||||||||||
120 | { | - | ||||||||||||
121 | #if 0 | - | ||||||||||||
122 | builtin_error (_("line editing not enabled")); | - | ||||||||||||
123 | return (EXECUTION_FAILURE); | - | ||||||||||||
124 | #else | - | ||||||||||||
125 | builtin_warning (_("line editing not enabled")); | - | ||||||||||||
126 | #endif | - | ||||||||||||
127 | } never executed: end of block | 0 | ||||||||||||
128 | - | |||||||||||||
129 | kmap = saved_keymap = (Keymap) NULL; | - | ||||||||||||
130 | flags = 0; | - | ||||||||||||
131 | initfile = map_name = fun_name = unbind_name = remove_seq = cmd_seq = (char *)NULL; | - | ||||||||||||
132 | return_code = EXECUTION_SUCCESS; | - | ||||||||||||
133 | - | |||||||||||||
134 | if (bash_readline_initialized == 0)
| 0 | ||||||||||||
135 | initialize_readline (); never executed: initialize_readline (); | 0 | ||||||||||||
136 | - | |||||||||||||
137 | begin_unwind_frame ("bind_builtin"); | - | ||||||||||||
138 | unwind_protect_var (rl_outstream); | - | ||||||||||||
139 | - | |||||||||||||
140 | rl_outstream = stdout; | - | ||||||||||||
141 | - | |||||||||||||
142 | reset_internal_getopt (); | - | ||||||||||||
143 | while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != -1)
| 0 | ||||||||||||
144 | { | - | ||||||||||||
145 | switch (opt) | - | ||||||||||||
146 | { | - | ||||||||||||
147 | case 'l': never executed: case 'l': | 0 | ||||||||||||
148 | flags |= LFLAG; | - | ||||||||||||
149 | break; never executed: break; | 0 | ||||||||||||
150 | case 'v': never executed: case 'v': | 0 | ||||||||||||
151 | flags |= VFLAG; | - | ||||||||||||
152 | break; never executed: break; | 0 | ||||||||||||
153 | case 'p': never executed: case 'p': | 0 | ||||||||||||
154 | flags |= PFLAG; | - | ||||||||||||
155 | break; never executed: break; | 0 | ||||||||||||
156 | case 'f': never executed: case 'f': | 0 | ||||||||||||
157 | flags |= FFLAG; | - | ||||||||||||
158 | initfile = list_optarg; | - | ||||||||||||
159 | break; never executed: break; | 0 | ||||||||||||
160 | case 'm': never executed: case 'm': | 0 | ||||||||||||
161 | flags |= MFLAG; | - | ||||||||||||
162 | map_name = list_optarg; | - | ||||||||||||
163 | break; never executed: break; | 0 | ||||||||||||
164 | case 'q': never executed: case 'q': | 0 | ||||||||||||
165 | flags |= QFLAG; | - | ||||||||||||
166 | fun_name = list_optarg; | - | ||||||||||||
167 | break; never executed: break; | 0 | ||||||||||||
168 | case 'u': never executed: case 'u': | 0 | ||||||||||||
169 | flags |= UFLAG; | - | ||||||||||||
170 | unbind_name = list_optarg; | - | ||||||||||||
171 | break; never executed: break; | 0 | ||||||||||||
172 | case 'r': never executed: case 'r': | 0 | ||||||||||||
173 | flags |= RFLAG; | - | ||||||||||||
174 | remove_seq = list_optarg; | - | ||||||||||||
175 | break; never executed: break; | 0 | ||||||||||||
176 | case 'V': never executed: case 'V': | 0 | ||||||||||||
177 | flags |= VVFLAG; | - | ||||||||||||
178 | break; never executed: break; | 0 | ||||||||||||
179 | case 'P': never executed: case 'P': | 0 | ||||||||||||
180 | flags |= PPFLAG; | - | ||||||||||||
181 | break; never executed: break; | 0 | ||||||||||||
182 | case 's': never executed: case 's': | 0 | ||||||||||||
183 | flags |= SFLAG; | - | ||||||||||||
184 | break; never executed: break; | 0 | ||||||||||||
185 | case 'S': never executed: case 'S': | 0 | ||||||||||||
186 | flags |= SSFLAG; | - | ||||||||||||
187 | break; never executed: break; | 0 | ||||||||||||
188 | case 'x': never executed: case 'x': | 0 | ||||||||||||
189 | flags |= XFLAG; | - | ||||||||||||
190 | cmd_seq = list_optarg; | - | ||||||||||||
191 | break; never executed: break; | 0 | ||||||||||||
192 | case 'X': never executed: case 'X': | 0 | ||||||||||||
193 | flags |= XXFLAG; | - | ||||||||||||
194 | break; never executed: break; | 0 | ||||||||||||
195 | CASE_HELPOPT; never executed: return (258); never executed: case -99: | 0 | ||||||||||||
196 | default: never executed: default: | 0 | ||||||||||||
197 | builtin_usage (); | - | ||||||||||||
198 | BIND_RETURN (EX_USAGE); never executed: goto bind_exit; | 0 | ||||||||||||
199 | } | - | ||||||||||||
200 | } | - | ||||||||||||
201 | - | |||||||||||||
202 | list = loptend; | - | ||||||||||||
203 | - | |||||||||||||
204 | /* First, see if we need to install a special keymap for this | - | ||||||||||||
205 | command. Then start on the arguments. */ | - | ||||||||||||
206 | - | |||||||||||||
207 | if ((flags & MFLAG) && map_name)
| 0 | ||||||||||||
208 | { | - | ||||||||||||
209 | kmap = rl_get_keymap_by_name (map_name); | - | ||||||||||||
210 | if (kmap == 0)
| 0 | ||||||||||||
211 | { | - | ||||||||||||
212 | builtin_error (_("`%s': invalid keymap name"), map_name); | - | ||||||||||||
213 | BIND_RETURN (EXECUTION_FAILURE); never executed: goto bind_exit; | 0 | ||||||||||||
214 | } | - | ||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||
216 | - | |||||||||||||
217 | if (kmap)
| 0 | ||||||||||||
218 | { | - | ||||||||||||
219 | saved_keymap = rl_get_keymap (); | - | ||||||||||||
220 | rl_set_keymap (kmap); | - | ||||||||||||
221 | } never executed: end of block | 0 | ||||||||||||
222 | - | |||||||||||||
223 | /* XXX - we need to add exclusive use tests here. It doesn't make sense | - | ||||||||||||
224 | to use some of these options together. */ | - | ||||||||||||
225 | /* Now hack the option arguments */ | - | ||||||||||||
226 | if (flags & LFLAG)
| 0 | ||||||||||||
227 | rl_list_funmap_names (); never executed: rl_list_funmap_names (); | 0 | ||||||||||||
228 | - | |||||||||||||
229 | if (flags & PFLAG)
| 0 | ||||||||||||
230 | rl_function_dumper (1); never executed: rl_function_dumper (1); | 0 | ||||||||||||
231 | - | |||||||||||||
232 | if (flags & PPFLAG)
| 0 | ||||||||||||
233 | rl_function_dumper (0); never executed: rl_function_dumper (0); | 0 | ||||||||||||
234 | - | |||||||||||||
235 | if (flags & SFLAG)
| 0 | ||||||||||||
236 | rl_macro_dumper (1); never executed: rl_macro_dumper (1); | 0 | ||||||||||||
237 | - | |||||||||||||
238 | if (flags & SSFLAG)
| 0 | ||||||||||||
239 | rl_macro_dumper (0); never executed: rl_macro_dumper (0); | 0 | ||||||||||||
240 | - | |||||||||||||
241 | if (flags & VFLAG)
| 0 | ||||||||||||
242 | rl_variable_dumper (1); never executed: rl_variable_dumper (1); | 0 | ||||||||||||
243 | - | |||||||||||||
244 | if (flags & VVFLAG)
| 0 | ||||||||||||
245 | rl_variable_dumper (0); never executed: rl_variable_dumper (0); | 0 | ||||||||||||
246 | - | |||||||||||||
247 | if ((flags & FFLAG) && initfile)
| 0 | ||||||||||||
248 | { | - | ||||||||||||
249 | if (rl_read_init_file (initfile) != 0)
| 0 | ||||||||||||
250 | { | - | ||||||||||||
251 | t = printable_filename (initfile, 0); | - | ||||||||||||
252 | builtin_error (_("%s: cannot read: %s"), t, strerror (errno)); | - | ||||||||||||
253 | if (t != initfile)
| 0 | ||||||||||||
254 | free (t); never executed: sh_xfree((t), "./bind.def", 254); | 0 | ||||||||||||
255 | BIND_RETURN (EXECUTION_FAILURE); never executed: goto bind_exit; | 0 | ||||||||||||
256 | } | - | ||||||||||||
257 | } never executed: end of block | 0 | ||||||||||||
258 | - | |||||||||||||
259 | if ((flags & QFLAG) && fun_name)
| 0 | ||||||||||||
260 | return_code = query_bindings (fun_name); never executed: return_code = query_bindings (fun_name); | 0 | ||||||||||||
261 | - | |||||||||||||
262 | if ((flags & UFLAG) && unbind_name)
| 0 | ||||||||||||
263 | return_code = unbind_command (unbind_name); never executed: return_code = unbind_command (unbind_name); | 0 | ||||||||||||
264 | - | |||||||||||||
265 | if ((flags & RFLAG) && remove_seq)
| 0 | ||||||||||||
266 | { | - | ||||||||||||
267 | opt = unbind_keyseq (remove_seq); | - | ||||||||||||
268 | BIND_RETURN (opt); never executed: goto bind_exit; | 0 | ||||||||||||
269 | } | - | ||||||||||||
270 | - | |||||||||||||
271 | if (flags & XFLAG)
| 0 | ||||||||||||
272 | return_code = bind_keyseq_to_unix_command (cmd_seq); never executed: return_code = bind_keyseq_to_unix_command (cmd_seq); | 0 | ||||||||||||
273 | - | |||||||||||||
274 | if (flags & XXFLAG)
| 0 | ||||||||||||
275 | return_code = print_unix_command_map (); never executed: return_code = print_unix_command_map (); | 0 | ||||||||||||
276 | - | |||||||||||||
277 | /* Process the rest of the arguments as binding specifications. */ | - | ||||||||||||
278 | while (list)
| 0 | ||||||||||||
279 | { | - | ||||||||||||
280 | rl_parse_and_bind (list->word->word); | - | ||||||||||||
281 | list = list->next; | - | ||||||||||||
282 | } never executed: end of block | 0 | ||||||||||||
283 | - | |||||||||||||
284 | bind_exit: code before this statement never executed: bind_exit: | 0 | ||||||||||||
285 | if (saved_keymap)
| 0 | ||||||||||||
286 | rl_set_keymap (saved_keymap); never executed: rl_set_keymap (saved_keymap); | 0 | ||||||||||||
287 | - | |||||||||||||
288 | run_unwind_frame ("bind_builtin"); | - | ||||||||||||
289 | - | |||||||||||||
290 | return (sh_chkwrite (return_code)); never executed: return (sh_chkwrite (return_code)); | 0 | ||||||||||||
291 | } | - | ||||||||||||
292 | - | |||||||||||||
293 | static int | - | ||||||||||||
294 | query_bindings (name) | - | ||||||||||||
295 | char *name; | - | ||||||||||||
296 | { | - | ||||||||||||
297 | rl_command_func_t *function; | - | ||||||||||||
298 | char **keyseqs; | - | ||||||||||||
299 | int j; | - | ||||||||||||
300 | - | |||||||||||||
301 | function = rl_named_function (name); | - | ||||||||||||
302 | if (function == 0)
| 0 | ||||||||||||
303 | { | - | ||||||||||||
304 | builtin_error (_("`%s': unknown function name"), name); | - | ||||||||||||
305 | return EXECUTION_FAILURE; never executed: return 1; | 0 | ||||||||||||
306 | } | - | ||||||||||||
307 | - | |||||||||||||
308 | keyseqs = rl_invoking_keyseqs (function); | - | ||||||||||||
309 | - | |||||||||||||
310 | if (!keyseqs)
| 0 | ||||||||||||
311 | { | - | ||||||||||||
312 | printf (_("%s is not bound to any keys.\n"), name); | - | ||||||||||||
313 | return EXECUTION_FAILURE; never executed: return 1; | 0 | ||||||||||||
314 | } | - | ||||||||||||
315 | - | |||||||||||||
316 | printf (_("%s can be invoked via "), name); | - | ||||||||||||
317 | for (j = 0; j < 5 && keyseqs[j]; j++)
| 0 | ||||||||||||
318 | printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n"); never executed: printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n"); | 0 | ||||||||||||
319 | if (keyseqs[j])
| 0 | ||||||||||||
320 | printf ("...\n"); never executed: printf ("...\n"); | 0 | ||||||||||||
321 | strvec_dispose (keyseqs); | - | ||||||||||||
322 | return EXECUTION_SUCCESS; never executed: return 0; | 0 | ||||||||||||
323 | } | - | ||||||||||||
324 | - | |||||||||||||
325 | static int | - | ||||||||||||
326 | unbind_command (name) | - | ||||||||||||
327 | char *name; | - | ||||||||||||
328 | { | - | ||||||||||||
329 | rl_command_func_t *function; | - | ||||||||||||
330 | - | |||||||||||||
331 | function = rl_named_function (name); | - | ||||||||||||
332 | if (function == 0)
| 0 | ||||||||||||
333 | { | - | ||||||||||||
334 | builtin_error (_("`%s': unknown function name"), name); | - | ||||||||||||
335 | return EXECUTION_FAILURE; never executed: return 1; | 0 | ||||||||||||
336 | } | - | ||||||||||||
337 | - | |||||||||||||
338 | rl_unbind_function_in_map (function, rl_get_keymap ()); | - | ||||||||||||
339 | return EXECUTION_SUCCESS; never executed: return 0; | 0 | ||||||||||||
340 | } | - | ||||||||||||
341 | - | |||||||||||||
342 | static int | - | ||||||||||||
343 | unbind_keyseq (seq) | - | ||||||||||||
344 | char *seq; | - | ||||||||||||
345 | { | - | ||||||||||||
346 | char *kseq; | - | ||||||||||||
347 | int kslen; | - | ||||||||||||
348 | - | |||||||||||||
349 | kseq = (char *)xmalloc ((2 * strlen (seq)) + 1); | - | ||||||||||||
350 | if (rl_translate_keyseq (seq, kseq, &kslen))
| 0 | ||||||||||||
351 | { | - | ||||||||||||
352 | free (kseq); | - | ||||||||||||
353 | builtin_error (_("`%s': cannot unbind"), seq); | - | ||||||||||||
354 | return EXECUTION_FAILURE; never executed: return 1; | 0 | ||||||||||||
355 | } | - | ||||||||||||
356 | if (rl_function_of_keyseq (kseq, (Keymap)0, (int *)0) == 0)
| 0 | ||||||||||||
357 | { | - | ||||||||||||
358 | free (kseq); | - | ||||||||||||
359 | return (EXECUTION_SUCCESS); never executed: return (0); | 0 | ||||||||||||
360 | } | - | ||||||||||||
361 | - | |||||||||||||
362 | /* I wish this didn't have to translate the key sequence again, but readline | - | ||||||||||||
363 | doesn't have a binding function that takes a translated key sequence as | - | ||||||||||||
364 | an argument. */ | - | ||||||||||||
365 | if (rl_bind_keyseq (seq, (rl_command_func_t *)NULL) != 0)
| 0 | ||||||||||||
366 | { | - | ||||||||||||
367 | free (kseq); | - | ||||||||||||
368 | builtin_error (_("`%s': cannot unbind"), seq); | - | ||||||||||||
369 | return (EXECUTION_FAILURE); never executed: return (1); | 0 | ||||||||||||
370 | } | - | ||||||||||||
371 | - | |||||||||||||
372 | free (kseq); | - | ||||||||||||
373 | return (EXECUTION_SUCCESS); never executed: return (0); | 0 | ||||||||||||
374 | } | - | ||||||||||||
375 | #endif /* READLINE */ | - | ||||||||||||
Source code | Switch to Preprocessed file |