aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/stringutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-01-23 11:07:12 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2020-01-23 11:07:12 -0500
commitcd69ec66c88633c09bc9a984a7f0930e09c7c96e (patch)
tree201590a789dd3900685535c1db45fe2a508acf76 /src/bin/psql/stringutils.c
parent5ba40b62318e4d941497333b72d589420a48d82f (diff)
downloadpostgresql-cd69ec66c88633c09bc9a984a7f0930e09c7c96e.tar.gz
postgresql-cd69ec66c88633c09bc9a984a7f0930e09c7c96e.zip
Improve psql's tab completion for filenames.
The Readline library contains a fair amount of knowledge about how to tab-complete filenames, but it turns out that that doesn't work too well unless we follow its expectation that we use its filename quoting hooks to quote and de-quote filenames. We were trying to do such quote handling within complete_from_files(), and that's still what we have to do if we're using libedit, which lacks those hooks. But for Readline, it works a lot better if we tell Readline that single-quote is a quoting character and then provide hooks that know the details of the quoting rules for SQL and psql meta-commands. Hence, resurrect the quoting hook functions that existed in the original version of tab-complete.c (and were disabled by commit f6689a328 because they "didn't work so well yet"), and whack on them until they do seem to work well. Notably, this fixes bug #16059 from Steven Winfield, who pointed out that the previous coding would strip quote marks from filenames in SQL COPY commands, even though they're syntactically necessary there. Now, we not only don't do that, but we'll add a quote mark when you tab-complete, even if you didn't type one. Getting this to work across a range of libedit versions (and, to a lesser extent, libreadline versions) was depressingly difficult. It will be interesting to see whether the new regression test cases pass everywhere in the buildfarm. Some future patch might try to handle quoted SQL identifiers with similar explicit quoting/dequoting logic, but that's for another day. Patch by me, reviewed by Peter Eisentraut. Discussion: https://postgr.es/m/16059-8836946734c02b84@postgresql.org
Diffstat (limited to 'src/bin/psql/stringutils.c')
-rw-r--r--src/bin/psql/stringutils.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/bin/psql/stringutils.c b/src/bin/psql/stringutils.c
index c74e58d9125..c521749661c 100644
--- a/src/bin/psql/stringutils.c
+++ b/src/bin/psql/stringutils.c
@@ -282,6 +282,7 @@ strip_quotes(char *source, char quote, char escape, int encoding)
* entails_quote - any of these present? need outer quotes
* quote - doubled within string, affixed to both ends
* escape - doubled within string
+ * force_quote - if true, quote the output even if it doesn't "need" it
* encoding - the active character-set encoding
*
* Do not use this as a substitute for PQescapeStringConn(). Use it for
@@ -289,12 +290,13 @@ strip_quotes(char *source, char quote, char escape, int encoding)
*/
char *
quote_if_needed(const char *source, const char *entails_quote,
- char quote, char escape, int encoding)
+ char quote, char escape, bool force_quote,
+ int encoding)
{
const char *src;
char *ret;
char *dst;
- bool need_quotes = false;
+ bool need_quotes = force_quote;
Assert(source != NULL);
Assert(quote != '\0');