input_complete.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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) \
  52. fprintf (stderr, "%s: text='%s' flags=%s\n", func, text, show_c_flags (flags))
  53. #else
  54. #define SHOW_C_CTX(func)
  55. #endif /* DO_CMPLETION_DEBUG */
  56. #define DO_INSERTION 1
  57. #define DO_QUERY 2
  58. /*** file scope type declarations ****************************************************************/
  59. typedef char *CompletionFunction (const char *text, int state, input_complete_t flags);
  60. typedef struct
  61. {
  62. size_t in_command_position;
  63. char *word;
  64. char *p;
  65. char *q;
  66. char *r;
  67. gboolean is_cd;
  68. input_complete_t flags;
  69. } try_complete_automation_state_t;
  70. /*** forward declarations (file scope functions) *************************************************/
  71. GPtrArray *try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags);
  72. void complete_engine_fill_completions (WInput *in);
  73. /*** file scope variables ************************************************************************/
  74. static WInput *input;
  75. static int min_end;
  76. static int start = 0;
  77. static int end = 0;
  78. /* --------------------------------------------------------------------------------------------- */
  79. /*** file scope functions ************************************************************************/
  80. /* --------------------------------------------------------------------------------------------- */
  81. #ifdef DO_COMPLETION_DEBUG
  82. /**
  83. * Useful to print/debug completion flags
  84. */
  85. static const char *
  86. show_c_flags (input_complete_t flags)
  87. {
  88. static char s_cf[] = "FHCVUDS";
  89. s_cf[0] = (flags & INPUT_COMPLETE_FILENAMES) != 0 ? 'F' : ' ';
  90. s_cf[1] = (flags & INPUT_COMPLETE_HOSTNAMES) != 0 ? 'H' : ' ';
  91. s_cf[2] = (flags & INPUT_COMPLETE_COMMANDS) != 0 ? 'C' : ' ';
  92. s_cf[3] = (flags & INPUT_COMPLETE_VARIABLES) != 0 ? 'V' : ' ';
  93. s_cf[4] = (flags & INPUT_COMPLETE_USERNAMES) != 0 ? 'U' : ' ';
  94. s_cf[5] = (flags & INPUT_COMPLETE_CD) != 0 ? 'D' : ' ';
  95. s_cf[6] = (flags & INPUT_COMPLETE_SHELL_ESC) != 0 ? 'S' : ' ';
  96. return s_cf;
  97. }
  98. #endif /* DO_CMPLETION_DEBUG */
  99. /* --------------------------------------------------------------------------------------------- */
  100. static char *
  101. filename_completion_function (const char *text, int state, input_complete_t flags)
  102. {
  103. static DIR *directory = NULL;
  104. static char *filename = NULL;
  105. static char *dirname = NULL;
  106. static char *users_dirname = NULL;
  107. static size_t filename_len = 0;
  108. static vfs_path_t *dirname_vpath = NULL;
  109. gboolean isdir = TRUE, isexec = FALSE;
  110. struct vfs_dirent *entry = NULL;
  111. SHOW_C_CTX ("filename_completion_function");
  112. if (text != NULL && (flags & INPUT_COMPLETE_SHELL_ESC) != 0)
  113. {
  114. char *u_text;
  115. char *result;
  116. char *e_result;
  117. u_text = str_shell_unescape (text);
  118. result = filename_completion_function (u_text, state, flags & (~INPUT_COMPLETE_SHELL_ESC));
  119. g_free (u_text);
  120. e_result = str_shell_escape (result);
  121. g_free (result);
  122. return e_result;
  123. }
  124. /* If we're starting the match process, initialize us a bit. */
  125. if (state == 0)
  126. {
  127. const char *temp;
  128. g_free (dirname);
  129. g_free (filename);
  130. g_free (users_dirname);
  131. vfs_path_free (dirname_vpath, TRUE);
  132. if ((*text != '\0') && (temp = strrchr (text, PATH_SEP)) != NULL)
  133. {
  134. filename = g_strdup (++temp);
  135. dirname = g_strndup (text, temp - text);
  136. }
  137. else
  138. {
  139. dirname = g_strdup (".");
  140. filename = g_strdup (text);
  141. }
  142. /* We aren't done yet. We also support the "~user" syntax. */
  143. /* Save the version of the directory that the user typed. */
  144. users_dirname = dirname;
  145. dirname = tilde_expand (dirname);
  146. canonicalize_pathname (dirname);
  147. dirname_vpath = vfs_path_from_str (dirname);
  148. /* Here we should do something with variable expansion
  149. and `command`.
  150. Maybe a dream - UNIMPLEMENTED yet. */
  151. directory = mc_opendir (dirname_vpath);
  152. filename_len = strlen (filename);
  153. }
  154. /* Now that we have some state, we can read the directory. */
  155. while (directory != NULL && (entry = mc_readdir (directory)) != NULL)
  156. {
  157. if (!str_is_valid_string (entry->d_name))
  158. continue;
  159. /* Special case for no filename.
  160. All entries except "." and ".." match. */
  161. if (filename_len == 0)
  162. {
  163. if (DIR_IS_DOT (entry->d_name) || DIR_IS_DOTDOT (entry->d_name))
  164. continue;
  165. }
  166. else
  167. {
  168. /* Otherwise, if these match up to the length of filename, then
  169. it may be a match. */
  170. if (entry->d_name[0] != filename[0] || entry->d_len < filename_len
  171. || strncmp (filename, entry->d_name, filename_len) != 0)
  172. continue;
  173. }
  174. isdir = TRUE;
  175. isexec = FALSE;
  176. {
  177. struct stat tempstat;
  178. vfs_path_t *tmp_vpath;
  179. tmp_vpath = vfs_path_build_filename (dirname, entry->d_name, (char *) NULL);
  180. /* Unix version */
  181. if (mc_stat (tmp_vpath, &tempstat) == 0)
  182. {
  183. uid_t my_uid;
  184. gid_t my_gid;
  185. my_uid = getuid ();
  186. my_gid = getgid ();
  187. if (!S_ISDIR (tempstat.st_mode))
  188. {
  189. isdir = FALSE;
  190. if ((my_uid == 0 && (tempstat.st_mode & 0111) != 0)
  191. || (my_uid == tempstat.st_uid && (tempstat.st_mode & 0100) != 0)
  192. || (my_gid == tempstat.st_gid && (tempstat.st_mode & 0010) != 0)
  193. || (tempstat.st_mode & 0001) != 0)
  194. isexec = TRUE;
  195. }
  196. }
  197. else
  198. {
  199. /* stat failed, strange. not a dir in any case */
  200. isdir = FALSE;
  201. }
  202. vfs_path_free (tmp_vpath, TRUE);
  203. }
  204. if ((flags & INPUT_COMPLETE_COMMANDS) != 0 && (isexec || isdir))
  205. break;
  206. if ((flags & INPUT_COMPLETE_CD) != 0 && isdir)
  207. break;
  208. if ((flags & INPUT_COMPLETE_FILENAMES) != 0)
  209. break;
  210. }
  211. if (entry == NULL)
  212. {
  213. if (directory != NULL)
  214. {
  215. mc_closedir (directory);
  216. directory = NULL;
  217. }
  218. MC_PTR_FREE (dirname);
  219. vfs_path_free (dirname_vpath, TRUE);
  220. dirname_vpath = NULL;
  221. MC_PTR_FREE (filename);
  222. MC_PTR_FREE (users_dirname);
  223. return NULL;
  224. }
  225. {
  226. GString *temp;
  227. temp = g_string_sized_new (16);
  228. if (users_dirname != NULL && !DIR_IS_DOT (users_dirname))
  229. {
  230. g_string_append (temp, users_dirname);
  231. /* We need a '/' at the end. */
  232. if (!IS_PATH_SEP (temp->str[temp->len - 1]))
  233. g_string_append_c (temp, PATH_SEP);
  234. }
  235. g_string_append_len (temp, entry->d_name, entry->d_len);
  236. if (isdir)
  237. g_string_append_c (temp, PATH_SEP);
  238. return g_string_free (temp, FALSE);
  239. }
  240. }
  241. /* --------------------------------------------------------------------------------------------- */
  242. /** We assume here that text[0] == '~' , if you want to call it in another way,
  243. you have to change the code */
  244. static char *
  245. username_completion_function (const char *text, int state, input_complete_t flags)
  246. {
  247. static struct passwd *entry = NULL;
  248. static size_t userlen = 0;
  249. (void) flags;
  250. SHOW_C_CTX ("username_completion_function");
  251. if (text[0] == '\\' && text[1] == '~')
  252. text++;
  253. if (state == 0)
  254. { /* Initialization stuff */
  255. setpwent ();
  256. userlen = strlen (text + 1);
  257. }
  258. while ((entry = getpwent ()) != NULL)
  259. {
  260. /* Null usernames should result in all users as possible completions. */
  261. if (userlen == 0)
  262. break;
  263. if (text[1] == entry->pw_name[0] && strncmp (text + 1, entry->pw_name, userlen) == 0)
  264. break;
  265. }
  266. if (entry != NULL)
  267. return g_strconcat ("~", entry->pw_name, PATH_SEP_STR, (char *) NULL);
  268. endpwent ();
  269. return NULL;
  270. }
  271. /* --------------------------------------------------------------------------------------------- */
  272. /** We assume text [0] == '$' and want to have a look at text [1], if it is
  273. equal to '{', so that we should append '}' at the end */
  274. static char *
  275. variable_completion_function (const char *text, int state, input_complete_t flags)
  276. {
  277. static char **env_p = NULL;
  278. static gboolean isbrace = FALSE;
  279. static size_t varlen = 0;
  280. const char *p = NULL;
  281. (void) flags;
  282. SHOW_C_CTX ("variable_completion_function");
  283. if (state == 0)
  284. { /* Initialization stuff */
  285. isbrace = (text[1] == '{');
  286. varlen = strlen (text + 1 + isbrace);
  287. env_p = environ;
  288. }
  289. while (*env_p != NULL)
  290. {
  291. p = strchr (*env_p, '=');
  292. if (p != NULL && ((size_t) (p - *env_p) >= varlen)
  293. && strncmp (text + 1 + isbrace, *env_p, varlen) == 0)
  294. break;
  295. env_p++;
  296. }
  297. if (*env_p == NULL)
  298. return NULL;
  299. {
  300. GString *temp;
  301. temp = g_string_new_len (*env_p, p - *env_p);
  302. if (isbrace)
  303. {
  304. g_string_prepend_c (temp, '{');
  305. g_string_append_c (temp, '}');
  306. }
  307. g_string_prepend_c (temp, '$');
  308. env_p++;
  309. return g_string_free (temp, FALSE);
  310. }
  311. }
  312. /* --------------------------------------------------------------------------------------------- */
  313. static gboolean
  314. host_equal_func (gconstpointer a, gconstpointer b)
  315. {
  316. return (strcmp ((const char *) a, (const char *) b) == 0);
  317. }
  318. /* --------------------------------------------------------------------------------------------- */
  319. static void
  320. fetch_hosts (const char *filename, GPtrArray *hosts)
  321. {
  322. FILE *file;
  323. char buffer[BUF_MEDIUM];
  324. char *bi;
  325. file = fopen (filename, "r");
  326. if (file == NULL)
  327. return;
  328. while (fgets (buffer, sizeof (buffer) - 1, file) != NULL)
  329. {
  330. /* Skip to first character. */
  331. for (bi = buffer; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi))
  332. ;
  333. /* Ignore comments... */
  334. if (bi[0] == '#')
  335. continue;
  336. /* Handle $include. */
  337. if (strncmp (bi, "$include ", 9) == 0)
  338. {
  339. char *includefile, *t;
  340. /* Find start of filename. */
  341. for (includefile = bi + 9; includefile[0] != '\0' && whitespace (includefile[0]);
  342. includefile++)
  343. ;
  344. t = includefile;
  345. /* Find end of filename. */
  346. for (; t[0] != '\0' && !str_isspace (t); str_next_char (&t))
  347. ;
  348. *t = '\0';
  349. fetch_hosts (includefile, hosts);
  350. continue;
  351. }
  352. /* Skip IP #s. */
  353. for (; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi))
  354. ;
  355. /* Get the host names separated by white space. */
  356. while (bi[0] != '\0' && bi[0] != '#')
  357. {
  358. char *lc_start, *name;
  359. for (; bi[0] != '\0' && str_isspace (bi); str_next_char (&bi))
  360. ;
  361. if (bi[0] == '#')
  362. continue;
  363. for (lc_start = bi; bi[0] != '\0' && !str_isspace (bi); str_next_char (&bi))
  364. ;
  365. if (bi == lc_start)
  366. continue;
  367. name = g_strndup (lc_start, bi - lc_start);
  368. if (!g_ptr_array_find_with_equal_func (hosts, name, host_equal_func, NULL))
  369. g_ptr_array_add (hosts, name);
  370. else
  371. g_free (name);
  372. }
  373. }
  374. fclose (file);
  375. }
  376. /* --------------------------------------------------------------------------------------------- */
  377. static char *
  378. hostname_completion_function (const char *text, int state, input_complete_t flags)
  379. {
  380. static GPtrArray *hosts = NULL;
  381. static unsigned int host_p = 0;
  382. static size_t textstart = 0;
  383. static size_t textlen = 0;
  384. (void) flags;
  385. SHOW_C_CTX ("hostname_completion_function");
  386. if (state == 0)
  387. { /* Initialization stuff */
  388. const char *p;
  389. if (hosts != NULL)
  390. g_ptr_array_free (hosts, TRUE);
  391. hosts = g_ptr_array_new_with_free_func (g_free);
  392. p = getenv ("HOSTFILE");
  393. fetch_hosts (p != NULL ? p : "/etc/hosts", hosts);
  394. host_p = 0;
  395. textstart = (*text == '@') ? 1 : 0;
  396. textlen = strlen (text + textstart);
  397. }
  398. for (; host_p < hosts->len; host_p++)
  399. {
  400. if (textlen == 0)
  401. break; /* Match all of them */
  402. if (strncmp (text + textstart, g_ptr_array_index (hosts, host_p), textlen) == 0)
  403. break;
  404. }
  405. if (host_p == hosts->len)
  406. {
  407. g_ptr_array_free (hosts, TRUE);
  408. hosts = NULL;
  409. return NULL;
  410. }
  411. {
  412. GString *temp;
  413. temp = g_string_sized_new (8);
  414. if (textstart != 0)
  415. g_string_append_c (temp, '@');
  416. g_string_append (temp, g_ptr_array_index (hosts, host_p));
  417. host_p++;
  418. return g_string_free (temp, FALSE);
  419. }
  420. }
  421. /* --------------------------------------------------------------------------------------------- */
  422. /**
  423. * This is the function to call when the word to complete is in a position
  424. * where a command word can be found. It looks around $PATH, looking for
  425. * commands that match. It also scans aliases, function names, and the
  426. * table of shell built-ins.
  427. */
  428. static char *
  429. command_completion_function (const char *text, int state, input_complete_t flags)
  430. {
  431. static const char *path_end = NULL;
  432. static gboolean isabsolute = FALSE;
  433. static int phase = 0;
  434. static size_t text_len = 0;
  435. static const char *const *words = NULL;
  436. static char *path = NULL;
  437. static char *cur_path = NULL;
  438. static char *cur_word = NULL;
  439. static int init_state = 0;
  440. static const char *const bash_reserved[] = { "if", "then", "else", "elif",
  441. "fi", "case", "esac", "for",
  442. "select", "while", "until", "do",
  443. "done", "in", "function", 0 };
  444. static const char *const bash_builtins[] = {
  445. "alias", "bg", "bind", "break", "builtin", "cd", "command", "continue",
  446. "declare", "dirs", "echo", "enable", "eval", "exec", "exit", "export",
  447. "fc", "fg", "getopts", "hash", "help", "history", "jobs", "kill",
  448. "let", "local", "logout", "popd", "pushd", "pwd", "read", "readonly",
  449. "return", "set", "shift", "source", "suspend", "test", "times", "trap",
  450. "type", "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 == '>')
  673. || (ti != text && 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 = completion_matches (state->word, command_completion_function,
  715. state->flags & (~INPUT_COMPLETE_FILENAMES));
  716. }
  717. else if ((state->flags & INPUT_COMPLETE_FILENAMES) != 0)
  718. {
  719. if (state->is_cd)
  720. state->flags &= ~(INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS);
  721. SHOW_C_CTX ("try_complete:filename_subst_1");
  722. matches = completion_matches (state->word, filename_completion_function, state->flags);
  723. if (matches == NULL && state->is_cd && !IS_PATH_SEP (*state->word) && *state->word != '~')
  724. {
  725. state->q = text + *lc_start;
  726. for (state->p = text;
  727. *state->p != '\0' && state->p < state->q && whitespace (*state->p);
  728. str_next_char (&state->p))
  729. ;
  730. if (strncmp (state->p, "cd", 2) == 0)
  731. for (state->p += 2;
  732. *state->p != '\0' && state->p < state->q && whitespace (*state->p);
  733. str_next_char (&state->p))
  734. ;
  735. if (state->p == state->q)
  736. {
  737. char *cdpath_ref, *cdpath;
  738. char c;
  739. cdpath_ref = g_strdup (getenv ("CDPATH"));
  740. cdpath = cdpath_ref;
  741. c = (cdpath == NULL) ? '\0' : ':';
  742. while (matches == NULL && c == ':')
  743. {
  744. char *s;
  745. s = strchr (cdpath, ':');
  746. /* cppcheck-suppress nullPointer */
  747. if (s == NULL)
  748. s = strchr (cdpath, '\0');
  749. c = *s;
  750. *s = '\0';
  751. if (*cdpath != '\0')
  752. {
  753. state->r = mc_build_filename (cdpath, state->word, (char *) NULL);
  754. SHOW_C_CTX ("try_complete:filename_subst_2");
  755. matches = completion_matches (state->r, filename_completion_function,
  756. state->flags);
  757. g_free (state->r);
  758. }
  759. *s = c;
  760. cdpath = str_get_next_char (s);
  761. }
  762. g_free (cdpath_ref);
  763. }
  764. }
  765. }
  766. return matches;
  767. }
  768. /* --------------------------------------------------------------------------------------------- */
  769. static gboolean
  770. insert_text (WInput *in, const char *text, ssize_t size)
  771. {
  772. size_t text_len;
  773. int buff_len;
  774. ssize_t new_size;
  775. text_len = strlen (text);
  776. buff_len = str_length (in->buffer->str);
  777. if (size < 0)
  778. size = (ssize_t) text_len;
  779. else
  780. size = MIN (size, (ssize_t) text_len);
  781. new_size = size + start - end;
  782. if (new_size != 0)
  783. {
  784. /* make a hole within buffer */
  785. size_t tail_len;
  786. tail_len = in->buffer->len - end;
  787. if (tail_len != 0)
  788. {
  789. char *tail;
  790. size_t hole_end;
  791. tail = g_strndup (in->buffer->str + end, tail_len);
  792. hole_end = end + new_size;
  793. if (in->buffer->len < hole_end)
  794. g_string_set_size (in->buffer, hole_end + tail_len);
  795. g_string_overwrite_len (in->buffer, hole_end, tail, tail_len);
  796. g_free (tail);
  797. }
  798. }
  799. g_string_overwrite_len (in->buffer, start, text, size);
  800. in->point += str_length (in->buffer->str) - buff_len;
  801. input_update (in, TRUE);
  802. end += new_size;
  803. return new_size != 0;
  804. }
  805. /* --------------------------------------------------------------------------------------------- */
  806. static cb_ret_t
  807. complete_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  808. {
  809. static int bl = 0;
  810. WGroup *g = GROUP (w);
  811. WDialog *h = DIALOG (w);
  812. switch (msg)
  813. {
  814. case MSG_KEY:
  815. switch (parm)
  816. {
  817. case KEY_LEFT:
  818. case KEY_RIGHT:
  819. bl = 0;
  820. h->ret_value = 0;
  821. dlg_close (h);
  822. return MSG_HANDLED;
  823. case KEY_BACKSPACE:
  824. bl = 0;
  825. /* exit from completion list if input line is empty */
  826. if (end == 0)
  827. {
  828. h->ret_value = 0;
  829. dlg_close (h);
  830. }
  831. /* Refill the list box and start again */
  832. else if (end == min_end)
  833. {
  834. end = str_get_prev_char (input->buffer->str + end) - input->buffer->str;
  835. input_handle_char (input, parm);
  836. h->ret_value = B_USER;
  837. dlg_close (h);
  838. }
  839. else
  840. {
  841. int new_end;
  842. int i;
  843. GList *e;
  844. new_end = str_get_prev_char (input->buffer->str + end) - input->buffer->str;
  845. for (i = 0, e = listbox_get_first_link (LISTBOX (g->current->data)); e != NULL;
  846. i++, e = g_list_next (e))
  847. {
  848. WLEntry *le = LENTRY (e->data);
  849. if (strncmp (input->buffer->str + start, le->text, new_end - start) == 0)
  850. {
  851. listbox_set_current (LISTBOX (g->current->data), i);
  852. end = new_end;
  853. input_handle_char (input, parm);
  854. widget_draw (WIDGET (g->current->data));
  855. break;
  856. }
  857. }
  858. }
  859. return MSG_HANDLED;
  860. default:
  861. if (parm < 32 || parm > 255)
  862. {
  863. bl = 0;
  864. if (widget_lookup_key (WIDGET (input), parm) != CK_Complete)
  865. return MSG_NOT_HANDLED;
  866. if (end == min_end)
  867. return MSG_HANDLED;
  868. /* This means we want to refill the list box and start again */
  869. h->ret_value = B_USER;
  870. dlg_close (h);
  871. }
  872. else
  873. {
  874. static char buff[MB_LEN_MAX] = "";
  875. GList *e;
  876. int i;
  877. int need_redraw = 0;
  878. int low = 4096;
  879. char *last_text = NULL;
  880. buff[bl++] = (char) parm;
  881. buff[bl] = '\0';
  882. switch (str_is_valid_char (buff, bl))
  883. {
  884. case -1:
  885. bl = 0;
  886. MC_FALLTHROUGH;
  887. case -2:
  888. return MSG_HANDLED;
  889. default:
  890. break;
  891. }
  892. for (i = 0, e = listbox_get_first_link (LISTBOX (g->current->data)); e != NULL;
  893. i++, e = g_list_next (e))
  894. {
  895. WLEntry *le = LENTRY (e->data);
  896. if (strncmp (input->buffer->str + start, le->text, end - start) == 0
  897. && strncmp (le->text + end - start, buff, bl) == 0)
  898. {
  899. if (need_redraw == 0)
  900. {
  901. need_redraw = 1;
  902. listbox_set_current (LISTBOX (g->current->data), i);
  903. last_text = le->text;
  904. }
  905. else
  906. {
  907. char *si, *sl;
  908. int si_num = 0;
  909. int sl_num = 0;
  910. /* count symbols between start and end */
  911. for (si = le->text + start; si < le->text + end;
  912. str_next_char (&si), si_num++)
  913. ;
  914. for (sl = last_text + start; sl < last_text + end;
  915. str_next_char (&sl), sl_num++)
  916. ;
  917. /* pointers to next symbols */
  918. si = &le->text[str_offset_to_pos (le->text, ++si_num)];
  919. sl = &last_text[str_offset_to_pos (last_text, ++sl_num)];
  920. while (si[0] != '\0' && sl[0] != '\0')
  921. {
  922. char *nexti, *nextl;
  923. nexti = str_get_next_char (si);
  924. nextl = str_get_next_char (sl);
  925. if (nexti - si != nextl - sl || strncmp (si, sl, nexti - si) != 0)
  926. break;
  927. si = nexti;
  928. sl = nextl;
  929. si_num++;
  930. }
  931. last_text = le->text;
  932. si = &last_text[str_offset_to_pos (last_text, si_num)];
  933. if (low > si - last_text)
  934. low = si - last_text;
  935. need_redraw = 2;
  936. }
  937. }
  938. }
  939. if (need_redraw == 2)
  940. {
  941. insert_text (input, last_text, low);
  942. widget_draw (WIDGET (g->current->data));
  943. }
  944. else if (need_redraw == 1)
  945. {
  946. h->ret_value = B_ENTER;
  947. dlg_close (h);
  948. }
  949. bl = 0;
  950. }
  951. }
  952. return MSG_HANDLED;
  953. default:
  954. return dlg_default_callback (w, sender, msg, parm, data);
  955. }
  956. }
  957. /* --------------------------------------------------------------------------------------------- */
  958. /** Returns TRUE if the user would like to see us again */
  959. static gboolean
  960. complete_engine (WInput *in, int what_to_do)
  961. {
  962. if (in->completions != NULL && str_offset_to_pos (in->buffer->str, in->point) != end)
  963. input_complete_free (in);
  964. if (in->completions == NULL)
  965. complete_engine_fill_completions (in);
  966. if (in->completions == NULL)
  967. tty_beep ();
  968. else
  969. {
  970. if ((what_to_do & DO_INSERTION) != 0
  971. || ((what_to_do & DO_QUERY) != 0 && in->completions->len == 1))
  972. {
  973. const char *lc_complete;
  974. lc_complete = g_ptr_array_index (in->completions, 0);
  975. if (!insert_text (in, lc_complete, -1) || in->completions->len > 1)
  976. tty_beep ();
  977. else
  978. input_complete_free (in);
  979. }
  980. if ((what_to_do & DO_QUERY) != 0 && in->completions != NULL && in->completions->len > 1)
  981. {
  982. int maxlen = 0;
  983. int i;
  984. size_t k;
  985. int count;
  986. int x, y, w, h;
  987. int start_x, start_y;
  988. char *q;
  989. WDialog *complete_dlg;
  990. WListbox *complete_list;
  991. for (k = 1; k < in->completions->len; k++)
  992. {
  993. q = g_ptr_array_index (in->completions, k);
  994. i = str_term_width1 (q);
  995. maxlen = MAX (maxlen, i);
  996. }
  997. count = in->completions->len - 1;
  998. start_x = WIDGET (in)->rect.x;
  999. start_y = WIDGET (in)->rect.y;
  1000. if (start_y - 2 >= count)
  1001. {
  1002. y = start_y - 2 - count;
  1003. h = 2 + count;
  1004. }
  1005. else if (start_y >= LINES - start_y - 1)
  1006. {
  1007. y = 0;
  1008. h = start_y;
  1009. }
  1010. else
  1011. {
  1012. y = start_y + 1;
  1013. h = LINES - start_y - 1;
  1014. }
  1015. x = start - in->term_first_shown - 2 + start_x;
  1016. w = maxlen + 4;
  1017. if (x + w > COLS)
  1018. x = COLS - w;
  1019. if (x < 0)
  1020. x = 0;
  1021. if (x + w > COLS)
  1022. w = COLS;
  1023. input = in;
  1024. min_end = end;
  1025. complete_dlg = dlg_create (TRUE, y, x, h, w, WPOS_KEEP_DEFAULT, TRUE, dialog_colors,
  1026. complete_callback, NULL, "[Completion]", NULL);
  1027. complete_list = listbox_new (1, 1, h - 2, w - 2, FALSE, NULL);
  1028. group_add_widget (GROUP (complete_dlg), complete_list);
  1029. for (k = 1; k < in->completions->len; k++)
  1030. {
  1031. q = g_ptr_array_index (in->completions, k);
  1032. listbox_add_item (complete_list, LISTBOX_APPEND_AT_END, 0, q, NULL, FALSE);
  1033. }
  1034. i = dlg_run (complete_dlg);
  1035. q = NULL;
  1036. if (i == B_ENTER)
  1037. {
  1038. listbox_get_current (complete_list, &q, NULL);
  1039. if (q != NULL)
  1040. insert_text (in, q, -1);
  1041. }
  1042. if (q != NULL || end != min_end)
  1043. input_complete_free (in);
  1044. widget_destroy (WIDGET (complete_dlg));
  1045. /* B_USER if user wants to start over again */
  1046. return (i == B_USER);
  1047. }
  1048. }
  1049. return FALSE;
  1050. }
  1051. /* --------------------------------------------------------------------------------------------- */
  1052. /*** public functions ****************************************************************************/
  1053. /* --------------------------------------------------------------------------------------------- */
  1054. /** Returns an array of matches, or NULL if none. */
  1055. GPtrArray *
  1056. try_complete (char *text, int *lc_start, int *lc_end, input_complete_t flags)
  1057. {
  1058. try_complete_automation_state_t state;
  1059. GPtrArray *matches = NULL;
  1060. memset (&state, 0, sizeof (state));
  1061. state.flags = flags;
  1062. SHOW_C_CTX ("try_complete");
  1063. state.word = g_strndup (text + *lc_start, *lc_end - *lc_start);
  1064. state.is_cd = check_is_cd (text, *lc_start, state.flags);
  1065. /* Determine if this could be a command word. It is if it appears at
  1066. the start of the line (ignoring preceding whitespace), or if it
  1067. appears after a character that separates commands. And we have to
  1068. be in a INPUT_COMPLETE_COMMANDS flagged Input line. */
  1069. if (!state.is_cd && (flags & INPUT_COMPLETE_COMMANDS) != 0)
  1070. try_complete_commands_prepare (&state, text, lc_start);
  1071. try_complete_find_start_sign (&state);
  1072. /* Command substitution? */
  1073. if (state.p > state.q && state.p > state.r)
  1074. {
  1075. SHOW_C_CTX ("try_complete:cmd_backq_subst");
  1076. matches = completion_matches (str_cget_next_char (state.p), command_completion_function,
  1077. state.flags & (~INPUT_COMPLETE_FILENAMES));
  1078. if (matches != NULL)
  1079. *lc_start += str_get_next_char (state.p) - state.word;
  1080. }
  1081. /* Variable name? */
  1082. else if (state.q > state.p && state.q > state.r)
  1083. {
  1084. SHOW_C_CTX ("try_complete:var_subst");
  1085. matches = completion_matches (state.q, variable_completion_function, state.flags);
  1086. if (matches != NULL)
  1087. *lc_start += state.q - state.word;
  1088. }
  1089. /* Starts with '@', then look through the known hostnames for
  1090. completion first. */
  1091. else if (state.r > state.p && state.r > state.q)
  1092. {
  1093. SHOW_C_CTX ("try_complete:host_subst");
  1094. matches = completion_matches (state.r, hostname_completion_function, state.flags);
  1095. if (matches != NULL)
  1096. *lc_start += state.r - state.word;
  1097. }
  1098. /* Starts with '~' and there is no slash in the word, then
  1099. try completing this word as a username. */
  1100. if (matches == NULL && *state.word == '~' && (state.flags & INPUT_COMPLETE_USERNAMES) != 0
  1101. && strchr (state.word, PATH_SEP) == NULL)
  1102. {
  1103. SHOW_C_CTX ("try_complete:user_subst");
  1104. matches = completion_matches (state.word, username_completion_function, state.flags);
  1105. }
  1106. /* If this word is in a command position, then
  1107. complete over possible command names, including aliases, functions,
  1108. and command names. */
  1109. if (matches == NULL)
  1110. matches = try_complete_all_possible (&state, text, lc_start);
  1111. /* And finally if nothing found, try complete directory name */
  1112. if (matches == NULL)
  1113. {
  1114. state.in_command_position = 0;
  1115. matches = try_complete_all_possible (&state, text, lc_start);
  1116. }
  1117. g_free (state.word);
  1118. if (matches != NULL && (flags & INPUT_COMPLETE_FILENAMES) != 0
  1119. && (flags & INPUT_COMPLETE_SHELL_ESC) == 0)
  1120. {
  1121. /* FIXME: HACK? INPUT_COMPLETE_SHELL_ESC is used only in command line. */
  1122. size_t i;
  1123. for (i = 0; i < matches->len; i++)
  1124. {
  1125. char *p;
  1126. p = g_ptr_array_index (matches, i);
  1127. /* Escape only '?', '*', and '&' symbols as described in the
  1128. manual page (see a11995e12b88285e044f644904c306ed6c342ad0). */
  1129. g_ptr_array_index (matches, i) = str_escape (p, -1, "?*&", TRUE);
  1130. g_free (p);
  1131. }
  1132. }
  1133. return matches;
  1134. }
  1135. /* --------------------------------------------------------------------------------------------- */
  1136. void
  1137. complete_engine_fill_completions (WInput *in)
  1138. {
  1139. char *s;
  1140. const char *word_separators;
  1141. word_separators = (in->completion_flags & INPUT_COMPLETE_SHELL_ESC) ? " \t;|<>" : "\t;|<>";
  1142. end = str_offset_to_pos (in->buffer->str, in->point);
  1143. s = in->buffer->str;
  1144. if (in->point != 0)
  1145. {
  1146. /* get symbol before in->point */
  1147. size_t i;
  1148. for (i = in->point - 1; i > 0; i--)
  1149. str_next_char (&s);
  1150. }
  1151. for (; s >= in->buffer->str; str_prev_char (&s))
  1152. {
  1153. start = s - in->buffer->str;
  1154. if (strchr (word_separators, *s) != NULL && !str_is_char_escaped (in->buffer->str, s))
  1155. break;
  1156. }
  1157. if (start < end)
  1158. {
  1159. str_next_char (&s);
  1160. start = s - in->buffer->str;
  1161. }
  1162. in->completions = try_complete (in->buffer->str, &start, &end, in->completion_flags);
  1163. }
  1164. /* --------------------------------------------------------------------------------------------- */
  1165. /* declared in lib/widget/input.h */
  1166. void
  1167. input_complete (WInput *in)
  1168. {
  1169. int engine_flags;
  1170. if (!str_is_valid_string (in->buffer->str))
  1171. return;
  1172. if (in->completions != NULL)
  1173. engine_flags = DO_QUERY;
  1174. else
  1175. {
  1176. engine_flags = DO_INSERTION;
  1177. if (mc_global.widget.show_all_if_ambiguous)
  1178. engine_flags |= DO_QUERY;
  1179. }
  1180. while (complete_engine (in, engine_flags))
  1181. ;
  1182. }
  1183. /* --------------------------------------------------------------------------------------------- */
  1184. void
  1185. input_complete_free (WInput *in)
  1186. {
  1187. if (in->completions != NULL)
  1188. {
  1189. g_ptr_array_free (in->completions, TRUE);
  1190. in->completions = NULL;
  1191. }
  1192. }
  1193. /* --------------------------------------------------------------------------------------------- */