33 *
44 * Copyright 2000 by PostgreSQL Global Development Group
55 *
6- * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.31 2000/06/29 16:27:57 momjian Exp $
6+ * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.32 2000/06/30 18:03:40 momjian Exp $
77 */
88#include "postgres.h"
99#include "mainloop.h"
@@ -44,7 +44,7 @@ MainLoop(FILE *source)
4444
4545bool success ;
4646volatile char in_quote ;/* == 0 for no in_quote */
47- volatile bool xcomment ; /* in extended comment */
47+ volatile bool in_xcomment ; /* in extended comment */
4848volatile int paren_level ;
4949unsignedint query_start ;
5050volatile int count_eof = 0 ;
@@ -80,7 +80,7 @@ MainLoop(FILE *source)
8080exit (EXIT_FAILURE );
8181}
8282
83- xcomment = false;
83+ in_xcomment = false;
8484in_quote = 0 ;
8585paren_level = 0 ;
8686slashCmdStatus = CMD_UNKNOWN ;/* set default */
@@ -123,7 +123,7 @@ MainLoop(FILE *source)
123123resetPQExpBuffer (query_buf );
124124
125125/* reset parsing state */
126- xcomment = false;
126+ in_xcomment = false;
127127in_quote = 0 ;
128128paren_level = 0 ;
129129count_eof = 0 ;
@@ -147,7 +147,7 @@ MainLoop(FILE *source)
147147line = xstrdup (query_buf -> data );
148148resetPQExpBuffer (query_buf );
149149/* reset parsing state since we are rescanning whole line */
150- xcomment = false;
150+ in_xcomment = false;
151151in_quote = 0 ;
152152paren_level = 0 ;
153153slashCmdStatus = CMD_UNKNOWN ;
@@ -168,7 +168,7 @@ MainLoop(FILE *source)
168168prompt_status = PROMPT_SINGLEQUOTE ;
169169else if (in_quote && in_quote == '"' )
170170prompt_status = PROMPT_DOUBLEQUOTE ;
171- else if (xcomment )
171+ else if (in_xcomment )
172172prompt_status = PROMPT_COMMENT ;
173173else if (paren_level )
174174prompt_status = PROMPT_PAREN ;
@@ -296,43 +296,51 @@ MainLoop(FILE *source)
296296bslash_count = 0 ;
297297
298298rescan :
299- /* start of extended comment? */
300- if (line [i ]== '/' && line [i + thislen ]== '*' )
299+ /*
300+ *It is important to place the in_* test routines
301+ * before the in_* detection routines.
302+ *i.e. we have to test if we are in a quote before
303+ *testing for comments. bjm 2000-06-30
304+ */
305+
306+ /* in quote? */
307+ if (in_quote )
301308{
302- xcomment = true;
303- ADVANCE_1 ;
309+ /* end of quote */
310+ if (line [i ]== in_quote && bslash_count %2 == 0 )
311+ in_quote = '\0' ;
304312}
305313
306314/* in extended comment? */
307- else if (xcomment )
315+ else if (in_xcomment )
308316{
309317if (line [i ]== '*' && line [i + thislen ]== '/' )
310318{
311- xcomment = false;
319+ in_xcomment = false;
312320ADVANCE_1 ;
313321}
314322}
315323
316- /* single-line comment? truncate line */
317- else if (line [i ]== '-' && line [i + thislen ]== '-' )
318- {
319- line [i ]= '\0' ;/* remove comment */
320- break ;
321- }
322-
323- /* in quote? */
324- else if (in_quote )
324+ /* start of extended comment? */
325+ else if (line [i ]== '/' && line [i + thislen ]== '*' )
325326{
326- /* end of quote */
327- if (line [i ]== in_quote && bslash_count %2 == 0 )
328- in_quote = '\0' ;
327+ in_xcomment = true;
328+ ADVANCE_1 ;
329329}
330330
331331/* start of quote */
332332else if (!was_bslash &&
333333 (line [i ]== '\'' || line [i ]== '"' ))
334334in_quote = line [i ];
335335
336+
337+ /* single-line comment? truncate line */
338+ else if (line [i ]== '-' && line [i + thislen ]== '-' )
339+ {
340+ line [i ]= '\0' ;/* remove comment */
341+ break ;
342+ }
343+
336344/* count nested parentheses */
337345else if (line [i ]== '(' )
338346paren_level ++ ;