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, N_ ("Pane&lize") },
 171         { B_REMOVE, NORMAL_BUTTON, N_ ("&Remove") },
 172         { B_ADD, NORMAL_BUTTON, N_ ("&Add new") },
 173         { B_CANCEL, NORMAL_BUTTON, N_ ("&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 #ifdef ENABLE_NLS
 192         panelize_but[i].text = _ (panelize_but[i].text);
 193 #endif
 194         blen += str_term_width1 (panelize_but[i].text) + 3 + 1;
 195         if (panelize_but[i].flags == DEFPUSH_BUTTON)
 196             blen += 2;
 197     }
 198 
 199     panelize_cols = COLS - 6;
 200     panelize_cols = MAX (panelize_cols, blen + 4);
 201 
 202     panelize_dlg =
 203         dlg_create (TRUE, 0, 0, 20, panelize_cols, WPOS_CENTER, FALSE, dialog_colors,
 204                     panelize_callback, NULL, "[External panelize]", _ ("External panelize"));
 205     g = GROUP (panelize_dlg);
 206 
 207     // add listbox to the dialogs
 208     y = UY;
 209     group_add_widget (g, groupbox_new (y++, UX, 12, panelize_cols - UX * 2, ""));
 210 
 211     l_panelize = listbox_new (y, UX + 1, 10, panelize_cols - UX * 2 - 2, FALSE, NULL);
 212     g_slist_foreach (panelize, panelize_entry_add_to_listbox, NULL);
 213     listbox_set_current (l_panelize, listbox_search_text (l_panelize, _ ("Other command")));
 214     group_add_widget (g, l_panelize);
 215 
 216     y += WIDGET (l_panelize)->rect.lines + 1;
 217     group_add_widget (g, label_new (y++, UX, _ ("Command")));
 218     pname = 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, panelize_but[i].ret_cmd, panelize_but[i].flags, panelize_but[i].text,
 232                         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"), _ ("Enter command label:"),
 277                               MC_HISTORY_FM_PANELIZE_ADD, "", INPUT_COMPLETE_NONE);
 278         if (label == NULL || *label == '\0')
 279             g_free (label);
 280         else
 281             add2panelize (label, input_get_text (pname));
 282     }
 283 }
 284 
 285 /* --------------------------------------------------------------------------------------------- */
 286 
 287 static void
 288 remove_from_panelize (panelize_entry_t *entry)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     if (strcmp (entry->label, _ ("Other command")) != 0)
 291     {
 292         panelize = g_slist_remove (panelize, entry);
 293         panelize_entry_free (entry);
 294     }
 295 }
 296 
 297 /* --------------------------------------------------------------------------------------------- */
 298 
 299 static void
 300 do_external_panelize (const char *command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 301 {
 302     dir_list *list = &current_panel->dir;
 303     mc_pipe_t *external;
 304     GError *error = NULL;
 305     GString *remain_file_name = NULL;
 306 
 307     external = mc_popen (command, TRUE, TRUE, &error);
 308     if (external == NULL)
 309     {
 310         message (D_ERROR, _ ("External panelize"), "%s", error->message);
 311         g_error_free (error);
 312         return;
 313     }
 314 
 315     // Clear the counters and the directory list
 316     panel_clean_dir (current_panel);
 317 
 318     panel_panelize_change_root (current_panel, current_panel->cwd_vpath);
 319 
 320     dir_list_init (list);
 321 
 322     while (TRUE)
 323     {
 324         GString *line;
 325         gboolean ok;
 326 
 327         // init buffers before call of mc_pread()
 328         external->out.len = MC_PIPE_BUFSIZE;
 329         external->err.len = MC_PIPE_BUFSIZE;
 330         external->err.null_term = TRUE;
 331 
 332         mc_pread (external, &error);
 333 
 334         if (error != NULL)
 335         {
 336             message (D_ERROR, MSG_ERROR, _ ("External panelize:\n%s"), error->message);
 337             g_error_free (error);
 338             break;
 339         }
 340 
 341         if (external->err.len > 0)
 342             message (D_ERROR, MSG_ERROR, _ ("External panelize:\n%s"), external->err.buf);
 343 
 344         if (external->out.len == MC_PIPE_STREAM_EOF)
 345             break;
 346 
 347         if (external->out.len == 0)
 348             continue;
 349 
 350         if (external->out.len == MC_PIPE_ERROR_READ)
 351         {
 352             errno = external->out.error;
 353             file_error_message (_ ("External panelize:\nfailed to read data from child stdout:"),
 354                                 NULL);
 355             break;
 356         }
 357 
 358         ok = TRUE;
 359 
 360         while (ok && (line = mc_pstream_get_string (&external->out)) != NULL)
 361         {
 362             char *name;
 363             gboolean link_to_dir, stale_link;
 364             struct stat st;
 365 
 366             // handle a \n-separated file list
 367 
 368             if (line->str[line->len - 1] == '\n')
 369             {
 370                 // entire file name or last chunk
 371 
 372                 g_string_truncate (line, line->len - 1);
 373 
 374                 // join filename chunks
 375                 if (remain_file_name != NULL)
 376                 {
 377                     g_string_append_len (remain_file_name, line->str, line->len);
 378                     g_string_free (line, TRUE);
 379                     line = remain_file_name;
 380                     remain_file_name = NULL;
 381                 }
 382             }
 383             else
 384             {
 385                 // first or middle chunk of file name
 386 
 387                 if (remain_file_name == NULL)
 388                     remain_file_name = line;
 389                 else
 390                 {
 391                     g_string_append_len (remain_file_name, line->str, line->len);
 392                     g_string_free (line, TRUE);
 393                 }
 394 
 395                 continue;
 396             }
 397 
 398             name = line->str;
 399 
 400             if (name[0] == '.' && IS_PATH_SEP (name[1]))
 401                 name += 2;
 402 
 403             if (handle_path (name, &st, &link_to_dir, &stale_link))
 404             {
 405                 ok = dir_list_append (list, name, &st, link_to_dir, stale_link);
 406 
 407                 if (ok)
 408                 {
 409                     file_mark (current_panel, list->len - 1, 0);
 410 
 411                     if ((list->len & 31) == 0)
 412                         rotate_dash (TRUE);
 413                 }
 414             }
 415 
 416             g_string_free (line, TRUE);
 417         }
 418     }
 419 
 420     if (remain_file_name != NULL)
 421         g_string_free (remain_file_name, TRUE);
 422 
 423     mc_pclose (external, NULL);
 424 
 425     current_panel->is_panelized = TRUE;
 426     panel_panelize_absolutize_if_needed (current_panel);
 427 
 428     panel_set_current_by_name (current_panel, NULL);
 429     panel_re_sort (current_panel);
 430     rotate_dash (FALSE);
 431 }
 432 
 433 /* --------------------------------------------------------------------------------------------- */
 434 /*** public functions ****************************************************************************/
 435 /* --------------------------------------------------------------------------------------------- */
 436 
 437 void
 438 external_panelize_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 439 {
 440     if (!vfs_current_is_local ())
 441     {
 442         message (D_ERROR, MSG_ERROR, _ ("Cannot run external panelize in a non-local directory"));
 443         return;
 444     }
 445 
 446     external_panelize_init ();
 447 
 448     // display file info
 449     tty_setcolor (SELECTED_COLOR);
 450 
 451     switch (dlg_run (panelize_dlg))
 452     {
 453     case B_CANCEL:
 454         break;
 455 
 456     case B_ADD:
 457         add2panelize_cmd ();
 458         break;
 459 
 460     case B_REMOVE:
 461     {
 462         panelize_entry_t *entry;
 463 
 464         listbox_get_current (l_panelize, NULL, (void **) &entry);
 465         remove_from_panelize (entry);
 466         break;
 467     }
 468 
 469     case B_ENTER:
 470         if (!input_is_empty (pname))
 471         {
 472             char *cmd;
 473 
 474             cmd = input_get_text (pname);
 475             widget_destroy (WIDGET (panelize_dlg));
 476             do_external_panelize (cmd);
 477             g_free (cmd);
 478             repaint_screen ();
 479             return;
 480         }
 481         break;
 482 
 483     default:
 484         break;
 485     }
 486 
 487     external_panelize_done ();
 488 }
 489 
 490 /* --------------------------------------------------------------------------------------------- */
 491 
 492 void
 493 external_panelize_load (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 494 {
 495     char **keys;
 496 
 497     keys = mc_config_get_keys (mc_global.main_config, panelize_section, NULL);
 498 
 499     add2panelize (g_strdup (_ ("Other command")), g_strdup (""));
 500 
 501     if (*keys == NULL)
 502     {
 503         add2panelize (g_strdup (_ ("Modified git files")), g_strdup ("git ls-files --modified"));
 504         add2panelize (g_strdup (_ ("Find rejects after patching")),
 505                       g_strdup ("find . -name \\*.rej -print"));
 506         add2panelize (g_strdup (_ ("Find *.orig after patching")),
 507                       g_strdup ("find . -name \\*.orig -print"));
 508         add2panelize (g_strdup (_ ("Find SUID and SGID programs")),
 509                       g_strdup ("find . \\( \\( -perm -04000 -a -perm /011 \\) -o \\( -perm -02000 "
 510                                 "-a -perm /01 \\) \\) -print"));
 511     }
 512     else
 513     {
 514         GIConv conv;
 515         char **profile_keys;
 516 
 517         conv = str_crt_conv_from ("UTF-8");
 518 
 519         for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
 520         {
 521             GString *buffer;
 522 
 523             if (mc_global.utf8_display || conv == INVALID_CONV)
 524                 buffer = g_string_new (*profile_keys);
 525             else
 526             {
 527                 buffer = g_string_new ("");
 528                 if (str_convert (conv, *profile_keys, buffer) == ESTR_FAILURE)
 529                     g_string_assign (buffer, *profile_keys);
 530             }
 531 
 532             add2panelize (
 533                 g_string_free (buffer, FALSE),
 534                 mc_config_get_string (mc_global.main_config, panelize_section, *profile_keys, ""));
 535         }
 536 
 537         str_close_conv (conv);
 538     }
 539 
 540     g_strfreev (keys);
 541 }
 542 
 543 /* --------------------------------------------------------------------------------------------- */
 544 
 545 void
 546 external_panelize_save (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 547 {
 548     GSList *l;
 549 
 550     mc_config_del_group (mc_global.main_config, panelize_section);
 551 
 552     for (l = panelize; l != NULL; l = g_slist_next (l))
 553     {
 554         panelize_entry_t *current = (panelize_entry_t *) l->data;
 555 
 556         if (strcmp (current->label, _ ("Other command")) != 0)
 557             mc_config_set_string (mc_global.main_config, panelize_section, current->label,
 558                                   current->command);
 559     }
 560 }
 561 
 562 /* --------------------------------------------------------------------------------------------- */
 563 
 564 void
 565 external_panelize_free (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 566 {
 567     g_clear_slist (&panelize, panelize_entry_free);
 568     panelize = NULL;
 569 }
 570 
 571 /* --------------------------------------------------------------------------------------------- */

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