input_complete.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. /*
  2. Input line filename/username/hostname/variable/command completion.
  3. (Let mc type for you...)
  4. Copyright (C) 1995-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Jakub Jelinek, 1995
  8. Slava Zanko <slavazanko@gmail.com>, 2013
  9. Andrew Borodin <aborodin@vmail.ru>, 2013-2022
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /** \file lib/widget/input_complete.c
  23. * \brief Source: Input line filename/username/hostname/variable/command completion
  24. */
  25. #include <config.h>
  26. #include <ctype.h>
  27. #include <limits.h> /* MB_LEN_MAX */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <dirent.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <pwd.h>
  35. #include <unistd.h>
  36. #include "lib/global.h"
  37. #include "lib/tty/tty.h"
  38. #include "lib/tty/key.h" /* XCTRL and ALT macros */
  39. #include "lib/vfs/vfs.h"
  40. #include "lib/strutil.h"
  41. #include "lib/util.h"
  42. #include "lib/widget.h"
  43. /*** global variables ****************************************************************************/
  44. /* Linux declares environ in <unistd.h>, so don't repeat it here. */
  45. #if (!(defined(__linux__) && defined (__USE_GNU)) && !defined(__CYGWIN__))
  46. extern char **environ;
  47. #endif
  48. /*** file scope macro definitions ****************************************************************/
  49. /* #define DO_COMPLETION_DEBUG */
  50. #ifdef DO_COMPLETION_DEBUG
  51. #define SHOW_C_CTX(func) fprintf(stderr, "%s: text='%s' flags=%s\n", func, text, show_c_flags(flags))
  52. #else
  53. #define SHOW_C_CTX(func)
  54. #endif /* DO_CMPLETION_DEBUG */
  55. #define DO_INSERTION 1
  56. #define DO_QUERY 2
  57. /*** file scope type declarations ****************************************************************/
  58. typedef char *CompletionFunction (const char *text, int state, input_complete_t flags);
  59. typedef struct
  60. {
  61. size_t in_command_position;
  62. char *word;
  63. char *p;
  64. char *q;
  65. char *r;
  66. gboolean is_cd;
  67. input_complete_t flags;
  68. } try_complete_automation_state_t;
  69. /*** forward declarations (file scope functions) *************************************************/
  70. GPtrArray *try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags);
  71. void complete_engine_fill_completions (WInput * in);
  72. /*** file scope variables ************************************************************************/
  73. static WInput *input;
  74. static int min_end;
  75. static int start = 0;
  76. static int end = 0;
  77. /* --------------------------------------------------------------------------------------------- */
  78. /*** file scope functions ************************************************************************/
  79. /* --------------------------------------------------------------------------------------------- */
  80. #ifdef DO_COMPLETION_DEBUG
  81. /**
  82. * Useful to print/debug completion flags
  83. */
  84. static const char *
  85. show_c_flags (input_complete_t flags)
  86. {
  87. static char s_cf[] = "FHCVUDS";
  88. s_cf[0] = (flags & INPUT_COMPLETE_FILENAMES) != 0 ? 'F' : ' ';
  89. s_cf[1] = (flags & INPUT_COMPLETE_HOSTNAMES) != 0 ? 'H' : ' ';
  90. s_cf[2] = (flags & INPUT_COMPLETE_COMMANDS) != 0 ? 'C' : ' ';
  91. s_cf[3] = (flags & INPUT_COMPLETE_VARIABLES) != 0 ? 'V' : ' ';
  92. s_cf[4] = (flags & INPUT_COMPLETE_USERNAMES) != 0 ? 'U' : ' ';
  93. s_cf[5] = (flags & INPUT_COMPLETE_CD) != 0 ? 'D' : ' ';
  94. s_cf[6] = (flags & INPUT_COMPLETE_SHELL_ESC) != 0 ? 'S' : ' ';
  95. return s_cf;
  96. }
  97. #endif /* DO_CMPLETION_DEBUG */
  98. /* --------------------------------------------------------------------------------------------- */
  99. static char *
  100. filename_completion_function (const char *text, int state, input_complete_t flags)
  101. {
  102. static DIR *directory = NULL;
  103. static char *filename = NULL;
  104. static char *dirname = NULL;
  105. static char *users_dirname = NULL;
  106. static size_t filename_len = 0;
  107. static vfs_path_t *dirname_vpath = NULL;
  108. gboolean isdir = TRUE, isexec = FALSE;
  109. struct vfs_dirent *entry = NULL;
  110. SHOW_C_CTX ("filename_completion_function");
  111. if (text != NULL && (flags & INPUT_COMPLETE_SHELL_ESC) != 0)
  112. {
  113. char *u_text;
  114. char *result;
  115. char *e_result;
  116. u_text = str_shell_unescape (text);
  117. result = filename_completion_function (u_text, state, flags & (~INPUT_COMPLETE_SHELL_ESC));
  118. g_free (u_text);
  119. e_result = str_shell_escape (result);
  120. g_free (result);
  121. return e_result;
  122. }
  123. /* If we're starting the match process, initialize us a bit. */
  124. if (state == 0)
  125. {
  126. const char *temp;
  127. g_free (dirname);
  128. g_free (filename);
  129. g_free (users_dirname);
  130. vfs_path_free (dirname_vpath, TRUE);
  131. if ((*text != '\0') && (temp = strrchr (text, PATH_SEP)) != NULL)
  132. {
  133. filename = g_strdup (++temp);
  134. dirname = g_strndup (text, temp - text);
  135. }
  136. else
  137. {
  138. dirname = g_strdup (".");
  139. filename = g_strdup (text);
  140. }
  141. /* We aren't done yet. We also support the "~user" syntax. */
  142. /* Save the version of the directory that the user typed. */
  143. users_dirname = dirname;
  144. dirname = tilde_expand (dirname);
  145. canonicalize_pathname (dirname);
  146. dirname_vpath = vfs_path_from_str (dirname);
  147. /* Here we should do something with variable expansion
  148. and `command`.
  149. Maybe a dream - UNIMPLEMENTED yet. */
  150. directory = mc_opendir (dirname_vpath);
  151. filename_len = strlen (filename);
  152. }
  153. /* Now that we have some state, we can read the directory. */
  154. while (directory != NULL && (entry = mc_readdir (directory)) != NULL)
  155. {
  156. if (!str_is_valid_string (entry->d_name))
  157. continue;
  158. /* Special case for no filename.
  159. All entries except "." and ".." match. */
  160. if (filename_len == 0)
  161. {
  162. if (DIR_IS_DOT (entry->d_name) || DIR_IS_DOTDOT (entry->d_name))
  163. continue;
  164. }
  165. else
  166. {
  167. /* Otherwise, if these match up to the length of filename, then
  168. it may be a match. */
  169. if (entry->d_name[0] != filename[0] || entry->d_len < filename_len
  170. || strncmp (filename, entry->d_name, filename_len) != 0)
  171. continue;
  172. }
  173. isdir = TRUE;
  174. isexec = FALSE;
  175. {
  176. struct stat tempstat;
  177. vfs_path_t *tmp_vpath;
  178. tmp_vpath = vfs_path_build_filename (dirname, entry->d_name, (char *) NULL);
  179. /* Unix version */
  180. if (mc_stat (tmp_vpath, &tempstat) == 0)
  181. {
  182. uid_t my_uid;
  183. gid_t my_gid;
  184. my_uid = getuid ();
  185. my_gid = getgid ();
  186. if (!S_ISDIR (tempstat.st_mode))
  187. {
  188. isdir = FALSE;
  189. if ((my_uid == 0 && (tempstat.st_mode & 0111) != 0) ||
  190. (my_uid == tempstat.st_uid && (tempstat.st_mode & 0100) != 0) ||
  191. (my_gid == tempstat.st_gid && (tempstat.st_mode & 0010) != 0) ||
  192. (tempstat.st_mode & 0001) != 0)
  193. isexec = TRUE;
  194. }
  195. }
  196. else
  197. {
  198. /* stat failed, strange. not a dir in any case */
  199. isdir = FALSE;
  200. }
  201. vfs_path_free (tmp_vpath, TRUE);
  202. }
  203. if ((flags & INPUT_COMPLETE_COMMANDS) != 0 && (isexec || isdir))
  204. break;
  205. if ((flags & INPUT_COMPLETE_CD) != 0 && isdir)
  206. break;
  207. if ((flags & INPUT_COMPLETE_FILENAMES) != 0)
  208. break;
  209. }
  210. if (entry == NULL)
  211. {
  212. if (directory != NULL)
  213. {
  214. mc_closedir (directory);
  215. directory = NULL;
  216. }
  217. MC_PTR_FREE (dirname);
  218. vfs_path_free (dirname_vpath, TRUE);
  219. dirname_vpath = NULL;
  220. MC_PTR_FREE (filename);
  221. MC_PTR_FREE (users_dirname);
  222. return NULL;
  223. }
  224. {
  225. GString *temp;
  226. temp = g_string_sized_new (16);
  227. if (users_dirname != NULL && !DIR_IS_DOT (users_dirname))
  228. {
  229. g_string_append (temp, users_dirname);
  230. /* We need a '/' at the end. */
  231. if (!IS_PATH_SEP (temp->str[temp->len - 1]))
  232. g_string_append_c (temp, PATH_SEP);
  233. }
  234. g_string_append_len (temp, entry->d_name, entry->d_len);
  235. if (isdir)
  236. g_string_append_c (temp, PATH_SEP);
  237. return g_string_free (temp, FALSE);
  238. }
  239. }
  240. /* --------------------------------------------------------------------------------------------- */
  241. /** We assume here that text[0] == '~' , if you want to call it in another way,
  242. you have to change the code */
  243. static char *
  244. username_completion_function (const char *text, int state, input_complete_t flags)
  245. {
  246. static struct passwd *entry = NULL;
  247. static size_t userlen = 0;
  248. (void) flags;
  249. SHOW_C_CTX ("username_completion_function");
  250. if (text[0] == '\\' && text[1] == '~')
  251. text++;
  252. if (state == 0)
  253. { /* Initialization stuff */
  254. setpwent ();
  255. userlen = strlen (text + 1);
  256. }
  257. while ((entry = getpwent ()) != NULL)
  258. {
  259. /* Null usernames should result in all users as possible completions. */
  260. if (userlen == 0)
  261. break;
  262. if (text[1] == entry->pw_name[0] && strncmp (text + 1, entry->pw_name, userlen) == 0)
  263. break;
  264. }
  265. if (entry != NULL)
  266. return g_strconcat ("~", entry->pw_name, PATH_SEP_STR, (char *) NULL);
  267. endpwent ();
  268. return NULL;
  269. }
  270. /* --------------------------------------------------------------------------------------------- */
  271. /** We assume text [0] == '$' and want to have a look at text [1], if it is
  272. equal to '{', so that we should append '}' at the end */
  273. static char *
  274. variable_completion_function (const char *text, int state, input_complete_t flags)
  275. {
  276. static char **env_p = NULL;
  277. static gboolean isbrace = FALSE;
  278. static size_t varlen = 0;
  279. const char *p = NULL;
  280. (void) flags;
  281. SHOW_C_CTX ("variable_completion_function");
  282. if (state == 0)
  283. { /* Initialization stuff */
  284. isbrace = (text[1] == '{');
  285. varlen = strlen (text + 1 + isbrace);
  286. env_p = environ;
  287. }
  288. while (*env_p != NULL)
  289. {
  290. p = strchr (*env_p, '=');
  291. if (p != NULL && ((size_t) (p - *env_p) >= varlen)
  292. && strncmp (text + 1 + isbrace, *env_p, varlen) == 0)
  293. break;
  294. env_p++;
  295. }
  296. if (*env_p == NULL)
  297. return NULL;
  298. {
  299. GString *temp;
  300. temp = g_string_new_len (*env_p, p - *env_p);
  301. if (isbrace)
  302. {
  303. g_string_prepend_c (temp, '{');
  304. g_string_append_c (temp, '}');
  305. }
  306. g_string_prepend_c (temp, '$');
  307. env_p++;
  308. return g_string_free (temp, FALSE);
  309. }
  310. }
  311. /* --------------------------------------------------------------------------------------------- */
  312. static gboolean
  313. host_equal_func (gconstpointer a, gconstpointer b)
  314. {
  315. return (strcmp ((const char *) a, (const char *) b) == 0);
  316. }
  317. /* --------------------------------------------------------------------------------------------- */
  318. static void
  319. fetch_hosts (const char *filename, GPtrArray *hosts)
  320. {
  321. FILE *file;
  322. char buffer[BUF_MEDIUM];
  323. char *bi;
  324. file = fopen (filename, "r");
  325. if (file == NULL)
  326. return;
  327. while (fgets (buffer, sizeof (buffer) - 1, file) != NULL)
  328. {
  329. /* Skip to first character. */
  330. for (bi = buffer; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi))
  331. ;
  332. /* Ignore comments... */
  333. if (bi[0] == '#')
  334. continue;
  335. /* Handle $include. */
  336. if (strncmp (bi, "$include ", 9) == 0)
  337. {
  338. char *includefile, *t;
  339. /* Find start of filename. */
  340. for (includefile = bi + 9; includefile[0] != '\0' && whitespace (includefile[0]);
  341. includefile++)
  342. ;
  343. t = includefile;
  344. /* Find end of filename. */
  345. for (; t[0] != '\0' && !str_isspace (t); str_next_char (&t))
  346. ;
  347. *t = '\0';
  348. fetch_hosts (includefile, hosts);
  349. continue;
  350. }
  351. /* Skip IP #s. */
  352. for (; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi))
  353. ;
  354. /* Get the host names separated by white space. */
  355. while (bi[0] != '\0' && bi[0] != '#')
  356. {
  357. char *lc_start, *name;
  358. for (; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi))
  359. ;
  360. if (bi[0] == '#')
  361. continue;
  362. for (lc_start = bi; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi))
  363. ;
  364. if (bi == lc_start)
  365. continue;
  366. name = g_strndup (lc_start, bi - lc_start);
  367. if (!g_ptr_array_find_with_equal_func (hosts, name, host_equal_func, NULL))
  368. g_ptr_array_add (hosts, name);
  369. else
  370. g_free (name);
  371. }
  372. }
  373. fclose (file);
  374. }
  375. /* --------------------------------------------------------------------------------------------- */
  376. static char *
  377. hostname_completion_function (const char *text, int state, input_complete_t flags)
  378. {
  379. static GPtrArray *hosts = NULL;
  380. static unsigned int host_p = 0;
  381. static size_t textstart = 0;
  382. static size_t textlen = 0;
  383. (void) flags;
  384. SHOW_C_CTX ("hostname_completion_function");
  385. if (state == 0)
  386. { /* Initialization stuff */
  387. const char *p;
  388. if (hosts != NULL)
  389. g_ptr_array_free (hosts, TRUE);
  390. hosts = g_ptr_array_new_with_free_func (g_free);
  391. p = getenv ("HOSTFILE");
  392. fetch_hosts (p != NULL ? p : "/etc/hosts", hosts);
  393. host_p = 0;
  394. textstart = (*text == '@') ? 1 : 0;
  395. textlen = strlen (text + textstart);
  396. }
  397. for (; host_p < hosts->len; host_p++)
  398. {
  399. if (textlen == 0)
  400. break; /* Match all of them */
  401. if (strncmp (text + textstart, g_ptr_array_index (hosts, host_p), textlen) == 0)
  402. break;
  403. }
  404. if (host_p == hosts->len)
  405. {
  406. g_ptr_array_free (hosts, TRUE);
  407. hosts = NULL;
  408. return NULL;
  409. }
  410. {
  411. GString *temp;
  412. temp = g_string_sized_new (8);
  413. if (textstart != 0)
  414. g_string_append_c (temp, '@');
  415. g_string_append (temp, g_ptr_array_index (hosts, host_p));
  416. host_p++;
  417. return g_string_free (temp, FALSE);
  418. }
  419. }
  420. /* --------------------------------------------------------------------------------------------- */
  421. /**
  422. * This is the function to call when the word to complete is in a position
  423. * where a command word can be found. It looks around $PATH, looking for
  424. * commands that match. It also scans aliases, function names, and the
  425. * table of shell built-ins.
  426. */
  427. static char *
  428. command_completion_function (const char *text, int state, input_complete_t flags)
  429. {
  430. static const char *path_end = NULL;
  431. static gboolean isabsolute = FALSE;
  432. static int phase = 0;
  433. static size_t text_len = 0;
  434. static const char *const *words = NULL;
  435. static char *path = NULL;
  436. static char *cur_path = NULL;
  437. static char *cur_word = NULL;
  438. static int init_state = 0;
  439. static const char *const bash_reserved[] = {
  440. "if", "then", "else", "elif", "fi", "case", "esac", "for",
  441. "select", "while", "until", "do", "done", "in", "function", 0
  442. };
  443. static const char *const bash_builtins[] = {
  444. "alias", "bg", "bind", "break", "builtin", "cd", "command",
  445. "continue", "declare", "dirs", "echo", "enable", "eval",
  446. "exec", "exit", "export", "fc", "fg", "getopts", "hash",
  447. "help", "history", "jobs", "kill", "let", "local", "logout",
  448. "popd", "pushd", "pwd", "read", "readonly", "return", "set",
  449. "shift", "source", "suspend", "test", "times", "trap", "type",
  450. "typeset", "ulimit", "umask", "unalias", "unset", "wait", 0
  451. };
  452. char *u_text;
  453. char *p, *found;
  454. SHOW_C_CTX ("command_completion_function");
  455. if ((flags & INPUT_COMPLETE_COMMANDS) == 0)
  456. return NULL;
  457. u_text = str_shell_unescape (text);
  458. flags &= ~INPUT_COMPLETE_SHELL_ESC;
  459. if (state == 0)
  460. { /* Initialize us a little bit */
  461. isabsolute = strchr (u_text, PATH_SEP) != NULL;
  462. if (!isabsolute)
  463. {
  464. words = bash_reserved;
  465. phase = 0;
  466. text_len = strlen (u_text);
  467. if (path == NULL)
  468. {
  469. path = g_strdup (getenv ("PATH"));
  470. if (path != NULL)
  471. {
  472. p = path;
  473. path_end = strchr (p, '\0');
  474. while ((p = strchr (p, PATH_ENV_SEP)) != NULL)
  475. *p++ = '\0';
  476. }
  477. }
  478. }
  479. }
  480. if (isabsolute)
  481. {
  482. p = filename_completion_function (u_text, state, flags);
  483. if (p != NULL)
  484. {
  485. char *temp_p = p;
  486. p = str_shell_escape (p);
  487. g_free (temp_p);
  488. }
  489. g_free (u_text);
  490. return p;
  491. }
  492. found = NULL;
  493. switch (phase)
  494. {
  495. case 0: /* Reserved words */
  496. for (; *words != NULL; words++)
  497. if (strncmp (*words, u_text, text_len) == 0)
  498. {
  499. g_free (u_text);
  500. return g_strdup (*(words++));
  501. }
  502. phase++;
  503. words = bash_builtins;
  504. MC_FALLTHROUGH;
  505. case 1: /* Builtin commands */
  506. for (; *words != NULL; words++)
  507. if (strncmp (*words, u_text, text_len) == 0)
  508. {
  509. g_free (u_text);
  510. return g_strdup (*(words++));
  511. }
  512. phase++;
  513. if (path == NULL)
  514. break;
  515. cur_path = path;
  516. cur_word = NULL;
  517. MC_FALLTHROUGH;
  518. case 2: /* And looking through the $PATH */
  519. while (found == NULL)
  520. {
  521. if (cur_word == NULL)
  522. {
  523. char *expanded;
  524. if (cur_path >= path_end)
  525. break;
  526. expanded = tilde_expand (*cur_path != '\0' ? cur_path : ".");
  527. cur_word = mc_build_filename (expanded, u_text, (char *) NULL);
  528. g_free (expanded);
  529. cur_path = strchr (cur_path, '\0') + 1;
  530. init_state = state;
  531. }
  532. found = filename_completion_function (cur_word, state - init_state, flags);
  533. if (found == NULL)
  534. MC_PTR_FREE (cur_word);
  535. }
  536. MC_FALLTHROUGH;
  537. default:
  538. break;
  539. }
  540. if (found == NULL)
  541. MC_PTR_FREE (path);
  542. else
  543. {
  544. p = strrchr (found, PATH_SEP);
  545. if (p != NULL)
  546. {
  547. char *tmp = found;
  548. found = str_shell_escape (p + 1);
  549. g_free (tmp);
  550. }
  551. }
  552. g_free (u_text);
  553. return found;
  554. }
  555. /* --------------------------------------------------------------------------------------------- */
  556. static int
  557. match_compare (gconstpointer a, gconstpointer b)
  558. {
  559. return strcmp (*(char *const *) a, *(char *const *) b);
  560. }
  561. /* --------------------------------------------------------------------------------------------- */
  562. /** Returns an array of char * matches with the longest common denominator
  563. in the 1st entry. Then a NULL terminated list of different possible
  564. completions follows.
  565. You have to supply your own CompletionFunction with the word you
  566. want to complete as the first argument and an count of previous matches
  567. as the second.
  568. In case no matches were found we return NULL. */
  569. static GPtrArray *
  570. completion_matches (const char *text, CompletionFunction entry_function, input_complete_t flags)
  571. {
  572. GPtrArray *match_list;
  573. char *string;
  574. match_list = g_ptr_array_new_with_free_func (g_free);
  575. while ((string = entry_function (text, match_list->len, flags)) != NULL)
  576. g_ptr_array_add (match_list, string);
  577. /* If there were any matches, then look through them finding out the
  578. lowest common denominator. That then becomes match_list[0]. */
  579. if (match_list->len == 0)
  580. {
  581. /* There were no matches. */
  582. g_ptr_array_free (match_list, TRUE);
  583. return NULL;
  584. }
  585. /* If only one match, just use that. */
  586. if (match_list->len > 1)
  587. {
  588. size_t i, j;
  589. size_t low = 4096; /* Count of max-matched characters. */
  590. g_ptr_array_sort (match_list, match_compare);
  591. /* And compare each member of the list with
  592. the next, finding out where they stop matching.
  593. If we find two equal strings, we have to put one away... */
  594. for (i = 0, j = 1; j < match_list->len;)
  595. {
  596. char *si, *sj, *mi;
  597. si = g_ptr_array_index (match_list, i);
  598. sj = g_ptr_array_index (match_list, j);
  599. mi = si;
  600. while (si[0] != '\0' && sj[0] != '\0')
  601. {
  602. char *ni, *nj;
  603. ni = str_get_next_char (si);
  604. nj = str_get_next_char (sj);
  605. if (ni - si != nj - sj || strncmp (si, sj, ni - si) != 0)
  606. break;
  607. si = ni;
  608. sj = nj;
  609. }
  610. if (si[0] == '\0' && sj[0] == '\0')
  611. {
  612. /* Two equal strings */
  613. g_ptr_array_remove_index (match_list, j);
  614. }
  615. else
  616. {
  617. low = MIN (low, (size_t) (si - mi));
  618. i++;
  619. j++;
  620. }
  621. }
  622. string = g_ptr_array_index (match_list, 0);
  623. g_ptr_array_insert (match_list, 0, g_strndup (string, low));
  624. }
  625. return match_list;
  626. }
  627. /* --------------------------------------------------------------------------------------------- */
  628. /** Check if directory completion is needed */
  629. static gboolean
  630. check_is_cd (const char *text, int lc_start, input_complete_t flags)
  631. {
  632. const char *p, *q;
  633. SHOW_C_CTX ("check_is_cd");
  634. if ((flags & INPUT_COMPLETE_CD) == 0)
  635. return FALSE;
  636. /* Skip initial spaces */
  637. p = text;
  638. q = text + lc_start;
  639. while (p < q && p[0] != '\0' && str_isspace (p))
  640. str_cnext_char (&p);
  641. /* Check if the command is "cd" and the cursor is after it */
  642. return (p[0] == 'c' && p[1] == 'd' && str_isspace (p + 2) && p + 2 < q);
  643. }
  644. /* --------------------------------------------------------------------------------------------- */
  645. static void
  646. try_complete_commands_prepare (try_complete_automation_state_t *state, char *text, int *lc_start)
  647. {
  648. const char *command_separator_chars = ";|&{(`";
  649. char *ti;
  650. if (*lc_start == 0)
  651. ti = text;
  652. else
  653. {
  654. ti = str_get_prev_char (&text[*lc_start]);
  655. while (ti > text && whitespace (ti[0]))
  656. str_prev_char (&ti);
  657. }
  658. if (ti == text)
  659. state->in_command_position++;
  660. else if (strchr (command_separator_chars, ti[0]) != NULL)
  661. {
  662. state->in_command_position++;
  663. if (ti != text)
  664. {
  665. int this_char, prev_char;
  666. /* Handle the two character tokens '>&', '<&', and '>|'.
  667. We are not in a command position after one of these. */
  668. this_char = ti[0];
  669. prev_char = str_get_prev_char (ti)[0];
  670. /* Quoted */
  671. if ((this_char == '&' && (prev_char == '<' || prev_char == '>'))
  672. || (this_char == '|' && prev_char == '>') || (ti != text
  673. && str_get_prev_char (ti)[0] == '\\'))
  674. state->in_command_position = 0;
  675. }
  676. }
  677. }
  678. /* --------------------------------------------------------------------------------------------- */
  679. static void
  680. try_complete_find_start_sign (try_complete_automation_state_t *state)
  681. {
  682. if ((state->flags & INPUT_COMPLETE_COMMANDS) != 0)
  683. state->p = strrchr (state->word, '`');
  684. if ((state->flags & (INPUT_COMPLETE_COMMANDS | INPUT_COMPLETE_VARIABLES)) != 0)
  685. {
  686. state->q = strrchr (state->word, '$');
  687. /* don't substitute variable in \$ case */
  688. if (str_is_char_escaped (state->word, state->q))
  689. {
  690. /* drop '\\' */
  691. str_move (state->q - 1, state->q);
  692. /* adjust flags */
  693. state->flags &= ~INPUT_COMPLETE_VARIABLES;
  694. state->q = NULL;
  695. }
  696. }
  697. if ((state->flags & INPUT_COMPLETE_HOSTNAMES) != 0)
  698. state->r = strrchr (state->word, '@');
  699. if (state->q != NULL && state->q[1] == '(' && (state->flags & INPUT_COMPLETE_COMMANDS) != 0)
  700. {
  701. if (state->q > state->p)
  702. state->p = str_get_next_char (state->q);
  703. state->q = NULL;
  704. }
  705. }
  706. /* --------------------------------------------------------------------------------------------- */
  707. static GPtrArray *
  708. try_complete_all_possible (try_complete_automation_state_t *state, char *text, int *lc_start)
  709. {
  710. GPtrArray *matches = NULL;
  711. if (state->in_command_position != 0)
  712. {
  713. SHOW_C_CTX ("try_complete:cmd_subst");
  714. matches =
  715. completion_matches (state->word, command_completion_function,
  716. state->flags & (~INPUT_COMPLETE_FILENAMES));
  717. }
  718. else if ((state->flags & INPUT_COMPLETE_FILENAMES) != 0)
  719. {
  720. if (state->is_cd)
  721. state->flags &= ~(INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS);
  722. SHOW_C_CTX ("try_complete:filename_subst_1");
  723. matches = completion_matches (state->word, filename_completion_function, state->flags);
  724. if (matches == NULL && state->is_cd && !IS_PATH_SEP (*state->word) && *state->word != '~')
  725. {
  726. state->q = text + *lc_start;
  727. for (state->p = text;
  728. *state->p != '\0' && state->p < state->q && whitespace (*state->p);
  729. str_next_char (&state->p))
  730. ;
  731. if (strncmp (state->p, "cd", 2) == 0)
  732. for (state->p += 2;
  733. *state->p != '\0' && state->p < state->q && whitespace (*state->p);
  734. str_next_char (&state->p))
  735. ;
  736. if (state->p == state->q)
  737. {
  738. char *cdpath_ref, *cdpath;
  739. char c;
  740. cdpath_ref = g_strdup (getenv ("CDPATH"));
  741. cdpath = cdpath_ref;
  742. c = (cdpath == NULL) ? '\0' : ':';
  743. while (matches == NULL && c == ':')
  744. {
  745. char *s;
  746. s = strchr (cdpath, ':');
  747. /* cppcheck-suppress nullPointer */
  748. if (s == NULL)
  749. s = strchr (cdpath, '\0');
  750. c = *s;
  751. *s = '\0';
  752. if (*cdpath != '\0')
  753. {
  754. state->r = mc_build_filename (cdpath, state->word, (char *) NULL);
  755. SHOW_C_CTX ("try_complete:filename_subst_2");
  756. matches =
  757. completion_matches (state->r, filename_completion_function,
  758. state->flags);
  759. g_free (state->r);
  760. }
  761. *s = c;
  762. cdpath = str_get_next_char (s);
  763. }
  764. g_free (cdpath_ref);
  765. }
  766. }
  767. }
  768. return matches;
  769. }
  770. /* --------------------------------------------------------------------------------------------- */
  771. static gboolean
  772. insert_text (WInput *in, const char *text, ssize_t size)
  773. {
  774. size_t text_len;
  775. int buff_len;
  776. ssize_t new_size;
  777. text_len = strlen (text);
  778. buff_len = str_length (in->buffer->str);
  779. if (size < 0)
  780. size = (ssize_t) text_len;
  781. else
  782. size = MIN (size, (ssize_t) text_len);
  783. new_size = size + start - end;
  784. if (new_size != 0)
  785. {
  786. /* make a hole within buffer */
  787. size_t tail_len;
  788. tail_len = in->buffer->len - end;
  789. if (tail_len != 0)
  790. {
  791. char *tail;
  792. size_t hole_end;
  793. tail = g_strndup (in->buffer->str + end, tail_len);
  794. hole_end = end + new_size;
  795. if (in->buffer->len < hole_end)
  796. g_string_set_size (in->buffer, hole_end + tail_len);
  797. g_string_overwrite_len (in->buffer, hole_end, tail, tail_len);
  798. g_free (tail);
  799. }
  800. }
  801. g_string_overwrite_len (in->buffer, start, text, size);
  802. in->point += str_length (in->buffer->str) - buff_len;
  803. input_update (in, TRUE);
  804. end += new_size;
  805. return new_size != 0;
  806. }
  807. /* --------------------------------------------------------------------------------------------- */
  808. static cb_ret_t
  809. complete_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  810. {
  811. static int bl = 0;
  812. WGroup *g = GROUP (w);
  813. WDialog *h = DIALOG (w);
  814. switch (msg)
  815. {
  816. case MSG_KEY:
  817. switch (parm)
  818. {
  819. case KEY_LEFT:
  820. case KEY_RIGHT:
  821. bl = 0;
  822. h->ret_value = 0;
  823. dlg_close (h);
  824. return MSG_HANDLED;
  825. case KEY_BACKSPACE:
  826. bl = 0;
  827. /* exit from completion list if input line is empty */
  828. if (end == 0)
  829. {
  830. h->ret_value = 0;
  831. dlg_close (h);
  832. }
  833. /* Refill the list box and start again */
  834. else if (end == min_end)
  835. {
  836. end = str_get_prev_char (input->buffer->str + end) - input->buffer->str;
  837. input_handle_char (input, parm);
  838. h->ret_value = B_USER;
  839. dlg_close (h);
  840. }
  841. else
  842. {
  843. int new_end;
  844. int i;
  845. GList *e;
  846. new_end = str_get_prev_char (input->buffer->str + end) - input->buffer->str;
  847. for (i = 0, e = listbox_get_first_link (LISTBOX (g->current->data));
  848. e != NULL; i++, e = g_list_next (e))
  849. {
  850. WLEntry *le = LENTRY (e->data);
  851. if (strncmp (input->buffer->str + start, le->text, new_end - start) == 0)
  852. {
  853. listbox_set_current (LISTBOX (g->current->data), i);
  854. end = new_end;
  855. input_handle_char (input, parm);
  856. widget_draw (WIDGET (g->current->data));
  857. break;
  858. }
  859. }
  860. }
  861. return MSG_HANDLED;
  862. default:
  863. if (parm < 32 || parm > 255)
  864. {
  865. bl = 0;
  866. if (widget_lookup_key (WIDGET (input), parm) != CK_Complete)
  867. return MSG_NOT_HANDLED;
  868. if (end == min_end)
  869. return MSG_HANDLED;
  870. /* This means we want to refill the list box and start again */
  871. h->ret_value = B_USER;
  872. dlg_close (h);
  873. }
  874. else
  875. {
  876. static char buff[MB_LEN_MAX] = "";
  877. GList *e;
  878. int i;
  879. int need_redraw = 0;
  880. int low = 4096;
  881. char *last_text = NULL;
  882. buff[bl++] = (char) parm;
  883. buff[bl] = '\0';
  884. switch (str_is_valid_char (buff, bl))
  885. {
  886. case -1:
  887. bl = 0;
  888. MC_FALLTHROUGH;
  889. case -2:
  890. return MSG_HANDLED;
  891. default:
  892. break;
  893. }
  894. for (i = 0, e = listbox_get_first_link (LISTBOX (g->current->data));
  895. e != NULL; i++, e = g_list_next (e))
  896. {
  897. WLEntry *le = LENTRY (e->data);
  898. if (strncmp (input->buffer->str + start, le->text, end - start) == 0
  899. && strncmp (le->text + end - start, buff, bl) == 0)
  900. {
  901. if (need_redraw == 0)
  902. {
  903. need_redraw = 1;
  904. listbox_set_current (LISTBOX (g->current->data), i);
  905. last_text = le->text;
  906. }
  907. else
  908. {
  909. char *si, *sl;
  910. int si_num = 0;
  911. int sl_num = 0;
  912. /* count symbols between start and end */
  913. for (si = le->text + start; si < le->text + end;
  914. str_next_char (&si), si_num++)
  915. ;
  916. for (sl = last_text + start; sl < last_text + end;
  917. str_next_char (&sl), sl_num++)
  918. ;
  919. /* pointers to next symbols */
  920. si = &le->text[str_offset_to_pos (le->text, ++si_num)];
  921. sl = &last_text[str_offset_to_pos (last_text, ++sl_num)];
  922. while (si[0] != '\0' && sl[0] != '\0')
  923. {
  924. char *nexti, *nextl;
  925. nexti = str_get_next_char (si);
  926. nextl = str_get_next_char (sl);
  927. if (nexti - si != nextl - sl || strncmp (si, sl, nexti - si) != 0)
  928. break;
  929. si = nexti;
  930. sl = nextl;
  931. si_num++;
  932. }
  933. last_text = le->text;
  934. si = &last_text[str_offset_to_pos (last_text, si_num)];
  935. if (low > si - last_text)
  936. low = si - last_text;
  937. need_redraw = 2;
  938. }
  939. }
  940. }
  941. if (need_redraw == 2)
  942. {
  943. insert_text (input, last_text, low);
  944. widget_draw (WIDGET (g->current->data));
  945. }
  946. else if (need_redraw == 1)
  947. {
  948. h->ret_value = B_ENTER;
  949. dlg_close (h);
  950. }
  951. bl = 0;
  952. }
  953. }
  954. return MSG_HANDLED;
  955. default:
  956. return dlg_default_callback (w, sender, msg, parm, data);
  957. }
  958. }
  959. /* --------------------------------------------------------------------------------------------- */
  960. /** Returns TRUE if the user would like to see us again */
  961. static gboolean
  962. complete_engine (WInput *in, int what_to_do)
  963. {
  964. if (in->completions != NULL && str_offset_to_pos (in->buffer->str, in->point) != end)
  965. input_complete_free (in);
  966. if (in->completions == NULL)
  967. complete_engine_fill_completions (in);
  968. if (in->completions == NULL)
  969. tty_beep ();
  970. else
  971. {
  972. if ((what_to_do & DO_INSERTION) != 0
  973. || ((what_to_do & DO_QUERY) != 0 && in->completions->len == 1))
  974. {
  975. const char *lc_complete;
  976. lc_complete = g_ptr_array_index (in->completions, 0);
  977. if (!insert_text (in, lc_complete, -1) || in->completions->len > 1)
  978. tty_beep ();
  979. else
  980. input_complete_free (in);
  981. }
  982. if ((what_to_do & DO_QUERY) != 0 && in->completions != NULL && in->completions->len > 1)
  983. {
  984. int maxlen = 0;
  985. int i;
  986. size_t k;
  987. int count;
  988. int x, y, w, h;
  989. int start_x, start_y;
  990. char *q;
  991. WDialog *complete_dlg;
  992. WListbox *complete_list;
  993. for (k = 1; k < in->completions->len; k++)
  994. {
  995. q = g_ptr_array_index (in->completions, k);
  996. i = str_term_width1 (q);
  997. maxlen = MAX (maxlen, i);
  998. }
  999. count = in->completions->len - 1;
  1000. start_x = WIDGET (in)->rect.x;
  1001. start_y = WIDGET (in)->rect.y;
  1002. if (start_y - 2 >= count)
  1003. {
  1004. y = start_y - 2 - count;
  1005. h = 2 + count;
  1006. }
  1007. else if (start_y >= LINES - start_y - 1)
  1008. {
  1009. y = 0;
  1010. h = start_y;
  1011. }
  1012. else
  1013. {
  1014. y = start_y + 1;
  1015. h = LINES - start_y - 1;
  1016. }
  1017. x = start - in->term_first_shown - 2 + start_x;
  1018. w = maxlen + 4;
  1019. if (x + w > COLS)
  1020. x = COLS - w;
  1021. if (x < 0)
  1022. x = 0;
  1023. if (x + w > COLS)
  1024. w = COLS;
  1025. input = in;
  1026. min_end = end;
  1027. complete_dlg =
  1028. dlg_create (TRUE, y, x, h, w, WPOS_KEEP_DEFAULT, TRUE,
  1029. dialog_colors, complete_callback, NULL, "[Completion]", NULL);
  1030. complete_list = listbox_new (1, 1, h - 2, w - 2, FALSE, NULL);
  1031. group_add_widget (GROUP (complete_dlg), complete_list);
  1032. for (k = 1; k < in->completions->len; k++)
  1033. {
  1034. q = g_ptr_array_index (in->completions, k);
  1035. listbox_add_item (complete_list, LISTBOX_APPEND_AT_END, 0, q, NULL, FALSE);
  1036. }
  1037. i = dlg_run (complete_dlg);
  1038. q = NULL;
  1039. if (i == B_ENTER)
  1040. {
  1041. listbox_get_current (complete_list, &q, NULL);
  1042. if (q != NULL)
  1043. insert_text (in, q, -1);
  1044. }
  1045. if (q != NULL || end != min_end)
  1046. input_complete_free (in);
  1047. widget_destroy (WIDGET (complete_dlg));
  1048. /* B_USER if user wants to start over again */
  1049. return (i == B_USER);
  1050. }
  1051. }
  1052. return FALSE;
  1053. }
  1054. /* --------------------------------------------------------------------------------------------- */
  1055. /*** public functions ****************************************************************************/
  1056. /* --------------------------------------------------------------------------------------------- */
  1057. /** Returns an array of matches, or NULL if none. */
  1058. GPtrArray *
  1059. try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
  1060. {
  1061. try_complete_automation_state_t state;
  1062. GPtrArray *matches = NULL;
  1063. memset (&state, 0, sizeof (state));
  1064. state.flags = flags;
  1065. SHOW_C_CTX ("try_complete");
  1066. state.word = g_strndup (text + *lc_start, *lc_end - *lc_start);
  1067. state.is_cd = check_is_cd (text, *lc_start, state.flags);
  1068. /* Determine if this could be a command word. It is if it appears at
  1069. the start of the line (ignoring preceding whitespace), or if it
  1070. appears after a character that separates commands. And we have to
  1071. be in a INPUT_COMPLETE_COMMANDS flagged Input line. */
  1072. if (!state.is_cd && (flags & INPUT_COMPLETE_COMMANDS) != 0)
  1073. try_complete_commands_prepare (&state, text, lc_start);
  1074. try_complete_find_start_sign (&state);
  1075. /* Command substitution? */
  1076. if (state.p > state.q && state.p > state.r)
  1077. {
  1078. SHOW_C_CTX ("try_complete:cmd_backq_subst");
  1079. matches = completion_matches (str_cget_next_char (state.p),
  1080. command_completion_function,
  1081. state.flags & (~INPUT_COMPLETE_FILENAMES));
  1082. if (matches != NULL)
  1083. *lc_start += str_get_next_char (state.p) - state.word;
  1084. }
  1085. /* Variable name? */
  1086. else if (state.q > state.p && state.q > state.r)
  1087. {
  1088. SHOW_C_CTX ("try_complete:var_subst");
  1089. matches = completion_matches (state.q, variable_completion_function, state.flags);
  1090. if (matches != NULL)
  1091. *lc_start += state.q - state.word;
  1092. }
  1093. /* Starts with '@', then look through the known hostnames for
  1094. completion first. */
  1095. else if (state.r > state.p && state.r > state.q)
  1096. {
  1097. SHOW_C_CTX ("try_complete:host_subst");
  1098. matches = completion_matches (state.r, hostname_completion_function, state.flags);
  1099. if (matches != NULL)
  1100. *lc_start += state.r - state.word;
  1101. }
  1102. /* Starts with '~' and there is no slash in the word, then
  1103. try completing this word as a username. */
  1104. if (matches == NULL && *state.word == '~' && (state.flags & INPUT_COMPLETE_USERNAMES) != 0
  1105. && strchr (state.word, PATH_SEP) == NULL)
  1106. {
  1107. SHOW_C_CTX ("try_complete:user_subst");
  1108. matches = completion_matches (state.word, username_completion_function, state.flags);
  1109. }
  1110. /* If this word is in a command position, then
  1111. complete over possible command names, including aliases, functions,
  1112. and command names. */
  1113. if (matches == NULL)
  1114. matches = try_complete_all_possible (&state, text, lc_start);
  1115. /* And finally if nothing found, try complete directory name */
  1116. if (matches == NULL)
  1117. {
  1118. state.in_command_position = 0;
  1119. matches = try_complete_all_possible (&state, text, lc_start);
  1120. }
  1121. g_free (state.word);
  1122. if (matches != NULL && (flags & INPUT_COMPLETE_FILENAMES) != 0 &&
  1123. (flags & INPUT_COMPLETE_SHELL_ESC) == 0)
  1124. {
  1125. /* FIXME: HACK? INPUT_COMPLETE_SHELL_ESC is used only in command line. */
  1126. size_t i;
  1127. for (i = 0; i < matches->len; i++)
  1128. {
  1129. char *p;
  1130. p = g_ptr_array_index (matches, i);
  1131. /* Escape only '?', '*', and '&' symbols as described in the
  1132. manual page (see a11995e12b88285e044f644904c306ed6c342ad0). */
  1133. g_ptr_array_index (matches, i) = str_escape (p, -1, "?*&", TRUE);
  1134. g_free (p);
  1135. }
  1136. }
  1137. return matches;
  1138. }
  1139. /* --------------------------------------------------------------------------------------------- */
  1140. void
  1141. complete_engine_fill_completions (WInput *in)
  1142. {
  1143. char *s;
  1144. const char *word_separators;
  1145. word_separators = (in->completion_flags & INPUT_COMPLETE_SHELL_ESC) ? " \t;|<>" : "\t;|<>";
  1146. end = str_offset_to_pos (in->buffer->str, in->point);
  1147. s = in->buffer->str;
  1148. if (in->point != 0)
  1149. {
  1150. /* get symbol before in->point */
  1151. size_t i;
  1152. for (i = in->point - 1; i > 0; i--)
  1153. str_next_char (&s);
  1154. }
  1155. for (; s >= in->buffer->str; str_prev_char (&s))
  1156. {
  1157. start = s - in->buffer->str;
  1158. if (strchr (word_separators, *s) != NULL && !str_is_char_escaped (in->buffer->str, s))
  1159. break;
  1160. }
  1161. if (start < end)
  1162. {
  1163. str_next_char (&s);
  1164. start = s - in->buffer->str;
  1165. }
  1166. in->completions = try_complete (in->buffer->str, &start, &end, in->completion_flags);
  1167. }
  1168. /* --------------------------------------------------------------------------------------------- */
  1169. /* declared in lib/widget/input.h */
  1170. void
  1171. input_complete (WInput *in)
  1172. {
  1173. int engine_flags;
  1174. if (!str_is_valid_string (in->buffer->str))
  1175. return;
  1176. if (in->completions != NULL)
  1177. engine_flags = DO_QUERY;
  1178. else
  1179. {
  1180. engine_flags = DO_INSERTION;
  1181. if (mc_global.widget.show_all_if_ambiguous)
  1182. engine_flags |= DO_QUERY;
  1183. }
  1184. while (complete_engine (in, engine_flags))
  1185. ;
  1186. }
  1187. /* --------------------------------------------------------------------------------------------- */
  1188. void
  1189. input_complete_free (WInput *in)
  1190. {
  1191. if (in->completions != NULL)
  1192. {
  1193. g_ptr_array_free (in->completions, TRUE);
  1194. in->completions = NULL;
  1195. }
  1196. }
  1197. /* --------------------------------------------------------------------------------------------- */