Commit bfbd58ce authored by Tatsuo Ishii's avatar Tatsuo Ishii

Adapt to the changes of libpq(eliminateing using putenv()).

parent 8fc386a2
...@@ -38,11 +38,12 @@ ...@@ -38,11 +38,12 @@
static backslashResult exec_command(const char *cmd, static backslashResult exec_command(const char *cmd,
char *const * options, char *const * options,
const char *options_string, const char *options_string,
PQExpBuffer query_buf); PQExpBuffer query_buf,
int encoding);
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf); static bool do_edit(const char *filename_arg, PQExpBuffer query_buf);
static char * unescape(const char *source); static char * unescape(const char *source, int encoding);
static bool do_connect(const char *new_dbname, static bool do_connect(const char *new_dbname,
const char *new_user); const char *new_user);
...@@ -79,7 +80,8 @@ static bool do_shell(const char *command); ...@@ -79,7 +80,8 @@ static bool do_shell(const char *command);
backslashResult backslashResult
HandleSlashCmds(const char *line, HandleSlashCmds(const char *line,
PQExpBuffer query_buf, PQExpBuffer query_buf,
const char **end_of_cmd) const char **end_of_cmd,
int encoding)
{ {
backslashResult status = CMD_SKIP_LINE; backslashResult status = CMD_SKIP_LINE;
char *my_line; char *my_line;
...@@ -131,14 +133,14 @@ HandleSlashCmds(const char *line, ...@@ -131,14 +133,14 @@ HandleSlashCmds(const char *line,
* whitespace */ * whitespace */
i = 0; i = 0;
token = strtokx(options_string, " \t", "\"'`", '\\', &quote, &pos); token = strtokx(options_string, " \t", "\"'`", '\\', &quote, &pos, encoding);
for (i = 0; token && i < NR_OPTIONS; i++) for (i = 0; token && i < NR_OPTIONS; i++)
{ {
switch (quote) switch (quote)
{ {
case '"': case '"':
options[i] = unescape(token); options[i] = unescape(token, encoding);
break; break;
case '\'': case '\'':
options[i] = xstrdup(token); options[i] = xstrdup(token);
...@@ -147,7 +149,7 @@ HandleSlashCmds(const char *line, ...@@ -147,7 +149,7 @@ HandleSlashCmds(const char *line,
{ {
bool error = false; bool error = false;
FILE *fd = NULL; FILE *fd = NULL;
char *file = unescape(token); char *file = unescape(token, encoding);
PQExpBufferData output; PQExpBufferData output;
char buf[512]; char buf[512];
size_t result; size_t result;
...@@ -217,14 +219,14 @@ HandleSlashCmds(const char *line, ...@@ -217,14 +219,14 @@ HandleSlashCmds(const char *line,
if (continue_parse) if (continue_parse)
break; break;
token = strtokx(NULL, " \t", "\"'`", '\\', &quote, &pos); token = strtokx(NULL, " \t", "\"'`", '\\', &quote, &pos, encoding);
} /* for */ } /* for */
options[i] = NULL; options[i] = NULL;
} }
cmd = my_line; cmd = my_line;
status = exec_command(cmd, options, options_string, query_buf); status = exec_command(cmd, options, options_string, query_buf, encoding);
if (status == CMD_UNKNOWN) if (status == CMD_UNKNOWN)
{ {
...@@ -246,7 +248,7 @@ HandleSlashCmds(const char *line, ...@@ -246,7 +248,7 @@ HandleSlashCmds(const char *line,
new_cmd[0] = cmd[0]; new_cmd[0] = cmd[0];
new_cmd[1] = '\0'; new_cmd[1] = '\0';
status = exec_command(new_cmd, (char *const *) new_options, my_line + 2, query_buf); status = exec_command(new_cmd, (char *const *) new_options, my_line + 2, query_buf, encoding);
} }
if (status == CMD_UNKNOWN) if (status == CMD_UNKNOWN)
...@@ -283,7 +285,8 @@ static backslashResult ...@@ -283,7 +285,8 @@ static backslashResult
exec_command(const char *cmd, exec_command(const char *cmd,
char *const * options, char *const * options,
const char *options_string, const char *options_string,
PQExpBuffer query_buf) PQExpBuffer query_buf,
int encoding)
{ {
bool success = true; /* indicate here if the command ran ok or bool success = true; /* indicate here if the command ran ok or
* failed */ * failed */
...@@ -338,7 +341,7 @@ exec_command(const char *cmd, ...@@ -338,7 +341,7 @@ exec_command(const char *cmd,
/* \copy */ /* \copy */
else if (strcasecmp(cmd, "copy") == 0) else if (strcasecmp(cmd, "copy") == 0)
success = do_copy(options_string); success = do_copy(options_string, encoding);
/* \copyright */ /* \copyright */
else if (strcmp(cmd, "copyright") == 0) else if (strcmp(cmd, "copyright") == 0)
...@@ -465,7 +468,7 @@ exec_command(const char *cmd, ...@@ -465,7 +468,7 @@ exec_command(const char *cmd,
success = false; success = false;
} }
else else
success = process_file(options[0]); success = process_file(options[0], encoding);
} }
...@@ -768,7 +771,7 @@ exec_command(const char *cmd, ...@@ -768,7 +771,7 @@ exec_command(const char *cmd,
* The return value is malloc()'ed. * The return value is malloc()'ed.
*/ */
static char * static char *
unescape(const char *source) unescape(const char *source, int encoding)
{ {
unsigned char *p; unsigned char *p;
bool esc = false; /* Last character we saw was the escape bool esc = false; /* Last character we saw was the escape
...@@ -790,7 +793,7 @@ unescape(const char *source) ...@@ -790,7 +793,7 @@ unescape(const char *source)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (p = (char *) source; *p; p += PQmblen(p)) for (p = (char *) source; *p; p += PQmblen(p, encoding))
{ {
if (esc) if (esc)
{ {
...@@ -1219,7 +1222,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf) ...@@ -1219,7 +1222,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
* Handler for \i, but can be used for other things as well. * Handler for \i, but can be used for other things as well.
*/ */
bool bool
process_file(const char *filename) process_file(const char *filename, int encoding)
{ {
FILE *fd; FILE *fd;
int result; int result;
...@@ -1241,7 +1244,7 @@ process_file(const char *filename) ...@@ -1241,7 +1244,7 @@ process_file(const char *filename)
return false; return false;
} }
result = MainLoop(fd); result = MainLoop(fd, encoding);
fclose(fd); fclose(fd);
return (result == EXIT_SUCCESS); return (result == EXIT_SUCCESS);
} }
......
...@@ -27,10 +27,10 @@ typedef enum _backslashResult ...@@ -27,10 +27,10 @@ typedef enum _backslashResult
backslashResult backslashResult
HandleSlashCmds(const char *line, HandleSlashCmds(const char *line,
PQExpBuffer query_buf, PQExpBuffer query_buf,
const char **end_of_cmd); const char **end_of_cmd, int encoding);
bool bool
process_file(const char *filename); process_file(const char *filename, int encoding);
bool bool
do_pset(const char *param, do_pset(const char *param,
......
...@@ -58,7 +58,7 @@ free_copy_options(struct copy_options * ptr) ...@@ -58,7 +58,7 @@ free_copy_options(struct copy_options * ptr)
static struct copy_options * static struct copy_options *
parse_slash_copy(const char *args) parse_slash_copy(const char *args, int encoding)
{ {
struct copy_options *result; struct copy_options *result;
char *line; char *line;
...@@ -74,7 +74,7 @@ parse_slash_copy(const char *args) ...@@ -74,7 +74,7 @@ parse_slash_copy(const char *args)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
token = strtokx(line, " \t", "\"", '\\', &quote, NULL); token = strtokx(line, " \t", "\"", '\\', &quote, NULL, encoding);
if (!token) if (!token)
error = true; error = true;
else else
...@@ -84,7 +84,7 @@ parse_slash_copy(const char *args) ...@@ -84,7 +84,7 @@ parse_slash_copy(const char *args)
if (!quote && strcasecmp(token, "binary") == 0) if (!quote && strcasecmp(token, "binary") == 0)
{ {
result->binary = true; result->binary = true;
token = strtokx(NULL, " \t", "\"", '\\', &quote, NULL); token = strtokx(NULL, " \t", "\"", '\\', &quote, NULL, encoding);
if (!token) if (!token)
error = true; error = true;
} }
...@@ -99,14 +99,14 @@ parse_slash_copy(const char *args) ...@@ -99,14 +99,14 @@ parse_slash_copy(const char *args)
if (!error) if (!error)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token) if (!token)
error = true; error = true;
else else
{ {
if (strcasecmp(token, "with") == 0) if (strcasecmp(token, "with") == 0)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token || strcasecmp(token, "oids") != 0) if (!token || strcasecmp(token, "oids") != 0)
error = true; error = true;
else else
...@@ -114,7 +114,7 @@ parse_slash_copy(const char *args) ...@@ -114,7 +114,7 @@ parse_slash_copy(const char *args)
if (!error) if (!error)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token) if (!token)
error = true; error = true;
} }
...@@ -131,7 +131,7 @@ parse_slash_copy(const char *args) ...@@ -131,7 +131,7 @@ parse_slash_copy(const char *args)
if (!error) if (!error)
{ {
token = strtokx(NULL, " \t", "'", '\\', &quote, NULL); token = strtokx(NULL, " \t", "'", '\\', &quote, NULL, encoding);
if (!token) if (!token)
error = true; error = true;
else if (!quote && (strcasecmp(token, "stdin")==0 || strcasecmp(token, "stdout")==0)) else if (!quote && (strcasecmp(token, "stdin")==0 || strcasecmp(token, "stdout")==0))
...@@ -142,21 +142,21 @@ parse_slash_copy(const char *args) ...@@ -142,21 +142,21 @@ parse_slash_copy(const char *args)
if (!error) if (!error)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (token) if (token)
{ {
if (strcasecmp(token, "using") == 0) if (strcasecmp(token, "using") == 0)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token || strcasecmp(token, "delimiters") != 0) if (!token || strcasecmp(token, "delimiters") != 0)
error = true; error = true;
else else
{ {
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL); token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
if (token) if (token)
{ {
result->delim = xstrdup(token); result->delim = xstrdup(token);
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
} }
else else
error = true; error = true;
...@@ -167,17 +167,17 @@ parse_slash_copy(const char *args) ...@@ -167,17 +167,17 @@ parse_slash_copy(const char *args)
{ {
if (strcasecmp(token, "with") == 0) if (strcasecmp(token, "with") == 0)
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token || strcasecmp(token, "null") != 0) if (!token || strcasecmp(token, "null") != 0)
error = true; error = true;
else else
{ {
token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL); token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token || strcasecmp(token, "as") != 0) if (!token || strcasecmp(token, "as") != 0)
error = true; error = true;
else else
{ {
token = strtokx(NULL, " \t", "'", '\\', NULL, NULL); token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
if (token) if (token)
result->null = xstrdup(token); result->null = xstrdup(token);
} }
...@@ -214,7 +214,7 @@ parse_slash_copy(const char *args) ...@@ -214,7 +214,7 @@ parse_slash_copy(const char *args)
* file or route its response into the file. * file or route its response into the file.
*/ */
bool bool
do_copy(const char *args) do_copy(const char *args, int encoding)
{ {
char query[128 + NAMEDATALEN]; char query[128 + NAMEDATALEN];
FILE *copystream; FILE *copystream;
...@@ -223,7 +223,7 @@ do_copy(const char *args) ...@@ -223,7 +223,7 @@ do_copy(const char *args)
bool success; bool success;
/* parse options */ /* parse options */
options = parse_slash_copy(args); options = parse_slash_copy(args, encoding);
if (!options) if (!options)
return false; return false;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
/* handler for \copy */ /* handler for \copy */
bool bool
do_copy(const char *args); do_copy(const char *args, int encoding);
/* lower level processors for copy in/out streams */ /* lower level processors for copy in/out streams */
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
* FIXME: rewrite this whole thing with flex * FIXME: rewrite this whole thing with flex
*/ */
int int
MainLoop(FILE *source) MainLoop(FILE *source, int encoding)
{ {
PQExpBuffer query_buf; /* buffer for query being accumulated */ PQExpBuffer query_buf; /* buffer for query being accumulated */
PQExpBuffer previous_buf; /* if there isn't anything in the new buffer PQExpBuffer previous_buf; /* if there isn't anything in the new buffer
...@@ -212,10 +212,10 @@ MainLoop(FILE *source) ...@@ -212,10 +212,10 @@ MainLoop(FILE *source)
* The current character is at line[i], the prior character at line[i * The current character is at line[i], the prior character at line[i
* - prevlen], the next character at line[i + thislen]. * - prevlen], the next character at line[i + thislen].
*/ */
#define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i)) #define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i, encoding))
success = true; success = true;
for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line) : 0; for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line, encoding) : 0;
i < len; i < len;
ADVANCE_1) ADVANCE_1)
{ {
...@@ -373,7 +373,7 @@ MainLoop(FILE *source) ...@@ -373,7 +373,7 @@ MainLoop(FILE *source)
/* handle backslash command */ /* handle backslash command */
slashCmdStatus = HandleSlashCmds(&line[i], slashCmdStatus = HandleSlashCmds(&line[i],
query_buf->len>0 ? query_buf : previous_buf, query_buf->len>0 ? query_buf : previous_buf,
&end_of_cmd); &end_of_cmd, encoding);
success = slashCmdStatus != CMD_ERROR; success = slashCmdStatus != CMD_ERROR;
......
...@@ -3,6 +3,6 @@ ...@@ -3,6 +3,6 @@
#include <stdio.h> #include <stdio.h>
int MainLoop(FILE *source); int MainLoop(FILE *source, int encoding);
#endif /* MAINLOOP_H */ #endif /* MAINLOOP_H */
...@@ -190,10 +190,10 @@ main(int argc, char **argv) ...@@ -190,10 +190,10 @@ main(int argc, char **argv)
/* process file given by -f */ /* process file given by -f */
if (options.action == ACT_FILE) if (options.action == ACT_FILE)
successResult = process_file(options.action_string) ? 0 : 1; successResult = process_file(options.action_string, PQclientencoding(pset.db)) ? 0 : 1;
/* process slash command if one was given to -c */ /* process slash command if one was given to -c */
else if (options.action == ACT_SINGLE_SLASH) else if (options.action == ACT_SINGLE_SLASH)
successResult = HandleSlashCmds(options.action_string, NULL, NULL) != CMD_ERROR ? 0 : 1; successResult = HandleSlashCmds(options.action_string, NULL, NULL, PQclientencoding(pset.db)) != CMD_ERROR ? 0 : 1;
/* If the query given to -c was a normal one, send it */ /* If the query given to -c was a normal one, send it */
else if (options.action == ACT_SINGLE_QUERY) else if (options.action == ACT_SINGLE_QUERY)
successResult = SendQuery( options.action_string) ? 0 : 1; successResult = SendQuery( options.action_string) ? 0 : 1;
...@@ -202,7 +202,7 @@ main(int argc, char **argv) ...@@ -202,7 +202,7 @@ main(int argc, char **argv)
{ {
process_psqlrc(); process_psqlrc();
initializeInput(options.no_readline ? 0 : 1); initializeInput(options.no_readline ? 0 : 1);
successResult = MainLoop(stdin); successResult = MainLoop(stdin, PQclientencoding(pset.db));
finishInput(); finishInput();
} }
...@@ -465,16 +465,20 @@ process_psqlrc(void) ...@@ -465,16 +465,20 @@ process_psqlrc(void)
{ {
char *psqlrc; char *psqlrc;
char *home; char *home;
int encoding;
#ifdef WIN32 #ifdef WIN32
#define R_OK 0 #define R_OK 0
#endif #endif
/* get client side encoding from envrionment variable if any */
encoding = PQenv2encoding();
/* System-wide startup file */ /* System-wide startup file */
if (access("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, R_OK) == 0) if (access("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, R_OK) == 0)
process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION); process_file("/etc/psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, encoding);
else if (access("/etc/psqlrc", R_OK) == 0) else if (access("/etc/psqlrc", R_OK) == 0)
process_file("/etc/psqlrc"); process_file("/etc/psqlrc", encoding);
/* Look for one in the home dir */ /* Look for one in the home dir */
home = getenv("HOME"); home = getenv("HOME");
...@@ -490,12 +494,12 @@ process_psqlrc(void) ...@@ -490,12 +494,12 @@ process_psqlrc(void)
sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home); sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
if (access(psqlrc, R_OK) == 0) if (access(psqlrc, R_OK) == 0)
process_file(psqlrc); process_file(psqlrc, encoding);
else else
{ {
sprintf(psqlrc, "%s/.psqlrc", home); sprintf(psqlrc, "%s/.psqlrc", home);
if (access(psqlrc, R_OK) == 0) if (access(psqlrc, R_OK) == 0)
process_file(psqlrc); process_file(psqlrc, encoding);
} }
free(psqlrc); free(psqlrc);
} }
......
...@@ -41,7 +41,8 @@ strtokx(const char *s, ...@@ -41,7 +41,8 @@ strtokx(const char *s,
const char *quote, const char *quote,
char escape, char escape,
char *was_quoted, char *was_quoted,
unsigned int *token_pos) unsigned int *token_pos,
int encoding)
{ {
static char *storage = NULL;/* store the local copy of the users static char *storage = NULL;/* store the local copy of the users
* string here */ * string here */
...@@ -93,7 +94,7 @@ strtokx(const char *s, ...@@ -93,7 +94,7 @@ strtokx(const char *s,
for (p = start; for (p = start;
*p && (*p != *cp || *(p - 1) == escape); *p && (*p != *cp || *(p - 1) == escape);
#ifdef MULTIBYTE #ifdef MULTIBYTE
p += PQmblen(p) p += PQmblen(p, encoding)
#else #else
p++ p++
#endif #endif
......
...@@ -8,6 +8,7 @@ extern char *strtokx(const char *s, ...@@ -8,6 +8,7 @@ extern char *strtokx(const char *s,
const char *quote, const char *quote,
char escape, char escape,
char *was_quoted, char *was_quoted,
unsigned int *token_pos); unsigned int *token_pos,
int encoding);
#endif /* STRINGUTILS_H */ #endif /* STRINGUTILS_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment