📄 bashhist.c
字号:
/* bashhist.c -- bash interface to the GNU history library. *//* Copyright (C) 1993-2010 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. Bash is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Bash 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. You should have received a copy of the GNU General Public License along with Bash. If not, see <http://www.gnu.org/licenses/>.*/#include "config.h"#if defined (HISTORY)#if defined (HAVE_UNISTD_H)# ifdef _MINIX# include <sys/types.h># endif# include <unistd.h>#endif#include "bashtypes.h"#include <stdio.h>#include <errno.h>#include "bashansi.h"#include "posixstat.h"#include "filecntl.h"#include "bashintl.h"#if defined (SYSLOG_HISTORY)# include <syslog.h>#endif#include "shell.h"#include "flags.h"#include "input.h"#include "parser.h" /* for the struct dstack stuff. */#include "pathexp.h" /* for the struct ignorevar stuff */#include "bashhist.h" /* matching prototypes and declarations */#include "builtins/common.h"#include <readline/history.h>#include <glob/glob.h>#include <glob/strmatch.h>#if defined (READLINE)# include "bashline.h"extern int rl_done, rl_dispatching; /* should really include readline.h */#endif#if !defined (errno)extern int errno;#endifstatic int histignore_item_func __P((struct ign *));static int check_history_control __P((char *));static void hc_erasedups __P((char *));static void really_add_history __P((char *));static struct ignorevar histignore ={ "HISTIGNORE", (struct ign *)0, 0, (char *)0, (sh_iv_item_func_t *)histignore_item_func,};#define HIGN_EXPAND 0x01/* Declarations of bash history variables. *//* Non-zero means to remember lines typed to the shell on the history list. This is different than the user-controlled behaviour; this becomes zero when we read lines from a file, for example. */int remember_on_history = 1;int enable_history_list = 1; /* value for `set -o history' *//* The number of lines that Bash has added to this history session. The difference between the number of the top element in the history list (offset from history_base) and the number of lines in the history file. Appending this session's history to the history file resets this to 0. */int history_lines_this_session;/* The number of lines that Bash has read from the history file. */int history_lines_in_file;#if defined (BANG_HISTORY)/* Non-zero means do no history expansion on this line, regardless of what history_expansion says. */int history_expansion_inhibited;#endif/* With the old default, every line was saved in the history individually. I.e., if the user enters: bash$ for i in a b c > do > echo $i > done Each line will be individually saved in the history. bash$ history 10 for i in a b c 11 do 12 echo $i 13 done 14 history If the variable command_oriented_history is set, multiple lines which form one command will be saved as one history entry. bash$ for i in a b c > do > echo $i > done bash$ history 10 for i in a b c do echo $i done 11 history The user can then recall the whole command all at once instead of just being able to recall one line at a time. This is now enabled by default. */int command_oriented_history = 1;/* Set to 1 if the first line of a possibly-multi-line command was saved in the history list. Managed by maybe_add_history(), but global so the history-manipluating builtins can see it. */int current_command_first_line_saved = 0;/* Non-zero means to store newlines in the history list when using command_oriented_history rather than trying to use semicolons. */int literal_history;/* Non-zero means to append the history to the history file at shell exit, even if the history has been stifled. */int force_append_history;/* A nit for picking at history saving. Flags have the following values: Value == 0 means save all lines parsed by the shell on the history. Value & HC_IGNSPACE means save all lines that do not start with a space. Value & HC_IGNDUPS means save all lines that do not match the last line saved. Value & HC_ERASEDUPS means to remove all other matching lines from the history list before saving the latest line. */int history_control;/* Set to 1 if the last command was added to the history list successfully as a separate history entry; set to 0 if the line was ignored or added to a previous entry as part of command-oriented-history processing. */int hist_last_line_added;/* Set to 1 if builtins/history.def:push_history added the last history entry. */int hist_last_line_pushed;#if defined (READLINE)/* If non-zero, and readline is being used, the user is offered the chance to re-edit a failed history expansion. */int history_reediting;/* If non-zero, and readline is being used, don't directly execute a line with history substitution. Reload it into the editing buffer instead and let the user further edit and confirm with a newline. */int hist_verify;#endif /* READLINE *//* Non-zero means to not save function definitions in the history list. */int dont_save_function_defs;/* Variables declared in other files used here. */extern int current_command_line_count;extern struct dstack dstack;extern int parser_state;static int bash_history_inhibit_expansion __P((char *, int));#if defined (READLINE)static void re_edit __P((char *));#endifstatic int history_expansion_p __P((char *));static int shell_comment __P((char *));static int should_expand __P((char *));static HIST_ENTRY *last_history_entry __P((void));static char *expand_histignore_pattern __P((char *));static int history_should_ignore __P((char *));/* Is the history expansion starting at string[i] one that should not be expanded? */static intbash_history_inhibit_expansion (string, i) char *string; int i;{ /* The shell uses ! as a pattern negation character in globbing [...] expressions, so let those pass without expansion. */ if (i > 0 && (string[i - 1] == '[') && member (']', string + i + 1)) return (1); /* The shell uses ! as the indirect expansion character, so let those expansions pass as well. */ else if (i > 1 && string[i - 1] == '{' && string[i - 2] == '$' && member ('}', string + i + 1)) return (1); /* The shell uses $! as a defined parameter expansion. */ else if (i > 1 && string[i - 1] == '$' && string[i] == '!') return (1);#if defined (EXTENDED_GLOB) else if (extended_glob && i > 1 && string[i+1] == '(' && member (')', string + i + 2)) return (1);#endif else return (0);}voidbash_initialize_history (){ history_quotes_inhibit_expansion = 1; history_search_delimiter_chars = ";&()|<>"; history_inhibit_expansion_function = bash_history_inhibit_expansion;#if defined (BANG_HISTORY) sv_histchars ("histchars");#endif}voidbash_history_reinit (interact) int interact;{#if defined (BANG_HISTORY) history_expansion = interact != 0; history_expansion_inhibited = 1;#endif remember_on_history = enable_history_list = interact != 0; history_inhibit_expansion_function = bash_history_inhibit_expansion;}voidbash_history_disable (){ remember_on_history = 0;#if defined (BANG_HISTORY) history_expansion_inhibited = 1;#endif}voidbash_history_enable (){ remember_on_history = 1;#if defined (BANG_HISTORY) history_expansion_inhibited = 0;#endif history_inhibit_expansion_function = bash_history_inhibit_expansion; sv_history_control ("HISTCONTROL"); sv_histignore ("HISTIGNORE");}/* Load the history list from the history file. */voidload_history (){ char *hf; /* Truncate history file for interactive shells which desire it. Note that the history file is automatically truncated to the size of HISTSIZE if the user does not explicitly set the size differently. */ set_if_not ("HISTSIZE", "500"); sv_histsize ("HISTSIZE"); set_if_not ("HISTFILESIZE", get_string_value ("HISTSIZE")); sv_histsize ("HISTFILESIZE"); /* Read the history in HISTFILE into the history list. */ hf = get_string_value ("HISTFILE"); if (hf && *hf && file_exists (hf)) { read_history (hf); using_history (); history_lines_in_file = where_history (); }}voidbash_clear_history (){ clear_history (); history_lines_this_session = 0;}/* Delete and free the history list entry at offset I. */intbash_delete_histent (i) int i;{ HIST_ENTRY *discard; discard = remove_history (i); if (discard) free_history_entry (discard); history_lines_this_session--; return 1;}intbash_delete_last_history (){ register int i; HIST_ENTRY **hlist, *histent; int r; hlist = history_list (); if (hlist == NULL) return 0; for (i = 0; hlist[i]; i++) ; i--; /* History_get () takes a parameter that must be offset by history_base. */ histent = history_get (history_base + i); /* Don't free this */ if (histent == NULL) return 0; r = bash_delete_histent (i); if (where_history () > history_length) history_set_pos (history_length); return r;}#ifdef INCLUDE_UNUSED/* Write the existing history out to the history file. */voidsave_history (){ char *hf; hf = get_string_value ("HISTFILE"); if (hf && *hf && file_exists (hf)) { /* Append only the lines that occurred this session to the history file. */ using_history (); if (history_lines_this_session <= where_history () || force_append_history) append_history (history_lines_this_session, hf); else write_history (hf); sv_histsize ("HISTFILESIZE"); }}#endifintmaybe_append_history (filename) char *filename;{ int fd, result; struct stat buf; result = EXECUTION_SUCCESS; if (history_lines_this_session && (history_lines_this_session <= where_history ())) { /* If the filename was supplied, then create it if necessary. */ if (stat (filename, &buf) == -1 && errno == ENOENT) { fd = open (filename, O_WRONLY|O_CREAT, 0600); if (fd < 0) { builtin_error (_("%s: cannot create: %s"), filename, strerror (errno)); return (EXECUTION_FAILURE); } close (fd); } result = append_history (history_lines_this_session, filename); history_lines_in_file += history_lines_this_session; history_lines_this_session = 0; } return (result);}/* If this is an interactive shell, then append the lines executed this session to the history file. */intmaybe_save_shell_history (){ int result; char *hf; result = 0; if (history_lines_this_session) { hf = get_string_value ("HISTFILE"); if (hf && *hf) { /* If the file doesn't exist, then create it. */ if (file_exists (hf) == 0) { int file; file = open (hf, O_CREAT | O_TRUNC | O_WRONLY, 0600); if (file != -1) close (file); } /* Now actually append the lines if the history hasn't been stifled. If the history has been stifled, rewrite the history file. */ using_history (); if (history_lines_this_session <= where_history () || force_append_history) { result = append_history (history_lines_this_session, hf); history_lines_in_file += history_lines_this_session; } else { result = write_history (hf); history_lines_in_file = history_lines_this_session; } history_lines_this_session = 0; sv_histsize ("HISTFILESIZE"); } } return (result);}#if defined (READLINE)/* Tell readline () that we have some text for it to edit. */static voidre_edit (text) char *text;{ if (bash_input.type == st_stdin) bash_re_edit (text);}#endif /* READLINE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -