Extend QUICK_INPUT and QUICK_LABELED_INPUT macros for getting completion flags via...
[midnight-commander.git] / src / filemanager / ext.c
blob375373ae2e4194a56f809223194e4ed1344274d4
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 =
231 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "",
232 INPUT_COMPLETE_DEFAULT);
233 if (parameter == NULL)
235 /* User canceled */
236 g_string_free (shell_string, TRUE);
237 exec_cleanup_file_name (filename_vpath, FALSE);
238 return NULL;
240 g_string_append (shell_string, parameter);
241 written_nonspace = TRUE;
242 g_free (parameter);
244 else
246 size_t len = strlen (lc_prompt);
248 if (len < sizeof (lc_prompt) - 1)
250 lc_prompt[len] = *lc_data;
251 lc_prompt[len + 1] = '\0';
255 else if (expand_prefix_found)
257 expand_prefix_found = FALSE;
258 if (*lc_data == '{')
259 parameter_found = TRUE;
260 else
262 int i;
263 char *v;
265 i = check_format_view (lc_data);
266 if (i != 0)
268 lc_data += i - 1;
269 run_view = TRUE;
271 else
273 i = check_format_cd (lc_data);
274 if (i > 0)
276 is_cd = TRUE;
277 quote_func = fake_name_quote;
278 do_local_copy = FALSE;
279 pbuffer = buffer;
280 lc_data += i - 1;
282 else
284 i = check_format_var (lc_data, &v);
285 if (i > 0 && v != NULL)
287 g_string_append (shell_string, v);
288 g_free (v);
289 lc_data += i;
291 else
293 char *text;
295 if (*lc_data != 'f')
296 text = expand_format (NULL, *lc_data, !is_cd);
297 else
299 text = exec_get_file_name (filename_vpath);
300 if (text == NULL)
302 g_string_free (shell_string, TRUE);
303 return NULL;
307 if (!is_cd)
308 g_string_append (shell_string, text);
309 else
311 strcpy (pbuffer, text);
312 pbuffer = strchr (pbuffer, 0);
315 g_free (text);
316 written_nonspace = TRUE;
322 else if (*lc_data == '%')
323 expand_prefix_found = TRUE;
324 else
326 if (*lc_data != ' ' && *lc_data != '\t')
327 written_nonspace = TRUE;
328 if (is_cd)
329 *(pbuffer++) = *lc_data;
330 else
331 g_string_append_c (shell_string, *lc_data);
333 } /* for */
334 return g_string_free (shell_string, FALSE);
337 /* --------------------------------------------------------------------------------------------- */
339 static void
340 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line,
341 vfs_path_t * temp_file_name_vpath)
343 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
344 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
346 mcview_altered_hex_mode = 0;
347 mcview_altered_nroff_flag = 0;
348 if (def_hex_mode != mcview_default_hex_mode)
349 changed_hex_mode = 1;
350 if (def_nroff_flag != mcview_default_nroff_flag)
351 changed_nroff_flag = 1;
353 /* If we've written whitespace only, then just load filename
354 * into view
356 if (written_nonspace)
358 mcview_viewer (cmd, filename_vpath, start_line);
359 mc_unlink (temp_file_name_vpath);
361 else
362 mcview_viewer (NULL, filename_vpath, start_line);
364 if (changed_hex_mode && !mcview_altered_hex_mode)
365 mcview_default_hex_mode = def_hex_mode;
366 if (changed_nroff_flag && !mcview_altered_nroff_flag)
367 mcview_default_nroff_flag = def_nroff_flag;
369 dialog_switch_process_pending ();
372 /* --------------------------------------------------------------------------------------------- */
374 static void
375 exec_extension_cd (void)
377 char *q;
378 vfs_path_t *p_vpath;
380 *pbuffer = '\0';
381 pbuffer = buffer;
382 /* while (*p == ' ' && *p == '\t')
383 * p++;
385 /* Search last non-space character. Start search at the end in order
386 not to short filenames containing spaces. */
387 q = pbuffer + strlen (pbuffer) - 1;
388 while (q >= pbuffer && (*q == ' ' || *q == '\t'))
389 q--;
390 q[1] = 0;
392 p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
393 do_cd (p_vpath, cd_parse_command);
394 vfs_path_free (p_vpath);
398 /* --------------------------------------------------------------------------------------------- */
400 static void
401 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line)
403 char *shell_string, *export_variables;
404 vfs_path_t *temp_file_name_vpath = NULL;
405 int cmd_file_fd;
406 FILE *cmd_file;
407 char *cmd = NULL;
409 g_return_if_fail (lc_data != NULL);
411 pbuffer = NULL;
412 localmtime = 0;
413 quote_func = name_quote;
414 run_view = FALSE;
415 is_cd = FALSE;
416 written_nonspace = FALSE;
418 /* Avoid making a local copy if we are doing a cd */
419 do_local_copy = !vfs_file_is_local (filename_vpath);
421 shell_string = exec_make_shell_string (lc_data, filename_vpath);
423 if (shell_string == NULL)
424 goto ret;
426 if (is_cd)
428 exec_extension_cd ();
429 g_free (shell_string);
430 goto ret;
434 * All commands should be run in /bin/sh regardless of user shell.
435 * To do that, create temporary shell script and run it.
436 * Sometimes it's not needed (e.g. for %cd and %view commands),
437 * but it's easier to create it anyway.
439 cmd_file_fd = mc_mkstemps (&temp_file_name_vpath, "mcext", SCRIPT_SUFFIX);
441 if (cmd_file_fd == -1)
443 message (D_ERROR, MSG_ERROR,
444 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
445 goto ret;
448 cmd_file = fdopen (cmd_file_fd, "w");
449 fputs ("#! /bin/sh\n\n", cmd_file);
451 export_variables = exec_get_export_variables (filename_vpath);
452 if (export_variables != NULL)
454 fprintf (cmd_file, "%s\n", export_variables);
455 g_free (export_variables);
458 fputs (shell_string, cmd_file);
459 g_free (shell_string);
462 * Make the script remove itself when it finishes.
463 * Don't do it for the viewer - it may need to rerun the script,
464 * so we clean up after calling view().
466 if (!run_view)
468 char *file_name;
470 file_name = vfs_path_to_str (temp_file_name_vpath);
471 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
472 g_free (file_name);
475 fclose (cmd_file);
477 if ((run_view && !written_nonspace) || is_cd)
479 mc_unlink (temp_file_name_vpath);
480 vfs_path_free (temp_file_name_vpath);
481 temp_file_name_vpath = NULL;
483 else
485 char *file_name;
487 file_name = vfs_path_to_str (temp_file_name_vpath);
488 /* Set executable flag on the command file ... */
489 mc_chmod (temp_file_name_vpath, S_IRWXU);
490 /* ... but don't rely on it - run /bin/sh explicitly */
491 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
492 g_free (file_name);
495 if (run_view)
496 exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath);
497 else
499 shell_execute (cmd, EXECUTE_INTERNAL);
500 if (mc_global.tty.console_flag != '\0')
502 handle_console (CONSOLE_SAVE);
503 if (output_lines && mc_global.keybar_visible)
504 show_console_contents (output_start_y,
505 LINES - mc_global.keybar_visible -
506 output_lines - 1, LINES - mc_global.keybar_visible - 1);
510 g_free (cmd);
512 exec_cleanup_file_name (filename_vpath, TRUE);
513 ret:
514 vfs_path_free (temp_file_name_vpath);
517 /* --------------------------------------------------------------------------------------------- */
519 * Run cmd_file with args, put result into buf.
520 * If error, put '\0' into buf[0]
521 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
523 * NOTES: buf is null-terminated string.
526 static int
527 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
529 gboolean read_bytes = FALSE;
530 char *command;
531 FILE *f;
533 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
534 f = popen (command, "r");
535 g_free (command);
537 if (f != NULL)
539 #ifdef __QNXNTO__
540 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
542 (void) pclose (f);
543 return -1;
545 #endif
546 read_bytes = (fgets (buf, buflen, f) != NULL);
547 if (!read_bytes)
548 buf[0] = '\0'; /* Paranoid termination */
549 pclose (f);
551 else
553 buf[0] = '\0'; /* Paranoid termination */
554 return -1;
557 buf[buflen - 1] = '\0';
559 return read_bytes ? 1 : 0;
562 /* --------------------------------------------------------------------------------------------- */
564 * Run the "file" command on the local file.
565 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
568 static int
569 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
571 char *tmp;
572 int ret;
574 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
575 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
576 g_free (tmp);
578 return ret;
581 /* --------------------------------------------------------------------------------------------- */
583 * Run the "enca" command on the local file.
584 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
587 #ifdef HAVE_CHARSET
588 static int
589 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
591 char *tmp, *lang, *args;
592 int ret;
594 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
595 lang = name_quote (autodetect_codeset, 0);
596 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
598 ret = get_popen_information ("enca", args, buf, buflen);
600 g_free (args);
601 g_free (lang);
602 g_free (tmp);
604 return ret;
606 #endif /* HAVE_CHARSET */
608 /* --------------------------------------------------------------------------------------------- */
610 * Invoke the "file" command on the file and match its output against PTR.
611 * have_type is a flag that is set if we already have tried to determine
612 * the type of that file.
613 * Return 1 for match, 0 for no match, -1 errors.
616 static gboolean
617 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type,
618 gboolean case_insense, GError ** error)
620 gboolean found = FALSE;
622 /* Following variables are valid if *have_type is 1 */
623 static char content_string[2048];
624 #ifdef HAVE_CHARSET
625 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
626 #endif
627 static size_t content_shift = 0;
628 static int got_data = 0;
630 if (!use_file_to_check_type)
631 return FALSE;
633 if (*have_type == 0)
635 vfs_path_t *localfile_vpath;
636 const char *realname; /* name used with "file" */
638 #ifdef HAVE_CHARSET
639 int got_encoding_data;
640 #endif /* HAVE_CHARSET */
642 /* Don't repeate even unsuccessful checks */
643 *have_type = 1;
645 localfile_vpath = mc_getlocalcopy (filename_vpath);
646 if (localfile_vpath == NULL)
648 char *filename;
650 filename = vfs_path_to_str (filename_vpath);
651 g_propagate_error (error,
652 g_error_new (MC_ERROR, -1,
653 _("Cannot fetch a local copy of %s"), filename));
654 g_free (filename);
655 return FALSE;
658 realname = vfs_path_get_last_path_str (localfile_vpath);
660 #ifdef HAVE_CHARSET
661 got_encoding_data = is_autodetect_codeset_enabled
662 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
664 if (got_encoding_data > 0)
666 char *pp;
667 int cp_id;
669 pp = strchr (encoding_id, '\n');
670 if (pp != NULL)
671 *pp = '\0';
673 cp_id = get_codepage_index (encoding_id);
674 if (cp_id == -1)
675 cp_id = default_source_codepage;
677 do_set_codepage (cp_id);
679 #endif /* HAVE_CHARSET */
681 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
683 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
685 if (got_data > 0)
687 char *pp;
688 size_t real_len;
690 pp = strchr (content_string, '\n');
691 if (pp != NULL)
692 *pp = '\0';
694 real_len = strlen (realname);
696 if (strncmp (content_string, realname, real_len) == 0)
698 /* Skip "realname: " */
699 content_shift = real_len;
700 if (content_string[content_shift] == ':')
702 /* Solaris' file prints tab(s) after ':' */
703 for (content_shift++;
704 content_string[content_shift] == ' '
705 || content_string[content_shift] == '\t'; content_shift++)
710 else
712 /* No data */
713 content_string[0] = '\0';
715 vfs_path_free (localfile_vpath);
718 if (got_data == -1)
720 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Pipe failed")));
721 return FALSE;
724 if (content_string[0] != '\0')
726 mc_search_t *search;
728 search = mc_search_new (ptr, -1);
729 if (search != NULL)
731 search->search_type = MC_SEARCH_T_REGEX;
732 search->is_case_sensitive = !case_insense;
733 found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
734 mc_search_free (search);
736 else
738 g_propagate_error (error, g_error_new (MC_ERROR, -1, _("Regular expression error")));
742 return found;
745 /* --------------------------------------------------------------------------------------------- */
746 /*** public functions ****************************************************************************/
747 /* --------------------------------------------------------------------------------------------- */
749 void
750 flush_extension_file (void)
752 g_free (data);
753 data = NULL;
756 /* --------------------------------------------------------------------------------------------- */
758 * The second argument is action, i.e. Open, View or Edit
760 * This function returns:
762 * -1 for a failure or user interrupt
763 * 0 if no command was run
764 * 1 if some command was run
766 * If action == "View" then a parameter is checked in the form of "View:%d",
767 * if the value for %d exists, then the viewer is started up at that line number.
771 regex_command (const vfs_path_t * filename_vpath, const char *action)
773 char *filename, *p, *q, *r, c;
774 size_t file_len;
775 gboolean found = FALSE;
776 gboolean error_flag = FALSE;
777 int ret = 0;
778 struct stat mystat;
779 int view_at_line_number;
780 char *include_target;
781 int include_target_len;
782 int have_type = 0; /* Flag used by regex_check_type() */
784 if (filename_vpath == NULL)
785 return 0;
787 /* Check for the special View:%d parameter */
788 if (strncmp (action, "View:", 5) == 0)
790 view_at_line_number = atoi (action + 5);
791 action = "View";
793 else
795 view_at_line_number = 0;
798 if (data == NULL)
800 char *extension_file;
801 gboolean mc_user_ext = TRUE;
802 gboolean home_error = FALSE;
804 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
805 if (!exist_file (extension_file))
807 g_free (extension_file);
808 check_stock_mc_ext:
809 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
810 if (!exist_file (extension_file))
812 g_free (extension_file);
813 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
815 mc_user_ext = FALSE;
818 g_file_get_contents (extension_file, &data, NULL, NULL);
819 g_free (extension_file);
820 if (data == NULL)
821 return 0;
823 if (strstr (data, "default/") == NULL)
825 if (strstr (data, "regex/") == NULL && strstr (data, "shell/") == NULL &&
826 strstr (data, "type/") == NULL)
828 g_free (data);
829 data = NULL;
831 if (!mc_user_ext)
833 char *title;
835 title = g_strdup_printf (_(" %s%s file error"),
836 mc_global.sysconfig_dir, MC_LIB_EXT);
837 message (D_ERROR, title, _("The format of the %smc.ext "
838 "file has changed with version 3.0. It seems that "
839 "the installation failed. Please fetch a fresh "
840 "copy from the Midnight Commander package."),
841 mc_global.sysconfig_dir);
842 g_free (title);
843 return 0;
846 home_error = TRUE;
847 goto check_stock_mc_ext;
851 if (home_error)
853 char *filebind_filename;
854 char *title;
856 filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
857 title = g_strdup_printf (_("%s file error"), filebind_filename);
858 message (D_ERROR, title,
859 _("The format of the %s file has "
860 "changed with version 3.0. You may either want to copy "
861 "it from %smc.ext or use that file as an example of how to write it."),
862 filebind_filename, mc_global.sysconfig_dir);
863 g_free (filebind_filename);
864 g_free (title);
868 mc_stat (filename_vpath, &mystat);
870 include_target = NULL;
871 include_target_len = 0;
872 filename = vfs_path_to_str (filename_vpath);
873 file_len = vfs_path_len (filename_vpath);
875 for (p = data; *p != '\0'; p++)
877 for (q = p; *q == ' ' || *q == '\t'; q++)
879 if (*q == '\n' || *q == '\0')
880 p = q; /* empty line */
881 if (*p == '#') /* comment */
882 while (*p != '\0' && *p != '\n')
883 p++;
884 if (*p == '\n')
885 continue;
886 if (*p == '\0')
887 break;
888 if (p == q)
889 { /* i.e. starts in the first column, should be
890 * keyword/descNL
892 gboolean case_insense;
894 found = FALSE;
895 q = strchr (p, '\n');
896 if (q == NULL)
897 q = strchr (p, '\0');
898 c = *q;
899 *q = '\0';
900 if (include_target)
902 if ((strncmp (p, "include/", 8) == 0)
903 && (strncmp (p + 8, include_target, include_target_len) == 0))
904 found = TRUE;
906 else if (strncmp (p, "regex/", 6) == 0)
908 mc_search_t *search;
910 p += 6;
911 case_insense = (strncmp (p, "i/", 2) == 0);
912 if (case_insense)
913 p += 2;
915 search = mc_search_new (p, -1);
916 if (search != NULL)
918 search->search_type = MC_SEARCH_T_REGEX;
919 search->is_case_sensitive = !case_insense;
920 found = mc_search_run (search, filename, 0, file_len, NULL);
921 mc_search_free (search);
924 else if (strncmp (p, "directory/", 10) == 0)
926 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
927 found = TRUE;
929 else if (strncmp (p, "shell/", 6) == 0)
931 int (*cmp_func) (const char *s1, const char *s2, size_t n) = strncmp;
933 p += 6;
934 case_insense = (strncmp (p, "i/", 2) == 0);
935 if (case_insense)
937 p += 2;
938 cmp_func = strncasecmp;
941 if (*p == '.' && file_len >= (size_t) (q - p))
943 if (cmp_func (p, filename + file_len - (q - p), q - p) == 0)
944 found = TRUE;
946 else
948 if ((size_t) (q - p) == file_len && cmp_func (p, filename, q - p) == 0)
949 found = TRUE;
952 else if (strncmp (p, "type/", 5) == 0)
954 GError *error = NULL;
956 p += 5;
958 case_insense = (strncmp (p, "i/", 2) == 0);
959 if (case_insense)
960 p += 2;
962 found = regex_check_type (filename_vpath, p, &have_type, case_insense, &error);
963 if (error != NULL)
965 g_error_free (error);
966 error_flag = TRUE; /* leave it if file cannot be opened */
969 else if (strncmp (p, "default/", 8) == 0)
970 found = TRUE;
972 *q = c;
973 p = q;
974 if (*p == '\0')
975 break;
977 else
978 { /* List of actions */
979 p = q;
980 q = strchr (p, '\n');
981 if (q == NULL)
982 q = strchr (p, '\0');
983 if (found && !error_flag)
985 r = strchr (p, '=');
986 if (r != NULL)
988 c = *r;
989 *r = '\0';
990 if (strcmp (p, "Include") == 0)
992 char *t;
994 include_target = p + 8;
995 t = strchr (include_target, '\n');
996 if (t != NULL)
997 *t = '\0';
998 include_target_len = strlen (include_target);
999 if (t != NULL)
1000 *t = '\n';
1002 *r = c;
1003 p = q;
1004 found = FALSE;
1006 if (*p == '\0')
1007 break;
1008 continue;
1011 if (strcmp (action, p) != 0)
1012 *r = c;
1013 else
1015 *r = c;
1017 for (p = r + 1; *p == ' ' || *p == '\t'; p++)
1020 /* Empty commands just stop searching
1021 * through, they don't do anything
1023 if (p < q)
1025 exec_extension (filename_vpath, r + 1, view_at_line_number);
1026 ret = 1;
1028 break;
1032 p = q;
1033 if (*p == '\0')
1034 break;
1037 g_free (filename);
1038 if (error_flag)
1039 ret = -1;
1040 return ret;
1043 /* --------------------------------------------------------------------------------------------- */