Commit 22fe451c authored by Tom Lane's avatar Tom Lane

psql's recognition of comments didn't work right in MULTIBYTE

environments; it was being careless about character lengths.
parent 109cbc72
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.175 1999/04/15 02:24:41 tgl Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.176 1999/04/25 23:10:36 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -1120,9 +1120,8 @@ static char * ...@@ -1120,9 +1120,8 @@ static char *
gets_fromFile(char *prompt, FILE *source) gets_fromFile(char *prompt, FILE *source)
{ {
char *line; char *line;
int len;
line = malloc(MAX_QUERY_BUFFER + 1); line = malloc(MAX_QUERY_BUFFER);
/* read up to MAX_QUERY_BUFFER characters */ /* read up to MAX_QUERY_BUFFER characters */
if (fgets(line, MAX_QUERY_BUFFER, source) == NULL) if (fgets(line, MAX_QUERY_BUFFER, source) == NULL)
...@@ -1131,12 +1130,11 @@ gets_fromFile(char *prompt, FILE *source) ...@@ -1131,12 +1130,11 @@ gets_fromFile(char *prompt, FILE *source)
return NULL; return NULL;
} }
line[MAX_QUERY_BUFFER - 1] = '\0'; line[MAX_QUERY_BUFFER - 1] = '\0'; /* this is unnecessary, I think */
len = strlen(line); if (strlen(line) == MAX_QUERY_BUFFER-1)
if (len == MAX_QUERY_BUFFER)
{ {
fprintf(stderr, "line read exceeds maximum length. Truncating at %d\n", fprintf(stderr, "line read exceeds maximum length. Truncating at %d\n",
MAX_QUERY_BUFFER); MAX_QUERY_BUFFER-1);
} }
return line; return line;
} }
...@@ -2585,18 +2583,22 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) ...@@ -2585,18 +2583,22 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
else else
{ {
int i; int i;
/*
* The current character is at line[i], the prior character
* at line[i - prevlen], the next character at line[i + thislen].
*/
#ifdef MULTIBYTE #ifdef MULTIBYTE
int mblen = 1; int prevlen = 0;
int thislen = (len > 0) ? PQmblen(line) : 0;
#define ADVANCE_I (prevlen = thislen, i += thislen, thislen = PQmblen(line+i))
#else
#define prevlen 1
#define thislen 1
#define ADVANCE_I (i++)
#endif #endif
was_bslash = false; was_bslash = false;
#ifdef MULTIBYTE for (i = 0; i < len; ADVANCE_I)
for (i = 0; i < len; mblen = PQmblen(line + i), i += mblen)
#else
for (i = 0; i < len; i++)
#endif
{ {
if (line[i] == '\\' && !in_quote) if (line[i] == '\\' && !in_quote)
{ {
...@@ -2616,8 +2618,6 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) ...@@ -2616,8 +2618,6 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
line[i] = hold_char; line[i] = hold_char;
query_start = line + i; query_start = line + i;
break; /* handle command */ break; /* handle command */
/* start an extended comment? */
} }
if (querySent && if (querySent &&
...@@ -2630,55 +2630,30 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) ...@@ -2630,55 +2630,30 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
if (was_bslash) if (was_bslash)
was_bslash = false; was_bslash = false;
#ifdef MULTIBYTE else if (i > 0 && line[i - prevlen] == '\\')
else if (i > 0 && line[i - mblen] == '\\')
#else
else if (i > 0 && line[i - 1] == '\\')
#endif
was_bslash = true; was_bslash = true;
/* inside a quote? */ /* inside a quote? */
if (in_quote && (line[i] != in_quote || was_bslash)) if (in_quote && (line[i] != in_quote || was_bslash))
/* do nothing */ ; /* do nothing */ ;
else if (xcomment != NULL) /* inside an extended /* inside an extended comment? */
* comment? */ else if (xcomment != NULL)
{ {
#ifdef MULTIBYTE if (line[i] == '*' && line[i + thislen] == '/')
if (line[i] == '*' && line[i + mblen] == '/')
#else
if (line[i] == '*' && line[i + 1] == '/')
#endif
{ {
xcomment = NULL; xcomment = NULL;
#ifdef MULTIBYTE ADVANCE_I;
i += mblen;
#else
i++;
#endif
} }
} }
/* possible backslash command? */ /* start of extended comment? */
#ifdef MULTIBYTE else if (line[i] == '/' && line[i + thislen] == '*')
else if (line[i] == '/' && line[i + mblen] == '*')
#else
else if (line[i] == '/' && line[i + 1] == '*')
#endif
{ {
xcomment = line + i; xcomment = line + i;
#ifdef MULTIBYTE ADVANCE_I;
i += mblen;
#else
i++;
#endif
} }
/* single-line comment? truncate line */ /* single-line comment? truncate line */
#ifdef MULTIBYTE else if ((line[i] == '-' && line[i + thislen] == '-') ||
else if ((line[i] == '-' && line[i + mblen] == '-') || (line[i] == '/' && line[i + thislen] == '/'))
(line[i] == '/' && line[i + mblen] == '/'))
#else
else if ((line[i] == '-' && line[i + 1] == '-') ||
(line[i] == '/' && line[i + 1] == '/'))
#endif
{ {
/* print comment at top of query */ /* print comment at top of query */
if (pset->singleStep) if (pset->singleStep)
...@@ -2693,9 +2668,9 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) ...@@ -2693,9 +2668,9 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
/* semi-colon? then send query now */ /* semi-colon? then send query now */
else if (!paren_level && line[i] == ';') else if (!paren_level && line[i] == ';')
{ {
char hold_char = line[i + 1]; char hold_char = line[i + thislen];
line[i + 1] = '\0'; line[i + thislen] = '\0';
if (query_start[0] != '\0') if (query_start[0] != '\0')
{ {
if (query[0] != '\0') if (query[0] != '\0')
...@@ -2708,11 +2683,10 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source) ...@@ -2708,11 +2683,10 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
} }
success = SendQuery(pset, query, NULL, NULL); success = SendQuery(pset, query, NULL, NULL);
successResult &= success; successResult &= success;
line[i + 1] = hold_char; line[i + thislen] = hold_char;
query_start = line + i + 1; query_start = line + i + thislen;
/* sometimes, people do ';\g', don't execute twice */ /* sometimes, people do ';\g', don't execute twice */
if (*query_start && /* keeps us from going off the end */ if (*query_start == '\\' &&
*query_start == '\\' &&
*(query_start + 1) == 'g') *(query_start + 1) == 'g')
query_start += 2; query_start += 2;
querySent = true; querySent = true;
......
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