From ea1ddcdec4462a96fd192e67908afc42e6cea6cb Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 6 Jan 2016 15:31:27 +0900
Subject: [PATCH 3/5] Detach common.c from psqlscan

Call standard_strings() and psql_error() via callback functions so
that psqlscan.l can live totally without common.c stuff. They are
bundled up with get_variable() callback in one struct since now we
have as many as four callback functions.
---
 src/bin/psql/common.c             |  51 ++++++++++++++++-
 src/bin/psql/common.h             |   8 ++-
 src/bin/psql/mainloop.c           |  10 ++--
 src/bin/psql/psqlscan.c           |   2 +-
 src/bin/psql/psqlscan.h           |  14 ++++-
 src/bin/psql/psqlscan_int.h       |   8 +--
 src/bin/psql/psqlscan_slash.c     |   2 +-
 src/bin/psql/psqlscan_slashbody.l |  50 ++++++++++-------
 src/bin/psql/psqlscanbody.l       | 113 ++++++++++++++++++++------------------
 src/bin/psql/startup.c            |   9 ++-
 10 files changed, 173 insertions(+), 94 deletions(-)

diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 1216d1e..79eb04e 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -30,6 +30,9 @@ static bool ExecQueryUsingCursor(const char *query, double *elapsed_msec);
 static bool command_no_begin(const char *query);
 static bool is_select_command(const char *query);
 
+PsqlScanCallbacks psqlscan_callbacks =
+{&get_variable, &psql_mblen, &standard_strings, &psql_error};
+
 
 /*
  * openQueryOutputFile --- attempt to open a query output file
@@ -1904,7 +1907,51 @@ recognized_connection_string(const char *connstr)
 
 /* Access callback to "shell variables" for lexer */
 const char *
-get_variable(const char *name)
+get_variable(const char *name, bool escape, bool as_ident,
+			 void (**free_func)(void *))
 {
-	return GetVariable(pset.vars, name);
+	const char *value;
+	char   *escaped_value;
+
+	*free_func = NULL;
+
+	value = GetVariable(pset.vars, name);
+
+	if (!escape)
+		return value;
+
+	/* Escaping. */
+
+	if (!value)
+		return NULL;
+
+	if (!pset.db)
+	{
+		psql_error("can't escape without active connection\n");
+		return NULL;
+	}
+
+	if (as_ident)
+		escaped_value =
+			PQescapeIdentifier(pset.db, value, strlen(value));
+	else
+		escaped_value =
+			PQescapeLiteral(pset.db, value, strlen(value));
+
+	if (escaped_value == NULL)
+	{
+		const char *error = PQerrorMessage(pset.db);
+
+		psql_error("%s", error);
+		return NULL;
+	}
+
+	*free_func = &PQfreemem;
+	return escaped_value;
+}
+
+int
+psql_mblen(const char *s)
+{
+	return PQmblen(s, pset.encoding);
 }
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index 636331e..686503a 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -13,6 +13,7 @@
 #include "libpq-fe.h"
 
 #include "print.h"
+#include "psqlscan.h"
 
 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
@@ -29,6 +30,8 @@ extern sigjmp_buf sigint_interrupt_jmp;
 
 extern volatile bool cancel_pressed;
 
+extern PsqlScanCallbacks psqlscan_callbacks;
+
 /* Note: cancel_pressed is defined in print.c, see that file for reasons */
 
 extern void setup_cancel_handler(void);
@@ -49,6 +52,9 @@ extern void expand_tilde(char **filename);
 
 extern bool recognized_connection_string(const char *connstr);
 
-extern const char *get_variable(const char *name);
+extern const char *get_variable(const char *name, bool escape, bool as_ident,
+								void (**free_func)(void *));
+
+extern int psql_mblen(const char *s);
 
 #endif   /* COMMON_H */
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c
index 76b2f78..13424be 100644
--- a/src/bin/psql/mainloop.c
+++ b/src/bin/psql/mainloop.c
@@ -16,7 +16,6 @@
 
 #include "mb/pg_wchar.h"
 
-
 /*
  * Main processing loop for reading lines of input
  *	and sending them to the backend.
@@ -233,8 +232,11 @@ MainLoop(FILE *source)
 		/*
 		 * Parse line, looking for command separators.
 		 */
-		psql_scan_setup(scan_state, line, strlen(line),
-						pset.db, &get_variable, pset.encoding);
+		/* set enc_mblen according to the encoding */
+		psqlscan_callbacks.enc_mblen =
+			(pg_valid_server_encoding_id(pset.encoding) ? NULL : &psql_mblen);
+
+		psql_scan_setup(scan_state, line, strlen(line),	&psqlscan_callbacks);
 		success = true;
 		line_saved_in_history = false;
 
@@ -375,7 +377,7 @@ MainLoop(FILE *source)
 					/* reset parsing state since we are rescanning whole line */
 					psql_scan_reset(scan_state);
 					psql_scan_setup(scan_state, line, strlen(line),
-									pset.db, &get_variable, pset.encoding);
+									&psqlscan_callbacks);
 					line_saved_in_history = false;
 					prompt_status = PROMPT_READY;
 				}
diff --git a/src/bin/psql/psqlscan.c b/src/bin/psql/psqlscan.c
index 7f09fa3..de7f746 100644
--- a/src/bin/psql/psqlscan.c
+++ b/src/bin/psql/psqlscan.c
@@ -14,5 +14,5 @@
  * multiple infrastructures for stdio.h and so on.  flex is absolutely
  * uncooperative about that, so we can't compile psqlscan.c on its own.
  */
-#include "common.h"
+#include "postgres_fe.h"
 #include "psqlscanbody.c"
diff --git a/src/bin/psql/psqlscan.h b/src/bin/psql/psqlscan.h
index 651f074..322edd3 100644
--- a/src/bin/psql/psqlscan.h
+++ b/src/bin/psql/psqlscan.h
@@ -15,8 +15,16 @@
 /* Abstract type for lexer's internal state */
 typedef struct PsqlScanStateData *PsqlScanState;
 
-/* Callback type for retrieve variables */
-typedef const char *(*VariableGetter)(const char *);
+typedef struct PsqlScanCallbacks
+{
+	const char *(*get_variable)(const char *, bool escape, bool as_ident,
+								void (**free_fn)(void *));
+	/* enc_mblen is needed only if encoding is not safe */
+	int	 (*enc_mblen)(const char *);
+	bool (*standard_strings)(void); /* standard_conforming_strings */
+	void (*error_out)(const char *fmt, ...) /* write error message */
+		pg_attribute_printf(1, 2);
+} PsqlScanCallbacks;
 
 /* Termination states for psql_scan() */
 typedef enum
@@ -31,7 +39,7 @@ extern PsqlScanState psql_scan_create(void);
 extern void psql_scan_destroy(PsqlScanState state);
 
 extern void psql_scan_setup(PsqlScanState state, const char *line, int line_len,
-							PGconn *db, VariableGetter getter, int encoding);
+							PsqlScanCallbacks *callbacks);
 extern void psql_scan_finish(PsqlScanState state);
 
 extern PsqlScanResult psql_scan(PsqlScanState state,
diff --git a/src/bin/psql/psqlscan_int.h b/src/bin/psql/psqlscan_int.h
index 6f9fa13..fb6b036 100644
--- a/src/bin/psql/psqlscan_int.h
+++ b/src/bin/psql/psqlscan_int.h
@@ -49,14 +49,12 @@ typedef struct PsqlScanStateData
 	char	   *scanbuf;		/* start of outer-level input buffer */
 	const char *scanline;		/* current input line at outer level */
 
-	/* safe_encoding, curline, refline are used by scan_emit() to replace FFs */
-	PGconn	   *db;				/* active connection */
-	int			encoding;		/* encoding being used now */
-	bool		safe_encoding;	/* is current encoding "safe"? */
 	const char *curline;		/* actual flex input string for cur buf */
 	const char *refline;		/* original data for cur buffer */
 	int			curpos;			/* current position in curline  */
-	VariableGetter get_variable; /* access to the "shell variable" repository */
+
+	PsqlScanCallbacks cb;		/* callback given from outside */
+
 
 	/*
 	 * All this state lives across successive input lines, until explicitly
diff --git a/src/bin/psql/psqlscan_slash.c b/src/bin/psql/psqlscan_slash.c
index 223bde4..bf8c0f3 100644
--- a/src/bin/psql/psqlscan_slash.c
+++ b/src/bin/psql/psqlscan_slash.c
@@ -14,6 +14,6 @@
  * infrastructures for stdio.h and so on.  flex is absolutely uncooperative
  * about that, so we can't compile psqlscan.c on its own.
  */
-#include "common.h"
+#include "postgres_fe.h"
 #include "psqlscan.h"
 #include "psqlscan_slashbody.c"
diff --git a/src/bin/psql/psqlscan_slashbody.l b/src/bin/psql/psqlscan_slashbody.l
index ca0870e..7f2edfd 100644
--- a/src/bin/psql/psqlscan_slashbody.l
+++ b/src/bin/psql/psqlscan_slashbody.l
@@ -36,6 +36,7 @@ static void evaluate_backtick(void);
 /* Track where lexer parsed up to */
 #define YY_USER_ACTION cur_state->curpos += yyleng;
 
+#define ENC_IS_SAFE(s) (!(s)->cb.enc_mblen)
 %}
 
 %option 8bit
@@ -184,13 +185,15 @@ other			.
 						ECHO;
 					else
 					{
-						char   *varname;
+						char	   *varname;
 						const char *value;
+						void	  (*free_fn)(void *) = NULL;
 
-						if (cur_state->get_variable)
+						if (cur_state->cb.get_variable)
 						{
 							varname = extract_substring(yytext + 1, yyleng - 1);
-							value = cur_state->get_variable(varname);
+							value = cur_state->cb.get_variable(varname,
+												   false, false, &free_fn);
 							free(varname);
 						}
 
@@ -202,7 +205,11 @@ other			.
 						 * Note that we needn't guard against recursion here.
 						 */
 						if (value)
+						{
 							appendPQExpBufferStr(output_buf, value);
+							if (free_fn)
+								free_fn((void*)value);
+						}
 						else
 							ECHO;
 
@@ -372,18 +379,19 @@ psql_scan_slash_command_initialize(PsqlScanState state)
  */
 static void
 psql_scan_slash_command_setup(PsqlScanState state,
-						  const char *line, int line_len,
-						  PGconn *db, VariableGetter vargetter, int encoding)
+							  const char *line, int line_len,
+							  PsqlScanCallbacks *cb)
 {
 	/* Mustn't be scanning already */
 	Assert(state->scanbufhandle == NULL);
 	Assert(state->buffer_stack == NULL);
+	Assert(cb->error_out != NULL);
 
-	/* Do we need to hack the character set encoding? */
-	state->encoding = encoding;
-	state->safe_encoding = pg_valid_server_encoding_id(state->encoding);
-
-	state->get_variable = vargetter;
+	/* copy callback functions */
+	state->cb.get_variable = cb->get_variable;
+	state->cb.enc_mblen = cb->enc_mblen;
+	state->cb.standard_strings = cb->standard_strings;
+	state->cb.error_out = cb->error_out;
 
 	/* needed for prepare_buffer */
 	cur_state = state;
@@ -411,13 +419,10 @@ static void
 psql_scan_slash_command_switch_lexer(PsqlScanState state)
 {
 	const char *newscanline = state->scanline + state->curpos;
-	PGconn		   *db = state->db;
-	VariableGetter	vargetter = state->get_variable;
-	int				encoding = state->encoding;
+	PsqlScanCallbacks cb = state->cb;
 
 	psql_scan_slash_command_initialize(state);
-	psql_scan_slash_command_setup(state, newscanline, strlen(newscanline),
-								  db, vargetter, encoding);
+	psql_scan_slash_command_setup(state, newscanline, strlen(newscanline), &cb);
 }
 
 /*
@@ -577,7 +582,10 @@ psql_scan_slash_option(PsqlScanState state,
 					{
 						if (!inquotes && type == OT_SQLID)
 							*cp = pg_tolower((unsigned char) *cp);
-						cp += PQmblen(cp, cur_state->encoding);
+						if (ENC_IS_SAFE(cur_state))
+							cp += strlen(cp);
+						else
+							cp += cur_state->cb.enc_mblen(cp);
 					}
 				}
 			}
@@ -586,7 +594,7 @@ psql_scan_slash_option(PsqlScanState state,
 		case xslashbackquote:
 		case xslashdquote:
 			/* must have hit EOL inside quotes */
-			psql_error("unterminated quoted string\n");
+			cur_state->cb.error_out("unterminated quoted string\n");
 			termPQExpBuffer(&mybuf);
 			return NULL;
 		case xslashwholeline:
@@ -660,7 +668,7 @@ evaluate_backtick(void)
 	fd = popen(cmd, PG_BINARY_R);
 	if (!fd)
 	{
-		psql_error("%s: %s\n", cmd, strerror(errno));
+		cur_state->cb.error_out("%s: %s\n", cmd, strerror(errno));
 		error = true;
 	}
 
@@ -671,7 +679,7 @@ evaluate_backtick(void)
 			result = fread(buf, 1, sizeof(buf), fd);
 			if (ferror(fd))
 			{
-				psql_error("%s: %s\n", cmd, strerror(errno));
+				cur_state->cb.error_out("%s: %s\n", cmd, strerror(errno));
 				error = true;
 				break;
 			}
@@ -681,13 +689,13 @@ evaluate_backtick(void)
 
 	if (fd && pclose(fd) == -1)
 	{
-		psql_error("%s: %s\n", cmd, strerror(errno));
+		cur_state->cb.error_out("%s: %s\n", cmd, strerror(errno));
 		error = true;
 	}
 
 	if (PQExpBufferDataBroken(cmd_output))
 	{
-		psql_error("%s: out of memory\n", cmd);
+		cur_state->cb.error_out("%s: out of memory\n", cmd);
 		error = true;
 	}
 
diff --git a/src/bin/psql/psqlscanbody.l b/src/bin/psql/psqlscanbody.l
index 4418a5c..4de29ec 100644
--- a/src/bin/psql/psqlscanbody.l
+++ b/src/bin/psql/psqlscanbody.l
@@ -54,6 +54,7 @@ PQExpBuffer output_buf;	/* current output buffer */
 /* Track where lexer parsed up to */
 #define YY_USER_ACTION cur_state->curpos += yyleng;
 
+#define ENC_IS_SAFE(s) (!(s)->cb.enc_mblen)
 %}
 
 %option 8bit
@@ -427,7 +428,7 @@ other			.
 				}
 
 {xqstart}		{
-					if (standard_strings())
+					if (cur_state->cb.standard_strings())
 						BEGIN(xq);
 					else
 						BEGIN(xe);
@@ -655,13 +656,15 @@ other			.
 
 :{variable_char}+	{
 					/* Possible psql variable substitution */
-					char   *varname = NULL;
+					char	   *varname = NULL;
 					const char *value = NULL;
+					void	  (*free_fn)(void *) = NULL;
 
-					if (cur_state->get_variable)
+					if (cur_state->cb.get_variable)
 					{
 						varname = extract_substring(yytext + 1, yyleng - 1);
-						value = cur_state->get_variable(varname);
+						value = cur_state->cb.get_variable(varname,
+ 									false, false, &free_fn);
 					}
 
 					if (value)
@@ -670,7 +673,7 @@ other			.
 						if (var_is_current_source(cur_state, varname))
 						{
 							/* Recursive expansion --- don't go there */
-							psql_error("skipping recursive expansion of variable \"%s\"\n",
+							cur_state->cb.error_out("skipping recursive expansion of variable \"%s\"\n",
 									   varname);
 							/* Instead copy the string as is */
 							ECHO;
@@ -681,6 +684,8 @@ other			.
 							push_new_buffer(value, varname);
 							/* yy_scan_string already made buffer active */
 						}
+						if (free_fn)
+							free_fn((void*)value);
 					}
 					else
 					{
@@ -860,6 +865,9 @@ other			.
 
 static void my_psql_scan_finish(PsqlScanState state);
 static void my_psql_scan_reset(PsqlScanState state);
+static void psql_error_errout(const char *fmt, ...)
+	__attribute__ ((format (printf, 1, 2)));
+static bool psql_standard_strings(void);
 
 static void
 psql_scan_initialize(PsqlScanState state)
@@ -911,17 +919,25 @@ psql_scan_destroy(PsqlScanState state)
  */
 void
 psql_scan_setup(PsqlScanState state, const char *line, int line_len,
-				PGconn *db, VariableGetter vargetter, int encoding)
+				PsqlScanCallbacks *cb)
 {
 	/* Mustn't be scanning already */
 	Assert(state->scanbufhandle == NULL);
 	Assert(state->buffer_stack == NULL);
 
-	/* Do we need to hack the character set encoding? */
-	state->encoding = encoding;
-	state->safe_encoding = pg_valid_server_encoding_id(state->encoding);
+	/* copy callback functions */
+	state->cb.get_variable = cb->get_variable;
+	if (cb->standard_strings)
+		state->cb.standard_strings = cb->standard_strings;
+	else
+		state->cb.standard_strings = &psql_standard_strings;
 
-	state->get_variable = vargetter;
+	state->cb.enc_mblen = cb->enc_mblen;
+
+	if (cb->error_out)
+		state->cb.error_out = cb->error_out;
+	else
+		state->cb.error_out = &psql_error_errout;
 
 	/* needed for prepare_buffer */
 	cur_state = state;
@@ -1141,13 +1157,10 @@ void
 psql_scan_switch_lexer(PsqlScanState state)
 {
 	const char	   *newscanline = state->scanline + state->curpos;
-	PGconn		   *db = state->db;
-	VariableGetter	vargetter = state->get_variable;
-	int				encoding = state->encoding;
+	PsqlScanCallbacks cb = state->cb;
 
 	psql_scan_initialize(state);
-	psql_scan_setup(state, newscanline, strlen(newscanline),
-					db, vargetter, encoding);
+	psql_scan_setup(state, newscanline, strlen(newscanline), &cb);
 }
 
 /*
@@ -1209,7 +1222,7 @@ push_new_buffer(const char *newstr, const char *varname)
 	stackelem->buf = prepare_buffer(newstr, strlen(newstr),
 									&stackelem->bufstring);
 	cur_state->curline = stackelem->bufstring;
-	if (cur_state->safe_encoding)
+	if (ENC_IS_SAFE(cur_state))
 	{
 		stackelem->origstring = NULL;
 		cur_state->refline = stackelem->bufstring;
@@ -1282,7 +1295,7 @@ prepare_buffer(const char *txt, int len, char **txtcopy)
 	*txtcopy = newtxt;
 	newtxt[len] = newtxt[len + 1] = YY_END_OF_BUFFER_CHAR;
 
-	if (cur_state->safe_encoding)
+	if (ENC_IS_SAFE(cur_state))
 		memcpy(newtxt, txt, len);
 	else
 	{
@@ -1291,7 +1304,7 @@ prepare_buffer(const char *txt, int len, char **txtcopy)
 
 		while (i < len)
 		{
-			int		thislen = PQmblen(txt + i, cur_state->encoding);
+			int		thislen = cur_state->cb.enc_mblen(txt + i);
 
 			/* first byte should always be okay... */
 			newtxt[i] = txt[i];
@@ -1315,7 +1328,7 @@ prepare_buffer(const char *txt, int len, char **txtcopy)
 void
 scan_emit(const char *txt, int len)
 {
-	if (cur_state->safe_encoding)
+	if (ENC_IS_SAFE(cur_state))
 		appendBinaryPQExpBuffer(output_buf, txt, len);
 	else
 	{
@@ -1347,7 +1360,7 @@ extract_substring(const char *txt, int len)
 {
 	char	   *result = (char *) pg_malloc(len + 1);
 
-	if (cur_state->safe_encoding)
+	if (ENC_IS_SAFE(cur_state))
 		memcpy(result, txt, len);
 	else
 	{
@@ -1381,45 +1394,23 @@ extract_substring(const char *txt, int len)
 void
 escape_variable(bool as_ident)
 {
-	char	   *varname;
-	const char *value;
-
 	/* Variable lookup if possible. */
-	if (cur_state->get_variable)
+	if (cur_state->cb.get_variable)
 	{
+		char		*varname;
+		const char  *value;
+		void	   (*free_fn)(void *);
+
 		varname = extract_substring(yytext + 2, yyleng - 3);
-		value = cur_state->get_variable(varname);
+		value = cur_state->cb.get_variable(varname, true, as_ident, &free_fn);
 		free(varname);
-	}
 
-	/* Escaping. */
-	if (value)
-	{
-		if (!cur_state->db)
-			psql_error("can't escape without active connection\n");
-		else
+		if (value)
 		{
-			char   *escaped_value;
-
-			if (as_ident)
-				escaped_value =
-					PQescapeIdentifier(cur_state->db, value, strlen(value));
-			else
-				escaped_value =
-					PQescapeLiteral(cur_state->db, value, strlen(value));
-
-			if (escaped_value == NULL)
-			{
-				const char *error = PQerrorMessage(cur_state->db);
-
-				psql_error("%s", error);
-			}
-			else
-			{
-				appendPQExpBufferStr(output_buf, escaped_value);
-				PQfreemem(escaped_value);
-				return;
-			}
+			appendPQExpBufferStr(output_buf, value);
+			if (free_fn)
+				free_fn((void*)value);
+			return;
 		}
 	}
 
@@ -1429,3 +1420,19 @@ escape_variable(bool as_ident)
 	 */
 	scan_emit(yytext, yyleng);
 }
+
+/* Default error output function */
+static void psql_error_errout(const char *fmt, ...)
+{
+	va_list	ap;
+
+	va_start(ap, fmt);
+	vfprintf(stderr, _(fmt), ap);
+	va_end(ap);
+}
+
+/* Default function to check standard_conforming_strings */
+static bool psql_standard_strings(void)
+{
+	return false;
+}
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 553efc0..47e9077 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -337,9 +337,12 @@ main(int argc, char *argv[])
 					puts(cell->val);
 
 				scan_state = psql_scan_create();
-				psql_scan_setup(scan_state,
-								cell->val, strlen(cell->val),
-								pset.db, get_variable, pset.encoding);
+				/* set enc_mblen according to the encoding */
+				psqlscan_callbacks.enc_mblen =
+					(pg_valid_server_encoding_id(pset.encoding) ?
+					 NULL : &psql_mblen);
+				psql_scan_setup(scan_state,	cell->val, strlen(cell->val),
+								&psqlscan_callbacks);
 
 				successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
 					? EXIT_SUCCESS : EXIT_FAILURE;
-- 
1.8.3.1

