Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / builtins / bind.def
index cdcd6a3..3a7cc4e 100644 (file)
 This file is bind.def, from which is created bind.c.
 It implements the builtin "bind" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2009 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 1, or (at your option) any later
-version.
+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.
+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; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+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 [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline-function]
-Bind a key sequence to a Readline function, or to a macro.  The
-syntax is equivalent to that found in ~/.inputrc, but must be
-passed as a single argument: bind '"\C-x\C-r": re-read-init-file'.
-Arguments we accept:
-  -m  keymap         Use `keymap' as the keymap for the duration of this
+$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.
-  -v                 List function names and bindings.
-  -d                 Dump functions and bindings such that they
-                     can be read back in.
-  -f  filename       Read key bindings from FILENAME.
+  -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
 
-#include <stdio.h>
-#include "../shell.h"
 #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 ();
+static int query_bindings __P((char *));
+static int unbind_command __P((char *));
 
-extern int bash_readline_initialized;
 extern int no_line_editing;
 
 #define BIND_RETURN(x)  do { return_code = x; goto bind_exit; } while (0)
 
-#define USAGE "usage: bind [-lvd] [-m keymap] [-f filename] [-q name] [keyseq:readline_func]"
+#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 = EXECUTION_SUCCESS;
-  FILE *old_rl_outstream;
+  int return_code;
   Keymap kmap, saved_keymap;
-  int lflag, dflag, fflag, vflag, qflag, mflag, opt;
-  char *initfile, *map_name, *fun_name;
+  int flags, opt;
+  char *initfile, *map_name, *fun_name, *unbind_name, *remove_seq, *cmd_seq;
 
   if (no_line_editing)
-    return (EXECUTION_FAILURE);
+    {
+#if 0
+      builtin_error (_("line editing not enabled"));
+      return (EXECUTION_FAILURE);
+#else
+      builtin_warning (_("line editing not enabled"));
+#endif
+    }
 
   kmap = saved_keymap = (Keymap) NULL;
-  lflag = dflag = vflag = fflag = qflag = mflag = 0;
-  initfile = map_name = fun_name = (char *)NULL;
+  flags = 0;
+  initfile = map_name = fun_name = unbind_name = remove_seq = (char *)NULL;
+  return_code = EXECUTION_SUCCESS;
 
-  if (!bash_readline_initialized)
+  if (bash_readline_initialized == 0)
     initialize_readline ();
 
-  /* Cannot use unwind_protect_pointer () on "FILE *", it is only
-     guaranteed to work for strings. */
-  /* XXX -- see if we can use unwind_protect here */
-  old_rl_outstream = rl_outstream;
+  begin_unwind_frame ("bind_builtin");
+  unwind_protect_var (rl_outstream);
+
   rl_outstream = stdout;
 
   reset_internal_getopt ();  
-  while ((opt = internal_getopt (list, "lvdf:q:m:")) != EOF)
+  while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != EOF)
     {
       switch (opt)
        {
        case 'l':
-         lflag++;
+         flags |= LFLAG;
          break;
-
        case 'v':
-         vflag++;
+         flags |= VFLAG;
          break;
-
-       case 'd':
-         dflag++;
+       case 'p':
+         flags |= PFLAG;
          break;
-
        case 'f':
-         fflag++;
+         flags |= FFLAG;
          initfile = list_optarg;
          break;
-
        case 'm':
-         mflag++;
+         flags |= MFLAG;
          map_name = list_optarg;
          break;
-
        case 'q':
-         qflag++;
+         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;
        default:
-         builtin_error (USAGE);
+         builtin_usage ();
          BIND_RETURN (EX_USAGE);
        }
     }
@@ -130,14 +204,14 @@ bind_builtin (list)
   /* First, see if we need to install a special keymap for this
      command.  Then start on the arguments. */
 
-  if (mflag && map_name)
+  if ((flags & MFLAG) && map_name)
     {
       kmap = rl_get_keymap_by_name (map_name);
-      if (!kmap)
-        {
-          builtin_error ("`%s': illegal keymap name", map_name);
+      if (kmap == 0)
+       {
+         builtin_error (_("`%s': invalid keymap name"), map_name);
          BIND_RETURN (EXECUTION_FAILURE);
-        }
+       }
     }
 
   if (kmap)
@@ -149,27 +223,57 @@ bind_builtin (list)
   /* 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 (lflag)
-    rl_list_funmap_names (0);
+  if (flags & LFLAG)
+    rl_list_funmap_names ();
+
+  if (flags & PFLAG)
+    rl_function_dumper (1);
 
-  if (vflag)
+  if (flags & PPFLAG)
     rl_function_dumper (0);
 
-  if (dflag)
-    rl_function_dumper (1);
+  if (flags & SFLAG)
+    rl_macro_dumper (1);
+
+  if (flags & SSFLAG)
+    rl_macro_dumper (0);
+
+  if (flags & VFLAG)
+    rl_variable_dumper (1);
 
-  if (fflag && initfile)
+  if (flags & VVFLAG)
+    rl_variable_dumper (0);
+
+  if ((flags & FFLAG) && initfile)
     {
       if (rl_read_init_file (initfile) != 0)
        {
-         builtin_error ("cannot read %s: %s", initfile, strerror (errno));
+         builtin_error (_("%s: cannot read: %s"), initfile, strerror (errno));
          BIND_RETURN (EXECUTION_FAILURE);
        }
     }
 
-  if (qflag && fun_name)
+  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)
+    {
+      if (rl_bind_keyseq (remove_seq, (rl_command_func_t *)NULL) != 0)
+       {
+         builtin_error (_("`%s': cannot unbind"), remove_seq);
+         BIND_RETURN (EXECUTION_FAILURE);
+       }
+    }
+
+  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)
     {
@@ -181,22 +285,23 @@ bind_builtin (list)
   if (saved_keymap)
     rl_set_keymap (saved_keymap);
 
-  rl_outstream = old_rl_outstream;
-  return (return_code);
+  run_unwind_frame ("bind_builtin");
+
+  return (sh_chkwrite (return_code));
 }
 
 static int
 query_bindings (name)
      char *name;
 {
-  Function *function;
+  rl_command_func_t *function;
   char **keyseqs;
   int j;
 
   function = rl_named_function (name);
-  if (!function)
+  if (function == 0)
     {
-      builtin_error ("unknown function name `%s'", name);
+      builtin_error (_("`%s': unknown function name"), name);
       return EXECUTION_FAILURE;
     }
 
@@ -204,16 +309,33 @@ query_bindings (name)
 
   if (!keyseqs)
     {
-      printf ("%s is not bound to any keys.\n", name);
+      printf (_("%s is not bound to any keys.\n"), name);
       return EXECUTION_FAILURE;
     }
 
-  printf ("%s can be invoked via ", name);
+  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");
-  free_array (keyseqs);
+  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;
 }
 #endif /* READLINE */