📄 complete.c
字号:
/* complete.c -- filename completion for readline. *//* Copyright (C) 1987-2004 Free Software Foundation, Inc. This file is part of the GNU Readline Library, a library for reading lines of text with interactive input and history editing. The GNU Readline Library 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 2, or (at your option) any later version. The GNU Readline Library 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. The GNU General Public License is often shipped with GNU software, and is generally kept in a file called COPYING or LICENSE. If you do not have a copy of the license, write to the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */#define READLINE_LIBRARY#if defined (HAVE_CONFIG_H)# include <config.h>#endif#include <sys/types.h>#include <fcntl.h>#if defined (HAVE_SYS_FILE_H)# include <sys/file.h>#endif#if defined (HAVE_UNISTD_H)# include <unistd.h>#endif /* HAVE_UNISTD_H */#if defined (HAVE_STDLIB_H)# include <stdlib.h>#else# include "ansi_stdlib.h"#endif /* HAVE_STDLIB_H */#include <stdio.h>#include <errno.h>#if !defined (errno)extern int errno;#endif /* !errno */#include <pwd.h>#include "posixdir.h"#include "posixstat.h"/* System-specific feature definitions and include files. */#include "rldefs.h"#include "rlmbutil.h"/* Some standard library routines. */#include "readline.h"#include "xmalloc.h"#include "rlprivate.h"#ifdef __STDC__typedef int QSFUNC (const void *, const void *);#elsetypedef int QSFUNC ();#endif#ifdef HAVE_LSTAT# define LSTAT lstat#else# define LSTAT stat#endif/* Unix version of a hidden file. Could be different on other systems. */#define HIDDEN_FILE(fname) ((fname)[0] == '.')/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is defined. */#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE)extern struct passwd *getpwent PARAMS((void));#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE *//* If non-zero, then this is the address of a function to call when completing a word would normally display the list of possible matches. This function is called instead of actually doing the display. It takes three arguments: (char **matches, int num_matches, int max_length) where MATCHES is the array of strings that matched, NUM_MATCHES is the number of strings in that array, and MAX_LENGTH is the length of the longest string in that array. */rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL;#if defined (VISIBLE_STATS)# if !defined (X_OK)# define X_OK 1# endifstatic int stat_char PARAMS((char *));#endifstatic int path_isdir PARAMS((const char *));static char *rl_quote_filename PARAMS((char *, int, char *));static void set_completion_defaults PARAMS((int));static int get_y_or_n PARAMS((int));static int _rl_internal_pager PARAMS((int));static char *printable_part PARAMS((char *));static int fnwidth PARAMS((const char *));static int fnprint PARAMS((const char *));static int print_filename PARAMS((char *, char *));static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int));static char **remove_duplicate_matches PARAMS((char **));static void insert_match PARAMS((char *, int, int, char *));static int append_to_match PARAMS((char *, int, int, int));static void insert_all_matches PARAMS((char **, int, char *));static void display_matches PARAMS((char **));static int compute_lcd_of_matches PARAMS((char **, int, const char *));static int postprocess_matches PARAMS((char ***, int));static char *make_quoted_replacement PARAMS((char *, int, char *));/* **************************************************************** *//* *//* Completion matching, from readline's point of view. *//* *//* **************************************************************** *//* Variables known only to the readline library. *//* If non-zero, non-unique completions always show the list of matches. */int _rl_complete_show_all = 0;/* If non-zero, non-unique completions show the list of matches, unless it is not possible to do partial completion and modify the line. */int _rl_complete_show_unmodified = 0;/* If non-zero, completed directory names have a slash appended. */int _rl_complete_mark_directories = 1;/* If non-zero, the symlinked directory completion behavior introduced in readline-4.2a is disabled, and symlinks that point to directories have a slash appended (subject to the value of _rl_complete_mark_directories). This is user-settable via the mark-symlinked-directories variable. */int _rl_complete_mark_symlink_dirs = 0;/* If non-zero, completions are printed horizontally in alphabetical order, like `ls -x'. */int _rl_print_completions_horizontally;/* Non-zero means that case is not significant in filename completion. */#if defined (__MSDOS__) && !defined (__DJGPP__)int _rl_completion_case_fold = 1;#elseint _rl_completion_case_fold;#endif/* If non-zero, don't match hidden files (filenames beginning with a `.' on Unix) when doing filename completion. */int _rl_match_hidden_files = 1;/* Global variables available to applications using readline. */#if defined (VISIBLE_STATS)/* Non-zero means add an additional character to each filename displayed during listing completion iff rl_filename_completion_desired which helps to indicate the type of file being listed. */int rl_visible_stats = 0;#endif /* VISIBLE_STATS *//* If non-zero, then this is the address of a function to call when completing on a directory name. The function is called with the address of a string (the current directory name) as an arg. */rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL;rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL;/* Non-zero means readline completion functions perform tilde expansion. */int rl_complete_with_tilde_expansion = 0;/* Pointer to the generator function for completion_matches (). NULL means to use rl_filename_completion_function (), the default filename completer. */rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL;/* Pointer to alternative function to create matches. Function is called with TEXT, START, and END. START and END are indices in RL_LINE_BUFFER saying what the boundaries of TEXT are. If this function exists and returns NULL then call the value of rl_completion_entry_function to try to match, otherwise use the array of strings returned. */rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL;/* Non-zero means to suppress normal filename completion after the user-specified completion function has been called. */int rl_attempted_completion_over = 0;/* Set to a character indicating the type of completion being performed by rl_complete_internal, available for use by application completion functions. */int rl_completion_type = 0;/* Up to this many items will be displayed in response to a possible-completions call. After that, we ask the user if she is sure she wants to see them all. */int rl_completion_query_items = 100;int _rl_page_completions = 1;/* The basic list of characters that signal a break between words for the completer routine. The contents of this variable is what breaks words in the shell, i.e. " \t\n\"\\'`@$><=" */const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) *//* List of basic quoting characters. */const char *rl_basic_quote_characters = "\"'";/* The list of characters that signal a break between words for rl_complete_internal. The default list is the contents of rl_basic_word_break_characters. *//*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;/* Hook function to allow an application to set the completion word break characters before readline breaks up the line. Allows position-dependent word break characters. */rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL;/* List of characters which can be used to quote a substring of the line. Completion occurs on the entire substring, and within the substring rl_completer_word_break_characters are treated as any other character, unless they also appear within this list. */const char *rl_completer_quote_characters = (const char *)NULL;/* List of characters that should be quoted in filenames by the completer. */const char *rl_filename_quote_characters = (const char *)NULL;/* List of characters that are word break characters, but should be left in TEXT when it is passed to the completion function. The shell uses this to help determine what kind of completing to do. */const char *rl_special_prefixes = (const char *)NULL;/* If non-zero, then disallow duplicates in the matches. */int rl_ignore_completion_duplicates = 1;/* Non-zero means that the results of the matches are to be treated as filenames. This is ALWAYS zero on entry, and can only be changed within a completion entry finder function. */int rl_filename_completion_desired = 0;/* Non-zero means that the results of the matches are to be quoted using double quotes (or an application-specific quoting mechanism) if the filename contains any characters in rl_filename_quote_chars. This is ALWAYS non-zero on entry, and can only be changed within a completion entry finder function. */int rl_filename_quoting_desired = 1;/* This function, if defined, is called by the completer when real filename completion is done, after all the matching names have been generated. It is passed a (char**) known as matches in the code below. It consists of a NULL-terminated array of pointers to potential matching strings. The 1st element (matches[0]) is the maximal substring that is common to all matches. This function can re-arrange the list of matches as required, but all elements of the array must be free()'d if they are deleted. The main intent of this function is to implement FIGNORE a la SunOS csh. */rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL;/* Set to a function to quote a filename in an application-specific fashion. Called with the text to quote, the type of match found (single or multiple) and a pointer to the quoting character to be used, which the function can reset if desired. */rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename; /* Function to call to remove quoting characters from a filename. Called before completion is attempted, so the embedded quotes do not interfere with matching names in the file system. Readline doesn't do anything with this; it's set only by applications. */rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL;/* Function to call to decide whether or not a word break character is quoted. If a character is quoted, it does not break words for the completer. */rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL;/* If non-zero, the completion functions don't append anything except a possible closing quote. This is set to 0 by rl_complete_internal and may be changed by an application-specific completion function. */int rl_completion_suppress_append = 0;/* Character appended to completed words when at the end of the line. The default is a space. */int rl_completion_append_character = ' ';/* If non-zero, the completion functions don't append any closing quote. This is set to 0 by rl_complete_internal and may be changed by an application-specific completion function. */int rl_completion_suppress_quote = 0;/* Set to any quote character readline thinks it finds before any application completion function is called. */int rl_completion_quote_character;/* Set to a non-zero value if readline found quoting anywhere in the word to be completed; set before any application completion function is called. */int rl_completion_found_quote;/* If non-zero, a slash will be appended to completed filenames that are symbolic links to directory names, subject to the value of the mark-directories variable (which is user-settable). This exists so that application completion functions can override the user's preference (set via the mark-symlinked-directories variable) if appropriate. It's set to the value of _rl_complete_mark_symlink_dirs in rl_complete_internal before any application-specific completion function is called, so without that function doing anything, the user's preferences are honored. */int rl_completion_mark_symlink_dirs;/* If non-zero, inhibit completion (temporarily). */int rl_inhibit_completion;/* Variables local to this file. *//* Local variable states what happened during the last completion attempt. */static int completion_changed_buffer;/*************************************//* *//* Bindable completion functions *//* *//*************************************//* Complete the word at or before point. You have supplied the function that does the initial simple matching selection algorithm (see rl_completion_matches ()). The default is to do filename completion. */intrl_complete (ignore, invoking_key) int ignore, invoking_key;{ if (rl_inhibit_completion) return (_rl_insert_char (ignore, invoking_key)); else if (rl_last_func == rl_complete && !completion_changed_buffer) return (rl_complete_internal ('?')); else if (_rl_complete_show_all) return (rl_complete_internal ('!')); else if (_rl_complete_show_unmodified) return (rl_complete_internal ('@')); else return (rl_complete_internal (TAB));}/* List the possible completions. See description of rl_complete (). */intrl_possible_completions (ignore, invoking_key) int ignore, invoking_key;{ return (rl_complete_internal ('?'));}intrl_insert_completions (ignore, invoking_key) int ignore, invoking_key;{ return (rl_complete_internal ('*'));}/* Return the correct value to pass to rl_complete_internal performing the same tests as rl_complete. This allows consecutive calls to an application's completion function to list possible completions and for an application-specific completion function to honor the show-all-if-ambiguous readline variable. */intrl_completion_mode (cfunc) rl_command_func_t *cfunc;{ if (rl_last_func == cfunc && !completion_changed_buffer) return '?'; else if (_rl_complete_show_all) return '!'; else if (_rl_complete_show_unmodified) return '@'; else return TAB;}/************************************//* *//* Completion utility functions *//* *//************************************//* Set default values for readline word completion. These are the variables that application completion functions can change or inspect. */static voidset_completion_defaults (what_to_do) int what_to_do;{ /* Only the completion entry function can change these. */ rl_filename_completion_desired = 0; rl_filename_quoting_desired = 1; rl_completion_type = what_to_do; rl_completion_suppress_append = rl_completion_suppress_quote = 0; /* The completion entry function may optionally change this. */ rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs;}/* The user must press "y" or "n". Non-zero return means "y" pressed. */static intget_y_or_n (for_pager) int for_pager;{ int c; for (;;) { RL_SETSTATE(RL_STATE_MOREINPUT); c = rl_read_key (); RL_UNSETSTATE(RL_STATE_MOREINPUT); if (c == 'y' || c == 'Y' || c == ' ') return (1); if (c == 'n' || c == 'N' || c == RUBOUT) return (0); if (c == ABORT_CHAR) _rl_abort_internal (); if (for_pager && (c == NEWLINE || c == RETURN)) return (2); if (for_pager && (c == 'q' || c == 'Q')) return (0); rl_ding (); }}static int_rl_internal_pager (lines)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -