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. */
25 #if defined (HAVE_UNISTD_H)
29 #include "bashtypes.h"
33 #include "posixstat.h"
38 #include "parser.h" /* for the struct dstack stuff. */
39 #include "pathexp.h" /* for the struct ignorevar stuff */
40 #include "builtins/common.h"
41 #include <readline/history.h>
42 #include <glob/fnmatch.h>
44 #if defined (READLINE)
45 # include "bashline.h"
52 static int histignore_item_func ();
54 static struct ignorevar histignore
=
60 (Function
*)histignore_item_func
,
63 #define HIGN_EXPAND 0x01
65 /* Declarations of bash history variables. */
66 /* Non-zero means to remember lines typed to the shell on the history
67 list. This is different than the user-controlled behaviour; this
68 becomes zero when we read lines from a file, for example. */
69 int remember_on_history
= 1;
71 /* The number of lines that Bash has added to this history session. */
72 int history_lines_this_session
;
74 /* The number of lines that Bash has read from the history file. */
75 int history_lines_in_file
;
77 #if defined (BANG_HISTORY)
78 /* Non-zero means do no history expansion on this line, regardless
79 of what history_expansion says. */
80 int history_expansion_inhibited
;
83 /* By default, every line is saved in the history individually. I.e.,
89 Each line will be individually saved in the history.
96 If the variable command_oriented_history is set, multiple lines
97 which form one command will be saved as one history entry.
108 The user can then recall the whole command all at once instead
109 of just being able to recall one line at a time.
111 int command_oriented_history
= 1;
113 /* Non-zero means to store newlines in the history list when using
114 command_oriented_history rather than trying to use semicolons. */
117 /* Non-zero means to append the history to the history file at shell
118 exit, even if the history has been stifled. */
119 int force_append_history
;
121 /* A nit for picking at history saving.
122 Value of 0 means save all lines parsed by the shell on the history.
123 Value of 1 means save all lines that do not start with a space.
124 Value of 2 means save all lines that do not match the last line saved. */
127 #if defined (READLINE)
128 /* If non-zero, and readline is being used, the user is offered the
129 chance to re-edit a failed history expansion. */
130 int history_reediting
;
132 /* If non-zero, and readline is being used, don't directly execute a
133 line with history substitution. Reload it into the editing buffer
134 instead and let the user further edit and confirm with a newline. */
138 /* Variables declared in other files used here. */
139 extern int interactive
;
140 extern int current_command_line_count
;
142 extern struct dstack dstack
;
144 extern char *extract_colon_unit ();
145 extern char *history_delimiting_chars ();
146 extern void maybe_add_history (); /* forward declaration */
148 static void bash_add_history ();
149 static int history_should_ignore ();
152 bash_initialize_history ()
154 history_quotes_inhibit_expansion
= 1;
155 history_search_delimiter_chars
= ";&()|<>";
159 bash_history_reinit (interact
)
162 #if defined (BANG_HISTORY)
163 history_expansion
= interact
!= 0;
164 history_expansion_inhibited
= 1;
166 remember_on_history
= interact
!= 0;
170 bash_history_disable ()
172 remember_on_history
= 0;
173 #if defined (BANG_HISTORY)
174 history_expansion_inhibited
= 1;
179 bash_history_enable ()
181 remember_on_history
= 1;
182 #if defined (BANG_HISTORY)
183 history_expansion_inhibited
= 0;
185 sv_history_control ("HISTCONTROL");
186 sv_histignore ("HISTIGNORE");
189 /* Load the history list from the history file. */
196 /* Truncate history file for interactive shells which desire it.
197 Note that the history file is automatically truncated to the
198 size of HISTSIZE if the user does not explicitly set the size
200 set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
201 sv_histsize ("HISTFILESIZE");
203 /* Read the history in HISTFILE into the history list. */
204 hf
= get_string_value ("HISTFILE");
206 if (hf
&& *hf
&& stat (hf
, &buf
) == 0)
210 history_lines_in_file
= where_history ();
214 /* Write the existing history out to the history file. */
221 hf
= get_string_value ("HISTFILE");
222 if (hf
&& *hf
&& stat (hf
, &buf
) == 0)
224 /* Append only the lines that occurred this session to
228 if (history_lines_this_session
< where_history () || force_append_history
)
229 append_history (history_lines_this_session
, hf
);
233 sv_histsize ("HISTFILESIZE");
238 maybe_append_history (filename
)
244 result
= EXECUTION_SUCCESS
;
245 if (history_lines_this_session
&& (history_lines_this_session
< where_history ()))
247 /* If the filename was supplied, then create it if necessary. */
248 if (stat (filename
, &buf
) == -1 && errno
== ENOENT
)
250 fd
= open (filename
, O_WRONLY
|O_CREAT
, 0666);
253 builtin_error ("%s: cannot create: %s", filename
, strerror (errno
));
254 return (EXECUTION_FAILURE
);
258 result
= append_history (history_lines_this_session
, filename
);
259 history_lines_in_file
+= history_lines_this_session
;
260 history_lines_this_session
= 0;
265 /* If this is an interactive shell, then append the lines executed
266 this session to the history file. */
268 maybe_save_shell_history ()
275 if (history_lines_this_session
)
277 hf
= get_string_value ("HISTFILE");
281 /* If the file doesn't exist, then create it. */
282 if (stat (hf
, &buf
) == -1)
285 file
= open (hf
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0666);
290 /* Now actually append the lines if the history hasn't been
291 stifled. If the history has been stifled, rewrite the
294 if (history_lines_this_session
<= where_history () || force_append_history
)
296 result
= append_history (history_lines_this_session
, hf
);
297 history_lines_in_file
+= history_lines_this_session
;
301 result
= write_history (hf
);
302 history_lines_in_file
= history_lines_this_session
;
304 history_lines_this_session
= 0;
306 sv_histsize ("HISTFILESIZE");
312 #if defined (READLINE)
313 /* Tell readline () that we have some text for it to edit. */
318 if (bash_input
.type
== st_stdin
)
321 #endif /* READLINE */
323 /* Return 1 if this line needs history expansion. */
325 history_expansion_p (line
)
330 for (s
= line
; *s
; s
++)
331 if (*s
== history_expansion_char
|| *s
== history_subst_char
)
336 /* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then
337 print the results of expanding the line if there were any changes.
338 If there is an error, return NULL, otherwise the expanded line is
339 returned. If ADDIT is non-zero the line is added to the history
340 list after history expansion. ADDIT is just a suggestion;
341 REMEMBER_ON_HISTORY can veto, and does.
342 Right now this does history expansion. */
344 pre_process_line (line
, print_changes
, addit
)
346 int print_changes
, addit
;
355 # if defined (BANG_HISTORY)
356 /* History expand the line. If this results in no errors, then
357 add that line to the history if ADDIT is non-zero. */
358 if (!history_expansion_inhibited
&& history_expansion
&& history_expansion_p (line
))
360 expanded
= history_expand (line
, &history_value
);
367 internal_error (history_value
);
368 else if (hist_verify
== 0)
369 fprintf (stderr
, "%s\n", history_value
);
372 /* If there was an error, return NULL. */
373 if (expanded
< 0 || expanded
== 2) /* 2 == print only */
375 free (history_value
);
377 # if defined (READLINE)
378 /* New hack. We can allow the user to edit the
379 failed history expansion. */
380 if (history_reediting
&& expanded
< 0)
382 # endif /* READLINE */
383 return ((char *)NULL
);
386 # if defined (READLINE)
387 if (hist_verify
&& expanded
== 1)
389 re_edit (history_value
);
390 return ((char *)NULL
);
395 /* Let other expansions know that return_value can be free'ed,
396 and that a line has been added to the history list. Note
397 that we only add lines that have something in them. */
399 return_value
= history_value
;
401 # endif /* BANG_HISTORY */
403 if (addit
&& remember_on_history
&& *return_value
)
404 maybe_add_history (return_value
);
407 return_value
= savestring (line
);
409 return (return_value
);
412 /* Add LINE to the history list depending on the value of HISTORY_CONTROL. */
414 maybe_add_history (line
)
422 /* Don't use the value of history_control to affect the second
423 and subsequent lines of a multi-line command when
424 command_oriented_history is enabled. */
425 if (command_oriented_history
&& current_command_line_count
> 1)
427 bash_add_history (line
);
431 switch (history_control
)
443 /* FALLTHROUGH if case == 3 (`ignoreboth') */
446 temp
= previous_history ();
448 if (temp
== 0 || STREQ (temp
->line
, line
) == 0)
455 if (should_add
&& history_should_ignore (line
) == 0)
456 bash_add_history (line
);
459 /* Add a line to the history list.
460 The variable COMMAND_ORIENTED_HISTORY controls the style of history
461 remembering; when non-zero, and LINE is not the first line of a
462 complete parser construct, append LINE to the last history line instead
463 of adding it as a new line. */
465 bash_add_history (line
)
468 int add_it
, offset
, curlen
;
469 HIST_ENTRY
*current
, *old
;
470 char *chars_to_add
, *new_line
;
473 if (command_oriented_history
&& current_command_line_count
> 1)
475 chars_to_add
= literal_history
? "\n" : history_delimiting_chars ();
478 current
= previous_history ();
482 /* If the previous line ended with an escaped newline (escaped
483 with backslash, but otherwise unquoted), then remove the quoted
484 newline, since that is what happens when the line is parsed. */
485 curlen
= strlen (current
->line
);
487 if (dstack
.delimiter_depth
== 0 && current
->line
[curlen
- 1] == '\\' &&
488 current
->line
[curlen
- 2] != '\\')
490 current
->line
[curlen
- 1] = '\0';
495 new_line
= (char *) xmalloc (1
498 + strlen (chars_to_add
));
499 sprintf (new_line
, "%s%s%s", current
->line
, chars_to_add
, line
);
500 offset
= where_history ();
501 old
= replace_history_entry (offset
, new_line
, current
->data
);
516 history_lines_this_session
++;
525 return (get_string_value ("HISTSIZE") ? history_base
+ where_history () : 1);
534 for (p
= s
; p
&& *p
; p
++)
545 histignore_item_func (ign
)
548 if (should_expand (ign
->val
))
549 ign
->flags
|= HIGN_EXPAND
;
554 setup_history_ignore (varname
)
557 setup_ignore_patterns (&histignore
);
561 last_history_entry ()
566 he
= previous_history ();
576 he
= last_history_entry ();
578 return ((char *)NULL
);
583 expand_histignore_pattern (pat
)
587 char *ret
, *p
, *r
, *t
;
588 int len
, rlen
, ind
, tlen
;
590 phe
= last_history_entry ();
592 if (phe
== (HIST_ENTRY
*)0)
593 return (savestring (pat
));
595 len
= strlen (phe
->line
);
596 rlen
= len
+ strlen (pat
) + 2;
597 ret
= xmalloc (rlen
);
599 for (p
= pat
, r
= ret
; p
&& *p
; )
604 if (glob_pattern_p (phe
->line
) || strchr (phe
->line
, '\\'))
606 t
= quote_globbing_chars (phe
->line
);
608 RESIZE_MALLOCED_BUFFER (ret
, ind
, tlen
, rlen
, rlen
);
609 r
= ret
+ ind
; /* in case reallocated */
616 tlen
= strlen (phe
->line
);
617 RESIZE_MALLOCED_BUFFER (ret
, ind
, tlen
, rlen
, rlen
);
618 r
= ret
+ ind
; /* in case reallocated */
619 strcpy (r
, phe
->line
);
626 if (*p
== '\\' && p
[1] == '&')
635 /* Return 1 if we should not put LINE into the history according to the
636 patterns in HISTIGNORE. */
638 history_should_ignore (line
)
641 register int i
, match
;
644 if (histignore
.num_ignores
== 0)
647 for (i
= match
= 0; i
< histignore
.num_ignores
; i
++)
649 if (histignore
.ignores
[i
].flags
& HIGN_EXPAND
)
650 npat
= expand_histignore_pattern (histignore
.ignores
[i
].val
);
652 npat
= histignore
.ignores
[i
].val
;
654 match
= fnmatch (npat
, line
, 0) != FNM_NOMATCH
;
656 if (histignore
.ignores
[i
].flags
& HIGN_EXPAND
)