On 09/26/2011 05:16 PM, Andrew Dunstan wrote:


On 09/26/2011 05:07 PM, Jeff Janes wrote:

But in any case, considering that we are both wondering if it works on
Windows, I think that argues that an automatic regression test would
be very handy.



I think an automated test should be possible. Something like:

   \setenv PGFOO blurfl
   \! echo $PGFOO %PGFOO%


and then have a couple of alternative results. When I get time to get back to this I'll experiment.



I can confirm it does work on Windows:

   C:\prog\bf\root\HEAD\testinst>type ..\..\..\setenv.sql
   \setenv PGFOO foo
   \! echo $PGFOO %PGFOO%
   \setenv PGFOO
   \! echo $PGFOO %PGFOO%
   C:\prog\bf\root\HEAD\testinst>bin\psql -f ..\..\..\setenv.sql postgres
   $PGFOO foo
   $PGFOO %PGFOO%


I think I agree on reflection with Josh Kupershmidt that we don't need a regression test for this.

Updated patch is attached - adding to Nov commitfest.

cheers

andrew


diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index d6941e0..d8ed5c8 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -2237,6 +2237,24 @@ lo_import 152801
 
 
       <varlistentry>
+        <term><literal>\setenv [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
+
+        <listitem>
+        <para>
+        Sets the environment variable <replaceable
+        class="parameter">name</replaceable> to <replaceable
+        class="parameter">value</replaceable>, or if the 
+		<replaceable class="parameter">value</replaceable> is
+		not supplied, unsets the environment variable. Example:
+<programlisting>
+foo=&gt; <userinput>\setenv PAGER less</userinput>
+foo=&gt; <userinput>\setenv LESS -imx4F</userinput>
+</programlisting>
+        </para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><literal>\sf[+] <replaceable class="parameter">function_description</> </literal></term>
 
         <listitem>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 2c38902..7a91905 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -1104,6 +1104,50 @@ exec_command(const char *cmd,
 		free(opt0);
 	}
 
+
+	/* \setenv -- set environment command */
+	else if (strcmp(cmd, "setenv") == 0)
+	{
+		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)
+		{
+			psql_error("\\%s: missing required argument\n", cmd);
+			success = false;
+		}
+		else if (!envval)
+		{
+			/* No argument - unset the environment variable */
+			unsetenv(envvar);
+			success = true;
+		}
+		else
+		{
+			/* Set variable to the value of the next argument */
+			int         len = strlen(envvar) + strlen(envval) + 1;
+			char	   *newval = malloc(len + 1);
+
+			if (!newval)
+			{
+				psql_error("out of memory\n");
+				exit(EXIT_FAILURE);
+			}
+			snprintf(newval, len+1, "%s=%s", envvar, envval);
+			putenv(newval);
+			success = true;
+			/*
+			 * Do not free newval here, it will screw up the environment
+			 * if you do. See putenv man page for details. That means we
+			 * leak a bit of memory here, but not enough to worry about.
+			 */
+		}
+		free(envvar);
+		free(envval);
+	}
+
 	/* \sf -- show a function's source code */
 	else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 53e4cd0..5c2172e 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -158,7 +158,7 @@ slashUsage(unsigned short int pager)
 {
 	FILE	   *output;
 
-	output = PageOutput(93, pager);
+	output = PageOutput(94, pager);
 
 	/* if you add/remove a line here, change the row count above */
 
@@ -257,6 +257,7 @@ slashUsage(unsigned short int pager)
 
 	fprintf(output, _("Operating System\n"));
 	fprintf(output, _("  \\cd [DIR]              change the current working directory\n"));
+	fprintf(output, _("  \\setenv NAME [VALUE]   set environment variable, or unset it if no value provided\n"));
 	fprintf(output, _("  \\timing [on|off]       toggle timing of commands (currently %s)\n"),
 			ON(pset.timing));
 	fprintf(output, _("  \\! [COMMAND]           execute command in shell or start interactive shell\n"));
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to