input_complete.c 44 KB

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