input_complete.c 44 KB

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