| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* histfile.c - functions to manipulate the history file. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    This file contains the GNU History Library (History), a set of | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |    routines for managing the text of previously typed lines. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    History is free software: you can redistribute it and/or modify | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    History is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with History.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* The goal is to make the implementation transparent, so that you
 | 
					
						
							|  |  |  |    don't have to know what data types are used, just what functions | 
					
						
							|  |  |  |    you can call.  I think I have done that. */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #define READLINE_LIBRARY
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #if defined (__TANDEM)
 | 
					
						
							|  |  |  | #  include <floss.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #if defined (HAVE_CONFIG_H)
 | 
					
						
							|  |  |  | #  include <config.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <sys/types.h>
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #if ! defined (_MINIX) && defined (HAVE_SYS_FILE_H)
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #  include <sys/file.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #include "posixstat.h"
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_STDLIB_H)
 | 
					
						
							|  |  |  | #  include <stdlib.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #  include "ansi_stdlib.h"
 | 
					
						
							|  |  |  | #endif /* HAVE_STDLIB_H */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | #include <ctype.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (__EMX__)
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #  undef HAVE_MMAP
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #  include <sys/mman.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #  ifdef MAP_FILE
 | 
					
						
							|  |  |  | #    define MAP_RFLAGS	(MAP_FILE|MAP_PRIVATE)
 | 
					
						
							|  |  |  | #    define MAP_WFLAGS	(MAP_FILE|MAP_SHARED)
 | 
					
						
							|  |  |  | #  else
 | 
					
						
							|  |  |  | #    define MAP_RFLAGS	MAP_PRIVATE
 | 
					
						
							|  |  |  | #    define MAP_WFLAGS	MAP_SHARED
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #  ifndef MAP_FAILED
 | 
					
						
							|  |  |  | #    define MAP_FAILED	((void *)-1)
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #endif /* HISTORY_USE_MMAP */
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* If we're compiling for __EMX__ (OS/2) or __CYGWIN__ (cygwin32 environment
 | 
					
						
							|  |  |  |    on win 95/98/nt), we want to open files with O_BINARY mode so that there | 
					
						
							|  |  |  |    is no \n -> \r\n conversion performed.  On other systems, we don't want to | 
					
						
							|  |  |  |    mess around with O_BINARY at all, so we ensure that it's defined to 0. */ | 
					
						
							|  |  |  | #if defined (__EMX__) || defined (__CYGWIN__)
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #  ifndef O_BINARY
 | 
					
						
							|  |  |  | #    define O_BINARY 0
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #else /* !__EMX__ && !__CYGWIN__ */
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #  undef O_BINARY
 | 
					
						
							|  |  |  | #  define O_BINARY 0
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #endif /* !__EMX__ && !__CYGWIN__ */
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #if !defined (errno)
 | 
					
						
							|  |  |  | extern int errno; | 
					
						
							|  |  |  | #endif /* !errno */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "history.h"
 | 
					
						
							|  |  |  | #include "histlib.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #include "rlshell.h"
 | 
					
						
							|  |  |  | #include "xmalloc.h"
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /* If non-zero, we write timestamps to the history file in history_do_write() */ | 
					
						
							|  |  |  | int history_write_timestamps = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Does S look like the beginning of a history timestamp entry?  Placeholder
 | 
					
						
							|  |  |  |    for more extensive tests. */ | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | #define HIST_TIMESTAMP_START(s)		(*(s) == history_comment_char && isdigit ((s)[1]) )
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* Return the string that should be used in the place of this
 | 
					
						
							|  |  |  |    filename.  This only matters when you don't specify the | 
					
						
							|  |  |  |    filename to read_history (), or write_history (). */ | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | history_filename (filename) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   char *return_val; | 
					
						
							|  |  |  |   const char *home; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   int home_len; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return_val = filename ? savestring (filename) : (char *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (return_val) | 
					
						
							|  |  |  |     return (return_val); | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   home = sh_get_env_value ("HOME"); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (home == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       home = "."; | 
					
						
							|  |  |  |       home_len = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     home_len = strlen (home); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   return_val = (char *)xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   strcpy (return_val, home); | 
					
						
							|  |  |  |   return_val[home_len] = '/'; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #if defined (__MSDOS__)
 | 
					
						
							|  |  |  |   strcpy (return_val + home_len + 1, "_history"); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   strcpy (return_val + home_len + 1, ".history"); | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (return_val); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Add the contents of FILENAME to the history list, a line at a time.
 | 
					
						
							|  |  |  |    If FILENAME is NULL, then read from ~/.history.  Returns 0 if | 
					
						
							|  |  |  |    successful, or errno if not. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | read_history (filename) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   return (read_history_range (filename, 0, -1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Read a range of lines from FILENAME, adding them to the history list.
 | 
					
						
							|  |  |  |    Start reading at the FROM'th line and end at the TO'th.  If FROM | 
					
						
							|  |  |  |    is zero, start at the beginning.  If TO is less than FROM, read | 
					
						
							|  |  |  |    until the end of the file.  If FILENAME is NULL, then read from | 
					
						
							|  |  |  |    ~/.history.  Returns 0 if successful, or errno if not. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | read_history_range (filename, from, to) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |      int from, to; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   register char *line_start, *line_end, *p; | 
					
						
							|  |  |  |   char *input, *buffer, *bufend, *last_ts; | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   int file, current_line, chars_read; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   struct stat finfo; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   size_t file_size; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #if defined (EFBIG)
 | 
					
						
							|  |  |  |   int overflow_errno = EFBIG; | 
					
						
							|  |  |  | #elif defined (EOVERFLOW)
 | 
					
						
							|  |  |  |   int overflow_errno = EOVERFLOW; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |   int overflow_errno = EIO; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   buffer = last_ts = (char *)NULL; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   input = history_filename (filename); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   file = open (input, O_RDONLY|O_BINARY, 0666); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ((file < 0) || (fstat (file, &finfo) == -1)) | 
					
						
							|  |  |  |     goto error_and_exit; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   file_size = (size_t)finfo.st_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* check for overflow on very large files */ | 
					
						
							|  |  |  |   if (file_size != finfo.st_size || file_size + 1 < file_size) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       errno = overflow_errno; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |       goto error_and_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   /* We map read/write and private so we can change newlines to NULs without
 | 
					
						
							|  |  |  |      affecting the underlying object. */ | 
					
						
							|  |  |  |   buffer = (char *)mmap (0, file_size, PROT_READ|PROT_WRITE, MAP_RFLAGS, file, 0); | 
					
						
							|  |  |  |   if ((void *)buffer == MAP_FAILED) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       errno = overflow_errno; | 
					
						
							|  |  |  |       goto error_and_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   chars_read = file_size; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |   buffer = (char *)malloc (file_size + 1); | 
					
						
							|  |  |  |   if (buffer == 0) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       errno = overflow_errno; | 
					
						
							|  |  |  |       goto error_and_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   chars_read = read (file, buffer, file_size); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   if (chars_read < 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |   error_and_exit: | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       if (errno != 0) | 
					
						
							|  |  |  | 	chars_read = errno; | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  | 	chars_read = EIO; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       if (file >= 0) | 
					
						
							|  |  |  | 	close (file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       FREE (input); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifndef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       FREE (buffer); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       return (chars_read); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   close (file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Set TO to larger than end of file if negative. */ | 
					
						
							|  |  |  |   if (to < 0) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     to = chars_read; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Start at beginning of file, work to end. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   bufend = buffer + chars_read; | 
					
						
							|  |  |  |   current_line = 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Skip lines until we are at FROM. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   for (line_start = line_end = buffer; line_end < bufend && current_line < from; line_end++) | 
					
						
							|  |  |  |     if (*line_end == '\n') | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       	p = line_end + 1; | 
					
						
							|  |  |  |       	/* If we see something we think is a timestamp, continue with this
 | 
					
						
							|  |  |  | 	   line.  We should check more extensively here... */ | 
					
						
							|  |  |  | 	if (HIST_TIMESTAMP_START(p) == 0) | 
					
						
							|  |  |  | 	  current_line++; | 
					
						
							|  |  |  | 	line_start = p; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* If there are lines left to gobble, then gobble them now. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   for (line_end = line_start; line_end < bufend; line_end++) | 
					
						
							|  |  |  |     if (*line_end == '\n') | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | 	/* Change to allow Windows-like \r\n end of line delimiter. */ | 
					
						
							|  |  |  | 	if (line_end > line_start && line_end[-1] == '\r') | 
					
						
							|  |  |  | 	  line_end[-1] = '\0'; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	  *line_end = '\0'; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	if (*line_start) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	  { | 
					
						
							|  |  |  | 	    if (HIST_TIMESTAMP_START(line_start) == 0) | 
					
						
							|  |  |  | 	      { | 
					
						
							|  |  |  | 		add_history (line_start); | 
					
						
							|  |  |  | 		if (last_ts) | 
					
						
							|  |  |  | 		  { | 
					
						
							|  |  |  | 		    add_history_time (last_ts); | 
					
						
							|  |  |  | 		    last_ts = NULL; | 
					
						
							|  |  |  | 		  } | 
					
						
							|  |  |  | 	      } | 
					
						
							|  |  |  | 	    else | 
					
						
							|  |  |  | 	      { | 
					
						
							|  |  |  | 		last_ts = line_start; | 
					
						
							|  |  |  | 		current_line--; | 
					
						
							|  |  |  | 	      } | 
					
						
							|  |  |  | 	  } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	current_line++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (current_line >= to) | 
					
						
							|  |  |  | 	  break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	line_start = line_end + 1; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FREE (input); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifndef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   FREE (buffer); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |   munmap (buffer, file_size); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Truncate the history file FNAME, leaving only LINES trailing lines.
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |    If FNAME is NULL, then use ~/.history.  Returns 0 on success, errno | 
					
						
							|  |  |  |    on failure. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | int | 
					
						
							|  |  |  | history_truncate_file (fname, lines) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *fname; | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  |      int lines; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |   char *buffer, *filename, *bp, *bp1;		/* bp1 == bp+1 */ | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   int file, chars_read, rv; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   struct stat finfo; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   size_t file_size; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   buffer = (char *)NULL; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   filename = history_filename (fname); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   file = open (filename, O_RDONLY|O_BINARY, 0666); | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   rv = 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   /* Don't try to truncate non-regular files. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (file == -1 || fstat (file, &finfo) == -1) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       rv = errno; | 
					
						
							|  |  |  |       if (file != -1) | 
					
						
							|  |  |  | 	close (file); | 
					
						
							|  |  |  |       goto truncate_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   if (S_ISREG (finfo.st_mode) == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       close (file); | 
					
						
							|  |  |  | #ifdef EFTYPE
 | 
					
						
							|  |  |  |       rv = EFTYPE; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |       rv = EINVAL; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |       goto truncate_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   file_size = (size_t)finfo.st_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* check for overflow on very large files */ | 
					
						
							|  |  |  |   if (file_size != finfo.st_size || file_size + 1 < file_size) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       close (file); | 
					
						
							|  |  |  | #if defined (EFBIG)
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |       rv = errno = EFBIG; | 
					
						
							|  |  |  | #elif defined (EOVERFLOW)
 | 
					
						
							|  |  |  |       rv = errno = EOVERFLOW; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |       rv = errno = EINVAL; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |       goto truncate_exit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   buffer = (char *)malloc (file_size + 1); | 
					
						
							|  |  |  |   if (buffer == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       close (file); | 
					
						
							|  |  |  |       goto truncate_exit; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   chars_read = read (file, buffer, file_size); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   close (file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (chars_read <= 0) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       rv = (chars_read < 0) ? errno : 0; | 
					
						
							|  |  |  |       goto truncate_exit; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Count backwards from the end of buffer until we have passed
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |      LINES lines.  bp1 is set funny initially.  But since bp[1] can't | 
					
						
							|  |  |  |      be a comment character (since it's off the end) and *bp can't be | 
					
						
							|  |  |  |      both a newline and the history comment character, it should be OK. */ | 
					
						
							|  |  |  |   for (bp1 = bp = buffer + chars_read - 1; lines && bp > buffer; bp--) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       if (*bp == '\n' && HIST_TIMESTAMP_START(bp1) == 0) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	lines--; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       bp1 = bp; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If this is the first line, then the file contains exactly the
 | 
					
						
							|  |  |  |      number of lines we want to truncate to, so we don't need to do | 
					
						
							|  |  |  |      anything.  It's the first line if we don't find a newline between | 
					
						
							|  |  |  |      the current value of i and 0.  Otherwise, write from the start of | 
					
						
							|  |  |  |      this line until the end of the buffer. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   for ( ; bp > buffer; bp--) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (*bp == '\n' && HIST_TIMESTAMP_START(bp1) == 0) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  | 	  bp++; | 
					
						
							|  |  |  | 	  break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       bp1 = bp; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Write only if there are more lines in the file than we want to
 | 
					
						
							|  |  |  |      truncate to. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   if (bp > buffer && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0600)) != -1)) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       write (file, bp, chars_read - (bp - buffer)); | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined (__BEOS__)
 | 
					
						
							|  |  |  |       /* BeOS ignores O_TRUNC. */ | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       ftruncate (file, chars_read - (bp - buffer)); | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       close (file); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  truncate_exit: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FREE (buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   free (filename); | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   return rv; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Workhorse function for writing history.  Writes NELEMENT entries
 | 
					
						
							|  |  |  |    from the history list to FILENAME.  OVERWRITE is non-zero if you | 
					
						
							|  |  |  |    wish to replace FILENAME with the entries. */ | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | history_do_write (filename, nelements, overwrite) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |      int nelements, overwrite; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register int i; | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   char *output; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   int file, mode, rv; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   size_t cursize; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   mode = overwrite ? O_RDWR|O_CREAT|O_TRUNC|O_BINARY : O_RDWR|O_APPEND|O_BINARY; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   output = history_filename (filename); | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   rv = 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |   if ((file = open (output, mode, 0600)) == -1) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       FREE (output); | 
					
						
							|  |  |  |       return (errno); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   cursize = overwrite ? 0 : lseek (file, 0, SEEK_END); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (nelements > history_length) | 
					
						
							|  |  |  |     nelements = history_length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Build a buffer of all the lines to write, and write them in one syscall.
 | 
					
						
							|  |  |  |      Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */ | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     HIST_ENTRY **the_history;	/* local */ | 
					
						
							|  |  |  |     register int j; | 
					
						
							|  |  |  |     int buffer_size; | 
					
						
							|  |  |  |     char *buffer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     the_history = history_list (); | 
					
						
							|  |  |  |     /* Calculate the total number of bytes to write. */ | 
					
						
							|  |  |  |     for (buffer_size = 0, i = history_length - nelements; i < history_length; i++) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #if 0
 | 
					
						
							|  |  |  |       buffer_size += 2 + HISTENT_BYTES (the_history[i]); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  | 	if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0]) | 
					
						
							|  |  |  | 	  buffer_size += strlen (the_history[i]->timestamp) + 1; | 
					
						
							|  |  |  | 	buffer_size += strlen (the_history[i]->line) + 1; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Allocate the buffer, and fill it. */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |     if (ftruncate (file, buffer_size+cursize) == -1) | 
					
						
							|  |  |  |       goto mmap_error; | 
					
						
							|  |  |  |     buffer = (char *)mmap (0, buffer_size, PROT_READ|PROT_WRITE, MAP_WFLAGS, file, cursize); | 
					
						
							|  |  |  |     if ((void *)buffer == MAP_FAILED) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  | mmap_error: | 
					
						
							|  |  |  | 	rv = errno; | 
					
						
							|  |  |  | 	FREE (output); | 
					
						
							|  |  |  | 	close (file); | 
					
						
							|  |  |  | 	return rv; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | #else    
 | 
					
						
							|  |  |  |     buffer = (char *)malloc (buffer_size); | 
					
						
							|  |  |  |     if (buffer == 0) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |       	rv = errno; | 
					
						
							|  |  |  | 	FREE (output); | 
					
						
							|  |  |  | 	close (file); | 
					
						
							|  |  |  | 	return rv; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (j = 0, i = history_length - nelements; i < history_length; i++) | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 	if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0]) | 
					
						
							|  |  |  | 	  { | 
					
						
							|  |  |  | 	    strcpy (buffer + j, the_history[i]->timestamp); | 
					
						
							|  |  |  | 	    j += strlen (the_history[i]->timestamp); | 
					
						
							|  |  |  | 	    buffer[j++] = '\n'; | 
					
						
							|  |  |  | 	  } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	strcpy (buffer + j, the_history[i]->line); | 
					
						
							|  |  |  | 	j += strlen (the_history[i]->line); | 
					
						
							|  |  |  | 	buffer[j++] = '\n'; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef HISTORY_USE_MMAP
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |     if (msync (buffer, buffer_size, 0) != 0 || munmap (buffer, buffer_size) != 0) | 
					
						
							|  |  |  |       rv = errno; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |     if (write (file, buffer, buffer_size) < 0) | 
					
						
							|  |  |  |       rv = errno; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     free (buffer); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   close (file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FREE (output); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   return (rv); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Append NELEMENT entries to FILENAME.  The entries appended are from
 | 
					
						
							|  |  |  |    the end of the list minus NELEMENTs up to the end of the list. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | append_history (nelements, filename) | 
					
						
							|  |  |  |      int nelements; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   return (history_do_write (filename, nelements, HISTORY_APPEND)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Overwrite FILENAME with the current history.  If FILENAME is NULL,
 | 
					
						
							|  |  |  |    then write the history list to ~/.history.  Values returned | 
					
						
							|  |  |  |    are as in read_history ().*/ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | write_history (filename) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |      const char *filename; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   return (history_do_write (filename, history_length, HISTORY_OVERWRITE)); | 
					
						
							|  |  |  | } |