From 22de9fbff084a482a5991191c8723a06d110e99f Mon Sep 17 00:00:00 2001
From: Corey Huinker <corey.huinker@moat.com>
Date: Mon, 27 Mar 2017 15:48:19 -0400
Subject: [PATCH 2/2] fix var scoping

---
 src/bin/psql/command.c | 87 ++++++++++++++++----------------------------------
 1 file changed, 28 insertions(+), 59 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index a6168df..98817eb 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1095,11 +1095,10 @@ exec_command_echo(PsqlScanState scan_state, const char *cmd)
 static bool
 exec_command_encoding(PsqlScanState scan_state)
 {
-	char	   *encoding;
 
 	if (is_active_branch(scan_state))
 	{
-		encoding = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char   *encoding = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!encoding)
 		{
@@ -1160,11 +1159,10 @@ static bool
 exec_command_f(PsqlScanState scan_state)
 {
 	bool		success = true;
-	char	   *fname;
 
 	if (is_active_branch(scan_state))
 	{
-		fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char	   *fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
 		free(fname);
@@ -1182,11 +1180,10 @@ exec_command_f(PsqlScanState scan_state)
 static bool
 exec_command_g(PsqlScanState scan_state, const char *cmd, backslashResult *status)
 {
-	char	*fname;
 
 	if (is_active_branch(scan_state))
 	{
-		fname = psql_scan_slash_option(scan_state, OT_FILEPIPE, NULL, false);
+		char	*fname = psql_scan_slash_option(scan_state, OT_FILEPIPE, NULL, false);
 
 		if (!fname)
 			pset.gfname = NULL;
@@ -1222,10 +1219,9 @@ exec_command_gexec(PsqlScanState scan_state, backslashResult *status)
 static bool
 exec_command_gset(PsqlScanState scan_state, backslashResult *status)
 {
-	char	   *prefix;
 	if (is_active_branch(scan_state))
 	{
-		prefix = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char	   *prefix = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (prefix)
 			pset.gset_prefix = prefix;
@@ -1247,13 +1243,11 @@ exec_command_gset(PsqlScanState scan_state, backslashResult *status)
 static bool
 exec_command_help(PsqlScanState scan_state)
 {
-	char	   *opt;
 
 	if (is_active_branch(scan_state))
 	{
 		size_t		len;
-
-		opt = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false);
+		char	   *opt = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false);
 
 		/* strip any trailing spaces and semicolons */
 		if (opt)
@@ -1297,11 +1291,10 @@ static bool
 exec_command_include(PsqlScanState scan_state, const char *cmd)
 {
 	bool		success = true;
-	char	   *fname;
 
 	if (is_active_branch(scan_state))
 	{
-		fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char   *fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		if (!fname)
 		{
@@ -1520,14 +1513,11 @@ static bool
 exec_command_lo(PsqlScanState scan_state, const char *cmd, backslashResult *status)
 {
 	bool	success = true;
-	char   *opt1, *opt2;
 
 	if (is_active_branch(scan_state))
 	{
-		opt1 = psql_scan_slash_option(scan_state,
-									  OT_NORMAL, NULL, true);
-		opt2 = psql_scan_slash_option(scan_state,
-									  OT_NORMAL, NULL, true);
+		char *opt1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *opt2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		if (strcmp(cmd + 3, "export") == 0)
 		{
@@ -1589,11 +1579,10 @@ static bool
 exec_command_out(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *fname;
 
 	if (is_active_branch(scan_state))
 	{
-		fname = psql_scan_slash_option(scan_state, OT_FILEPIPE, NULL, true);
+		char   *fname = psql_scan_slash_option(scan_state, OT_FILEPIPE, NULL, true);
 
 		expand_tilde(&fname);
 		success = setQFout(fname);
@@ -1692,16 +1681,13 @@ static bool
 exec_command_prompt(PsqlScanState scan_state, const char *cmd)
 {
 	bool		success = true;
-	char	   *arg1,
-			   *arg2;
 
 	if (is_active_branch(scan_state))
 	{
 		char	   *opt,
 				   *prompt_text = NULL;
-
-		arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
-		arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char	   *arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char	   *arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!arg1)
 		{
@@ -1763,13 +1749,11 @@ static bool
 exec_command_pset(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt0,
-		   *opt1;
 
 	if (is_active_branch(scan_state))
 	{
-		opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
-		opt1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *opt1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!opt0)
 		{
@@ -1840,11 +1824,10 @@ static bool
 exec_command_s(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *fname;
 
 	if (is_active_branch(scan_state))
 	{
-		fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *fname = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		expand_tilde(&fname);
 		success = printHistory(fname, pset.popt.topt.pager);
@@ -1865,11 +1848,10 @@ static bool
 exec_command_set(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt0;
 
 	if (is_active_branch(scan_state))
 	{
-		opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!opt0)
 		{
@@ -1917,13 +1899,11 @@ static bool
 exec_command_setenv(PsqlScanState scan_state, const char *cmd)
 {
 	bool	success = true;
-	char   *envvar;
-	char   *envval;
 
 	if (is_active_branch(scan_state))
 	{
-		envvar = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
-		envval = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *envvar = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *envval = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!envvar)
 		{
@@ -1971,13 +1951,13 @@ static bool
 exec_command_sf(PsqlScanState scan_state, const char *cmd,
 				backslashResult *status)
 {
-	char	   *func;
 
 	if (is_active_branch(scan_state))
 	{
 		bool		show_linenumbers = (strcmp(cmd, "sf+") == 0);
 		PQExpBuffer func_buf;
 		Oid			foid = InvalidOid;
+		char	   *func;
 
 		func_buf = createPQExpBuffer();
 		func = psql_scan_slash_option(scan_state,
@@ -2063,13 +2043,13 @@ static bool
 exec_command_sv(PsqlScanState scan_state, const char *cmd,
 				backslashResult *status)
 {
-	char	   *view;
 
 	if (is_active_branch(scan_state))
 	{
 		bool		show_linenumbers = (strcmp(cmd, "sv+") == 0);
 		PQExpBuffer view_buf;
 		Oid			view_oid = InvalidOid;
+		char	   *view;
 
 		view_buf = createPQExpBuffer();
 		view = psql_scan_slash_option(scan_state,
@@ -2149,11 +2129,10 @@ static bool
 exec_command_t(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
-		opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
 		free(opt);
@@ -2169,11 +2148,10 @@ static bool
 exec_command_T(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *value;
 
 	if (is_active_branch(scan_state))
 	{
-		value = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *value = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		success = do_pset("tableattr", value, &pset.popt, pset.quiet);
 		free(value);
@@ -2189,11 +2167,10 @@ static bool
 exec_command_timing(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
-		opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (opt)
 			success = ParseVariableBool(opt, "\\timing", &pset.timing);
@@ -2219,12 +2196,10 @@ static bool
 exec_command_unset(PsqlScanState scan_state, const char *cmd)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
-		opt = psql_scan_slash_option(scan_state,
-												 OT_NORMAL, NULL, false);
+		char *opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!opt)
 		{
@@ -2337,13 +2312,12 @@ static bool
 exec_command_watch(PsqlScanState scan_state, PQExpBuffer query_buf)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
 		double		sleep = 2;
 
-		opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		/* Convert optional sleep-length argument */
 		if (opt)
@@ -2371,11 +2345,10 @@ static bool
 exec_command_x(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
-		opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		success = do_pset("expanded", opt, &pset.popt, pset.quiet);
 		free(opt);
@@ -2391,11 +2364,10 @@ static bool
 exec_command_z(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *pattern;
 
 	if (is_active_branch(scan_state))
 	{
-		pattern = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
+		char *pattern = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
 
 		success = permissionsList(pattern);
 		if (pattern)
@@ -2412,11 +2384,10 @@ static bool
 exec_command_shell_escape(PsqlScanState scan_state)
 {
 	bool	success = true;
-	char   *opt;
 
 	if (is_active_branch(scan_state))
 	{
-		opt = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false);
+		char *opt = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, false);
 
 		success = do_shell(opt);
 		free(opt);
@@ -2431,11 +2402,9 @@ exec_command_shell_escape(PsqlScanState scan_state)
 static bool
 exec_command_slash_command_help(PsqlScanState scan_state)
 {
-	char   *opt0 = NULL;
-
 	if (is_active_branch(scan_state))
 	{
-		opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
+		char *opt0 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
 
 		if (!opt0 || strcmp(opt0, "commands") == 0)
 			slashUsage(pset.popt.topt.pager);
-- 
2.7.4

