>From ae55ba632d7fc92c60eb2bae327d5e66adaf100a Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 24 Jul 2015 17:14:40 +0900
Subject: [PATCH 3/3] Allow backslash continuation for backslash commads.

Difrent from SQL statements, back slash command lines are continued by
a back slash at the end of the line.
---
 src/bin/pgbench/pgbench.c | 104 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 76 insertions(+), 28 deletions(-)

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 32a5094..f4f4abc 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -293,6 +293,14 @@ typedef struct
 	double		sum2_lag;		/* sum(lag*lag) */
 } AggVals;
 
+typedef struct ProcComState
+{
+	PsqlScanState scan_state;
+	PQExpBuffer	  outbuf;
+	bool		  in_backslash_cmd;
+} ProcComStateData;
+typedef ProcComStateData *ProcComState;
+
 static Command **sql_files[MAX_FILES];	/* SQL script files */
 static int	num_files;			/* number of script files */
 static int	num_commands = 0;	/* total number of Command structs */
@@ -2221,33 +2229,69 @@ syntax_error(const char *source, const int lineno,
 	exit(1);
 }
 
+
+static ProcComState
+createProcComState(void)
+{
+	ProcComState ret = (ProcComState) pg_malloc(sizeof(ProcComStateData));
+
+	ret->scan_state = psql_scan_create();
+	ret->outbuf = createPQExpBuffer();
+
+	return ret;
+}
+
+#define proccom_reset_outbuf(pcs) resetPQExpBuffer((pcs)->outbuf)
+#define proccom_finish_scan(pcs) psql_scan_finish((pcs)->scan_state)
+#define proccom_in_backslash_command(pcs) ((pcs)->in_backslash_cmd)
+
 /* Parse a backslash command; return a Command struct  */
 static Command *
-process_backslash_commands(char *buf, const char *source, const int lineno)
+process_backslash_commands(ProcComState proc_state, char *buf,
+						   const char *source, const int lineno)
 {
 	const char	delim[] = " \f\n\r\t\v";
 
 	Command    *my_commands;
 	int			j;
 	char	   *p,
+			   *start,
 			   *tok;
 	int			max_args = -1;
 
+	/* Skip leading whitespace */
+ 	p = buf;
+	while (isspace((unsigned char) *p))
+		p++;
+
+	if (*p == '\\')
+		proccom_in_backslash_command(proc_state) = true;
+
+	if (!proccom_in_backslash_command(proc_state))
+		return NULL;
+
 	/* Make the string buf end at the next newline */
+	start = p;
 	if ((p = strchr(buf, '\n')) != NULL)
 		*p = '\0';
 
- 	p = buf;
-	while (isspace((unsigned char) *p))
-		p++;
+	/* Mark as "in back slash command" if the fist character is bask slash */
+	proccom_in_backslash_command(proc_state) = (*(--p) == '\\');
 
-	p = buf;
+	/* The last back slash is needless */
+	if (proccom_in_backslash_command(proc_state))
+		*p-- = '\0';
 
-	if (*p != '\\')
-	{
-		fprintf(stderr, "Invalid backslash found: %s:%d\n", source, lineno);
-		exit(1);
-	}		
+	appendPQExpBufferStr(proc_state->outbuf, start);
+
+	if (proccom_in_backslash_command(proc_state) && !isspace(*p))
+		appendPQExpBufferChar(proc_state->outbuf, ' ');
+
+	/* back slash command is not terminated, go to next line */
+	if (proccom_in_backslash_command(proc_state))
+		return NULL;
+
+	p = proc_state->outbuf->data;
 
 	/* Allocate and initialize Command structure */
 	my_commands = (Command *) pg_malloc(sizeof(Command));
@@ -2412,13 +2456,21 @@ process_backslash_commands(char *buf, const char *source, const int lineno)
 
 /* Parse a command line, return non-null if any command terminates. */
 static Command *
-process_commands(PsqlScanState scan_state, PQExpBuffer qbuf, char *buf,
+process_commands(ProcComState proc_state, char *buf,
 				 const char *source, const int lineno)
 {
 	Command *command = NULL;
+	PsqlScanState scan_state = proc_state->scan_state;
 	PsqlScanResult scan_result;
 	promptStatus_t prompt_status = PROMPT_READY; /* dummy  */
+	PQExpBuffer qbuf = proc_state->outbuf;
 
+	command = process_backslash_commands(proc_state, buf, source, lineno);
+
+	/* go to next line for continuation line of backslash command. */
+	if (command != NULL || proccom_in_backslash_command(proc_state))
+		return command;
+	
 	psql_scan_setup(scan_state, buf, strlen(buf), NULL, NULL, 0);
 						
 	scan_result = psql_scan(scan_state, qbuf, &prompt_status);
@@ -2450,8 +2502,8 @@ process_commands(PsqlScanState scan_state, PQExpBuffer qbuf, char *buf,
 	}
 	else if (scan_result == PSCAN_BACKSLASH)
 	{
-		/* backslash commands are always one-liner  */
-		command = process_backslash_commands(buf, source, lineno);
+		fprintf(stderr, "Backslash continuation is not allows in SQL statement: %s:%d\n", source, lineno);
+		exit(1);
 	}
 
 	psql_scan_finish(scan_state);
@@ -2514,8 +2566,7 @@ process_file(char *filename)
 				index;
 	char	   *buf;
 	int			alloc_num;
-	PsqlScanState scan_state;
-	PQExpBuffer query_buf = createPQExpBuffer();
+	ProcComState proc_state = createProcComState();
 
 	if (num_files >= MAX_FILES)
 	{
@@ -2536,8 +2587,8 @@ process_file(char *filename)
 		return false;
 	}
 
-	scan_state = psql_scan_create();
-	resetPQExpBuffer(query_buf);
+	proccom_reset_outbuf(proc_state);
+	proccom_in_backslash_command(proc_state) = false;
 
 	lineno = 0;
 	index = 0;
@@ -2548,8 +2599,7 @@ process_file(char *filename)
 
 		lineno += 1;
 
-		command = process_commands(scan_state, query_buf, buf,
-								   filename, lineno);
+		command = process_commands(proc_state, buf, filename, lineno);
 		free(buf);
 
 		if (command == NULL)
@@ -2563,7 +2613,7 @@ process_file(char *filename)
 		}
 
 		my_commands[index] = command;
-		resetPQExpBuffer(query_buf);
+		proccom_reset_outbuf(proc_state);
 		if (index >= alloc_num)
 		{
 			alloc_num += COMMANDS_ALLOC_NUM;
@@ -2574,7 +2624,7 @@ process_file(char *filename)
 	}
 	fclose(fd);
 
-	psql_scan_finish(scan_state);
+	proccom_finish_scan(proc_state);
 
 	my_commands[index] = NULL;
 
@@ -2593,14 +2643,12 @@ process_builtin(char *tb, const char *source)
 				index;
 	char		buf[BUFSIZ];
 	int			alloc_num;
-	PsqlScanState scan_state;
-	PQExpBuffer query_buf = createPQExpBuffer();
+	ProcComState proc_state = createProcComState();
 
 	alloc_num = COMMANDS_ALLOC_NUM;
 	my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
 
-	scan_state = psql_scan_create();
-	resetPQExpBuffer(query_buf);
+	proccom_reset_outbuf(proc_state);
 
 	lineno = 0;
 	index = 0;
@@ -2624,11 +2672,11 @@ process_builtin(char *tb, const char *source)
 
 		lineno += 1;
 
-		command = process_commands(scan_state, query_buf, buf, source, lineno);
+		command = process_commands(proc_state, buf, source, lineno);
 		if (command == NULL)
 			continue;
 
-		resetPQExpBuffer(query_buf);
+		proccom_reset_outbuf(proc_state);
 		my_commands[index] = command;
 		index++;
 
@@ -2640,7 +2688,7 @@ process_builtin(char *tb, const char *source)
 	}
 
 	my_commands[index] = NULL;
-	psql_scan_finish(scan_state);
+	proccom_finish_scan(proc_state);
 
 	return my_commands;
 }
-- 
1.8.3.1

