Skip to content

Commit e6d9e21

Browse files
committed
Add a \setenv command to psql.
This can be used to set (or unset) environment variables that will affect programs called by psql (such as the PAGER), probably most usefully in a .psqlrc file. Andrew Dunstan, reviewed by Josh Kupershmidt.
1 parent eb06416 commit e6d9e21

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,24 @@ lo_import 152801
22412241
</varlistentry>
22422242

22432243

2244+
<varlistentry>
2245+
<term><literal>\setenv [ <replaceable class="parameter">name</replaceable> [ <replaceable class="parameter">value</replaceable> ] ]</literal></term>
2246+
2247+
<listitem>
2248+
<para>
2249+
Sets the environment variable <replaceable
2250+
class="parameter">name</replaceable> to <replaceable
2251+
class="parameter">value</replaceable>, or if the
2252+
<replaceable class="parameter">value</replaceable> is
2253+
not supplied, unsets the environment variable. Example:
2254+
<programlisting>
2255+
testdb=&gt; <userinput>\setenv PAGER less</userinput>
2256+
testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
2257+
</programlisting>
2258+
</para>
2259+
</listitem>
2260+
</varlistentry>
2261+
22442262
<varlistentry>
22452263
<term><literal>\sf[+] <replaceable class="parameter">function_description</> </literal></term>
22462264

src/bin/psql/command.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,51 @@ exec_command(const char *cmd,
11101110
free(opt0);
11111111
}
11121112

1113+
1114+
/* \setenv -- set environment command */
1115+
else if (strcmp(cmd, "setenv") == 0)
1116+
{
1117+
char *envvar = psql_scan_slash_option(scan_state,
1118+
OT_NORMAL, NULL, false);
1119+
char *envval = psql_scan_slash_option(scan_state,
1120+
OT_NORMAL, NULL, false);
1121+
1122+
if (!envvar)
1123+
{
1124+
psql_error("\\%s: missing required argument\n", cmd);
1125+
success = false;
1126+
}
1127+
else if (strchr(envvar,'=') != NULL)
1128+
{
1129+
psql_error("\\%s: environment variable name must not contain '='\n",
1130+
cmd);
1131+
success = false;
1132+
}
1133+
else if (!envval)
1134+
{
1135+
/* No argument - unset the environment variable */
1136+
unsetenv(envvar);
1137+
success = true;
1138+
}
1139+
else
1140+
{
1141+
/* Set variable to the value of the next argument */
1142+
int len = strlen(envvar) + strlen(envval) + 1;
1143+
char *newval = pg_malloc(len + 1);
1144+
1145+
snprintf(newval, len+1, "%s=%s", envvar, envval);
1146+
putenv(newval);
1147+
success = true;
1148+
/*
1149+
* Do not free newval here, it will screw up the environment
1150+
* if you do. See putenv man page for details. That means we
1151+
* leak a bit of memory here, but not enough to worry about.
1152+
*/
1153+
}
1154+
free(envvar);
1155+
free(envval);
1156+
}
1157+
11131158
/* \sf -- show a function's source code */
11141159
else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
11151160
{

src/bin/psql/help.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ slashUsage(unsigned short int pager)
158158
{
159159
FILE *output;
160160

161-
output = PageOutput(93, pager);
161+
output = PageOutput(94, pager);
162162

163163
/* if you add/remove a line here, change the row count above */
164164

@@ -257,6 +257,7 @@ slashUsage(unsigned short int pager)
257257

258258
fprintf(output, _("Operating System\n"));
259259
fprintf(output, _(" \\cd [DIR] change the current working directory\n"));
260+
fprintf(output, _(" \\setenv NAME [VALUE] set or unset environment variable\n"));
260261
fprintf(output, _(" \\timing [on|off] toggle timing of commands (currently %s)\n"),
261262
ON(pset.timing));
262263
fprintf(output, _(" \\! [COMMAND] execute command in shell or start interactive shell\n"));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy