bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] fix bind -X quoting


From: Grisha Levit
Subject: [PATCH] fix bind -X quoting
Date: Tue, 18 Jul 2023 02:22:46 -0400

The output of `bind -X' is not reusable if the bound command has quotes,
backslashes, etc.

$ bind -x '"\eX": echo "x"'
$ bind -X
"\eX": "echo \"x\""
$ bind -x "$(bind -X)"
$ bind -X
"\eX": "echo \\\"x\\\""

This patch changes rl_macro_dumper to not untranslate the macro body
when passed a negative print_readably argument.  This is technically an
API change, so maybe not the best fix, though I doubt it will impact any
real usage.
---
 bashline.c                   | 2 +-
 lib/readline/bind.c          | 6 +++++-
 lib/readline/doc/rltech.texi | 4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/bashline.c b/bashline.c
index 2fb00e82..e2b329f9 100644
--- a/bashline.c
+++ b/bashline.c
@@ -4583,7 +4583,7 @@ print_unix_command_map (void)
   save = rl_get_keymap ();
   cmd_xmap = get_cmd_xmap_from_keymap (save);
   rl_set_keymap (cmd_xmap);
-  rl_macro_dumper (1);
+  rl_macro_dumper (-1);
   rl_set_keymap (save);
   return 0;
 }
diff --git a/lib/readline/bind.c b/lib/readline/bind.c
index ee6d6e17..bae0e6cc 100644
--- a/lib/readline/bind.c
+++ b/lib/readline/bind.c
@@ -2861,7 +2861,11 @@ _rl_macro_dumper_internal (int print_readably,
Keymap map, char *prefix)
        {
        case ISMACR:
          keyname = _rl_get_keyname (key);
-         out = _rl_untranslate_macro_value ((char *)map[key].function, 0);
+
+         if (print_readably < 0)
+           out = savestring ((char *)map[key].function);
+         else
+           out = _rl_untranslate_macro_value ((char *)map[key].function, 0);

          if (print_readably)
            fprintf (rl_outstream, "\"%s%s\": \"%s\"\n", prefix ? prefix : "",
diff --git a/lib/readline/doc/rltech.texi b/lib/readline/doc/rltech.texi
index 4d3f3cca..83a34a5f 100644
--- a/lib/readline/doc/rltech.texi
+++ b/lib/readline/doc/rltech.texi
@@ -1354,8 +1354,10 @@ use @code{rl_generic_bind()} instead.
 @deftypefun void rl_macro_dumper (int readable)
 Print the key sequences bound to macros and their values, using
 the current keymap, to @code{rl_outstream}.
-If @var{readable} is non-zero, the list is formatted in such a way
+If @var{readable} is greater than zero, the list is formatted in such a way
 that it can be made part of an @code{inputrc} file and re-read.
+If @var{readable} is less than zero, the macros are printed in "translated"
+form.
 @end deftypefun

 @deftypefun int rl_variable_bind (const char *variable, const char *value)
-- 
2.41.0



reply via email to

[Prev in Thread] Current Thread [Next in Thread]