2 Extension dependent execution.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 The Free Software Foundation, Inc.
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/>.
29 * \brief Source: extension dependent execution
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"
47 #include "lib/vfs/vfs.h"
48 #include "lib/widget.h"
50 #include "lib/charsets.h" /* get_codepage_index */
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"
61 #include "src/selcodepage.h" /* do_set_codepage */
64 #include "panel.h" /* do_cd */
69 /*** global variables ****************************************************************************/
71 /*** file scope macro definitions ****************************************************************/
74 #define FILE_CMD "file -L "
76 #define FILE_CMD "file "
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
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 /* --------------------------------------------------------------------------------------------- */
105 exec_cleanup_file_name (const vfs_path_t
* filename_vpath
, gboolean has_changed
)
107 if (localfilecopy_vpath
== NULL
)
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 /* --------------------------------------------------------------------------------------------- */
125 exec_get_file_name (const vfs_path_t
* filename_vpath
)
128 return quote_func (vfs_path_get_last_path_str (filename_vpath
), 0);
130 if (localfilecopy_vpath
== NULL
)
133 localfilecopy_vpath
= mc_getlocalcopy (filename_vpath
);
134 if (localfilecopy_vpath
== 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 exec_expand_format (char symbol
, gboolean is_result_quoted
)
150 text
= expand_format (NULL
, symbol
, TRUE
);
151 if (is_result_quoted
&& text
!= NULL
)
155 quoted_text
= g_strdup_printf ("\"%s\"", text
);
162 /* --------------------------------------------------------------------------------------------- */
165 exec_get_export_variables (const vfs_path_t
* filename_vpath
)
168 GString
*export_vars_string
;
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
},
186 text
= exec_get_file_name (filename_vpath
);
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
);
194 for (i
= 0; export_variables
[i
].name
!= NULL
; i
++)
197 exec_expand_format (export_variables
[i
].symbol
, export_variables
[i
].is_result_quoted
);
200 g_string_append_printf (export_vars_string
,
201 "%s=%s\nexport %s\n", export_variables
[i
].name
, text
,
202 export_variables
[i
].name
);
206 return g_string_free (export_vars_string
, FALSE
);
209 /* --------------------------------------------------------------------------------------------- */
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
++)
229 parameter_found
= FALSE
;
231 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "",
232 INPUT_COMPLETE_NONE
);
233 if (parameter
== NULL
)
236 g_string_free (shell_string
, TRUE
);
237 exec_cleanup_file_name (filename_vpath
, FALSE
);
240 g_string_append (shell_string
, parameter
);
241 written_nonspace
= TRUE
;
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
;
259 parameter_found
= TRUE
;
265 i
= check_format_view (lc_data
);
273 i
= check_format_cd (lc_data
);
277 quote_func
= fake_name_quote
;
278 do_local_copy
= FALSE
;
284 i
= check_format_var (lc_data
, &v
);
285 if (i
> 0 && v
!= NULL
)
287 g_string_append (shell_string
, v
);
296 text
= expand_format (NULL
, *lc_data
, !is_cd
);
299 text
= exec_get_file_name (filename_vpath
);
302 g_string_free (shell_string
, TRUE
);
308 g_string_append (shell_string
, text
);
311 strcpy (pbuffer
, text
);
312 pbuffer
= strchr (pbuffer
, 0);
316 written_nonspace
= TRUE
;
322 else if (*lc_data
== '%')
323 expand_prefix_found
= TRUE
;
326 if (*lc_data
!= ' ' && *lc_data
!= '\t')
327 written_nonspace
= TRUE
;
329 *(pbuffer
++) = *lc_data
;
331 g_string_append_c (shell_string
, *lc_data
);
334 return g_string_free (shell_string
, FALSE
);
337 /* --------------------------------------------------------------------------------------------- */
340 exec_extension_view (char *cmd
, const vfs_path_t
* filename_vpath
, int start_line
)
342 int def_hex_mode
= mcview_default_hex_mode
, changed_hex_mode
= 0;
343 int def_nroff_flag
= mcview_default_nroff_flag
, changed_nroff_flag
= 0;
345 mcview_altered_hex_mode
= 0;
346 mcview_altered_nroff_flag
= 0;
347 if (def_hex_mode
!= mcview_default_hex_mode
)
348 changed_hex_mode
= 1;
349 if (def_nroff_flag
!= mcview_default_nroff_flag
)
350 changed_nroff_flag
= 1;
352 mcview_viewer (cmd
, filename_vpath
, start_line
);
354 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
355 mcview_default_hex_mode
= def_hex_mode
;
356 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
357 mcview_default_nroff_flag
= def_nroff_flag
;
359 dialog_switch_process_pending ();
362 /* --------------------------------------------------------------------------------------------- */
365 exec_extension_cd (void)
372 /* while (*p == ' ' && *p == '\t')
375 /* Search last non-space character. Start search at the end in order
376 not to short filenames containing spaces. */
377 q
= pbuffer
+ strlen (pbuffer
) - 1;
378 while (q
>= pbuffer
&& (*q
== ' ' || *q
== '\t'))
382 p_vpath
= vfs_path_from_str_flags (pbuffer
, VPF_NO_CANON
);
383 do_cd (p_vpath
, cd_parse_command
);
384 vfs_path_free (p_vpath
);
388 /* --------------------------------------------------------------------------------------------- */
391 exec_extension (const vfs_path_t
* filename_vpath
, const char *lc_data
, int start_line
)
393 char *shell_string
, *export_variables
;
394 vfs_path_t
*script_vpath
= NULL
;
399 g_return_if_fail (lc_data
!= NULL
);
403 quote_func
= name_quote
;
406 written_nonspace
= FALSE
;
408 /* Avoid making a local copy if we are doing a cd */
409 do_local_copy
= !vfs_file_is_local (filename_vpath
);
411 shell_string
= exec_make_shell_string (lc_data
, filename_vpath
);
413 if (shell_string
== NULL
)
418 exec_extension_cd ();
419 g_free (shell_string
);
424 * All commands should be run in /bin/sh regardless of user shell.
425 * To do that, create temporary shell script and run it.
426 * Sometimes it's not needed (e.g. for %cd and %view commands),
427 * but it's easier to create it anyway.
429 cmd_file_fd
= mc_mkstemps (&script_vpath
, "mcext", SCRIPT_SUFFIX
);
431 if (cmd_file_fd
== -1)
433 message (D_ERROR
, MSG_ERROR
,
434 _("Cannot create temporary command file\n%s"), unix_error_string (errno
));
438 cmd_file
= fdopen (cmd_file_fd
, "w");
439 fputs ("#! /bin/sh\n\n", cmd_file
);
441 export_variables
= exec_get_export_variables (filename_vpath
);
442 if (export_variables
!= NULL
)
444 fprintf (cmd_file
, "%s\n", export_variables
);
445 g_free (export_variables
);
448 fputs (shell_string
, cmd_file
);
449 g_free (shell_string
);
452 * Make the script remove itself when it finishes.
453 * Don't do it for the viewer - it may need to rerun the script,
454 * so we clean up after calling view().
460 file_name
= vfs_path_to_str (script_vpath
);
461 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
467 if ((run_view
&& !written_nonspace
) || is_cd
)
469 mc_unlink (script_vpath
);
470 vfs_path_free (script_vpath
);
477 file_name
= vfs_path_to_str (script_vpath
);
478 /* Set executable flag on the command file ... */
479 mc_chmod (script_vpath
, S_IRWXU
);
480 /* ... but don't rely on it - run /bin/sh explicitly */
481 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
487 /* If we've written whitespace only, then just load filename into view */
488 if (!written_nonspace
)
489 exec_extension_view (NULL
, filename_vpath
, start_line
);
492 exec_extension_view (cmd
, filename_vpath
, start_line
);
493 mc_unlink (script_vpath
);
498 shell_execute (cmd
, EXECUTE_INTERNAL
);
499 if (mc_global
.tty
.console_flag
!= '\0')
501 handle_console (CONSOLE_SAVE
);
502 if (output_lines
&& mc_global
.keybar_visible
)
503 show_console_contents (output_start_y
,
504 LINES
- mc_global
.keybar_visible
-
505 output_lines
- 1, LINES
- mc_global
.keybar_visible
- 1);
511 exec_cleanup_file_name (filename_vpath
, TRUE
);
513 vfs_path_free (script_vpath
);
516 /* --------------------------------------------------------------------------------------------- */
518 * Run cmd_file with args, put result into buf.
519 * If error, put '\0' into buf[0]
520 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
522 * NOTES: buf is null-terminated string.
526 get_popen_information (const char *cmd_file
, const char *args
, char *buf
, int buflen
)
528 gboolean read_bytes
= FALSE
;
532 command
= g_strconcat (cmd_file
, args
, " 2>/dev/null", (char *) NULL
);
533 f
= popen (command
, "r");
539 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0)
545 read_bytes
= (fgets (buf
, buflen
, f
) != NULL
);
547 buf
[0] = '\0'; /* Paranoid termination */
552 buf
[0] = '\0'; /* Paranoid termination */
556 buf
[buflen
- 1] = '\0';
558 return read_bytes
? 1 : 0;
561 /* --------------------------------------------------------------------------------------------- */
563 * Run the "file" command on the local file.
564 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
568 get_file_type_local (const vfs_path_t
* filename_vpath
, char *buf
, int buflen
)
573 tmp
= name_quote (vfs_path_get_last_path_str (filename_vpath
), 0);
574 ret
= get_popen_information (FILE_CMD
, tmp
, buf
, buflen
);
580 /* --------------------------------------------------------------------------------------------- */
582 * Run the "enca" command on the local file.
583 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
588 get_file_encoding_local (const vfs_path_t
* filename_vpath
, char *buf
, int buflen
)
590 char *tmp
, *lang
, *args
;
593 tmp
= name_quote (vfs_path_get_last_path_str (filename_vpath
), 0);
594 lang
= name_quote (autodetect_codeset
, 0);
595 args
= g_strconcat (" -L", lang
, " -i ", tmp
, (char *) NULL
);
597 ret
= get_popen_information ("enca", args
, buf
, buflen
);
605 #endif /* HAVE_CHARSET */
607 /* --------------------------------------------------------------------------------------------- */
609 * Invoke the "file" command on the file and match its output against PTR.
610 * have_type is a flag that is set if we already have tried to determine
611 * the type of that file.
612 * Return 1 for match, 0 for no match, -1 errors.
616 regex_check_type (const vfs_path_t
* filename_vpath
, const char *ptr
, int *have_type
,
617 gboolean case_insense
, GError
** error
)
619 gboolean found
= FALSE
;
621 /* Following variables are valid if *have_type is 1 */
622 static char content_string
[2048];
624 static char encoding_id
[21]; /* CSISO51INISCYRILLIC -- 20 */
626 static size_t content_shift
= 0;
627 static int got_data
= 0;
629 if (!use_file_to_check_type
)
634 vfs_path_t
*localfile_vpath
;
635 const char *realname
; /* name used with "file" */
638 int got_encoding_data
;
639 #endif /* HAVE_CHARSET */
641 /* Don't repeate even unsuccessful checks */
644 localfile_vpath
= mc_getlocalcopy (filename_vpath
);
645 if (localfile_vpath
== NULL
)
649 filename
= vfs_path_to_str (filename_vpath
);
650 g_propagate_error (error
,
651 g_error_new (MC_ERROR
, -1,
652 _("Cannot fetch a local copy of %s"), filename
));
657 realname
= vfs_path_get_last_path_str (localfile_vpath
);
660 got_encoding_data
= is_autodetect_codeset_enabled
661 ? get_file_encoding_local (localfile_vpath
, encoding_id
, sizeof (encoding_id
)) : 0;
663 if (got_encoding_data
> 0)
668 pp
= strchr (encoding_id
, '\n');
672 cp_id
= get_codepage_index (encoding_id
);
674 cp_id
= default_source_codepage
;
676 do_set_codepage (cp_id
);
678 #endif /* HAVE_CHARSET */
680 mc_ungetlocalcopy (filename_vpath
, localfile_vpath
, FALSE
);
682 got_data
= get_file_type_local (localfile_vpath
, content_string
, sizeof (content_string
));
689 pp
= strchr (content_string
, '\n');
693 real_len
= strlen (realname
);
695 if (strncmp (content_string
, realname
, real_len
) == 0)
697 /* Skip "realname: " */
698 content_shift
= real_len
;
699 if (content_string
[content_shift
] == ':')
701 /* Solaris' file prints tab(s) after ':' */
702 for (content_shift
++;
703 content_string
[content_shift
] == ' '
704 || content_string
[content_shift
] == '\t'; content_shift
++)
712 content_string
[0] = '\0';
714 vfs_path_free (localfile_vpath
);
719 g_propagate_error (error
, g_error_new (MC_ERROR
, -1, _("Pipe failed")));
723 if (content_string
[0] != '\0')
727 search
= mc_search_new (ptr
, -1);
730 search
->search_type
= MC_SEARCH_T_REGEX
;
731 search
->is_case_sensitive
= !case_insense
;
732 found
= mc_search_run (search
, content_string
+ content_shift
, 0, -1, NULL
);
733 mc_search_free (search
);
737 g_propagate_error (error
, g_error_new (MC_ERROR
, -1, _("Regular expression error")));
744 /* --------------------------------------------------------------------------------------------- */
745 /*** public functions ****************************************************************************/
746 /* --------------------------------------------------------------------------------------------- */
749 flush_extension_file (void)
755 /* --------------------------------------------------------------------------------------------- */
757 * The second argument is action, i.e. Open, View or Edit
759 * This function returns:
761 * -1 for a failure or user interrupt
762 * 0 if no command was run
763 * 1 if some command was run
765 * If action == "View" then a parameter is checked in the form of "View:%d",
766 * if the value for %d exists, then the viewer is started up at that line number.
770 regex_command (const vfs_path_t
* filename_vpath
, const char *action
)
772 char *filename
, *p
, *q
, *r
, c
;
774 gboolean found
= FALSE
;
775 gboolean error_flag
= FALSE
;
778 int view_at_line_number
;
779 char *include_target
;
780 int include_target_len
;
781 int have_type
= 0; /* Flag used by regex_check_type() */
783 if (filename_vpath
== NULL
)
786 /* Check for the special View:%d parameter */
787 if (strncmp (action
, "View:", 5) == 0)
789 view_at_line_number
= atoi (action
+ 5);
794 view_at_line_number
= 0;
799 char *extension_file
;
800 gboolean mc_user_ext
= TRUE
;
801 gboolean home_error
= FALSE
;
803 extension_file
= mc_config_get_full_path (MC_FILEBIND_FILE
);
804 if (!exist_file (extension_file
))
806 g_free (extension_file
);
808 extension_file
= mc_build_filename (mc_global
.sysconfig_dir
, MC_LIB_EXT
, NULL
);
809 if (!exist_file (extension_file
))
811 g_free (extension_file
);
812 extension_file
= mc_build_filename (mc_global
.share_data_dir
, MC_LIB_EXT
, NULL
);
817 g_file_get_contents (extension_file
, &data
, NULL
, NULL
);
818 g_free (extension_file
);
822 if (strstr (data
, "default/") == NULL
)
824 if (strstr (data
, "regex/") == NULL
&& strstr (data
, "shell/") == NULL
&&
825 strstr (data
, "type/") == NULL
)
834 title
= g_strdup_printf (_(" %s%s file error"),
835 mc_global
.sysconfig_dir
, MC_LIB_EXT
);
836 message (D_ERROR
, title
, _("The format of the %smc.ext "
837 "file has changed with version 3.0. It seems that "
838 "the installation failed. Please fetch a fresh "
839 "copy from the Midnight Commander package."),
840 mc_global
.sysconfig_dir
);
846 goto check_stock_mc_ext
;
852 char *filebind_filename
;
855 filebind_filename
= mc_config_get_full_path (MC_FILEBIND_FILE
);
856 title
= g_strdup_printf (_("%s file error"), filebind_filename
);
857 message (D_ERROR
, title
,
858 _("The format of the %s file has "
859 "changed with version 3.0. You may either want to copy "
860 "it from %smc.ext or use that file as an example of how to write it."),
861 filebind_filename
, mc_global
.sysconfig_dir
);
862 g_free (filebind_filename
);
867 mc_stat (filename_vpath
, &mystat
);
869 include_target
= NULL
;
870 include_target_len
= 0;
871 filename
= vfs_path_to_str (filename_vpath
);
872 file_len
= vfs_path_len (filename_vpath
);
874 for (p
= data
; *p
!= '\0'; p
++)
876 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++)
878 if (*q
== '\n' || *q
== '\0')
879 p
= q
; /* empty line */
880 if (*p
== '#') /* comment */
881 while (*p
!= '\0' && *p
!= '\n')
888 { /* i.e. starts in the first column, should be
891 gboolean case_insense
;
894 q
= strchr (p
, '\n');
896 q
= strchr (p
, '\0');
901 if ((strncmp (p
, "include/", 8) == 0)
902 && (strncmp (p
+ 8, include_target
, include_target_len
) == 0))
905 else if (strncmp (p
, "regex/", 6) == 0)
910 case_insense
= (strncmp (p
, "i/", 2) == 0);
914 search
= mc_search_new (p
, -1);
917 search
->search_type
= MC_SEARCH_T_REGEX
;
918 search
->is_case_sensitive
= !case_insense
;
919 found
= mc_search_run (search
, filename
, 0, file_len
, NULL
);
920 mc_search_free (search
);
923 else if (strncmp (p
, "directory/", 10) == 0)
925 if (S_ISDIR (mystat
.st_mode
) && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
928 else if (strncmp (p
, "shell/", 6) == 0)
930 int (*cmp_func
) (const char *s1
, const char *s2
, size_t n
) = strncmp
;
933 case_insense
= (strncmp (p
, "i/", 2) == 0);
937 cmp_func
= strncasecmp
;
940 if (*p
== '.' && file_len
>= (size_t) (q
- p
))
942 if (cmp_func (p
, filename
+ file_len
- (q
- p
), q
- p
) == 0)
947 if ((size_t) (q
- p
) == file_len
&& cmp_func (p
, filename
, q
- p
) == 0)
951 else if (strncmp (p
, "type/", 5) == 0)
953 GError
*error
= NULL
;
957 case_insense
= (strncmp (p
, "i/", 2) == 0);
961 found
= regex_check_type (filename_vpath
, p
, &have_type
, case_insense
, &error
);
964 g_error_free (error
);
965 error_flag
= TRUE
; /* leave it if file cannot be opened */
968 else if (strncmp (p
, "default/", 8) == 0)
977 { /* List of actions */
979 q
= strchr (p
, '\n');
981 q
= strchr (p
, '\0');
982 if (found
&& !error_flag
)
989 if (strcmp (p
, "Include") == 0)
993 include_target
= p
+ 8;
994 t
= strchr (include_target
, '\n');
997 include_target_len
= strlen (include_target
);
1010 if (strcmp (action
, p
) != 0)
1016 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++)
1019 /* Empty commands just stop searching
1020 * through, they don't do anything
1024 exec_extension (filename_vpath
, r
+ 1, view_at_line_number
);
1042 /* --------------------------------------------------------------------------------------------- */