1 /* bashhist.c -- bash interface to the GNU history library. */
3 /* Copyright (C) 1993 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <sys/types.h>
25 #include "posixstat.h"
29 #include <readline/history.h>
31 /* Declarations of bash history variables. */
32 /* Non-zero means to remember lines typed to the shell on the history
33 list. This is different than the user-controlled behaviour; this
34 becomes zero when we read lines from a file, for example. */
35 int remember_on_history = 1;
37 /* The number of lines that Bash has added to this history session. */
38 int history_lines_this_session = 0;
40 /* The number of lines that Bash has read from the history file. */
41 int history_lines_in_file = 0;
43 /* Non-zero means do no history expansion on this line, regardless
44 of what history_expansion says. */
45 int history_expansion_inhibited = 0;
47 /* By default, every line is saved in the history individually. I.e.,
53 Each line will be individually saved in the history.
60 If the variable command_oriented_history is set, multiple lines
61 which form one command will be saved as one history entry.
72 The user can then recall the whole command all at once instead
73 of just being able to recall one line at a time.
75 int command_oriented_history = 0;
77 /* A nit for picking at history saving.
78 Value of 0 means save all lines parsed by the shell on the history.
79 Value of 1 means save all lines that do not start with a space.
80 Value of 2 means save all lines that do not match the last line saved. */
81 int history_control = 0;
83 /* Variables declared in other files used here. */
84 extern int interactive;
85 extern int current_command_line_count;
86 extern int delimiter_depth;
88 extern char *history_delimiting_chars ();
89 extern void maybe_add_history (); /* forward declaration */
91 static void bash_add_history ();
93 /* Load the history list from the history file. */
99 /* Truncate history file for interactive shells which desire it.
100 Note that the history file is automatically truncated to the
101 size of HISTSIZE if the user does not explicitly set the size
103 set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
104 stupidly_hack_special_variables ("HISTFILESIZE");
106 /* Read the history in HISTFILE into the history list. */
107 hf = get_string_value ("HISTFILE");
113 if (stat (hf, &buf) == 0)
117 history_lines_in_file = where_history ();
122 /* Write the existing history out to the history file. */
126 char *hf = get_string_value ("HISTFILE");
132 if (stat (hf, &buf) == 0)
134 /* Append only the lines that occurred this session to
138 if (history_lines_this_session < where_history ())
139 append_history (history_lines_this_session, hf);
146 /* If this is an interactive shell, then append the lines executed
147 this session to the history file. */
149 maybe_save_shell_history ()
153 if (history_lines_this_session)
155 char *hf = get_string_value ("HISTFILE");
161 /* If the file doesn't exist, then create it. */
162 if (stat (hf, &buf) == -1)
164 int file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
169 /* Now actually append the lines if the history hasn't been
170 stifled. If the history has been stifled, rewrite the
173 if (history_lines_this_session <= where_history ())
175 result = append_history (history_lines_this_session, hf);
176 history_lines_in_file += history_lines_this_session;
180 result = write_history (hf);
181 history_lines_in_file = history_lines_this_session;
183 history_lines_this_session = 0;
189 #if defined (HISTORY_REEDITING)
190 /* Tell readline () that we have some text for it to edit. */
195 #if defined (READLINE)
196 if (strcmp (bash_input.name, "readline stdin") == 0)
198 #endif /* READLINE */
200 #endif /* HISTORY_REEDITING */
202 /* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then
203 print the results of expanding the line if there were any changes.
204 If there is an error, return NULL, otherwise the expanded line is
205 returned. If ADDIT is non-zero the line is added to the history
206 list after history expansion. ADDIT is just a suggestion;
207 REMEMBER_ON_HISTORY can veto, and does.
208 Right now this does history expansion. */
210 pre_process_line (line, print_changes, addit)
212 int print_changes, addit;
220 # if defined (BANG_HISTORY)
221 /* History expand the line. If this results in no errors, then
222 add that line to the history if ADDIT is non-zero. */
223 if (!history_expansion_inhibited && history_expansion)
225 expanded = history_expand (line, &history_value);
232 internal_error (history_value);
234 fprintf (stderr, "%s\n", history_value);
237 /* If there was an error, return NULL. */
238 if (expanded < 0 || expanded == 2) /* 2 == print only */
240 free (history_value);
242 # if defined (HISTORY_REEDITING)
243 /* New hack. We can allow the user to edit the
244 failed history expansion. */
246 # endif /* HISTORY_REEDITING */
247 return ((char *)NULL);
251 /* Let other expansions know that return_value can be free'ed,
252 and that a line has been added to the history list. Note
253 that we only add lines that have something in them. */
255 return_value = history_value;
257 # endif /* BANG_HISTORY */
259 if (addit && remember_on_history && *return_value)
260 maybe_add_history (return_value);
263 return_value = savestring (line);
265 return (return_value);
268 /* Add LINE to the history list depending on the value of HISTORY_CONTROL. */
270 maybe_add_history (line)
275 /* Don't use the value of history_control to affect the second
276 and subsequent lines of a multi-line command when
277 command_oriented_history is enabled. */
278 if (command_oriented_history && current_command_line_count > 1)
286 bash_add_history (line);
290 bash_add_history (line);
295 /* FALLTHROUGH if case == 3 (`ignoreboth') */
301 temp = previous_history ();
303 if (!temp || (STREQ (temp->line, line) == 0))
304 bash_add_history (line);
312 /* Add a line to the history list.
313 The variable COMMAND_ORIENTED_HISTORY controls the style of history
314 remembering; when non-zero, and LINE is not the first line of a
315 complete parser construct, append LINE to the last history line instead
316 of adding it as a new line. */
318 bash_add_history (line)
323 if (command_oriented_history && current_command_line_count > 1)
326 register HIST_ENTRY *current, *old;
327 char *chars_to_add, *new_line;
329 chars_to_add = history_delimiting_chars ();
333 current = previous_history ();
337 /* If the previous line ended with an escaped newline (escaped
338 with backslash, but otherwise unquoted), then remove the quoted
339 newline, since that is what happens when the line is parsed. */
342 curlen = strlen (current->line);
344 if (!delimiter_depth && current->line[curlen - 1] == '\\' &&
345 current->line[curlen - 2] != '\\')
347 current->line[curlen - 1] = '\0';
352 offset = where_history ();
353 new_line = (char *) xmalloc (1
356 + strlen (chars_to_add));
357 sprintf (new_line, "%s%s%s", current->line, chars_to_add, line);
358 old = replace_history_entry (offset, new_line, current->data);
363 /* Note that the old data is not freed, since it was simply
364 copied to the new history entry. */
377 history_lines_this_session++;
386 if (get_string_value ("HISTSIZE"))
387 return (history_base + where_history ());
389 return (1); /* default to command number 1 */