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"
56 #include "src/main.h" /* do_cd */
58 #include "src/consaver/cons.saver.h"
59 #include "src/viewer/mcviewer.h"
62 #include "src/selcodepage.h" /* do_set_codepage */
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 /* --------------------------------------------------------------------------------------------- */
147 exec_get_export_variables (const vfs_path_t
* filename_vpath
)
150 GString
*export_vars_string
;
158 } export_variables
[] = {
159 {'p', "MC_EXT_BASENAME"},
160 {'d', "MC_EXT_CURRENTDIR"},
161 {'s', "MC_EXT_SELECTED"},
162 {'t', "MC_EXT_ONLYTAGGED"},
167 text
= exec_get_file_name (filename_vpath
);
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
);
175 for (i
= 0; export_variables
[i
].name
!= NULL
; i
++)
177 text
= expand_format (NULL
, export_variables
[i
].symbol
, TRUE
);
180 g_string_append_printf (export_vars_string
,
181 "%s=%s\nexport %s\n", export_variables
[i
].name
, text
,
182 export_variables
[i
].name
);
186 return g_string_free (export_vars_string
, FALSE
);
189 /* --------------------------------------------------------------------------------------------- */
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
++)
209 parameter_found
= FALSE
;
210 parameter
= input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "");
211 if (parameter
== NULL
)
214 g_string_free (shell_string
, TRUE
);
215 exec_cleanup_file_name (filename_vpath
, FALSE
);
218 g_string_append (shell_string
, parameter
);
219 written_nonspace
= TRUE
;
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
;
237 parameter_found
= TRUE
;
243 i
= check_format_view (lc_data
);
251 i
= check_format_cd (lc_data
);
255 quote_func
= fake_name_quote
;
256 do_local_copy
= FALSE
;
262 i
= check_format_var (lc_data
, &v
);
263 if (i
> 0 && v
!= NULL
)
265 g_string_append (shell_string
, v
);
274 text
= expand_format (NULL
, *lc_data
, !is_cd
);
277 text
= exec_get_file_name (filename_vpath
);
280 g_string_free (shell_string
, TRUE
);
286 g_string_append (shell_string
, text
);
289 strcpy (pbuffer
, text
);
290 pbuffer
= strchr (pbuffer
, 0);
294 written_nonspace
= TRUE
;
300 else if (*lc_data
== '%')
301 expand_prefix_found
= TRUE
;
304 if (*lc_data
!= ' ' && *lc_data
!= '\t')
305 written_nonspace
= TRUE
;
307 *(pbuffer
++) = *lc_data
;
309 g_string_append_c (shell_string
, *lc_data
);
312 return g_string_free (shell_string
, FALSE
);
315 /* --------------------------------------------------------------------------------------------- */
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
334 if (written_nonspace
)
336 mcview_viewer (cmd
, filename_vpath
, start_line
);
337 mc_unlink (temp_file_name_vpath
);
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 /* --------------------------------------------------------------------------------------------- */
353 exec_extension_cd (void)
360 /* while (*p == ' ' && *p == '\t')
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'))
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 /* --------------------------------------------------------------------------------------------- */
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
;
387 g_return_if_fail (lc_data
!= NULL
);
391 quote_func
= name_quote
;
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
)
406 exec_extension_cd ();
407 g_free (shell_string
);
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
));
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().
448 file_name
= vfs_path_to_str (temp_file_name_vpath
);
449 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
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
;
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
);
474 exec_extension_view (cmd
, filename_vpath
, start_line
, temp_file_name_vpath
);
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);
490 exec_cleanup_file_name (filename_vpath
, TRUE
);
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.
505 get_popen_information (const char *cmd_file
, const char *args
, char *buf
, int buflen
)
507 gboolean read_bytes
= FALSE
;
511 command
= g_strconcat (cmd_file
, args
, " 2>/dev/null", (char *) NULL
);
512 f
= popen (command
, "r");
518 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0)
524 read_bytes
= (fgets (buf
, buflen
, f
) != NULL
);
526 buf
[0] = '\0'; /* Paranoid termination */
531 buf
[0] = '\0'; /* Paranoid termination */
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.
547 get_file_type_local (const vfs_path_t
* filename_vpath
, char *buf
, int buflen
)
552 tmp
= name_quote (vfs_path_get_last_path_str (filename_vpath
), 0);
553 ret
= get_popen_information (FILE_CMD
, tmp
, buf
, buflen
);
559 /* --------------------------------------------------------------------------------------------- */
561 * Run the "enca" command on the local file.
562 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
567 get_file_encoding_local (const vfs_path_t
* filename_vpath
, char *buf
, int buflen
)
569 char *tmp
, *lang
, *args
;
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
);
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.
595 regex_check_type (const vfs_path_t
* filename_vpath
, const char *ptr
, int *have_type
)
599 /* Following variables are valid if *have_type is 1 */
600 static char content_string
[2048];
602 static char encoding_id
[21]; /* CSISO51INISCYRILLIC -- 20 */
604 static size_t content_shift
= 0;
605 static int got_data
= 0;
607 if (!use_file_to_check_type
)
612 vfs_path_t
*localfile_vpath
;
613 const char *realname
; /* name used with "file" */
616 int got_encoding_data
;
617 #endif /* HAVE_CHARSET */
619 /* Don't repeate even unsuccessful checks */
622 localfile_vpath
= mc_getlocalcopy (filename_vpath
);
623 if (localfile_vpath
== NULL
)
626 realname
= vfs_path_get_last_path_str (localfile_vpath
);
629 got_encoding_data
= is_autodetect_codeset_enabled
630 ? get_file_encoding_local (localfile_vpath
, encoding_id
, sizeof (encoding_id
)) : 0;
632 if (got_encoding_data
> 0)
637 pp
= strchr (encoding_id
, '\n');
641 cp_id
= get_codepage_index (encoding_id
);
643 cp_id
= default_source_codepage
;
645 do_set_codepage (cp_id
);
647 #endif /* HAVE_CHARSET */
649 mc_ungetlocalcopy (filename_vpath
, localfile_vpath
, FALSE
);
651 got_data
= get_file_type_local (localfile_vpath
, content_string
, sizeof (content_string
));
658 pp
= strchr (content_string
, '\n');
662 real_len
= strlen (realname
);
664 if (strncmp (content_string
, realname
, real_len
) == 0)
666 /* Skip "realname: " */
667 content_shift
= real_len
;
668 if (content_string
[content_shift
] == ':')
670 /* Solaris' file prints tab(s) after ':' */
671 for (content_shift
++;
672 content_string
[content_shift
] == ' '
673 || content_string
[content_shift
] == '\t'; content_shift
++)
681 content_string
[0] = '\0';
683 vfs_path_free (localfile_vpath
);
689 if (content_string
[0] != '\0'
690 && mc_search (ptr
, content_string
+ content_shift
, MC_SEARCH_T_REGEX
))
698 /* --------------------------------------------------------------------------------------------- */
699 /*** public functions ****************************************************************************/
700 /* --------------------------------------------------------------------------------------------- */
703 flush_extension_file (void)
709 /* --------------------------------------------------------------------------------------------- */
711 * The second argument is action, i.e. Open, View or Edit
713 * This function returns:
715 * -1 for a failure or user interrupt
716 * 0 if no command was run
717 * 1 if some command was run
719 * If action == "View" then a parameter is checked in the form of "View:%d",
720 * if the value for %d exists, then the viewer is started up at that line number.
724 regex_command (const vfs_path_t
* filename_vpath
, const char *action
)
726 char *filename
, *p
, *q
, *r
, c
;
732 int view_at_line_number
;
733 char *include_target
;
734 int include_target_len
;
735 int have_type
= 0; /* Flag used by regex_check_type() */
737 if (filename_vpath
== NULL
)
740 /* Check for the special View:%d parameter */
741 if (strncmp (action
, "View:", 5) == 0)
743 view_at_line_number
= atoi (action
+ 5);
748 view_at_line_number
= 0;
753 char *extension_file
;
757 extension_file
= mc_config_get_full_path (MC_FILEBIND_FILE
);
758 if (!exist_file (extension_file
))
760 g_free (extension_file
);
762 extension_file
= mc_build_filename (mc_global
.sysconfig_dir
, MC_LIB_EXT
, NULL
);
763 if (!exist_file (extension_file
))
765 g_free (extension_file
);
766 extension_file
= mc_build_filename (mc_global
.share_data_dir
, MC_LIB_EXT
, NULL
);
771 g_file_get_contents (extension_file
, &data
, NULL
, NULL
);
772 g_free (extension_file
);
776 if (!strstr (data
, "default/"))
778 if (!strstr (data
, "regex/") && !strstr (data
, "shell/") && !strstr (data
, "type/"))
785 goto check_stock_mc_ext
;
789 char *title
= g_strdup_printf (_(" %s%s file error"),
790 mc_global
.sysconfig_dir
, MC_LIB_EXT
);
791 message (D_ERROR
, title
, _("The format of the %smc.ext "
792 "file has changed with version 3.0. It seems that "
793 "the installation failed. Please fetch a fresh "
794 "copy from the Midnight Commander package."),
795 mc_global
.sysconfig_dir
);
803 char *filebind_filename
= mc_config_get_full_path (MC_FILEBIND_FILE
);
804 char *title
= g_strdup_printf (_("%s file error"), filebind_filename
);
805 message (D_ERROR
, title
,
806 _("The format of the %s file has "
807 "changed with version 3.0. You may either want to copy "
808 "it from %smc.ext or use that file as an example of how to write it."),
809 filebind_filename
, mc_global
.sysconfig_dir
);
810 g_free (filebind_filename
);
815 mc_stat (filename_vpath
, &mystat
);
817 include_target
= NULL
;
818 include_target_len
= 0;
819 filename
= vfs_path_to_str (filename_vpath
);
820 file_len
= vfs_path_len (filename_vpath
);
822 for (p
= data
; *p
; p
++)
824 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
825 if (*q
== '\n' || !*q
)
826 p
= q
; /* empty line */
827 if (*p
== '#') /* comment */
828 while (*p
&& *p
!= '\n')
835 { /* i.e. starts in the first column, should be
839 q
= strchr (p
, '\n');
846 if ((strncmp (p
, "include/", 8) == 0)
847 && (strncmp (p
+ 8, include_target
, include_target_len
) == 0))
850 else if (!strncmp (p
, "regex/", 6))
853 /* Do not transform shell patterns, you can use shell/ for
856 if (mc_search (p
, filename
, MC_SEARCH_T_REGEX
))
859 else if (!strncmp (p
, "directory/", 10))
861 if (S_ISDIR (mystat
.st_mode
) && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
864 else if (!strncmp (p
, "shell/", 6))
867 if (*p
== '.' && file_len
>= (size_t) (q
- p
))
869 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
874 if ((size_t) (q
- p
) == file_len
&& !strncmp (p
, filename
, q
- p
))
878 else if (!strncmp (p
, "type/", 5))
882 res
= regex_check_type (filename_vpath
, p
, &have_type
);
886 error_flag
= 1; /* leave it if file cannot be opened */
888 else if (!strncmp (p
, "default/", 8))
898 { /* List of actions */
900 q
= strchr (p
, '\n');
903 if (found
&& !error_flag
)
910 if (strcmp (p
, "Include") == 0)
914 include_target
= p
+ 8;
915 t
= strchr (include_target
, '\n');
918 include_target_len
= strlen (include_target
);
931 if (strcmp (action
, p
) != 0)
937 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++)
940 /* Empty commands just stop searching
941 * through, they don't do anything
945 exec_extension (filename_vpath
, r
+ 1, view_at_line_number
);
963 /* --------------------------------------------------------------------------------------------- */