panelize.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. External panelize
  3. Copyright (C) 1995-2018
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Janne Kukonlehto, 1995
  7. Jakub Jelinek, 1995
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software: you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /** \file panelize.c
  21. * \brief Source: External panelization module
  22. */
  23. #include <config.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. #include "lib/global.h"
  31. #include "lib/skin.h"
  32. #include "lib/vfs/vfs.h"
  33. #include "lib/mcconfig.h" /* Load/save directories panelize */
  34. #include "lib/strutil.h"
  35. #include "lib/util.h"
  36. #include "lib/widget.h"
  37. #include "src/setup.h" /* For profile_bname */
  38. #include "src/history.h"
  39. #include "dir.h"
  40. #include "midnight.h" /* current_panel */
  41. #include "layout.h" /* rotate_dash() */
  42. #include "panel.h" /* WPanel */
  43. #include "panelize.h"
  44. /*** global variables ****************************************************************************/
  45. /*** file scope macro definitions ****************************************************************/
  46. #define UX 3
  47. #define UY 2
  48. #define B_ADD B_USER
  49. #define B_REMOVE (B_USER + 1)
  50. /*** file scope type declarations ****************************************************************/
  51. /*** file scope variables ************************************************************************/
  52. static WListbox *l_panelize;
  53. static WDialog *panelize_dlg;
  54. static int last_listitem;
  55. static WInput *pname;
  56. static const char *panelize_section = "Panelize";
  57. /* Directory panelize */
  58. static struct panelize
  59. {
  60. char *command;
  61. char *label;
  62. struct panelize *next;
  63. } *panelize = NULL;
  64. /* --------------------------------------------------------------------------------------------- */
  65. /*** file scope functions ************************************************************************/
  66. /* --------------------------------------------------------------------------------------------- */
  67. static void
  68. update_command (void)
  69. {
  70. if (l_panelize->pos != last_listitem)
  71. {
  72. struct panelize *data = NULL;
  73. last_listitem = l_panelize->pos;
  74. listbox_get_current (l_panelize, NULL, (void **) &data);
  75. input_assign_text (pname, data->command);
  76. pname->point = 0;
  77. input_update (pname, TRUE);
  78. }
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. static cb_ret_t
  82. panelize_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  83. {
  84. switch (msg)
  85. {
  86. case MSG_INIT:
  87. case MSG_NOTIFY: /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
  88. update_command ();
  89. return MSG_HANDLED;
  90. default:
  91. return dlg_default_callback (w, sender, msg, parm, data);
  92. }
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. static void
  96. init_panelize (void)
  97. {
  98. struct
  99. {
  100. int ret_cmd;
  101. button_flags_t flags;
  102. const char *text;
  103. } panelize_but[] =
  104. {
  105. /* *INDENT-OFF* */
  106. { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
  107. { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
  108. { B_ADD, NORMAL_BUTTON, N_("&Add new") },
  109. { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
  110. /* *INDENT-ON* */
  111. };
  112. size_t i;
  113. int blen;
  114. int panelize_cols;
  115. struct panelize *current;
  116. int x, y;
  117. last_listitem = 0;
  118. do_refresh ();
  119. i = G_N_ELEMENTS (panelize_but);
  120. blen = i - 1; /* gaps between buttons */
  121. while (i-- != 0)
  122. {
  123. #ifdef ENABLE_NLS
  124. panelize_but[i].text = _(panelize_but[i].text);
  125. #endif
  126. blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
  127. if (panelize_but[i].flags == DEFPUSH_BUTTON)
  128. blen += 2;
  129. }
  130. panelize_cols = COLS - 6;
  131. panelize_cols = MAX (panelize_cols, blen + 4);
  132. panelize_dlg =
  133. dlg_create (TRUE, 0, 0, 20, panelize_cols, WPOS_CENTER, FALSE, dialog_colors,
  134. panelize_callback, NULL, "[External panelize]", _("External panelize"));
  135. /* add listbox to the dialogs */
  136. y = UY;
  137. add_widget (panelize_dlg, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
  138. l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
  139. for (current = panelize; current != NULL; current = current->next)
  140. listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, current->label, current, FALSE);
  141. listbox_select_entry (l_panelize, listbox_search_text (l_panelize, _("Other command")));
  142. add_widget (panelize_dlg, l_panelize);
  143. y += WIDGET (l_panelize)->lines + 1;
  144. add_widget (panelize_dlg, label_new (y++, UX, _("Command")));
  145. pname =
  146. input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
  147. INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
  148. INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
  149. INPUT_COMPLETE_SHELL_ESC);
  150. add_widget (panelize_dlg, pname);
  151. add_widget (panelize_dlg, hline_new (y++, -1, -1));
  152. x = (panelize_cols - blen) / 2;
  153. for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
  154. {
  155. WButton *b;
  156. b = button_new (y, x,
  157. panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
  158. add_widget (panelize_dlg, b);
  159. x += button_get_len (b) + 1;
  160. }
  161. widget_select (WIDGET (l_panelize));
  162. }
  163. /* --------------------------------------------------------------------------------------------- */
  164. static void
  165. panelize_done (void)
  166. {
  167. dlg_destroy (panelize_dlg);
  168. repaint_screen ();
  169. }
  170. /* --------------------------------------------------------------------------------------------- */
  171. static void
  172. add2panelize (char *label, char *command)
  173. {
  174. struct panelize *current;
  175. struct panelize *old = NULL;
  176. current = panelize;
  177. while (current != NULL && strcmp (current->label, label) <= 0)
  178. {
  179. old = current;
  180. current = current->next;
  181. }
  182. if (old == NULL)
  183. {
  184. panelize = g_new (struct panelize, 1);
  185. panelize->label = label;
  186. panelize->command = command;
  187. panelize->next = current;
  188. }
  189. else
  190. {
  191. struct panelize *new;
  192. new = g_new (struct panelize, 1);
  193. new->label = label;
  194. new->command = command;
  195. old->next = new;
  196. new->next = current;
  197. }
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. static void
  201. add2panelize_cmd (void)
  202. {
  203. if (!input_is_empty (pname))
  204. {
  205. char *label;
  206. label = input_dialog (_("Add to external panelize"),
  207. _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
  208. INPUT_COMPLETE_NONE);
  209. if (label == NULL || *label == '\0')
  210. g_free (label);
  211. else
  212. add2panelize (label, g_strdup (pname->buffer));
  213. }
  214. }
  215. /* --------------------------------------------------------------------------------------------- */
  216. static void
  217. remove_from_panelize (struct panelize *entry)
  218. {
  219. if (strcmp (entry->label, _("Other command")) != 0)
  220. {
  221. if (entry == panelize)
  222. panelize = panelize->next;
  223. else
  224. {
  225. struct panelize *current = panelize;
  226. while (current != NULL && current->next != entry)
  227. current = current->next;
  228. if (current != NULL)
  229. current->next = entry->next;
  230. }
  231. g_free (entry->label);
  232. g_free (entry->command);
  233. g_free (entry);
  234. }
  235. }
  236. /* --------------------------------------------------------------------------------------------- */
  237. static void
  238. do_external_panelize (char *command)
  239. {
  240. dir_list *list = &current_panel->dir;
  241. FILE *external;
  242. open_error_pipe ();
  243. external = popen (command, "r");
  244. if (external == NULL)
  245. {
  246. close_error_pipe (D_ERROR, _("Cannot invoke command."));
  247. return;
  248. }
  249. /* Clear the counters and the directory list */
  250. panel_clean_dir (current_panel);
  251. panelize_change_root (current_panel->cwd_vpath);
  252. dir_list_init (list);
  253. while (TRUE)
  254. {
  255. char line[MC_MAXPATHLEN];
  256. size_t len;
  257. char *name;
  258. int link_to_dir, stale_link;
  259. struct stat st;
  260. clearerr (external);
  261. if (fgets (line, sizeof (line), external) == NULL)
  262. {
  263. if (ferror (external) != 0 && errno == EINTR)
  264. continue;
  265. break;
  266. }
  267. len = strlen (line);
  268. if (line[len - 1] == '\n')
  269. line[len - 1] = '\0';
  270. if (line[0] == '\0')
  271. continue;
  272. name = line;
  273. if (line[0] == '.' && IS_PATH_SEP (line[1]))
  274. name += 2;
  275. if (!handle_path (name, &st, &link_to_dir, &stale_link))
  276. continue;
  277. if (!dir_list_append (list, name, &st, link_to_dir != 0, stale_link != 0))
  278. break;
  279. file_mark (current_panel, list->len - 1, 0);
  280. if ((list->len & 31) == 0)
  281. rotate_dash (TRUE);
  282. }
  283. current_panel->is_panelized = TRUE;
  284. panelize_absolutize_if_needed (current_panel);
  285. if (pclose (external) < 0)
  286. message (D_NORMAL, _("External panelize"), _("Pipe close failed"));
  287. close_error_pipe (D_NORMAL, NULL);
  288. try_to_select (current_panel, NULL);
  289. panel_re_sort (current_panel);
  290. rotate_dash (FALSE);
  291. }
  292. /* --------------------------------------------------------------------------------------------- */
  293. static void
  294. do_panelize_cd (WPanel * panel)
  295. {
  296. int i;
  297. dir_list *list;
  298. gboolean panelized_same;
  299. dir_list_clean (&panel->dir);
  300. if (panelized_panel.root_vpath == NULL)
  301. panelize_change_root (current_panel->cwd_vpath);
  302. if (panelized_panel.list.len < 1)
  303. dir_list_init (&panelized_panel.list);
  304. else if (panelized_panel.list.len > panel->dir.size)
  305. dir_list_grow (&panel->dir, panelized_panel.list.len - panel->dir.size);
  306. list = &panel->dir;
  307. list->len = panelized_panel.list.len;
  308. panelized_same = vfs_path_equal (panelized_panel.root_vpath, panel->cwd_vpath);
  309. for (i = 0; i < panelized_panel.list.len; i++)
  310. {
  311. if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname))
  312. {
  313. list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
  314. list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,
  315. panelized_panel.list.list[i].fnamelen);
  316. }
  317. else
  318. {
  319. vfs_path_t *tmp_vpath;
  320. const char *fname;
  321. tmp_vpath =
  322. vfs_path_append_new (panelized_panel.root_vpath, panelized_panel.list.list[i].fname,
  323. (char *) NULL);
  324. fname = vfs_path_as_str (tmp_vpath);
  325. list->list[i].fnamelen = strlen (fname);
  326. list->list[i].fname = g_strndup (fname, list->list[i].fnamelen);
  327. vfs_path_free (tmp_vpath);
  328. }
  329. list->list[i].f.link_to_dir = panelized_panel.list.list[i].f.link_to_dir;
  330. list->list[i].f.stale_link = panelized_panel.list.list[i].f.stale_link;
  331. list->list[i].f.dir_size_computed = panelized_panel.list.list[i].f.dir_size_computed;
  332. list->list[i].f.marked = panelized_panel.list.list[i].f.marked;
  333. list->list[i].st = panelized_panel.list.list[i].st;
  334. list->list[i].sort_key = panelized_panel.list.list[i].sort_key;
  335. list->list[i].second_sort_key = panelized_panel.list.list[i].second_sort_key;
  336. }
  337. panel->is_panelized = TRUE;
  338. panelize_absolutize_if_needed (panel);
  339. try_to_select (panel, NULL);
  340. }
  341. /* --------------------------------------------------------------------------------------------- */
  342. /*** public functions ****************************************************************************/
  343. /* --------------------------------------------------------------------------------------------- */
  344. /**
  345. * Change root directory of panelized content.
  346. * @param new_root - object with new path.
  347. */
  348. void
  349. panelize_change_root (const vfs_path_t * new_root)
  350. {
  351. vfs_path_free (panelized_panel.root_vpath);
  352. panelized_panel.root_vpath = vfs_path_clone (new_root);
  353. }
  354. /* --------------------------------------------------------------------------------------------- */
  355. void
  356. panelize_save_panel (WPanel * panel)
  357. {
  358. int i;
  359. dir_list *list = &panel->dir;
  360. panelize_change_root (current_panel->cwd_vpath);
  361. if (panelized_panel.list.len > 0)
  362. dir_list_clean (&panelized_panel.list);
  363. if (panel->dir.len == 0)
  364. return;
  365. if (panel->dir.len > panelized_panel.list.size)
  366. dir_list_grow (&panelized_panel.list, panel->dir.len - panelized_panel.list.size);
  367. panelized_panel.list.len = panel->dir.len;
  368. for (i = 0; i < panel->dir.len; i++)
  369. {
  370. panelized_panel.list.list[i].fnamelen = list->list[i].fnamelen;
  371. panelized_panel.list.list[i].fname =
  372. g_strndup (list->list[i].fname, list->list[i].fnamelen);
  373. panelized_panel.list.list[i].f.link_to_dir = list->list[i].f.link_to_dir;
  374. panelized_panel.list.list[i].f.stale_link = list->list[i].f.stale_link;
  375. panelized_panel.list.list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
  376. panelized_panel.list.list[i].f.marked = list->list[i].f.marked;
  377. panelized_panel.list.list[i].st = list->list[i].st;
  378. panelized_panel.list.list[i].sort_key = list->list[i].sort_key;
  379. panelized_panel.list.list[i].second_sort_key = list->list[i].second_sort_key;
  380. }
  381. }
  382. /* --------------------------------------------------------------------------------------------- */
  383. /**
  384. * Conditionally switches a panel's directory to "/" (root).
  385. *
  386. * If a panelized panel's listing contain absolute paths, this function
  387. * sets the panel's directory to "/". Otherwise it does nothing.
  388. *
  389. * Rationale:
  390. *
  391. * This makes tokenized strings like "%d/%p" work. This also makes other
  392. * places work where such naive concatenation is done in code (e.g., when
  393. * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
  394. *
  395. * When to call:
  396. *
  397. * You should always call this function after you populate the listing
  398. * of a panelized panel.
  399. */
  400. void
  401. panelize_absolutize_if_needed (WPanel * panel)
  402. {
  403. const dir_list *const list = &panel->dir;
  404. /* Note: We don't support mixing of absolute and relative paths, which is
  405. * why it's ok for us to check only the 1st entry. */
  406. if (list->len > 1 && g_path_is_absolute (list->list[1].fname))
  407. {
  408. vfs_path_t *root;
  409. root = vfs_path_from_str (PATH_SEP_STR);
  410. panel_set_cwd (panel, root);
  411. if (panel == current_panel)
  412. mc_chdir (root);
  413. vfs_path_free (root);
  414. }
  415. }
  416. /* --------------------------------------------------------------------------------------------- */
  417. void
  418. cd_panelize_cmd (void)
  419. {
  420. if (!SELECTED_IS_PANEL)
  421. set_display_type (MENU_PANEL_IDX, view_listing);
  422. do_panelize_cd (PANEL (get_panel_widget (MENU_PANEL_IDX)));
  423. }
  424. /* --------------------------------------------------------------------------------------------- */
  425. void
  426. external_panelize (void)
  427. {
  428. if (!vfs_current_is_local ())
  429. {
  430. message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
  431. return;
  432. }
  433. init_panelize ();
  434. /* display file info */
  435. tty_setcolor (SELECTED_COLOR);
  436. switch (dlg_run (panelize_dlg))
  437. {
  438. case B_CANCEL:
  439. break;
  440. case B_ADD:
  441. add2panelize_cmd ();
  442. break;
  443. case B_REMOVE:
  444. {
  445. struct panelize *entry;
  446. listbox_get_current (l_panelize, NULL, (void **) &entry);
  447. remove_from_panelize (entry);
  448. break;
  449. }
  450. case B_ENTER:
  451. if (!input_is_empty (pname))
  452. {
  453. char *cmd;
  454. cmd = g_strdup (pname->buffer);
  455. dlg_destroy (panelize_dlg);
  456. do_external_panelize (cmd);
  457. g_free (cmd);
  458. repaint_screen ();
  459. return;
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. panelize_done ();
  466. }
  467. /* --------------------------------------------------------------------------------------------- */
  468. void
  469. load_panelize (void)
  470. {
  471. char **keys;
  472. keys = mc_config_get_keys (mc_global.main_config, panelize_section, NULL);
  473. add2panelize (g_strdup (_("Other command")), g_strdup (""));
  474. if (*keys == NULL)
  475. {
  476. add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
  477. add2panelize (g_strdup (_("Find rejects after patching")),
  478. g_strdup ("find . -name \\*.rej -print"));
  479. add2panelize (g_strdup (_("Find *.orig after patching")),
  480. g_strdup ("find . -name \\*.orig -print"));
  481. add2panelize (g_strdup (_("Find SUID and SGID programs")),
  482. g_strdup
  483. ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
  484. }
  485. else
  486. {
  487. GIConv conv;
  488. char **profile_keys;
  489. conv = str_crt_conv_from ("UTF-8");
  490. for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
  491. {
  492. GString *buffer;
  493. if (mc_global.utf8_display || conv == INVALID_CONV)
  494. buffer = g_string_new (*profile_keys);
  495. else
  496. {
  497. buffer = g_string_new ("");
  498. if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
  499. g_string_assign (buffer, *profile_keys);
  500. }
  501. add2panelize (g_string_free (buffer, FALSE),
  502. mc_config_get_string (mc_global.main_config, panelize_section,
  503. *profile_keys, ""));
  504. }
  505. str_close_conv (conv);
  506. }
  507. g_strfreev (keys);
  508. }
  509. /* --------------------------------------------------------------------------------------------- */
  510. void
  511. save_panelize (void)
  512. {
  513. struct panelize *current;
  514. mc_config_del_group (mc_global.main_config, panelize_section);
  515. for (current = panelize; current != NULL; current = current->next)
  516. if (strcmp (current->label, _("Other command")) != 0)
  517. mc_config_set_string (mc_global.main_config,
  518. panelize_section, current->label, current->command);
  519. }
  520. /* --------------------------------------------------------------------------------------------- */
  521. void
  522. done_panelize (void)
  523. {
  524. struct panelize *current, *next;
  525. for (current = panelize; current != NULL; current = next)
  526. {
  527. next = current->next;
  528. g_free (current->label);
  529. g_free (current->command);
  530. g_free (current);
  531. }
  532. }
  533. /* --------------------------------------------------------------------------------------------- */