7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -1120,9 +1120,8 @@ static char *
1120
1120
gets_fromFile (char * prompt , FILE * source )
1121
1121
{
1122
1122
char * line ;
1123
- int len ;
1124
1123
1125
- line = malloc (MAX_QUERY_BUFFER + 1 );
1124
+ line = malloc (MAX_QUERY_BUFFER );
1126
1125
1127
1126
/* read up to MAX_QUERY_BUFFER characters */
1128
1127
if (fgets (line , MAX_QUERY_BUFFER , source ) == NULL )
@@ -1131,12 +1130,11 @@ gets_fromFile(char *prompt, FILE *source)
1131
1130
return NULL ;
1132
1131
}
1133
1132
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 )
1137
1135
{
1138
1136
fprintf (stderr , "line read exceeds maximum length. Truncating at %d\n" ,
1139
- MAX_QUERY_BUFFER );
1137
+ MAX_QUERY_BUFFER - 1 );
1140
1138
}
1141
1139
return line ;
1142
1140
}
@@ -2585,18 +2583,22 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
2585
2583
else
2586
2584
{
2587
2585
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
+ */
2589
2590
#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++)
2592
2598
#endif
2593
2599
2594
2600
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 )
2600
2602
{
2601
2603
if (line [i ] == '\\' && !in_quote )
2602
2604
{
@@ -2616,8 +2618,6 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
2616
2618
line [i ] = hold_char ;
2617
2619
query_start = line + i ;
2618
2620
break ; /* handle command */
2619
-
2620
- /* start an extended comment? */
2621
2621
}
2622
2622
2623
2623
if (querySent &&
@@ -2630,55 +2630,30 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
2630
2630
2631
2631
if (was_bslash )
2632
2632
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 ] == '\\' )
2638
2634
was_bslash = true;
2639
2635
2640
2636
/* inside a quote? */
2641
2637
if (in_quote && (line [i ] != in_quote || was_bslash ))
2642
2638
/* do nothing */ ;
2643
- else if ( xcomment != NULL ) /* inside an extended
2644
- * comment? */
2639
+ /* inside an extended comment? */
2640
+ else if ( xcomment != NULL )
2645
2641
{
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 ] == '/' )
2651
2643
{
2652
2644
xcomment = NULL ;
2653
- #ifdef MULTIBYTE
2654
- i += mblen ;
2655
- #else
2656
- i ++ ;
2657
- #endif
2645
+ ADVANCE_I ;
2658
2646
}
2659
2647
}
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 ] == '*' )
2666
2650
{
2667
2651
xcomment = line + i ;
2668
- #ifdef MULTIBYTE
2669
- i += mblen ;
2670
- #else
2671
- i ++ ;
2672
- #endif
2652
+ ADVANCE_I ;
2673
2653
}
2674
2654
/* 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 ] == '/' ))
2682
2657
{
2683
2658
/* print comment at top of query */
2684
2659
if (pset -> singleStep )
@@ -2693,9 +2668,9 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
2693
2668
/* semi-colon? then send query now */
2694
2669
else if (!paren_level && line [i ] == ';' )
2695
2670
{
2696
- char hold_char = line [i + 1 ];
2671
+ char hold_char = line [i + thislen ];
2697
2672
2698
- line [i + 1 ] = '\0' ;
2673
+ line [i + thislen ] = '\0' ;
2699
2674
if (query_start [0 ] != '\0' )
2700
2675
{
2701
2676
if (query [0 ] != '\0' )
@@ -2708,11 +2683,10 @@ MainLoop(PsqlSettings *pset, char *query, FILE *source)
2708
2683
}
2709
2684
success = SendQuery (pset , query , NULL , NULL );
2710
2685
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 ;
2713
2688
/* 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 == '\\' &&
2716
2690
* (query_start + 1 ) == 'g' )
2717
2691
query_start += 2 ;
2718
2692
querySent = true;
0 commit comments