(panel_callback) [MSG_FOCUS]: remove self-draw here.
[midnight-commander.git] / src / filemanager / ext.c
blob3be115ad1682d061ea07bfa7782c8077b00b2d22
1 /*
2 Extension dependent execution.
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Jakub Jelinek, 1995
9 Miguel de Icaza, 1994
10 Slava Zanko <slavazanko@gmail.com>, 2013
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 #ifdef USE_FILE_CMD
54 #include "src/setup.h" /* use_file_to_check_type */
55 #endif
56 #include "src/execute.h"
57 #include "src/history.h"
58 #include "src/usermenu.h"
60 #include "src/consaver/cons.saver.h"
61 #include "src/viewer/mcviewer.h"
63 #ifdef HAVE_CHARSET
64 #include "src/selcodepage.h" /* do_set_codepage */
65 #endif
67 #include "filemanager.h" /* current_panel */
68 #include "panel.h" /* panel_cd */
70 #include "ext.h"
72 /*** global variables ****************************************************************************/
74 /*** file scope macro definitions ****************************************************************/
76 #ifdef USE_FILE_CMD
77 #ifdef FILE_B
78 #define FILE_CMD "file -z " FILE_B FILE_S FILE_L
79 #else
80 #define FILE_CMD "file -z " FILE_S FILE_L
81 #endif
82 #endif
84 /*** file scope type declarations ****************************************************************/
86 typedef char *(*quote_func_t) (const char *name, gboolean quote_percent);
88 /*** forward declarations (file scope functions) *************************************************/
90 /*** file scope variables ************************************************************************/
92 /* This variable points to a copy of the mc.ext file in memory
93 * With this we avoid loading/parsing the file each time we
94 * need it
96 static mc_config_t *ext_ini = NULL;
97 static gchar **ext_ini_groups = NULL;
98 static vfs_path_t *localfilecopy_vpath = NULL;
99 static char buffer[BUF_1K];
101 static char *pbuffer = NULL;
102 static time_t localmtime = 0;
103 static quote_func_t quote_func = name_quote;
104 static gboolean run_view = FALSE;
105 static gboolean is_cd = FALSE;
106 static gboolean written_nonspace = FALSE;
107 static gboolean do_local_copy = FALSE;
109 static const char *descr_group = "mc.ext.ini";
110 static const char *default_group = "Default";
112 /* --------------------------------------------------------------------------------------------- */
113 /*** file scope functions ************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
116 static void
117 exec_cleanup_script (vfs_path_t *script_vpath)
119 if (script_vpath != NULL)
121 (void) mc_unlink (script_vpath);
122 vfs_path_free (script_vpath, TRUE);
126 /* --------------------------------------------------------------------------------------------- */
128 static void
129 exec_cleanup_file_name (const vfs_path_t *filename_vpath, gboolean has_changed)
131 if (localfilecopy_vpath == NULL)
132 return;
134 if (has_changed)
136 struct stat mystat;
138 mc_stat (localfilecopy_vpath, &mystat);
139 has_changed = localmtime != mystat.st_mtime;
141 mc_ungetlocalcopy (filename_vpath, localfilecopy_vpath, has_changed);
142 vfs_path_free (localfilecopy_vpath, TRUE);
143 localfilecopy_vpath = NULL;
146 /* --------------------------------------------------------------------------------------------- */
148 static char *
149 exec_get_file_name (const vfs_path_t *filename_vpath)
151 if (!do_local_copy)
152 return quote_func (vfs_path_get_last_path_str (filename_vpath), FALSE);
154 if (localfilecopy_vpath == NULL)
156 struct stat mystat;
157 localfilecopy_vpath = mc_getlocalcopy (filename_vpath);
158 if (localfilecopy_vpath == NULL)
159 return NULL;
161 mc_stat (localfilecopy_vpath, &mystat);
162 localmtime = mystat.st_mtime;
165 return quote_func (vfs_path_get_last_path_str (localfilecopy_vpath), FALSE);
168 /* --------------------------------------------------------------------------------------------- */
170 static char *
171 exec_expand_format (char symbol, gboolean is_result_quoted)
173 char *text;
175 text = expand_format (NULL, symbol, TRUE);
176 if (is_result_quoted && text != NULL)
178 char *quoted_text;
180 quoted_text = g_strdup_printf ("\"%s\"", text);
181 g_free (text);
182 text = quoted_text;
184 return text;
187 /* --------------------------------------------------------------------------------------------- */
189 static GString *
190 exec_get_export_variables (const vfs_path_t *filename_vpath)
192 char *text;
193 GString *export_vars_string;
194 size_t i;
196 /* *INDENT-OFF* */
197 struct
199 const char symbol;
200 const char *name;
201 const gboolean is_result_quoted;
202 } export_variables[] = {
203 {'p', "MC_EXT_BASENAME", FALSE},
204 {'d', "MC_EXT_CURRENTDIR", FALSE},
205 {'s', "MC_EXT_SELECTED", TRUE},
206 {'t', "MC_EXT_ONLYTAGGED", TRUE},
207 {'\0', NULL, FALSE}
209 /* *INDENT-ON* */
211 text = exec_get_file_name (filename_vpath);
212 if (text == NULL)
213 return NULL;
215 export_vars_string = g_string_new ("MC_EXT_FILENAME=");
216 g_string_append_printf (export_vars_string, "%s\nexport MC_EXT_FILENAME\n", text);
217 g_free (text);
219 for (i = 0; export_variables[i].name != NULL; i++)
221 text =
222 exec_expand_format (export_variables[i].symbol, export_variables[i].is_result_quoted);
223 if (text != NULL)
225 g_string_append_printf (export_vars_string,
226 "%s=%s\nexport %s\n", export_variables[i].name, text,
227 export_variables[i].name);
228 g_free (text);
232 return export_vars_string;
235 /* --------------------------------------------------------------------------------------------- */
237 static GString *
238 exec_make_shell_string (const char *lc_data, const vfs_path_t *filename_vpath)
240 GString *shell_string;
241 char lc_prompt[80] = "\0";
242 gboolean parameter_found = FALSE;
243 gboolean expand_prefix_found = FALSE;
245 shell_string = g_string_new ("");
247 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
249 if (parameter_found)
251 if (*lc_data == '}')
253 char *parameter;
255 parameter_found = FALSE;
256 parameter =
257 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "",
258 INPUT_COMPLETE_NONE);
259 if (parameter == NULL)
261 /* User canceled */
262 g_string_free (shell_string, TRUE);
263 exec_cleanup_file_name (filename_vpath, FALSE);
264 return NULL;
266 g_string_append (shell_string, parameter);
267 written_nonspace = TRUE;
268 g_free (parameter);
270 else
272 size_t len = strlen (lc_prompt);
274 if (len < sizeof (lc_prompt) - 1)
276 lc_prompt[len] = *lc_data;
277 lc_prompt[len + 1] = '\0';
281 else if (expand_prefix_found)
283 expand_prefix_found = FALSE;
284 if (*lc_data == '{')
285 parameter_found = TRUE;
286 else
288 int i;
290 i = check_format_view (lc_data);
291 if (i != 0)
293 lc_data += i - 1;
294 run_view = TRUE;
296 else
298 i = check_format_cd (lc_data);
299 if (i > 0)
301 is_cd = TRUE;
302 quote_func = fake_name_quote;
303 do_local_copy = FALSE;
304 pbuffer = buffer;
305 lc_data += i - 1;
307 else
309 char *v;
311 i = check_format_var (lc_data, &v);
312 if (i > 0)
314 g_string_append (shell_string, v);
315 g_free (v);
316 lc_data += i;
318 else
320 char *text;
322 if (*lc_data != 'f')
323 text = expand_format (NULL, *lc_data, !is_cd);
324 else
326 text = exec_get_file_name (filename_vpath);
327 if (text == NULL)
329 g_string_free (shell_string, TRUE);
330 return NULL;
334 if (text != NULL)
336 if (!is_cd)
337 g_string_append (shell_string, text);
338 else
340 strcpy (pbuffer, text);
341 pbuffer = strchr (pbuffer, '\0');
344 g_free (text);
347 written_nonspace = TRUE;
353 else if (*lc_data == '%')
354 expand_prefix_found = TRUE;
355 else
357 if (!whitespace (*lc_data))
358 written_nonspace = TRUE;
359 if (is_cd)
360 *(pbuffer++) = *lc_data;
361 else
362 g_string_append_c (shell_string, *lc_data);
364 } /* for */
366 return shell_string;
369 /* --------------------------------------------------------------------------------------------- */
371 static void
372 exec_extension_view (void *target, char *cmd, const vfs_path_t *filename_vpath, int start_line)
374 mcview_mode_flags_t def_flags = {
375 /* *INDENT-OFF* */
376 .wrap = FALSE,
377 .hex = mcview_global_flags.hex,
378 .magic = FALSE,
379 .nroff = mcview_global_flags.nroff
380 /* *INDENT-ON* */
383 mcview_mode_flags_t changed_flags;
385 mcview_clear_mode_flags (&changed_flags);
386 mcview_altered_flags.hex = FALSE;
387 mcview_altered_flags.nroff = FALSE;
388 if (def_flags.hex != mcview_global_flags.hex)
389 changed_flags.hex = TRUE;
390 if (def_flags.nroff != mcview_global_flags.nroff)
391 changed_flags.nroff = TRUE;
393 if (target == NULL)
394 mcview_viewer (cmd, filename_vpath, start_line, 0, 0);
395 else
396 mcview_load ((WView *) target, cmd, vfs_path_as_str (filename_vpath), start_line, 0, 0);
398 if (changed_flags.hex && !mcview_altered_flags.hex)
399 mcview_global_flags.hex = def_flags.hex;
400 if (changed_flags.nroff && !mcview_altered_flags.nroff)
401 mcview_global_flags.nroff = def_flags.nroff;
403 dialog_switch_process_pending ();
406 /* --------------------------------------------------------------------------------------------- */
408 static void
409 exec_extension_cd (WPanel *panel)
411 char *q;
412 vfs_path_t *p_vpath;
414 *pbuffer = '\0';
415 pbuffer = buffer;
416 /* Search last non-space character. Start search at the end in order
417 not to short filenames containing spaces. */
418 q = pbuffer + strlen (pbuffer) - 1;
419 while (q >= pbuffer && whitespace (*q))
420 q--;
421 q[1] = 0;
423 p_vpath = vfs_path_from_str_flags (pbuffer, VPF_NO_CANON);
424 panel_cd (panel, p_vpath, cd_parse_command);
425 vfs_path_free (p_vpath, TRUE);
429 /* --------------------------------------------------------------------------------------------- */
431 static vfs_path_t *
432 exec_extension (WPanel *panel, void *target, const vfs_path_t *filename_vpath,
433 const char *lc_data, int start_line)
435 GString *shell_string, *export_variables;
436 vfs_path_t *script_vpath = NULL;
437 int cmd_file_fd;
438 FILE *cmd_file;
439 char *cmd = NULL;
441 pbuffer = NULL;
442 localmtime = 0;
443 quote_func = name_quote;
444 run_view = FALSE;
445 is_cd = FALSE;
446 written_nonspace = FALSE;
448 /* Avoid making a local copy if we are doing a cd */
449 do_local_copy = !vfs_file_is_local (filename_vpath);
451 shell_string = exec_make_shell_string (lc_data, filename_vpath);
452 if (shell_string == NULL)
453 goto ret;
455 if (is_cd)
457 exec_extension_cd (panel);
458 g_string_free (shell_string, TRUE);
459 goto ret;
463 * All commands should be run in /bin/sh regardless of user shell.
464 * To do that, create temporary shell script and run it.
465 * Sometimes it's not needed (e.g. for %cd and %view commands),
466 * but it's easier to create it anyway.
468 cmd_file_fd = mc_mkstemps (&script_vpath, "mcext", SCRIPT_SUFFIX);
470 if (cmd_file_fd == -1)
472 message (D_ERROR, MSG_ERROR,
473 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
474 g_string_free (shell_string, TRUE);
475 goto ret;
478 cmd_file = fdopen (cmd_file_fd, "w");
479 fputs ("#! /bin/sh\n\n", cmd_file);
481 export_variables = exec_get_export_variables (filename_vpath);
482 if (export_variables != NULL)
484 fputs (export_variables->str, cmd_file);
485 g_string_free (export_variables, TRUE);
488 fputs (shell_string->str, cmd_file);
489 g_string_free (shell_string, TRUE);
492 * Make the script remove itself when it finishes.
493 * Don't do it for the viewer - it may need to rerun the script,
494 * so we clean up after calling view().
496 if (!run_view)
497 fprintf (cmd_file, "\n/bin/rm -f %s\n", vfs_path_as_str (script_vpath));
499 fclose (cmd_file);
501 if ((run_view && !written_nonspace) || is_cd)
503 exec_cleanup_script (script_vpath);
504 script_vpath = NULL;
506 else
508 /* Set executable flag on the command file ... */
509 mc_chmod (script_vpath, S_IRWXU);
510 /* ... but don't rely on it - run /bin/sh explicitly */
511 cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (script_vpath), (char *) NULL);
514 if (run_view)
516 /* If we've written whitespace only, then just load filename into view */
517 if (!written_nonspace)
518 exec_extension_view (target, NULL, filename_vpath, start_line);
519 else
520 exec_extension_view (target, cmd, filename_vpath, start_line);
522 else
524 shell_execute (cmd, EXECUTE_INTERNAL);
525 if (mc_global.tty.console_flag != '\0')
527 handle_console (CONSOLE_SAVE);
528 if (output_lines != 0 && mc_global.keybar_visible)
530 unsigned char end_line;
532 end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
533 show_console_contents (output_start_y, end_line - output_lines, end_line);
538 g_free (cmd);
540 exec_cleanup_file_name (filename_vpath, TRUE);
541 ret:
542 return script_vpath;
545 /* --------------------------------------------------------------------------------------------- */
547 * Run cmd_file with args, put result into buf.
548 * If error, put '\0' into buf[0]
549 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
551 * NOTES: buf is null-terminated string.
554 #ifdef USE_FILE_CMD
555 static int
556 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
558 gboolean read_bytes = FALSE;
559 char *command;
560 FILE *f;
562 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
563 f = popen (command, "r");
564 g_free (command);
566 if (f != NULL)
568 #ifdef __QNXNTO__
569 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
571 (void) pclose (f);
572 return -1;
574 #endif
575 read_bytes = (fgets (buf, buflen, f) != NULL);
576 if (!read_bytes)
577 buf[0] = '\0'; /* Paranoid termination */
578 pclose (f);
580 else
582 buf[0] = '\0'; /* Paranoid termination */
583 return -1;
586 buf[buflen - 1] = '\0';
588 return read_bytes ? 1 : 0;
591 /* --------------------------------------------------------------------------------------------- */
593 * Run the "file" command on the local file.
594 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
597 static int
598 get_file_type_local (const vfs_path_t *filename_vpath, char *buf, int buflen)
600 char *filename_quoted;
601 int ret = 0;
603 filename_quoted = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
604 if (filename_quoted != NULL)
606 ret = get_popen_information (FILE_CMD, filename_quoted, buf, buflen);
607 g_free (filename_quoted);
610 return ret;
613 /* --------------------------------------------------------------------------------------------- */
615 * Run the "enca" command on the local file.
616 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
619 #ifdef HAVE_CHARSET
620 static int
621 get_file_encoding_local (const vfs_path_t *filename_vpath, char *buf, int buflen)
623 char *filename_quoted;
624 int ret = 0;
626 filename_quoted = name_quote (vfs_path_get_last_path_str (filename_vpath), FALSE);
627 if (filename_quoted != NULL)
629 char *lang;
631 lang = name_quote (autodetect_codeset, FALSE);
632 if (lang != NULL)
634 char *args;
636 args = g_strdup_printf (" -L %s -i %s", lang, filename_quoted);
637 g_free (lang);
639 ret = get_popen_information ("enca", args, buf, buflen);
640 g_free (args);
643 g_free (filename_quoted);
646 return ret;
648 #endif /* HAVE_CHARSET */
650 /* --------------------------------------------------------------------------------------------- */
652 * Invoke the "file" command on the file and match its output against PTR.
653 * have_type is a flag that is set if we already have tried to determine
654 * the type of that file.
655 * Return TRUE for match, FALSE otherwise.
658 static gboolean
659 regex_check_type (const vfs_path_t *filename_vpath, const char *ptr, gboolean case_insense,
660 gboolean *have_type, GError **mcerror)
662 gboolean found = FALSE;
664 /* Following variables are valid if *have_type is TRUE */
665 static char content_string[2048];
666 static size_t content_shift = 0;
667 static int got_data = 0;
669 mc_return_val_if_error (mcerror, FALSE);
671 if (!*have_type)
673 vfs_path_t *localfile_vpath;
675 #ifdef HAVE_CHARSET
676 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
677 int got_encoding_data;
678 #endif /* HAVE_CHARSET */
680 /* Don't repeate even unsuccessful checks */
681 *have_type = TRUE;
683 localfile_vpath = mc_getlocalcopy (filename_vpath);
684 if (localfile_vpath == NULL)
686 mc_propagate_error (mcerror, 0, _("Cannot fetch a local copy of %s"),
687 vfs_path_as_str (filename_vpath));
688 return FALSE;
692 #ifdef HAVE_CHARSET
693 got_encoding_data = is_autodetect_codeset_enabled
694 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
696 if (got_encoding_data > 0)
698 char *pp;
699 int cp_id;
701 pp = strchr (encoding_id, '\n');
702 if (pp != NULL)
703 *pp = '\0';
705 cp_id = get_codepage_index (encoding_id);
706 if (cp_id == -1)
707 cp_id = default_source_codepage;
709 do_set_codepage (cp_id);
711 #endif /* HAVE_CHARSET */
713 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
715 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
717 if (got_data > 0)
719 char *pp;
721 pp = strchr (content_string, '\n');
722 if (pp != NULL)
723 *pp = '\0';
725 #ifndef FILE_B
727 const char *real_name; /* name used with "file" */
728 size_t real_len;
730 real_name = vfs_path_get_last_path_str (localfile_vpath);
731 real_len = strlen (real_name);
733 if (strncmp (content_string, real_name, real_len) == 0)
735 /* Skip "real_name: " */
736 content_shift = real_len;
738 /* Solaris' file prints tab(s) after ':' */
739 if (content_string[content_shift] == ':')
740 for (content_shift++; whitespace (content_string[content_shift]);
741 content_shift++)
745 #endif /* FILE_B */
747 else
749 /* No data */
750 content_string[0] = '\0';
752 vfs_path_free (localfile_vpath, TRUE);
755 if (got_data == -1)
757 mc_propagate_error (mcerror, 0, "%s", _("Pipe failed"));
758 return FALSE;
761 if (content_string[0] != '\0')
763 mc_search_t *search;
765 search = mc_search_new (ptr, DEFAULT_CHARSET);
766 if (search != NULL)
768 search->search_type = MC_SEARCH_T_REGEX;
769 search->is_case_sensitive = !case_insense;
770 found = mc_search_run (search, content_string + content_shift, 0, -1, NULL);
771 mc_search_free (search);
773 else
775 mc_propagate_error (mcerror, 0, "%s", _("Regular expression error"));
779 return found;
781 #endif /* USE_FILE_CMD */
783 /* --------------------------------------------------------------------------------------------- */
785 static void
786 check_old_extension_file (void)
788 char *extension_old_file;
790 extension_old_file = mc_config_get_full_path (MC_EXT_OLD_FILE);
791 if (exist_file (extension_old_file))
792 message (D_ERROR, _("Warning"),
793 _("You have an outdated %s file.\nMidnight Commander now uses %s file.\n"
794 "Please copy your modifications of the old file to the new one."),
795 extension_old_file, MC_EXT_FILE);
796 g_free (extension_old_file);
799 /* --------------------------------------------------------------------------------------------- */
801 static gboolean
802 load_extension_file (void)
804 char *extension_file;
805 gboolean mc_user_ext = TRUE;
806 gboolean home_error = FALSE;
808 extension_file = mc_config_get_full_path (MC_EXT_FILE);
809 if (!exist_file (extension_file))
811 g_free (extension_file);
813 check_old_extension_file ();
815 check_stock_mc_ext:
816 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_EXT_FILE, (char *) NULL);
817 if (!exist_file (extension_file))
819 g_free (extension_file);
820 extension_file =
821 mc_build_filename (mc_global.share_data_dir, MC_EXT_FILE, (char *) NULL);
822 if (!exist_file (extension_file))
823 MC_PTR_FREE (extension_file);
825 mc_user_ext = FALSE;
828 if (extension_file != NULL)
830 ext_ini = mc_config_init (extension_file, TRUE);
831 g_free (extension_file);
833 if (ext_ini == NULL)
834 return FALSE;
836 /* Check version */
837 if (!mc_config_has_group (ext_ini, descr_group))
839 flush_extension_file ();
841 if (!mc_user_ext)
843 message (D_ERROR, MSG_ERROR,
844 _("The format of the\n%s%s\nfile has changed with version 4.0.\n"
845 "It seems that the installation has failed.\nPlease fetch a fresh copy "
846 "from the Midnight Commander package."),
847 mc_global.sysconfig_dir, MC_EXT_FILE);
848 return FALSE;
851 home_error = TRUE;
852 goto check_stock_mc_ext;
855 if (home_error)
857 extension_file = mc_config_get_full_path (MC_EXT_FILE);
858 message (D_ERROR, MSG_ERROR,
859 _("The format of the\n%s\nfile has changed with version 4.0.\nYou may either want "
860 "to copy it from\n%s%s\nor use that file as an example of how to write it."),
861 extension_file, mc_global.sysconfig_dir, MC_EXT_FILE);
862 g_free (extension_file);
865 return TRUE;
868 /* --------------------------------------------------------------------------------------------- */
869 /*** public functions ****************************************************************************/
870 /* --------------------------------------------------------------------------------------------- */
872 void
873 flush_extension_file (void)
875 g_strfreev (ext_ini_groups);
876 ext_ini_groups = NULL;
878 mc_config_deinit (ext_ini);
879 ext_ini = NULL;
882 /* --------------------------------------------------------------------------------------------- */
884 * The second argument is action, i.e. Open, View or Edit
885 * Use target object to open file in.
887 * This function returns:
889 * -1 for a failure or user interrupt
890 * 0 if no command was run
891 * 1 if some command was run
893 * If action == "View" then a parameter is checked in the form of "View:%d",
894 * if the value for %d exists, then the viewer is started up at that line number.
898 regex_command_for (void *target, const vfs_path_t *filename_vpath, const char *action,
899 vfs_path_t **script_vpath)
901 const char *filename;
902 size_t filename_len;
903 gboolean found = FALSE;
904 gboolean error_flag = FALSE;
905 int ret = 0;
906 struct stat mystat;
907 int view_at_line_number = 0;
908 #ifdef USE_FILE_CMD
909 gboolean have_type = FALSE; /* Flag used by regex_check_type() */
910 #endif
911 char **group_iter;
912 char *include_group = NULL;
913 const char *current_group;
915 if (filename_vpath == NULL)
916 return 0;
918 if (script_vpath != NULL)
919 *script_vpath = NULL;
921 /* Check for the special View:%d parameter */
922 if (strncmp (action, "View:", 5) == 0)
924 view_at_line_number = atoi (action + 5);
925 action = "View";
928 if (ext_ini == NULL && !load_extension_file ())
929 return 0;
931 mc_stat (filename_vpath, &mystat);
933 filename = vfs_path_get_last_path_str (filename_vpath);
934 filename = x_basename (filename);
935 filename_len = strlen (filename);
937 if (ext_ini_groups == NULL)
938 ext_ini_groups = mc_config_get_groups (ext_ini, NULL);
940 /* find matched type, regex or shell pattern */
941 for (group_iter = ext_ini_groups; *group_iter != NULL && !found; group_iter++)
943 enum
945 TYPE_UNUSED,
946 TYPE_NOT_FOUND,
947 TYPE_FOUND
948 } type_state = TYPE_UNUSED;
950 const gchar *g = *group_iter;
951 gchar *pattern;
952 gboolean ignore_case;
954 if (strcmp (g, descr_group) == 0 || strncmp (g, "Include/", 8) == 0
955 || strcmp (g, default_group) == 0)
956 continue;
958 /* The "Directory" parameter is a special case: if it's present then
959 "Type", "Regex", and "Shell" parameters are ignored */
960 pattern = mc_config_get_string_raw (ext_ini, g, "Directory", NULL);
961 if (pattern != NULL)
963 found = S_ISDIR (mystat.st_mode)
964 && mc_search (pattern, DEFAULT_CHARSET, vfs_path_as_str (filename_vpath),
965 MC_SEARCH_T_REGEX);
966 g_free (pattern);
968 continue; /* stop if found */
971 #ifdef USE_FILE_CMD
972 if (use_file_to_check_type)
974 pattern = mc_config_get_string_raw (ext_ini, g, "Type", NULL);
975 if (pattern != NULL)
977 GError *mcerror = NULL;
979 ignore_case = mc_config_get_bool (ext_ini, g, "TypeIgnoreCase", FALSE);
980 type_state =
981 regex_check_type (filename_vpath, pattern, ignore_case, &have_type, &mcerror)
982 ? TYPE_FOUND : TYPE_NOT_FOUND;
983 g_free (pattern);
985 if (mc_error_message (&mcerror, NULL))
986 error_flag = TRUE; /* leave it if file cannot be opened */
988 if (type_state == TYPE_NOT_FOUND)
989 continue;
992 #endif /* USE_FILE_CMD */
994 pattern = mc_config_get_string_raw (ext_ini, g, "Regex", NULL);
995 if (pattern != NULL)
997 mc_search_t *search;
999 ignore_case = mc_config_get_bool (ext_ini, g, "RegexIgnoreCase", FALSE);
1000 search = mc_search_new (pattern, DEFAULT_CHARSET);
1001 g_free (pattern);
1003 if (search != NULL)
1005 search->search_type = MC_SEARCH_T_REGEX;
1006 search->is_case_sensitive = !ignore_case;
1007 found = mc_search_run (search, filename, 0, filename_len, NULL);
1008 mc_search_free (search);
1011 found = found && (type_state == TYPE_UNUSED || type_state == TYPE_FOUND);
1013 else
1015 pattern = mc_config_get_string_raw (ext_ini, g, "Shell", NULL);
1016 if (pattern != NULL)
1018 int (*cmp_func) (const char *s1, const char *s2, size_t n);
1019 size_t pattern_len;
1021 ignore_case = mc_config_get_bool (ext_ini, g, "ShellIgnoreCase", FALSE);
1022 cmp_func = ignore_case ? strncasecmp : strncmp;
1023 pattern_len = strlen (pattern);
1025 if (*pattern == '.' && filename_len >= pattern_len)
1026 found =
1027 cmp_func (pattern, filename + filename_len - pattern_len, pattern_len) == 0;
1028 else
1029 found = pattern_len == filename_len
1030 && cmp_func (pattern, filename, filename_len) == 0;
1032 g_free (pattern);
1034 found = found && (type_state == TYPE_UNUSED || type_state == TYPE_FOUND);
1036 else
1037 found = type_state == TYPE_FOUND;
1041 /* group is found, process actions */
1042 if (found)
1044 char *include_value;
1046 group_iter--;
1048 /* "Include" parameter has the highest priority over any actions */
1049 include_value = mc_config_get_string_raw (ext_ini, *group_iter, "Include", NULL);
1050 if (include_value != NULL)
1052 /* find "Include/include_value" group */
1053 include_group = g_strconcat ("Include/", include_value, (char *) NULL);
1054 g_free (include_value);
1055 found = mc_config_has_group (ext_ini, include_group);
1059 if (found)
1060 current_group = include_group != NULL ? include_group : *group_iter;
1061 else
1063 current_group = default_group;
1064 found = mc_config_has_group (ext_ini, current_group);
1067 if (found && !error_flag)
1069 gchar *action_value;
1071 action_value = mc_config_get_string_raw (ext_ini, current_group, action, NULL);
1072 if (action_value == NULL)
1074 /* Not found, try the action from default section */
1075 action_value = mc_config_get_string_raw (ext_ini, default_group, action, NULL);
1076 found = (action_value != NULL && *action_value != '\0');
1078 else
1080 /* If action's value is empty, ignore action from default section */
1081 found = (*action_value != '\0');
1084 if (found)
1086 vfs_path_t *sv;
1088 sv = exec_extension (current_panel, target, filename_vpath, action_value,
1089 view_at_line_number);
1090 if (script_vpath != NULL)
1091 *script_vpath = sv;
1092 else
1093 exec_cleanup_script (sv);
1095 ret = 1;
1098 g_free (action_value);
1101 g_free (include_group);
1103 return (error_flag ? -1 : ret);
1106 /* --------------------------------------------------------------------------------------------- */