root/src/filemanager/panelize.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. update_command
  2. panelize_callback
  3. init_panelize
  4. panelize_done
  5. add2panelize
  6. add2panelize_cmd
  7. remove_from_panelize
  8. do_external_panelize
  9. do_panelize_cd
  10. panelize_change_root
  11. panelize_save_panel
  12. panelize_absolutize_if_needed
  13. cd_panelize_cmd
  14. external_panelize
  15. load_panelize
  16. save_panelize
  17. done_panelize

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

/* [previous][next][first][last][top][bottom][index][help]  */