1 /* histfile.c - functions to manipulate the history file. */
3 /* Copyright (C) 1989, 1992 Free Software Foundation, Inc.
5 This file contains the GNU History Library (the Library), a set of
6 routines for managing the text of previously typed lines.
8 The Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 The Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 The GNU General Public License is often shipped with GNU software, and
19 is generally kept in a file called COPYING or LICENSE. If you do not
20 have a copy of the license, write to the Free Software Foundation,
21 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* The goal is to make the implementation transparent, so that you
24 don't have to know what data types are used, just what functions
25 you can call. I think I have done that. */
26 #define READLINE_LIBRARY
28 #if defined (HAVE_CONFIG_H)
34 #include <sys/types.h>
39 #if defined (HAVE_STDLIB_H)
42 # include "ansi_stdlib.h"
43 #endif /* HAVE_STDLIB_H */
45 #if defined (HAVE_UNISTD_H)
49 #if defined (HAVE_STRING_H)
53 #endif /* !HAVE_STRING_H */
60 /* If we're not compiling for __EMX__, we don't want this at all. Ever. */
73 /* Functions imported from shell.c */
74 extern char *get_env_value ();
76 extern char *xmalloc (), *xrealloc ();
78 /* Return the string that should be used in the place of this
79 filename. This only matters when you don't specify the
80 filename to read_history (), or write_history (). */
82 history_filename (filename)
85 char *return_val, *home;
88 return_val = filename ? savestring (filename) : (char *)NULL;
93 home = get_env_value ("HOME");
101 home_len = strlen (home);
103 return_val = xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */
104 strcpy (return_val, home);
105 return_val[home_len] = '/';
106 strcpy (return_val + home_len + 1, ".history");
111 /* Add the contents of FILENAME to the history list, a line at a time.
112 If FILENAME is NULL, then read from ~/.history. Returns 0 if
113 successful, or errno if not. */
115 read_history (filename)
118 return (read_history_range (filename, 0, -1));
121 /* Read a range of lines from FILENAME, adding them to the history list.
122 Start reading at the FROM'th line and end at the TO'th. If FROM
123 is zero, start at the beginning. If TO is less than FROM, read
124 until the end of the file. If FILENAME is NULL, then read from
125 ~/.history. Returns 0 if successful, or errno if not. */
127 read_history_range (filename, from, to)
131 register int line_start, line_end;
132 char *input, *buffer = (char *)NULL;
133 int file, current_line;
136 input = history_filename (filename);
137 file = open (input, O_RDONLY|O_BINARY, 0666);
139 if ((file < 0) || (fstat (file, &finfo) == -1))
142 buffer = xmalloc ((int)finfo.st_size + 1);
144 if (read (file, buffer, finfo.st_size) != finfo.st_size)
158 /* Set TO to larger than end of file if negative. */
162 /* Start at beginning of file, work to end. */
163 line_start = line_end = current_line = 0;
165 /* Skip lines until we are at FROM. */
166 while (line_start < finfo.st_size && current_line < from)
168 for (line_end = line_start; line_end < finfo.st_size; line_end++)
169 if (buffer[line_end] == '\n')
172 line_start = line_end + 1;
173 if (current_line == from)
178 /* If there are lines left to gobble, then gobble them now. */
179 for (line_end = line_start; line_end < finfo.st_size; line_end++)
180 if (buffer[line_end] == '\n')
182 buffer[line_end] = '\0';
184 if (buffer[line_start])
185 add_history (buffer + line_start);
189 if (current_line >= to)
192 line_start = line_end + 1;
201 /* Truncate the history file FNAME, leaving only LINES trailing lines.
202 If FNAME is NULL, then use ~/.history. */
204 history_truncate_file (fname, lines)
209 int file, chars_read;
210 char *buffer, *filename;
213 buffer = (char *)NULL;
214 filename = history_filename (fname);
215 file = open (filename, O_RDONLY|O_BINARY, 0666);
217 if (file == -1 || fstat (file, &finfo) == -1)
220 buffer = xmalloc ((int)finfo.st_size + 1);
221 chars_read = read (file, buffer, finfo.st_size);
227 /* Count backwards from the end of buffer until we have passed
229 for (i = chars_read - 1; lines && i; i--)
231 if (buffer[i] == '\n')
235 /* If this is the first line, then the file contains exactly the
236 number of lines we want to truncate to, so we don't need to do
237 anything. It's the first line if we don't find a newline between
238 the current value of i and 0. Otherwise, write from the start of
239 this line until the end of the buffer. */
241 if (buffer[i] == '\n')
247 /* Write only if there are more lines in the file than we want to
249 if (i && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0666)) != -1))
251 write (file, buffer + i, finfo.st_size - i);
263 /* Workhorse function for writing history. Writes NELEMENT entries
264 from the history list to FILENAME. OVERWRITE is non-zero if you
265 wish to replace FILENAME with the entries. */
267 history_do_write (filename, nelements, overwrite)
269 int nelements, overwrite;
275 mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
276 output = history_filename (filename);
278 if ((file = open (output, mode, 0666)) == -1)
284 if (nelements > history_length)
285 nelements = history_length;
287 /* Build a buffer of all the lines to write, and write them in one syscall.
288 Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
290 HIST_ENTRY **the_history; /* local */
295 the_history = history_list ();
296 /* Calculate the total number of bytes to write. */
297 for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
298 buffer_size += 1 + strlen (the_history[i]->line);
300 /* Allocate the buffer, and fill it. */
301 buffer = xmalloc (buffer_size);
303 for (j = 0, i = history_length - nelements; i < history_length; i++)
305 strcpy (buffer + j, the_history[i]->line);
306 j += strlen (the_history[i]->line);
310 write (file, buffer, buffer_size);
321 /* Append NELEMENT entries to FILENAME. The entries appended are from
322 the end of the list minus NELEMENTs up to the end of the list. */
324 append_history (nelements, filename)
328 return (history_do_write (filename, nelements, HISTORY_APPEND));
331 /* Overwrite FILENAME with the current history. If FILENAME is NULL,
332 then write the history list to ~/.history. Values returned
333 are as in read_history ().*/
335 write_history (filename)
338 return (history_do_write (filename, history_length, HISTORY_OVERWRITE));