+
+/* Filename quoting for completion. */
+/* A function to strip quotes that are not protected by backquotes. It
+ allows single quotes to appear within double quotes, and vice versa.
+ It should be smarter. */
+static char *
+bash_dequote_filename (text, quote_char)
+ char *text;
+{
+ char *ret, *p, *r;
+ int l, quoted;
+
+ l = strlen (text);
+ ret = xmalloc (l + 1);
+ for (quoted = quote_char, p = text, r = ret; p && *p; p++)
+ {
+ /* Allow backslash-quoted characters to pass through unscathed. */
+ if (*p == '\\')
+ {
+ *r++ = *++p;
+ if (*p == '\0')
+ break;
+ continue;
+ }
+ /* Close quote. */
+ if (quoted && *p == quoted)
+ {
+ quoted = 0;
+ continue;
+ }
+ /* Open quote. */
+ if (quoted == 0 && (*p == '\'' || *p == '"'))
+ {
+ quoted = *p;
+ continue;
+ }
+ *r++ = *p;
+ }
+ *r = '\0';
+ return ret;
+}
+
+/* Quote a filename using double quotes. */
+static char *
+bash_quote_filename (s, rtype, qcp)
+ char *s;
+ int rtype;
+ char *qcp;
+{
+ char *rtext, *mtext, *ret;
+ int rlen, cs;
+
+ rtext = (char *)NULL;
+
+ /* If RTYPE == MULT_MATCH, it means that there is
+ more than one match. In this case, we do not add
+ the closing quote or attempt to perform tilde
+ expansion. If RTYPE == SINGLE_MATCH, we try
+ to perform tilde expansion, because single and double
+ quotes inhibit tilde expansion by the shell. */
+
+ mtext = s;
+ if (mtext[0] == '~' && rtype == SINGLE_MATCH)
+ mtext = bash_tilde_expand (s);
+
+ cs = completion_quoting_style;
+ /* Might need to modify the default completion style based on *qcp,
+ since it's set to any user-provided opening quote. */
+ if (*qcp == '"')
+ cs = COMPLETE_DQUOTE;
+ else if (*qcp == '\'')
+ cs = COMPLETE_SQUOTE;
+#if defined (BANG_HISTORY)
+ else if (*qcp == '\0' && history_expansion && cs == COMPLETE_DQUOTE &&
+ history_expansion_inhibited == 0 && strchr (mtext, '!'))
+ cs = COMPLETE_BSQUOTE;
+#endif
+
+ switch (cs)
+ {
+ case COMPLETE_DQUOTE:
+ rtext = double_quote (mtext);
+ break;
+ case COMPLETE_SQUOTE:
+ rtext = single_quote (mtext);
+ break;
+ case COMPLETE_BSQUOTE:
+ rtext = backslash_quote (mtext);
+ break;
+ }
+
+ if (mtext != s)
+ free (mtext);
+
+ /* Leave the opening quote intact. The readline completion code takes
+ care of avoiding doubled opening quotes. */
+ rlen = strlen (rtext);
+ ret = xmalloc (rlen + 1);
+ strcpy (ret, rtext);
+
+ /* If there are multiple matches, cut off the closing quote. */
+ if (rtype == MULT_MATCH && cs != COMPLETE_BSQUOTE)
+ ret[rlen - 1] = '\0';
+ free (rtext);
+ return ret;
+}
+
+#endif /* READLINE */