complete.c 79 KB

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