root/src/filemanager/ext.c

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

DEFINITIONS

This source file includes following definitions.
  1. exec_cleanup_script
  2. exec_cleanup_file_name
  3. exec_get_file_name
  4. exec_expand_format
  5. exec_get_export_variables
  6. exec_make_shell_string
  7. exec_extension_view
  8. exec_extension_cd
  9. exec_extension
  10. get_popen_information
  11. get_file_type_local
  12. get_file_encoding_local
  13. regex_check_type
  14. flush_extension_file
  15. regex_command_for

   1 /*
   2    Extension dependent execution.
   3 
   4    Copyright (C) 1994-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Jakub Jelinek, 1995
   9    Miguel de Icaza, 1994
  10    Slava Zanko <slavazanko@gmail.com>, 2013
  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  ext.c
  29  *  \brief Source: extension dependent execution
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include <ctype.h>
  35 #include <errno.h>
  36 #include <stdlib.h>
  37 #include <stdio.h>
  38 #include <string.h>
  39 #include <unistd.h>
  40 
  41 #include "lib/global.h"
  42 #include "lib/tty/tty.h"
  43 #include "lib/search.h"
  44 #include "lib/fileloc.h"
  45 #include "lib/mcconfig.h"
  46 #include "lib/util.h"
  47 #include "lib/vfs/vfs.h"
  48 #include "lib/widget.h"
  49 #ifdef HAVE_CHARSET
  50 #include "lib/charsets.h"       /* get_codepage_index */
  51 #endif
  52 
  53 #include "src/setup.h"          /* use_file_to_check_type */
  54 #include "src/execute.h"
  55 #include "src/history.h"
  56 #include "src/usermenu.h"
  57 
  58 #include "src/consaver/cons.saver.h"
  59 #include "src/viewer/mcviewer.h"
  60 
  61 #ifdef HAVE_CHARSET
  62 #include "src/selcodepage.h"    /* do_set_codepage */
  63 #endif
  64 
  65 #include "panel.h"              /* do_cd */
  66 
  67 #include "ext.h"
  68 
  69 /*** global variables ****************************************************************************/
  70 
  71 /*** file scope macro definitions ****************************************************************/
  72 
  73 #ifdef FILE_L
  74 #define FILE_CMD "file -L "
  75 #else
  76 #define FILE_CMD "file "
  77 #endif
  78 
  79 /*** file scope type declarations ****************************************************************/
  80 
  81 typedef char *(*quote_func_t) (const char *name, gboolean quote_percent);
  82 
  83 /*** file scope variables ************************************************************************/
  84 
  85 /* This variable points to a copy of the mc.ext file in memory
  86  * With this we avoid loading/parsing the file each time we
  87  * need it
  88  */
  89 static char *data = NULL;
  90 static vfs_path_t *localfilecopy_vpath = NULL;
  91 static char buffer[BUF_1K];
  92 
  93 static char *pbuffer = NULL;
  94 static time_t localmtime = 0;
  95 static quote_func_t quote_func = name_quote;
  96 static gboolean run_view = FALSE;
  97 static gboolean is_cd = FALSE;
  98 static gboolean written_nonspace = FALSE;
  99 static gboolean do_local_copy = FALSE;
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 /*** file scope functions ************************************************************************/
 103 /* --------------------------------------------------------------------------------------------- */
 104 
 105 static void
 106 exec_cleanup_script (vfs_path_t * script_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 107 {
 108     if (script_vpath != NULL)
 109     {
 110         (void) mc_unlink (script_vpath);
 111         vfs_path_free (script_vpath);
 112     }
 113 }
 114 
 115 /* --------------------------------------------------------------------------------------------- */
 116 
 117 static void
 118 exec_cleanup_file_name (const vfs_path_t * filename_vpath, gboolean has_changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
 119 {
 120     if (localfilecopy_vpath == NULL)
 121         return;
 122 
 123     if (has_changed)
 124     {
 125         struct stat mystat;
 126 
 127         mc_stat (localfilecopy_vpath, &mystat);
 128         has_changed = localmtime != mystat.st_mtime;
 129     }
 130     mc_ungetlocalcopy (filename_vpath, localfilecopy_vpath, has_changed);
 131     vfs_path_free (localfilecopy_vpath);
 132     localfilecopy_vpath = NULL;
 133 }
 134 
 135 /* --------------------------------------------------------------------------------------------- */
 136 
 137 static char *
 138 exec_get_file_name (const vfs_path_t * filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     if (!do_local_copy)
 141         return quote_func (vfs_path_get_last_path_str (filename_vpath), FALSE);
 142 
 143     if (localfilecopy_vpath == NULL)
 144     {
 145         struct stat mystat;
 146         localfilecopy_vpath = mc_getlocalcopy (filename_vpath);
 147         if (localfilecopy_vpath == NULL)
 148             return NULL;
 149 
 150         mc_stat (localfilecopy_vpath, &mystat);
 151         localmtime = mystat.st_mtime;
 152     }
 153 
 154     return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), FALSE);
 155 }
 156 
 157 /* --------------------------------------------------------------------------------------------- */
 158 
 159 static char *
 160 exec_expand_format (char symbol, gboolean is_result_quoted)
     /* [previous][next][first][last][top][bottom][index][help]  */
 161 {
 162     char *text;
 163 
 164     text = expand_format (NULL, symbol, TRUE);
 165     if (is_result_quoted && text != NULL)
 166     {
 167         char *quoted_text;
 168 
 169         quoted_text = g_strdup_printf ("\"%s\"", text);
 170         g_free (text);
 171         text = quoted_text;
 172     }
 173     return text;
 174 }
 175 
 176 /* --------------------------------------------------------------------------------------------- */
 177 
 178 static char *
 179 exec_get_export_variables (const vfs_path_t * filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 180 {
 181     char *text;
 182     GString *export_vars_string;
 183     size_t i;
 184 
 185     /* *INDENT-OFF* */
 186     struct
 187     {
 188         const char symbol;
 189         const char *name;
 190         const gboolean is_result_quoted;
 191     } export_variables[] = {
 192         {'p', "MC_EXT_BASENAME", FALSE},
 193         {'d', "MC_EXT_CURRENTDIR", FALSE},
 194         {'s', "MC_EXT_SELECTED", TRUE},
 195         {'t', "MC_EXT_ONLYTAGGED", TRUE},
 196         {'\0', NULL, FALSE}
 197     };
 198     /* *INDENT-ON* */
 199 
 200     text = exec_get_file_name (filename_vpath);
 201     if (text == NULL)
 202         return NULL;
 203 
 204     export_vars_string = g_string_new ("MC_EXT_FILENAME=");
 205     g_string_append_printf (export_vars_string, "%s\nexport MC_EXT_FILENAME\n", text);
 206     g_free (text);
 207 
 208     for (i = 0; export_variables[i].name != NULL; i++)
 209     {
 210         text =
 211             exec_expand_format (export_variables[i].symbol, export_variables[i].is_result_quoted);
 212         if (text != NULL)
 213         {
 214             g_string_append_printf (export_vars_string,
 215                                     "%s=%s\nexport %s\n", export_variables[i].name, text,
 216                                     export_variables[i].name);
 217             g_free (text);
 218         }
 219     }
 220     return g_string_free (export_vars_string, FALSE);
 221 }
 222 
 223 /* --------------------------------------------------------------------------------------------- */
 224 
 225 static char *
 226 exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     GString *shell_string;
 229     char lc_prompt[80] = "\0";
 230     gboolean parameter_found = FALSE;
 231     gboolean expand_prefix_found = FALSE;
 232 
 233     shell_string = g_string_new ("");
 234 
 235     for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
 236     {
 237         if (parameter_found)
 238         {
 239             if (*lc_data == '}')
 240             {
 241                 char *parameter;
 242 
 243                 parameter_found = FALSE;
 244                 parameter =
 245                     input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "",
 246                                   INPUT_COMPLETE_NONE);
 247                 if (parameter == NULL)
 248                 {
 249                     /* User canceled */
 250                     g_string_free (shell_string, TRUE);
 251                     exec_cleanup_file_name (filename_vpath, FALSE);
 252                     return NULL;
 253                 }
 254                 g_string_append (shell_string, parameter);
 255                 written_nonspace = TRUE;
 256                 g_free (parameter);
 257             }
 258             else
 259             {
 260                 size_t len = strlen (lc_prompt);
 261 
 262                 if (len < sizeof (lc_prompt) - 1)
 263                 {
 264                     lc_prompt[len] = *lc_data;
 265                     lc_prompt[len + 1] = '\0';
 266                 }
 267             }
 268         }
 269         else if (expand_prefix_found)
 270         {
 271             expand_prefix_found = FALSE;
 272             if (*lc_data == '{')
 273                 parameter_found = TRUE;
 274             else
 275             {
 276                 int i;
 277                 char *v;
 278 
 279                 i = check_format_view (lc_data);
 280                 if (i != 0)
 281                 {
 282                     lc_data += i - 1;
 283                     run_view = TRUE;
 284                 }
 285                 else
 286                 {
 287                     i = check_format_cd (lc_data);
 288                     if (i > 0)
 289                     {
 290                         is_cd = TRUE;
 291                         quote_func = fake_name_quote;
 292                         do_local_copy = FALSE;
 293                         pbuffer = buffer;
 294                         lc_data += i - 1;
 295                     }
 296                     else
 297                     {
 298                         i = check_format_var (lc_data, &v);
 299                         if (i > 0 && v != NULL)
 300                         {
 301                             g_string_append (shell_string, v);
 302                             g_free (v);
 303                             lc_data += i;
 304                         }
 305                         else
 306                         {
 307                             char *text;
 308 
 309                             if (*lc_data != 'f')
 310                                 text = expand_format (NULL, *lc_data, !is_cd);
 311                             else
 312                             {
 313                                 text = exec_get_file_name (filename_vpath);
 314                                 if (text == NULL)
 315                                 {
 316                                     g_string_free (shell_string, TRUE);
 317                                     return NULL;
 318                                 }
 319                             }
 320 
 321                             if (!is_cd)
 322                                 g_string_append (shell_string, text);
 323                             else
 324                             {
 325                                 strcpy (pbuffer, text);
 326                                 pbuffer = strchr (pbuffer, 0);
 327                             }
 328 
 329                             g_free (text);
 330                             written_nonspace = TRUE;
 331                         }
 332                     }
 333                 }
 334             }
 335         }
 336         else if (*lc_data == '%')
 337             expand_prefix_found = TRUE;
 338         else
 339         {
 340             if (!whitespace (*lc_data))
 341                 written_nonspace = TRUE;
 342             if (is_cd)
 343                 *(pbuffer++) = *lc_data;
 344             else
 345                 g_string_append_c (shell_string, *lc_data);
 346         }
 347     }                           /* for */
 348     return g_string_free (shell_string, FALSE);
 349 }
 350 
 351 /* --------------------------------------------------------------------------------------------- */
 352 
 353 static void
 354 exec_extension_view (void *target, char *cmd, const vfs_path_t * filename_vpath, int start_line)
     /* [previous][next][first][last][top][bottom][index][help]  */
 355 {
 356     mcview_mode_flags_t def_flags = {
 357         /* *INDENT-OFF* */
 358         .wrap = FALSE,
 359         .hex = mcview_global_flags.hex,
 360         .magic = FALSE,
 361         .nroff = mcview_global_flags.nroff
 362         /* *INDENT-ON* */
 363     };
 364 
 365     mcview_mode_flags_t changed_flags;
 366 
 367     mcview_clear_mode_flags (&changed_flags);
 368     mcview_altered_flags.hex = FALSE;
 369     mcview_altered_flags.nroff = FALSE;
 370     if (def_flags.hex != mcview_global_flags.hex)
 371         changed_flags.hex = TRUE;
 372     if (def_flags.nroff != mcview_global_flags.nroff)
 373         changed_flags.nroff = TRUE;
 374 
 375     if (target == NULL)
 376         mcview_viewer (cmd, filename_vpath, start_line, 0, 0);
 377     else
 378         mcview_load ((WView *) target, cmd, vfs_path_as_str (filename_vpath), start_line, 0, 0);
 379 
 380     if (changed_flags.hex && !mcview_altered_flags.hex)
 381         mcview_global_flags.hex = def_flags.hex;
 382     if (changed_flags.nroff && !mcview_altered_flags.nroff)
 383         mcview_global_flags.nroff = def_flags.nroff;
 384 
 385     dialog_switch_process_pending ();
 386 }
 387 
 388 /* --------------------------------------------------------------------------------------------- */
 389 
 390 static void
 391 exec_extension_cd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 392 {
 393     char *q;
 394     vfs_path_t *p_vpath;
 395 
 396     *pbuffer = '\0';
 397     pbuffer = buffer;
 398     /* Search last non-space character. Start search at the end in order
 399        not to short filenames containing spaces. */
 400     q = pbuffer + strlen (pbuffer) - 1;
 401     while (q >= pbuffer && whitespace (*q))
 402         q--;
 403     q[1] = 0;
 404 
 405     p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
 406     do_cd (p_vpath, cd_parse_command);
 407     vfs_path_free (p_vpath);
 408 }
 409 
 410 
 411 /* --------------------------------------------------------------------------------------------- */
 412 
 413 static vfs_path_t *
 414 exec_extension (void *target, const vfs_path_t * filename_vpath, const char *lc_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 415                 int start_line)
 416 {
 417     char *shell_string, *export_variables;
 418     vfs_path_t *script_vpath = NULL;
 419     int cmd_file_fd;
 420     FILE *cmd_file;
 421     char *cmd = NULL;
 422 
 423     g_return_val_if_fail (lc_data != NULL, NULL);
 424 
 425     pbuffer = NULL;
 426     localmtime = 0;
 427     quote_func = name_quote;
 428     run_view = FALSE;
 429     is_cd = FALSE;
 430     written_nonspace = FALSE;
 431 
 432     /* Avoid making a local copy if we are doing a cd */
 433     do_local_copy = !vfs_file_is_local (filename_vpath);
 434 
 435     shell_string = exec_make_shell_string (lc_data, filename_vpath);
 436 
 437     if (shell_string == NULL)
 438         goto ret;
 439 
 440     if (is_cd)
 441     {
 442         exec_extension_cd ();
 443         g_free (shell_string);
 444         goto ret;
 445     }
 446 
 447     /*
 448      * All commands should be run in /bin/sh regardless of user shell.
 449      * To do that, create temporary shell script and run it.
 450      * Sometimes it's not needed (e.g. for %cd and %view commands),
 451      * but it's easier to create it anyway.
 452      */
 453     cmd_file_fd = mc_mkstemps (&script_vpath, "mcext", SCRIPT_SUFFIX);
 454 
 455     if (cmd_file_fd == -1)
 456     {
 457         message (D_ERROR, MSG_ERROR,
 458                  _("Cannot create temporary command file\n%s"), unix_error_string (errno));
 459         goto ret;
 460     }
 461 
 462     cmd_file = fdopen (cmd_file_fd, "w");
 463     fputs ("#! /bin/sh\n\n", cmd_file);
 464 
 465     export_variables = exec_get_export_variables (filename_vpath);
 466     if (export_variables != NULL)
 467     {
 468         fprintf (cmd_file, "%s\n", export_variables);
 469         g_free (export_variables);
 470     }
 471 
 472     fputs (shell_string, cmd_file);
 473     g_free (shell_string);
 474 
 475     /*
 476      * Make the script remove itself when it finishes.
 477      * Don't do it for the viewer - it may need to rerun the script,
 478      * so we clean up after calling view().
 479      */
 480     if (!run_view)
 481         fprintf (cmd_file, "\n/bin/rm -f %s\n", vfs_path_as_str (script_vpath));
 482 
 483     fclose (cmd_file);
 484 
 485     if ((run_view && !written_nonspace) || is_cd)
 486     {
 487         exec_cleanup_script (script_vpath);
 488         script_vpath = NULL;
 489     }
 490     else
 491     {
 492         /* Set executable flag on the command file ... */
 493         mc_chmod (script_vpath, S_IRWXU);
 494         /* ... but don't rely on it - run /bin/sh explicitly */
 495         cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (script_vpath), (char *) NULL);
 496     }
 497 
 498     if (run_view)
 499     {
 500         /* If we've written whitespace only, then just load filename into view */
 501         if (!written_nonspace)
 502             exec_extension_view (target, NULL, filename_vpath, start_line);
 503         else
 504             exec_extension_view (target, cmd, filename_vpath, start_line);
 505     }
 506     else
 507     {
 508         shell_execute (cmd, EXECUTE_INTERNAL);
 509         if (mc_global.tty.console_flag != '\0')
 510         {
 511             handle_console (CONSOLE_SAVE);
 512             if (output_lines && mc_global.keybar_visible)
 513                 show_console_contents (output_start_y,
 514                                        LINES - mc_global.keybar_visible -
 515                                        output_lines - 1, LINES - mc_global.keybar_visible - 1);
 516         }
 517     }
 518 
 519     g_free (cmd);
 520 
 521     exec_cleanup_file_name (filename_vpath, TRUE);
 522   ret:
 523     return script_vpath;
 524 }
 525 
 526 /* --------------------------------------------------------------------------------------------- */
 527 /**
 528  * Run cmd_file with args, put result into buf.
 529  * If error, put '\0' into buf[0]
 530  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 531  *
 532  * NOTES: buf is null-terminated string.
 533  */
 534 
 535 static int
 536 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 537 {
 538     gboolean read_bytes = FALSE;
 539     char *command;
 540     FILE *f;
 541 
 542     command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
 543     f = popen (command, "r");
 544     g_free (command);
 545 
 546     if (f != NULL)
 547     {
 548 #ifdef __QNXNTO__
 549         if (setvbuf (f, NULL, _IOFBF, 0) != 0)
 550         {
 551             (void) pclose (f);
 552             return -1;
 553         }
 554 #endif
 555         read_bytes = (fgets (buf, buflen, f) != NULL);
 556         if (!read_bytes)
 557             buf[0] = '\0';      /* Paranoid termination */
 558         pclose (f);
 559     }
 560     else
 561     {
 562         buf[0] = '\0';          /* Paranoid termination */
 563         return -1;
 564     }
 565 
 566     buf[buflen - 1] = '\0';
 567 
 568     return read_bytes ? 1 : 0;
 569 }
 570 
 571 /* --------------------------------------------------------------------------------------------- */
 572 /**
 573  * Run the "file" command on the local file.
 574  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 575  */
 576 
 577 static int
 578 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 579 {
 580     char *tmp;
 581     int ret;
 582 
 583     tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
 584     ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
 585     g_free (tmp);
 586 
 587     return ret;
 588 }
 589 
 590 /* --------------------------------------------------------------------------------------------- */
 591 /**
 592  * Run the "enca" command on the local file.
 593  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 594  */
 595 
 596 #ifdef HAVE_CHARSET
 597 static int
 598 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 599 {
 600     char *tmp, *lang, *args;
 601     int ret;
 602 
 603     tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
 604     lang = name_quote (autodetect_codeset, FALSE);
 605     args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
 606 
 607     ret = get_popen_information ("enca", args, buf, buflen);
 608 
 609     g_free (args);
 610     g_free (lang);
 611     g_free (tmp);
 612 
 613     return ret;
 614 }
 615 #endif /* HAVE_CHARSET */
 616 
 617 /* --------------------------------------------------------------------------------------------- */
 618 /**
 619  * Invoke the "file" command on the file and match its output against PTR.
 620  * have_type is a flag that is set if we already have tried to determine
 621  * the type of that file.
 622  * Return TRUE for match, FALSE otherwise.
 623  */
 624 
 625 static gboolean
 626 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, gboolean case_insense,
     /* [previous][next][first][last][top][bottom][index][help]  */
 627                   gboolean * have_type, GError ** mcerror)
 628 {
 629     gboolean found = FALSE;
 630 
 631     /* Following variables are valid if *have_type is TRUE */
 632     static char content_string[2048];
 633     static size_t content_shift = 0;
 634     static int got_data = 0;
 635 
 636     mc_return_val_if_error (mcerror, FALSE);
 637 
 638     if (!use_file_to_check_type)
 639         return FALSE;
 640 
 641     if (!*have_type)
 642     {
 643         vfs_path_t *localfile_vpath;
 644         const char *realname;   /* name used with "file" */
 645 
 646 #ifdef HAVE_CHARSET
 647         static char encoding_id[21];    /* CSISO51INISCYRILLIC -- 20 */
 648         int got_encoding_data;
 649 #endif /* HAVE_CHARSET */
 650 
 651         /* Don't repeate even unsuccessful checks */
 652         *have_type = TRUE;
 653 
 654         localfile_vpath = mc_getlocalcopy (filename_vpath);
 655         if (localfile_vpath == NULL)
 656         {
 657             mc_propagate_error (mcerror, 0, _("Cannot fetch a local copy of %s"),
 658                                 vfs_path_as_str (filename_vpath));
 659             return FALSE;
 660         }
 661 
 662         realname = vfs_path_get_last_path_str (localfile_vpath);
 663 
 664 #ifdef HAVE_CHARSET
 665         got_encoding_data = is_autodetect_codeset_enabled
 666             ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
 667 
 668         if (got_encoding_data > 0)
 669         {
 670             char *pp;
 671             int cp_id;
 672 
 673             pp = strchr (encoding_id, '\n');
 674             if (pp != NULL)
 675                 *pp = '\0';
 676 
 677             cp_id = get_codepage_index (encoding_id);
 678             if (cp_id == -1)
 679                 cp_id = default_source_codepage;
 680 
 681             do_set_codepage (cp_id);
 682         }
 683 #endif /* HAVE_CHARSET */
 684 
 685         got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
 686 
 687         mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
 688 
 689         if (got_data > 0)
 690         {
 691             char *pp;
 692             size_t real_len;
 693 
 694             pp = strchr (content_string, '\n');
 695             if (pp != NULL)
 696                 *pp = '\0';
 697 
 698             real_len = strlen (realname);
 699 
 700             if (strncmp (content_string, realname, real_len) == 0)
 701             {
 702                 /* Skip "realname: " */
 703                 content_shift = real_len;
 704                 if (content_string[content_shift] == ':')
 705                 {
 706                     /* Solaris' file prints tab(s) after ':' */
 707                     for (content_shift++; whitespace (content_string[content_shift]);
 708                          content_shift++)
 709                         ;
 710                 }
 711             }
 712         }
 713         else
 714         {
 715             /* No data */
 716             content_string[0] = '\0';
 717         }
 718         vfs_path_free (localfile_vpath);
 719     }
 720 
 721     if (got_data == -1)
 722     {
 723         mc_propagate_error (mcerror, 0, "%s", _("Pipe failed"));
 724         return FALSE;
 725     }
 726 
 727     if (content_string[0] != '\0')
 728     {
 729         mc_search_t *search;
 730 
 731         search = mc_search_new (ptr, DEFAULT_CHARSET);
 732         if (search != NULL)
 733         {
 734             search->search_type = MC_SEARCH_T_REGEX;
 735             search->is_case_sensitive = !case_insense;
 736             found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
 737             mc_search_free (search);
 738         }
 739         else
 740         {
 741             mc_propagate_error (mcerror, 0, "%s", _("Regular expression error"));
 742         }
 743     }
 744 
 745     return found;
 746 }
 747 
 748 /* --------------------------------------------------------------------------------------------- */
 749 /*** public functions ****************************************************************************/
 750 /* --------------------------------------------------------------------------------------------- */
 751 
 752 void
 753 flush_extension_file (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 754 {
 755     MC_PTR_FREE (data);
 756 }
 757 
 758 /* --------------------------------------------------------------------------------------------- */
 759 /**
 760  * The second argument is action, i.e. Open, View or Edit
 761  * Use target object to open file in.
 762  *
 763  * This function returns:
 764  *
 765  * -1 for a failure or user interrupt
 766  * 0 if no command was run
 767  * 1 if some command was run
 768  *
 769  * If action == "View" then a parameter is checked in the form of "View:%d",
 770  * if the value for %d exists, then the viewer is started up at that line number.
 771  */
 772 
 773 int
 774 regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action,
     /* [previous][next][first][last][top][bottom][index][help]  */
 775                    vfs_path_t ** script_vpath)
 776 {
 777     char *p, *q, *r, c;
 778     const char *filename;
 779     size_t file_len;
 780     gboolean found = FALSE;
 781     gboolean error_flag = FALSE;
 782     int ret = 0;
 783     struct stat mystat;
 784     int view_at_line_number = 0;
 785     char *include_target = NULL;
 786     size_t include_target_len = 0;
 787     gboolean have_type = FALSE; /* Flag used by regex_check_type() */
 788 
 789     if (filename_vpath == NULL)
 790         return 0;
 791 
 792     if (script_vpath != NULL)
 793         *script_vpath = NULL;
 794 
 795     /* Check for the special View:%d parameter */
 796     if (strncmp (action, "View:", 5) == 0)
 797     {
 798         view_at_line_number = atoi (action + 5);
 799         action = "View";
 800     }
 801 
 802     if (data == NULL)
 803     {
 804         char *extension_file;
 805         gboolean mc_user_ext = TRUE;
 806         gboolean home_error = FALSE;
 807 
 808         extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
 809         if (!exist_file (extension_file))
 810         {
 811             g_free (extension_file);
 812           check_stock_mc_ext:
 813             extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, (char *) NULL);
 814             if (!exist_file (extension_file))
 815             {
 816                 g_free (extension_file);
 817                 extension_file =
 818                     mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, (char *) NULL);
 819             }
 820             mc_user_ext = FALSE;
 821         }
 822 
 823         g_file_get_contents (extension_file, &data, NULL, NULL);
 824         g_free (extension_file);
 825         if (data == NULL)
 826             return 0;
 827 
 828         if (strstr (data, "default/") == NULL)
 829         {
 830             if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
 831                 strstr (data, "type/") == NULL)
 832             {
 833                 MC_PTR_FREE (data);
 834 
 835                 if (!mc_user_ext)
 836                 {
 837                     char *title;
 838 
 839                     title = g_strdup_printf (_(" %s%s file error"),
 840                                              mc_global.sysconfig_dir, MC_LIB_EXT);
 841                     message (D_ERROR, title, _("The format of the %smc.ext "
 842                                                "file has changed with version 3.0. It seems that "
 843                                                "the installation failed. Please fetch a fresh "
 844                                                "copy from the Midnight Commander package."),
 845                              mc_global.sysconfig_dir);
 846                     g_free (title);
 847                     return 0;
 848                 }
 849 
 850                 home_error = TRUE;
 851                 goto check_stock_mc_ext;
 852             }
 853         }
 854 
 855         if (home_error)
 856         {
 857             char *filebind_filename;
 858             char *title;
 859 
 860             filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
 861             title = g_strdup_printf (_("%s file error"), filebind_filename);
 862             message (D_ERROR, title,
 863                      _("The format of the %s file has "
 864                        "changed with version 3.0. You may either want to copy "
 865                        "it from %smc.ext or use that file as an example of how to write it."),
 866                      filebind_filename, mc_global.sysconfig_dir);
 867             g_free (filebind_filename);
 868             g_free (title);
 869         }
 870     }
 871 
 872     mc_stat (filename_vpath, &mystat);
 873 
 874     filename = vfs_path_get_last_path_str (filename_vpath);
 875     filename = x_basename (filename);
 876     file_len = strlen (filename);
 877 
 878     for (p = data; *p != '\0'; p++)
 879     {
 880         for (q = p; whitespace (*q); q++)
 881             ;
 882         if (*q == '\n' || *q == '\0')
 883             p = q;              /* empty line */
 884         if (*p == '#')          /* comment */
 885             while (*p != '\0' && *p != '\n')
 886                 p++;
 887         if (*p == '\n')
 888             continue;
 889         if (*p == '\0')
 890             break;
 891         if (p == q)
 892         {
 893             /* i.e. starts in the first column, should be keyword/descNL */
 894             gboolean case_insense;
 895 
 896             found = FALSE;
 897             q = strchr (p, '\n');
 898             if (q == NULL)
 899                 q = strchr (p, '\0');
 900             c = *q;
 901             *q = '\0';
 902             if (include_target != NULL)
 903             {
 904                 if ((strncmp (p, "include/", 8) == 0)
 905                     && (strncmp (p + 8, include_target, include_target_len) == 0))
 906                     found = TRUE;
 907             }
 908             else if (strncmp (p, "regex/", 6) == 0)
 909             {
 910                 mc_search_t *search;
 911 
 912                 p += 6;
 913                 case_insense = (strncmp (p, "i/", 2) == 0);
 914                 if (case_insense)
 915                     p += 2;
 916 
 917                 search = mc_search_new (p, DEFAULT_CHARSET);
 918                 if (search != NULL)
 919                 {
 920                     search->search_type = MC_SEARCH_T_REGEX;
 921                     search->is_case_sensitive = !case_insense;
 922                     found = mc_search_run (search, filename, 0, file_len, NULL);
 923                     mc_search_free (search);
 924                 }
 925             }
 926             else if (strncmp (p, "directory/", 10) == 0)
 927             {
 928                 if (S_ISDIR (mystat.st_mode)
 929                     && mc_search (p + 10, DEFAULT_CHARSET, vfs_path_as_str (filename_vpath),
 930                                   MC_SEARCH_T_REGEX))
 931                     found = TRUE;
 932             }
 933             else if (strncmp (p, "shell/", 6) == 0)
 934             {
 935                 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
 936 
 937                 p += 6;
 938                 case_insense = (strncmp (p, "i/", 2) == 0);
 939                 if (case_insense)
 940                 {
 941                     p += 2;
 942                     cmp_func = strncasecmp;
 943                 }
 944 
 945                 if (*p == '.' && file_len >= (size_t) (q - p))
 946                 {
 947                     if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
 948                         found = TRUE;
 949                 }
 950                 else
 951                 {
 952                     if ((size_t) (q - p) == file_len && cmp_func (p, filename, file_len) == 0)
 953                         found = TRUE;
 954                 }
 955             }
 956             else if (strncmp (p, "type/", 5) == 0)
 957             {
 958                 GError *mcerror = NULL;
 959 
 960                 p += 5;
 961 
 962                 case_insense = (strncmp (p, "i/", 2) == 0);
 963                 if (case_insense)
 964                     p += 2;
 965 
 966                 found = regex_check_type (filename_vpath, p, case_insense, &have_type, &mcerror);
 967                 if (mc_error_message (&mcerror, NULL))
 968                     error_flag = TRUE;  /* leave it if file cannot be opened */
 969             }
 970             else if (strncmp (p, "default/", 8) == 0)
 971                 found = TRUE;
 972 
 973             *q = c;
 974         }
 975         else
 976         {                       /* List of actions */
 977             p = q;
 978             q = strchr (p, '\n');
 979             if (q == NULL)
 980                 q = strchr (p, '\0');
 981             if (found && !error_flag)
 982             {
 983                 r = strchr (p, '=');
 984                 if (r != NULL)
 985                 {
 986                     c = *r;
 987                     *r = '\0';
 988                     if (strcmp (p, "Include") == 0)
 989                     {
 990                         char *t;
 991 
 992                         include_target = p + 8;
 993                         t = strchr (include_target, '\n');
 994 
 995                         if (t != NULL)
 996                             include_target_len = (size_t) (t - include_target);
 997                         else
 998                             include_target_len = strlen (include_target);
 999 
1000                         *r = c;
1001                         p = q;
1002                         found = FALSE;
1003 
1004                         if (*p == '\0')
1005                             break;
1006                         continue;
1007                     }
1008 
1009                     if (strcmp (action, p) != 0)
1010                         *r = c;
1011                     else
1012                     {
1013                         *r = c;
1014 
1015                         for (p = r + 1; whitespace (*p); p++)
1016                             ;
1017 
1018                         /* Empty commands just stop searching
1019                          * through, they don't do anything
1020                          */
1021                         if (p < q)
1022                         {
1023                             vfs_path_t *sv;
1024 
1025                             sv = exec_extension (target, filename_vpath, r + 1,
1026                                                  view_at_line_number);
1027                             if (script_vpath != NULL)
1028                                 *script_vpath = sv;
1029                             else
1030                                 exec_cleanup_script (sv);
1031 
1032                             ret = 1;
1033                         }
1034                         break;
1035                     }
1036                 }
1037             }
1038         }
1039 
1040         p = q;
1041         if (*p == '\0')
1042             break;
1043     }
1044 
1045     return (error_flag ? -1 : ret);
1046 }
1047 
1048 /* --------------------------------------------------------------------------------------------- */

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