📄 complete.c
字号:
/* complete.c -- filename completion for readline. */
/* Copyright (C) 1987, 1989, 1992 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>
#if !defined (HAVE_GETPW_DECLS)
extern struct passwd *getpwent ();
#endif /* USG && !HAVE_GETPW_DECLS */
/* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */
#if defined (isc386) && defined (_POSIX_SOURCE)
# if defined (__STDC__)
extern struct passwd *getpwent (void);
# else
extern struct passwd *getpwent ();
# endif /* !__STDC__ */
#endif /* isc386 && _POSIX_SOURCE */
#include "posixdir.h"
#include "posixstat.h"
/* System-specific feature definitions and include files. */
#include "rldefs.h"
/* Some standard library routines. */
#include "readline.h"
#include "xmalloc.h"
#include "rlprivate.h"
#ifdef __STDC__
typedef int QSFUNC (const void *, const void *);
#else
typedef int QSFUNC ();
#endif
/* 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. */
VFunction *rl_completion_display_matches_hook = (VFunction *)NULL;
/* Forward declarations for functions defined and used in this file. */
char *filename_completion_function __P((char *, int));
char **completion_matches __P((char *, CPFunction *));
#if defined (VISIBLE_STATS)
# if !defined (X_OK)
# define X_OK 1
# endif
static int stat_char __P((char *));
#endif
static char *rl_quote_filename __P((char *, int, char *));
static char *rl_strpbrk __P((char *, char *));
static char **remove_duplicate_matches __P((char **));
static void insert_match __P((char *, int, int, char *));
static int append_to_match __P((char *, int, int));
static void insert_all_matches __P((char **, int, char *));
static void display_matches __P((char **));
static int compute_lcd_of_matches __P((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, completed directory names have a slash appended. */
int _rl_complete_mark_directories = 1;
/* 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;
#else
int _rl_completion_case_fold;
#endif
/* 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. */
Function *rl_directory_completion_hook = (Function *)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 filename_completion_function (), the default filename
completer. */
Function *rl_completion_entry_function = (Function *)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. */
CPPFunction *rl_attempted_completion_function = (CPPFunction *)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;
/* 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\"\\'`@$><=" */
char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
/* List of basic quoting characters. */
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. */
char *rl_completer_word_break_characters = (char *)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. */
char *rl_completer_quote_characters = (char *)NULL;
/* List of characters that should be quoted in filenames by the completer. */
char *rl_filename_quote_characters = (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. */
char *rl_special_prefixes = (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. */
Function *rl_ignore_some_completions_function = (Function *)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. */
CPFunction *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. */
CPFunction *rl_filename_dequoting_function = (CPFunction *)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. */
Function *rl_char_is_quoted_p = (Function *)NULL;
/* 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, 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
completion_matches ()). The default is to do filename completion. */
int
rl_complete (ignore, invoking_key)
int ignore, invoking_key;
{
if (rl_inhibit_completion)
return (rl_insert (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
return (rl_complete_internal (TAB));
}
/* List the possible completions. See description of rl_complete (). */
int
rl_possible_completions (ignore, invoking_key)
int ignore, invoking_key;
{
return (rl_complete_internal ('?'));
}
int
rl_insert_completions (ignore, invoking_key)
int ignore, invoking_key;
{
return (rl_complete_internal ('*'));
}
/************************************/
/* */
/* Completion utility functions */
/* */
/************************************/
/* Find the first occurrence in STRING1 of any character from STRING2.
Return a pointer to the character in STRING1. */
static char *
rl_strpbrk (string1, string2)
char *string1, *string2;
{
register char *scan;
for (; *string1; string1++)
{
for (scan = string2; *scan; scan++)
{
if (*string1 == *scan)
{
return (string1);
}
}
}
return ((char *)NULL);
}
/* The user must press "y" or "n". Non-zero return means "y" pressed. */
static int
get_y_or_n ()
{
int c;
for (;;)
{
c = rl_read_key ();
if (c == 'y' || c == 'Y' || c == ' ')
return (1);
if (c == 'n' || c == 'N' || c == RUBOUT)
return (0);
if (c == ABORT_CHAR)
_rl_abort_internal ();
ding ();
}
}
#if defined (VISIBLE_STATS)
/* Return the character which best describes FILENAME.
`@' for symbolic links
`/' for directories
`*' for executables
`=' for sockets
`|' for FIFOs
`%' for character special devices
`#' for block special devices */
static int
stat_char (filename)
char *filename;
{
struct stat finfo;
int character, r;
#if defined (HAVE_LSTAT) && defined (S_ISLNK)
r = lstat (filename, &finfo);
#else
r = stat (filename, &finfo);
#endif
if (r == -1)
return (0);
character = 0;
if (S_ISDIR (finfo.st_mode))
character = '/';
#if defined (S_ISCHR)
else if (S_ISCHR (finfo.st_mode))
character = '%';
#endif /* S_ISCHR */
#if defined (S_ISBLK)
else if (S_ISBLK (finfo.st_mode))
character = '#';
#endif /* S_ISBLK */
#if defined (S_ISLNK)
else if (S_ISLNK (finfo.st_mode))
character = '@';
#endif /* S_ISLNK */
#if defined (S_ISSOCK)
else if (S_ISSOCK (finfo.st_mode))
character = '=';
#endif /* S_ISSOCK */
#if defined (S_ISFIFO)
else if (S_ISFIFO (finfo.st_mode))
character = '|';
#endif
else if (S_ISREG (finfo.st_mode))
{
if (access (filename, X_OK) == 0)
character = '*';
}
return (character);
}
#endif /* VISIBLE_STATS */
/* Return the portion of PATHNAME that should be output when listing
possible completions. If we are hacking filename completion, we
are only interested in the basename, the portion following the
final slash. Otherwise, we return what we were passed. */
static char *
printable_part (pathname)
char *pathname;
{
char *temp;
temp = rl_filename_completion_desired ? strrchr (pathname, '/') : (char *)NULL;
#if defined (HAVE_DOS_STYLE_FILE_SYSTEM)
if (temp == 0 && rl_filename_completion_desired
&& (pathname[0] && pathname[1] == ':'))
temp = pathname + 1;
#endif
return (temp ? ++temp : pathname);
}
/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
are using it, check for and output a single character for `special'
filenames. Return the number of characters we output. */
#define PUTX(c) \
do { \
if (CTRL_CHAR (c)) \
{ \
putc ('^', rl_outstream); \
putc (UNCTRL (c), rl_outstream); \
printed_len += 2; \
} \
else if (c == RUBOUT) \
{ \
putc ('^', rl_outstream); \
putc ('?', rl_outstream); \
printed_len += 2; \
} \
else \
{ \
putc (c, rl_outstream); \
printed_len++; \
} \
} while (0)
static int
print_filename (to_print, full_pathname)
char *to_print, *full_pathname;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -