📄 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */#define READLINE_LIBRARY#include <stdio.h>#include <sys/types.h>#include <fcntl.h>#if !defined (NO_SYS_FILE)# include <sys/file.h>#endif /* !NO_SYS_FILE */#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 <errno.h>/* Not all systems declare ERRNO in errno.h... and some systems #define it! */#if !defined (errno)extern int errno;#endif /* !errno */#include <pwd.h>#if defined (USG) && !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);# elseextern struct passwd *getpwent ();# endif /* !__STDC__ */#endif /* isc386 && _POSIX_SOURCE */#include "posixstat.h"/* System-specific feature definitions and include files. */#include "rldefs.h"/* Some standard library routines. */#include "readline.h"/* Possible values for do_replace in rl_complete_internal. */#define NO_MATCH 0#define SINGLE_MATCH 1#define MULT_MATCH 2#if !defined (strchr) && !defined (__STDC__)extern char *strchr (), *strrchr ();#endif /* !strchr && !__STDC__ */extern char *tilde_expand ();extern char *rl_copy_text ();extern Function *rl_last_func;extern int rl_editing_mode;extern int screenwidth;/* Forward declarations for functions defined and used in this file. */char *filename_completion_function ();char **completion_matches ();static int compare_strings ();static char *rl_strpbrk ();#if defined (STATIC_MALLOC)static char *xmalloc (), *xrealloc ();#elseextern char *xmalloc (), *xrealloc ();#endif /* STATIC_MALLOC *//* 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;/* If non-zero, non-unique completions always show the list of matches. */int _rl_complete_show_all = 0;#if defined (VISIBLE_STATS)# if !defined (X_OK)# define X_OK 1# endifstatic int stat_char ();/* 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 *//* **************************************************************** *//* *//* Completion matching, from readline's point of view. *//* *//* **************************************************************** *//* Pointer to the generator function for completion_matches (). NULL means to use filename_entry_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;/* Local variable states what happened during the last completion attempt. */static int completion_changed_buffer = 0;/* 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. */rl_complete (ignore, invoking_key) int ignore, invoking_key;{ 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 (). */rl_possible_completions (ignore, invoking_key) int ignore, invoking_key;{ return (rl_complete_internal ('?'));}rl_insert_completions (ignore, invoking_key) int ignore, invoking_key;{ return (rl_complete_internal ('*'));}/* The user must press "y" or "n". Non-zero return means "y" pressed. */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 (); ding (); }}/* 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\"\\'`@$><=;|&{(";/* 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 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_word_break_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;#if defined (SHELL)/* A function to strip quotes that are not protected by backquotes. It allows single quotes to appear within double quotes, and vice versa. It should be smarter. It's fairly shell-specific, hence the SHELL definition wrapper. */static char *_delete_quotes (text) char *text;{ char *ret, *p, *r; int l, quoted; l = strlen (text); ret = xmalloc (l + 1); for (quoted = 0, p = text, r = ret; p && *p; p++) { /* Allow backslash-quoted characters to pass through unscathed. */ if (*p == '\\') continue; /* Close quote. */ if (quoted && *p == quoted) { quoted = 0; continue; } /* Open quote. */ if (quoted == 0 && (*p == '\'' || *p == '"')) { quoted = *p; continue; } *r++ = *p; } *r = '\0'; return ret;}#endif /* SHELL *//* 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 = (char *)NULL; if (rl_filename_completion_desired) temp = strrchr (pathname, '/'); if (!temp) return (pathname); else return (++temp);}/* 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 1 if we printed an extension character, 0 if not. */static intprint_filename (to_print, full_pathname) char *to_print, *full_pathname;{#if !defined (VISIBLE_STATS) fputs (to_print, rl_outstream); return 0;#else char *s, c, *new_full_pathname; int extension_char = 0, slen, tlen; fputs (to_print, rl_outstream); if (rl_filename_completion_desired && rl_visible_stats) { /* If to_print != full_pathname, to_print is the basename of the path passed. In this case, we try to expand the directory name before checking for the stat character. */ if (to_print != full_pathname) { /* Terminate the directory name. */ c = to_print[-1]; to_print[-1] = '\0'; s = tilde_expand (full_pathname); if (rl_directory_completion_hook) (*rl_directory_completion_hook) (&s); slen = strlen (s); tlen = strlen (to_print); new_full_pathname = xmalloc (slen + tlen + 2); strcpy (new_full_pathname, s); new_full_pathname[slen] = '/'; strcpy (new_full_pathname + slen + 1, to_print); extension_char = stat_char (new_full_pathname); free (new_full_pathname); to_print[-1] = c; } else { s = tilde_expand (full_pathname); extension_char = stat_char (s); } free (s); if (extension_char) putc (extension_char, rl_outstream); return (extension_char != 0); } else return 0;#endif /* VISIBLE_STATS */}/* Complete the word at or before point. WHAT_TO_DO says what to do with the completion. `?' means list the possible completions. TAB means do standard completion. `*' means insert all of the possible completions. `!' means to do standard completion, and list all possible completions if there is more than one. */rl_complete_internal (what_to_do) int what_to_do;{ char **matches; Function *our_func; int start, scan, end, delimiter = 0, pass_next; char *text, *saved_line_buffer; char *replacement; char quote_char = '\0'; int found_quote = 0; if (rl_line_buffer) saved_line_buffer = savestring (rl_line_buffer); else saved_line_buffer = (char *)NULL; if (rl_completion_entry_function) our_func = rl_completion_entry_function; else our_func = (Function *)filename_completion_function; /* Only the completion entry function can change these. */ rl_filename_completion_desired = 0; rl_filename_quoting_desired = 1; /* We now look backwards for the start of a filename/variable word. */ end = rl_point; if (rl_point) { if (rl_completer_quote_characters) { /* We have a list of characters which can be used in pairs to quote substrings for the completer. Try to find the start of an unclosed quoted substring. */ /* FOUND_QUOTE is set so we know what kind of quotes we found. */ for (scan = pass_next = 0; scan < end; scan++) { if (pass_next) { pass_next = 0; continue; } if (rl_line_buffer[scan] == '\\') { pass_next = 1; found_quote |= 4; continue; } if (quote_char != '\0') { /* Ignore everything until the matching close quote char. */ if (rl_line_buffer[scan] == quote_char) { /* Found matching close. Abandon this substring. */ quote_char = '\0'; rl_point = end; } } else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) { /* Found start of a quoted substring. */ quote_char = rl_line_buffer[scan]; rl_point = scan + 1; /* Shell-like quoting conventions. */ if (quote_char == '\'') found_quote |= 1; else if (quote_char == '"') found_quote |= 2; } } } if (rl_point == end) { int quoted = 0; /* We didn't find an unclosed quoted substring upon which to do completion, so use the word break characters to find the substring on which to complete. */ while (--rl_point) { scan = rl_line_buffer[rl_point]; if (strchr (rl_completer_word_break_characters, scan) == 0) continue;#if defined (SHELL) /* Don't let word break characters in quoted substrings break words for the completer. */ if (found_quote && char_is_quoted (rl_line_buffer, rl_point)) continue;#endif /* SHELL */ /* Convoluted code, but it avoids an n^2 algorithm with calls to char_is_quoted. */ break; } } /* If we are at an unquoted word break, then advance past it. */ scan = rl_line_buffer[rl_point];#if defined (SHELL) if ((found_quote == 0 || char_is_quoted (rl_line_buffer, rl_point) == 0) &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -