Fix mode switch after CK_FileNext/CK_FilePrev.
[midnight-commander.git] / src / filemanager / ext.c
blobd8979102d09db84d83ca08b64cb475813b8ad9a1
1 /*
2 Extension dependent execution.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Jakub Jelinek, 1995
10 Miguel de Icaza, 1994
12 This file is part of the Midnight Commander.
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.
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.
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/>.
28 /** \file ext.c
29 * \brief Source: extension dependent execution
32 #include <config.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
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
53 #include "src/setup.h" /* use_file_to_check_type */
54 #include "src/execute.h"
55 #include "src/history.h"
57 #include "src/consaver/cons.saver.h"
58 #include "src/viewer/mcviewer.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h" /* do_set_codepage */
62 #endif
64 #include "panel.h" /* do_cd */
65 #include "usermenu.h"
67 #include "ext.h"
69 /*** global variables ****************************************************************************/
71 /*** file scope macro definitions ****************************************************************/
73 #ifdef FILE_L
74 #define FILE_CMD "file -L "
75 #else
76 #define FILE_CMD "file "
77 #endif
79 /*** file scope type declarations ****************************************************************/
81 typedef char *(*quote_func_t) (const char *name, int quote_percent);
83 /*** file scope variables ************************************************************************/
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
89 static char *data = NULL;
90 static vfs_path_t *localfilecopy_vpath = NULL;
91 static char buffer[BUF_1K];
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;
101 /* --------------------------------------------------------------------------------------------- */
102 /*** file scope functions ************************************************************************/
103 /* --------------------------------------------------------------------------------------------- */
105 static void
106 exec_cleanup_script (vfs_path_t * script_vpath)
108 if (script_vpath != NULL)
110 (void) mc_unlink (script_vpath);
111 vfs_path_free (script_vpath);
115 /* --------------------------------------------------------------------------------------------- */
117 static void
118 exec_cleanup_file_name (const vfs_path_t * filename_vpath, gboolean has_changed)
120 if (localfilecopy_vpath == NULL)
121 return;
123 if (has_changed)
125 struct stat mystat;
127 mc_stat (localfilecopy_vpath, &mystat);
128 has_changed = localmtime != mystat.st_mtime;
130 mc_ungetlocalcopy (filename_vpath, localfilecopy_vpath, has_changed);
131 vfs_path_free (localfilecopy_vpath);
132 localfilecopy_vpath = NULL;
135 /* --------------------------------------------------------------------------------------------- */
137 static char *
138 exec_get_file_name (const vfs_path_t * filename_vpath)
140 if (!do_local_copy)
141 return quote_func (vfs_path_get_last_path_str (filename_vpath), 0);
143 if (localfilecopy_vpath == NULL)
145 struct stat mystat;
146 localfilecopy_vpath = mc_getlocalcopy (filename_vpath);
147 if (localfilecopy_vpath == NULL)
148 return NULL;
150 mc_stat (localfilecopy_vpath, &mystat);
151 localmtime = mystat.st_mtime;
154 return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), 0);
157 /* --------------------------------------------------------------------------------------------- */
159 static char *
160 exec_expand_format (char symbol, gboolean is_result_quoted)
162 char *text;
164 text = expand_format (NULL, symbol, TRUE);
165 if (is_result_quoted && text != NULL)
167 char *quoted_text;
169 quoted_text = g_strdup_printf ("\"%s\"", text);
170 g_free (text);
171 text = quoted_text;
173 return text;
176 /* --------------------------------------------------------------------------------------------- */
178 static char *
179 exec_get_export_variables (const vfs_path_t * filename_vpath)
181 char *text;
182 GString *export_vars_string;
183 size_t i;
185 /* *INDENT-OFF* */
186 struct
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}
198 /* *INDENT-ON* */
200 text = exec_get_file_name (filename_vpath);
201 if (text == NULL)
202 return NULL;
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);
208 for (i = 0; export_variables[i].name != NULL; i++)
210 text =
211 exec_expand_format (export_variables[i].symbol, export_variables[i].is_result_quoted);
212 if (text != NULL)
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);
220 return g_string_free (export_vars_string, FALSE);
223 /* --------------------------------------------------------------------------------------------- */
225 static char *
226 exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
228 GString *shell_string;
229 char lc_prompt[80] = "\0";
230 gboolean parameter_found = FALSE;
231 gboolean expand_prefix_found = FALSE;
233 shell_string = g_string_new ("");
235 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
237 if (parameter_found)
239 if (*lc_data == '}')
241 char *parameter;
243 parameter_found = FALSE;
244 parameter =
245 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "",
246 INPUT_COMPLETE_NONE);
247 if (parameter == NULL)
249 /* User canceled */
250 g_string_free (shell_string, TRUE);
251 exec_cleanup_file_name (filename_vpath, FALSE);
252 return NULL;
254 g_string_append (shell_string, parameter);
255 written_nonspace = TRUE;
256 g_free (parameter);
258 else
260 size_t len = strlen (lc_prompt);
262 if (len < sizeof (lc_prompt) - 1)
264 lc_prompt[len] = *lc_data;
265 lc_prompt[len + 1] = '\0';
269 else if (expand_prefix_found)
271 expand_prefix_found = FALSE;
272 if (*lc_data == '{')
273 parameter_found = TRUE;
274 else
276 int i;
277 char *v;
279 i = check_format_view (lc_data);
280 if (i != 0)
282 lc_data += i - 1;
283 run_view = TRUE;
285 else
287 i = check_format_cd (lc_data);
288 if (i > 0)
290 is_cd = TRUE;
291 quote_func = fake_name_quote;
292 do_local_copy = FALSE;
293 pbuffer = buffer;
294 lc_data += i - 1;
296 else
298 i = check_format_var (lc_data, &v);
299 if (i > 0 && v != NULL)
301 g_string_append (shell_string, v);
302 g_free (v);
303 lc_data += i;
305 else
307 char *text;
309 if (*lc_data != 'f')
310 text = expand_format (NULL, *lc_data, !is_cd);
311 else
313 text = exec_get_file_name (filename_vpath);
314 if (text == NULL)
316 g_string_free (shell_string, TRUE);
317 return NULL;
321 if (!is_cd)
322 g_string_append (shell_string, text);
323 else
325 strcpy (pbuffer, text);
326 pbuffer = strchr (pbuffer, 0);
329 g_free (text);
330 written_nonspace = TRUE;
336 else if (*lc_data == '%')
337 expand_prefix_found = TRUE;
338 else
340 if (*lc_data != ' ' && *lc_data != '\t')
341 written_nonspace = TRUE;
342 if (is_cd)
343 *(pbuffer++) = *lc_data;
344 else
345 g_string_append_c (shell_string, *lc_data);
347 } /* for */
348 return g_string_free (shell_string, FALSE);
351 /* --------------------------------------------------------------------------------------------- */
353 static void
354 exec_extension_view (void *target, char *cmd, const vfs_path_t * filename_vpath, int start_line)
356 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
357 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
359 mcview_altered_hex_mode = 0;
360 mcview_altered_nroff_flag = 0;
361 if (def_hex_mode != mcview_default_hex_mode)
362 changed_hex_mode = 1;
363 if (def_nroff_flag != mcview_default_nroff_flag)
364 changed_nroff_flag = 1;
366 if (target == NULL)
367 mcview_viewer (cmd, filename_vpath, start_line);
368 else
370 char *file_name;
372 file_name = vfs_path_to_str (filename_vpath);
373 mcview_load ((mcview_t *) target, cmd, file_name, start_line);
374 g_free (file_name);
377 if (changed_hex_mode && !mcview_altered_hex_mode)
378 mcview_default_hex_mode = def_hex_mode;
379 if (changed_nroff_flag && !mcview_altered_nroff_flag)
380 mcview_default_nroff_flag = def_nroff_flag;
382 dialog_switch_process_pending ();
385 /* --------------------------------------------------------------------------------------------- */
387 static void
388 exec_extension_cd (void)
390 char *q;
391 vfs_path_t *p_vpath;
393 *pbuffer = '\0';
394 pbuffer = buffer;
395 /* while (*p == ' ' && *p == '\t')
396 * p++;
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 && (*q == ' ' || *q == '\t'))
402 q--;
403 q[1] = 0;
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);
411 /* --------------------------------------------------------------------------------------------- */
413 static vfs_path_t *
414 exec_extension (void *target, const vfs_path_t * filename_vpath, const char *lc_data, int start_line)
416 char *shell_string, *export_variables;
417 vfs_path_t *script_vpath = NULL;
418 int cmd_file_fd;
419 FILE *cmd_file;
420 char *cmd = NULL;
422 g_return_val_if_fail (lc_data != NULL, NULL);
424 pbuffer = NULL;
425 localmtime = 0;
426 quote_func = name_quote;
427 run_view = FALSE;
428 is_cd = FALSE;
429 written_nonspace = FALSE;
431 /* Avoid making a local copy if we are doing a cd */
432 do_local_copy = !vfs_file_is_local (filename_vpath);
434 shell_string = exec_make_shell_string (lc_data, filename_vpath);
436 if (shell_string == NULL)
437 goto ret;
439 if (is_cd)
441 exec_extension_cd ();
442 g_free (shell_string);
443 goto ret;
447 * All commands should be run in /bin/sh regardless of user shell.
448 * To do that, create temporary shell script and run it.
449 * Sometimes it's not needed (e.g. for %cd and %view commands),
450 * but it's easier to create it anyway.
452 cmd_file_fd = mc_mkstemps (&script_vpath, "mcext", SCRIPT_SUFFIX);
454 if (cmd_file_fd == -1)
456 message (D_ERROR, MSG_ERROR,
457 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
458 goto ret;
461 cmd_file = fdopen (cmd_file_fd, "w");
462 fputs ("#! /bin/sh\n\n", cmd_file);
464 export_variables = exec_get_export_variables (filename_vpath);
465 if (export_variables != NULL)
467 fprintf (cmd_file, "%s\n", export_variables);
468 g_free (export_variables);
471 fputs (shell_string, cmd_file);
472 g_free (shell_string);
475 * Make the script remove itself when it finishes.
476 * Don't do it for the viewer - it may need to rerun the script,
477 * so we clean up after calling view().
479 if (!run_view)
481 char *file_name;
483 file_name = vfs_path_to_str (script_vpath);
484 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
485 g_free (file_name);
488 fclose (cmd_file);
490 if ((run_view && !written_nonspace) || is_cd)
492 exec_cleanup_script (script_vpath);
493 script_vpath = NULL;
495 else
497 char *file_name;
499 file_name = vfs_path_to_str (script_vpath);
500 /* Set executable flag on the command file ... */
501 mc_chmod (script_vpath, S_IRWXU);
502 /* ... but don't rely on it - run /bin/sh explicitly */
503 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
504 g_free (file_name);
507 if (run_view)
509 /* If we've written whitespace only, then just load filename into view */
510 if (!written_nonspace)
511 exec_extension_view (target, NULL, filename_vpath, start_line);
512 else
513 exec_extension_view (target, cmd, filename_vpath, start_line);
515 else
517 shell_execute (cmd, EXECUTE_INTERNAL);
518 if (mc_global.tty.console_flag != '\0')
520 handle_console (CONSOLE_SAVE);
521 if (output_lines && mc_global.keybar_visible)
522 show_console_contents (output_start_y,
523 LINES - mc_global.keybar_visible -
524 output_lines - 1, LINES - mc_global.keybar_visible - 1);
528 g_free (cmd);
530 exec_cleanup_file_name (filename_vpath, TRUE);
531 ret:
532 return script_vpath;
535 /* --------------------------------------------------------------------------------------------- */
537 * Run cmd_file with args, put result into buf.
538 * If error, put '\0' into buf[0]
539 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
541 * NOTES: buf is null-terminated string.
544 static int
545 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
547 gboolean read_bytes = FALSE;
548 char *command;
549 FILE *f;
551 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
552 f = popen (command, "r");
553 g_free (command);
555 if (f != NULL)
557 #ifdef __QNXNTO__
558 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
560 (void) pclose (f);
561 return -1;
563 #endif
564 read_bytes = (fgets (buf, buflen, f) != NULL);
565 if (!read_bytes)
566 buf[0] = '\0'; /* Paranoid termination */
567 pclose (f);
569 else
571 buf[0] = '\0'; /* Paranoid termination */
572 return -1;
575 buf[buflen - 1] = '\0';
577 return read_bytes ? 1 : 0;
580 /* --------------------------------------------------------------------------------------------- */
582 * Run the "file" command on the local file.
583 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
586 static int
587 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
589 char *tmp;
590 int ret;
592 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
593 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
594 g_free (tmp);
596 return ret;
599 /* --------------------------------------------------------------------------------------------- */
601 * Run the "enca" command on the local file.
602 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
605 #ifdef HAVE_CHARSET
606 static int
607 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
609 char *tmp, *lang, *args;
610 int ret;
612 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
613 lang = name_quote (autodetect_codeset, 0);
614 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
616 ret = get_popen_information ("enca", args, buf, buflen);
618 g_free (args);
619 g_free (lang);
620 g_free (tmp);
622 return ret;
624 #endif /* HAVE_CHARSET */
626 /* --------------------------------------------------------------------------------------------- */
628 * Invoke the "file" command on the file and match its output against PTR.
629 * have_type is a flag that is set if we already have tried to determine
630 * the type of that file.
631 * Return 1 for match, 0 for no match, -1 errors.
634 static gboolean
635 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type,
636 gboolean case_insense, GError ** error)
638 gboolean found = FALSE;
640 /* Following variables are valid if *have_type is 1 */
641 static char content_string[2048];
642 #ifdef HAVE_CHARSET
643 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
644 #endif
645 static size_t content_shift = 0;
646 static int got_data = 0;
648 if (!use_file_to_check_type)
649 return FALSE;
651 if (*have_type == 0)
653 vfs_path_t *localfile_vpath;
654 const char *realname; /* name used with "file" */
656 #ifdef HAVE_CHARSET
657 int got_encoding_data;
658 #endif /* HAVE_CHARSET */
660 /* Don't repeate even unsuccessful checks */
661 *have_type = 1;
663 localfile_vpath = mc_getlocalcopy (filename_vpath);
664 if (localfile_vpath == NULL)
666 char *filename;
668 filename = vfs_path_to_str (filename_vpath);
669 g_propagate_error (error,
670 g_error_new (MC_ERROR, -1,
671 _("Cannot fetch a local copy of %s"), filename));
672 g_free (filename);
673 return FALSE;
676 realname = vfs_path_get_last_path_str (localfile_vpath);
678 #ifdef HAVE_CHARSET
679 got_encoding_data = is_autodetect_codeset_enabled
680 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
682 if (got_encoding_data > 0)
684 char *pp;
685 int cp_id;
687 pp = strchr (encoding_id, '\n');
688 if (pp != NULL)
689 *pp = '\0';
691 cp_id = get_codepage_index (encoding_id);
692 if (cp_id == -1)
693 cp_id = default_source_codepage;
695 do_set_codepage (cp_id);
697 #endif /* HAVE_CHARSET */
699 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
701 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
703 if (got_data > 0)
705 char *pp;
706 size_t real_len;
708 pp = strchr (content_string, '\n');
709 if (pp != NULL)
710 *pp = '\0';
712 real_len = strlen (realname);
714 if (strncmp (content_string, realname, real_len) == 0)
716 /* Skip "realname: " */
717 content_shift = real_len;
718 if (content_string[content_shift] == ':')
720 /* Solaris' file prints tab(s) after ':' */
721 for (content_shift++;
722 content_string[content_shift] == ' '
723 || content_string[content_shift] == '\t'; content_shift++)
728 else
730 /* No data */
731 content_string[0] = '\0';
733 vfs_path_free (localfile_vpath);
736 if (got_data == -1)
738 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed")));
739 return FALSE;
742 if (content_string[0] != '\0')
744 mc_search_t *search;
746 search = mc_search_new (ptr, -1);
747 if (search != NULL)
749 search->search_type = MC_SEARCH_T_REGEX;
750 search->is_case_sensitive = !case_insense;
751 found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
752 mc_search_free (search);
754 else
756 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error")));
760 return found;
763 /* --------------------------------------------------------------------------------------------- */
764 /*** public functions ****************************************************************************/
765 /* --------------------------------------------------------------------------------------------- */
767 void
768 flush_extension_file (void)
770 g_free (data);
771 data = NULL;
774 /* --------------------------------------------------------------------------------------------- */
776 * The second argument is action, i.e. Open, View or Edit
777 * Use target object to open file in.
779 * This function returns:
781 * -1 for a failure or user interrupt
782 * 0 if no command was run
783 * 1 if some command was run
785 * If action == "View" then a parameter is checked in the form of "View:%d",
786 * if the value for %d exists, then the viewer is started up at that line number.
790 regex_command_for (void *target, const vfs_path_t * filename_vpath, const char *action,
791 vfs_path_t ** script_vpath)
793 char *filename, *p, *q, *r, c;
794 size_t file_len;
795 gboolean found = FALSE;
796 gboolean error_flag = FALSE;
797 int ret = 0;
798 struct stat mystat;
799 int view_at_line_number;
800 char *include_target;
801 int include_target_len;
802 int have_type = 0; /* Flag used by regex_check_type() */
804 if (filename_vpath == NULL)
805 return 0;
807 if (script_vpath != NULL)
808 *script_vpath = NULL;
810 /* Check for the special View:%d parameter */
811 if (strncmp (action, "View:", 5) == 0)
813 view_at_line_number = atoi (action + 5);
814 action = "View";
816 else
818 view_at_line_number = 0;
821 if (data == NULL)
823 char *extension_file;
824 gboolean mc_user_ext = TRUE;
825 gboolean home_error = FALSE;
827 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
828 if (!exist_file (extension_file))
830 g_free (extension_file);
831 check_stock_mc_ext:
832 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
833 if (!exist_file (extension_file))
835 g_free (extension_file);
836 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
838 mc_user_ext = FALSE;
841 g_file_get_contents (extension_file, &data, NULL, NULL);
842 g_free (extension_file);
843 if (data == NULL)
844 return 0;
846 if (strstr (data, "default/") == NULL)
848 if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
849 strstr (data, "type/") == NULL)
851 g_free (data);
852 data = NULL;
854 if (!mc_user_ext)
856 char *title;
858 title = g_strdup_printf (_(" %s%s file error"),
859 mc_global.sysconfig_dir, MC_LIB_EXT);
860 message (D_ERROR, title, _("The format of the %smc.ext "
861 "file has changed with version 3.0. It seems that "
862 "the installation failed. Please fetch a fresh "
863 "copy from the Midnight Commander package."),
864 mc_global.sysconfig_dir);
865 g_free (title);
866 return 0;
869 home_error = TRUE;
870 goto check_stock_mc_ext;
874 if (home_error)
876 char *filebind_filename;
877 char *title;
879 filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
880 title = g_strdup_printf (_("%s file error"), filebind_filename);
881 message (D_ERROR, title,
882 _("The format of the %s file has "
883 "changed with version 3.0. You may either want to copy "
884 "it from %smc.ext or use that file as an example of how to write it."),
885 filebind_filename, mc_global.sysconfig_dir);
886 g_free (filebind_filename);
887 g_free (title);
891 mc_stat (filename_vpath, &mystat);
893 include_target = NULL;
894 include_target_len = 0;
895 filename = vfs_path_to_str (filename_vpath);
896 file_len = vfs_path_len (filename_vpath);
898 for (p = data; *p != '\0'; p++)
900 for (q = p; *q == ' ' || *q == '\t'; q++)
902 if (*q == '\n' || *q == '\0')
903 p = q; /* empty line */
904 if (*p == '#') /* comment */
905 while (*p != '\0' && *p != '\n')
906 p++;
907 if (*p == '\n')
908 continue;
909 if (*p == '\0')
910 break;
911 if (p == q)
912 { /* i.e. starts in the first column, should be
913 * keyword/descNL
915 gboolean case_insense;
917 found = FALSE;
918 q = strchr (p, '\n');
919 if (q == NULL)
920 q = strchr (p, '\0');
921 c = *q;
922 *q = '\0';
923 if (include_target)
925 if ((strncmp (p, "include/", 8) == 0)
926 && (strncmp (p + 8, include_target, include_target_len) == 0))
927 found = TRUE;
929 else if (strncmp (p, "regex/", 6) == 0)
931 mc_search_t *search;
933 p += 6;
934 case_insense = (strncmp (p, "i/", 2) == 0);
935 if (case_insense)
936 p += 2;
938 search = mc_search_new (p, -1);
939 if (search != NULL)
941 search->search_type = MC_SEARCH_T_REGEX;
942 search->is_case_sensitive = !case_insense;
943 found = mc_search_run (search, filename, 0, file_len, NULL);
944 mc_search_free (search);
947 else if (strncmp (p, "directory/", 10) == 0)
949 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
950 found = TRUE;
952 else if (strncmp (p, "shell/", 6) == 0)
954 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
956 p += 6;
957 case_insense = (strncmp (p, "i/", 2) == 0);
958 if (case_insense)
960 p += 2;
961 cmp_func = strncasecmp;
964 if (*p == '.' && file_len >= (size_t) (q - p))
966 if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
967 found = TRUE;
969 else
971 if ((size_t) (q - p) == file_len && cmp_func (p, filename, q - p) == 0)
972 found = TRUE;
975 else if (strncmp (p, "type/", 5) == 0)
977 GError *error = NULL;
979 p += 5;
981 case_insense = (strncmp (p, "i/", 2) == 0);
982 if (case_insense)
983 p += 2;
985 found = regex_check_type (filename_vpath, p, &have_type, case_insense, &error);
986 if (error != NULL)
988 g_error_free (error);
989 error_flag = TRUE; /* leave it if file cannot be opened */
992 else if (strncmp (p, "default/", 8) == 0)
993 found = TRUE;
995 *q = c;
996 p = q;
997 if (*p == '\0')
998 break;
1000 else
1001 { /* List of actions */
1002 p = q;
1003 q = strchr (p, '\n');
1004 if (q == NULL)
1005 q = strchr (p, '\0');
1006 if (found && !error_flag)
1008 r = strchr (p, '=');
1009 if (r != NULL)
1011 c = *r;
1012 *r = '\0';
1013 if (strcmp (p, "Include") == 0)
1015 char *t;
1017 include_target = p + 8;
1018 t = strchr (include_target, '\n');
1019 if (t != NULL)
1020 *t = '\0';
1021 include_target_len = strlen (include_target);
1022 if (t != NULL)
1023 *t = '\n';
1025 *r = c;
1026 p = q;
1027 found = FALSE;
1029 if (*p == '\0')
1030 break;
1031 continue;
1034 if (strcmp (action, p) != 0)
1035 *r = c;
1036 else
1038 *r = c;
1040 for (p = r + 1; *p == ' ' || *p == '\t'; p++)
1043 /* Empty commands just stop searching
1044 * through, they don't do anything
1046 if (p < q)
1048 vfs_path_t *sv;
1050 sv = exec_extension (target, filename_vpath, r + 1, view_at_line_number);
1051 if (script_vpath != NULL)
1052 *script_vpath = sv;
1053 else
1054 exec_cleanup_script (sv);
1056 ret = 1;
1058 break;
1062 p = q;
1063 if (*p == '\0')
1064 break;
1067 g_free (filename);
1068 if (error_flag)
1069 ret = -1;
1070 return ret;
1073 /* --------------------------------------------------------------------------------------------- */