1/* $FreeBSD: head/contrib/libreadline/complete.c 119614 2003-08-31 18:29:38Z ache $ */
| 1/* $FreeBSD: head/contrib/libreadline/complete.c 136758 2004-10-21 20:02:02Z peter $ */ 2
|
2/* complete.c -- filename completion for readline. */ 3
| 3/* complete.c -- filename completion for readline. */ 4
|
4/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
| 5/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
|
5 6 This file is part of the GNU Readline Library, a library for 7 reading lines of text with interactive input and history editing. 8 9 The GNU Readline Library is free software; you can redistribute it 10 and/or modify it under the terms of the GNU General Public License 11 as published by the Free Software Foundation; either version 2, or 12 (at your option) any later version. 13 14 The GNU Readline Library is distributed in the hope that it will be 15 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 16 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 The GNU General Public License is often shipped with GNU software, and 20 is generally kept in a file called COPYING or LICENSE. If you do not 21 have a copy of the license, write to the Free Software Foundation, 22 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 23#define READLINE_LIBRARY 24 25#if defined (HAVE_CONFIG_H) 26# include <config.h> 27#endif 28 29#include <sys/types.h> 30#include <fcntl.h> 31#if defined (HAVE_SYS_FILE_H)
| 6 7 This file is part of the GNU Readline Library, a library for 8 reading lines of text with interactive input and history editing. 9 10 The GNU Readline Library is free software; you can redistribute it 11 and/or modify it under the terms of the GNU General Public License 12 as published by the Free Software Foundation; either version 2, or 13 (at your option) any later version. 14 15 The GNU Readline Library is distributed in the hope that it will be 16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 The GNU General Public License is often shipped with GNU software, and 21 is generally kept in a file called COPYING or LICENSE. If you do not 22 have a copy of the license, write to the Free Software Foundation, 23 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 24#define READLINE_LIBRARY 25 26#if defined (HAVE_CONFIG_H) 27# include <config.h> 28#endif 29 30#include <sys/types.h> 31#include <fcntl.h> 32#if defined (HAVE_SYS_FILE_H)
|
32#include
| 33# include <sys/file.h>
|
33#endif 34 35#if defined (HAVE_UNISTD_H) 36# include <unistd.h> 37#endif /* HAVE_UNISTD_H */ 38 39#if defined (HAVE_STDLIB_H) 40# include <stdlib.h> 41#else 42# include "ansi_stdlib.h" 43#endif /* HAVE_STDLIB_H */ 44 45#include <stdio.h> 46 47#include <errno.h> 48#if !defined (errno) 49extern int errno; 50#endif /* !errno */ 51 52#include <pwd.h> 53 54#include "posixdir.h" 55#include "posixstat.h" 56 57/* System-specific feature definitions and include files. */ 58#include "rldefs.h" 59#include "rlmbutil.h" 60 61/* Some standard library routines. */ 62#include "readline.h" 63#include "xmalloc.h" 64#include "rlprivate.h" 65 66#ifdef __STDC__ 67typedef int QSFUNC (const void *, const void *); 68#else 69typedef int QSFUNC (); 70#endif 71 72#ifdef HAVE_LSTAT 73# define LSTAT lstat 74#else 75# define LSTAT stat 76#endif 77 78/* Unix version of a hidden file. Could be different on other systems. */ 79#define HIDDEN_FILE(fname) ((fname)[0] == '.') 80 81/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is 82 defined. */ 83#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE) 84extern struct passwd *getpwent PARAMS((void)); 85#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */ 86 87/* If non-zero, then this is the address of a function to call when 88 completing a word would normally display the list of possible matches. 89 This function is called instead of actually doing the display. 90 It takes three arguments: (char **matches, int num_matches, int max_length) 91 where MATCHES is the array of strings that matched, NUM_MATCHES is the 92 number of strings in that array, and MAX_LENGTH is the length of the 93 longest string in that array. */ 94rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL; 95 96#if defined (VISIBLE_STATS) 97# if !defined (X_OK) 98# define X_OK 1 99# endif 100static int stat_char PARAMS((char *)); 101#endif 102
| 34#endif 35 36#if defined (HAVE_UNISTD_H) 37# include <unistd.h> 38#endif /* HAVE_UNISTD_H */ 39 40#if defined (HAVE_STDLIB_H) 41# include <stdlib.h> 42#else 43# include "ansi_stdlib.h" 44#endif /* HAVE_STDLIB_H */ 45 46#include <stdio.h> 47 48#include <errno.h> 49#if !defined (errno) 50extern int errno; 51#endif /* !errno */ 52 53#include <pwd.h> 54 55#include "posixdir.h" 56#include "posixstat.h" 57 58/* System-specific feature definitions and include files. */ 59#include "rldefs.h" 60#include "rlmbutil.h" 61 62/* Some standard library routines. */ 63#include "readline.h" 64#include "xmalloc.h" 65#include "rlprivate.h" 66 67#ifdef __STDC__ 68typedef int QSFUNC (const void *, const void *); 69#else 70typedef int QSFUNC (); 71#endif 72 73#ifdef HAVE_LSTAT 74# define LSTAT lstat 75#else 76# define LSTAT stat 77#endif 78 79/* Unix version of a hidden file. Could be different on other systems. */ 80#define HIDDEN_FILE(fname) ((fname)[0] == '.') 81 82/* Most systems don't declare getpwent in <pwd.h> if _POSIX_SOURCE is 83 defined. */ 84#if !defined (HAVE_GETPW_DECLS) || defined (_POSIX_SOURCE) 85extern struct passwd *getpwent PARAMS((void)); 86#endif /* !HAVE_GETPW_DECLS || _POSIX_SOURCE */ 87 88/* If non-zero, then this is the address of a function to call when 89 completing a word would normally display the list of possible matches. 90 This function is called instead of actually doing the display. 91 It takes three arguments: (char **matches, int num_matches, int max_length) 92 where MATCHES is the array of strings that matched, NUM_MATCHES is the 93 number of strings in that array, and MAX_LENGTH is the length of the 94 longest string in that array. */ 95rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)NULL; 96 97#if defined (VISIBLE_STATS) 98# if !defined (X_OK) 99# define X_OK 1 100# endif 101static int stat_char PARAMS((char *)); 102#endif 103
|
| 104static int path_isdir PARAMS((const char *)); 105
|
103static char *rl_quote_filename PARAMS((char *, int, char *)); 104 105static void set_completion_defaults PARAMS((int)); 106static int get_y_or_n PARAMS((int)); 107static int _rl_internal_pager PARAMS((int)); 108static char *printable_part PARAMS((char *));
| 106static char *rl_quote_filename PARAMS((char *, int, char *)); 107 108static void set_completion_defaults PARAMS((int)); 109static int get_y_or_n PARAMS((int)); 110static int _rl_internal_pager PARAMS((int)); 111static char *printable_part PARAMS((char *));
|
| 112static int fnwidth PARAMS((const char *)); 113static int fnprint PARAMS((const char *));
|
109static int print_filename PARAMS((char *, char *)); 110 111static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int)); 112 113static char **remove_duplicate_matches PARAMS((char **)); 114static void insert_match PARAMS((char *, int, int, char *)); 115static int append_to_match PARAMS((char *, int, int, int)); 116static void insert_all_matches PARAMS((char **, int, char *)); 117static void display_matches PARAMS((char **)); 118static int compute_lcd_of_matches PARAMS((char **, int, const char *)); 119static int postprocess_matches PARAMS((char ***, int)); 120 121static char *make_quoted_replacement PARAMS((char *, int, char *)); 122 123/* **************************************************************** */ 124/* */ 125/* Completion matching, from readline's point of view. */ 126/* */ 127/* **************************************************************** */ 128 129/* Variables known only to the readline library. */ 130 131/* If non-zero, non-unique completions always show the list of matches. */ 132int _rl_complete_show_all = 0; 133
| 114static int print_filename PARAMS((char *, char *)); 115 116static char **gen_completion_matches PARAMS((char *, int, int, rl_compentry_func_t *, int, int)); 117 118static char **remove_duplicate_matches PARAMS((char **)); 119static void insert_match PARAMS((char *, int, int, char *)); 120static int append_to_match PARAMS((char *, int, int, int)); 121static void insert_all_matches PARAMS((char **, int, char *)); 122static void display_matches PARAMS((char **)); 123static int compute_lcd_of_matches PARAMS((char **, int, const char *)); 124static int postprocess_matches PARAMS((char ***, int)); 125 126static char *make_quoted_replacement PARAMS((char *, int, char *)); 127 128/* **************************************************************** */ 129/* */ 130/* Completion matching, from readline's point of view. */ 131/* */ 132/* **************************************************************** */ 133 134/* Variables known only to the readline library. */ 135 136/* If non-zero, non-unique completions always show the list of matches. */ 137int _rl_complete_show_all = 0; 138
|
| 139/* If non-zero, non-unique completions show the list of matches, unless it 140 is not possible to do partial completion and modify the line. */ 141int _rl_complete_show_unmodified = 0; 142
|
134/* If non-zero, completed directory names have a slash appended. */ 135int _rl_complete_mark_directories = 1; 136 137/* If non-zero, the symlinked directory completion behavior introduced in 138 readline-4.2a is disabled, and symlinks that point to directories have 139 a slash appended (subject to the value of _rl_complete_mark_directories). 140 This is user-settable via the mark-symlinked-directories variable. */ 141int _rl_complete_mark_symlink_dirs = 0; 142 143/* If non-zero, completions are printed horizontally in alphabetical order, 144 like `ls -x'. */ 145int _rl_print_completions_horizontally; 146 147/* Non-zero means that case is not significant in filename completion. */ 148#if defined (__MSDOS__) && !defined (__DJGPP__) 149int _rl_completion_case_fold = 1; 150#else 151int _rl_completion_case_fold; 152#endif 153 154/* If non-zero, don't match hidden files (filenames beginning with a `.' on 155 Unix) when doing filename completion. */ 156int _rl_match_hidden_files = 1; 157 158/* Global variables available to applications using readline. */ 159 160#if defined (VISIBLE_STATS) 161/* Non-zero means add an additional character to each filename displayed 162 during listing completion iff rl_filename_completion_desired which helps 163 to indicate the type of file being listed. */ 164int rl_visible_stats = 0; 165#endif /* VISIBLE_STATS */ 166 167/* If non-zero, then this is the address of a function to call when 168 completing on a directory name. The function is called with 169 the address of a string (the current directory name) as an arg. */ 170rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL; 171 172rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL; 173 174/* Non-zero means readline completion functions perform tilde expansion. */ 175int rl_complete_with_tilde_expansion = 0; 176 177/* Pointer to the generator function for completion_matches (). 178 NULL means to use rl_filename_completion_function (), the default filename 179 completer. */ 180rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL; 181 182/* Pointer to alternative function to create matches. 183 Function is called with TEXT, START, and END. 184 START and END are indices in RL_LINE_BUFFER saying what the boundaries 185 of TEXT are. 186 If this function exists and returns NULL then call the value of 187 rl_completion_entry_function to try to match, otherwise use the 188 array of strings returned. */ 189rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL; 190 191/* Non-zero means to suppress normal filename completion after the 192 user-specified completion function has been called. */ 193int rl_attempted_completion_over = 0; 194 195/* Set to a character indicating the type of completion being performed 196 by rl_complete_internal, available for use by application completion 197 functions. */ 198int rl_completion_type = 0; 199 200/* Up to this many items will be displayed in response to a 201 possible-completions call. After that, we ask the user if 202 she is sure she wants to see them all. */ 203int rl_completion_query_items = 100; 204 205int _rl_page_completions = 1; 206 207/* The basic list of characters that signal a break between words for the 208 completer routine. The contents of this variable is what breaks words 209 in the shell, i.e. " \t\n\"\\'`@$><=" */ 210const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */ 211 212/* List of basic quoting characters. */ 213const char *rl_basic_quote_characters = "\"'"; 214 215/* The list of characters that signal a break between words for 216 rl_complete_internal. The default list is the contents of 217 rl_basic_word_break_characters. */
| 143/* If non-zero, completed directory names have a slash appended. */ 144int _rl_complete_mark_directories = 1; 145 146/* If non-zero, the symlinked directory completion behavior introduced in 147 readline-4.2a is disabled, and symlinks that point to directories have 148 a slash appended (subject to the value of _rl_complete_mark_directories). 149 This is user-settable via the mark-symlinked-directories variable. */ 150int _rl_complete_mark_symlink_dirs = 0; 151 152/* If non-zero, completions are printed horizontally in alphabetical order, 153 like `ls -x'. */ 154int _rl_print_completions_horizontally; 155 156/* Non-zero means that case is not significant in filename completion. */ 157#if defined (__MSDOS__) && !defined (__DJGPP__) 158int _rl_completion_case_fold = 1; 159#else 160int _rl_completion_case_fold; 161#endif 162 163/* If non-zero, don't match hidden files (filenames beginning with a `.' on 164 Unix) when doing filename completion. */ 165int _rl_match_hidden_files = 1; 166 167/* Global variables available to applications using readline. */ 168 169#if defined (VISIBLE_STATS) 170/* Non-zero means add an additional character to each filename displayed 171 during listing completion iff rl_filename_completion_desired which helps 172 to indicate the type of file being listed. */ 173int rl_visible_stats = 0; 174#endif /* VISIBLE_STATS */ 175 176/* If non-zero, then this is the address of a function to call when 177 completing on a directory name. The function is called with 178 the address of a string (the current directory name) as an arg. */ 179rl_icppfunc_t *rl_directory_completion_hook = (rl_icppfunc_t *)NULL; 180 181rl_icppfunc_t *rl_directory_rewrite_hook = (rl_icppfunc_t *)NULL; 182 183/* Non-zero means readline completion functions perform tilde expansion. */ 184int rl_complete_with_tilde_expansion = 0; 185 186/* Pointer to the generator function for completion_matches (). 187 NULL means to use rl_filename_completion_function (), the default filename 188 completer. */ 189rl_compentry_func_t *rl_completion_entry_function = (rl_compentry_func_t *)NULL; 190 191/* Pointer to alternative function to create matches. 192 Function is called with TEXT, START, and END. 193 START and END are indices in RL_LINE_BUFFER saying what the boundaries 194 of TEXT are. 195 If this function exists and returns NULL then call the value of 196 rl_completion_entry_function to try to match, otherwise use the 197 array of strings returned. */ 198rl_completion_func_t *rl_attempted_completion_function = (rl_completion_func_t *)NULL; 199 200/* Non-zero means to suppress normal filename completion after the 201 user-specified completion function has been called. */ 202int rl_attempted_completion_over = 0; 203 204/* Set to a character indicating the type of completion being performed 205 by rl_complete_internal, available for use by application completion 206 functions. */ 207int rl_completion_type = 0; 208 209/* Up to this many items will be displayed in response to a 210 possible-completions call. After that, we ask the user if 211 she is sure she wants to see them all. */ 212int rl_completion_query_items = 100; 213 214int _rl_page_completions = 1; 215 216/* The basic list of characters that signal a break between words for the 217 completer routine. The contents of this variable is what breaks words 218 in the shell, i.e. " \t\n\"\\'`@$><=" */ 219const char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{("; /* }) */ 220 221/* List of basic quoting characters. */ 222const char *rl_basic_quote_characters = "\"'"; 223 224/* The list of characters that signal a break between words for 225 rl_complete_internal. The default list is the contents of 226 rl_basic_word_break_characters. */
|
218const char *rl_completer_word_break_characters = (const char *)NULL;
| 227/*const*/ char *rl_completer_word_break_characters = (/*const*/ char *)NULL;
|
219
| 228
|
| 229/* Hook function to allow an application to set the completion word 230 break characters before readline breaks up the line. Allows 231 position-dependent word break characters. */ 232rl_cpvfunc_t *rl_completion_word_break_hook = (rl_cpvfunc_t *)NULL; 233
|
220/* List of characters which can be used to quote a substring of the line. 221 Completion occurs on the entire substring, and within the substring 222 rl_completer_word_break_characters are treated as any other character, 223 unless they also appear within this list. */ 224const char *rl_completer_quote_characters = (const char *)NULL; 225 226/* List of characters that should be quoted in filenames by the completer. */ 227const char *rl_filename_quote_characters = (const char *)NULL; 228 229/* List of characters that are word break characters, but should be left 230 in TEXT when it is passed to the completion function. The shell uses 231 this to help determine what kind of completing to do. */ 232const char *rl_special_prefixes = (const char *)NULL; 233 234/* If non-zero, then disallow duplicates in the matches. */ 235int rl_ignore_completion_duplicates = 1; 236 237/* Non-zero means that the results of the matches are to be treated 238 as filenames. This is ALWAYS zero on entry, and can only be changed 239 within a completion entry finder function. */ 240int rl_filename_completion_desired = 0; 241 242/* Non-zero means that the results of the matches are to be quoted using 243 double quotes (or an application-specific quoting mechanism) if the 244 filename contains any characters in rl_filename_quote_chars. This is 245 ALWAYS non-zero on entry, and can only be changed within a completion 246 entry finder function. */ 247int rl_filename_quoting_desired = 1; 248 249/* This function, if defined, is called by the completer when real 250 filename completion is done, after all the matching names have been 251 generated. It is passed a (char**) known as matches in the code below. 252 It consists of a NULL-terminated array of pointers to potential 253 matching strings. The 1st element (matches[0]) is the maximal 254 substring that is common to all matches. This function can re-arrange 255 the list of matches as required, but all elements of the array must be 256 free()'d if they are deleted. The main intent of this function is 257 to implement FIGNORE a la SunOS csh. */ 258rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL; 259 260/* Set to a function to quote a filename in an application-specific fashion. 261 Called with the text to quote, the type of match found (single or multiple) 262 and a pointer to the quoting character to be used, which the function can 263 reset if desired. */ 264rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename; 265 266/* Function to call to remove quoting characters from a filename. Called 267 before completion is attempted, so the embedded quotes do not interfere 268 with matching names in the file system. Readline doesn't do anything 269 with this; it's set only by applications. */ 270rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL; 271 272/* Function to call to decide whether or not a word break character is 273 quoted. If a character is quoted, it does not break words for the 274 completer. */ 275rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL; 276 277/* If non-zero, the completion functions don't append anything except a 278 possible closing quote. This is set to 0 by rl_complete_internal and 279 may be changed by an application-specific completion function. */ 280int rl_completion_suppress_append = 0; 281 282/* Character appended to completed words when at the end of the line. The 283 default is a space. */ 284int rl_completion_append_character = ' '; 285
| 234/* List of characters which can be used to quote a substring of the line. 235 Completion occurs on the entire substring, and within the substring 236 rl_completer_word_break_characters are treated as any other character, 237 unless they also appear within this list. */ 238const char *rl_completer_quote_characters = (const char *)NULL; 239 240/* List of characters that should be quoted in filenames by the completer. */ 241const char *rl_filename_quote_characters = (const char *)NULL; 242 243/* List of characters that are word break characters, but should be left 244 in TEXT when it is passed to the completion function. The shell uses 245 this to help determine what kind of completing to do. */ 246const char *rl_special_prefixes = (const char *)NULL; 247 248/* If non-zero, then disallow duplicates in the matches. */ 249int rl_ignore_completion_duplicates = 1; 250 251/* Non-zero means that the results of the matches are to be treated 252 as filenames. This is ALWAYS zero on entry, and can only be changed 253 within a completion entry finder function. */ 254int rl_filename_completion_desired = 0; 255 256/* Non-zero means that the results of the matches are to be quoted using 257 double quotes (or an application-specific quoting mechanism) if the 258 filename contains any characters in rl_filename_quote_chars. This is 259 ALWAYS non-zero on entry, and can only be changed within a completion 260 entry finder function. */ 261int rl_filename_quoting_desired = 1; 262 263/* This function, if defined, is called by the completer when real 264 filename completion is done, after all the matching names have been 265 generated. It is passed a (char**) known as matches in the code below. 266 It consists of a NULL-terminated array of pointers to potential 267 matching strings. The 1st element (matches[0]) is the maximal 268 substring that is common to all matches. This function can re-arrange 269 the list of matches as required, but all elements of the array must be 270 free()'d if they are deleted. The main intent of this function is 271 to implement FIGNORE a la SunOS csh. */ 272rl_compignore_func_t *rl_ignore_some_completions_function = (rl_compignore_func_t *)NULL; 273 274/* Set to a function to quote a filename in an application-specific fashion. 275 Called with the text to quote, the type of match found (single or multiple) 276 and a pointer to the quoting character to be used, which the function can 277 reset if desired. */ 278rl_quote_func_t *rl_filename_quoting_function = rl_quote_filename; 279 280/* Function to call to remove quoting characters from a filename. Called 281 before completion is attempted, so the embedded quotes do not interfere 282 with matching names in the file system. Readline doesn't do anything 283 with this; it's set only by applications. */ 284rl_dequote_func_t *rl_filename_dequoting_function = (rl_dequote_func_t *)NULL; 285 286/* Function to call to decide whether or not a word break character is 287 quoted. If a character is quoted, it does not break words for the 288 completer. */ 289rl_linebuf_func_t *rl_char_is_quoted_p = (rl_linebuf_func_t *)NULL; 290 291/* If non-zero, the completion functions don't append anything except a 292 possible closing quote. This is set to 0 by rl_complete_internal and 293 may be changed by an application-specific completion function. */ 294int rl_completion_suppress_append = 0; 295 296/* Character appended to completed words when at the end of the line. The 297 default is a space. */ 298int rl_completion_append_character = ' '; 299
|
| 300/* If non-zero, the completion functions don't append any closing quote. 301 This is set to 0 by rl_complete_internal and may be changed by an 302 application-specific completion function. */ 303int rl_completion_suppress_quote = 0; 304 305/* Set to any quote character readline thinks it finds before any application 306 completion function is called. */ 307int rl_completion_quote_character; 308 309/* Set to a non-zero value if readline found quoting anywhere in the word to 310 be completed; set before any application completion function is called. */ 311int rl_completion_found_quote; 312
|
286/* If non-zero, a slash will be appended to completed filenames that are 287 symbolic links to directory names, subject to the value of the 288 mark-directories variable (which is user-settable). This exists so 289 that application completion functions can override the user's preference 290 (set via the mark-symlinked-directories variable) if appropriate. 291 It's set to the value of _rl_complete_mark_symlink_dirs in 292 rl_complete_internal before any application-specific completion 293 function is called, so without that function doing anything, the user's 294 preferences are honored. */ 295int rl_completion_mark_symlink_dirs; 296 297/* If non-zero, inhibit completion (temporarily). */ 298int rl_inhibit_completion; 299 300/* Variables local to this file. */ 301 302/* Local variable states what happened during the last completion attempt. */ 303static int completion_changed_buffer; 304 305/*************************************/ 306/* */ 307/* Bindable completion functions */ 308/* */ 309/*************************************/ 310 311/* Complete the word at or before point. You have supplied the function 312 that does the initial simple matching selection algorithm (see 313 rl_completion_matches ()). The default is to do filename completion. */ 314int 315rl_complete (ignore, invoking_key) 316 int ignore, invoking_key; 317{ 318 if (rl_inhibit_completion) 319 return (_rl_insert_char (ignore, invoking_key)); 320 else if (rl_last_func == rl_complete && !completion_changed_buffer) 321 return (rl_complete_internal ('?')); 322 else if (_rl_complete_show_all) 323 return (rl_complete_internal ('!'));
| 313/* If non-zero, a slash will be appended to completed filenames that are 314 symbolic links to directory names, subject to the value of the 315 mark-directories variable (which is user-settable). This exists so 316 that application completion functions can override the user's preference 317 (set via the mark-symlinked-directories variable) if appropriate. 318 It's set to the value of _rl_complete_mark_symlink_dirs in 319 rl_complete_internal before any application-specific completion 320 function is called, so without that function doing anything, the user's 321 preferences are honored. */ 322int rl_completion_mark_symlink_dirs; 323 324/* If non-zero, inhibit completion (temporarily). */ 325int rl_inhibit_completion; 326 327/* Variables local to this file. */ 328 329/* Local variable states what happened during the last completion attempt. */ 330static int completion_changed_buffer; 331 332/*************************************/ 333/* */ 334/* Bindable completion functions */ 335/* */ 336/*************************************/ 337 338/* Complete the word at or before point. You have supplied the function 339 that does the initial simple matching selection algorithm (see 340 rl_completion_matches ()). The default is to do filename completion. */ 341int 342rl_complete (ignore, invoking_key) 343 int ignore, invoking_key; 344{ 345 if (rl_inhibit_completion) 346 return (_rl_insert_char (ignore, invoking_key)); 347 else if (rl_last_func == rl_complete && !completion_changed_buffer) 348 return (rl_complete_internal ('?')); 349 else if (_rl_complete_show_all) 350 return (rl_complete_internal ('!'));
|
| 351 else if (_rl_complete_show_unmodified) 352 return (rl_complete_internal ('@'));
|
324 else 325 return (rl_complete_internal (TAB)); 326} 327 328/* List the possible completions. See description of rl_complete (). */ 329int 330rl_possible_completions (ignore, invoking_key) 331 int ignore, invoking_key; 332{ 333 return (rl_complete_internal ('?')); 334} 335 336int 337rl_insert_completions (ignore, invoking_key) 338 int ignore, invoking_key; 339{ 340 return (rl_complete_internal ('*')); 341} 342 343/* Return the correct value to pass to rl_complete_internal performing 344 the same tests as rl_complete. This allows consecutive calls to an 345 application's completion function to list possible completions and for 346 an application-specific completion function to honor the 347 show-all-if-ambiguous readline variable. */ 348int 349rl_completion_mode (cfunc) 350 rl_command_func_t *cfunc; 351{ 352 if (rl_last_func == cfunc && !completion_changed_buffer) 353 return '?'; 354 else if (_rl_complete_show_all) 355 return '!';
| 353 else 354 return (rl_complete_internal (TAB)); 355} 356 357/* List the possible completions. See description of rl_complete (). */ 358int 359rl_possible_completions (ignore, invoking_key) 360 int ignore, invoking_key; 361{ 362 return (rl_complete_internal ('?')); 363} 364 365int 366rl_insert_completions (ignore, invoking_key) 367 int ignore, invoking_key; 368{ 369 return (rl_complete_internal ('*')); 370} 371 372/* Return the correct value to pass to rl_complete_internal performing 373 the same tests as rl_complete. This allows consecutive calls to an 374 application's completion function to list possible completions and for 375 an application-specific completion function to honor the 376 show-all-if-ambiguous readline variable. */ 377int 378rl_completion_mode (cfunc) 379 rl_command_func_t *cfunc; 380{ 381 if (rl_last_func == cfunc && !completion_changed_buffer) 382 return '?'; 383 else if (_rl_complete_show_all) 384 return '!';
|
| 385 else if (_rl_complete_show_unmodified) 386 return '@';
|
356 else 357 return TAB; 358} 359 360/************************************/ 361/* */ 362/* Completion utility functions */ 363/* */ 364/************************************/ 365 366/* Set default values for readline word completion. These are the variables 367 that application completion functions can change or inspect. */ 368static void 369set_completion_defaults (what_to_do) 370 int what_to_do; 371{ 372 /* Only the completion entry function can change these. */ 373 rl_filename_completion_desired = 0; 374 rl_filename_quoting_desired = 1; 375 rl_completion_type = what_to_do;
| 387 else 388 return TAB; 389} 390 391/************************************/ 392/* */ 393/* Completion utility functions */ 394/* */ 395/************************************/ 396 397/* Set default values for readline word completion. These are the variables 398 that application completion functions can change or inspect. */ 399static void 400set_completion_defaults (what_to_do) 401 int what_to_do; 402{ 403 /* Only the completion entry function can change these. */ 404 rl_filename_completion_desired = 0; 405 rl_filename_quoting_desired = 1; 406 rl_completion_type = what_to_do;
|
376 rl_completion_suppress_append = 0;
| 407 rl_completion_suppress_append = rl_completion_suppress_quote = 0;
|
377 378 /* The completion entry function may optionally change this. */ 379 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs; 380} 381 382/* The user must press "y" or "n". Non-zero return means "y" pressed. */ 383static int 384get_y_or_n (for_pager) 385 int for_pager; 386{ 387 int c; 388 389 for (;;) 390 { 391 RL_SETSTATE(RL_STATE_MOREINPUT); 392 c = rl_read_key (); 393 RL_UNSETSTATE(RL_STATE_MOREINPUT); 394 395 if (c == 'y' || c == 'Y' || c == ' ') 396 return (1); 397 if (c == 'n' || c == 'N' || c == RUBOUT) 398 return (0); 399 if (c == ABORT_CHAR) 400 _rl_abort_internal (); 401 if (for_pager && (c == NEWLINE || c == RETURN)) 402 return (2); 403 if (for_pager && (c == 'q' || c == 'Q')) 404 return (0); 405 rl_ding (); 406 } 407} 408 409static int 410_rl_internal_pager (lines) 411 int lines; 412{ 413 int i; 414 415 fprintf (rl_outstream, "--More--"); 416 fflush (rl_outstream); 417 i = get_y_or_n (1); 418 _rl_erase_entire_line (); 419 if (i == 0) 420 return -1; 421 else if (i == 2) 422 return (lines - 1); 423 else 424 return 0; 425} 426
| 408 409 /* The completion entry function may optionally change this. */ 410 rl_completion_mark_symlink_dirs = _rl_complete_mark_symlink_dirs; 411} 412 413/* The user must press "y" or "n". Non-zero return means "y" pressed. */ 414static int 415get_y_or_n (for_pager) 416 int for_pager; 417{ 418 int c; 419 420 for (;;) 421 { 422 RL_SETSTATE(RL_STATE_MOREINPUT); 423 c = rl_read_key (); 424 RL_UNSETSTATE(RL_STATE_MOREINPUT); 425 426 if (c == 'y' || c == 'Y' || c == ' ') 427 return (1); 428 if (c == 'n' || c == 'N' || c == RUBOUT) 429 return (0); 430 if (c == ABORT_CHAR) 431 _rl_abort_internal (); 432 if (for_pager && (c == NEWLINE || c == RETURN)) 433 return (2); 434 if (for_pager && (c == 'q' || c == 'Q')) 435 return (0); 436 rl_ding (); 437 } 438} 439 440static int 441_rl_internal_pager (lines) 442 int lines; 443{ 444 int i; 445 446 fprintf (rl_outstream, "--More--"); 447 fflush (rl_outstream); 448 i = get_y_or_n (1); 449 _rl_erase_entire_line (); 450 if (i == 0) 451 return -1; 452 else if (i == 2) 453 return (lines - 1); 454 else 455 return 0; 456} 457
|
| 458static int 459path_isdir (filename) 460 const char *filename; 461{ 462 struct stat finfo; 463 464 return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)); 465} 466
|
427#if defined (VISIBLE_STATS) 428/* Return the character which best describes FILENAME. 429 `@' for symbolic links 430 `/' for directories 431 `*' for executables 432 `=' for sockets 433 `|' for FIFOs 434 `%' for character special devices 435 `#' for block special devices */ 436static int 437stat_char (filename) 438 char *filename; 439{ 440 struct stat finfo; 441 int character, r; 442 443#if defined (HAVE_LSTAT) && defined (S_ISLNK) 444 r = lstat (filename, &finfo); 445#else 446 r = stat (filename, &finfo); 447#endif 448 449 if (r == -1) 450 return (0); 451 452 character = 0; 453 if (S_ISDIR (finfo.st_mode)) 454 character = '/'; 455#if defined (S_ISCHR) 456 else if (S_ISCHR (finfo.st_mode)) 457 character = '%'; 458#endif /* S_ISCHR */ 459#if defined (S_ISBLK) 460 else if (S_ISBLK (finfo.st_mode)) 461 character = '#'; 462#endif /* S_ISBLK */ 463#if defined (S_ISLNK) 464 else if (S_ISLNK (finfo.st_mode)) 465 character = '@'; 466#endif /* S_ISLNK */ 467#if defined (S_ISSOCK) 468 else if (S_ISSOCK (finfo.st_mode)) 469 character = '='; 470#endif /* S_ISSOCK */ 471#if defined (S_ISFIFO) 472 else if (S_ISFIFO (finfo.st_mode)) 473 character = '|'; 474#endif 475 else if (S_ISREG (finfo.st_mode)) 476 { 477 if (access (filename, X_OK) == 0) 478 character = '*'; 479 } 480 return (character); 481} 482#endif /* VISIBLE_STATS */ 483 484/* Return the portion of PATHNAME that should be output when listing 485 possible completions. If we are hacking filename completion, we 486 are only interested in the basename, the portion following the 487 final slash. Otherwise, we return what we were passed. Since 488 printing empty strings is not very informative, if we're doing 489 filename completion, and the basename is the empty string, we look 490 for the previous slash and return the portion following that. If 491 there's no previous slash, we just return what we were passed. */ 492static char * 493printable_part (pathname) 494 char *pathname; 495{ 496 char *temp, *x; 497 498 if (rl_filename_completion_desired == 0) /* don't need to do anything */ 499 return (pathname); 500 501 temp = strrchr (pathname, '/'); 502#if defined (__MSDOS__) 503 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':') 504 temp = pathname + 1; 505#endif 506 507 if (temp == 0 || *temp == '\0') 508 return (pathname); 509 /* If the basename is NULL, we might have a pathname like '/usr/src/'. 510 Look for a previous slash and, if one is found, return the portion 511 following that slash. If there's no previous slash, just return the 512 pathname we were passed. */ 513 else if (temp[1] == '\0') 514 { 515 for (x = temp - 1; x > pathname; x--) 516 if (*x == '/') 517 break; 518 return ((*x == '/') ? x + 1 : pathname); 519 } 520 else 521 return ++temp; 522} 523
| 467#if defined (VISIBLE_STATS) 468/* Return the character which best describes FILENAME. 469 `@' for symbolic links 470 `/' for directories 471 `*' for executables 472 `=' for sockets 473 `|' for FIFOs 474 `%' for character special devices 475 `#' for block special devices */ 476static int 477stat_char (filename) 478 char *filename; 479{ 480 struct stat finfo; 481 int character, r; 482 483#if defined (HAVE_LSTAT) && defined (S_ISLNK) 484 r = lstat (filename, &finfo); 485#else 486 r = stat (filename, &finfo); 487#endif 488 489 if (r == -1) 490 return (0); 491 492 character = 0; 493 if (S_ISDIR (finfo.st_mode)) 494 character = '/'; 495#if defined (S_ISCHR) 496 else if (S_ISCHR (finfo.st_mode)) 497 character = '%'; 498#endif /* S_ISCHR */ 499#if defined (S_ISBLK) 500 else if (S_ISBLK (finfo.st_mode)) 501 character = '#'; 502#endif /* S_ISBLK */ 503#if defined (S_ISLNK) 504 else if (S_ISLNK (finfo.st_mode)) 505 character = '@'; 506#endif /* S_ISLNK */ 507#if defined (S_ISSOCK) 508 else if (S_ISSOCK (finfo.st_mode)) 509 character = '='; 510#endif /* S_ISSOCK */ 511#if defined (S_ISFIFO) 512 else if (S_ISFIFO (finfo.st_mode)) 513 character = '|'; 514#endif 515 else if (S_ISREG (finfo.st_mode)) 516 { 517 if (access (filename, X_OK) == 0) 518 character = '*'; 519 } 520 return (character); 521} 522#endif /* VISIBLE_STATS */ 523 524/* Return the portion of PATHNAME that should be output when listing 525 possible completions. If we are hacking filename completion, we 526 are only interested in the basename, the portion following the 527 final slash. Otherwise, we return what we were passed. Since 528 printing empty strings is not very informative, if we're doing 529 filename completion, and the basename is the empty string, we look 530 for the previous slash and return the portion following that. If 531 there's no previous slash, we just return what we were passed. */ 532static char * 533printable_part (pathname) 534 char *pathname; 535{ 536 char *temp, *x; 537 538 if (rl_filename_completion_desired == 0) /* don't need to do anything */ 539 return (pathname); 540 541 temp = strrchr (pathname, '/'); 542#if defined (__MSDOS__) 543 if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':') 544 temp = pathname + 1; 545#endif 546 547 if (temp == 0 || *temp == '\0') 548 return (pathname); 549 /* If the basename is NULL, we might have a pathname like '/usr/src/'. 550 Look for a previous slash and, if one is found, return the portion 551 following that slash. If there's no previous slash, just return the 552 pathname we were passed. */ 553 else if (temp[1] == '\0') 554 { 555 for (x = temp - 1; x > pathname; x--) 556 if (*x == '/') 557 break; 558 return ((*x == '/') ? x + 1 : pathname); 559 } 560 else 561 return ++temp; 562} 563
|
| 564/* Compute width of STRING when displayed on screen by print_filename */ 565static int 566fnwidth (string) 567 const char *string; 568{ 569 int width, pos; 570#if defined (HANDLE_MULTIBYTE) 571 mbstate_t ps; 572 int left, w; 573 size_t clen; 574 wchar_t wc; 575 576 left = strlen (string) + 1; 577 memset (&ps, 0, sizeof (mbstate_t)); 578#endif 579 580 width = pos = 0; 581 while (string[pos]) 582 { 583 if (CTRL_CHAR (*string) || *string == RUBOUT) 584 { 585 width += 2; 586 pos++; 587 } 588 else 589 { 590#if defined (HANDLE_MULTIBYTE) 591 clen = mbrtowc (&wc, string + pos, left - pos, &ps); 592 if (MB_INVALIDCH (clen)) 593 { 594 width++; 595 pos++; 596 memset (&ps, 0, sizeof (mbstate_t)); 597 } 598 else if (MB_NULLWCH (clen)) 599 break; 600 else 601 { 602 pos += clen; 603 w = wcwidth (wc); 604 width += (w >= 0) ? w : 1; 605 } 606#else 607 width++; 608 pos++; 609#endif 610 } 611 } 612 613 return width; 614} 615 616static int 617fnprint (to_print) 618 const char *to_print; 619{ 620 int printed_len; 621 const char *s; 622#if defined (HANDLE_MULTIBYTE) 623 mbstate_t ps; 624 const char *end; 625 size_t tlen; 626 627 end = to_print + strlen (to_print) + 1; 628 memset (&ps, 0, sizeof (mbstate_t)); 629#endif 630 631 printed_len = 0; 632 s = to_print; 633 while (*s) 634 { 635 if (CTRL_CHAR (*s)) 636 { 637 putc ('^', rl_outstream); 638 putc (UNCTRL (*s), rl_outstream); 639 printed_len += 2; 640 s++; 641#if defined (HANDLE_MULTIBYTE) 642 memset (&ps, 0, sizeof (mbstate_t)); 643#endif 644 } 645 else if (*s == RUBOUT) 646 { 647 putc ('^', rl_outstream); 648 putc ('?', rl_outstream); 649 printed_len += 2; 650 s++; 651#if defined (HANDLE_MULTIBYTE) 652 memset (&ps, 0, sizeof (mbstate_t)); 653#endif 654 } 655 else 656 { 657#if defined (HANDLE_MULTIBYTE) 658 tlen = mbrlen (s, end - s, &ps); 659 if (MB_INVALIDCH (tlen)) 660 { 661 tlen = 1; 662 memset (&ps, 0, sizeof (mbstate_t)); 663 } 664 else if (MB_NULLWCH (tlen)) 665 break; 666 fwrite (s, 1, tlen, rl_outstream); 667 s += tlen; 668#else 669 putc (*s, rl_outstream); 670 s++; 671#endif 672 printed_len++; 673 } 674 } 675 676 return printed_len; 677} 678
|
524/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we 525 are using it, check for and output a single character for `special' 526 filenames. Return the number of characters we output. */ 527
| 679/* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we 680 are using it, check for and output a single character for `special' 681 filenames. Return the number of characters we output. */ 682
|
528#define PUTX(c) \ 529 do { \ 530 if (CTRL_CHAR (c)) \ 531 { \ 532 putc ('^', rl_outstream); \ 533 putc (UNCTRL (c), rl_outstream); \ 534 printed_len += 2; \ 535 } \ 536 else if (c == RUBOUT) \ 537 { \ 538 putc ('^', rl_outstream); \ 539 putc ('?', rl_outstream); \ 540 printed_len += 2; \ 541 } \ 542 else \ 543 { \ 544 putc (c, rl_outstream); \ 545 printed_len++; \ 546 } \ 547 } while (0) 548
| |
549static int 550print_filename (to_print, full_pathname) 551 char *to_print, *full_pathname; 552{
| 683static int 684print_filename (to_print, full_pathname) 685 char *to_print, *full_pathname; 686{
|
553 int printed_len = 0; 554#if !defined (VISIBLE_STATS) 555 char *s; 556 557 for (s = to_print; *s; s++) 558 { 559 PUTX (*s); 560 } 561#else
| 687 int printed_len, extension_char, slen, tlen;
|
562 char *s, c, *new_full_pathname;
| 688 char *s, c, *new_full_pathname;
|
563 int extension_char, slen, tlen;
| |
564
| 689
|
565 for (s = to_print; *s; s++) 566 { 567 PUTX (*s); 568 }
| 690 extension_char = 0; 691 printed_len = fnprint (to_print);
|
569
| 692
|
570 if (rl_filename_completion_desired && rl_visible_stats)
| 693#if defined (VISIBLE_STATS) 694 if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories)) 695#else 696 if (rl_filename_completion_desired && _rl_complete_mark_directories) 697#endif
|
571 { 572 /* If to_print != full_pathname, to_print is the basename of the 573 path passed. In this case, we try to expand the directory 574 name before checking for the stat character. */ 575 if (to_print != full_pathname) 576 { 577 /* Terminate the directory name. */ 578 c = to_print[-1]; 579 to_print[-1] = '\0'; 580 581 /* If setting the last slash in full_pathname to a NUL results in 582 full_pathname being the empty string, we are trying to complete 583 files in the root directory. If we pass a null string to the 584 bash directory completion hook, for example, it will expand it 585 to the current directory. We just want the `/'. */ 586 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/"); 587 if (rl_directory_completion_hook) 588 (*rl_directory_completion_hook) (&s); 589 590 slen = strlen (s); 591 tlen = strlen (to_print); 592 new_full_pathname = (char *)xmalloc (slen + tlen + 2); 593 strcpy (new_full_pathname, s); 594 new_full_pathname[slen] = '/'; 595 strcpy (new_full_pathname + slen + 1, to_print); 596
| 698 { 699 /* If to_print != full_pathname, to_print is the basename of the 700 path passed. In this case, we try to expand the directory 701 name before checking for the stat character. */ 702 if (to_print != full_pathname) 703 { 704 /* Terminate the directory name. */ 705 c = to_print[-1]; 706 to_print[-1] = '\0'; 707 708 /* If setting the last slash in full_pathname to a NUL results in 709 full_pathname being the empty string, we are trying to complete 710 files in the root directory. If we pass a null string to the 711 bash directory completion hook, for example, it will expand it 712 to the current directory. We just want the `/'. */ 713 s = tilde_expand (full_pathname && *full_pathname ? full_pathname : "/"); 714 if (rl_directory_completion_hook) 715 (*rl_directory_completion_hook) (&s); 716 717 slen = strlen (s); 718 tlen = strlen (to_print); 719 new_full_pathname = (char *)xmalloc (slen + tlen + 2); 720 strcpy (new_full_pathname, s); 721 new_full_pathname[slen] = '/'; 722 strcpy (new_full_pathname + slen + 1, to_print); 723
|
597 extension_char = stat_char (new_full_pathname);
| 724#if defined (VISIBLE_STATS) 725 if (rl_visible_stats) 726 extension_char = stat_char (new_full_pathname); 727 else 728#endif 729 if (path_isdir (new_full_pathname)) 730 extension_char = '/';
|
598 599 free (new_full_pathname); 600 to_print[-1] = c; 601 } 602 else 603 { 604 s = tilde_expand (full_pathname);
| 731 732 free (new_full_pathname); 733 to_print[-1] = c; 734 } 735 else 736 { 737 s = tilde_expand (full_pathname);
|
605 extension_char = stat_char (s);
| 738#if defined (VISIBLE_STATS) 739 if (rl_visible_stats) 740 extension_char = stat_char (s); 741 else 742#endif 743 if (path_isdir (s)) 744 extension_char = '/';
|
606 } 607 608 free (s); 609 if (extension_char) 610 { 611 putc (extension_char, rl_outstream); 612 printed_len++; 613 } 614 }
| 745 } 746 747 free (s); 748 if (extension_char) 749 { 750 putc (extension_char, rl_outstream); 751 printed_len++; 752 } 753 }
|
615#endif /* VISIBLE_STATS */
| 754
|
616 return printed_len; 617} 618 619static char * 620rl_quote_filename (s, rtype, qcp) 621 char *s; 622 int rtype; 623 char *qcp; 624{ 625 char *r; 626 627 r = (char *)xmalloc (strlen (s) + 2); 628 *r = *rl_completer_quote_characters; 629 strcpy (r + 1, s); 630 if (qcp) 631 *qcp = *rl_completer_quote_characters; 632 return r; 633} 634 635/* Find the bounds of the current word for completion purposes, and leave 636 rl_point set to the end of the word. This function skips quoted 637 substrings (characters between matched pairs of characters in 638 rl_completer_quote_characters). First we try to find an unclosed 639 quoted substring on which to do matching. If one is not found, we use 640 the word break characters to find the boundaries of the current word. 641 We call an application-specific function to decide whether or not a 642 particular word break character is quoted; if that function returns a 643 non-zero result, the character does not break a word. This function 644 returns the opening quote character if we found an unclosed quoted 645 substring, '\0' otherwise. FP, if non-null, is set to a value saying 646 which (shell-like) quote characters we found (single quote, double 647 quote, or backslash) anywhere in the string. DP, if non-null, is set to 648 the value of the delimiter character that caused a word break. */ 649 650char 651_rl_find_completion_word (fp, dp) 652 int *fp, *dp; 653{ 654 int scan, end, found_quote, delimiter, pass_next, isbrk;
| 755 return printed_len; 756} 757 758static char * 759rl_quote_filename (s, rtype, qcp) 760 char *s; 761 int rtype; 762 char *qcp; 763{ 764 char *r; 765 766 r = (char *)xmalloc (strlen (s) + 2); 767 *r = *rl_completer_quote_characters; 768 strcpy (r + 1, s); 769 if (qcp) 770 *qcp = *rl_completer_quote_characters; 771 return r; 772} 773 774/* Find the bounds of the current word for completion purposes, and leave 775 rl_point set to the end of the word. This function skips quoted 776 substrings (characters between matched pairs of characters in 777 rl_completer_quote_characters). First we try to find an unclosed 778 quoted substring on which to do matching. If one is not found, we use 779 the word break characters to find the boundaries of the current word. 780 We call an application-specific function to decide whether or not a 781 particular word break character is quoted; if that function returns a 782 non-zero result, the character does not break a word. This function 783 returns the opening quote character if we found an unclosed quoted 784 substring, '\0' otherwise. FP, if non-null, is set to a value saying 785 which (shell-like) quote characters we found (single quote, double 786 quote, or backslash) anywhere in the string. DP, if non-null, is set to 787 the value of the delimiter character that caused a word break. */ 788 789char 790_rl_find_completion_word (fp, dp) 791 int *fp, *dp; 792{ 793 int scan, end, found_quote, delimiter, pass_next, isbrk;
|
655 char quote_char;
| 794 char quote_char, *brkchars;
|
656 657 end = rl_point; 658 found_quote = delimiter = 0; 659 quote_char = '\0'; 660
| 795 796 end = rl_point; 797 found_quote = delimiter = 0; 798 quote_char = '\0'; 799
|
| 800 brkchars = 0; 801 if (rl_completion_word_break_hook) 802 brkchars = (*rl_completion_word_break_hook) (); 803 if (brkchars == 0) 804 brkchars = rl_completer_word_break_characters; 805
|
661 if (rl_completer_quote_characters) 662 { 663 /* We have a list of characters which can be used in pairs to 664 quote substrings for the completer. Try to find the start 665 of an unclosed quoted substring. */ 666 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
| 806 if (rl_completer_quote_characters) 807 { 808 /* We have a list of characters which can be used in pairs to 809 quote substrings for the completer. Try to find the start 810 of an unclosed quoted substring. */ 811 /* FOUND_QUOTE is set so we know what kind of quotes we found. */
|
| 812#if defined (HANDLE_MULTIBYTE) 813 for (scan = pass_next = 0; scan < end; 814 scan = ((MB_CUR_MAX == 1 || rl_byte_oriented) 815 ? (scan + 1) 816 : _rl_find_next_mbchar (rl_line_buffer, scan, 1, MB_FIND_ANY))) 817#else
|
667 for (scan = pass_next = 0; scan < end; scan++)
| 818 for (scan = pass_next = 0; scan < end; scan++)
|
| 819#endif
|
668 { 669 if (pass_next) 670 { 671 pass_next = 0; 672 continue; 673 } 674 675 /* Shell-like semantics for single quotes -- don't allow backslash 676 to quote anything in single quotes, especially not the closing 677 quote. If you don't like this, take out the check on the value 678 of quote_char. */ 679 if (quote_char != '\'' && rl_line_buffer[scan] == '\\') 680 { 681 pass_next = 1; 682 found_quote |= RL_QF_BACKSLASH; 683 continue; 684 } 685 686 if (quote_char != '\0') 687 { 688 /* Ignore everything until the matching close quote char. */ 689 if (rl_line_buffer[scan] == quote_char) 690 { 691 /* Found matching close. Abandon this substring. */ 692 quote_char = '\0'; 693 rl_point = end; 694 } 695 } 696 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) 697 { 698 /* Found start of a quoted substring. */ 699 quote_char = rl_line_buffer[scan]; 700 rl_point = scan + 1; 701 /* Shell-like quoting conventions. */ 702 if (quote_char == '\'') 703 found_quote |= RL_QF_SINGLE_QUOTE; 704 else if (quote_char == '"') 705 found_quote |= RL_QF_DOUBLE_QUOTE; 706 else 707 found_quote |= RL_QF_OTHER_QUOTE; 708 } 709 } 710 } 711 712 if (rl_point == end && quote_char == '\0') 713 { 714 /* We didn't find an unclosed quoted substring upon which to do 715 completion, so use the word break characters to find the 716 substring on which to complete. */ 717#if defined (HANDLE_MULTIBYTE) 718 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY)) 719#else 720 while (--rl_point) 721#endif 722 { 723 scan = rl_line_buffer[rl_point]; 724
| 820 { 821 if (pass_next) 822 { 823 pass_next = 0; 824 continue; 825 } 826 827 /* Shell-like semantics for single quotes -- don't allow backslash 828 to quote anything in single quotes, especially not the closing 829 quote. If you don't like this, take out the check on the value 830 of quote_char. */ 831 if (quote_char != '\'' && rl_line_buffer[scan] == '\\') 832 { 833 pass_next = 1; 834 found_quote |= RL_QF_BACKSLASH; 835 continue; 836 } 837 838 if (quote_char != '\0') 839 { 840 /* Ignore everything until the matching close quote char. */ 841 if (rl_line_buffer[scan] == quote_char) 842 { 843 /* Found matching close. Abandon this substring. */ 844 quote_char = '\0'; 845 rl_point = end; 846 } 847 } 848 else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan])) 849 { 850 /* Found start of a quoted substring. */ 851 quote_char = rl_line_buffer[scan]; 852 rl_point = scan + 1; 853 /* Shell-like quoting conventions. */ 854 if (quote_char == '\'') 855 found_quote |= RL_QF_SINGLE_QUOTE; 856 else if (quote_char == '"') 857 found_quote |= RL_QF_DOUBLE_QUOTE; 858 else 859 found_quote |= RL_QF_OTHER_QUOTE; 860 } 861 } 862 } 863 864 if (rl_point == end && quote_char == '\0') 865 { 866 /* We didn't find an unclosed quoted substring upon which to do 867 completion, so use the word break characters to find the 868 substring on which to complete. */ 869#if defined (HANDLE_MULTIBYTE) 870 while (rl_point = _rl_find_prev_mbchar (rl_line_buffer, rl_point, MB_FIND_ANY)) 871#else 872 while (--rl_point) 873#endif 874 { 875 scan = rl_line_buffer[rl_point]; 876
|
725 if (strchr (rl_completer_word_break_characters, scan) == 0)
| 877 if (strchr (brkchars, scan) == 0)
|
726 continue; 727 728 /* Call the application-specific function to tell us whether 729 this word break character is quoted and should be skipped. */ 730 if (rl_char_is_quoted_p && found_quote && 731 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point)) 732 continue; 733 734 /* Convoluted code, but it avoids an n^2 algorithm with calls 735 to char_is_quoted. */ 736 break; 737 } 738 } 739 740 /* If we are at an unquoted word break, then advance past it. */ 741 scan = rl_line_buffer[rl_point]; 742 743 /* If there is an application-specific function to say whether or not 744 a character is quoted and we found a quote character, let that 745 function decide whether or not a character is a word break, even 746 if it is found in rl_completer_word_break_characters. Don't bother 747 if we're at the end of the line, though. */ 748 if (scan) 749 { 750 if (rl_char_is_quoted_p) 751 isbrk = (found_quote == 0 || 752 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
| 878 continue; 879 880 /* Call the application-specific function to tell us whether 881 this word break character is quoted and should be skipped. */ 882 if (rl_char_is_quoted_p && found_quote && 883 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point)) 884 continue; 885 886 /* Convoluted code, but it avoids an n^2 algorithm with calls 887 to char_is_quoted. */ 888 break; 889 } 890 } 891 892 /* If we are at an unquoted word break, then advance past it. */ 893 scan = rl_line_buffer[rl_point]; 894 895 /* If there is an application-specific function to say whether or not 896 a character is quoted and we found a quote character, let that 897 function decide whether or not a character is a word break, even 898 if it is found in rl_completer_word_break_characters. Don't bother 899 if we're at the end of the line, though. */ 900 if (scan) 901 { 902 if (rl_char_is_quoted_p) 903 isbrk = (found_quote == 0 || 904 (*rl_char_is_quoted_p) (rl_line_buffer, rl_point) == 0) &&
|
753 strchr (rl_completer_word_break_characters, scan) != 0;
| 905 strchr (brkchars, scan) != 0;
|
754 else
| 906 else
|
755 isbrk = strchr (rl_completer_word_break_characters, scan) != 0;
| 907 isbrk = strchr (brkchars, scan) != 0;
|
756 757 if (isbrk) 758 { 759 /* If the character that caused the word break was a quoting 760 character, then remember it as the delimiter. */ 761 if (rl_basic_quote_characters && 762 strchr (rl_basic_quote_characters, scan) && 763 (end - rl_point) > 1) 764 delimiter = scan; 765 766 /* If the character isn't needed to determine something special 767 about what kind of completion to perform, then advance past it. */ 768 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0) 769 rl_point++; 770 } 771 } 772 773 if (fp) 774 *fp = found_quote; 775 if (dp) 776 *dp = delimiter; 777 778 return (quote_char); 779} 780 781static char ** 782gen_completion_matches (text, start, end, our_func, found_quote, quote_char) 783 char *text; 784 int start, end; 785 rl_compentry_func_t *our_func; 786 int found_quote, quote_char; 787{ 788 char **matches, *temp; 789
| 908 909 if (isbrk) 910 { 911 /* If the character that caused the word break was a quoting 912 character, then remember it as the delimiter. */ 913 if (rl_basic_quote_characters && 914 strchr (rl_basic_quote_characters, scan) && 915 (end - rl_point) > 1) 916 delimiter = scan; 917 918 /* If the character isn't needed to determine something special 919 about what kind of completion to perform, then advance past it. */ 920 if (rl_special_prefixes == 0 || strchr (rl_special_prefixes, scan) == 0) 921 rl_point++; 922 } 923 } 924 925 if (fp) 926 *fp = found_quote; 927 if (dp) 928 *dp = delimiter; 929 930 return (quote_char); 931} 932 933static char ** 934gen_completion_matches (text, start, end, our_func, found_quote, quote_char) 935 char *text; 936 int start, end; 937 rl_compentry_func_t *our_func; 938 int found_quote, quote_char; 939{ 940 char **matches, *temp; 941
|
| 942 rl_completion_found_quote = found_quote; 943 rl_completion_quote_character = quote_char; 944
|
790 /* If the user wants to TRY to complete, but then wants to give 791 up and use the default completion function, they set the 792 variable rl_attempted_completion_function. */ 793 if (rl_attempted_completion_function) 794 { 795 matches = (*rl_attempted_completion_function) (text, start, end); 796 797 if (matches || rl_attempted_completion_over) 798 { 799 rl_attempted_completion_over = 0; 800 return (matches); 801 } 802 } 803 804 /* Beware -- we're stripping the quotes here. Do this only if we know 805 we are doing filename completion and the application has defined a 806 filename dequoting function. */ 807 temp = (char *)NULL; 808 809 if (found_quote && our_func == rl_filename_completion_function && 810 rl_filename_dequoting_function) 811 { 812 /* delete single and double quotes */ 813 temp = (*rl_filename_dequoting_function) (text, quote_char); 814 text = temp; /* not freeing text is not a memory leak */ 815 } 816 817 matches = rl_completion_matches (text, our_func); 818 FREE (temp); 819 return matches; 820} 821 822/* Filter out duplicates in MATCHES. This frees up the strings in 823 MATCHES. */ 824static char ** 825remove_duplicate_matches (matches) 826 char **matches; 827{ 828 char *lowest_common; 829 int i, j, newlen; 830 char dead_slot; 831 char **temp_array; 832 833 /* Sort the items. */ 834 for (i = 0; matches[i]; i++) 835 ; 836 837 /* Sort the array without matches[0], since we need it to 838 stay in place no matter what. */ 839 if (i) 840 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); 841 842 /* Remember the lowest common denominator for it may be unique. */ 843 lowest_common = savestring (matches[0]); 844 845 for (i = newlen = 0; matches[i + 1]; i++) 846 { 847 if (strcmp (matches[i], matches[i + 1]) == 0) 848 { 849 free (matches[i]); 850 matches[i] = (char *)&dead_slot; 851 } 852 else 853 newlen++; 854 } 855 856 /* We have marked all the dead slots with (char *)&dead_slot. 857 Copy all the non-dead entries into a new array. */ 858 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *)); 859 for (i = j = 1; matches[i]; i++) 860 { 861 if (matches[i] != (char *)&dead_slot) 862 temp_array[j++] = matches[i]; 863 } 864 temp_array[j] = (char *)NULL; 865 866 if (matches[0] != (char *)&dead_slot) 867 free (matches[0]); 868 869 /* Place the lowest common denominator back in [0]. */ 870 temp_array[0] = lowest_common; 871 872 /* If there is one string left, and it is identical to the 873 lowest common denominator, then the LCD is the string to 874 insert. */ 875 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0) 876 { 877 free (temp_array[1]); 878 temp_array[1] = (char *)NULL; 879 } 880 return (temp_array); 881} 882 883/* Find the common prefix of the list of matches, and put it into 884 matches[0]. */ 885static int 886compute_lcd_of_matches (match_list, matches, text) 887 char **match_list; 888 int matches; 889 const char *text; 890{ 891 register int i, c1, c2, si; 892 int low; /* Count of max-matched characters. */
| 945 /* If the user wants to TRY to complete, but then wants to give 946 up and use the default completion function, they set the 947 variable rl_attempted_completion_function. */ 948 if (rl_attempted_completion_function) 949 { 950 matches = (*rl_attempted_completion_function) (text, start, end); 951 952 if (matches || rl_attempted_completion_over) 953 { 954 rl_attempted_completion_over = 0; 955 return (matches); 956 } 957 } 958 959 /* Beware -- we're stripping the quotes here. Do this only if we know 960 we are doing filename completion and the application has defined a 961 filename dequoting function. */ 962 temp = (char *)NULL; 963 964 if (found_quote && our_func == rl_filename_completion_function && 965 rl_filename_dequoting_function) 966 { 967 /* delete single and double quotes */ 968 temp = (*rl_filename_dequoting_function) (text, quote_char); 969 text = temp; /* not freeing text is not a memory leak */ 970 } 971 972 matches = rl_completion_matches (text, our_func); 973 FREE (temp); 974 return matches; 975} 976 977/* Filter out duplicates in MATCHES. This frees up the strings in 978 MATCHES. */ 979static char ** 980remove_duplicate_matches (matches) 981 char **matches; 982{ 983 char *lowest_common; 984 int i, j, newlen; 985 char dead_slot; 986 char **temp_array; 987 988 /* Sort the items. */ 989 for (i = 0; matches[i]; i++) 990 ; 991 992 /* Sort the array without matches[0], since we need it to 993 stay in place no matter what. */ 994 if (i) 995 qsort (matches+1, i-1, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); 996 997 /* Remember the lowest common denominator for it may be unique. */ 998 lowest_common = savestring (matches[0]); 999 1000 for (i = newlen = 0; matches[i + 1]; i++) 1001 { 1002 if (strcmp (matches[i], matches[i + 1]) == 0) 1003 { 1004 free (matches[i]); 1005 matches[i] = (char *)&dead_slot; 1006 } 1007 else 1008 newlen++; 1009 } 1010 1011 /* We have marked all the dead slots with (char *)&dead_slot. 1012 Copy all the non-dead entries into a new array. */ 1013 temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *)); 1014 for (i = j = 1; matches[i]; i++) 1015 { 1016 if (matches[i] != (char *)&dead_slot) 1017 temp_array[j++] = matches[i]; 1018 } 1019 temp_array[j] = (char *)NULL; 1020 1021 if (matches[0] != (char *)&dead_slot) 1022 free (matches[0]); 1023 1024 /* Place the lowest common denominator back in [0]. */ 1025 temp_array[0] = lowest_common; 1026 1027 /* If there is one string left, and it is identical to the 1028 lowest common denominator, then the LCD is the string to 1029 insert. */ 1030 if (j == 2 && strcmp (temp_array[0], temp_array[1]) == 0) 1031 { 1032 free (temp_array[1]); 1033 temp_array[1] = (char *)NULL; 1034 } 1035 return (temp_array); 1036} 1037 1038/* Find the common prefix of the list of matches, and put it into 1039 matches[0]. */ 1040static int 1041compute_lcd_of_matches (match_list, matches, text) 1042 char **match_list; 1043 int matches; 1044 const char *text; 1045{ 1046 register int i, c1, c2, si; 1047 int low; /* Count of max-matched characters. */
|
| 1048 char *dtext; /* dequoted TEXT, if needed */
|
893#if defined (HANDLE_MULTIBYTE) 894 int v; 895 mbstate_t ps1, ps2; 896 wchar_t wc1, wc2; 897#endif 898 899 /* If only one match, just use that. Otherwise, compare each 900 member of the list with the next, finding out where they 901 stop matching. */ 902 if (matches == 1) 903 { 904 match_list[0] = match_list[1]; 905 match_list[1] = (char *)NULL; 906 return 1; 907 } 908 909 for (i = 1, low = 100000; i < matches; i++) 910 { 911#if defined (HANDLE_MULTIBYTE) 912 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 913 { 914 memset (&ps1, 0, sizeof (mbstate_t)); 915 memset (&ps2, 0, sizeof (mbstate_t)); 916 } 917#endif 918 if (_rl_completion_case_fold) 919 { 920 for (si = 0; 921 (c1 = _rl_to_lower(match_list[i][si])) && 922 (c2 = _rl_to_lower(match_list[i + 1][si])); 923 si++) 924#if defined (HANDLE_MULTIBYTE) 925 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 926 { 927 v = mbrtowc (&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1); 928 mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2); 929 wc1 = towlower (wc1); 930 wc2 = towlower (wc2); 931 if (wc1 != wc2) 932 break; 933 else if (v > 1) 934 si += v - 1; 935 } 936 else 937#endif 938 if (c1 != c2) 939 break; 940 } 941 else 942 { 943 for (si = 0; 944 (c1 = match_list[i][si]) && 945 (c2 = match_list[i + 1][si]); 946 si++) 947#if defined (HANDLE_MULTIBYTE) 948 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 949 { 950 mbstate_t ps_back = ps1; 951 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2)) 952 break; 953 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1) 954 si += v - 1; 955 } 956 else 957#endif 958 if (c1 != c2) 959 break; 960 } 961 962 if (low > si) 963 low = si; 964 } 965 966 /* If there were multiple matches, but none matched up to even the 967 first character, and the user typed something, use that as the 968 value of matches[0]. */ 969 if (low == 0 && text && *text) 970 { 971 match_list[0] = (char *)xmalloc (strlen (text) + 1); 972 strcpy (match_list[0], text); 973 } 974 else 975 { 976 match_list[0] = (char *)xmalloc (low + 1); 977 978 /* XXX - this might need changes in the presence of multibyte chars */ 979 980 /* If we are ignoring case, try to preserve the case of the string 981 the user typed in the face of multiple matches differing in case. */ 982 if (_rl_completion_case_fold) 983 {
| 1049#if defined (HANDLE_MULTIBYTE) 1050 int v; 1051 mbstate_t ps1, ps2; 1052 wchar_t wc1, wc2; 1053#endif 1054 1055 /* If only one match, just use that. Otherwise, compare each 1056 member of the list with the next, finding out where they 1057 stop matching. */ 1058 if (matches == 1) 1059 { 1060 match_list[0] = match_list[1]; 1061 match_list[1] = (char *)NULL; 1062 return 1; 1063 } 1064 1065 for (i = 1, low = 100000; i < matches; i++) 1066 { 1067#if defined (HANDLE_MULTIBYTE) 1068 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 1069 { 1070 memset (&ps1, 0, sizeof (mbstate_t)); 1071 memset (&ps2, 0, sizeof (mbstate_t)); 1072 } 1073#endif 1074 if (_rl_completion_case_fold) 1075 { 1076 for (si = 0; 1077 (c1 = _rl_to_lower(match_list[i][si])) && 1078 (c2 = _rl_to_lower(match_list[i + 1][si])); 1079 si++) 1080#if defined (HANDLE_MULTIBYTE) 1081 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 1082 { 1083 v = mbrtowc (&wc1, match_list[i]+si, strlen (match_list[i]+si), &ps1); 1084 mbrtowc (&wc2, match_list[i+1]+si, strlen (match_list[i+1]+si), &ps2); 1085 wc1 = towlower (wc1); 1086 wc2 = towlower (wc2); 1087 if (wc1 != wc2) 1088 break; 1089 else if (v > 1) 1090 si += v - 1; 1091 } 1092 else 1093#endif 1094 if (c1 != c2) 1095 break; 1096 } 1097 else 1098 { 1099 for (si = 0; 1100 (c1 = match_list[i][si]) && 1101 (c2 = match_list[i + 1][si]); 1102 si++) 1103#if defined (HANDLE_MULTIBYTE) 1104 if (MB_CUR_MAX > 1 && rl_byte_oriented == 0) 1105 { 1106 mbstate_t ps_back = ps1; 1107 if (!_rl_compare_chars (match_list[i], si, &ps1, match_list[i+1], si, &ps2)) 1108 break; 1109 else if ((v = _rl_get_char_len (&match_list[i][si], &ps_back)) > 1) 1110 si += v - 1; 1111 } 1112 else 1113#endif 1114 if (c1 != c2) 1115 break; 1116 } 1117 1118 if (low > si) 1119 low = si; 1120 } 1121 1122 /* If there were multiple matches, but none matched up to even the 1123 first character, and the user typed something, use that as the 1124 value of matches[0]. */ 1125 if (low == 0 && text && *text) 1126 { 1127 match_list[0] = (char *)xmalloc (strlen (text) + 1); 1128 strcpy (match_list[0], text); 1129 } 1130 else 1131 { 1132 match_list[0] = (char *)xmalloc (low + 1); 1133 1134 /* XXX - this might need changes in the presence of multibyte chars */ 1135 1136 /* If we are ignoring case, try to preserve the case of the string 1137 the user typed in the face of multiple matches differing in case. */ 1138 if (_rl_completion_case_fold) 1139 {
|
| 1140 /* We're making an assumption here: 1141 IF we're completing filenames AND 1142 the application has defined a filename dequoting function AND 1143 we found a quote character AND 1144 the application has requested filename quoting 1145 THEN 1146 we assume that TEXT was dequoted before checking against 1147 the file system and needs to be dequoted here before we 1148 check against the list of matches 1149 FI */ 1150 dtext = (char *)NULL; 1151 if (rl_filename_completion_desired && 1152 rl_filename_dequoting_function && 1153 rl_completion_found_quote && 1154 rl_filename_quoting_desired) 1155 { 1156 dtext = (*rl_filename_dequoting_function) (text, rl_completion_quote_character); 1157 text = dtext; 1158 } 1159
|
984 /* sort the list to get consistent answers. */ 985 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare); 986 987 si = strlen (text); 988 if (si <= low) 989 { 990 for (i = 1; i <= matches; i++) 991 if (strncmp (match_list[i], text, si) == 0) 992 { 993 strncpy (match_list[0], match_list[i], low); 994 break; 995 } 996 /* no casematch, use first entry */ 997 if (i > matches) 998 strncpy (match_list[0], match_list[1], low); 999 } 1000 else 1001 /* otherwise, just use the text the user typed. */ 1002 strncpy (match_list[0], text, low);
| 1160 /* sort the list to get consistent answers. */ 1161 qsort (match_list+1, matches, sizeof(char *), (QSFUNC *)_rl_qsort_string_compare); 1162 1163 si = strlen (text); 1164 if (si <= low) 1165 { 1166 for (i = 1; i <= matches; i++) 1167 if (strncmp (match_list[i], text, si) == 0) 1168 { 1169 strncpy (match_list[0], match_list[i], low); 1170 break; 1171 } 1172 /* no casematch, use first entry */ 1173 if (i > matches) 1174 strncpy (match_list[0], match_list[1], low); 1175 } 1176 else 1177 /* otherwise, just use the text the user typed. */ 1178 strncpy (match_list[0], text, low);
|
| 1179 1180 FREE (dtext);
|
1003 } 1004 else 1005 strncpy (match_list[0], match_list[1], low); 1006 1007 match_list[0][low] = '\0'; 1008 } 1009 1010 return matches; 1011} 1012 1013static int 1014postprocess_matches (matchesp, matching_filenames) 1015 char ***matchesp; 1016 int matching_filenames; 1017{ 1018 char *t, **matches, **temp_matches; 1019 int nmatch, i; 1020 1021 matches = *matchesp; 1022 1023 if (matches == 0) 1024 return 0; 1025 1026 /* It seems to me that in all the cases we handle we would like 1027 to ignore duplicate possiblilities. Scan for the text to 1028 insert being identical to the other completions. */ 1029 if (rl_ignore_completion_duplicates) 1030 { 1031 temp_matches = remove_duplicate_matches (matches); 1032 free (matches); 1033 matches = temp_matches; 1034 } 1035 1036 /* If we are matching filenames, then here is our chance to 1037 do clever processing by re-examining the list. Call the 1038 ignore function with the array as a parameter. It can 1039 munge the array, deleting matches as it desires. */ 1040 if (rl_ignore_some_completions_function && matching_filenames) 1041 { 1042 for (nmatch = 1; matches[nmatch]; nmatch++) 1043 ; 1044 (void)(*rl_ignore_some_completions_function) (matches); 1045 if (matches == 0 || matches[0] == 0) 1046 { 1047 FREE (matches); 1048 *matchesp = (char **)0; 1049 return 0; 1050 } 1051 else 1052 { 1053 /* If we removed some matches, recompute the common prefix. */ 1054 for (i = 1; matches[i]; i++) 1055 ; 1056 if (i > 1 && i < nmatch) 1057 { 1058 t = matches[0]; 1059 compute_lcd_of_matches (matches, i - 1, t); 1060 FREE (t); 1061 } 1062 } 1063 } 1064 1065 *matchesp = matches; 1066 return (1); 1067} 1068 1069/* A convenience function for displaying a list of strings in 1070 columnar format on readline's output stream. MATCHES is the list 1071 of strings, in argv format, LEN is the number of strings in MATCHES, 1072 and MAX is the length of the longest string in MATCHES. */ 1073void 1074rl_display_match_list (matches, len, max) 1075 char **matches; 1076 int len, max; 1077{ 1078 int count, limit, printed_len, lines; 1079 int i, j, k, l; 1080 char *temp; 1081 1082 /* How many items of MAX length can we fit in the screen window? */ 1083 max += 2; 1084 limit = _rl_screenwidth / max; 1085 if (limit != 1 && (limit * max == _rl_screenwidth)) 1086 limit--; 1087 1088 /* Avoid a possible floating exception. If max > _rl_screenwidth, 1089 limit will be 0 and a divide-by-zero fault will result. */ 1090 if (limit == 0) 1091 limit = 1; 1092 1093 /* How many iterations of the printing loop? */ 1094 count = (len + (limit - 1)) / limit; 1095 1096 /* Watch out for special case. If LEN is less than LIMIT, then 1097 just do the inner printing loop. 1098 0 < len <= limit implies count = 1. */ 1099 1100 /* Sort the items if they are not already sorted. */ 1101 if (rl_ignore_completion_duplicates == 0) 1102 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); 1103 1104 rl_crlf (); 1105 1106 lines = 0; 1107 if (_rl_print_completions_horizontally == 0) 1108 { 1109 /* Print the sorted items, up-and-down alphabetically, like ls. */ 1110 for (i = 1; i <= count; i++) 1111 { 1112 for (j = 0, l = i; j < limit; j++) 1113 { 1114 if (l > len || matches[l] == 0) 1115 break; 1116 else 1117 { 1118 temp = printable_part (matches[l]); 1119 printed_len = print_filename (temp, matches[l]); 1120 1121 if (j + 1 < limit) 1122 for (k = 0; k < max - printed_len; k++) 1123 putc (' ', rl_outstream); 1124 } 1125 l += count; 1126 } 1127 rl_crlf (); 1128 lines++; 1129 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count) 1130 { 1131 lines = _rl_internal_pager (lines); 1132 if (lines < 0) 1133 return; 1134 } 1135 } 1136 } 1137 else 1138 { 1139 /* Print the sorted items, across alphabetically, like ls -x. */ 1140 for (i = 1; matches[i]; i++) 1141 { 1142 temp = printable_part (matches[i]); 1143 printed_len = print_filename (temp, matches[i]); 1144 /* Have we reached the end of this line? */ 1145 if (matches[i+1]) 1146 { 1147 if (i && (limit > 1) && (i % limit) == 0) 1148 { 1149 rl_crlf (); 1150 lines++; 1151 if (_rl_page_completions && lines >= _rl_screenheight - 1) 1152 { 1153 lines = _rl_internal_pager (lines); 1154 if (lines < 0) 1155 return; 1156 } 1157 } 1158 else 1159 for (k = 0; k < max - printed_len; k++) 1160 putc (' ', rl_outstream); 1161 } 1162 } 1163 rl_crlf (); 1164 } 1165} 1166 1167/* Display MATCHES, a list of matching filenames in argv format. This 1168 handles the simple case -- a single match -- first. If there is more 1169 than one match, we compute the number of strings in the list and the 1170 length of the longest string, which will be needed by the display 1171 function. If the application wants to handle displaying the list of 1172 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the 1173 address of a function, and we just call it. If we're handling the 1174 display ourselves, we just call rl_display_match_list. We also check 1175 that the list of matches doesn't exceed the user-settable threshold, 1176 and ask the user if he wants to see the list if there are more matches 1177 than RL_COMPLETION_QUERY_ITEMS. */ 1178static void 1179display_matches (matches) 1180 char **matches; 1181{ 1182 int len, max, i; 1183 char *temp; 1184 1185 /* Move to the last visible line of a possibly-multiple-line command. */ 1186 _rl_move_vert (_rl_vis_botlin); 1187 1188 /* Handle simple case first. What if there is only one answer? */ 1189 if (matches[1] == 0) 1190 { 1191 temp = printable_part (matches[0]); 1192 rl_crlf (); 1193 print_filename (temp, matches[0]); 1194 rl_crlf (); 1195 1196 rl_forced_update_display (); 1197 rl_display_fixed = 1; 1198 1199 return; 1200 } 1201 1202 /* There is more than one answer. Find out how many there are, 1203 and find the maximum printed length of a single entry. */ 1204 for (max = 0, i = 1; matches[i]; i++) 1205 { 1206 temp = printable_part (matches[i]);
| 1181 } 1182 else 1183 strncpy (match_list[0], match_list[1], low); 1184 1185 match_list[0][low] = '\0'; 1186 } 1187 1188 return matches; 1189} 1190 1191static int 1192postprocess_matches (matchesp, matching_filenames) 1193 char ***matchesp; 1194 int matching_filenames; 1195{ 1196 char *t, **matches, **temp_matches; 1197 int nmatch, i; 1198 1199 matches = *matchesp; 1200 1201 if (matches == 0) 1202 return 0; 1203 1204 /* It seems to me that in all the cases we handle we would like 1205 to ignore duplicate possiblilities. Scan for the text to 1206 insert being identical to the other completions. */ 1207 if (rl_ignore_completion_duplicates) 1208 { 1209 temp_matches = remove_duplicate_matches (matches); 1210 free (matches); 1211 matches = temp_matches; 1212 } 1213 1214 /* If we are matching filenames, then here is our chance to 1215 do clever processing by re-examining the list. Call the 1216 ignore function with the array as a parameter. It can 1217 munge the array, deleting matches as it desires. */ 1218 if (rl_ignore_some_completions_function && matching_filenames) 1219 { 1220 for (nmatch = 1; matches[nmatch]; nmatch++) 1221 ; 1222 (void)(*rl_ignore_some_completions_function) (matches); 1223 if (matches == 0 || matches[0] == 0) 1224 { 1225 FREE (matches); 1226 *matchesp = (char **)0; 1227 return 0; 1228 } 1229 else 1230 { 1231 /* If we removed some matches, recompute the common prefix. */ 1232 for (i = 1; matches[i]; i++) 1233 ; 1234 if (i > 1 && i < nmatch) 1235 { 1236 t = matches[0]; 1237 compute_lcd_of_matches (matches, i - 1, t); 1238 FREE (t); 1239 } 1240 } 1241 } 1242 1243 *matchesp = matches; 1244 return (1); 1245} 1246 1247/* A convenience function for displaying a list of strings in 1248 columnar format on readline's output stream. MATCHES is the list 1249 of strings, in argv format, LEN is the number of strings in MATCHES, 1250 and MAX is the length of the longest string in MATCHES. */ 1251void 1252rl_display_match_list (matches, len, max) 1253 char **matches; 1254 int len, max; 1255{ 1256 int count, limit, printed_len, lines; 1257 int i, j, k, l; 1258 char *temp; 1259 1260 /* How many items of MAX length can we fit in the screen window? */ 1261 max += 2; 1262 limit = _rl_screenwidth / max; 1263 if (limit != 1 && (limit * max == _rl_screenwidth)) 1264 limit--; 1265 1266 /* Avoid a possible floating exception. If max > _rl_screenwidth, 1267 limit will be 0 and a divide-by-zero fault will result. */ 1268 if (limit == 0) 1269 limit = 1; 1270 1271 /* How many iterations of the printing loop? */ 1272 count = (len + (limit - 1)) / limit; 1273 1274 /* Watch out for special case. If LEN is less than LIMIT, then 1275 just do the inner printing loop. 1276 0 < len <= limit implies count = 1. */ 1277 1278 /* Sort the items if they are not already sorted. */ 1279 if (rl_ignore_completion_duplicates == 0) 1280 qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare); 1281 1282 rl_crlf (); 1283 1284 lines = 0; 1285 if (_rl_print_completions_horizontally == 0) 1286 { 1287 /* Print the sorted items, up-and-down alphabetically, like ls. */ 1288 for (i = 1; i <= count; i++) 1289 { 1290 for (j = 0, l = i; j < limit; j++) 1291 { 1292 if (l > len || matches[l] == 0) 1293 break; 1294 else 1295 { 1296 temp = printable_part (matches[l]); 1297 printed_len = print_filename (temp, matches[l]); 1298 1299 if (j + 1 < limit) 1300 for (k = 0; k < max - printed_len; k++) 1301 putc (' ', rl_outstream); 1302 } 1303 l += count; 1304 } 1305 rl_crlf (); 1306 lines++; 1307 if (_rl_page_completions && lines >= (_rl_screenheight - 1) && i < count) 1308 { 1309 lines = _rl_internal_pager (lines); 1310 if (lines < 0) 1311 return; 1312 } 1313 } 1314 } 1315 else 1316 { 1317 /* Print the sorted items, across alphabetically, like ls -x. */ 1318 for (i = 1; matches[i]; i++) 1319 { 1320 temp = printable_part (matches[i]); 1321 printed_len = print_filename (temp, matches[i]); 1322 /* Have we reached the end of this line? */ 1323 if (matches[i+1]) 1324 { 1325 if (i && (limit > 1) && (i % limit) == 0) 1326 { 1327 rl_crlf (); 1328 lines++; 1329 if (_rl_page_completions && lines >= _rl_screenheight - 1) 1330 { 1331 lines = _rl_internal_pager (lines); 1332 if (lines < 0) 1333 return; 1334 } 1335 } 1336 else 1337 for (k = 0; k < max - printed_len; k++) 1338 putc (' ', rl_outstream); 1339 } 1340 } 1341 rl_crlf (); 1342 } 1343} 1344 1345/* Display MATCHES, a list of matching filenames in argv format. This 1346 handles the simple case -- a single match -- first. If there is more 1347 than one match, we compute the number of strings in the list and the 1348 length of the longest string, which will be needed by the display 1349 function. If the application wants to handle displaying the list of 1350 matches itself, it sets RL_COMPLETION_DISPLAY_MATCHES_HOOK to the 1351 address of a function, and we just call it. If we're handling the 1352 display ourselves, we just call rl_display_match_list. We also check 1353 that the list of matches doesn't exceed the user-settable threshold, 1354 and ask the user if he wants to see the list if there are more matches 1355 than RL_COMPLETION_QUERY_ITEMS. */ 1356static void 1357display_matches (matches) 1358 char **matches; 1359{ 1360 int len, max, i; 1361 char *temp; 1362 1363 /* Move to the last visible line of a possibly-multiple-line command. */ 1364 _rl_move_vert (_rl_vis_botlin); 1365 1366 /* Handle simple case first. What if there is only one answer? */ 1367 if (matches[1] == 0) 1368 { 1369 temp = printable_part (matches[0]); 1370 rl_crlf (); 1371 print_filename (temp, matches[0]); 1372 rl_crlf (); 1373 1374 rl_forced_update_display (); 1375 rl_display_fixed = 1; 1376 1377 return; 1378 } 1379 1380 /* There is more than one answer. Find out how many there are, 1381 and find the maximum printed length of a single entry. */ 1382 for (max = 0, i = 1; matches[i]; i++) 1383 { 1384 temp = printable_part (matches[i]);
|
1207 len = strlen (temp);
| 1385 len = fnwidth (temp);
|
1208 1209 if (len > max) 1210 max = len; 1211 } 1212 1213 len = i - 1; 1214 1215 /* If the caller has defined a display hook, then call that now. */ 1216 if (rl_completion_display_matches_hook) 1217 { 1218 (*rl_completion_display_matches_hook) (matches, len, max); 1219 return; 1220 } 1221 1222 /* If there are many items, then ask the user if she really wants to 1223 see them all. */ 1224 if (len >= rl_completion_query_items) 1225 { 1226 rl_crlf (); 1227 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); 1228 fflush (rl_outstream); 1229 if (get_y_or_n (0) == 0) 1230 { 1231 rl_crlf (); 1232 1233 rl_forced_update_display (); 1234 rl_display_fixed = 1; 1235 1236 return; 1237 } 1238 } 1239 1240 rl_display_match_list (matches, len, max); 1241 1242 rl_forced_update_display (); 1243 rl_display_fixed = 1; 1244} 1245 1246static char * 1247make_quoted_replacement (match, mtype, qc) 1248 char *match; 1249 int mtype; 1250 char *qc; /* Pointer to quoting character, if any */ 1251{ 1252 int should_quote, do_replace; 1253 char *replacement; 1254 1255 /* If we are doing completion on quoted substrings, and any matches 1256 contain any of the completer_word_break_characters, then auto- 1257 matically prepend the substring with a quote character (just pick 1258 the first one from the list of such) if it does not already begin 1259 with a quote string. FIXME: Need to remove any such automatically 1260 inserted quote character when it no longer is necessary, such as 1261 if we change the string we are completing on and the new set of 1262 matches don't require a quoted substring. */ 1263 replacement = match; 1264 1265 should_quote = match && rl_completer_quote_characters && 1266 rl_filename_completion_desired && 1267 rl_filename_quoting_desired; 1268 1269 if (should_quote) 1270 should_quote = should_quote && (!qc || !*qc || 1271 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc))); 1272 1273 if (should_quote) 1274 { 1275 /* If there is a single match, see if we need to quote it. 1276 This also checks whether the common prefix of several 1277 matches needs to be quoted. */ 1278 should_quote = rl_filename_quote_characters 1279 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0) 1280 : 0; 1281 1282 do_replace = should_quote ? mtype : NO_MATCH; 1283 /* Quote the replacement, since we found an embedded 1284 word break character in a potential match. */ 1285 if (do_replace != NO_MATCH && rl_filename_quoting_function) 1286 replacement = (*rl_filename_quoting_function) (match, do_replace, qc); 1287 } 1288 return (replacement); 1289} 1290 1291static void 1292insert_match (match, start, mtype, qc) 1293 char *match; 1294 int start, mtype; 1295 char *qc; 1296{ 1297 char *replacement; 1298 char oqc; 1299 1300 oqc = qc ? *qc : '\0'; 1301 replacement = make_quoted_replacement (match, mtype, qc); 1302 1303 /* Now insert the match. */ 1304 if (replacement) 1305 { 1306 /* Don't double an opening quote character. */ 1307 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc && 1308 replacement[0] == *qc) 1309 start--; 1310 /* If make_quoted_replacement changed the quoting character, remove 1311 the opening quote and insert the (fully-quoted) replacement. */ 1312 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc && 1313 replacement[0] != oqc) 1314 start--; 1315 _rl_replace_text (replacement, start, rl_point - 1); 1316 if (replacement != match) 1317 free (replacement); 1318 } 1319} 1320 1321/* Append any necessary closing quote and a separator character to the 1322 just-inserted match. If the user has specified that directories 1323 should be marked by a trailing `/', append one of those instead. The 1324 default trailing character is a space. Returns the number of characters 1325 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS 1326 has them) and don't add a suffix for a symlink to a directory. A 1327 nontrivial match is one that actually adds to the word being completed. 1328 The variable rl_completion_mark_symlink_dirs controls this behavior 1329 (it's initially set to the what the user has chosen, indicated by the 1330 value of _rl_complete_mark_symlink_dirs, but may be modified by an 1331 application's completion function). */ 1332static int 1333append_to_match (text, delimiter, quote_char, nontrivial_match) 1334 char *text; 1335 int delimiter, quote_char, nontrivial_match; 1336{ 1337 char temp_string[4], *filename; 1338 int temp_string_index, s; 1339 struct stat finfo; 1340 1341 temp_string_index = 0;
| 1386 1387 if (len > max) 1388 max = len; 1389 } 1390 1391 len = i - 1; 1392 1393 /* If the caller has defined a display hook, then call that now. */ 1394 if (rl_completion_display_matches_hook) 1395 { 1396 (*rl_completion_display_matches_hook) (matches, len, max); 1397 return; 1398 } 1399 1400 /* If there are many items, then ask the user if she really wants to 1401 see them all. */ 1402 if (len >= rl_completion_query_items) 1403 { 1404 rl_crlf (); 1405 fprintf (rl_outstream, "Display all %d possibilities? (y or n)", len); 1406 fflush (rl_outstream); 1407 if (get_y_or_n (0) == 0) 1408 { 1409 rl_crlf (); 1410 1411 rl_forced_update_display (); 1412 rl_display_fixed = 1; 1413 1414 return; 1415 } 1416 } 1417 1418 rl_display_match_list (matches, len, max); 1419 1420 rl_forced_update_display (); 1421 rl_display_fixed = 1; 1422} 1423 1424static char * 1425make_quoted_replacement (match, mtype, qc) 1426 char *match; 1427 int mtype; 1428 char *qc; /* Pointer to quoting character, if any */ 1429{ 1430 int should_quote, do_replace; 1431 char *replacement; 1432 1433 /* If we are doing completion on quoted substrings, and any matches 1434 contain any of the completer_word_break_characters, then auto- 1435 matically prepend the substring with a quote character (just pick 1436 the first one from the list of such) if it does not already begin 1437 with a quote string. FIXME: Need to remove any such automatically 1438 inserted quote character when it no longer is necessary, such as 1439 if we change the string we are completing on and the new set of 1440 matches don't require a quoted substring. */ 1441 replacement = match; 1442 1443 should_quote = match && rl_completer_quote_characters && 1444 rl_filename_completion_desired && 1445 rl_filename_quoting_desired; 1446 1447 if (should_quote) 1448 should_quote = should_quote && (!qc || !*qc || 1449 (rl_completer_quote_characters && strchr (rl_completer_quote_characters, *qc))); 1450 1451 if (should_quote) 1452 { 1453 /* If there is a single match, see if we need to quote it. 1454 This also checks whether the common prefix of several 1455 matches needs to be quoted. */ 1456 should_quote = rl_filename_quote_characters 1457 ? (_rl_strpbrk (match, rl_filename_quote_characters) != 0) 1458 : 0; 1459 1460 do_replace = should_quote ? mtype : NO_MATCH; 1461 /* Quote the replacement, since we found an embedded 1462 word break character in a potential match. */ 1463 if (do_replace != NO_MATCH && rl_filename_quoting_function) 1464 replacement = (*rl_filename_quoting_function) (match, do_replace, qc); 1465 } 1466 return (replacement); 1467} 1468 1469static void 1470insert_match (match, start, mtype, qc) 1471 char *match; 1472 int start, mtype; 1473 char *qc; 1474{ 1475 char *replacement; 1476 char oqc; 1477 1478 oqc = qc ? *qc : '\0'; 1479 replacement = make_quoted_replacement (match, mtype, qc); 1480 1481 /* Now insert the match. */ 1482 if (replacement) 1483 { 1484 /* Don't double an opening quote character. */ 1485 if (qc && *qc && start && rl_line_buffer[start - 1] == *qc && 1486 replacement[0] == *qc) 1487 start--; 1488 /* If make_quoted_replacement changed the quoting character, remove 1489 the opening quote and insert the (fully-quoted) replacement. */ 1490 else if (qc && (*qc != oqc) && start && rl_line_buffer[start - 1] == oqc && 1491 replacement[0] != oqc) 1492 start--; 1493 _rl_replace_text (replacement, start, rl_point - 1); 1494 if (replacement != match) 1495 free (replacement); 1496 } 1497} 1498 1499/* Append any necessary closing quote and a separator character to the 1500 just-inserted match. If the user has specified that directories 1501 should be marked by a trailing `/', append one of those instead. The 1502 default trailing character is a space. Returns the number of characters 1503 appended. If NONTRIVIAL_MATCH is set, we test for a symlink (if the OS 1504 has them) and don't add a suffix for a symlink to a directory. A 1505 nontrivial match is one that actually adds to the word being completed. 1506 The variable rl_completion_mark_symlink_dirs controls this behavior 1507 (it's initially set to the what the user has chosen, indicated by the 1508 value of _rl_complete_mark_symlink_dirs, but may be modified by an 1509 application's completion function). */ 1510static int 1511append_to_match (text, delimiter, quote_char, nontrivial_match) 1512 char *text; 1513 int delimiter, quote_char, nontrivial_match; 1514{ 1515 char temp_string[4], *filename; 1516 int temp_string_index, s; 1517 struct stat finfo; 1518 1519 temp_string_index = 0;
|
1342 if (quote_char && rl_point && rl_line_buffer[rl_point - 1] != quote_char)
| 1520 if (quote_char && rl_point && rl_completion_suppress_quote == 0 && 1521 rl_line_buffer[rl_point - 1] != quote_char)
|
1343 temp_string[temp_string_index++] = quote_char; 1344 1345 if (delimiter) 1346 temp_string[temp_string_index++] = delimiter; 1347 else if (rl_completion_suppress_append == 0 && rl_completion_append_character) 1348 temp_string[temp_string_index++] = rl_completion_append_character; 1349 1350 temp_string[temp_string_index++] = '\0'; 1351 1352 if (rl_filename_completion_desired) 1353 { 1354 filename = tilde_expand (text); 1355 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0) 1356 ? LSTAT (filename, &finfo) 1357 : stat (filename, &finfo); 1358 if (s == 0 && S_ISDIR (finfo.st_mode)) 1359 { 1360 if (_rl_complete_mark_directories) 1361 { 1362 /* This is clumsy. Avoid putting in a double slash if point 1363 is at the end of the line and the previous character is a 1364 slash. */ 1365 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/') 1366 ; 1367 else if (rl_line_buffer[rl_point] != '/') 1368 rl_insert_text ("/"); 1369 } 1370 } 1371#ifdef S_ISLNK 1372 /* Don't add anything if the filename is a symlink and resolves to a 1373 directory. */ 1374 else if (s == 0 && S_ISLNK (finfo.st_mode) && 1375 stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)) 1376 ; 1377#endif 1378 else 1379 { 1380 if (rl_point == rl_end && temp_string_index) 1381 rl_insert_text (temp_string); 1382 } 1383 free (filename); 1384 } 1385 else 1386 { 1387 if (rl_point == rl_end && temp_string_index) 1388 rl_insert_text (temp_string); 1389 } 1390 1391 return (temp_string_index); 1392} 1393 1394static void 1395insert_all_matches (matches, point, qc) 1396 char **matches; 1397 int point; 1398 char *qc; 1399{ 1400 int i; 1401 char *rp; 1402 1403 rl_begin_undo_group (); 1404 /* remove any opening quote character; make_quoted_replacement will add 1405 it back. */ 1406 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc) 1407 point--; 1408 rl_delete_text (point, rl_point); 1409 rl_point = point; 1410 1411 if (matches[1]) 1412 { 1413 for (i = 1; matches[i]; i++) 1414 { 1415 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc); 1416 rl_insert_text (rp); 1417 rl_insert_text (" "); 1418 if (rp != matches[i]) 1419 free (rp); 1420 } 1421 } 1422 else 1423 { 1424 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc); 1425 rl_insert_text (rp); 1426 rl_insert_text (" "); 1427 if (rp != matches[0]) 1428 free (rp); 1429 } 1430 rl_end_undo_group (); 1431} 1432 1433void 1434_rl_free_match_list (matches) 1435 char **matches; 1436{ 1437 register int i; 1438 1439 if (matches == 0) 1440 return; 1441 1442 for (i = 0; matches[i]; i++) 1443 free (matches[i]); 1444 free (matches); 1445} 1446 1447/* Complete the word at or before point. 1448 WHAT_TO_DO says what to do with the completion. 1449 `?' means list the possible completions. 1450 TAB means do standard completion. 1451 `*' means insert all of the possible completions. 1452 `!' means to do standard completion, and list all possible completions if
| 1522 temp_string[temp_string_index++] = quote_char; 1523 1524 if (delimiter) 1525 temp_string[temp_string_index++] = delimiter; 1526 else if (rl_completion_suppress_append == 0 && rl_completion_append_character) 1527 temp_string[temp_string_index++] = rl_completion_append_character; 1528 1529 temp_string[temp_string_index++] = '\0'; 1530 1531 if (rl_filename_completion_desired) 1532 { 1533 filename = tilde_expand (text); 1534 s = (nontrivial_match && rl_completion_mark_symlink_dirs == 0) 1535 ? LSTAT (filename, &finfo) 1536 : stat (filename, &finfo); 1537 if (s == 0 && S_ISDIR (finfo.st_mode)) 1538 { 1539 if (_rl_complete_mark_directories) 1540 { 1541 /* This is clumsy. Avoid putting in a double slash if point 1542 is at the end of the line and the previous character is a 1543 slash. */ 1544 if (rl_point && rl_line_buffer[rl_point] == '\0' && rl_line_buffer[rl_point - 1] == '/') 1545 ; 1546 else if (rl_line_buffer[rl_point] != '/') 1547 rl_insert_text ("/"); 1548 } 1549 } 1550#ifdef S_ISLNK 1551 /* Don't add anything if the filename is a symlink and resolves to a 1552 directory. */ 1553 else if (s == 0 && S_ISLNK (finfo.st_mode) && 1554 stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode)) 1555 ; 1556#endif 1557 else 1558 { 1559 if (rl_point == rl_end && temp_string_index) 1560 rl_insert_text (temp_string); 1561 } 1562 free (filename); 1563 } 1564 else 1565 { 1566 if (rl_point == rl_end && temp_string_index) 1567 rl_insert_text (temp_string); 1568 } 1569 1570 return (temp_string_index); 1571} 1572 1573static void 1574insert_all_matches (matches, point, qc) 1575 char **matches; 1576 int point; 1577 char *qc; 1578{ 1579 int i; 1580 char *rp; 1581 1582 rl_begin_undo_group (); 1583 /* remove any opening quote character; make_quoted_replacement will add 1584 it back. */ 1585 if (qc && *qc && point && rl_line_buffer[point - 1] == *qc) 1586 point--; 1587 rl_delete_text (point, rl_point); 1588 rl_point = point; 1589 1590 if (matches[1]) 1591 { 1592 for (i = 1; matches[i]; i++) 1593 { 1594 rp = make_quoted_replacement (matches[i], SINGLE_MATCH, qc); 1595 rl_insert_text (rp); 1596 rl_insert_text (" "); 1597 if (rp != matches[i]) 1598 free (rp); 1599 } 1600 } 1601 else 1602 { 1603 rp = make_quoted_replacement (matches[0], SINGLE_MATCH, qc); 1604 rl_insert_text (rp); 1605 rl_insert_text (" "); 1606 if (rp != matches[0]) 1607 free (rp); 1608 } 1609 rl_end_undo_group (); 1610} 1611 1612void 1613_rl_free_match_list (matches) 1614 char **matches; 1615{ 1616 register int i; 1617 1618 if (matches == 0) 1619 return; 1620 1621 for (i = 0; matches[i]; i++) 1622 free (matches[i]); 1623 free (matches); 1624} 1625 1626/* Complete the word at or before point. 1627 WHAT_TO_DO says what to do with the completion. 1628 `?' means list the possible completions. 1629 TAB means do standard completion. 1630 `*' means insert all of the possible completions. 1631 `!' means to do standard completion, and list all possible completions if
|
1453 there is more than one. */
| 1632 there is more than one. 1633 `@' means to do standard completion, and list all possible completions if 1634 there is more than one and partial completion is not possible. */
|
1454int 1455rl_complete_internal (what_to_do) 1456 int what_to_do; 1457{ 1458 char **matches; 1459 rl_compentry_func_t *our_func; 1460 int start, end, delimiter, found_quote, i, nontrivial_lcd; 1461 char *text, *saved_line_buffer; 1462 char quote_char; 1463 1464 RL_SETSTATE(RL_STATE_COMPLETING); 1465 1466 set_completion_defaults (what_to_do); 1467 1468 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; 1469 our_func = rl_completion_entry_function 1470 ? rl_completion_entry_function 1471 : rl_filename_completion_function;
| 1635int 1636rl_complete_internal (what_to_do) 1637 int what_to_do; 1638{ 1639 char **matches; 1640 rl_compentry_func_t *our_func; 1641 int start, end, delimiter, found_quote, i, nontrivial_lcd; 1642 char *text, *saved_line_buffer; 1643 char quote_char; 1644 1645 RL_SETSTATE(RL_STATE_COMPLETING); 1646 1647 set_completion_defaults (what_to_do); 1648 1649 saved_line_buffer = rl_line_buffer ? savestring (rl_line_buffer) : (char *)NULL; 1650 our_func = rl_completion_entry_function 1651 ? rl_completion_entry_function 1652 : rl_filename_completion_function;
|
1472
| |
1473 /* We now look backwards for the start of a filename/variable word. */ 1474 end = rl_point; 1475 found_quote = delimiter = 0; 1476 quote_char = '\0'; 1477 1478 if (rl_point) 1479 /* This (possibly) changes rl_point. If it returns a non-zero char, 1480 we know we have an open quote. */ 1481 quote_char = _rl_find_completion_word (&found_quote, &delimiter); 1482 1483 start = rl_point; 1484 rl_point = end; 1485 1486 text = rl_copy_text (start, end); 1487 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char); 1488 /* nontrivial_lcd is set if the common prefix adds something to the word 1489 being completed. */ 1490 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0; 1491 free (text); 1492 1493 if (matches == 0) 1494 { 1495 rl_ding (); 1496 FREE (saved_line_buffer); 1497 completion_changed_buffer = 0; 1498 RL_UNSETSTATE(RL_STATE_COMPLETING); 1499 return (0); 1500 } 1501 1502 /* If we are matching filenames, the attempted completion function will 1503 have set rl_filename_completion_desired to a non-zero value. The basic 1504 rl_filename_completion_function does this. */ 1505 i = rl_filename_completion_desired; 1506 1507 if (postprocess_matches (&matches, i) == 0) 1508 { 1509 rl_ding (); 1510 FREE (saved_line_buffer); 1511 completion_changed_buffer = 0; 1512 RL_UNSETSTATE(RL_STATE_COMPLETING); 1513 return (0); 1514 } 1515 1516 switch (what_to_do) 1517 { 1518 case TAB: 1519 case '!':
| 1653 /* We now look backwards for the start of a filename/variable word. */ 1654 end = rl_point; 1655 found_quote = delimiter = 0; 1656 quote_char = '\0'; 1657 1658 if (rl_point) 1659 /* This (possibly) changes rl_point. If it returns a non-zero char, 1660 we know we have an open quote. */ 1661 quote_char = _rl_find_completion_word (&found_quote, &delimiter); 1662 1663 start = rl_point; 1664 rl_point = end; 1665 1666 text = rl_copy_text (start, end); 1667 matches = gen_completion_matches (text, start, end, our_func, found_quote, quote_char); 1668 /* nontrivial_lcd is set if the common prefix adds something to the word 1669 being completed. */ 1670 nontrivial_lcd = matches && strcmp (text, matches[0]) != 0; 1671 free (text); 1672 1673 if (matches == 0) 1674 { 1675 rl_ding (); 1676 FREE (saved_line_buffer); 1677 completion_changed_buffer = 0; 1678 RL_UNSETSTATE(RL_STATE_COMPLETING); 1679 return (0); 1680 } 1681 1682 /* If we are matching filenames, the attempted completion function will 1683 have set rl_filename_completion_desired to a non-zero value. The basic 1684 rl_filename_completion_function does this. */ 1685 i = rl_filename_completion_desired; 1686 1687 if (postprocess_matches (&matches, i) == 0) 1688 { 1689 rl_ding (); 1690 FREE (saved_line_buffer); 1691 completion_changed_buffer = 0; 1692 RL_UNSETSTATE(RL_STATE_COMPLETING); 1693 return (0); 1694 } 1695 1696 switch (what_to_do) 1697 { 1698 case TAB: 1699 case '!':
|
| 1700 case '@':
|
1520 /* Insert the first match with proper quoting. */ 1521 if (*matches[0]) 1522 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); 1523 1524 /* If there are more matches, ring the bell to indicate. 1525 If we are in vi mode, Posix.2 says to not ring the bell. 1526 If the `show-all-if-ambiguous' variable is set, display 1527 all the matches immediately. Otherwise, if this was the 1528 only match, and we are hacking files, check the file to 1529 see if it was a directory. If so, and the `mark-directories' 1530 variable is set, add a '/' to the name. If not, and we 1531 are at the end of the line, then add a space. */ 1532 if (matches[1]) 1533 { 1534 if (what_to_do == '!') 1535 { 1536 display_matches (matches); 1537 break; 1538 }
| 1701 /* Insert the first match with proper quoting. */ 1702 if (*matches[0]) 1703 insert_match (matches[0], start, matches[1] ? MULT_MATCH : SINGLE_MATCH, "e_char); 1704 1705 /* If there are more matches, ring the bell to indicate. 1706 If we are in vi mode, Posix.2 says to not ring the bell. 1707 If the `show-all-if-ambiguous' variable is set, display 1708 all the matches immediately. Otherwise, if this was the 1709 only match, and we are hacking files, check the file to 1710 see if it was a directory. If so, and the `mark-directories' 1711 variable is set, add a '/' to the name. If not, and we 1712 are at the end of the line, then add a space. */ 1713 if (matches[1]) 1714 { 1715 if (what_to_do == '!') 1716 { 1717 display_matches (matches); 1718 break; 1719 }
|
| 1720 else if (what_to_do == '@') 1721 { 1722 if (nontrivial_lcd == 0) 1723 display_matches (matches); 1724 break; 1725 }
|
1539 else if (rl_editing_mode != vi_mode) 1540 rl_ding (); /* There are other matches remaining. */ 1541 } 1542 else 1543 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd); 1544 1545 break; 1546 1547 case '*': 1548 insert_all_matches (matches, start, "e_char); 1549 break; 1550 1551 case '?': 1552 display_matches (matches); 1553 break; 1554 1555 default: 1556 fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do); 1557 rl_ding (); 1558 FREE (saved_line_buffer); 1559 RL_UNSETSTATE(RL_STATE_COMPLETING); 1560 return 1; 1561 } 1562 1563 _rl_free_match_list (matches); 1564 1565 /* Check to see if the line has changed through all of this manipulation. */ 1566 if (saved_line_buffer) 1567 { 1568 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; 1569 free (saved_line_buffer); 1570 } 1571 1572 RL_UNSETSTATE(RL_STATE_COMPLETING); 1573 return 0; 1574} 1575 1576/***************************************************************/ 1577/* */ 1578/* Application-callable completion match generator functions */ 1579/* */ 1580/***************************************************************/ 1581 1582/* Return an array of (char *) which is a list of completions for TEXT. 1583 If there are no completions, return a NULL pointer. 1584 The first entry in the returned array is the substitution for TEXT. 1585 The remaining entries are the possible completions. 1586 The array is terminated with a NULL pointer. 1587 1588 ENTRY_FUNCTION is a function of two args, and returns a (char *). 1589 The first argument is TEXT. 1590 The second is a state argument; it should be zero on the first call, and 1591 non-zero on subsequent calls. It returns a NULL pointer to the caller 1592 when there are no more matches. 1593 */ 1594char ** 1595rl_completion_matches (text, entry_function) 1596 const char *text; 1597 rl_compentry_func_t *entry_function; 1598{ 1599 /* Number of slots in match_list. */ 1600 int match_list_size; 1601 1602 /* The list of matches. */ 1603 char **match_list; 1604 1605 /* Number of matches actually found. */ 1606 int matches; 1607 1608 /* Temporary string binder. */ 1609 char *string; 1610 1611 matches = 0; 1612 match_list_size = 10; 1613 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *)); 1614 match_list[1] = (char *)NULL; 1615 1616 while (string = (*entry_function) (text, matches)) 1617 { 1618 if (matches + 1 == match_list_size) 1619 match_list = (char **)xrealloc 1620 (match_list, ((match_list_size += 10) + 1) * sizeof (char *)); 1621 1622 match_list[++matches] = string; 1623 match_list[matches + 1] = (char *)NULL; 1624 } 1625 1626 /* If there were any matches, then look through them finding out the 1627 lowest common denominator. That then becomes match_list[0]. */ 1628 if (matches) 1629 compute_lcd_of_matches (match_list, matches, text); 1630 else /* There were no matches. */ 1631 { 1632 free (match_list); 1633 match_list = (char **)NULL; 1634 } 1635 return (match_list); 1636} 1637 1638/* A completion function for usernames. 1639 TEXT contains a partial username preceded by a random 1640 character (usually `~'). */ 1641char * 1642rl_username_completion_function (text, state) 1643 const char *text; 1644 int state; 1645{ 1646#if defined (__WIN32__) || defined (__OPENNT) 1647 return (char *)NULL; 1648#else /* !__WIN32__ && !__OPENNT) */ 1649 static char *username = (char *)NULL; 1650 static struct passwd *entry; 1651 static int namelen, first_char, first_char_loc; 1652 char *value; 1653 1654 if (state == 0) 1655 { 1656 FREE (username); 1657 1658 first_char = *text; 1659 first_char_loc = first_char == '~'; 1660 1661 username = savestring (&text[first_char_loc]); 1662 namelen = strlen (username); 1663 setpwent (); 1664 } 1665 1666 while (entry = getpwent ()) 1667 { 1668 /* Null usernames should result in all users as possible completions. */ 1669 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen))) 1670 break; 1671 } 1672 1673 if (entry == 0) 1674 { 1675 endpwent (); 1676 return ((char *)NULL); 1677 } 1678 else 1679 { 1680 value = (char *)xmalloc (2 + strlen (entry->pw_name)); 1681 1682 *value = *text; 1683 1684 strcpy (value + first_char_loc, entry->pw_name); 1685 1686 if (first_char == '~') 1687 rl_filename_completion_desired = 1; 1688 1689 return (value); 1690 } 1691#endif /* !__WIN32__ && !__OPENNT */ 1692} 1693 1694/* Okay, now we write the entry_function for filename completion. In the 1695 general case. Note that completion in the shell is a little different 1696 because of all the pathnames that must be followed when looking up the 1697 completion for a command. */ 1698char * 1699rl_filename_completion_function (text, state) 1700 const char *text; 1701 int state; 1702{ 1703 static DIR *directory = (DIR *)NULL; 1704 static char *filename = (char *)NULL; 1705 static char *dirname = (char *)NULL; 1706 static char *users_dirname = (char *)NULL; 1707 static int filename_len; 1708 char *temp; 1709 int dirlen; 1710 struct dirent *entry; 1711 1712 /* If we don't have any state, then do some initialization. */ 1713 if (state == 0) 1714 { 1715 /* If we were interrupted before closing the directory or reading 1716 all of its contents, close it. */ 1717 if (directory) 1718 { 1719 closedir (directory); 1720 directory = (DIR *)NULL; 1721 } 1722 FREE (dirname); 1723 FREE (filename); 1724 FREE (users_dirname); 1725 1726 filename = savestring (text); 1727 if (*text == 0) 1728 text = "."; 1729 dirname = savestring (text); 1730 1731 temp = strrchr (dirname, '/'); 1732 1733#if defined (__MSDOS__) 1734 /* special hack for //X/... */ 1735 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/') 1736 temp = strrchr (dirname + 3, '/'); 1737#endif 1738 1739 if (temp) 1740 { 1741 strcpy (filename, ++temp); 1742 *temp = '\0'; 1743 } 1744#if defined (__MSDOS__) 1745 /* searches from current directory on the drive */ 1746 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':') 1747 { 1748 strcpy (filename, dirname + 2); 1749 dirname[2] = '\0'; 1750 } 1751#endif 1752 else 1753 { 1754 dirname[0] = '.'; 1755 dirname[1] = '\0'; 1756 } 1757 1758 /* We aren't done yet. We also support the "~user" syntax. */ 1759 1760 /* Save the version of the directory that the user typed. */ 1761 users_dirname = savestring (dirname); 1762 1763 if (*dirname == '~') 1764 { 1765 temp = tilde_expand (dirname); 1766 free (dirname); 1767 dirname = temp; 1768 } 1769 1770 if (rl_directory_rewrite_hook) 1771 (*rl_directory_rewrite_hook) (&dirname); 1772 1773 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname)) 1774 { 1775 free (users_dirname); 1776 users_dirname = savestring (dirname); 1777 } 1778 1779 directory = opendir (dirname); 1780 filename_len = strlen (filename); 1781 1782 rl_filename_completion_desired = 1; 1783 } 1784 1785 /* At this point we should entertain the possibility of hacking wildcarded 1786 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name 1787 contains globbing characters, then build an array of directories, and 1788 then map over that list while completing. */ 1789 /* *** UNIMPLEMENTED *** */ 1790 1791 /* Now that we have some state, we can read the directory. */ 1792 1793 entry = (struct dirent *)NULL; 1794 while (directory && (entry = readdir (directory))) 1795 { 1796 /* Special case for no filename. If the user has disabled the 1797 `match-hidden-files' variable, skip filenames beginning with `.'. 1798 All other entries except "." and ".." match. */ 1799 if (filename_len == 0) 1800 { 1801 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (entry->d_name)) 1802 continue; 1803 1804 if (entry->d_name[0] != '.' || 1805 (entry->d_name[1] && 1806 (entry->d_name[1] != '.' || entry->d_name[2]))) 1807 break; 1808 } 1809 else 1810 { 1811 /* Otherwise, if these match up to the length of filename, then 1812 it is a match. */ 1813 if (_rl_completion_case_fold) 1814 { 1815 if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) && 1816 (((int)D_NAMLEN (entry)) >= filename_len) && 1817 (_rl_strnicmp (filename, entry->d_name, filename_len) == 0)) 1818 break; 1819 } 1820 else 1821 { 1822 if ((entry->d_name[0] == filename[0]) && 1823 (((int)D_NAMLEN (entry)) >= filename_len) && 1824 (strncmp (filename, entry->d_name, filename_len) == 0)) 1825 break; 1826 } 1827 } 1828 } 1829 1830 if (entry == 0) 1831 { 1832 if (directory) 1833 { 1834 closedir (directory); 1835 directory = (DIR *)NULL; 1836 } 1837 if (dirname) 1838 { 1839 free (dirname); 1840 dirname = (char *)NULL; 1841 } 1842 if (filename) 1843 { 1844 free (filename); 1845 filename = (char *)NULL; 1846 } 1847 if (users_dirname) 1848 { 1849 free (users_dirname); 1850 users_dirname = (char *)NULL; 1851 } 1852 1853 return (char *)NULL; 1854 } 1855 else 1856 { 1857 /* dirname && (strcmp (dirname, ".") != 0) */ 1858 if (dirname && (dirname[0] != '.' || dirname[1])) 1859 { 1860 if (rl_complete_with_tilde_expansion && *users_dirname == '~') 1861 { 1862 dirlen = strlen (dirname); 1863 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); 1864 strcpy (temp, dirname); 1865 /* Canonicalization cuts off any final slash present. We 1866 may need to add it back. */ 1867 if (dirname[dirlen - 1] != '/') 1868 { 1869 temp[dirlen++] = '/'; 1870 temp[dirlen] = '\0'; 1871 } 1872 } 1873 else 1874 { 1875 dirlen = strlen (users_dirname); 1876 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); 1877 strcpy (temp, users_dirname); 1878 /* Make sure that temp has a trailing slash here. */ 1879 if (users_dirname[dirlen - 1] != '/') 1880 temp[dirlen++] = '/'; 1881 } 1882 1883 strcpy (temp + dirlen, entry->d_name); 1884 } 1885 else 1886 temp = savestring (entry->d_name); 1887 1888 return (temp); 1889 } 1890} 1891 1892/* An initial implementation of a menu completion function a la tcsh. The 1893 first time (if the last readline command was not rl_menu_complete), we 1894 generate the list of matches. This code is very similar to the code in 1895 rl_complete_internal -- there should be a way to combine the two. Then, 1896 for each item in the list of matches, we insert the match in an undoable 1897 fashion, with the appropriate character appended (this happens on the 1898 second and subsequent consecutive calls to rl_menu_complete). When we 1899 hit the end of the match list, we restore the original unmatched text, 1900 ring the bell, and reset the counter to zero. */ 1901int 1902rl_menu_complete (count, ignore) 1903 int count, ignore; 1904{ 1905 rl_compentry_func_t *our_func; 1906 int matching_filenames, found_quote; 1907 1908 static char *orig_text; 1909 static char **matches = (char **)0; 1910 static int match_list_index = 0; 1911 static int match_list_size = 0; 1912 static int orig_start, orig_end; 1913 static char quote_char; 1914 static int delimiter; 1915 1916 /* The first time through, we generate the list of matches and set things 1917 up to insert them. */ 1918 if (rl_last_func != rl_menu_complete) 1919 { 1920 /* Clean up from previous call, if any. */ 1921 FREE (orig_text); 1922 if (matches) 1923 _rl_free_match_list (matches); 1924 1925 match_list_index = match_list_size = 0; 1926 matches = (char **)NULL; 1927 1928 /* Only the completion entry function can change these. */ 1929 set_completion_defaults ('%'); 1930 1931 our_func = rl_completion_entry_function 1932 ? rl_completion_entry_function 1933 : rl_filename_completion_function; 1934 1935 /* We now look backwards for the start of a filename/variable word. */ 1936 orig_end = rl_point; 1937 found_quote = delimiter = 0; 1938 quote_char = '\0'; 1939 1940 if (rl_point) 1941 /* This (possibly) changes rl_point. If it returns a non-zero char, 1942 we know we have an open quote. */ 1943 quote_char = _rl_find_completion_word (&found_quote, &delimiter); 1944 1945 orig_start = rl_point; 1946 rl_point = orig_end; 1947 1948 orig_text = rl_copy_text (orig_start, orig_end); 1949 matches = gen_completion_matches (orig_text, orig_start, orig_end, 1950 our_func, found_quote, quote_char); 1951 1952 /* If we are matching filenames, the attempted completion function will 1953 have set rl_filename_completion_desired to a non-zero value. The basic 1954 rl_filename_completion_function does this. */ 1955 matching_filenames = rl_filename_completion_desired; 1956 1957 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0) 1958 { 1959 rl_ding (); 1960 FREE (matches); 1961 matches = (char **)0; 1962 FREE (orig_text); 1963 orig_text = (char *)0; 1964 completion_changed_buffer = 0; 1965 return (0); 1966 } 1967 1968 for (match_list_size = 0; matches[match_list_size]; match_list_size++) 1969 ; 1970 /* matches[0] is lcd if match_list_size > 1, but the circular buffer 1971 code below should take care of it. */ 1972 } 1973 1974 /* Now we have the list of matches. Replace the text between 1975 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with 1976 matches[match_list_index], and add any necessary closing char. */ 1977 1978 if (matches == 0 || match_list_size == 0) 1979 { 1980 rl_ding (); 1981 FREE (matches); 1982 matches = (char **)0; 1983 completion_changed_buffer = 0; 1984 return (0); 1985 } 1986 1987 match_list_index = (match_list_index + count) % match_list_size; 1988 if (match_list_index < 0) 1989 match_list_index += match_list_size; 1990 1991 if (match_list_index == 0 && match_list_size > 1) 1992 { 1993 rl_ding (); 1994 insert_match (orig_text, orig_start, MULT_MATCH, "e_char); 1995 } 1996 else 1997 { 1998 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char); 1999 append_to_match (matches[match_list_index], delimiter, quote_char, 2000 strcmp (orig_text, matches[match_list_index])); 2001 } 2002 2003 completion_changed_buffer = 1; 2004 return (0); 2005}
| 1726 else if (rl_editing_mode != vi_mode) 1727 rl_ding (); /* There are other matches remaining. */ 1728 } 1729 else 1730 append_to_match (matches[0], delimiter, quote_char, nontrivial_lcd); 1731 1732 break; 1733 1734 case '*': 1735 insert_all_matches (matches, start, "e_char); 1736 break; 1737 1738 case '?': 1739 display_matches (matches); 1740 break; 1741 1742 default: 1743 fprintf (stderr, "\r\nreadline: bad value %d for what_to_do in rl_complete\n", what_to_do); 1744 rl_ding (); 1745 FREE (saved_line_buffer); 1746 RL_UNSETSTATE(RL_STATE_COMPLETING); 1747 return 1; 1748 } 1749 1750 _rl_free_match_list (matches); 1751 1752 /* Check to see if the line has changed through all of this manipulation. */ 1753 if (saved_line_buffer) 1754 { 1755 completion_changed_buffer = strcmp (rl_line_buffer, saved_line_buffer) != 0; 1756 free (saved_line_buffer); 1757 } 1758 1759 RL_UNSETSTATE(RL_STATE_COMPLETING); 1760 return 0; 1761} 1762 1763/***************************************************************/ 1764/* */ 1765/* Application-callable completion match generator functions */ 1766/* */ 1767/***************************************************************/ 1768 1769/* Return an array of (char *) which is a list of completions for TEXT. 1770 If there are no completions, return a NULL pointer. 1771 The first entry in the returned array is the substitution for TEXT. 1772 The remaining entries are the possible completions. 1773 The array is terminated with a NULL pointer. 1774 1775 ENTRY_FUNCTION is a function of two args, and returns a (char *). 1776 The first argument is TEXT. 1777 The second is a state argument; it should be zero on the first call, and 1778 non-zero on subsequent calls. It returns a NULL pointer to the caller 1779 when there are no more matches. 1780 */ 1781char ** 1782rl_completion_matches (text, entry_function) 1783 const char *text; 1784 rl_compentry_func_t *entry_function; 1785{ 1786 /* Number of slots in match_list. */ 1787 int match_list_size; 1788 1789 /* The list of matches. */ 1790 char **match_list; 1791 1792 /* Number of matches actually found. */ 1793 int matches; 1794 1795 /* Temporary string binder. */ 1796 char *string; 1797 1798 matches = 0; 1799 match_list_size = 10; 1800 match_list = (char **)xmalloc ((match_list_size + 1) * sizeof (char *)); 1801 match_list[1] = (char *)NULL; 1802 1803 while (string = (*entry_function) (text, matches)) 1804 { 1805 if (matches + 1 == match_list_size) 1806 match_list = (char **)xrealloc 1807 (match_list, ((match_list_size += 10) + 1) * sizeof (char *)); 1808 1809 match_list[++matches] = string; 1810 match_list[matches + 1] = (char *)NULL; 1811 } 1812 1813 /* If there were any matches, then look through them finding out the 1814 lowest common denominator. That then becomes match_list[0]. */ 1815 if (matches) 1816 compute_lcd_of_matches (match_list, matches, text); 1817 else /* There were no matches. */ 1818 { 1819 free (match_list); 1820 match_list = (char **)NULL; 1821 } 1822 return (match_list); 1823} 1824 1825/* A completion function for usernames. 1826 TEXT contains a partial username preceded by a random 1827 character (usually `~'). */ 1828char * 1829rl_username_completion_function (text, state) 1830 const char *text; 1831 int state; 1832{ 1833#if defined (__WIN32__) || defined (__OPENNT) 1834 return (char *)NULL; 1835#else /* !__WIN32__ && !__OPENNT) */ 1836 static char *username = (char *)NULL; 1837 static struct passwd *entry; 1838 static int namelen, first_char, first_char_loc; 1839 char *value; 1840 1841 if (state == 0) 1842 { 1843 FREE (username); 1844 1845 first_char = *text; 1846 first_char_loc = first_char == '~'; 1847 1848 username = savestring (&text[first_char_loc]); 1849 namelen = strlen (username); 1850 setpwent (); 1851 } 1852 1853 while (entry = getpwent ()) 1854 { 1855 /* Null usernames should result in all users as possible completions. */ 1856 if (namelen == 0 || (STREQN (username, entry->pw_name, namelen))) 1857 break; 1858 } 1859 1860 if (entry == 0) 1861 { 1862 endpwent (); 1863 return ((char *)NULL); 1864 } 1865 else 1866 { 1867 value = (char *)xmalloc (2 + strlen (entry->pw_name)); 1868 1869 *value = *text; 1870 1871 strcpy (value + first_char_loc, entry->pw_name); 1872 1873 if (first_char == '~') 1874 rl_filename_completion_desired = 1; 1875 1876 return (value); 1877 } 1878#endif /* !__WIN32__ && !__OPENNT */ 1879} 1880 1881/* Okay, now we write the entry_function for filename completion. In the 1882 general case. Note that completion in the shell is a little different 1883 because of all the pathnames that must be followed when looking up the 1884 completion for a command. */ 1885char * 1886rl_filename_completion_function (text, state) 1887 const char *text; 1888 int state; 1889{ 1890 static DIR *directory = (DIR *)NULL; 1891 static char *filename = (char *)NULL; 1892 static char *dirname = (char *)NULL; 1893 static char *users_dirname = (char *)NULL; 1894 static int filename_len; 1895 char *temp; 1896 int dirlen; 1897 struct dirent *entry; 1898 1899 /* If we don't have any state, then do some initialization. */ 1900 if (state == 0) 1901 { 1902 /* If we were interrupted before closing the directory or reading 1903 all of its contents, close it. */ 1904 if (directory) 1905 { 1906 closedir (directory); 1907 directory = (DIR *)NULL; 1908 } 1909 FREE (dirname); 1910 FREE (filename); 1911 FREE (users_dirname); 1912 1913 filename = savestring (text); 1914 if (*text == 0) 1915 text = "."; 1916 dirname = savestring (text); 1917 1918 temp = strrchr (dirname, '/'); 1919 1920#if defined (__MSDOS__) 1921 /* special hack for //X/... */ 1922 if (dirname[0] == '/' && dirname[1] == '/' && ISALPHA ((unsigned char)dirname[2]) && dirname[3] == '/') 1923 temp = strrchr (dirname + 3, '/'); 1924#endif 1925 1926 if (temp) 1927 { 1928 strcpy (filename, ++temp); 1929 *temp = '\0'; 1930 } 1931#if defined (__MSDOS__) 1932 /* searches from current directory on the drive */ 1933 else if (ISALPHA ((unsigned char)dirname[0]) && dirname[1] == ':') 1934 { 1935 strcpy (filename, dirname + 2); 1936 dirname[2] = '\0'; 1937 } 1938#endif 1939 else 1940 { 1941 dirname[0] = '.'; 1942 dirname[1] = '\0'; 1943 } 1944 1945 /* We aren't done yet. We also support the "~user" syntax. */ 1946 1947 /* Save the version of the directory that the user typed. */ 1948 users_dirname = savestring (dirname); 1949 1950 if (*dirname == '~') 1951 { 1952 temp = tilde_expand (dirname); 1953 free (dirname); 1954 dirname = temp; 1955 } 1956 1957 if (rl_directory_rewrite_hook) 1958 (*rl_directory_rewrite_hook) (&dirname); 1959 1960 if (rl_directory_completion_hook && (*rl_directory_completion_hook) (&dirname)) 1961 { 1962 free (users_dirname); 1963 users_dirname = savestring (dirname); 1964 } 1965 1966 directory = opendir (dirname); 1967 filename_len = strlen (filename); 1968 1969 rl_filename_completion_desired = 1; 1970 } 1971 1972 /* At this point we should entertain the possibility of hacking wildcarded 1973 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name 1974 contains globbing characters, then build an array of directories, and 1975 then map over that list while completing. */ 1976 /* *** UNIMPLEMENTED *** */ 1977 1978 /* Now that we have some state, we can read the directory. */ 1979 1980 entry = (struct dirent *)NULL; 1981 while (directory && (entry = readdir (directory))) 1982 { 1983 /* Special case for no filename. If the user has disabled the 1984 `match-hidden-files' variable, skip filenames beginning with `.'. 1985 All other entries except "." and ".." match. */ 1986 if (filename_len == 0) 1987 { 1988 if (_rl_match_hidden_files == 0 && HIDDEN_FILE (entry->d_name)) 1989 continue; 1990 1991 if (entry->d_name[0] != '.' || 1992 (entry->d_name[1] && 1993 (entry->d_name[1] != '.' || entry->d_name[2]))) 1994 break; 1995 } 1996 else 1997 { 1998 /* Otherwise, if these match up to the length of filename, then 1999 it is a match. */ 2000 if (_rl_completion_case_fold) 2001 { 2002 if ((_rl_to_lower (entry->d_name[0]) == _rl_to_lower (filename[0])) && 2003 (((int)D_NAMLEN (entry)) >= filename_len) && 2004 (_rl_strnicmp (filename, entry->d_name, filename_len) == 0)) 2005 break; 2006 } 2007 else 2008 { 2009 if ((entry->d_name[0] == filename[0]) && 2010 (((int)D_NAMLEN (entry)) >= filename_len) && 2011 (strncmp (filename, entry->d_name, filename_len) == 0)) 2012 break; 2013 } 2014 } 2015 } 2016 2017 if (entry == 0) 2018 { 2019 if (directory) 2020 { 2021 closedir (directory); 2022 directory = (DIR *)NULL; 2023 } 2024 if (dirname) 2025 { 2026 free (dirname); 2027 dirname = (char *)NULL; 2028 } 2029 if (filename) 2030 { 2031 free (filename); 2032 filename = (char *)NULL; 2033 } 2034 if (users_dirname) 2035 { 2036 free (users_dirname); 2037 users_dirname = (char *)NULL; 2038 } 2039 2040 return (char *)NULL; 2041 } 2042 else 2043 { 2044 /* dirname && (strcmp (dirname, ".") != 0) */ 2045 if (dirname && (dirname[0] != '.' || dirname[1])) 2046 { 2047 if (rl_complete_with_tilde_expansion && *users_dirname == '~') 2048 { 2049 dirlen = strlen (dirname); 2050 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); 2051 strcpy (temp, dirname); 2052 /* Canonicalization cuts off any final slash present. We 2053 may need to add it back. */ 2054 if (dirname[dirlen - 1] != '/') 2055 { 2056 temp[dirlen++] = '/'; 2057 temp[dirlen] = '\0'; 2058 } 2059 } 2060 else 2061 { 2062 dirlen = strlen (users_dirname); 2063 temp = (char *)xmalloc (2 + dirlen + D_NAMLEN (entry)); 2064 strcpy (temp, users_dirname); 2065 /* Make sure that temp has a trailing slash here. */ 2066 if (users_dirname[dirlen - 1] != '/') 2067 temp[dirlen++] = '/'; 2068 } 2069 2070 strcpy (temp + dirlen, entry->d_name); 2071 } 2072 else 2073 temp = savestring (entry->d_name); 2074 2075 return (temp); 2076 } 2077} 2078 2079/* An initial implementation of a menu completion function a la tcsh. The 2080 first time (if the last readline command was not rl_menu_complete), we 2081 generate the list of matches. This code is very similar to the code in 2082 rl_complete_internal -- there should be a way to combine the two. Then, 2083 for each item in the list of matches, we insert the match in an undoable 2084 fashion, with the appropriate character appended (this happens on the 2085 second and subsequent consecutive calls to rl_menu_complete). When we 2086 hit the end of the match list, we restore the original unmatched text, 2087 ring the bell, and reset the counter to zero. */ 2088int 2089rl_menu_complete (count, ignore) 2090 int count, ignore; 2091{ 2092 rl_compentry_func_t *our_func; 2093 int matching_filenames, found_quote; 2094 2095 static char *orig_text; 2096 static char **matches = (char **)0; 2097 static int match_list_index = 0; 2098 static int match_list_size = 0; 2099 static int orig_start, orig_end; 2100 static char quote_char; 2101 static int delimiter; 2102 2103 /* The first time through, we generate the list of matches and set things 2104 up to insert them. */ 2105 if (rl_last_func != rl_menu_complete) 2106 { 2107 /* Clean up from previous call, if any. */ 2108 FREE (orig_text); 2109 if (matches) 2110 _rl_free_match_list (matches); 2111 2112 match_list_index = match_list_size = 0; 2113 matches = (char **)NULL; 2114 2115 /* Only the completion entry function can change these. */ 2116 set_completion_defaults ('%'); 2117 2118 our_func = rl_completion_entry_function 2119 ? rl_completion_entry_function 2120 : rl_filename_completion_function; 2121 2122 /* We now look backwards for the start of a filename/variable word. */ 2123 orig_end = rl_point; 2124 found_quote = delimiter = 0; 2125 quote_char = '\0'; 2126 2127 if (rl_point) 2128 /* This (possibly) changes rl_point. If it returns a non-zero char, 2129 we know we have an open quote. */ 2130 quote_char = _rl_find_completion_word (&found_quote, &delimiter); 2131 2132 orig_start = rl_point; 2133 rl_point = orig_end; 2134 2135 orig_text = rl_copy_text (orig_start, orig_end); 2136 matches = gen_completion_matches (orig_text, orig_start, orig_end, 2137 our_func, found_quote, quote_char); 2138 2139 /* If we are matching filenames, the attempted completion function will 2140 have set rl_filename_completion_desired to a non-zero value. The basic 2141 rl_filename_completion_function does this. */ 2142 matching_filenames = rl_filename_completion_desired; 2143 2144 if (matches == 0 || postprocess_matches (&matches, matching_filenames) == 0) 2145 { 2146 rl_ding (); 2147 FREE (matches); 2148 matches = (char **)0; 2149 FREE (orig_text); 2150 orig_text = (char *)0; 2151 completion_changed_buffer = 0; 2152 return (0); 2153 } 2154 2155 for (match_list_size = 0; matches[match_list_size]; match_list_size++) 2156 ; 2157 /* matches[0] is lcd if match_list_size > 1, but the circular buffer 2158 code below should take care of it. */ 2159 } 2160 2161 /* Now we have the list of matches. Replace the text between 2162 rl_line_buffer[orig_start] and rl_line_buffer[rl_point] with 2163 matches[match_list_index], and add any necessary closing char. */ 2164 2165 if (matches == 0 || match_list_size == 0) 2166 { 2167 rl_ding (); 2168 FREE (matches); 2169 matches = (char **)0; 2170 completion_changed_buffer = 0; 2171 return (0); 2172 } 2173 2174 match_list_index = (match_list_index + count) % match_list_size; 2175 if (match_list_index < 0) 2176 match_list_index += match_list_size; 2177 2178 if (match_list_index == 0 && match_list_size > 1) 2179 { 2180 rl_ding (); 2181 insert_match (orig_text, orig_start, MULT_MATCH, "e_char); 2182 } 2183 else 2184 { 2185 insert_match (matches[match_list_index], orig_start, SINGLE_MATCH, "e_char); 2186 append_to_match (matches[match_list_index], delimiter, quote_char, 2187 strcmp (orig_text, matches[match_list_index])); 2188 } 2189 2190 completion_changed_buffer = 1; 2191 return (0); 2192}
|