Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 22fe451

Browse files
committed
psql's recognition of comments didn't work right in MULTIBYTE
environments; it was being careless about character lengths.
1 parent 109cbc7 commit 22fe451

File tree

1 file changed

+32
-58
lines changed

1 file changed

+32
-58
lines changed

src/bin/psql/psql.c

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.175 1999/04/15 02:24:41 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.176 1999/04/25 23:10:36 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1120,9 +1120,8 @@ static char *
11201120
gets_fromFile(char *prompt, FILE *source)
11211121
{
11221122
char *line;
1123-
int len;
11241123

1125-
line = malloc(MAX_QUERY_BUFFER + 1);
1124+
line = malloc(MAX_QUERY_BUFFER);
11261125

11271126
/* read up to MAX_QUERY_BUFFER characters */
11281127
if (fgets(line, MAX_QUERY_BUFFER, source) == NULL)
@@ -1131,12 +1130,11 @@ gets_fromFile(char *prompt, FILE *source)
11311130
return NULL;
11321131
}
11331132

1134-
line[MAX_QUERY_BUFFER - 1] = '\0';
1135-
len = strlen(line);
1136-
if (len == MAX_QUERY_BUFFER)
1133+
line[MAX_QUERY_BUFFER - 1] = '\0'; /* this is unnecessary, I think */
1134+
if (strlen(line) == MAX_QUERY_BUFFER-1)
11371135
{
11381136
fprintf(stderr, "line read exceeds maximum length. Truncating at %d\n",
1139-
MAX_QUERY_BUFFER);
1137+
MAX_QUERY_BUFFER-1);
11401138
}
11411139
return line;
11421140
}
@@ -2585,18 +2583,22 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
25852583
else
25862584
{
25872585
int i;
2588-
2586+
/*
2587+
* The current character is at line[i], the prior character
2588+
* at line[i - prevlen], the next character at line[i + thislen].
2589+
*/
25892590
#ifdef MULTIBYTE
2590-
int mblen = 1;
2591-
2591+
int prevlen = 0;
2592+
int thislen = (len > 0) ? PQmblen(line) : 0;
2593+
#define ADVANCE_I (prevlen = thislen, i += thislen, thislen = PQmblen(line+i))
2594+
#else
2595+
#define prevlen 1
2596+
#define thislen 1
2597+
#define ADVANCE_I (i++)
25922598
#endif
25932599

25942600
was_bslash = false;
2595-
#ifdef MULTIBYTE
2596-
for (i = 0; i < len; mblen = PQmblen(line + i), i += mblen)
2597-
#else
2598-
for (i = 0; i < len; i++)
2599-
#endif
2601+
for (i = 0; i < len; ADVANCE_I)
26002602
{
26012603
if (line[i] == '\\' && !in_quote)
26022604
{
@@ -2616,8 +2618,6 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
26162618
line[i] = hold_char;
26172619
query_start = line + i;
26182620
break; /* handle command */
2619-
2620-
/* start an extended comment? */
26212621
}
26222622

26232623
if (querySent &&
@@ -2630,55 +2630,30 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
26302630

26312631
if (was_bslash)
26322632
was_bslash = false;
2633-
#ifdef MULTIBYTE
2634-
else if (i > 0 && line[i - mblen] == '\\')
2635-
#else
2636-
else if (i > 0 && line[i - 1] == '\\')
2637-
#endif
2633+
else if (i > 0 && line[i - prevlen] == '\\')
26382634
was_bslash = true;
26392635

26402636
/* inside a quote? */
26412637
if (in_quote && (line[i] != in_quote || was_bslash))
26422638
/* do nothing */ ;
2643-
else if (xcomment != NULL) /* inside an extended
2644-
* comment? */
2639+
/* inside an extended comment? */
2640+
else if (xcomment != NULL)
26452641
{
2646-
#ifdef MULTIBYTE
2647-
if (line[i] == '*' && line[i + mblen] == '/')
2648-
#else
2649-
if (line[i] == '*' && line[i + 1] == '/')
2650-
#endif
2642+
if (line[i] == '*' && line[i + thislen] == '/')
26512643
{
26522644
xcomment = NULL;
2653-
#ifdef MULTIBYTE
2654-
i += mblen;
2655-
#else
2656-
i++;
2657-
#endif
2645+
ADVANCE_I;
26582646
}
26592647
}
2660-
/* possible backslash command? */
2661-
#ifdef MULTIBYTE
2662-
else if (line[i] == '/' && line[i + mblen] == '*')
2663-
#else
2664-
else if (line[i] == '/' && line[i + 1] == '*')
2665-
#endif
2648+
/* start of extended comment? */
2649+
else if (line[i] == '/' && line[i + thislen] == '*')
26662650
{
26672651
xcomment = line + i;
2668-
#ifdef MULTIBYTE
2669-
i += mblen;
2670-
#else
2671-
i++;
2672-
#endif
2652+
ADVANCE_I;
26732653
}
26742654
/* single-line comment? truncate line */
2675-
#ifdef MULTIBYTE
2676-
else if ((line[i] == '-' && line[i + mblen] == '-') ||
2677-
(line[i] == '/' && line[i + mblen] == '/'))
2678-
#else
2679-
else if ((line[i] == '-' && line[i + 1] == '-') ||
2680-
(line[i] == '/' && line[i + 1] == '/'))
2681-
#endif
2655+
else if ((line[i] == '-' && line[i + thislen] == '-') ||
2656+
(line[i] == '/' && line[i + thislen] == '/'))
26822657
{
26832658
/* print comment at top of query */
26842659
if (pset->singleStep)
@@ -2693,9 +2668,9 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
26932668
/* semi-colon? then send query now */
26942669
else if (!paren_level && line[i] == ';')
26952670
{
2696-
char hold_char = line[i + 1];
2671+
char hold_char = line[i + thislen];
26972672

2698-
line[i + 1] = '\0';
2673+
line[i + thislen] = '\0';
26992674
if (query_start[0] != '\0')
27002675
{
27012676
if (query[0] != '\0')
@@ -2708,11 +2683,10 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
27082683
}
27092684
success = SendQuery(pset, query, NULL, NULL);
27102685
successResult &= success;
2711-
line[i + 1] = hold_char;
2712-
query_start = line + i + 1;
2686+
line[i + thislen] = hold_char;
2687+
query_start = line + i + thislen;
27132688
/* sometimes, people do ';\g', don't execute twice */
2714-
if (*query_start && /* keeps us from going off the end */
2715-
*query_start == '\\' &&
2689+
if (*query_start == '\\' &&
27162690
*(query_start + 1) == 'g')
27172691
query_start += 2;
27182692
querySent = true;

0 commit comments

Comments
 (0)