Manual pages: mcmcdiffmceditmcview

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

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