7 Bug-Reported-by: Albert Shih <Albert.Shih@obspm.fr>
8 Bug-Reference-ID: Wed, 5 Mar 2014 23:01:40 +0100
9 Bug-Reference-URL: http://lists.gnu.org/archive/html/bug-bash/2014-03/msg00028.html
13 Patch (apply with `patch -p0'):
15 This patch changes the behavior of programmable completion to compensate
16 for two assumptions made by the bash-completion package. Bash-4.3 changed
17 to dequote the argument to programmable completion only under certain
18 circumstances, to make the behavior of compgen more consistent when run
19 from the command line -- closer to the behavior when run by a shell function
20 run as part of programmable completion. Bash-completion can pass quoted
21 arguments to compgen when the original word to be completed was not quoted,
22 expecting programmable completion to dequote the word before attempting
25 This patch fixes two cases:
27 1. An empty string that bash-completion passes to compgen as a quoted null
30 2. An unquoted word that bash-completion quotes using single quotes or
31 backslashes before passing it to compgen.
33 In these cases, since readline did not detect a quote character in the original
34 word to be completed, bash-4.3
38 @@ -324,6 +324,7 @@ extern char *sh_un_double_quote __P((cha
39 extern char *sh_backslash_quote __P((char *, const char *, int));
40 extern char *sh_backslash_quote_for_double_quotes __P((char *));
41 extern int sh_contains_shell_metas __P((char *));
42 +extern int sh_contains_quotes __P((char *));
44 /* declarations for functions defined in lib/sh/spell.c */
45 extern int spname __P((char *, char *));
46 --- a/lib/sh/shquote.c
47 +++ b/lib/sh/shquote.c
48 @@ -311,3 +311,17 @@ sh_contains_shell_metas (string)
54 +sh_contains_quotes (string)
59 + for (s = string; s && *s; s++)
61 + if (*s == '\'' || *s == '"' || *s == '\\')
68 @@ -183,6 +183,7 @@ ITEMLIST it_variables = { LIST_DYNAMIC,
70 COMPSPEC *pcomp_curcs;
71 const char *pcomp_curcmd;
72 +const char *pcomp_curtxt;
76 @@ -753,6 +754,32 @@ pcomp_filename_completion_function (text
78 dfn = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
80 + /* Intended to solve a mismatched assumption by bash-completion. If
81 + the text to be completed is empty, but bash-completion turns it into
82 + a quoted string ('') assuming that this code will dequote it before
83 + calling readline, do the dequoting. */
84 + else if (iscompgen && iscompleting &&
85 + pcomp_curtxt && *pcomp_curtxt == 0 &&
86 + text && (*text == '\'' || *text == '"') && text[1] == text[0] && text[2] == 0 &&
87 + rl_filename_dequoting_function)
88 + dfn = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
89 + /* Another mismatched assumption by bash-completion. If compgen is being
90 + run as part of bash-completion, and the argument to compgen is not
91 + the same as the word originally passed to the programmable completion
92 + code, dequote the argument if it has quote characters. It's an
93 + attempt to detect when bash-completion is quoting its filename
94 + argument before calling compgen. */
95 + /* We could check whether gen_shell_function_matches is in the call
96 + stack by checking whether the gen-shell-function-matches tag is in
97 + the unwind-protect stack, but there's no function to do that yet.
98 + We could simply check whether we're executing in a function by
99 + checking variable_context, and may end up doing that. */
100 + else if (iscompgen && iscompleting && rl_filename_dequoting_function &&
101 + pcomp_curtxt && text &&
102 + STREQ (pcomp_curtxt, text) == 0 &&
103 + variable_context &&
104 + sh_contains_quotes (text)) /* guess */
105 + dfn = (*rl_filename_dequoting_function) ((char *)text, rl_completion_quote_character);
107 dfn = savestring (text);
109 @@ -1522,7 +1549,7 @@ gen_progcomp_completions (ocmd, cmd, wor
112 COMPSPEC *cs, *oldcs;
113 - const char *oldcmd;
114 + const char *oldcmd, *oldtxt;
117 cs = progcomp_search (ocmd);
118 @@ -1545,14 +1572,17 @@ gen_progcomp_completions (ocmd, cmd, wor
121 oldcmd = pcomp_curcmd;
122 + oldtxt = pcomp_curtxt;
126 + pcomp_curtxt = word;
128 ret = gen_compspec_completions (cs, cmd, word, start, end, foundp);
131 pcomp_curcmd = oldcmd;
132 + pcomp_curtxt = oldtxt;
134 /* We need to conditionally handle setting *retryp here */
139 regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
140 looks for to find the patch level (for the sccs version string). */
142 -#define PATCHLEVEL 9
143 +#define PATCHLEVEL 10
145 #endif /* _PATCHLEVEL_H_ */