Merge branch '2944_cleanup'
[midnight-commander.git] / src / filemanager / ext.c
blobd2978d297ff5af977db9178e029a50e421e648ed
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 /*** file scope functions ************************************************************************/
102 /* --------------------------------------------------------------------------------------------- */
104 static void
105 exec_cleanup_file_name (const vfs_path_t * filename_vpath, gboolean has_changed)
107 if (localfilecopy_vpath == NULL)
108 return;
110 if (has_changed)
112 struct stat mystat;
114 mc_stat (localfilecopy_vpath, &mystat);
115 has_changed = localmtime != mystat.st_mtime;
117 mc_ungetlocalcopy (filename_vpath, localfilecopy_vpath, has_changed);
118 vfs_path_free (localfilecopy_vpath);
119 localfilecopy_vpath = NULL;
122 /* --------------------------------------------------------------------------------------------- */
124 static char *
125 exec_get_file_name (const vfs_path_t * filename_vpath)
127 if (!do_local_copy)
128 return quote_func (vfs_path_get_last_path_str (filename_vpath), 0);
130 if (localfilecopy_vpath == NULL)
132 struct stat mystat;
133 localfilecopy_vpath = mc_getlocalcopy (filename_vpath);
134 if (localfilecopy_vpath == NULL)
135 return NULL;
137 mc_stat (localfilecopy_vpath, &mystat);
138 localmtime = mystat.st_mtime;
141 return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), 0);
144 /* --------------------------------------------------------------------------------------------- */
145 static char *
146 exec_expand_format (char symbol, gboolean is_result_quoted)
148 char *text;
150 text = expand_format (NULL, symbol, TRUE);
151 if (is_result_quoted && text != NULL)
153 char *quoted_text;
155 quoted_text = g_strdup_printf ("\"%s\"", text);
156 g_free (text);
157 text = quoted_text;
159 return text;
162 /* --------------------------------------------------------------------------------------------- */
164 static char *
165 exec_get_export_variables (const vfs_path_t * filename_vpath)
167 char *text;
168 GString *export_vars_string;
169 size_t i;
171 /* *INDENT-OFF* */
172 struct
174 const char symbol;
175 const char *name;
176 const gboolean is_result_quoted;
177 } export_variables[] = {
178 {'p', "MC_EXT_BASENAME", FALSE},
179 {'d', "MC_EXT_CURRENTDIR", FALSE},
180 {'s', "MC_EXT_SELECTED", TRUE},
181 {'t', "MC_EXT_ONLYTAGGED", TRUE},
182 {'\0', NULL, FALSE}
184 /* *INDENT-ON* */
186 text = exec_get_file_name (filename_vpath);
187 if (text == NULL)
188 return NULL;
190 export_vars_string = g_string_new ("MC_EXT_FILENAME=");
191 g_string_append_printf (export_vars_string, "%s\nexport MC_EXT_FILENAME\n", text);
192 g_free (text);
194 for (i = 0; export_variables[i].name != NULL; i++)
196 text =
197 exec_expand_format (export_variables[i].symbol, export_variables[i].is_result_quoted);
198 if (text != NULL)
200 g_string_append_printf (export_vars_string,
201 "%s=%s\nexport %s\n", export_variables[i].name, text,
202 export_variables[i].name);
203 g_free (text);
206 return g_string_free (export_vars_string, FALSE);
209 /* --------------------------------------------------------------------------------------------- */
211 static char *
212 exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
214 GString *shell_string;
215 char lc_prompt[80] = "\0";
216 gboolean parameter_found = FALSE;
217 gboolean expand_prefix_found = FALSE;
219 shell_string = g_string_new ("");
221 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
223 if (parameter_found)
225 if (*lc_data == '}')
227 char *parameter;
229 parameter_found = FALSE;
230 parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
231 if (parameter == NULL)
233 /* User canceled */
234 g_string_free (shell_string, TRUE);
235 exec_cleanup_file_name (filename_vpath, FALSE);
236 return NULL;
238 g_string_append (shell_string, parameter);
239 written_nonspace = TRUE;
240 g_free (parameter);
242 else
244 size_t len = strlen (lc_prompt);
246 if (len < sizeof (lc_prompt) - 1)
248 lc_prompt[len] = *lc_data;
249 lc_prompt[len + 1] = '\0';
253 else if (expand_prefix_found)
255 expand_prefix_found = FALSE;
256 if (*lc_data == '{')
257 parameter_found = TRUE;
258 else
260 int i;
261 char *v;
263 i = check_format_view (lc_data);
264 if (i != 0)
266 lc_data += i - 1;
267 run_view = TRUE;
269 else
271 i = check_format_cd (lc_data);
272 if (i > 0)
274 is_cd = TRUE;
275 quote_func = fake_name_quote;
276 do_local_copy = FALSE;
277 pbuffer = buffer;
278 lc_data += i - 1;
280 else
282 i = check_format_var (lc_data, &v);
283 if (i > 0 && v != NULL)
285 g_string_append (shell_string, v);
286 g_free (v);
287 lc_data += i;
289 else
291 char *text;
293 if (*lc_data != 'f')
294 text = expand_format (NULL, *lc_data, !is_cd);
295 else
297 text = exec_get_file_name (filename_vpath);
298 if (text == NULL)
300 g_string_free (shell_string, TRUE);
301 return NULL;
305 if (!is_cd)
306 g_string_append (shell_string, text);
307 else
309 strcpy (pbuffer, text);
310 pbuffer = strchr (pbuffer, 0);
313 g_free (text);
314 written_nonspace = TRUE;
320 else if (*lc_data == '%')
321 expand_prefix_found = TRUE;
322 else
324 if (*lc_data != ' ' && *lc_data != '\t')
325 written_nonspace = TRUE;
326 if (is_cd)
327 *(pbuffer++) = *lc_data;
328 else
329 g_string_append_c (shell_string, *lc_data);
331 } /* for */
332 return g_string_free (shell_string, FALSE);
335 /* --------------------------------------------------------------------------------------------- */
337 static void
338 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line,
339 vfs_path_t * temp_file_name_vpath)
341 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
342 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
344 mcview_altered_hex_mode = 0;
345 mcview_altered_nroff_flag = 0;
346 if (def_hex_mode != mcview_default_hex_mode)
347 changed_hex_mode = 1;
348 if (def_nroff_flag != mcview_default_nroff_flag)
349 changed_nroff_flag = 1;
351 /* If we've written whitespace only, then just load filename
352 * into view
354 if (written_nonspace)
356 mcview_viewer (cmd, filename_vpath, start_line);
357 mc_unlink (temp_file_name_vpath);
359 else
360 mcview_viewer (NULL, filename_vpath, start_line);
362 if (changed_hex_mode && !mcview_altered_hex_mode)
363 mcview_default_hex_mode = def_hex_mode;
364 if (changed_nroff_flag && !mcview_altered_nroff_flag)
365 mcview_default_nroff_flag = def_nroff_flag;
367 dialog_switch_process_pending ();
370 /* --------------------------------------------------------------------------------------------- */
372 static void
373 exec_extension_cd (void)
375 char *q;
376 vfs_path_t *p_vpath;
378 *pbuffer = '\0';
379 pbuffer = buffer;
380 /* while (*p == ' ' && *p == '\t')
381 * p++;
383 /* Search last non-space character. Start search at the end in order
384 not to short filenames containing spaces. */
385 q = pbuffer + strlen (pbuffer) - 1;
386 while (q >= pbuffer && (*q == ' ' || *q == '\t'))
387 q--;
388 q[1] = 0;
390 p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
391 do_cd (p_vpath, cd_parse_command);
392 vfs_path_free (p_vpath);
396 /* --------------------------------------------------------------------------------------------- */
398 static void
399 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line)
401 char *shell_string, *export_variables;
402 vfs_path_t *temp_file_name_vpath = NULL;
403 int cmd_file_fd;
404 FILE *cmd_file;
405 char *cmd = NULL;
407 g_return_if_fail (lc_data != NULL);
409 pbuffer = NULL;
410 localmtime = 0;
411 quote_func = name_quote;
412 run_view = FALSE;
413 is_cd = FALSE;
414 written_nonspace = FALSE;
416 /* Avoid making a local copy if we are doing a cd */
417 do_local_copy = !vfs_file_is_local (filename_vpath);
419 shell_string = exec_make_shell_string (lc_data, filename_vpath);
421 if (shell_string == NULL)
422 goto ret;
424 if (is_cd)
426 exec_extension_cd ();
427 g_free (shell_string);
428 goto ret;
432 * All commands should be run in /bin/sh regardless of user shell.
433 * To do that, create temporary shell script and run it.
434 * Sometimes it's not needed (e.g. for %cd and %view commands),
435 * but it's easier to create it anyway.
437 cmd_file_fd = mc_mkstemps (&temp_file_name_vpath, "mcext", SCRIPT_SUFFIX);
439 if (cmd_file_fd == -1)
441 message (D_ERROR, MSG_ERROR,
442 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
443 goto ret;
446 cmd_file = fdopen (cmd_file_fd, "w");
447 fputs ("#! /bin/sh\n\n", cmd_file);
449 export_variables = exec_get_export_variables (filename_vpath);
450 if (export_variables != NULL)
452 fprintf (cmd_file, "%s\n", export_variables);
453 g_free (export_variables);
456 fputs (shell_string, cmd_file);
457 g_free (shell_string);
460 * Make the script remove itself when it finishes.
461 * Don't do it for the viewer - it may need to rerun the script,
462 * so we clean up after calling view().
464 if (!run_view)
466 char *file_name;
468 file_name = vfs_path_to_str (temp_file_name_vpath);
469 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
470 g_free (file_name);
473 fclose (cmd_file);
475 if ((run_view && !written_nonspace) || is_cd)
477 mc_unlink (temp_file_name_vpath);
478 vfs_path_free (temp_file_name_vpath);
479 temp_file_name_vpath = NULL;
481 else
483 char *file_name;
485 file_name = vfs_path_to_str (temp_file_name_vpath);
486 /* Set executable flag on the command file ... */
487 mc_chmod (temp_file_name_vpath, S_IRWXU);
488 /* ... but don't rely on it - run /bin/sh explicitly */
489 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
490 g_free (file_name);
493 if (run_view)
494 exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath);
495 else
497 shell_execute (cmd, EXECUTE_INTERNAL);
498 if (mc_global.tty.console_flag != '\0')
500 handle_console (CONSOLE_SAVE);
501 if (output_lines && mc_global.keybar_visible)
502 show_console_contents (output_start_y,
503 LINES - mc_global.keybar_visible -
504 output_lines - 1, LINES - mc_global.keybar_visible - 1);
508 g_free (cmd);
510 exec_cleanup_file_name (filename_vpath, TRUE);
511 ret:
512 vfs_path_free (temp_file_name_vpath);
515 /* --------------------------------------------------------------------------------------------- */
517 * Run cmd_file with args, put result into buf.
518 * If error, put '\0' into buf[0]
519 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
521 * NOTES: buf is null-terminated string.
524 static int
525 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
527 gboolean read_bytes = FALSE;
528 char *command;
529 FILE *f;
531 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
532 f = popen (command, "r");
533 g_free (command);
535 if (f != NULL)
537 #ifdef __QNXNTO__
538 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
540 (void) pclose (f);
541 return -1;
543 #endif
544 read_bytes = (fgets (buf, buflen, f) != NULL);
545 if (!read_bytes)
546 buf[0] = '\0'; /* Paranoid termination */
547 pclose (f);
549 else
551 buf[0] = '\0'; /* Paranoid termination */
552 return -1;
555 buf[buflen - 1] = '\0';
557 return read_bytes ? 1 : 0;
560 /* --------------------------------------------------------------------------------------------- */
562 * Run the "file" command on the local file.
563 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
566 static int
567 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
569 char *tmp;
570 int ret;
572 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
573 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
574 g_free (tmp);
576 return ret;
579 /* --------------------------------------------------------------------------------------------- */
581 * Run the "enca" command on the local file.
582 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
585 #ifdef HAVE_CHARSET
586 static int
587 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
589 char *tmp, *lang, *args;
590 int ret;
592 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
593 lang = name_quote (autodetect_codeset, 0);
594 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
596 ret = get_popen_information ("enca", args, buf, buflen);
598 g_free (args);
599 g_free (lang);
600 g_free (tmp);
602 return ret;
604 #endif /* HAVE_CHARSET */
606 /* --------------------------------------------------------------------------------------------- */
608 * Invoke the "file" command on the file and match its output against PTR.
609 * have_type is a flag that is set if we already have tried to determine
610 * the type of that file.
611 * Return 1 for match, 0 for no match, -1 errors.
614 static gboolean
615 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type,
616 gboolean case_insense, GError ** error)
618 gboolean found = FALSE;
620 /* Following variables are valid if *have_type is 1 */
621 static char content_string[2048];
622 #ifdef HAVE_CHARSET
623 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
624 #endif
625 static size_t content_shift = 0;
626 static int got_data = 0;
628 if (!use_file_to_check_type)
629 return FALSE;
631 if (*have_type == 0)
633 vfs_path_t *localfile_vpath;
634 const char *realname; /* name used with "file" */
636 #ifdef HAVE_CHARSET
637 int got_encoding_data;
638 #endif /* HAVE_CHARSET */
640 /* Don't repeate even unsuccessful checks */
641 *have_type = 1;
643 localfile_vpath = mc_getlocalcopy (filename_vpath);
644 if (localfile_vpath == NULL)
646 char *filename;
648 filename = vfs_path_to_str (filename_vpath);
649 g_propagate_error (error,
650 g_error_new (MC_ERROR, -1,
651 _("Cannot fetch a local copy of %s"), filename));
652 g_free (filename);
653 return FALSE;
656 realname = vfs_path_get_last_path_str (localfile_vpath);
658 #ifdef HAVE_CHARSET
659 got_encoding_data = is_autodetect_codeset_enabled
660 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
662 if (got_encoding_data > 0)
664 char *pp;
665 int cp_id;
667 pp = strchr (encoding_id, '\n');
668 if (pp != NULL)
669 *pp = '\0';
671 cp_id = get_codepage_index (encoding_id);
672 if (cp_id == -1)
673 cp_id = default_source_codepage;
675 do_set_codepage (cp_id);
677 #endif /* HAVE_CHARSET */
679 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
681 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
683 if (got_data > 0)
685 char *pp;
686 size_t real_len;
688 pp = strchr (content_string, '\n');
689 if (pp != NULL)
690 *pp = '\0';
692 real_len = strlen (realname);
694 if (strncmp (content_string, realname, real_len) == 0)
696 /* Skip "realname: " */
697 content_shift = real_len;
698 if (content_string[content_shift] == ':')
700 /* Solaris' file prints tab(s) after ':' */
701 for (content_shift++;
702 content_string[content_shift] == ' '
703 || content_string[content_shift] == '\t'; content_shift++)
708 else
710 /* No data */
711 content_string[0] = '\0';
713 vfs_path_free (localfile_vpath);
716 if (got_data == -1)
718 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed")));
719 return FALSE;
722 if (content_string[0] != '\0')
724 mc_search_t *search;
726 search = mc_search_new (ptr, -1);
727 if (search != NULL)
729 search->search_type = MC_SEARCH_T_REGEX;
730 search->is_case_sensitive = !case_insense;
731 found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
732 mc_search_free (search);
734 else
736 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error")));
740 return found;
743 /* --------------------------------------------------------------------------------------------- */
744 /*** public functions ****************************************************************************/
745 /* --------------------------------------------------------------------------------------------- */
747 void
748 flush_extension_file (void)
750 g_free (data);
751 data = NULL;
754 /* --------------------------------------------------------------------------------------------- */
756 * The second argument is action, i.e. Open, View or Edit
758 * This function returns:
760 * -1 for a failure or user interrupt
761 * 0 if no command was run
762 * 1 if some command was run
764 * If action == "View" then a parameter is checked in the form of "View:%d",
765 * if the value for %d exists, then the viewer is started up at that line number.
769 regex_command (const vfs_path_t * filename_vpath, const char *action)
771 char *filename, *p, *q, *r, c;
772 size_t file_len;
773 gboolean found = FALSE;
774 gboolean error_flag = FALSE;
775 int ret = 0;
776 struct stat mystat;
777 int view_at_line_number;
778 char *include_target;
779 int include_target_len;
780 int have_type = 0; /* Flag used by regex_check_type() */
782 if (filename_vpath == NULL)
783 return 0;
785 /* Check for the special View:%d parameter */
786 if (strncmp (action, "View:", 5) == 0)
788 view_at_line_number = atoi (action + 5);
789 action = "View";
791 else
793 view_at_line_number = 0;
796 if (data == NULL)
798 char *extension_file;
799 gboolean mc_user_ext = TRUE;
800 gboolean home_error = FALSE;
802 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
803 if (!exist_file (extension_file))
805 g_free (extension_file);
806 check_stock_mc_ext:
807 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
808 if (!exist_file (extension_file))
810 g_free (extension_file);
811 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
813 mc_user_ext = FALSE;
816 g_file_get_contents (extension_file, &data, NULL, NULL);
817 g_free (extension_file);
818 if (data == NULL)
819 return 0;
821 if (strstr (data, "default/") == NULL)
823 if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
824 strstr (data, "type/") == NULL)
826 g_free (data);
827 data = NULL;
829 if (!mc_user_ext)
831 char *title;
833 title = g_strdup_printf (_(" %s%s file error"),
834 mc_global.sysconfig_dir, MC_LIB_EXT);
835 message (D_ERROR, title, _("The format of the %smc.ext "
836 "file has changed with version 3.0. It seems that "
837 "the installation failed. Please fetch a fresh "
838 "copy from the Midnight Commander package."),
839 mc_global.sysconfig_dir);
840 g_free (title);
841 return 0;
844 home_error = TRUE;
845 goto check_stock_mc_ext;
849 if (home_error)
851 char *filebind_filename;
852 char *title;
854 filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
855 title = g_strdup_printf (_("%s file error"), filebind_filename);
856 message (D_ERROR, title,
857 _("The format of the %s file has "
858 "changed with version 3.0. You may either want to copy "
859 "it from %smc.ext or use that file as an example of how to write it."),
860 filebind_filename, mc_global.sysconfig_dir);
861 g_free (filebind_filename);
862 g_free (title);
866 mc_stat (filename_vpath, &mystat);
868 include_target = NULL;
869 include_target_len = 0;
870 filename = vfs_path_to_str (filename_vpath);
871 file_len = vfs_path_len (filename_vpath);
873 for (p = data; *p != '\0'; p++)
875 for (q = p; *q == ' ' || *q == '\t'; q++)
877 if (*q == '\n' || *q == '\0')
878 p = q; /* empty line */
879 if (*p == '#') /* comment */
880 while (*p != '\0' && *p != '\n')
881 p++;
882 if (*p == '\n')
883 continue;
884 if (*p == '\0')
885 break;
886 if (p == q)
887 { /* i.e. starts in the first column, should be
888 * keyword/descNL
890 gboolean case_insense;
892 found = FALSE;
893 q = strchr (p, '\n');
894 if (q == NULL)
895 q = strchr (p, '\0');
896 c = *q;
897 *q = '\0';
898 if (include_target)
900 if ((strncmp (p, "include/", 8) == 0)
901 && (strncmp (p + 8, include_target, include_target_len) == 0))
902 found = TRUE;
904 else if (strncmp (p, "regex/", 6) == 0)
906 mc_search_t *search;
908 p += 6;
909 case_insense = (strncmp (p, "i/", 2) == 0);
910 if (case_insense)
911 p += 2;
913 search = mc_search_new (p, -1);
914 if (search != NULL)
916 search->search_type = MC_SEARCH_T_REGEX;
917 search->is_case_sensitive = !case_insense;
918 found = mc_search_run (search, filename, 0, file_len, NULL);
919 mc_search_free (search);
922 else if (strncmp (p, "directory/", 10) == 0)
924 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
925 found = TRUE;
927 else if (strncmp (p, "shell/", 6) == 0)
929 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
931 p += 6;
932 case_insense = (strncmp (p, "i/", 2) == 0);
933 if (case_insense)
935 p += 2;
936 cmp_func = strncasecmp;
939 if (*p == '.' && file_len >= (size_t) (q - p))
941 if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
942 found = TRUE;
944 else
946 if ((size_t) (q - p) == file_len && cmp_func (p, filename, q - p) == 0)
947 found = TRUE;
950 else if (strncmp (p, "type/", 5) == 0)
952 GError *error = NULL;
954 p += 5;
956 case_insense = (strncmp (p, "i/", 2) == 0);
957 if (case_insense)
958 p += 2;
960 found = regex_check_type (filename_vpath, p, &have_type, case_insense, &error);
961 if (error != NULL)
963 g_error_free (error);
964 error_flag = TRUE; /* leave it if file cannot be opened */
967 else if (strncmp (p, "default/", 8) == 0)
968 found = TRUE;
970 *q = c;
971 p = q;
972 if (*p == '\0')
973 break;
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)
983 r = strchr (p, '=');
984 if (r != NULL)
986 c = *r;
987 *r = '\0';
988 if (strcmp (p, "Include") == 0)
990 char *t;
992 include_target = p + 8;
993 t = strchr (include_target, '\n');
994 if (t != NULL)
995 *t = '\0';
996 include_target_len = strlen (include_target);
997 if (t != NULL)
998 *t = '\n';
1000 *r = c;
1001 p = q;
1002 found = FALSE;
1004 if (*p == '\0')
1005 break;
1006 continue;
1009 if (strcmp (action, p) != 0)
1010 *r = c;
1011 else
1013 *r = c;
1015 for (p = r + 1; *p == ' ' || *p == '\t'; p++)
1018 /* Empty commands just stop searching
1019 * through, they don't do anything
1021 if (p < q)
1023 exec_extension (filename_vpath, r + 1, view_at_line_number);
1024 ret = 1;
1026 break;
1030 p = q;
1031 if (*p == '\0')
1032 break;
1035 g_free (filename);
1036 if (error_flag)
1037 ret = -1;
1038 return ret;
1041 /* --------------------------------------------------------------------------------------------- */