Implement case insensitive for type keyword.
[midnight-commander.git] / src / filemanager / ext.c
blob1a52031308ebc257ea71bb56e4cec334a61cd79e
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"
56 #include "src/main.h" /* do_cd */
58 #include "src/consaver/cons.saver.h"
59 #include "src/viewer/mcviewer.h"
61 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h" /* do_set_codepage */
63 #endif
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 /* --------------------------------------------------------------------------------------------- */
146 static char *
147 exec_get_export_variables (const vfs_path_t * filename_vpath)
149 char *text;
150 GString *export_vars_string;
151 size_t i;
153 /* *INDENT-OFF* */
154 struct
156 const char symbol;
157 const char *name;
158 } export_variables[] = {
159 {'p', "MC_EXT_BASENAME"},
160 {'d', "MC_EXT_CURRENTDIR"},
161 {'s', "MC_EXT_SELECTED"},
162 {'t', "MC_EXT_ONLYTAGGED"},
163 {'\0', NULL}
165 /* *INDENT-ON* */
167 text = exec_get_file_name (filename_vpath);
168 if (text == NULL)
169 return NULL;
171 export_vars_string = g_string_new ("MC_EXT_FILENAME=");
172 g_string_append_printf (export_vars_string, "%s\nexport MC_EXT_FILENAME\n", text);
173 g_free (text);
175 for (i = 0; export_variables[i].name != NULL; i++)
177 text = expand_format (NULL, export_variables[i].symbol, TRUE);
178 if (text != NULL)
180 g_string_append_printf (export_vars_string,
181 "%s=%s\nexport %s\n", export_variables[i].name, text,
182 export_variables[i].name);
183 g_free (text);
186 return g_string_free (export_vars_string, FALSE);
189 /* --------------------------------------------------------------------------------------------- */
191 static char *
192 exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
194 GString *shell_string;
195 char lc_prompt[80] = "\0";
196 gboolean parameter_found = FALSE;
197 gboolean expand_prefix_found = FALSE;
199 shell_string = g_string_new ("");
201 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
203 if (parameter_found)
205 if (*lc_data == '}')
207 char *parameter;
209 parameter_found = FALSE;
210 parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
211 if (parameter == NULL)
213 /* User canceled */
214 g_string_free (shell_string, TRUE);
215 exec_cleanup_file_name (filename_vpath, FALSE);
216 return NULL;
218 g_string_append (shell_string, parameter);
219 written_nonspace = TRUE;
220 g_free (parameter);
222 else
224 size_t len = strlen (lc_prompt);
226 if (len < sizeof (lc_prompt) - 1)
228 lc_prompt[len] = *lc_data;
229 lc_prompt[len + 1] = '\0';
233 else if (expand_prefix_found)
235 expand_prefix_found = FALSE;
236 if (*lc_data == '{')
237 parameter_found = TRUE;
238 else
240 int i;
241 char *v;
243 i = check_format_view (lc_data);
244 if (i != 0)
246 lc_data += i - 1;
247 run_view = TRUE;
249 else
251 i = check_format_cd (lc_data);
252 if (i > 0)
254 is_cd = TRUE;
255 quote_func = fake_name_quote;
256 do_local_copy = FALSE;
257 pbuffer = buffer;
258 lc_data += i - 1;
260 else
262 i = check_format_var (lc_data, &v);
263 if (i > 0 && v != NULL)
265 g_string_append (shell_string, v);
266 g_free (v);
267 lc_data += i;
269 else
271 char *text;
273 if (*lc_data != 'f')
274 text = expand_format (NULL, *lc_data, !is_cd);
275 else
277 text = exec_get_file_name (filename_vpath);
278 if (text == NULL)
280 g_string_free (shell_string, TRUE);
281 return NULL;
285 if (!is_cd)
286 g_string_append (shell_string, text);
287 else
289 strcpy (pbuffer, text);
290 pbuffer = strchr (pbuffer, 0);
293 g_free (text);
294 written_nonspace = TRUE;
300 else if (*lc_data == '%')
301 expand_prefix_found = TRUE;
302 else
304 if (*lc_data != ' ' && *lc_data != '\t')
305 written_nonspace = TRUE;
306 if (is_cd)
307 *(pbuffer++) = *lc_data;
308 else
309 g_string_append_c (shell_string, *lc_data);
311 } /* for */
312 return g_string_free (shell_string, FALSE);
315 /* --------------------------------------------------------------------------------------------- */
317 static void
318 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line,
319 vfs_path_t * temp_file_name_vpath)
321 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
322 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
324 mcview_altered_hex_mode = 0;
325 mcview_altered_nroff_flag = 0;
326 if (def_hex_mode != mcview_default_hex_mode)
327 changed_hex_mode = 1;
328 if (def_nroff_flag != mcview_default_nroff_flag)
329 changed_nroff_flag = 1;
331 /* If we've written whitespace only, then just load filename
332 * into view
334 if (written_nonspace)
336 mcview_viewer (cmd, filename_vpath, start_line);
337 mc_unlink (temp_file_name_vpath);
339 else
340 mcview_viewer (NULL, filename_vpath, start_line);
342 if (changed_hex_mode && !mcview_altered_hex_mode)
343 mcview_default_hex_mode = def_hex_mode;
344 if (changed_nroff_flag && !mcview_altered_nroff_flag)
345 mcview_default_nroff_flag = def_nroff_flag;
347 dialog_switch_process_pending ();
350 /* --------------------------------------------------------------------------------------------- */
352 static void
353 exec_extension_cd (void)
355 char *q;
356 vfs_path_t *p_vpath;
358 *pbuffer = '\0';
359 pbuffer = buffer;
360 /* while (*p == ' ' && *p == '\t')
361 * p++;
363 /* Search last non-space character. Start search at the end in order
364 not to short filenames containing spaces. */
365 q = pbuffer + strlen (pbuffer) - 1;
366 while (q >= pbuffer && (*q == ' ' || *q == '\t'))
367 q--;
368 q[1] = 0;
370 p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
371 do_cd (p_vpath, cd_parse_command);
372 vfs_path_free (p_vpath);
376 /* --------------------------------------------------------------------------------------------- */
378 static void
379 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line)
381 char *shell_string, *export_variables;
382 vfs_path_t *temp_file_name_vpath = NULL;
383 int cmd_file_fd;
384 FILE *cmd_file;
385 char *cmd = NULL;
387 g_return_if_fail (lc_data != NULL);
389 pbuffer = NULL;
390 localmtime = 0;
391 quote_func = name_quote;
392 run_view = FALSE;
393 is_cd = FALSE;
394 written_nonspace = FALSE;
396 /* Avoid making a local copy if we are doing a cd */
397 do_local_copy = !vfs_file_is_local (filename_vpath);
399 shell_string = exec_make_shell_string (lc_data, filename_vpath);
401 if (shell_string == NULL)
402 goto ret;
404 if (is_cd)
406 exec_extension_cd ();
407 g_free (shell_string);
408 goto ret;
412 * All commands should be run in /bin/sh regardless of user shell.
413 * To do that, create temporary shell script and run it.
414 * Sometimes it's not needed (e.g. for %cd and %view commands),
415 * but it's easier to create it anyway.
417 cmd_file_fd = mc_mkstemps (&temp_file_name_vpath, "mcext", SCRIPT_SUFFIX);
419 if (cmd_file_fd == -1)
421 message (D_ERROR, MSG_ERROR,
422 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
423 goto ret;
426 cmd_file = fdopen (cmd_file_fd, "w");
427 fputs ("#! /bin/sh\n\n", cmd_file);
429 export_variables = exec_get_export_variables (filename_vpath);
430 if (export_variables != NULL)
432 fprintf (cmd_file, "%s\n", export_variables);
433 g_free (export_variables);
436 fputs (shell_string, cmd_file);
437 g_free (shell_string);
440 * Make the script remove itself when it finishes.
441 * Don't do it for the viewer - it may need to rerun the script,
442 * so we clean up after calling view().
444 if (!run_view)
446 char *file_name;
448 file_name = vfs_path_to_str (temp_file_name_vpath);
449 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
450 g_free (file_name);
453 fclose (cmd_file);
455 if ((run_view && !written_nonspace) || is_cd)
457 mc_unlink (temp_file_name_vpath);
458 vfs_path_free (temp_file_name_vpath);
459 temp_file_name_vpath = NULL;
461 else
463 char *file_name;
465 file_name = vfs_path_to_str (temp_file_name_vpath);
466 /* Set executable flag on the command file ... */
467 mc_chmod (temp_file_name_vpath, S_IRWXU);
468 /* ... but don't rely on it - run /bin/sh explicitly */
469 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
470 g_free (file_name);
473 if (run_view)
474 exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath);
475 else
477 shell_execute (cmd, EXECUTE_INTERNAL);
478 if (mc_global.tty.console_flag != '\0')
480 handle_console (CONSOLE_SAVE);
481 if (output_lines && mc_global.keybar_visible)
482 show_console_contents (output_start_y,
483 LINES - mc_global.keybar_visible -
484 output_lines - 1, LINES - mc_global.keybar_visible - 1);
488 g_free (cmd);
490 exec_cleanup_file_name (filename_vpath, TRUE);
491 ret:
492 vfs_path_free (temp_file_name_vpath);
495 /* --------------------------------------------------------------------------------------------- */
497 * Run cmd_file with args, put result into buf.
498 * If error, put '\0' into buf[0]
499 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
501 * NOTES: buf is null-terminated string.
504 static int
505 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
507 gboolean read_bytes = FALSE;
508 char *command;
509 FILE *f;
511 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
512 f = popen (command, "r");
513 g_free (command);
515 if (f != NULL)
517 #ifdef __QNXNTO__
518 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
520 (void) pclose (f);
521 return -1;
523 #endif
524 read_bytes = (fgets (buf, buflen, f) != NULL);
525 if (!read_bytes)
526 buf[0] = '\0'; /* Paranoid termination */
527 pclose (f);
529 else
531 buf[0] = '\0'; /* Paranoid termination */
532 return -1;
535 buf[buflen - 1] = '\0';
537 return read_bytes ? 1 : 0;
540 /* --------------------------------------------------------------------------------------------- */
542 * Run the "file" command on the local file.
543 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
546 static int
547 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
549 char *tmp;
550 int ret;
552 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
553 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
554 g_free (tmp);
556 return ret;
559 /* --------------------------------------------------------------------------------------------- */
561 * Run the "enca" command on the local file.
562 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
565 #ifdef HAVE_CHARSET
566 static int
567 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
569 char *tmp, *lang, *args;
570 int ret;
572 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
573 lang = name_quote (autodetect_codeset, 0);
574 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
576 ret = get_popen_information ("enca", args, buf, buflen);
578 g_free (args);
579 g_free (lang);
580 g_free (tmp);
582 return ret;
584 #endif /* HAVE_CHARSET */
586 /* --------------------------------------------------------------------------------------------- */
588 * Invoke the "file" command on the file and match its output against PTR.
589 * have_type is a flag that is set if we already have tried to determine
590 * the type of that file.
591 * Return 1 for match, 0 for no match, -1 errors.
594 static gboolean
595 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type,
596 gboolean case_insense, GError ** error)
598 gboolean found = FALSE;
600 /* Following variables are valid if *have_type is 1 */
601 static char content_string[2048];
602 #ifdef HAVE_CHARSET
603 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
604 #endif
605 static size_t content_shift = 0;
606 static int got_data = 0;
608 if (!use_file_to_check_type)
609 return FALSE;
611 if (*have_type == 0)
613 vfs_path_t *localfile_vpath;
614 const char *realname; /* name used with "file" */
616 #ifdef HAVE_CHARSET
617 int got_encoding_data;
618 #endif /* HAVE_CHARSET */
620 /* Don't repeate even unsuccessful checks */
621 *have_type = 1;
623 localfile_vpath = mc_getlocalcopy (filename_vpath);
624 if (localfile_vpath == NULL)
626 char *filename;
628 filename = vfs_path_to_str (filename_vpath);
629 g_propagate_error (error,
630 g_error_new (MC_ERROR, -1,
631 _("Cannot fetch a local copy of %s"), filename));
632 g_free (filename);
633 return FALSE;
636 realname = vfs_path_get_last_path_str (localfile_vpath);
638 #ifdef HAVE_CHARSET
639 got_encoding_data = is_autodetect_codeset_enabled
640 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
642 if (got_encoding_data > 0)
644 char *pp;
645 int cp_id;
647 pp = strchr (encoding_id, '\n');
648 if (pp != NULL)
649 *pp = '\0';
651 cp_id = get_codepage_index (encoding_id);
652 if (cp_id == -1)
653 cp_id = default_source_codepage;
655 do_set_codepage (cp_id);
657 #endif /* HAVE_CHARSET */
659 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
661 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
663 if (got_data > 0)
665 char *pp;
666 size_t real_len;
668 pp = strchr (content_string, '\n');
669 if (pp != NULL)
670 *pp = '\0';
672 real_len = strlen (realname);
674 if (strncmp (content_string, realname, real_len) == 0)
676 /* Skip "realname: " */
677 content_shift = real_len;
678 if (content_string[content_shift] == ':')
680 /* Solaris' file prints tab(s) after ':' */
681 for (content_shift++;
682 content_string[content_shift] == ' '
683 || content_string[content_shift] == '\t'; content_shift++)
688 else
690 /* No data */
691 content_string[0] = '\0';
693 vfs_path_free (localfile_vpath);
696 if (got_data == -1)
698 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed")));
699 return FALSE;
702 if (content_string[0] != '\0')
704 mc_search_t *search;
706 search = mc_search_new (ptr, -1);
707 if (search != NULL)
709 search->search_type = MC_SEARCH_T_REGEX;
710 search->is_case_sensitive = !case_insense;
711 found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
712 mc_search_free (search);
714 else
716 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error")));
720 return found;
723 /* --------------------------------------------------------------------------------------------- */
724 /*** public functions ****************************************************************************/
725 /* --------------------------------------------------------------------------------------------- */
727 void
728 flush_extension_file (void)
730 g_free (data);
731 data = NULL;
734 /* --------------------------------------------------------------------------------------------- */
736 * The second argument is action, i.e. Open, View or Edit
738 * This function returns:
740 * -1 for a failure or user interrupt
741 * 0 if no command was run
742 * 1 if some command was run
744 * If action == "View" then a parameter is checked in the form of "View:%d",
745 * if the value for %d exists, then the viewer is started up at that line number.
749 regex_command (const vfs_path_t * filename_vpath, const char *action)
751 char *filename, *p, *q, *r, c;
752 size_t file_len;
753 gboolean found = FALSE;
754 gboolean error_flag = FALSE;
755 int ret = 0;
756 struct stat mystat;
757 int view_at_line_number;
758 char *include_target;
759 int include_target_len;
760 int have_type = 0; /* Flag used by regex_check_type() */
762 if (filename_vpath == NULL)
763 return 0;
765 /* Check for the special View:%d parameter */
766 if (strncmp (action, "View:", 5) == 0)
768 view_at_line_number = atoi (action + 5);
769 action = "View";
771 else
773 view_at_line_number = 0;
776 if (data == NULL)
778 char *extension_file;
779 gboolean mc_user_ext = TRUE;
780 gboolean home_error = FALSE;
782 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
783 if (!exist_file (extension_file))
785 g_free (extension_file);
786 check_stock_mc_ext:
787 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
788 if (!exist_file (extension_file))
790 g_free (extension_file);
791 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
793 mc_user_ext = FALSE;
796 g_file_get_contents (extension_file, &data, NULL, NULL);
797 g_free (extension_file);
798 if (data == NULL)
799 return 0;
801 if (strstr (data, "default/") == NULL)
803 if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
804 strstr (data, "type/") == NULL)
806 g_free (data);
807 data = NULL;
809 if (!mc_user_ext)
811 char *title;
813 title = g_strdup_printf (_(" %s%s file error"),
814 mc_global.sysconfig_dir, MC_LIB_EXT);
815 message (D_ERROR, title, _("The format of the %smc.ext "
816 "file has changed with version 3.0. It seems that "
817 "the installation failed. Please fetch a fresh "
818 "copy from the Midnight Commander package."),
819 mc_global.sysconfig_dir);
820 g_free (title);
821 return 0;
824 home_error = TRUE;
825 goto check_stock_mc_ext;
829 if (home_error)
831 char *filebind_filename;
832 char *title;
834 filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
835 title = g_strdup_printf (_("%s file error"), filebind_filename);
836 message (D_ERROR, title,
837 _("The format of the %s file has "
838 "changed with version 3.0. You may either want to copy "
839 "it from %smc.ext or use that file as an example of how to write it."),
840 filebind_filename, mc_global.sysconfig_dir);
841 g_free (filebind_filename);
842 g_free (title);
846 mc_stat (filename_vpath, &mystat);
848 include_target = NULL;
849 include_target_len = 0;
850 filename = vfs_path_to_str (filename_vpath);
851 file_len = vfs_path_len (filename_vpath);
853 for (p = data; *p != '\0'; p++)
855 for (q = p; *q == ' ' || *q == '\t'; q++)
857 if (*q == '\n' || *q == '\0')
858 p = q; /* empty line */
859 if (*p == '#') /* comment */
860 while (*p != '\0' && *p != '\n')
861 p++;
862 if (*p == '\n')
863 continue;
864 if (*p == '\0')
865 break;
866 if (p == q)
867 { /* i.e. starts in the first column, should be
868 * keyword/descNL
870 gboolean case_insense;
872 found = FALSE;
873 q = strchr (p, '\n');
874 if (q == NULL)
875 q = strchr (p, '\0');
876 c = *q;
877 *q = '\0';
878 if (include_target)
880 if ((strncmp (p, "include/", 8) == 0)
881 && (strncmp (p + 8, include_target, include_target_len) == 0))
882 found = TRUE;
884 else if (strncmp (p, "regex/", 6) == 0)
886 mc_search_t *search;
888 p += 6;
889 case_insense = (strncmp (p, "i/", 2) == 0);
890 if (case_insense)
891 p += 2;
893 search = mc_search_new (p, -1);
894 if (search != NULL)
896 search->search_type = MC_SEARCH_T_REGEX;
897 search->is_case_sensitive = !case_insense;
898 found = mc_search_run (search, filename, 0, file_len, NULL);
899 mc_search_free (search);
902 else if (strncmp (p, "directory/", 10) == 0)
904 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
905 found = TRUE;
907 else if (strncmp (p, "shell/", 6) == 0)
909 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
911 p += 6;
912 case_insense = (strncmp (p, "i/", 2) == 0);
913 if (case_insense)
915 p += 2;
916 cmp_func = strncasecmp;
919 if (*p == '.' && file_len >= (size_t) (q - p))
921 if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
922 found = TRUE;
924 else
926 if ((size_t) (q - p) == file_len && cmp_func (p, filename, q - p) == 0)
927 found = TRUE;
930 else if (strncmp (p, "type/", 5) == 0)
932 GError *error = NULL;
934 p += 5;
936 case_insense = (strncmp (p, "i/", 2) == 0);
937 if (case_insense)
938 p += 2;
940 found = regex_check_type (filename_vpath, p, &have_type, case_insense, &error);
941 if (error != NULL)
943 g_error_free (error);
944 error_flag = TRUE; /* leave it if file cannot be opened */
947 else if (strncmp (p, "default/", 8) == 0)
948 found = TRUE;
950 *q = c;
951 p = q;
952 if (*p == '\0')
953 break;
955 else
956 { /* List of actions */
957 p = q;
958 q = strchr (p, '\n');
959 if (q == NULL)
960 q = strchr (p, '\0');
961 if (found && !error_flag)
963 r = strchr (p, '=');
964 if (r != NULL)
966 c = *r;
967 *r = '\0';
968 if (strcmp (p, "Include") == 0)
970 char *t;
972 include_target = p + 8;
973 t = strchr (include_target, '\n');
974 if (t != NULL)
975 *t = '\0';
976 include_target_len = strlen (include_target);
977 if (t != NULL)
978 *t = '\n';
980 *r = c;
981 p = q;
982 found = FALSE;
984 if (*p == '\0')
985 break;
986 continue;
989 if (strcmp (action, p) != 0)
990 *r = c;
991 else
993 *r = c;
995 for (p = r + 1; *p == ' ' || *p == '\t'; p++)
998 /* Empty commands just stop searching
999 * through, they don't do anything
1001 if (p < q)
1003 exec_extension (filename_vpath, r + 1, view_at_line_number);
1004 ret = 1;
1006 break;
1010 p = q;
1011 if (*p == '\0')
1012 break;
1015 g_free (filename);
1016 if (error_flag)
1017 ret = -1;
1018 return ret;
1021 /* --------------------------------------------------------------------------------------------- */