summaryrefslogtreecommitdiffstats
path: root/builtins/bind.def
blob: ccfc08dcd7b1b8862d3e1297dfc44d66454e9edb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
This file is bind.def, from which is created bind.c.
It implements the builtin "bind" in Bash.

Copyright (C) 1987-2020 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Bash.  If not, see <http://www.gnu.org/licenses/>.

$PRODUCES bind.c

#include <config.h>

$BUILTIN bind
$DEPENDS_ON READLINE
$FUNCTION bind_builtin
$SHORT_DOC bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
Set Readline key bindings and variables.

Bind a key sequence to a Readline function or a macro, or set a
Readline variable.  The non-option argument syntax is equivalent to
that found in ~/.inputrc, but must be passed as a single argument:
e.g., bind '"\C-x\C-r": re-read-init-file'.

Options:
  -m  keymap         Use KEYMAP as the keymap for the duration of this
                     command.  Acceptable keymap names are emacs,
                     emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
                     vi-command, and vi-insert.
  -l                 List names of functions.
  -P                 List function names and bindings.
  -p                 List functions and bindings in a form that can be
                     reused as input.
  -S                 List key sequences that invoke macros and their values
  -s                 List key sequences that invoke macros and their values
                     in a form that can be reused as input.
  -V                 List variable names and values
  -v                 List variable names and values in a form that can
                     be reused as input.
  -q  function-name  Query about which keys invoke the named function.
  -u  function-name  Unbind all keys which are bound to the named function.
  -r  keyseq         Remove the binding for KEYSEQ.
  -f  filename       Read key bindings from FILENAME.
  -x  keyseq:shell-command	Cause SHELL-COMMAND to be executed when
				KEYSEQ is entered.
  -X                 List key sequences bound with -x and associated commands
                     in a form that can be reused as input.

Exit Status:
bind returns 0 unless an unrecognized option is given or an error occurs.
$END

#if defined (READLINE)

#if defined (HAVE_UNISTD_H)
#  ifdef _MINIX
#    include <sys/types.h>
#  endif
#  include <unistd.h>
#endif

#include <stdio.h>
#include <errno.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */

#include <readline/readline.h>
#include <readline/history.h>

#include "../bashintl.h"

#include "../shell.h"
#include "../bashline.h"
#include "bashgetopt.h"
#include "common.h"

static int query_bindings PARAMS((char *));
static int unbind_command PARAMS((char *));
static int unbind_keyseq PARAMS((char *));

#define BIND_RETURN(x)  do { return_code = x; goto bind_exit; } while (0)

#define LFLAG	0x0001
#define PFLAG	0x0002
#define FFLAG	0x0004
#define VFLAG	0x0008
#define QFLAG	0x0010
#define MFLAG	0x0020
#define RFLAG	0x0040
#define PPFLAG	0x0080
#define VVFLAG	0x0100
#define SFLAG   0x0200
#define SSFLAG  0x0400
#define UFLAG	0x0800
#define XFLAG	0x1000
#define XXFLAG	0x2000

int
bind_builtin (list)
     WORD_LIST *list;
{
  int return_code;
  Keymap kmap, saved_keymap;
  int flags, opt;
  char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq, *t;

  if (no_line_editing)
    {
#if 0
      builtin_error (_("line editing not enabled"));
      return (EXECUTION_FAILURE);
#else
      builtin_warning (_("line editing not enabled"));
#endif
    }

  kmap = saved_keymap = (Keymap) NULL;
  flags = 0;
  initfile = map_name = fun_name = unbind_name = remove_seq = cmd_seq = (char *)NULL;
  return_code = EXECUTION_SUCCESS;

  if (bash_readline_initialized == 0)
    initialize_readline ();

  begin_unwind_frame ("bind_builtin");
  unwind_protect_var (rl_outstream);

  rl_outstream = stdout;

  reset_internal_getopt ();  
  while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != -1)
    {
      switch (opt)
	{
	case 'l':
	  flags |= LFLAG;
	  break;
	case 'v':
	  flags |= VFLAG;
	  break;
	case 'p':
	  flags |= PFLAG;
	  break;
	case 'f':
	  flags |= FFLAG;
	  initfile = list_optarg;
	  break;
	case 'm':
	  flags |= MFLAG;
	  map_name = list_optarg;
	  break;
	case 'q':
	  flags |= QFLAG;
	  fun_name = list_optarg;
	  break;
	case 'u':
	  flags |= UFLAG;
	  unbind_name = list_optarg;
	  break;
	case 'r':
	  flags |= RFLAG;
	  remove_seq = list_optarg;
	  break;
	case 'V':
	  flags |= VVFLAG;
	  break;
	case 'P':
	  flags |= PPFLAG;
	  break;
	case 's':
	  flags |= SFLAG;
	  break;
	case 'S':
	  flags |= SSFLAG;
	  break;
	case 'x':
	  flags |= XFLAG;
	  cmd_seq = list_optarg;
	  break;
	case 'X':
	  flags |= XXFLAG;
	  break;
	case GETOPT_HELP:
	default:
	  builtin_usage ();
	  BIND_RETURN (EX_USAGE);
	}
    }

  list = loptend;

  /* First, see if we need to install a special keymap for this
     command.  Then start on the arguments. */

  if ((flags & MFLAG) && map_name)
    {
      kmap = rl_get_keymap_by_name (map_name);
      if (kmap == 0)
	{
	  builtin_error (_("`%s': invalid keymap name"), map_name);
	  BIND_RETURN (EXECUTION_FAILURE);
	}
    }

  if (kmap)
    {
      saved_keymap = rl_get_keymap ();
      rl_set_keymap (kmap);
    }

  /* XXX - we need to add exclusive use tests here.  It doesn't make sense
     to use some of these options together. */
  /* Now hack the option arguments */
  if (flags & LFLAG)
    rl_list_funmap_names ();

  if (flags & PFLAG)
    rl_function_dumper (1);

  if (flags & PPFLAG)
    rl_function_dumper (0);

  if (flags & SFLAG)
    rl_macro_dumper (1);

  if (flags & SSFLAG)
    rl_macro_dumper (0);

  if (flags & VFLAG)
    rl_variable_dumper (1);

  if (flags & VVFLAG)
    rl_variable_dumper (0);

  if ((flags & FFLAG) && initfile)
    {
      if (rl_read_init_file (initfile) != 0)
	{
	  t = printable_filename (initfile, 0);
	  builtin_error (_("%s: cannot read: %s"), t, strerror (errno));
	  if (t != initfile)
	    free (t);
	  BIND_RETURN (EXECUTION_FAILURE);
	}
    }

  if ((flags & QFLAG) && fun_name)
    return_code = query_bindings (fun_name);

  if ((flags & UFLAG) && unbind_name)
    return_code = unbind_command (unbind_name);

  if ((flags & RFLAG) && remove_seq)
    {
      opt = unbind_keyseq (remove_seq);
      BIND_RETURN (opt);
    }

  if (flags & XFLAG)
    return_code = bind_keyseq_to_unix_command (cmd_seq);

  if (flags & XXFLAG)
    return_code = print_unix_command_map ();

  /* Process the rest of the arguments as binding specifications. */
  while (list)
    {
      int olen, nlen, d, i;
      char **obindings, **nbindings;

      obindings = rl_invoking_keyseqs (bash_execute_unix_command);
      olen = obindings ? strvec_len (obindings) : 0;

      rl_parse_and_bind (list->word->word);

      nbindings = rl_invoking_keyseqs (bash_execute_unix_command);
      nlen = nbindings ? strvec_len (nbindings) : 0;

      if (nlen < olen)	/* fewer bind -x bindings */
	for (d = olen - nlen, i = 0; i < olen && d > 0; i++)
	  if (nlen == 0 || strvec_search (nbindings, obindings[i]) >= 0)
	    {
	      unbind_unix_command (obindings[i]);
	      d--;
	    }

      strvec_dispose (obindings);
      strvec_dispose (nbindings);

      list = list->next;
    }

 bind_exit:
  if (saved_keymap)
    rl_set_keymap (saved_keymap);

  run_unwind_frame ("bind_builtin");

  if (return_code < 0)
    return_code = EXECUTION_FAILURE;

  return (sh_chkwrite (return_code));
}

static int
query_bindings (name)
     char *name;
{
  rl_command_func_t *function;
  char **keyseqs;
  int j;

  function = rl_named_function (name);
  if (function == 0)
    {
      builtin_error (_("`%s': unknown function name"), name);
      return EXECUTION_FAILURE;
    }

  keyseqs = rl_invoking_keyseqs (function);

  if (!keyseqs)
    {
      printf (_("%s is not bound to any keys.\n"), name);
      return EXECUTION_FAILURE;
    }

  printf (_("%s can be invoked via "), name);
  for (j = 0; j < 5 && keyseqs[j]; j++)
    printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
  if (keyseqs[j])
    printf ("...\n");
  strvec_dispose (keyseqs);
  return EXECUTION_SUCCESS;
}

static int
unbind_command (name)
     char *name;
{
  rl_command_func_t *function;

  function = rl_named_function (name);
  if (function == 0)
    {
      builtin_error (_("`%s': unknown function name"), name);
      return EXECUTION_FAILURE;
    }

  rl_unbind_function_in_map (function, rl_get_keymap ());
  return EXECUTION_SUCCESS;
}

static int
unbind_keyseq (seq)
     char *seq;
{
  char *kseq;
  int kslen, type;
  rl_command_func_t *f;

  kseq = (char *)xmalloc ((2 * strlen (seq)) + 1);
  if (rl_translate_keyseq (seq, kseq, &kslen))
    {
      free (kseq);
      builtin_error (_("`%s': cannot unbind"), seq);
      return EXECUTION_FAILURE;
    }
  if ((f = rl_function_of_keyseq_len (kseq, kslen, (Keymap)0, &type)) == 0)
    {
      free (kseq);
      return (EXECUTION_SUCCESS);
    }
  if (type == ISKMAP)
    f = ((Keymap) f)[ANYOTHERKEY].function;

  /* I wish this didn't have to translate the key sequence again, but readline
     doesn't have a binding function that takes a translated key sequence as
     an argument. */
  if (rl_bind_keyseq (seq, (rl_command_func_t *)NULL) != 0)
    {
      free (kseq);
      builtin_error (_("`%s': cannot unbind"), seq);
      return (EXECUTION_FAILURE);
    }

  if (f == bash_execute_unix_command)
    unbind_unix_command (seq);

  free (kseq);
  return (EXECUTION_SUCCESS);
}
#endif /* READLINE */