Hi all,

Attached is a patch for the following TODO item:

o Improve psql's handling of multi-line queries

  Currently, while \e saves a single query as one entry, interactive
  queries are saved one line at a time.  Ideally all queries
  whould be saved like \e does.

I know now's probably not a good time, but it caught my eye and looked
easy enough even for my rusty C.

Regards, Steve Woodcock
Index: input.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.c,v
retrieving revision 1.45
diff -c -r1.45 input.c
*** input.c	10 Jun 2005 15:40:41 -0000	1.45
--- input.c	28 Aug 2005 13:00:30 -0000
***************
*** 24,29 ****
--- 24,31 ----
  #ifdef USE_READLINE
  static bool useReadline;
  static bool useHistory;
+ static PQExpBuffer hist_buf;	/* buffer for history entry being accumulated */
+ static PQExpBuffer prev_buf;	/* previous history entry */
  char  *psql_history;
  
  
***************
*** 90,97 ****
  #ifdef USE_READLINE
  	char	   *s;
  
- 	static char *prev_hist = NULL;
- 
  	if (useReadline)
  		/* On some platforms, readline is declared as readline(char *) */
  		s = readline((char *) prompt);
--- 92,97 ----
***************
*** 100,120 ****
  
  	if (useHistory && s && s[0])
  	{
! 		enum histcontrol HC;
! 
! 		HC = GetHistControlConfig();
! 
! 		if (((HC & hctl_ignorespace) && s[0] == ' ') ||
! 			((HC & hctl_ignoredups) && prev_hist && strcmp(s, prev_hist) == 0))
! 		{
! 			/* Ignore this line as far as history is concerned */
! 		}
! 		else
! 		{
! 			free(prev_hist);
! 			prev_hist = pg_strdup(s);
! 			add_history(s);
! 		}
  	}
  
  	return s;
--- 100,109 ----
  
  	if (useHistory && s && s[0])
  	{
! 		/* Append line to history buffer, separating with newline */
! 		if (hist_buf->len > 0)
! 			appendPQExpBufferChar(hist_buf, '\n');
! 		appendPQExpBufferStr(hist_buf, s);
  	}
  
  	return s;
***************
*** 197,202 ****
--- 186,194 ----
  
  		if (psql_history)
  			read_history(psql_history);
+ 
+ 		hist_buf = createPQExpBuffer();
+ 		prev_buf = createPQExpBuffer();
  	}
  #endif
  
***************
*** 228,233 ****
--- 220,257 ----
  }
  
  
+ void
+ maybeAddHistory(void)
+ {
+ #ifdef USE_READLINE
+ 	if (useHistory)
+ 	{
+ 		enum histcontrol HC;
+ 
+ 		HC = GetHistControlConfig();
+ 
+ 		if (((HC & hctl_ignorespace) && hist_buf->data[0] == ' ') ||
+ 			((HC & hctl_ignoredups) && strcmp(hist_buf->data, prev_buf->data) == 0))
+ 		{
+ 			/* Ignore this buffer as far as history is concerned */
+ 			resetPQExpBuffer(hist_buf);
+ 		}
+ 		else
+ 		{
+ 			PQExpBuffer tmp;
+ 
+ 			add_history(hist_buf->data);
+ 
+ 			/* swap hist and prev and reset hist */
+ 			tmp = prev_buf;
+ 			prev_buf = hist_buf;
+ 			hist_buf = tmp;
+ 			resetPQExpBuffer(hist_buf);
+ 		}
+ 	}
+ #endif
+ }
+ 
  
  static void
  #ifdef HAVE_ATEXIT
Index: input.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/input.h,v
retrieving revision 1.23
diff -c -r1.23 input.h
*** input.h	1 Jan 2005 05:43:08 -0000	1.23
--- input.h	28 Aug 2005 13:00:30 -0000
***************
*** 38,42 ****
--- 38,43 ----
  
  void		initializeInput(int flags);
  bool		saveHistory(char *fname);
+ void		maybeAddHistory(void);
  
  #endif   /* INPUT_H */
Index: mainloop.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/mainloop.c,v
retrieving revision 1.67
diff -c -r1.67 mainloop.c
*** mainloop.c	22 Feb 2005 04:40:55 -0000	1.67
--- mainloop.c	28 Aug 2005 13:00:31 -0000
***************
*** 286,291 ****
--- 286,297 ----
  					break;
  			}
  
+ #ifdef USE_READLINE
+ 			if (pset.cur_cmd_interactive &&
+ 				scan_result == PSCAN_INCOMPLETE)
+ 				maybeAddHistory();
+ #endif
+ 
  			/* fall out of loop if lexer reached EOL */
  			if (scan_result == PSCAN_INCOMPLETE ||
  				scan_result == PSCAN_EOL)



---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to