Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22fe451

Browse files
committed
psql's recognition of comments didn't work right in MULTIBYTE
environments; it was being careless about character lengths.
1 parent109cbc7 commit22fe451

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-
intlen;
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
returnNULL;
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
returnline;
11421140
}
@@ -2585,18 +2583,22 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
25852583
else
25862584
{
25872585
inti;
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
#ifdefMULTIBYTE
2590-
intmblen=1;
2591-
2591+
intprevlen=0;
2592+
intthislen= (len>0) ?PQmblen(line) :0;
2593+
#defineADVANCE_I (prevlen = thislen, i += thislen, thislen = PQmblen(line+i))
2594+
#else
2595+
#defineprevlen 1
2596+
#definethislen 1
2597+
#defineADVANCE_I (i++)
25922598
#endif
25932599

25942600
was_bslash= false;
2595-
#ifdefMULTIBYTE
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-
#ifdefMULTIBYTE
2634-
elseif (i>0&&line[i-mblen]== '\\')
2635-
#else
2636-
elseif (i>0&&line[i-1]== '\\')
2637-
#endif
2633+
elseif (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-
elseif (xcomment!=NULL)/* inside an extended
2644-
* comment? */
2639+
/* inside an extended comment? */
2640+
elseif (xcomment!=NULL)
26452641
{
2646-
#ifdefMULTIBYTE
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-
#ifdefMULTIBYTE
2654-
i+=mblen;
2655-
#else
2656-
i++;
2657-
#endif
2645+
ADVANCE_I;
26582646
}
26592647
}
2660-
/* possible backslash command? */
2661-
#ifdefMULTIBYTE
2662-
elseif (line[i]=='/'&&line[i+mblen]=='*')
2663-
#else
2664-
elseif (line[i]=='/'&&line[i+1]=='*')
2665-
#endif
2648+
/* start of extended comment? */
2649+
elseif (line[i]=='/'&&line[i+thislen]=='*')
26662650
{
26672651
xcomment=line+i;
2668-
#ifdefMULTIBYTE
2669-
i+=mblen;
2670-
#else
2671-
i++;
2672-
#endif
2652+
ADVANCE_I;
26732653
}
26742654
/* single-line comment? truncate line */
2675-
#ifdefMULTIBYTE
2676-
elseif ((line[i]=='-'&&line[i+mblen]=='-')||
2677-
(line[i]=='/'&&line[i+mblen]=='/'))
2678-
#else
2679-
elseif ((line[i]=='-'&&line[i+1]=='-')||
2680-
(line[i]=='/'&&line[i+1]=='/'))
2681-
#endif
2655+
elseif ((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
elseif (!paren_level&&line[i]==';')
26952670
{
2696-
charhold_char=line[i+1];
2671+
charhold_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)

[8]ページ先頭

©2009-2025 Movatter.jp