input_complete.c 44 KB

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