Skip to content

Commit bfbd58c

Browse files
committed
Adapt to the changes of libpq(eliminateing using putenv()).
1 parent 8fc386a commit bfbd58c

File tree

9 files changed

+59
-50
lines changed

9 files changed

+59
-50
lines changed

src/bin/psql/command.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
static backslashResult exec_command(const char *cmd,
3939
char *const * options,
4040
const char *options_string,
41-
PQExpBuffer query_buf);
41+
PQExpBuffer query_buf,
42+
int encoding);
4243

4344
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
4445

45-
static char * unescape(const char *source);
46+
static char * unescape(const char *source, int encoding);
4647

4748
static bool do_connect(const char *new_dbname,
4849
const char *new_user);
@@ -79,7 +80,8 @@ static bool do_shell(const char *command);
7980
backslashResult
8081
HandleSlashCmds(const char *line,
8182
PQExpBuffer query_buf,
82-
const char **end_of_cmd)
83+
const char **end_of_cmd,
84+
int encoding)
8385
{
8486
backslashResult status = CMD_SKIP_LINE;
8587
char *my_line;
@@ -131,14 +133,14 @@ HandleSlashCmds(const char *line,
131133
* whitespace */
132134

133135
i = 0;
134-
token = strtokx(options_string, " \t", "\"'`", '\\', &quote, &pos);
136+
token = strtokx(options_string, " \t", "\"'`", '\\', &quote, &pos, encoding);
135137

136138
for (i = 0; token && i < NR_OPTIONS; i++)
137139
{
138140
switch (quote)
139141
{
140142
case '"':
141-
options[i] = unescape(token);
143+
options[i] = unescape(token, encoding);
142144
break;
143145
case '\'':
144146
options[i] = xstrdup(token);
@@ -147,7 +149,7 @@ HandleSlashCmds(const char *line,
147149
{
148150
bool error = false;
149151
FILE *fd = NULL;
150-
char *file = unescape(token);
152+
char *file = unescape(token, encoding);
151153
PQExpBufferData output;
152154
char buf[512];
153155
size_t result;
@@ -217,14 +219,14 @@ HandleSlashCmds(const char *line,
217219
if (continue_parse)
218220
break;
219221

220-
token = strtokx(NULL, " \t", "\"'`", '\\', &quote, &pos);
222+
token = strtokx(NULL, " \t", "\"'`", '\\', &quote, &pos, encoding);
221223
} /* for */
222224

223225
options[i] = NULL;
224226
}
225227

226228
cmd = my_line;
227-
status = exec_command(cmd, options, options_string, query_buf);
229+
status = exec_command(cmd, options, options_string, query_buf, encoding);
228230

229231
if (status == CMD_UNKNOWN)
230232
{
@@ -246,7 +248,7 @@ HandleSlashCmds(const char *line,
246248
new_cmd[0] = cmd[0];
247249
new_cmd[1] = '\0';
248250

249-
status = exec_command(new_cmd, (char *const *) new_options, my_line + 2, query_buf);
251+
status = exec_command(new_cmd, (char *const *) new_options, my_line + 2, query_buf, encoding);
250252
}
251253

252254
if (status == CMD_UNKNOWN)
@@ -283,7 +285,8 @@ static backslashResult
283285
exec_command(const char *cmd,
284286
char *const * options,
285287
const char *options_string,
286-
PQExpBuffer query_buf)
288+
PQExpBuffer query_buf,
289+
int encoding)
287290
{
288291
bool success = true; /* indicate here if the command ran ok or
289292
* failed */
@@ -338,7 +341,7 @@ exec_command(const char *cmd,
338341

339342
/* \copy */
340343
else if (strcasecmp(cmd, "copy") == 0)
341-
success = do_copy(options_string);
344+
success = do_copy(options_string, encoding);
342345

343346
/* \copyright */
344347
else if (strcmp(cmd, "copyright") == 0)
@@ -465,7 +468,7 @@ exec_command(const char *cmd,
465468
success = false;
466469
}
467470
else
468-
success = process_file(options[0]);
471+
success = process_file(options[0], encoding);
469472
}
470473

471474

@@ -768,7 +771,7 @@ exec_command(const char *cmd,
768771
* The return value is malloc()'ed.
769772
*/
770773
static char *
771-
unescape(const char *source)
774+
unescape(const char *source, int encoding)
772775
{
773776
unsigned char *p;
774777
bool esc = false; /* Last character we saw was the escape
@@ -790,7 +793,7 @@ unescape(const char *source)
790793
exit(EXIT_FAILURE);
791794
}
792795

793-
for (p = (char *) source; *p; p += PQmblen(p))
796+
for (p = (char *) source; *p; p += PQmblen(p, encoding))
794797
{
795798
if (esc)
796799
{
@@ -1219,7 +1222,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
12191222
* Handler for \i, but can be used for other things as well.
12201223
*/
12211224
bool
1222-
process_file(const char *filename)
1225+
process_file(const char *filename, int encoding)
12231226
{
12241227
FILE *fd;
12251228
int result;
@@ -1241,7 +1244,7 @@ process_file(const char *filename)
12411244
return false;
12421245
}
12431246

1244-
result = MainLoop(fd);
1247+
result = MainLoop(fd, encoding);
12451248
fclose(fd);
12461249
return (result == EXIT_SUCCESS);
12471250
}

src/bin/psql/command.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ typedef enum _backslashResult
2727
backslashResult
2828
HandleSlashCmds(const char *line,
2929
PQExpBuffer query_buf,
30-
const char **end_of_cmd);
30+
const char **end_of_cmd, int encoding);
3131

3232
bool
33-
process_file(const char *filename);
33+
process_file(const char *filename, int encoding);
3434

3535
bool
3636
do_pset(const char *param,

src/bin/psql/copy.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ free_copy_options(struct copy_options * ptr)
5858

5959

6060
static struct copy_options *
61-
parse_slash_copy(const char *args)
61+
parse_slash_copy(const char *args, int encoding)
6262
{
6363
struct copy_options *result;
6464
char *line;
@@ -74,7 +74,7 @@ parse_slash_copy(const char *args)
7474
exit(EXIT_FAILURE);
7575
}
7676

77-
token = strtokx(line, " \t", "\"", '\\', &quote, NULL);
77+
token = strtokx(line, " \t", "\"", '\\', &quote, NULL, encoding);
7878
if (!token)
7979
error = true;
8080
else
@@ -84,7 +84,7 @@ parse_slash_copy(const char *args)
8484
if (!quote && strcasecmp(token, "binary") == 0)
8585
{
8686
result->binary = true;
87-
token = strtokx(NULL, " \t", "\"", '\\', &quote, NULL);
87+
token = strtokx(NULL, " \t", "\"", '\\', &quote, NULL, encoding);
8888
if (!token)
8989
error = true;
9090
}
@@ -99,22 +99,22 @@ parse_slash_copy(const char *args)
9999

100100
if (!error)
101101
{
102-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
102+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
103103
if (!token)
104104
error = true;
105105
else
106106
{
107107
if (strcasecmp(token, "with") == 0)
108108
{
109-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
109+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
110110
if (!token || strcasecmp(token, "oids") != 0)
111111
error = true;
112112
else
113113
result->oids = true;
114114

115115
if (!error)
116116
{
117-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
117+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
118118
if (!token)
119119
error = true;
120120
}
@@ -131,7 +131,7 @@ parse_slash_copy(const char *args)
131131

132132
if (!error)
133133
{
134-
token = strtokx(NULL, " \t", "'", '\\', &quote, NULL);
134+
token = strtokx(NULL, " \t", "'", '\\', &quote, NULL, encoding);
135135
if (!token)
136136
error = true;
137137
else if (!quote && (strcasecmp(token, "stdin")==0 || strcasecmp(token, "stdout")==0))
@@ -142,21 +142,21 @@ parse_slash_copy(const char *args)
142142

143143
if (!error)
144144
{
145-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
145+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
146146
if (token)
147147
{
148148
if (strcasecmp(token, "using") == 0)
149149
{
150-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
150+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
151151
if (!token || strcasecmp(token, "delimiters") != 0)
152152
error = true;
153153
else
154154
{
155-
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL);
155+
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
156156
if (token)
157157
{
158158
result->delim = xstrdup(token);
159-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
159+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
160160
}
161161
else
162162
error = true;
@@ -167,17 +167,17 @@ parse_slash_copy(const char *args)
167167
{
168168
if (strcasecmp(token, "with") == 0)
169169
{
170-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
170+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
171171
if (!token || strcasecmp(token, "null") != 0)
172172
error = true;
173173
else
174174
{
175-
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
175+
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
176176
if (!token || strcasecmp(token, "as") != 0)
177177
error = true;
178178
else
179179
{
180-
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL);
180+
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
181181
if (token)
182182
result->null = xstrdup(token);
183183
}
@@ -214,7 +214,7 @@ parse_slash_copy(const char *args)
214214
* file or route its response into the file.
215215
*/
216216
bool
217-
do_copy(const char *args)
217+
do_copy(const char *args, int encoding)
218218
{
219219
char query[128 + NAMEDATALEN];
220220
FILE *copystream;
@@ -223,7 +223,7 @@ do_copy(const char *args)
223223
bool success;
224224

225225
/* parse options */
226-
options = parse_slash_copy(args);
226+
options = parse_slash_copy(args, encoding);
227227

228228
if (!options)
229229
return false;

src/bin/psql/copy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
/* handler for \copy */
1010
bool
11-
do_copy(const char *args);
11+
do_copy(const char *args, int encoding);
1212

1313

1414
/* lower level processors for copy in/out streams */

src/bin/psql/mainloop.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* FIXME: rewrite this whole thing with flex
2626
*/
2727
int
28-
MainLoop(FILE *source)
28+
MainLoop(FILE *source, int encoding)
2929
{
3030
PQExpBuffer query_buf; /* buffer for query being accumulated */
3131
PQExpBuffer previous_buf; /* if there isn't anything in the new buffer
@@ -212,10 +212,10 @@ MainLoop(FILE *source)
212212
* The current character is at line[i], the prior character at line[i
213213
* - prevlen], the next character at line[i + thislen].
214214
*/
215-
#define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i))
215+
#define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i, encoding))
216216

217217
success = true;
218-
for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line) : 0;
218+
for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line, encoding) : 0;
219219
i < len;
220220
ADVANCE_1)
221221
{
@@ -373,7 +373,7 @@ MainLoop(FILE *source)
373373
/* handle backslash command */
374374
slashCmdStatus = HandleSlashCmds(&line[i],
375375
query_buf->len>0 ? query_buf : previous_buf,
376-
&end_of_cmd);
376+
&end_of_cmd, encoding);
377377

378378
success = slashCmdStatus != CMD_ERROR;
379379

src/bin/psql/mainloop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
#include <stdio.h>
55

6-
int MainLoop(FILE *source);
6+
int MainLoop(FILE *source, int encoding);
77

88
#endif /* MAINLOOP_H */

src/bin/psql/startup.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ main(int argc, char **argv)
190190

191191
/* process file given by -f */
192192
if (options.action == ACT_FILE)
193-
successResult = process_file(options.action_string) ? 0 : 1;
193+
successResult = process_file(options.action_string, PQclientencoding(pset.db)) ? 0 : 1;
194194
/* process slash command if one was given to -c */
195195
else if (options.action == ACT_SINGLE_SLASH)
196-
successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR ? 0 : 1;
196+
successResult = HandleSlashCmds(options.action_string, NULL, NULL, PQclientencoding(pset.db)) != CMD_ERROR ? 0 : 1;
197197
/* If the query given to -c was a normal one, send it */
198198
else if (options.action == ACT_SINGLE_QUERY)
199199
successResult = SendQuery( options.action_string) ? 0 : 1;
@@ -202,7 +202,7 @@ main(int argc, char **argv)
202202
{
203203
process_psqlrc();
204204
initializeInput(options.no_readline ? 0 : 1);
205-
successResult = MainLoop(stdin);
205+
successResult = MainLoop(stdin, PQclientencoding(pset.db));
206206
finishInput();
207207
}
208208

@@ -465,16 +465,20 @@ process_psqlrc(void)
465465
{
466466
char *psqlrc;
467467
char *home;
468+
int encoding;
468469

469470
#ifdef WIN32
470471
#define R_OK 0
471472
#endif
472473

474+
/* get client side encoding from envrionment variable if any */
475+
encoding = PQenv2encoding();
476+
473477
/* System-wide startup file */
474478
if (access("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, R_OK) == 0)
475-
process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION);
479+
process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, encoding);
476480
else if (access("/etc/psqlrc", R_OK) == 0)
477-
process_file("/etc/psqlrc");
481+
process_file("/etc/psqlrc", encoding);
478482

479483
/* Look for one in the home dir */
480484
home = getenv("HOME");
@@ -490,12 +494,12 @@ process_psqlrc(void)
490494

491495
sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
492496
if (access(psqlrc, R_OK) == 0)
493-
process_file(psqlrc);
497+
process_file(psqlrc, encoding);
494498
else
495499
{
496500
sprintf(psqlrc, "%s/.psqlrc", home);
497501
if (access(psqlrc, R_OK) == 0)
498-
process_file(psqlrc);
502+
process_file(psqlrc, encoding);
499503
}
500504
free(psqlrc);
501505
}

0 commit comments

Comments
 (0)