root/src/filemanager/panelize.c

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

DEFINITIONS

This source file includes following definitions.
  1. panelize_entry_free
  2. panelize_entry_cmp_by_label
  3. panelize_entry_add_to_listbox
  4. update_command
  5. panelize_callback
  6. external_panelize_init
  7. external_panelize_done
  8. add2panelize
  9. add2panelize_cmd
  10. remove_from_panelize
  11. do_external_panelize
  12. external_panelize_cmd
  13. external_panelize_load
  14. external_panelize_save
  15. external_panelize_free

   1 /*
   2    External panelize
   3 
   4    Copyright (C) 1995-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Janne Kukonlehto, 1995
   9    Jakub Jelinek, 1995
  10    Andrew Borodin <aborodin@vmail.ru> 2011-2023
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  26  */
  27 
  28 /** \file panelize.c
  29  *  \brief Source: External panelization module
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include "lib/global.h"
  35 
  36 #include "lib/skin.h"
  37 #include "lib/tty/tty.h"
  38 #include "lib/vfs/vfs.h"
  39 #include "lib/mcconfig.h"       /* Load/save directories panelize */
  40 #include "lib/strutil.h"
  41 #include "lib/widget.h"
  42 #include "lib/util.h"           /* mc_pipe_t */
  43 
  44 #include "src/history.h"
  45 
  46 #include "filemanager.h"        /* current_panel */
  47 #include "layout.h"             /* rotate_dash() */
  48 #include "panel.h"              /* WPanel, dir.h */
  49 
  50 #include "panelize.h"
  51 
  52 /*** global variables ****************************************************************************/
  53 
  54 /*** file scope macro definitions ****************************************************************/
  55 
  56 #define UX 3
  57 #define UY 2
  58 
  59 #define B_ADD    B_USER
  60 #define B_REMOVE (B_USER + 1)
  61 
  62 /*** file scope type declarations ****************************************************************/
  63 
  64 typedef struct
  65 {
  66     char *command;
  67     char *label;
  68 } panelize_entry_t;
  69 
  70 /*** forward declarations (file scope functions) *************************************************/
  71 
  72 /*** file scope variables ************************************************************************/
  73 
  74 static WListbox *l_panelize;
  75 static WDialog *panelize_dlg;
  76 static int last_listitem;
  77 static WInput *pname;
  78 static GSList *panelize = NULL;
  79 
  80 static const char *panelize_section = "Panelize";
  81 
  82 /* --------------------------------------------------------------------------------------------- */
  83 /*** file scope functions ************************************************************************/
  84 /* --------------------------------------------------------------------------------------------- */
  85 
  86 static void
  87 panelize_entry_free (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 {
  89     panelize_entry_t *entry = (panelize_entry_t *) data;
  90 
  91     g_free (entry->command);
  92     g_free (entry->label);
  93     g_free (entry);
  94 }
  95 
  96 /* --------------------------------------------------------------------------------------------- */
  97 
  98 static int
  99 panelize_entry_cmp_by_label (gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 100 {
 101     const panelize_entry_t *entry = (const panelize_entry_t *) a;
 102     const char *label = (const char *) b;
 103 
 104     return strcmp (entry->label, label);
 105 }
 106 
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 static void
 110 panelize_entry_add_to_listbox (gpointer data, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     panelize_entry_t *entry = (panelize_entry_t *) data;
 113 
 114     (void) user_data;
 115 
 116     listbox_add_item (l_panelize, LISTBOX_APPEND_AT_END, 0, entry->label, entry, FALSE);
 117 }
 118 
 119 /* --------------------------------------------------------------------------------------------- */
 120 
 121 static void
 122 update_command (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 123 {
 124     if (l_panelize->current != last_listitem)
 125     {
 126         panelize_entry_t *data = NULL;
 127 
 128         last_listitem = l_panelize->current;
 129         listbox_get_current (l_panelize, NULL, (void **) &data);
 130         input_assign_text (pname, data->command);
 131         pname->point = 0;
 132         input_update (pname, TRUE);
 133     }
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 static cb_ret_t
 139 panelize_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     switch (msg)
 142     {
 143     case MSG_INIT:
 144         group_default_callback (w, NULL, MSG_INIT, 0, NULL);
 145         MC_FALLTHROUGH;
 146 
 147     case MSG_NOTIFY:           /* MSG_NOTIFY is fired by the listbox to tell us the item has changed. */
 148         update_command ();
 149         return MSG_HANDLED;
 150 
 151     default:
 152         return dlg_default_callback (w, sender, msg, parm, data);
 153     }
 154 }
 155 
 156 /* --------------------------------------------------------------------------------------------- */
 157 
 158 static void
 159 external_panelize_init (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 160 {
 161     struct
 162     {
 163         int ret_cmd;
 164         button_flags_t flags;
 165         const char *text;
 166     } panelize_but[] = {
 167         /* *INDENT-OFF* */
 168         { B_ENTER, DEFPUSH_BUTTON, N_("Pane&lize") },
 169         { B_REMOVE, NORMAL_BUTTON, N_("&Remove") },
 170         { B_ADD, NORMAL_BUTTON, N_("&Add new") },
 171         { B_CANCEL, NORMAL_BUTTON, N_("&Cancel") }
 172         /* *INDENT-ON* */
 173     };
 174 
 175     WGroup *g;
 176 
 177     size_t i;
 178     int blen;
 179     int panelize_cols;
 180     int x, y;
 181 
 182     last_listitem = 0;
 183 
 184     do_refresh ();
 185 
 186     i = G_N_ELEMENTS (panelize_but);
 187     blen = i - 1;               /* gaps between buttons */
 188     while (i-- != 0)
 189     {
 190 #ifdef ENABLE_NLS
 191         panelize_but[i].text = _(panelize_but[i].text);
 192 #endif
 193         blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
 194         if (panelize_but[i].flags == DEFPUSH_BUTTON)
 195             blen += 2;
 196     }
 197 
 198     panelize_cols = COLS - 6;
 199     panelize_cols = MAX (panelize_cols, blen + 4);
 200 
 201     panelize_dlg =
 202         dlg_create (TRUE, 0, 0, 20, panelize_cols, WPOS_CENTER, FALSE, dialog_colors,
 203                     panelize_callback, NULL, "[External panelize]", _("External panelize"));
 204     g = GROUP (panelize_dlg);
 205 
 206     /* add listbox to the dialogs */
 207     y = UY;
 208     group_add_widget (g, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
 209 
 210     l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
 211     g_slist_foreach (panelize, panelize_entry_add_to_listbox, NULL);
 212     listbox_set_current (l_panelize, listbox_search_text (l_panelize, _("Other command")));
 213     group_add_widget (g, l_panelize);
 214 
 215     y += WIDGET (l_panelize)->rect.lines + 1;
 216     group_add_widget (g, label_new (y++, UX, _("Command")));
 217     pname =
 218         input_new (y++, UX, input_colors, panelize_cols - UX * 2, "", "in",
 219                    INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_COMMANDS |
 220                    INPUT_COMPLETE_VARIABLES | INPUT_COMPLETE_USERNAMES | INPUT_COMPLETE_CD |
 221                    INPUT_COMPLETE_SHELL_ESC);
 222     group_add_widget (g, pname);
 223 
 224     group_add_widget (g, hline_new (y++, -1, -1));
 225 
 226     x = (panelize_cols - blen) / 2;
 227     for (i = 0; i < G_N_ELEMENTS (panelize_but); i++)
 228     {
 229         WButton *b;
 230 
 231         b = button_new (y, x,
 232                         panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text, NULL);
 233         group_add_widget (g, b);
 234 
 235         x += button_get_len (b) + 1;
 236     }
 237 
 238     widget_select (WIDGET (l_panelize));
 239 }
 240 
 241 /* --------------------------------------------------------------------------------------------- */
 242 
 243 static void
 244 external_panelize_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 245 {
 246     widget_destroy (WIDGET (panelize_dlg));
 247     repaint_screen ();
 248 }
 249 
 250 /* --------------------------------------------------------------------------------------------- */
 251 
 252 static void
 253 add2panelize (char *label, char *command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 254 {
 255     panelize_entry_t *entry;
 256 
 257     entry = g_try_new (panelize_entry_t, 1);
 258     if (entry != NULL)
 259     {
 260         entry->label = label;
 261         entry->command = command;
 262 
 263         panelize = g_slist_insert_sorted (panelize, entry, panelize_entry_cmp_by_label);
 264     }
 265 }
 266 
 267 /* --------------------------------------------------------------------------------------------- */
 268 
 269 static void
 270 add2panelize_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 271 {
 272     if (!input_is_empty (pname))
 273     {
 274         char *label;
 275 
 276         label = input_dialog (_("Add to external panelize"),
 277                               _("Enter command label:"), MC_HISTORY_FM_PANELIZE_ADD, "",
 278                               INPUT_COMPLETE_NONE);
 279         if (label == NULL || *label == '\0')
 280             g_free (label);
 281         else
 282             add2panelize (label, input_get_text (pname));
 283     }
 284 }
 285 
 286 /* --------------------------------------------------------------------------------------------- */
 287 
 288 static void
 289 remove_from_panelize (panelize_entry_t *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 290 {
 291     if (strcmp (entry->label, _("Other command")) != 0)
 292     {
 293         panelize = g_slist_remove (panelize, entry);
 294         panelize_entry_free (entry);
 295     }
 296 }
 297 
 298 /* --------------------------------------------------------------------------------------------- */
 299 
 300 static void
 301 do_external_panelize (const char *command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 302 {
 303     dir_list *list = &current_panel->dir;
 304     mc_pipe_t *external;
 305     GError *error = NULL;
 306     GString *remain_file_name = NULL;
 307 
 308     external = mc_popen (command, TRUE, TRUE, &error);
 309     if (external == NULL)
 310     {
 311         message (D_ERROR, _("External panelize"), "%s", error->message);
 312         g_error_free (error);
 313         return;
 314     }
 315 
 316     /* Clear the counters and the directory list */
 317     panel_clean_dir (current_panel);
 318 
 319     panel_panelize_change_root (current_panel, current_panel->cwd_vpath);
 320 
 321     dir_list_init (list);
 322 
 323     while (TRUE)
 324     {
 325         GString *line;
 326         gboolean ok;
 327 
 328         /* init buffers before call of mc_pread() */
 329         external->out.len = MC_PIPE_BUFSIZE;
 330         external->err.len = MC_PIPE_BUFSIZE;
 331         external->err.null_term = TRUE;
 332 
 333         mc_pread (external, &error);
 334 
 335         if (error != NULL)
 336         {
 337             message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), error->message);
 338             g_error_free (error);
 339             break;
 340         }
 341 
 342         if (external->err.len > 0)
 343             message (D_ERROR, MSG_ERROR, _("External panelize:\n%s"), external->err.buf);
 344 
 345         if (external->out.len == MC_PIPE_STREAM_EOF)
 346             break;
 347 
 348         if (external->out.len == 0)
 349             continue;
 350 
 351         if (external->out.len == MC_PIPE_ERROR_READ)
 352         {
 353             message (D_ERROR, MSG_ERROR,
 354                      _("External panelize:\nfailed to read data from child stdout:\n%s"),
 355                      unix_error_string (external->out.error));
 356             break;
 357         }
 358 
 359         ok = TRUE;
 360 
 361         while (ok && (line = mc_pstream_get_string (&external->out)) != NULL)
 362         {
 363             char *name;
 364             gboolean link_to_dir, stale_link;
 365             struct stat st;
 366 
 367             /* handle a \n-separated file list */
 368 
 369             if (line->str[line->len - 1] == '\n')
 370             {
 371                 /* entire file name or last chunk */
 372 
 373                 g_string_truncate (line, line->len - 1);
 374 
 375                 /* join filename chunks */
 376                 if (remain_file_name != NULL)
 377                 {
 378                     g_string_append_len (remain_file_name, line->str, line->len);
 379                     g_string_free (line, TRUE);
 380                     line = remain_file_name;
 381                     remain_file_name = NULL;
 382                 }
 383             }
 384             else
 385             {
 386                 /* first or middle chunk of file name */
 387 
 388                 if (remain_file_name == NULL)
 389                     remain_file_name = line;
 390                 else
 391                 {
 392                     g_string_append_len (remain_file_name, line->str, line->len);
 393                     g_string_free (line, TRUE);
 394                 }
 395 
 396                 continue;
 397             }
 398 
 399             name = line->str;
 400 
 401             if (name[0] == '.' && IS_PATH_SEP (name[1]))
 402                 name += 2;
 403 
 404             if (handle_path (name, &st, &link_to_dir, &stale_link))
 405             {
 406                 ok = dir_list_append (list, name, &st, link_to_dir, stale_link);
 407 
 408                 if (ok)
 409                 {
 410                     file_mark (current_panel, list->len - 1, 0);
 411 
 412                     if ((list->len & 31) == 0)
 413                         rotate_dash (TRUE);
 414                 }
 415             }
 416 
 417             g_string_free (line, TRUE);
 418         }
 419     }
 420 
 421     if (remain_file_name != NULL)
 422         g_string_free (remain_file_name, TRUE);
 423 
 424     mc_pclose (external, NULL);
 425 
 426     current_panel->is_panelized = TRUE;
 427     panel_panelize_absolutize_if_needed (current_panel);
 428 
 429     panel_set_current_by_name (current_panel, NULL);
 430     panel_re_sort (current_panel);
 431     rotate_dash (FALSE);
 432 }
 433 
 434 /* --------------------------------------------------------------------------------------------- */
 435 /*** public functions ****************************************************************************/
 436 /* --------------------------------------------------------------------------------------------- */
 437 
 438 void
 439 external_panelize_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 440 {
 441     if (!vfs_current_is_local ())
 442     {
 443         message (D_ERROR, MSG_ERROR, _("Cannot run external panelize in a non-local directory"));
 444         return;
 445     }
 446 
 447     external_panelize_init ();
 448 
 449     /* display file info */
 450     tty_setcolor (SELECTED_COLOR);
 451 
 452     switch (dlg_run (panelize_dlg))
 453     {
 454     case B_CANCEL:
 455         break;
 456 
 457     case B_ADD:
 458         add2panelize_cmd ();
 459         break;
 460 
 461     case B_REMOVE:
 462         {
 463             panelize_entry_t *entry;
 464 
 465             listbox_get_current (l_panelize, NULL, (void **) &entry);
 466             remove_from_panelize (entry);
 467             break;
 468         }
 469 
 470     case B_ENTER:
 471         if (!input_is_empty (pname))
 472         {
 473             char *cmd;
 474 
 475             cmd = input_get_text (pname);
 476             widget_destroy (WIDGET (panelize_dlg));
 477             do_external_panelize (cmd);
 478             g_free (cmd);
 479             repaint_screen ();
 480             return;
 481         }
 482         break;
 483 
 484     default:
 485         break;
 486     }
 487 
 488     external_panelize_done ();
 489 }
 490 
 491 /* --------------------------------------------------------------------------------------------- */
 492 
 493 void
 494 external_panelize_load (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 495 {
 496     char **keys;
 497 
 498     keys = mc_config_get_keys (mc_global.main_config, panelize_section, NULL);
 499 
 500     add2panelize (g_strdup (_("Other command")), g_strdup (""));
 501 
 502     if (*keys == NULL)
 503     {
 504         add2panelize (g_strdup (_("Modified git files")), g_strdup ("git ls-files --modified"));
 505         add2panelize (g_strdup (_("Find rejects after patching")),
 506                       g_strdup ("find . -name \\*.rej -print"));
 507         add2panelize (g_strdup (_("Find *.orig after patching")),
 508                       g_strdup ("find . -name \\*.orig -print"));
 509         add2panelize (g_strdup (_("Find SUID and SGID programs")),
 510                       g_strdup
 511                       ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 -a -perm /01 \\) \\) -print"));
 512     }
 513     else
 514     {
 515         GIConv conv;
 516         char **profile_keys;
 517 
 518         conv = str_crt_conv_from ("UTF-8");
 519 
 520         for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
 521         {
 522             GString *buffer;
 523 
 524             if (mc_global.utf8_display || conv == INVALID_CONV)
 525                 buffer = g_string_new (*profile_keys);
 526             else
 527             {
 528                 buffer = g_string_new ("");
 529                 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
 530                     g_string_assign (buffer, *profile_keys);
 531             }
 532 
 533             add2panelize (g_string_free (buffer, FALSE),
 534                           mc_config_get_string (mc_global.main_config, panelize_section,
 535                                                 *profile_keys, ""));
 536         }
 537 
 538         str_close_conv (conv);
 539     }
 540 
 541     g_strfreev (keys);
 542 }
 543 
 544 /* --------------------------------------------------------------------------------------------- */
 545 
 546 void
 547 external_panelize_save (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 548 {
 549     GSList *l;
 550 
 551     mc_config_del_group (mc_global.main_config, panelize_section);
 552 
 553     for (l = panelize; l != NULL; l = g_slist_next (l))
 554     {
 555         panelize_entry_t *current = (panelize_entry_t *) l->data;
 556 
 557         if (strcmp (current->label, _("Other command")) != 0)
 558             mc_config_set_string (mc_global.main_config,
 559                                   panelize_section, current->label, current->command);
 560     }
 561 }
 562 
 563 /* --------------------------------------------------------------------------------------------- */
 564 
 565 void
 566 external_panelize_free (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 567 {
 568     g_clear_slist (&panelize, panelize_entry_free);
 569     panelize = NULL;
 570 }
 571 
 572 /* --------------------------------------------------------------------------------------------- */

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