aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2005-06-26 03:04:37 +0000
committerBruce Momjian <bruce@momjian.us>2005-06-26 03:04:37 +0000
commitbb3cce4ec9cc157a8dfc80b5b1770c2beac0a57e (patch)
tree5631b9dda006f8c56f6b1d4f64c72f015adf2468 /src/backend
parentc96375a39b28e54e19fa5c9c2e3dd69c44b7618c (diff)
downloadpostgresql-bb3cce4ec9cc157a8dfc80b5b1770c2beac0a57e.tar.gz
postgresql-bb3cce4ec9cc157a8dfc80b5b1770c2beac0a57e.zip
Add E'' syntax so eventually normal strings can treat backslashes
literally. Add GUC variables: "escape_string_warning" - warn about backslashes in non-E strings "escape_string_syntax" - supports E'' syntax? "standard_compliant_strings" - treats backslashes literally in '' Update code to use E'' when escapes are used.
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/parser/scan.l51
-rw-r--r--src/backend/utils/misc/guc.c35
-rw-r--r--src/backend/utils/misc/postgresql.conf.sample1
3 files changed, 83 insertions, 4 deletions
diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l
index 0a250e8dbe8..4c556a5fba2 100644
--- a/src/backend/parser/scan.l
+++ b/src/backend/parser/scan.l
@@ -24,7 +24,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.125 2005/06/15 16:28:06 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/parser/scan.l,v 1.126 2005/06/26 03:03:38 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -48,7 +48,9 @@
extern YYSTYPE yylval;
static int xcdepth = 0; /* depth of nesting in slash-star comments */
-static char *dolqstart; /* current $foo$ quote start string */
+static char *dolqstart; /* current $foo$ quote start string */
+static bool warn_on_first_escape;
+bool escape_string_warning;
/*
* literalbuf is used to accumulate literal values when multiple rules
@@ -64,6 +66,7 @@ static int literalalloc; /* current allocated buffer size */
static void addlit(char *ytext, int yleng);
static void addlitchar(unsigned char ychar);
static char *litbufdup(void);
+static void check_escape_warning(void);
/*
* When we parse a token that requires multiple lexer rules to process,
@@ -185,6 +188,10 @@ xhinside [^']*
/* National character */
xnstart [nN]{quote}
+/* Quote string does not warn about escapes */
+xestart [eE]{quote}
+xeinside [^']*
+
/* Extended quote
* xqdouble implements embedded quote, ''''
*/
@@ -410,6 +417,13 @@ other .
}
{xqstart} {
+ warn_on_first_escape = true;
+ token_start = yytext;
+ BEGIN(xq);
+ startlit();
+ }
+{xestart} {
+ warn_on_first_escape = false;
token_start = yytext;
BEGIN(xq);
startlit();
@@ -428,14 +442,36 @@ other .
addlit(yytext, yyleng);
}
<xq>{xqescape} {
+ if (yytext[1] == '\'')
+ {
+ if (warn_on_first_escape && escape_string_warning)
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
+ errmsg("Invalid use of \\' in a normal string"),
+ errhint("Use '' to place quotes in strings, or use the escape string syntax (E'').")));
+ }
+ else if (yytext[1] == '\\')
+ {
+ if (warn_on_first_escape && escape_string_warning)
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
+ errmsg("Invalid use of \\\\ in a normal string"),
+ errhint("Use the escape string syntax for backslashes, e.g. E'\\\\'.")));
+ }
+ else
+ check_escape_warning();
addlitchar(unescape_single_char(yytext[1]));
}
<xq>{xqoctesc} {
unsigned char c = strtoul(yytext+1, NULL, 8);
+
+ check_escape_warning();
addlitchar(c);
}
<xq>{xqhexesc} {
unsigned char c = strtoul(yytext+2, NULL, 16);
+
+ check_escape_warning();
addlitchar(c);
}
<xq>{quotecontinue} {
@@ -810,3 +846,14 @@ unescape_single_char(unsigned char c)
return c;
}
}
+
+static void
+check_escape_warning(void)
+{
+ if (warn_on_first_escape && escape_string_warning)
+ ereport(WARNING,
+ (errcode(ERRCODE_INVALID_USE_OF_ESCAPE_CHARACTER),
+ errmsg("Invalid use of escapes in an ordinary string"),
+ errhint("Use the escape string syntax for escapes, e.g. E'\\r\\n'.")));
+ warn_on_first_escape = false; /* warn only once per string */
+}
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 05ce93cf58f..17386049426 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -10,7 +10,7 @@
* Written by Peter Eisentraut <peter_e@gmx.net>.
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.268 2005/06/17 22:32:47 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.269 2005/06/26 03:03:41 momjian Exp $
*
*--------------------------------------------------------------------
*/
@@ -189,7 +189,9 @@ static int max_function_args;
static int max_index_keys;
static int max_identifier_length;
static int block_size;
-static bool integer_datetimes;
+static bool integer_datetimes;
+static bool escape_string_syntax;
+static bool standard_compliant_strings;
/* should be static, but commands/variable.c needs to get at it */
char *session_authorization_string;
@@ -873,6 +875,35 @@ static struct config_bool ConfigureNamesBool[] =
false, NULL, NULL
},
+ {
+ {"escape_string_warning", PGC_USERSET, COMPAT_OPTIONS_PREVIOUS,
+ gettext_noop("Warn about backslash escapes in ordinary, non-escape-syntax strings."),
+ NULL
+ },
+ &escape_string_warning,
+ false, NULL, NULL
+ },
+
+ {
+ {"escape_string_syntax", PGC_INTERNAL, PRESET_OPTIONS,
+ gettext_noop("Escape string syntax (E'') is supported."),
+ NULL,
+ GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+ },
+ &escape_string_syntax,
+ true, NULL, NULL
+ },
+
+ {
+ {"standard_compliant_strings", PGC_INTERNAL, PRESET_OPTIONS,
+ gettext_noop("'' strings treat backslashes literally."),
+ NULL,
+ GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+ },
+ &standard_compliant_strings,
+ false, NULL, NULL
+ },
+
/* End-of-list marker */
{
{NULL, 0, 0, NULL, NULL}, NULL, false, NULL, NULL
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index 4c5a1ed59e3..72f447c76b4 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -330,6 +330,7 @@
#regex_flavor = advanced # advanced, extended, or basic
#sql_inheritance = true
#default_with_oids = false
+#escape_string_warning = false
# - Other Platforms & Clients -