i-bash/builtins/bind.def

333 lines
7.9 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is bind.def, from which is created bind.c.
It implements the builtin "bind" in Bash.
2009-01-12 13:36:28 +00:00
Copyright (C) 1987-2009 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
This file is part of GNU Bash, the Bourne Again SHell.
2009-01-12 13:36:28 +00:00
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.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
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.
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
1996-08-26 18:22:31 +00:00
$PRODUCES bind.c
1996-12-23 17:02:34 +00:00
#include <config.h>
1996-08-26 18:22:31 +00:00
$BUILTIN bind
$DEPENDS_ON READLINE
$FUNCTION bind_builtin
2002-07-17 14:10:11 +00:00
$SHORT_DOC bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
2009-01-12 13:36:28 +00:00
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
1996-08-26 18:22:31 +00:00
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.
1996-12-23 17:02:34 +00:00
-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
2002-07-17 14:10:11 +00:00
-s List key sequences that invoke macros and their values
in a form that can be reused as input.
2009-01-12 13:36:28 +00:00
-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.
Exit Status:
bind returns 0 unless an unrecognized option is given or an error occurs.
1996-08-26 18:22:31 +00:00
$END
#if defined (READLINE)
1996-12-23 17:02:34 +00:00
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
#include <stdio.h>
1996-08-26 18:22:31 +00:00
#include <errno.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
1996-12-23 17:02:34 +00:00
1996-08-26 18:22:31 +00:00
#include <readline/readline.h>
#include <readline/history.h>
1996-12-23 17:02:34 +00:00
2004-07-27 13:29:18 +00:00
#include "../bashintl.h"
1996-12-23 17:02:34 +00:00
#include "../shell.h"
#include "../bashline.h"
1996-08-26 18:22:31 +00:00
#include "bashgetopt.h"
1996-12-23 17:02:34 +00:00
#include "common.h"
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
static int query_bindings __P((char *));
static int unbind_command __P((char *));
1996-08-26 18:22:31 +00:00
extern int no_line_editing;
#define BIND_RETURN(x) do { return_code = x; goto bind_exit; } while (0)
1998-04-17 19:52:44 +00:00
#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
2000-03-17 21:46:59 +00:00
#define XFLAG 0x1000
1996-08-26 18:22:31 +00:00
int
bind_builtin (list)
WORD_LIST *list;
{
1996-12-23 17:02:34 +00:00
int return_code;
1996-08-26 18:22:31 +00:00
Keymap kmap, saved_keymap;
1996-12-23 17:02:34 +00:00
int flags, opt;
2000-03-17 21:46:59 +00:00
char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
1996-08-26 18:22:31 +00:00
if (no_line_editing)
2009-01-12 13:36:28 +00:00
{
#if 0
builtin_error (_("line editing not enabled"));
return (EXECUTION_FAILURE);
#else
builtin_warning (_("line editing not enabled"));
#endif
}
1996-08-26 18:22:31 +00:00
kmap = saved_keymap = (Keymap) NULL;
1996-12-23 17:02:34 +00:00
flags = 0;
1998-04-17 19:52:44 +00:00
initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
1996-12-23 17:02:34 +00:00
return_code = EXECUTION_SUCCESS;
1996-08-26 18:22:31 +00:00
2009-01-12 13:36:28 +00:00
if (bash_readline_initialized == 0)
1996-08-26 18:22:31 +00:00
initialize_readline ();
2001-11-13 17:56:06 +00:00
begin_unwind_frame ("bind_builtin");
unwind_protect_var (rl_outstream);
1996-08-26 18:22:31 +00:00
rl_outstream = stdout;
reset_internal_getopt ();
2000-03-17 21:46:59 +00:00
while ((opt = internal_getopt (list, "lvpVPsSf:q:u:m:r:x:")) != EOF)
1996-08-26 18:22:31 +00:00
{
switch (opt)
{
case 'l':
1996-12-23 17:02:34 +00:00
flags |= LFLAG;
1996-08-26 18:22:31 +00:00
break;
case 'v':
1996-12-23 17:02:34 +00:00
flags |= VFLAG;
1996-08-26 18:22:31 +00:00
break;
1996-12-23 17:02:34 +00:00
case 'p':
flags |= PFLAG;
1996-08-26 18:22:31 +00:00
break;
case 'f':
1996-12-23 17:02:34 +00:00
flags |= FFLAG;
1996-08-26 18:22:31 +00:00
initfile = list_optarg;
break;
case 'm':
1996-12-23 17:02:34 +00:00
flags |= MFLAG;
1996-08-26 18:22:31 +00:00
map_name = list_optarg;
break;
case 'q':
1996-12-23 17:02:34 +00:00
flags |= QFLAG;
1996-08-26 18:22:31 +00:00
fun_name = list_optarg;
break;
1998-04-17 19:52:44 +00:00
case 'u':
flags |= UFLAG;
unbind_name = list_optarg;
break;
1996-12-23 17:02:34 +00:00
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;
2000-03-17 21:46:59 +00:00
case 'x':
flags |= XFLAG;
cmd_seq = list_optarg;
break;
1996-08-26 18:22:31 +00:00
default:
1996-12-23 17:02:34 +00:00
builtin_usage ();
1996-08-26 18:22:31 +00:00
BIND_RETURN (EX_USAGE);
}
}
list = loptend;
/* First, see if we need to install a special keymap for this
command. Then start on the arguments. */
1996-12-23 17:02:34 +00:00
if ((flags & MFLAG) && map_name)
1996-08-26 18:22:31 +00:00
{
kmap = rl_get_keymap_by_name (map_name);
if (!kmap)
2001-04-06 19:14:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("`%s': invalid keymap name"), map_name);
1996-08-26 18:22:31 +00:00
BIND_RETURN (EXECUTION_FAILURE);
2001-04-06 19:14:31 +00:00
}
1996-08-26 18:22:31 +00:00
}
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 */
1996-12-23 17:02:34 +00:00
if (flags & LFLAG)
rl_list_funmap_names ();
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (flags & PFLAG)
rl_function_dumper (1);
if (flags & PPFLAG)
1996-08-26 18:22:31 +00:00
rl_function_dumper (0);
1996-12-23 17:02:34 +00:00
if (flags & SFLAG)
rl_macro_dumper (1);
if (flags & SSFLAG)
rl_macro_dumper (0);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (flags & VFLAG)
rl_variable_dumper (1);
if (flags & VVFLAG)
rl_variable_dumper (0);
if ((flags & FFLAG) && initfile)
1996-08-26 18:22:31 +00:00
{
if (rl_read_init_file (initfile) != 0)
{
2004-07-27 13:29:18 +00:00
builtin_error (_("%s: cannot read: %s"), initfile, strerror (errno));
1996-08-26 18:22:31 +00:00
BIND_RETURN (EXECUTION_FAILURE);
}
}
1996-12-23 17:02:34 +00:00
if ((flags & QFLAG) && fun_name)
1996-08-26 18:22:31 +00:00
return_code = query_bindings (fun_name);
1998-04-17 19:52:44 +00:00
if ((flags & UFLAG) && unbind_name)
return_code = unbind_command (unbind_name);
1996-12-23 17:02:34 +00:00
if ((flags & RFLAG) && remove_seq)
{
2001-11-13 17:56:06 +00:00
if (rl_set_key (remove_seq, (rl_command_func_t *)NULL, rl_get_keymap ()) != 0)
1996-12-23 17:02:34 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("`%s': cannot unbind"), remove_seq);
1996-12-23 17:02:34 +00:00
BIND_RETURN (EXECUTION_FAILURE);
}
}
2000-03-17 21:46:59 +00:00
if (flags & XFLAG)
return_code = bind_keyseq_to_unix_command (cmd_seq);
1996-08-26 18:22:31 +00:00
/* Process the rest of the arguments as binding specifications. */
while (list)
{
rl_parse_and_bind (list->word->word);
list = list->next;
}
bind_exit:
if (saved_keymap)
rl_set_keymap (saved_keymap);
2001-11-13 17:56:06 +00:00
run_unwind_frame ("bind_builtin");
2009-01-12 13:36:28 +00:00
return (sh_chkwrite (return_code));
1996-08-26 18:22:31 +00:00
}
static int
query_bindings (name)
char *name;
{
2001-11-13 17:56:06 +00:00
rl_command_func_t *function;
1996-08-26 18:22:31 +00:00
char **keyseqs;
int j;
function = rl_named_function (name);
1998-04-17 19:52:44 +00:00
if (function == 0)
1996-08-26 18:22:31 +00:00
{
2004-07-27 13:29:18 +00:00
builtin_error (_("`%s': unknown function name"), name);
1996-08-26 18:22:31 +00:00
return EXECUTION_FAILURE;
}
keyseqs = rl_invoking_keyseqs (function);
if (!keyseqs)
{
2004-07-27 13:29:18 +00:00
printf (_("%s is not bound to any keys.\n"), name);
1996-08-26 18:22:31 +00:00
return EXECUTION_FAILURE;
}
2004-07-27 13:29:18 +00:00
printf (_("%s can be invoked via "), name);
1996-08-26 18:22:31 +00:00
for (j = 0; j < 5 && keyseqs[j]; j++)
printf ("\"%s\"%s", keyseqs[j], keyseqs[j + 1] ? ", " : ".\n");
if (keyseqs[j])
printf ("...\n");
2002-07-17 14:10:11 +00:00
strvec_dispose (keyseqs);
1996-08-26 18:22:31 +00:00
return EXECUTION_SUCCESS;
}
1998-04-17 19:52:44 +00:00
static int
unbind_command (name)
char *name;
{
2001-11-13 17:56:06 +00:00
rl_command_func_t *function;
1998-04-17 19:52:44 +00:00
function = rl_named_function (name);
if (function == 0)
{
2009-01-12 13:36:28 +00:00
builtin_error (_("`%s': unknown function name"), name);
1998-04-17 19:52:44 +00:00
return EXECUTION_FAILURE;
}
rl_unbind_function_in_map (function, rl_get_keymap ());
return EXECUTION_SUCCESS;
}
1996-08-26 18:22:31 +00:00
#endif /* READLINE */