⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bashline.c

📁 android-w.song.android.widget
💻 C
📖 第 1 页 / 共 5 页
字号:
/* bashline.c -- Bash's interface to the readline library. *//* Copyright (C) 1987-2011 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 (READLINE)#include "bashtypes.h"#include "posixstat.h"#if defined (HAVE_UNISTD_H)#  include <unistd.h>#endif#if defined (HAVE_GRP_H)#  include <grp.h>#endif#if defined (HAVE_NETDB_H)#  include <netdb.h>#endif#include <stdio.h>#include "chartypes.h"#include "bashansi.h"#include "bashintl.h"#include "shell.h"#include "input.h"#include "builtins.h"#include "bashhist.h"#include "bashline.h"#include "execute_cmd.h"#include "findcmd.h"#include "pathexp.h"#include "shmbutil.h"#include "builtins/common.h"#include <readline/rlconf.h>#include <readline/readline.h>#include <readline/history.h>#include <glob/glob.h>#if defined (ALIAS)#  include "alias.h"#endif#if defined (PROGRAMMABLE_COMPLETION)#  include "pcomplete.h"#endif/* These should agree with the defines for emacs_mode and vi_mode in   rldefs.h, even though that's not a public readline header file. */#ifndef EMACS_EDITING_MODE#  define NO_EDITING_MODE	-1#  define EMACS_EDITING_MODE	 1#  define VI_EDITING_MODE	 0#endif#define RL_BOOLEAN_VARIABLE_VALUE(s)	((s)[0] == 'o' && (s)[1] == 'n' && (s)[2] == '\0')#if defined (BRACE_COMPLETION)extern int bash_brace_completion __P((int, int));#endif /* BRACE_COMPLETION *//* To avoid including curses.h/term.h/termcap.h and that whole mess. */extern int tputs __P((const char *string, int nlines, int (*outx)(int)));/* Forward declarations *//* Functions bound to keys in Readline for Bash users. */static int shell_expand_line __P((int, int));static int display_shell_version __P((int, int));static int operate_and_get_next __P((int, int));static int bash_ignore_filenames __P((char **));static int bash_ignore_everything __P((char **));#if defined (BANG_HISTORY)static char *history_expand_line_internal __P((char *));static int history_expand_line __P((int, int));static int tcsh_magic_space __P((int, int));#endif /* BANG_HISTORY */#ifdef ALIASstatic int alias_expand_line __P((int, int));#endif#if defined (BANG_HISTORY) && defined (ALIAS)static int history_and_alias_expand_line __P((int, int));#endifstatic int bash_forward_shellword __P((int, int));static int bash_backward_shellword __P((int, int));static int bash_kill_shellword __P((int, int));static int bash_backward_kill_shellword __P((int, int));/* Helper functions for Readline. */static char *restore_tilde __P((char *, char *));static char *bash_filename_rewrite_hook __P((char *, int));static void bash_directory_expansion __P((char **));static int bash_directory_completion_hook __P((char **));static int filename_completion_ignore __P((char **));static int bash_push_line __P((void));static void cleanup_expansion_error __P((void));static void maybe_make_readline_line __P((char *));static void set_up_new_line __P((char *));static int check_redir __P((int));static char **attempt_shell_completion __P((const char *, int, int));static char *variable_completion_function __P((const char *, int));static char *hostname_completion_function __P((const char *, int));static char *command_subst_completion_function __P((const char *, int));static void build_history_completion_array __P((void));static char *history_completion_generator __P((const char *, int));static int dynamic_complete_history __P((int, int));static int bash_dabbrev_expand __P((int, int));static void initialize_hostname_list __P((void));static void add_host_name __P((char *));static void snarf_hosts_from_file __P((char *));static char **hostnames_matching __P((char *));static void _ignore_completion_names __P((char **, sh_ignore_func_t *));static int name_is_acceptable __P((const char *));static int test_for_directory __P((const char *));static int return_zero __P((const char *));static char *bash_dequote_filename __P((char *, int));static char *quote_word_break_chars __P((char *));static char *bash_quote_filename __P((char *, int, char *));static int putx __P((int));static int bash_execute_unix_command __P((int, int));static void init_unix_command_map __P((void));static int isolate_sequence __P((char *, int, int, int *));static int set_saved_history __P((void));#if defined (ALIAS)static int posix_edit_macros __P((int, int));#endif#if defined (PROGRAMMABLE_COMPLETION)static int find_cmd_start __P((int));static int find_cmd_end __P((int));static char *find_cmd_name __P((int));static char *prog_complete_return __P((const char *, int));static char **prog_complete_matches;#endif/* Variables used here but defined in other files. */#if defined (BANG_HISTORY)extern int hist_verify;#endifextern int current_command_line_count, saved_command_line_count;extern int last_command_exit_value;extern int array_needs_making;extern int posixly_correct, no_symbolic_links;extern char *current_prompt_string, *ps1_prompt;extern STRING_INT_ALIST word_token_alist[];extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;/* SPECIFIC_COMPLETION_FUNCTIONS specifies that we have individual   completion functions which indicate what type of completion should be   done (at or before point) that can be bound to key sequences with   the readline library. */#define SPECIFIC_COMPLETION_FUNCTIONS#if defined (SPECIFIC_COMPLETION_FUNCTIONS)static int bash_specific_completion __P((int, rl_compentry_func_t *));static int bash_complete_filename_internal __P((int));static int bash_complete_username_internal __P((int));static int bash_complete_hostname_internal __P((int));static int bash_complete_variable_internal __P((int));static int bash_complete_command_internal __P((int));static int bash_complete_filename __P((int, int));static int bash_possible_filename_completions __P((int, int));static int bash_complete_username __P((int, int));static int bash_possible_username_completions __P((int, int));static int bash_complete_hostname __P((int, int));static int bash_possible_hostname_completions __P((int, int));static int bash_complete_variable __P((int, int));static int bash_possible_variable_completions __P((int, int));static int bash_complete_command __P((int, int));static int bash_possible_command_completions __P((int, int));static char *glob_complete_word __P((const char *, int));static int bash_glob_completion_internal __P((int));static int bash_glob_complete_word __P((int, int));static int bash_glob_expand_word __P((int, int));static int bash_glob_list_expansions __P((int, int));#endif /* SPECIFIC_COMPLETION_FUNCTIONS */static int edit_and_execute_command __P((int, int, int, char *));#if defined (VI_MODE)static int vi_edit_and_execute_command __P((int, int));static int bash_vi_complete __P((int, int));#endifstatic int emacs_edit_and_execute_command __P((int, int));/* Non-zero once initalize_readline () has been called. */int bash_readline_initialized = 0;/* If non-zero, we do hostname completion, breaking words at `@' and   trying to complete the stuff after the `@' from our own internal   host list. */int perform_hostname_completion = 1;/* If non-zero, we don't do command completion on an empty line. */int no_empty_command_completion;/* Set FORCE_FIGNORE if you want to honor FIGNORE even if it ignores the   only possible matches.  Set to 0 if you want to match filenames if they   are the only possible matches, even if FIGNORE says to. */int force_fignore = 1;/* Perform spelling correction on directory names during word completion */int dircomplete_spelling = 0;static char *bash_completer_word_break_characters = " \t\n\"'@><=;|&(:";static char *bash_nohostname_word_break_characters = " \t\n\"'><=;|&(:";/* )) */static rl_hook_func_t *old_rl_startup_hook = (rl_hook_func_t *)NULL;static int dot_in_path = 0;/* Set to non-zero when dabbrev-expand is running */static int dabbrev_expand_active = 0;/* What kind of quoting is performed by bash_quote_filename:	COMPLETE_DQUOTE = double-quoting the filename	COMPLETE_SQUOTE = single_quoting the filename	COMPLETE_BSQUOTE = backslash-quoting special chars in the filename*/#define COMPLETE_DQUOTE  1#define COMPLETE_SQUOTE  2#define COMPLETE_BSQUOTE 3static int completion_quoting_style = COMPLETE_BSQUOTE;/* Flag values for the final argument to bash_default_completion */#define DEFCOMP_CMDPOS		1/* Change the readline VI-mode keymaps into or out of Posix.2 compliance.   Called when the shell is put into or out of `posix' mode. */voidposix_readline_initialize (on_or_off)     int on_or_off;{  if (on_or_off)    rl_variable_bind ("comment-begin", "#");#if defined (VI_MODE)  rl_bind_key_in_map (CTRL ('I'), on_or_off ? rl_insert : rl_complete, vi_insertion_keymap);#endif}voidreset_completer_word_break_chars (){  rl_completer_word_break_characters = perform_hostname_completion ? savestring (bash_completer_word_break_characters) : savestring (bash_nohostname_word_break_characters);}/* When this function returns, rl_completer_word_break_characters points to   dynamically allocated memory. */intenable_hostname_completion (on_or_off)     int on_or_off;{  int old_value;  char *at, *nv, *nval;  old_value = perform_hostname_completion;  if (on_or_off)    {      perform_hostname_completion = 1;      rl_special_prefixes = "$@";    }  else    {      perform_hostname_completion = 0;      rl_special_prefixes = "$";    }  /* Now we need to figure out how to appropriately modify and assign     rl_completer_word_break_characters depending on whether we want     hostname completion on or off. */  /* If this is the first time this has been called     (bash_readline_initialized == 0), use the sames values as before, but     allocate new memory for rl_completer_word_break_characters. */  if (bash_readline_initialized == 0 &&      (rl_completer_word_break_characters == 0 ||        rl_completer_word_break_characters == rl_basic_word_break_characters))    {      if (on_or_off)	rl_completer_word_break_characters = savestring (bash_completer_word_break_characters);      else	rl_completer_word_break_characters = savestring (bash_nohostname_word_break_characters);    }  else    {      /* See if we have anything to do. */      at = strchr (rl_completer_word_break_characters, '@');      if ((at == 0 && on_or_off == 0) || (at != 0 && on_or_off != 0))        return old_value;      /* We have something to do.  Do it. */      nval = (char *)xmalloc (strlen (rl_completer_word_break_characters) + 1 + on_or_off);      if (on_or_off == 0)	{	  /* Turn it off -- just remove `@' from word break chars.  We want	     to remove all occurrences of `@' from the char list, so we loop	     rather than just copy the rest of the list over AT. */	  for (nv = nval, at = rl_completer_word_break_characters; *at; )	    if (*at != '@')	      *nv++ = *at++;	    else	      at++;	  *nv = '\0';	}      else	{	  nval[0] = '@';	  strcpy (nval + 1, rl_completer_word_break_characters);        }      free (rl_completer_word_break_characters);      rl_completer_word_break_characters = nval;    }  return (old_value);}/* Called once from parse.y if we are going to use readline. */voidinitialize_readline (){  rl_command_func_t *func;  char kseq[2];  if (bash_readline_initialized)    return;  rl_terminal_name = get_string_value ("TERM");  rl_instream = stdin;  rl_outstream = stderr;  /* Allow conditional parsing of the ~/.inputrc file. */  rl_readline_name = "Bash";  /* Add bindable names before calling rl_initialize so they may be     referenced in the various inputrc files. */  rl_add_defun ("shell-expand-line", shell_expand_line, -1);#ifdef BANG_HISTORY  rl_add_defun ("history-expand-line", history_expand_line, -1);  rl_add_defun ("magic-space", tcsh_magic_space, -1);#endif  rl_add_defun ("shell-forward-word", bash_forward_shellword, -1);  rl_add_defun ("shell-backward-word", bash_backward_shellword, -1);  rl_add_defun ("shell-kill-word", bash_kill_shellword, -1);  rl_add_defun ("shell-backward-kill-word", bash_backward_kill_shellword, -1);#ifdef ALIAS  rl_add_defun ("alias-expand-line", alias_expand_line, -1);#  ifdef BANG_HISTORY  rl_add_defun ("history-and-alias-expand-line", history_and_alias_expand_line, -1);#  endif#endif  /* Backwards compatibility. */  rl_add_defun ("insert-last-argument", rl_yank_last_arg, -1);  rl_add_defun ("operate-and-get-next", operate_and_get_next, -1);  rl_add_defun ("display-shell-version", display_shell_version, -1);  rl_add_defun ("edit-and-execute-command", emacs_edit_and_execute_command, -1);#if defined (BRACE_COMPLETION)  rl_add_defun ("complete-into-braces", bash_brace_completion, -1);#endif#if defined (SPECIFIC_COMPLETION_FUNCTIONS)  rl_add_defun ("complete-filename", bash_complete_filename, -1);  rl_add_defun ("possible-filename-completions", bash_possible_filename_completions, -1);  rl_add_defun ("complete-username", bash_complete_username, -1);  rl_add_defun ("possible-username-completions", bash_possible_username_completions, -1);  rl_add_defun ("complete-hostname", bash_complete_hostname, -1);  rl_add_defun ("possible-hostname-completions", bash_possible_hostname_completions, -1);  rl_add_defun ("complete-variable", bash_complete_variable, -1);  rl_add_defun ("possible-variable-completions", bash_possible_variable_completions, -1);  rl_add_defun ("complete-command", bash_complete_command, -1);  rl_add_defun ("possible-command-completions", bash_possible_command_completions, -1);  rl_add_defun ("glob-complete-word", bash_glob_complete_word, -1);  rl_add_defun ("glob-expand-word", bash_glob_expand_word, -1);  rl_add_defun ("glob-list-expansions", bash_glob_list_expansions, -1);#endif  rl_add_defun ("dynamic-complete-history", dynamic_complete_history, -1);  rl_add_defun ("dabbrev-expand", bash_dabbrev_expand, -1);  /* Bind defaults before binding our custom shell keybindings. */  if (RL_ISSTATE(RL_STATE_INITIALIZED) == 0)    rl_initialize ();  /* Bind up our special shell functions. */  rl_bind_key_if_unbound_in_map (CTRL('E'), shell_expand_line, emacs_meta_keymap);#ifdef BANG_HISTORY  rl_bind_key_if_unbound_in_map ('^', history_expand_line, emacs_meta_keymap);#endif  rl_bind_key_if_unbound_in_map (CTRL ('O'), operate_and_get_next, emacs_standard_keymap);  rl_bind_key_if_unbound_in_map (CTRL ('V'), display_shell_version, emacs_ctlx_keymap);  /* In Bash, the user can switch editing modes with "set -o [vi emacs]",     so it is not necessary to allow C-M-j for context switching.  Turn     off this occasionally confusing behaviour. */  kseq[0] = CTRL('J');  kseq[1] = '\0';  func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);  if (func == rl_vi_editing_mode)    rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);  kseq[0] = CTRL('M');  func = rl_function_of_keyseq (kseq, emacs_meta_keymap, (int *)NULL);  if (func == rl_vi_editing_mode)    rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);#if defined (VI_MODE)  rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);#endif#if defined (BRACE_COMPLETION)  rl_bind_key_if_unbound_in_map ('{', bash_brace_completion, emacs_meta_keymap); /*}*/#endif /* BRACE_COMPLETION */#if defined (SPECIFIC_COMPLETION_FUNCTIONS)  rl_bind_key_if_unbound_in_map ('/', bash_complete_filename, emacs_meta_keymap);  rl_bind_key_if_unbound_in_map ('/', bash_possible_filename_completions, emacs_ctlx_keymap);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -