1 /* bashhist.c -- bash interface to the GNU history library. */
3 /* Copyright (C) 1993-2015 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
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #if defined (HAVE_UNISTD_H)
27 # include <sys/types.h>
32 #include "bashtypes.h"
36 #include "posixstat.h"
41 #if defined (SYSLOG_HISTORY)
48 #include "parser.h" /* for the struct dstack stuff. */
49 #include "pathexp.h" /* for the struct ignorevar stuff */
50 #include "bashhist.h" /* matching prototypes and declarations */
51 #include "builtins/common.h"
53 #include <readline/history.h>
54 #include <glob/glob.h>
55 #include <glob/strmatch.h>
57 #if defined (READLINE)
58 # include "bashline.h"
59 extern int rl_done
, rl_dispatching
; /* should really include readline.h */
66 static int histignore_item_func
__P((struct ign
*));
67 static int check_history_control
__P((char *));
68 static void hc_erasedups
__P((char *));
69 static void really_add_history
__P((char *));
71 static struct ignorevar histignore
=
77 (sh_iv_item_func_t
*)histignore_item_func
,
80 #define HIGN_EXPAND 0x01
82 /* Declarations of bash history variables. */
83 /* Non-zero means to remember lines typed to the shell on the history
84 list. This is different than the user-controlled behaviour; this
85 becomes zero when we read lines from a file, for example. */
86 int remember_on_history
= 0;
87 int enable_history_list
= 0; /* value for `set -o history' */
89 /* The number of lines that Bash has added to this history session. The
90 difference between the number of the top element in the history list
91 (offset from history_base) and the number of lines in the history file.
92 Appending this session's history to the history file resets this to 0. */
93 int history_lines_this_session
;
95 /* The number of lines that Bash has read from the history file. */
96 int history_lines_in_file
;
98 #if defined (BANG_HISTORY)
99 /* Non-zero means do no history expansion on this line, regardless
100 of what history_expansion says. */
101 int history_expansion_inhibited
;
102 /* If non-zero, double quotes can quote the history expansion character. */
103 int double_quotes_inhibit_history_expansion
= 0;
106 /* With the old default, every line was saved in the history individually.
107 I.e., if the user enters:
112 Each line will be individually saved in the history.
119 If the variable command_oriented_history is set, multiple lines
120 which form one command will be saved as one history entry.
131 The user can then recall the whole command all at once instead
132 of just being able to recall one line at a time.
134 This is now enabled by default.
136 int command_oriented_history
= 1;
138 /* Set to 1 if the first line of a possibly-multi-line command was saved
139 in the history list. Managed by maybe_add_history(), but global so
140 the history-manipluating builtins can see it. */
141 int current_command_first_line_saved
= 0;
143 /* Non-zero means to store newlines in the history list when using
144 command_oriented_history rather than trying to use semicolons. */
147 /* Non-zero means to append the history to the history file at shell
148 exit, even if the history has been stifled. */
149 int force_append_history
;
151 /* A nit for picking at history saving. Flags have the following values:
153 Value == 0 means save all lines parsed by the shell on the history.
154 Value & HC_IGNSPACE means save all lines that do not start with a space.
155 Value & HC_IGNDUPS means save all lines that do not match the last
157 Value & HC_ERASEDUPS means to remove all other matching lines from the
158 history list before saving the latest line. */
161 /* Set to 1 if the last command was added to the history list successfully
162 as a separate history entry; set to 0 if the line was ignored or added
163 to a previous entry as part of command-oriented-history processing. */
164 int hist_last_line_added
;
166 /* Set to 1 if builtins/history.def:push_history added the last history
168 int hist_last_line_pushed
;
170 #if defined (READLINE)
171 /* If non-zero, and readline is being used, the user is offered the
172 chance to re-edit a failed history expansion. */
173 int history_reediting
;
175 /* If non-zero, and readline is being used, don't directly execute a
176 line with history substitution. Reload it into the editing buffer
177 instead and let the user further edit and confirm with a newline. */
180 #endif /* READLINE */
182 /* Non-zero means to not save function definitions in the history list. */
183 int dont_save_function_defs
;
185 /* Variables declared in other files used here. */
186 extern int current_command_line_count
;
188 extern struct dstack dstack
;
189 extern int parser_state
;
191 #if defined (BANG_HISTORY)
192 static int bash_history_inhibit_expansion
__P((char *, int));
194 #if defined (READLINE)
195 static void re_edit
__P((char *));
197 static int history_expansion_p
__P((char *));
198 static int shell_comment
__P((char *));
199 static int should_expand
__P((char *));
200 static HIST_ENTRY
*last_history_entry
__P((void));
201 static char *expand_histignore_pattern
__P((char *));
202 static int history_should_ignore
__P((char *));
204 #if defined (BANG_HISTORY)
205 /* Is the history expansion starting at string[i] one that should not
208 bash_history_inhibit_expansion (string
, i
)
215 hx
[0] = history_expansion_char
;
218 /* The shell uses ! as a pattern negation character in globbing [...]
219 expressions, so let those pass without expansion. */
220 if (i
> 0 && (string
[i
- 1] == '[') && member (']', string
+ i
+ 1))
222 /* The shell uses ! as the indirect expansion character, so let those
223 expansions pass as well. */
224 else if (i
> 1 && string
[i
- 1] == '{' && string
[i
- 2] == '$' &&
225 member ('}', string
+ i
+ 1))
227 /* The shell uses $! as a defined parameter expansion. */
228 else if (i
> 1 && string
[i
- 1] == '$' && string
[i
] == '!')
230 #if defined (EXTENDED_GLOB)
231 else if (extended_glob
&& i
> 1 && string
[i
+1] == '(' && member (')', string
+ i
+ 2))
235 /* Make sure the history expansion should not be skipped by quoting or
236 command/process substitution. */
237 else if ((t
= skip_to_histexp (string
, 0, hx
, SD_NOJMP
|SD_HISTEXP
)) > 0)
239 /* Skip instances of history expansion appearing on the line before
243 t
= skip_to_histexp (string
, t
+1, hx
, SD_NOJMP
|SD_HISTEXP
);
255 bash_initialize_history ()
257 history_quotes_inhibit_expansion
= 1;
258 history_search_delimiter_chars
= ";&()|<>";
259 #if defined (BANG_HISTORY)
260 history_inhibit_expansion_function
= bash_history_inhibit_expansion
;
261 sv_histchars ("histchars");
266 bash_history_reinit (interact
)
269 #if defined (BANG_HISTORY)
270 history_expansion
= interact
!= 0;
271 history_expansion_inhibited
= 1; /* XXX */
272 history_inhibit_expansion_function
= bash_history_inhibit_expansion
;
274 remember_on_history
= enable_history_list
;
278 bash_history_disable ()
280 remember_on_history
= 0;
281 #if defined (BANG_HISTORY)
282 history_expansion_inhibited
= 1;
287 bash_history_enable ()
289 remember_on_history
= enable_history_list
= 1;
290 #if defined (BANG_HISTORY)
291 history_expansion_inhibited
= 0;
292 history_inhibit_expansion_function
= bash_history_inhibit_expansion
;
294 sv_history_control ("HISTCONTROL");
295 sv_histignore ("HISTIGNORE");
298 /* Load the history list from the history file. */
304 /* Truncate history file for interactive shells which desire it.
305 Note that the history file is automatically truncated to the
306 size of HISTSIZE if the user does not explicitly set the size
308 set_if_not ("HISTSIZE", "500");
309 sv_histsize ("HISTSIZE");
311 set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE"));
312 sv_histsize ("HISTFILESIZE");
314 /* Read the history in HISTFILE into the history list. */
315 hf
= get_string_value ("HISTFILE");
317 if (hf
&& *hf
&& file_exists (hf
))
320 /* We have read all of the lines from the history file, even if we
321 read more lines than $HISTSIZE. Remember the total number of lines
322 we read so we don't count the last N lines as new over and over
324 history_lines_in_file
= history_lines_read_from_file
;
326 /* history_lines_in_file = where_history () + history_base - 1; */
331 bash_clear_history ()
334 history_lines_this_session
= 0;
335 /* XXX - reset history_lines_read_from_file? */
338 /* Delete and free the history list entry at offset I. */
340 bash_delete_histent (i
)
345 discard
= remove_history (i
);
347 free_history_entry (discard
);
348 history_lines_this_session
--;
354 bash_delete_last_history ()
357 HIST_ENTRY
**hlist
, *histent
;
360 hlist
= history_list ();
364 for (i
= 0; hlist
[i
]; i
++)
368 /* History_get () takes a parameter that must be offset by history_base. */
369 histent
= history_get (history_base
+ i
); /* Don't free this */
373 r
= bash_delete_histent (i
);
375 if (where_history () > history_length
)
376 history_set_pos (history_length
);
381 #ifdef INCLUDE_UNUSED
382 /* Write the existing history out to the history file. */
389 hf
= get_string_value ("HISTFILE");
390 if (hf
&& *hf
&& file_exists (hf
))
392 /* Append only the lines that occurred this session to
396 if (history_lines_this_session
<= where_history () || force_append_history
)
397 r
= append_history (history_lines_this_session
, hf
);
399 r
= write_history (hf
);
400 sv_histsize ("HISTFILESIZE");
406 maybe_append_history (filename
)
412 result
= EXECUTION_SUCCESS
;
413 if (history_lines_this_session
> 0 && (history_lines_this_session
<= where_history ()))
415 /* If the filename was supplied, then create it if necessary. */
416 if (stat (filename
, &buf
) == -1 && errno
== ENOENT
)
418 fd
= open (filename
, O_WRONLY
|O_CREAT
, 0600);
421 builtin_error (_("%s: cannot create: %s"), filename
, strerror (errno
));
422 return (EXECUTION_FAILURE
);
426 result
= append_history (history_lines_this_session
, filename
);
427 /* Pretend we already read these lines from the file because we just
429 history_lines_in_file
+= history_lines_this_session
;
430 history_lines_this_session
= 0;
433 history_lines_this_session
= 0; /* reset if > where_history() */
438 /* If this is an interactive shell, then append the lines executed
439 this session to the history file. */
441 maybe_save_shell_history ()
447 if (history_lines_this_session
> 0)
449 hf
= get_string_value ("HISTFILE");
453 /* If the file doesn't exist, then create it. */
454 if (file_exists (hf
) == 0)
457 file
= open (hf
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
462 /* Now actually append the lines if the history hasn't been
463 stifled. If the history has been stifled, rewrite the
466 if (history_lines_this_session
<= where_history () || force_append_history
)
468 result
= append_history (history_lines_this_session
, hf
);
469 history_lines_in_file
+= history_lines_this_session
;
473 result
= write_history (hf
);
474 history_lines_in_file
= history_lines_written_to_file
;
475 /* history_lines_in_file = where_history () + history_base - 1; */
477 history_lines_this_session
= 0;
479 sv_histsize ("HISTFILESIZE");
485 #if defined (READLINE)
486 /* Tell readline () that we have some text for it to edit. */
491 if (bash_input
.type
== st_stdin
)
494 #endif /* READLINE */
496 /* Return 1 if this line needs history expansion. */
498 history_expansion_p (line
)
503 for (s
= line
; *s
; s
++)
504 if (*s
== history_expansion_char
|| *s
== history_subst_char
)
509 /* Do pre-processing on LINE. If PRINT_CHANGES is non-zero, then
510 print the results of expanding the line if there were any changes.
511 If there is an error, return NULL, otherwise the expanded line is
512 returned. If ADDIT is non-zero the line is added to the history
513 list after history expansion. ADDIT is just a suggestion;
514 REMEMBER_ON_HISTORY can veto, and does.
515 Right now this does history expansion. */
517 pre_process_line (line
, print_changes
, addit
)
519 int print_changes
, addit
;
528 # if defined (BANG_HISTORY)
529 /* History expand the line. If this results in no errors, then
530 add that line to the history if ADDIT is non-zero. */
531 if (!history_expansion_inhibited
&& history_expansion
&& history_expansion_p (line
))
533 expanded
= history_expand (line
, &history_value
);
540 internal_error ("%s", history_value
);
541 #if defined (READLINE)
542 else if (hist_verify
== 0 || expanded
== 2)
546 fprintf (stderr
, "%s\n", history_value
);
549 /* If there was an error, return NULL. */
550 if (expanded
< 0 || expanded
== 2) /* 2 == print only */
552 # if defined (READLINE)
553 if (expanded
== 2 && rl_dispatching
== 0 && *history_value
)
555 if (expanded
== 2 && *history_value
)
556 # endif /* !READLINE */
557 maybe_add_history (history_value
);
559 free (history_value
);
561 # if defined (READLINE)
562 /* New hack. We can allow the user to edit the
563 failed history expansion. */
564 if (history_reediting
&& expanded
< 0 && rl_done
)
566 # endif /* READLINE */
567 return ((char *)NULL
);
570 # if defined (READLINE)
571 if (hist_verify
&& expanded
== 1)
573 re_edit (history_value
);
574 free (history_value
);
575 return ((char *)NULL
);
580 /* Let other expansions know that return_value can be free'ed,
581 and that a line has been added to the history list. Note
582 that we only add lines that have something in them. */
584 return_value
= history_value
;
586 # endif /* BANG_HISTORY */
588 if (addit
&& remember_on_history
&& *return_value
)
589 maybe_add_history (return_value
);
593 return_value
= savestring (line
);
596 return (return_value
);
599 /* Return 1 if the first non-whitespace character in LINE is a `#', indicating
600 * that the line is a shell comment. */
607 for (p
= line
; p
&& *p
&& whitespace (*p
); p
++)
609 return (p
&& *p
== '#');
612 #ifdef INCLUDE_UNUSED
613 /* Remove shell comments from LINE. A `#' and anything after it is a comment.
614 This isn't really useful yet, since it doesn't handle quoting. */
616 filter_comments (line
)
621 for (p
= line
; p
&& *p
&& *p
!= '#'; p
++)
629 /* Check LINE against what HISTCONTROL says to do. Returns 1 if the line
630 should be saved; 0 if it should be discarded. */
632 check_history_control (line
)
638 if (history_control
== 0)
641 /* ignorespace or ignoreboth */
642 if ((history_control
& HC_IGNSPACE
) && *line
== ' ')
645 /* ignoredups or ignoreboth */
646 if (history_control
& HC_IGNDUPS
)
649 temp
= previous_history ();
651 r
= (temp
== 0 || STREQ (temp
->line
, line
) == 0);
662 /* Remove all entries matching LINE from the history list. Triggered when
663 HISTCONTROL includes `erasedups'. */
672 while (temp
= previous_history ())
674 if (STREQ (temp
->line
, line
))
676 r
= where_history ();
677 temp
= remove_history (r
);
679 free_history_entry (temp
);
685 /* Add LINE to the history list, handling possibly multi-line compound
686 commands. We note whether or not we save the first line of each command
687 (which is usually the entire command and history entry), and don't add
688 the second and subsequent lines of a multi-line compound command if we
689 didn't save the first line. We don't usually save shell comment lines in
690 compound commands in the history, because they could have the effect of
691 commenting out the rest of the command when the entire command is saved as
692 a single history entry (when COMMAND_ORIENTED_HISTORY is enabled). If
693 LITERAL_HISTORY is set, we're saving lines in the history with embedded
694 newlines, so it's OK to save comment lines. If we're collecting the body
695 of a here-document, we should act as if literal_history is enabled, because
696 we want to save the entire contents of the here-document as it was
697 entered. We also make sure to save multiple-line quoted strings or other
700 maybe_add_history (line
)
703 hist_last_line_added
= 0;
705 /* Don't use the value of history_control to affect the second
706 and subsequent lines of a multi-line command (old code did
707 this only when command_oriented_history is enabled). */
708 if (current_command_line_count
> 1)
710 if (current_command_first_line_saved
&&
711 ((parser_state
& PST_HEREDOC
) || literal_history
|| dstack
.delimiter_depth
!= 0 || shell_comment (line
) == 0))
712 bash_add_history (line
);
716 /* This is the first line of a (possible multi-line) command. Note whether
717 or not we should save the first line and remember it. */
718 current_command_first_line_saved
= check_add_history (line
, 0);
721 /* Just check LINE against HISTCONTROL and HISTIGNORE and add it to the
722 history if it's OK. Used by `history -s' as well as maybe_add_history().
723 Returns 1 if the line was saved in the history, 0 otherwise. */
725 check_add_history (line
, force
)
729 if (check_history_control (line
) && history_should_ignore (line
) == 0)
731 /* We're committed to saving the line. If the user has requested it,
732 remove other matching lines from the history. */
733 if (history_control
& HC_ERASEDUPS
)
738 really_add_history (line
);
742 bash_add_history (line
);
748 #if defined (SYSLOG_HISTORY)
749 #define SYSLOG_MAXLEN 600
751 extern char *shell_name
;
754 #define OPENLOG_OPTS 0
758 bash_syslog_history (line
)
761 char trunc
[SYSLOG_MAXLEN
];
762 static int first
= 1;
766 openlog (shell_name
, OPENLOG_OPTS
, SYSLOG_FACILITY
);
770 if (strlen(line
) < SYSLOG_MAXLEN
)
771 syslog (SYSLOG_FACILITY
|SYSLOG_LEVEL
, "HISTORY: PID=%d UID=%d %s", getpid(), current_user
.uid
, line
);
774 strncpy (trunc
, line
, SYSLOG_MAXLEN
);
775 trunc
[SYSLOG_MAXLEN
- 1] = '\0';
776 syslog (SYSLOG_FACILITY
|SYSLOG_LEVEL
, "HISTORY (TRUNCATED): PID=%d UID=%d %s", getpid(), current_user
.uid
, trunc
);
781 /* Add a line to the history list.
782 The variable COMMAND_ORIENTED_HISTORY controls the style of history
783 remembering; when non-zero, and LINE is not the first line of a
784 complete parser construct, append LINE to the last history line instead
785 of adding it as a new line. */
787 bash_add_history (line
)
790 int add_it
, offset
, curlen
;
791 HIST_ENTRY
*current
, *old
;
792 char *chars_to_add
, *new_line
;
795 if (command_oriented_history
&& current_command_line_count
> 1)
797 /* The second and subsequent lines of a here document have the trailing
798 newline preserved. We don't want to add extra newlines here, but we
799 do want to add one after the first line (which is the command that
800 contains the here-doc specifier). parse.y:history_delimiting_chars()
801 does the right thing to take care of this for us. We don't want to
802 add extra newlines if the user chooses to enable literal_history,
803 so we have to duplicate some of what that function does here. */
804 if ((parser_state
& PST_HEREDOC
) && literal_history
&& current_command_line_count
> 2 && line
[strlen (line
) - 1] == '\n')
807 chars_to_add
= literal_history
? "\n" : history_delimiting_chars (line
);
810 current
= previous_history ();
814 /* If the previous line ended with an escaped newline (escaped
815 with backslash, but otherwise unquoted), then remove the quoted
816 newline, since that is what happens when the line is parsed. */
817 curlen
= strlen (current
->line
);
819 if (dstack
.delimiter_depth
== 0 && current
->line
[curlen
- 1] == '\\' &&
820 current
->line
[curlen
- 2] != '\\')
822 current
->line
[curlen
- 1] = '\0';
827 /* If we're not in some kind of quoted construct, the current history
828 entry ends with a newline, and we're going to add a semicolon,
829 don't. In some cases, it results in a syntax error (e.g., before
830 a close brace), and it should not be needed. */
831 if (dstack
.delimiter_depth
== 0 && current
->line
[curlen
- 1] == '\n' && *chars_to_add
== ';')
834 new_line
= (char *)xmalloc (1
837 + strlen (chars_to_add
));
838 sprintf (new_line
, "%s%s%s", current
->line
, chars_to_add
, line
);
839 offset
= where_history ();
840 old
= replace_history_entry (offset
, new_line
, current
->data
);
844 free_history_entry (old
);
851 really_add_history (line
);
853 #if defined (SYSLOG_HISTORY)
854 bash_syslog_history (line
);
861 really_add_history (line
)
864 hist_last_line_added
= 1;
865 hist_last_line_pushed
= 0;
867 history_lines_this_session
++;
874 return (remember_on_history
? history_base
+ where_history () : 1);
883 for (p
= s
; p
&& *p
; p
++)
894 histignore_item_func (ign
)
897 if (should_expand (ign
->val
))
898 ign
->flags
|= HIGN_EXPAND
;
903 setup_history_ignore (varname
)
906 setup_ignore_patterns (&histignore
);
910 last_history_entry ()
915 he
= previous_history ();
925 he
= last_history_entry ();
927 return ((char *)NULL
);
932 expand_histignore_pattern (pat
)
938 phe
= last_history_entry ();
940 if (phe
== (HIST_ENTRY
*)0)
941 return (savestring (pat
));
943 ret
= strcreplace (pat
, '&', phe
->line
, 1);
948 /* Return 1 if we should not put LINE into the history according to the
949 patterns in HISTIGNORE. */
951 history_should_ignore (line
)
954 register int i
, match
;
957 if (histignore
.num_ignores
== 0)
960 for (i
= match
= 0; i
< histignore
.num_ignores
; i
++)
962 if (histignore
.ignores
[i
].flags
& HIGN_EXPAND
)
963 npat
= expand_histignore_pattern (histignore
.ignores
[i
].val
);
965 npat
= histignore
.ignores
[i
].val
;
967 match
= strmatch (npat
, line
, FNMATCH_EXTFLAG
) != FNM_NOMATCH
;
969 if (histignore
.ignores
[i
].flags
& HIGN_EXPAND
)