static backslashResult exec_command(const char *cmd,
char *const * options,
const char *options_string,
- PQExpBuffer query_buf);
+ PQExpBuffer query_buf,
+ int encoding);
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,
const char *new_user);
backslashResult
HandleSlashCmds(const char *line,
PQExpBuffer query_buf,
- const char **end_of_cmd)
+ const char **end_of_cmd,
+ int encoding)
{
backslashResult status = CMD_SKIP_LINE;
char *my_line;
* whitespace */
i = 0;
- token = strtokx(options_string, " \t", "\"'`", '\\', "e, &pos);
+ token = strtokx(options_string, " \t", "\"'`", '\\', "e, &pos, encoding);
for (i = 0; token && i < NR_OPTIONS; i++)
{
switch (quote)
{
case '"':
- options[i] = unescape(token);
+ options[i] = unescape(token, encoding);
break;
case '\'':
options[i] = xstrdup(token);
{
bool error = false;
FILE *fd = NULL;
- char *file = unescape(token);
+ char *file = unescape(token, encoding);
PQExpBufferData output;
char buf[512];
size_t result;
if (continue_parse)
break;
- token = strtokx(NULL, " \t", "\"'`", '\\', "e, &pos);
+ token = strtokx(NULL, " \t", "\"'`", '\\', "e, &pos, encoding);
} /* for */
options[i] = NULL;
}
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)
{
new_cmd[0] = cmd[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)
exec_command(const char *cmd,
char *const * options,
const char *options_string,
- PQExpBuffer query_buf)
+ PQExpBuffer query_buf,
+ int encoding)
{
bool success = true; /* indicate here if the command ran ok or
* failed */
/* \copy */
else if (strcasecmp(cmd, "copy") == 0)
- success = do_copy(options_string);
+ success = do_copy(options_string, encoding);
/* \copyright */
else if (strcmp(cmd, "copyright") == 0)
success = false;
}
else
- success = process_file(options[0]);
+ success = process_file(options[0], encoding);
}
* The return value is malloc()'ed.
*/
static char *
-unescape(const char *source)
+unescape(const char *source, int encoding)
{
unsigned char *p;
bool esc = false; /* Last character we saw was the escape
exit(EXIT_FAILURE);
}
- for (p = (char *) source; *p; p += PQmblen(p))
+ for (p = (char *) source; *p; p += PQmblen(p, encoding))
{
if (esc)
{
* Handler for \i, but can be used for other things as well.
*/
bool
-process_file(const char *filename)
+process_file(const char *filename, int encoding)
{
FILE *fd;
int result;
return false;
}
- result = MainLoop(fd);
+ result = MainLoop(fd, encoding);
fclose(fd);
return (result == EXIT_SUCCESS);
}
backslashResult
HandleSlashCmds(const char *line,
PQExpBuffer query_buf,
- const char **end_of_cmd);
+ const char **end_of_cmd, int encoding);
bool
-process_file(const char *filename);
+process_file(const char *filename, int encoding);
bool
do_pset(const char *param,
static struct copy_options *
-parse_slash_copy(const char *args)
+parse_slash_copy(const char *args, int encoding)
{
struct copy_options *result;
char *line;
exit(EXIT_FAILURE);
}
- token = strtokx(line, " \t", "\"", '\\', "e, NULL);
+ token = strtokx(line, " \t", "\"", '\\', "e, NULL, encoding);
if (!token)
error = true;
else
if (!quote && strcasecmp(token, "binary") == 0)
{
result->binary = true;
- token = strtokx(NULL, " \t", "\"", '\\', "e, NULL);
+ token = strtokx(NULL, " \t", "\"", '\\', "e, NULL, encoding);
if (!token)
error = true;
}
if (!error)
{
- token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token)
error = true;
else
{
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)
error = true;
else
if (!error)
{
- token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token)
error = true;
}
if (!error)
{
- token = strtokx(NULL, " \t", "'", '\\', "e, NULL);
+ token = strtokx(NULL, " \t", "'", '\\', "e, NULL, encoding);
if (!token)
error = true;
else if (!quote && (strcasecmp(token, "stdin")==0 || strcasecmp(token, "stdout")==0))
if (!error)
{
- token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (token)
{
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)
error = true;
else
{
- token = strtokx(NULL, " \t", "'", '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
if (token)
{
result->delim = xstrdup(token);
- token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
}
else
error = true;
{
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)
error = true;
else
{
- token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", NULL, '\\', NULL, NULL, encoding);
if (!token || strcasecmp(token, "as") != 0)
error = true;
else
{
- token = strtokx(NULL, " \t", "'", '\\', NULL, NULL);
+ token = strtokx(NULL, " \t", "'", '\\', NULL, NULL, encoding);
if (token)
result->null = xstrdup(token);
}
* file or route its response into the file.
*/
bool
-do_copy(const char *args)
+do_copy(const char *args, int encoding)
{
char query[128 + NAMEDATALEN];
FILE *copystream;
bool success;
/* parse options */
- options = parse_slash_copy(args);
+ options = parse_slash_copy(args, encoding);
if (!options)
return false;
/* handler for \copy */
bool
- do_copy(const char *args);
+ do_copy(const char *args, int encoding);
/* lower level processors for copy in/out streams */
* FIXME: rewrite this whole thing with flex
*/
int
-MainLoop(FILE *source)
+MainLoop(FILE *source, int encoding)
{
PQExpBuffer query_buf; /* buffer for query being accumulated */
PQExpBuffer previous_buf; /* if there isn't anything in the new buffer
* The current character is at line[i], the prior character at line[i
* - 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;
- 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;
ADVANCE_1)
{
/* handle backslash command */
slashCmdStatus = HandleSlashCmds(&line[i],
query_buf->len>0 ? query_buf : previous_buf,
- &end_of_cmd);
+ &end_of_cmd, encoding);
success = slashCmdStatus != CMD_ERROR;
#include <stdio.h>
-int MainLoop(FILE *source);
+int MainLoop(FILE *source, int encoding);
#endif /* MAINLOOP_H */
/* process file given by -f */
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 */
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 */
else if (options.action == ACT_SINGLE_QUERY)
successResult = SendQuery( options.action_string) ? 0 : 1;
{
process_psqlrc();
initializeInput(options.no_readline ? 0 : 1);
- successResult = MainLoop(stdin);
+ successResult = MainLoop(stdin, PQclientencoding(pset.db));
finishInput();
}
{
char *psqlrc;
char *home;
+ int encoding;
#ifdef WIN32
#define R_OK 0
#endif
+ /* get client side encoding from envrionment variable if any */
+ encoding = PQenv2encoding();
+
/* System-wide startup file */
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)
- process_file("/etc/psqlrc");
+ process_file("/etc/psqlrc", encoding);
/* Look for one in the home dir */
home = getenv("HOME");
sprintf(psqlrc, "%s/.psqlrc-" PG_RELEASE "." PG_VERSION "." PG_SUBVERSION, home);
if (access(psqlrc, R_OK) == 0)
- process_file(psqlrc);
+ process_file(psqlrc, encoding);
else
{
sprintf(psqlrc, "%s/.psqlrc", home);
if (access(psqlrc, R_OK) == 0)
- process_file(psqlrc);
+ process_file(psqlrc, encoding);
}
free(psqlrc);
}
const char *quote,
char escape,
char *was_quoted,
- unsigned int *token_pos)
+ unsigned int *token_pos,
+ int encoding)
{
static char *storage = NULL;/* store the local copy of the users
* string here */
for (p = start;
*p && (*p != *cp || *(p - 1) == escape);
#ifdef MULTIBYTE
- p += PQmblen(p)
+ p += PQmblen(p, encoding)
#else
p++
#endif
const char *quote,
char escape,
char *was_quoted,
- unsigned int *token_pos);
+ unsigned int *token_pos,
+ int encoding);
#endif /* STRINGUTILS_H */