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-2020
   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 != 0 && mc_global.keybar_visible)
 513             {
 514                 unsigned char end_line;
 515 
 516                 end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
 517                 show_console_contents (output_start_y, end_line - output_lines, end_line);
 518             }
 519         }
 520     }
 521 
 522     g_free (cmd);
 523 
 524     exec_cleanup_file_name (filename_vpath, TRUE);
 525   ret:
 526     return script_vpath;
 527 }
 528 
 529 /* --------------------------------------------------------------------------------------------- */
 530 /**
 531  * Run cmd_file with args, put result into buf.
 532  * If error, put '\0' into buf[0]
 533  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 534  *
 535  * NOTES: buf is null-terminated string.
 536  */
 537 
 538 static int
 539 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 540 {
 541     gboolean read_bytes = FALSE;
 542     char *command;
 543     FILE *f;
 544 
 545     command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
 546     f = popen (command, "r");
 547     g_free (command);
 548 
 549     if (f != NULL)
 550     {
 551 #ifdef __QNXNTO__
 552         if (setvbuf (f, NULL, _IOFBF, 0) != 0)
 553         {
 554             (void) pclose (f);
 555             return -1;
 556         }
 557 #endif
 558         read_bytes = (fgets (buf, buflen, f) != NULL);
 559         if (!read_bytes)
 560             buf[0] = '\0';      /* Paranoid termination */
 561         pclose (f);
 562     }
 563     else
 564     {
 565         buf[0] = '\0';          /* Paranoid termination */
 566         return -1;
 567     }
 568 
 569     buf[buflen - 1] = '\0';
 570 
 571     return read_bytes ? 1 : 0;
 572 }
 573 
 574 /* --------------------------------------------------------------------------------------------- */
 575 /**
 576  * Run the "file" command on the local file.
 577  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 578  */
 579 
 580 static int
 581 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 582 {
 583     char *tmp;
 584     int ret;
 585 
 586     tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
 587     ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
 588     g_free (tmp);
 589 
 590     return ret;
 591 }
 592 
 593 /* --------------------------------------------------------------------------------------------- */
 594 /**
 595  * Run the "enca" command on the local file.
 596  * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
 597  */
 598 
 599 #ifdef HAVE_CHARSET
 600 static int
 601 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
     /* [previous][next][first][last][top][bottom][index][help]  */
 602 {
 603     char *tmp, *lang, *args;
 604     int ret;
 605 
 606     tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
 607     lang = name_quote (autodetect_codeset, FALSE);
 608     args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
 609 
 610     ret = get_popen_information ("enca", args, buf, buflen);
 611 
 612     g_free (args);
 613     g_free (lang);
 614     g_free (tmp);
 615 
 616     return ret;
 617 }
 618 #endif /* HAVE_CHARSET */
 619 
 620 /* --------------------------------------------------------------------------------------------- */
 621 /**
 622  * Invoke the "file" command on the file and match its output against PTR.
 623  * have_type is a flag that is set if we already have tried to determine
 624  * the type of that file.
 625  * Return TRUE for match, FALSE otherwise.
 626  */
 627 
 628 static gboolean
 629 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, gboolean case_insense,
     /* [previous][next][first][last][top][bottom][index][help]  */
 630                   gboolean * have_type, GError ** mcerror)
 631 {
 632     gboolean found = FALSE;
 633 
 634     /* Following variables are valid if *have_type is TRUE */
 635     static char content_string[2048];
 636     static size_t content_shift = 0;
 637     static int got_data = 0;
 638 
 639     mc_return_val_if_error (mcerror, FALSE);
 640 
 641     if (!use_file_to_check_type)
 642         return FALSE;
 643 
 644     if (!*have_type)
 645     {
 646         vfs_path_t *localfile_vpath;
 647         const char *realname;   /* name used with "file" */
 648 
 649 #ifdef HAVE_CHARSET
 650         static char encoding_id[21];    /* CSISO51INISCYRILLIC -- 20 */
 651         int got_encoding_data;
 652 #endif /* HAVE_CHARSET */
 653 
 654         /* Don't repeate even unsuccessful checks */
 655         *have_type = TRUE;
 656 
 657         localfile_vpath = mc_getlocalcopy (filename_vpath);
 658         if (localfile_vpath == NULL)
 659         {
 660             mc_propagate_error (mcerror, 0, _("Cannot fetch a local copy of %s"),
 661                                 vfs_path_as_str (filename_vpath));
 662             return FALSE;
 663         }
 664 
 665         realname = vfs_path_get_last_path_str (localfile_vpath);
 666 
 667 #ifdef HAVE_CHARSET
 668         got_encoding_data = is_autodetect_codeset_enabled
 669             ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
 670 
 671         if (got_encoding_data > 0)
 672         {
 673             char *pp;
 674             int cp_id;
 675 
 676             pp = strchr (encoding_id, '\n');
 677             if (pp != NULL)
 678                 *pp = '\0';
 679 
 680             cp_id = get_codepage_index (encoding_id);
 681             if (cp_id == -1)
 682                 cp_id = default_source_codepage;
 683 
 684             do_set_codepage (cp_id);
 685         }
 686 #endif /* HAVE_CHARSET */
 687 
 688         got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
 689 
 690         mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
 691 
 692         if (got_data > 0)
 693         {
 694             char *pp;
 695             size_t real_len;
 696 
 697             pp = strchr (content_string, '\n');
 698             if (pp != NULL)
 699                 *pp = '\0';
 700 
 701             real_len = strlen (realname);
 702 
 703             if (strncmp (content_string, realname, real_len) == 0)
 704             {
 705                 /* Skip "realname: " */
 706                 content_shift = real_len;
 707                 if (content_string[content_shift] == ':')
 708                 {
 709                     /* Solaris' file prints tab(s) after ':' */
 710                     for (content_shift++; whitespace (content_string[content_shift]);
 711                          content_shift++)
 712                         ;
 713                 }
 714             }
 715         }
 716         else
 717         {
 718             /* No data */
 719             content_string[0] = '\0';
 720         }
 721         vfs_path_free (localfile_vpath);
 722     }
 723 
 724     if (got_data == -1)
 725     {
 726         mc_propagate_error (mcerror, 0, "%s", _("Pipe failed"));
 727         return FALSE;
 728     }
 729 
 730     if (content_string[0] != '\0')
 731     {
 732         mc_search_t *search;
 733 
 734         search = mc_search_new (ptr, DEFAULT_CHARSET);
 735         if (search != NULL)
 736         {
 737             search->search_type = MC_SEARCH_T_REGEX;
 738             search->is_case_sensitive = !case_insense;
 739             found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
 740             mc_search_free (search);
 741         }
 742         else
 743         {
 744             mc_propagate_error (mcerror, 0, "%s", _("Regular expression error"));
 745         }
 746     }
 747 
 748     return found;
 749 }
 750 
 751 /* --------------------------------------------------------------------------------------------- */
 752 /*** public functions ****************************************************************************/
 753 /* --------------------------------------------------------------------------------------------- */
 754 
 755 void
 756 flush_extension_file (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 757 {
 758     MC_PTR_FREE (data);
 759 }
 760 
 761 /* --------------------------------------------------------------------------------------------- */
 762 /**
 763  * The second argument is action, i.e. Open, View or Edit
 764  * Use target object to open file in.
 765  *
 766  * This function returns:
 767  *
 768  * -1 for a failure or user interrupt
 769  * 0 if no command was run
 770  * 1 if some command was run
 771  *
 772  * If action == "View" then a parameter is checked in the form of "View:%d",
 773  * if the value for %d exists, then the viewer is started up at that line number.
 774  */
 775 
 776 int
 777 regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action,
     /* [previous][next][first][last][top][bottom][index][help]  */
 778                    vfs_path_t ** script_vpath)
 779 {
 780     char *p, *q, *r, c;
 781     const char *filename;
 782     size_t file_len;
 783     gboolean found = FALSE;
 784     gboolean error_flag = FALSE;
 785     int ret = 0;
 786     struct stat mystat;
 787     int view_at_line_number = 0;
 788     char *include_target = NULL;
 789     size_t include_target_len = 0;
 790     gboolean have_type = FALSE; /* Flag used by regex_check_type() */
 791 
 792     if (filename_vpath == NULL)
 793         return 0;
 794 
 795     if (script_vpath != NULL)
 796         *script_vpath = NULL;
 797 
 798     /* Check for the special View:%d parameter */
 799     if (strncmp (action, "View:", 5) == 0)
 800     {
 801         view_at_line_number = atoi (action + 5);
 802         action = "View";
 803     }
 804 
 805     if (data == NULL)
 806     {
 807         char *extension_file;
 808         gboolean mc_user_ext = TRUE;
 809         gboolean home_error = FALSE;
 810 
 811         extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
 812         if (!exist_file (extension_file))
 813         {
 814             g_free (extension_file);
 815           check_stock_mc_ext:
 816             extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, (char *) NULL);
 817             if (!exist_file (extension_file))
 818             {
 819                 g_free (extension_file);
 820                 extension_file =
 821                     mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, (char *) NULL);
 822             }
 823             mc_user_ext = FALSE;
 824         }
 825 
 826         g_file_get_contents (extension_file, &data, NULL, NULL);
 827         g_free (extension_file);
 828         if (data == NULL)
 829             return 0;
 830 
 831         if (strstr (data, "default/") == NULL)
 832         {
 833             if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
 834                 strstr (data, "type/") == NULL)
 835             {
 836                 MC_PTR_FREE (data);
 837 
 838                 if (!mc_user_ext)
 839                 {
 840                     char *title;
 841 
 842                     title = g_strdup_printf (_(" %s%s file error"),
 843                                              mc_global.sysconfig_dir, MC_LIB_EXT);
 844                     message (D_ERROR, title, _("The format of the %smc.ext "
 845                                                "file has changed with version 3.0. It seems that "
 846                                                "the installation failed. Please fetch a fresh "
 847                                                "copy from the Midnight Commander package."),
 848                              mc_global.sysconfig_dir);
 849                     g_free (title);
 850                     return 0;
 851                 }
 852 
 853                 home_error = TRUE;
 854                 goto check_stock_mc_ext;
 855             }
 856         }
 857 
 858         if (home_error)
 859         {
 860             char *filebind_filename;
 861             char *title;
 862 
 863             filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
 864             title = g_strdup_printf (_("%s file error"), filebind_filename);
 865             message (D_ERROR, title,
 866                      _("The format of the %s file has "
 867                        "changed with version 3.0. You may either want to copy "
 868                        "it from %smc.ext or use that file as an example of how to write it."),
 869                      filebind_filename, mc_global.sysconfig_dir);
 870             g_free (filebind_filename);
 871             g_free (title);
 872         }
 873     }
 874 
 875     mc_stat (filename_vpath, &mystat);
 876 
 877     filename = vfs_path_get_last_path_str (filename_vpath);
 878     filename = x_basename (filename);
 879     file_len = strlen (filename);
 880 
 881     for (p = data; *p != '\0'; p++)
 882     {
 883         for (q = p; whitespace (*q); q++)
 884             ;
 885         if (*q == '\n' || *q == '\0')
 886             p = q;              /* empty line */
 887         if (*p == '#')          /* comment */
 888             while (*p != '\0' && *p != '\n')
 889                 p++;
 890         if (*p == '\n')
 891             continue;
 892         if (*p == '\0')
 893             break;
 894         if (p == q)
 895         {
 896             /* i.e. starts in the first column, should be keyword/descNL */
 897             gboolean case_insense;
 898 
 899             found = FALSE;
 900             q = strchr (p, '\n');
 901             if (q == NULL)
 902                 q = strchr (p, '\0');
 903             c = *q;
 904             *q = '\0';
 905             if (include_target != NULL)
 906             {
 907                 if ((strncmp (p, "include/", 8) == 0)
 908                     && (strncmp (p + 8, include_target, include_target_len) == 0))
 909                     found = TRUE;
 910             }
 911             else if (strncmp (p, "regex/", 6) == 0)
 912             {
 913                 mc_search_t *search;
 914 
 915                 p += 6;
 916                 case_insense = (strncmp (p, "i/", 2) == 0);
 917                 if (case_insense)
 918                     p += 2;
 919 
 920                 search = mc_search_new (p, DEFAULT_CHARSET);
 921                 if (search != NULL)
 922                 {
 923                     search->search_type = MC_SEARCH_T_REGEX;
 924                     search->is_case_sensitive = !case_insense;
 925                     found = mc_search_run (search, filename, 0, file_len, NULL);
 926                     mc_search_free (search);
 927                 }
 928             }
 929             else if (strncmp (p, "directory/", 10) == 0)
 930             {
 931                 if (S_ISDIR (mystat.st_mode)
 932                     && mc_search (p + 10, DEFAULT_CHARSET, vfs_path_as_str (filename_vpath),
 933                                   MC_SEARCH_T_REGEX))
 934                     found = TRUE;
 935             }
 936             else if (strncmp (p, "shell/", 6) == 0)
 937             {
 938                 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
 939 
 940                 p += 6;
 941                 case_insense = (strncmp (p, "i/", 2) == 0);
 942                 if (case_insense)
 943                 {
 944                     p += 2;
 945                     cmp_func = strncasecmp;
 946                 }
 947 
 948                 if (*p == '.' && file_len >= (size_t) (q - p))
 949                 {
 950                     if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
 951                         found = TRUE;
 952                 }
 953                 else
 954                 {
 955                     if ((size_t) (q - p) == file_len && cmp_func (p, filename, file_len) == 0)
 956                         found = TRUE;
 957                 }
 958             }
 959             else if (strncmp (p, "type/", 5) == 0)
 960             {
 961                 GError *mcerror = NULL;
 962 
 963                 p += 5;
 964 
 965                 case_insense = (strncmp (p, "i/", 2) == 0);
 966                 if (case_insense)
 967                     p += 2;
 968 
 969                 found = regex_check_type (filename_vpath, p, case_insense, &have_type, &mcerror);
 970                 if (mc_error_message (&mcerror, NULL))
 971                     error_flag = TRUE;  /* leave it if file cannot be opened */
 972             }
 973             else if (strncmp (p, "default/", 8) == 0)
 974                 found = TRUE;
 975 
 976             *q = c;
 977         }
 978         else
 979         {                       /* List of actions */
 980             p = q;
 981             q = strchr (p, '\n');
 982             if (q == NULL)
 983                 q = strchr (p, '\0');
 984             if (found && !error_flag)
 985             {
 986                 r = strchr (p, '=');
 987                 if (r != NULL)
 988                 {
 989                     c = *r;
 990                     *r = '\0';
 991                     if (strcmp (p, "Include") == 0)
 992                     {
 993                         char *t;
 994 
 995                         include_target = p + 8;
 996                         t = strchr (include_target, '\n');
 997 
 998                         if (t != NULL)
 999                             include_target_len = (size_t) (t - include_target);
1000                         else
1001                             include_target_len = strlen (include_target);
1002 
1003                         *r = c;
1004                         p = q;
1005                         found = FALSE;
1006 
1007                         if (*p == '\0')
1008                             break;
1009                         continue;
1010                     }
1011 
1012                     if (strcmp (action, p) != 0)
1013                         *r = c;
1014                     else
1015                     {
1016                         *r = c;
1017 
1018                         for (p = r + 1; whitespace (*p); p++)
1019                             ;
1020 
1021                         /* Empty commands just stop searching
1022                          * through, they don't do anything
1023                          */
1024                         if (p < q)
1025                         {
1026                             vfs_path_t *sv;
1027 
1028                             sv = exec_extension (target, filename_vpath, r + 1,
1029                                                  view_at_line_number);
1030                             if (script_vpath != NULL)
1031                                 *script_vpath = sv;
1032                             else
1033                                 exec_cleanup_script (sv);
1034 
1035                             ret = 1;
1036                         }
1037                         break;
1038                     }
1039                 }
1040             }
1041         }
1042 
1043         p = q;
1044         if (*p == '\0')
1045             break;
1046     }
1047 
1048     return (error_flag ? -1 : ret);
1049 }
1050 
1051 /* --------------------------------------------------------------------------------------------- */

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