1 /* Extension dependent execution.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 Free Software Foundation, Inc.
5 Written by: 1995 Jakub Jelinek
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 * \brief Source: extension dependent execution
35 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/search.h"
38 #include "lib/fileloc.h"
39 #include "lib/mcconfig.h"
41 #include "lib/vfs/mc-vfs/vfs.h"
42 #include "lib/widget.h"
43 #include "lib/charsets.h" /* get_codepage_index */
45 #include "src/setup.h" /* use_file_to_check_type */
46 #include "src/execute.h"
47 #include "src/history.h"
49 #include "src/consaver/cons.saver.h"
50 #include "src/viewer/mcviewer.h"
53 #include "src/selcodepage.h" /* do_set_codepage */
61 /*** global variables ****************************************************************************/
63 /*** file scope macro definitions ****************************************************************/
66 #define FILE_CMD "file -L "
68 #define FILE_CMD "file "
71 /*** file scope type declarations ****************************************************************/
73 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
75 /*** file scope variables ************************************************************************/
77 /* This variable points to a copy of the mc.ext file in memory
78 * With this we avoid loading/parsing the file each time we
81 static char *data
= NULL
;
83 /*** file scope functions ************************************************************************/
84 /* --------------------------------------------------------------------------------------------- */
87 exec_extension (const char *filename
, const char *lc_data
, int *move_dir
, int start_line
)
94 int expand_prefix_found
= 0;
95 int parameter_found
= 0;
98 int def_hex_mode
= mcview_default_hex_mode
, changed_hex_mode
= 0;
99 int def_nroff_flag
= mcview_default_nroff_flag
, changed_nroff_flag
= 0;
100 int written_nonspace
= 0;
104 char *localcopy
= NULL
;
106 time_t localmtime
= 0;
108 quote_func_t quote_func
= name_quote
;
110 g_return_if_fail (filename
!= NULL
);
111 g_return_if_fail (lc_data
!= NULL
);
113 /* Avoid making a local copy if we are doing a cd */
114 if (!vfs_file_is_local (filename
))
120 * All commands should be run in /bin/sh regardless of user shell.
121 * To do that, create temporary shell script and run it.
122 * Sometimes it's not needed (e.g. for %cd and %view commands),
123 * but it's easier to create it anyway.
125 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
127 if (cmd_file_fd
== -1)
129 message (D_ERROR
, MSG_ERROR
,
130 _("Cannot create temporary command file\n%s"), unix_error_string (errno
));
134 cmd_file
= fdopen (cmd_file_fd
, "w");
135 fputs ("#! /bin/sh\n", cmd_file
);
138 for (; *lc_data
!= '\0' && *lc_data
!= '\n'; lc_data
++)
147 parameter
= input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "");
148 if (parameter
== NULL
)
155 mc_ungetlocalcopy (filename
, localcopy
, 0);
161 fputs (parameter
, cmd_file
);
162 written_nonspace
= 1;
167 size_t len
= strlen (lc_prompt
);
169 if (len
< sizeof (lc_prompt
) - 1)
171 lc_prompt
[len
] = *lc_data
;
172 lc_prompt
[len
+ 1] = '\0';
176 else if (expand_prefix_found
)
178 expand_prefix_found
= 0;
186 i
= check_format_view (lc_data
);
194 i
= check_format_cd (lc_data
);
198 quote_func
= fake_name_quote
;
205 i
= check_format_var (lc_data
, &v
);
206 if (i
> 0 && v
!= NULL
)
217 text
= expand_format (NULL
, *lc_data
, !is_cd
);
222 localcopy
= mc_getlocalcopy (filename
);
223 if (localcopy
== NULL
)
230 mc_stat (localcopy
, &mystat
);
231 localmtime
= mystat
.st_mtime
;
232 text
= quote_func (localcopy
, 0);
236 fn
= vfs_canon_and_translate (filename
);
237 text
= quote_func (fn
, 0);
243 fputs (text
, cmd_file
);
251 written_nonspace
= 1;
257 else if (*lc_data
== '%')
258 expand_prefix_found
= 1;
261 if (*lc_data
!= ' ' && *lc_data
!= '\t')
262 written_nonspace
= 1;
266 fputc (*lc_data
, cmd_file
);
271 * Make the script remove itself when it finishes.
272 * Don't do it for the viewer - it may need to rerun the script,
273 * so we clean up after calling view().
276 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
280 if ((run_view
&& !written_nonspace
) || is_cd
)
288 /* Set executable flag on the command file ... */
289 chmod (file_name
, S_IRWXU
);
290 /* ... but don't rely on it - run /bin/sh explicitly */
291 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
298 mcview_altered_hex_mode
= 0;
299 mcview_altered_nroff_flag
= 0;
300 if (def_hex_mode
!= mcview_default_hex_mode
)
301 changed_hex_mode
= 1;
302 if (def_nroff_flag
!= mcview_default_nroff_flag
)
303 changed_nroff_flag
= 1;
305 /* If we've written whitespace only, then just load filename
308 if (written_nonspace
)
310 ret
= mcview_viewer (cmd
, filename
, start_line
);
314 ret
= mcview_viewer (NULL
, filename
, start_line
);
316 if (move_dir
!= NULL
)
319 case MCVIEW_WANT_NEXT
:
322 case MCVIEW_WANT_PREV
:
329 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
330 mcview_default_hex_mode
= def_hex_mode
;
331 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
332 mcview_default_nroff_flag
= def_nroff_flag
;
334 dialog_switch_process_pending ();
341 /* while (*p == ' ' && *p == '\t')
344 /* Search last non-space character. Start search at the end in order
345 not to short filenames containing spaces. */
346 q
= p
+ strlen (p
) - 1;
347 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
350 do_cd (p
, cd_parse_command
);
354 shell_execute (cmd
, EXECUTE_INTERNAL
);
357 handle_console (CONSOLE_SAVE
);
358 if (output_lines
&& keybar_visible
)
359 show_console_contents (output_start_y
,
360 LINES
- keybar_visible
-
361 output_lines
- 1, LINES
- keybar_visible
- 1);
370 mc_stat (localcopy
, &mystat
);
371 mc_ungetlocalcopy (filename
, localcopy
, localmtime
!= mystat
.st_mtime
);
376 /* --------------------------------------------------------------------------------------------- */
378 * Run cmd_file with args, put result into buf.
379 * If error, put '\0' into buf[0]
380 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
382 * NOTES: buf is null-terminated string.
386 get_popen_information (const char *cmd_file
, const char *args
, char *buf
, int buflen
)
388 gboolean read_bytes
= FALSE
;
392 command
= g_strconcat (cmd_file
, args
, " 2>/dev/null", (char *) NULL
);
393 f
= popen (command
, "r");
399 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0)
405 read_bytes
= (fgets (buf
, buflen
, f
) != NULL
);
407 buf
[0] = '\0'; /* Paranoid termination */
412 buf
[0] = '\0'; /* Paranoid termination */
416 buf
[buflen
- 1] = '\0';
418 return read_bytes
? 1 : 0;
421 /* --------------------------------------------------------------------------------------------- */
423 * Run the "file" command on the local file.
424 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
428 get_file_type_local (const char *filename
, char *buf
, int buflen
)
433 tmp
= name_quote (filename
, 0);
434 ret
= get_popen_information (FILE_CMD
, tmp
, buf
, buflen
);
440 /* --------------------------------------------------------------------------------------------- */
442 * Run the "enca" command on the local file.
443 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
448 get_file_encoding_local (const char *filename
, char *buf
, int buflen
)
450 char *tmp
, *lang
, *args
;
453 tmp
= name_quote (filename
, 0);
454 lang
= name_quote (autodetect_codeset
, 0);
455 args
= g_strconcat (" -L", lang
, " -i ", tmp
, (char *) NULL
);
457 ret
= get_popen_information ("enca", args
, buf
, buflen
);
465 #endif /* HAVE_CHARSET */
467 /* --------------------------------------------------------------------------------------------- */
469 * Invoke the "file" command on the file and match its output against PTR.
470 * have_type is a flag that is set if we already have tried to determine
471 * the type of that file.
472 * Return 1 for match, 0 for no match, -1 errors.
476 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
480 /* Following variables are valid if *have_type is 1 */
481 static char content_string
[2048];
482 static char encoding_id
[21]; /* CSISO51INISCYRILLIC -- 20 */
483 static size_t content_shift
= 0;
484 static int got_data
= 0;
486 if (!use_file_to_check_type
)
491 char *realname
; /* name used with "file" */
495 int got_encoding_data
;
496 #endif /* HAVE_CHARSET */
498 /* Don't repeate even unsuccessful checks */
501 localfile
= mc_getlocalcopy (filename
);
502 if (localfile
== NULL
)
505 realname
= localfile
;
508 got_encoding_data
= is_autodetect_codeset_enabled
509 ? get_file_encoding_local (localfile
, encoding_id
, sizeof (encoding_id
)) : 0;
511 if (got_encoding_data
> 0)
516 pp
= strchr (encoding_id
, '\n');
520 cp_id
= get_codepage_index (encoding_id
);
522 cp_id
= default_source_codepage
;
524 do_set_codepage (cp_id
);
526 #endif /* HAVE_CHARSET */
528 mc_ungetlocalcopy (filename
, localfile
, 0);
530 got_data
= get_file_type_local (localfile
, content_string
, sizeof (content_string
));
537 pp
= strchr (content_string
, '\n');
541 real_len
= strlen (realname
);
543 if (strncmp (content_string
, realname
, real_len
) == 0)
545 /* Skip "realname: " */
546 content_shift
= real_len
;
547 if (content_string
[content_shift
] == ':')
549 /* Solaris' file prints tab(s) after ':' */
550 for (content_shift
++;
551 content_string
[content_shift
] == ' '
552 || content_string
[content_shift
] == '\t'; content_shift
++)
560 content_string
[0] = '\0';
568 if (content_string
[0] != '\0'
569 && mc_search (ptr
, content_string
+ content_shift
, MC_SEARCH_T_REGEX
))
577 /* --------------------------------------------------------------------------------------------- */
578 /*** public functions ****************************************************************************/
579 /* --------------------------------------------------------------------------------------------- */
582 flush_extension_file (void)
588 /* --------------------------------------------------------------------------------------------- */
590 * The second argument is action, i.e. Open, View or Edit
592 * This function returns:
594 * -1 for a failure or user interrupt
595 * 0 if no command was run
596 * 1 if some command was run
598 * If action == "View" then a parameter is checked in the form of "View:%d",
599 * if the value for %d exists, then the viewer is started up at that line number.
603 regex_command (const char *filename
, const char *action
, int *move_dir
)
606 int file_len
= strlen (filename
);
611 int view_at_line_number
;
612 char *include_target
;
613 int include_target_len
;
614 int have_type
= 0; /* Flag used by regex_check_type() */
616 /* Check for the special View:%d parameter */
617 if (strncmp (action
, "View:", 5) == 0)
619 view_at_line_number
= atoi (action
+ 5);
624 view_at_line_number
= 0;
629 char *extension_file
;
633 extension_file
= g_build_filename (mc_config_get_data_path (), MC_FILEBIND_FILE
, NULL
);
634 if (!exist_file (extension_file
))
636 g_free (extension_file
);
638 extension_file
= concat_dir_and_file (mc_sysconfig_dir
, MC_LIB_EXT
);
639 if (!exist_file (extension_file
))
641 g_free (extension_file
);
642 extension_file
= concat_dir_and_file (mc_share_data_dir
, MC_LIB_EXT
);
647 g_file_get_contents (extension_file
, &data
, NULL
, NULL
);
648 g_free (extension_file
);
652 if (!strstr (data
, "default/"))
654 if (!strstr (data
, "regex/") && !strstr (data
, "shell/") && !strstr (data
, "type/"))
661 goto check_stock_mc_ext
;
665 char *title
= g_strdup_printf (_(" %s%s file error"),
666 mc_sysconfig_dir
, MC_LIB_EXT
);
667 message (D_ERROR
, title
, _("The format of the %smc.ext "
668 "file has changed with version 3.0. It seems that "
669 "the installation failed. Please fetch a fresh "
670 "copy from the Midnight Commander package."),
679 char *title
= g_strdup_printf (_("%s%s%s file error"),
680 mc_config_get_data_path (), PATH_SEP_STR
,
682 message (D_ERROR
, title
,
683 _("The format of the %s%s%s file has "
684 "changed with version 3.0. You may either want to copy "
685 "it from %smc.ext or use that file as an example of how to write it."),
686 mc_config_get_data_path (), PATH_SEP_STR
, MC_FILEBIND_FILE
, mc_sysconfig_dir
);
690 mc_stat (filename
, &mystat
);
692 include_target
= NULL
;
693 include_target_len
= 0;
694 for (p
= data
; *p
; p
++)
696 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
697 if (*q
== '\n' || !*q
)
698 p
= q
; /* empty line */
699 if (*p
== '#') /* comment */
700 while (*p
&& *p
!= '\n')
707 { /* i.e. starts in the first column, should be
711 q
= strchr (p
, '\n');
718 if ((strncmp (p
, "include/", 8) == 0)
719 && (strncmp (p
+ 8, include_target
, include_target_len
) == 0))
722 else if (!strncmp (p
, "regex/", 6))
725 /* Do not transform shell patterns, you can use shell/ for
728 if (mc_search (p
, filename
, MC_SEARCH_T_REGEX
))
731 else if (!strncmp (p
, "directory/", 10))
733 if (S_ISDIR (mystat
.st_mode
) && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
736 else if (!strncmp (p
, "shell/", 6))
739 if (*p
== '.' && file_len
>= (q
- p
))
741 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
746 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
750 else if (!strncmp (p
, "type/", 5))
754 res
= regex_check_type (filename
, p
, &have_type
);
758 error_flag
= 1; /* leave it if file cannot be opened */
760 else if (!strncmp (p
, "default/", 8))
770 { /* List of actions */
772 q
= strchr (p
, '\n');
775 if (found
&& !error_flag
)
782 if (strcmp (p
, "Include") == 0)
786 include_target
= p
+ 8;
787 t
= strchr (include_target
, '\n');
790 include_target_len
= strlen (include_target
);
802 if (!strcmp (action
, p
))
805 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
807 /* Empty commands just stop searching
808 * through, they don't do anything
810 * We need to copy the filename because exec_extension
811 * may end up invoking update_panels thus making the
812 * filename parameter invalid (ie, most of the time,
813 * we get filename as a pointer from current_panel->dir).
817 char *filename_copy
= g_strdup (filename
);
819 exec_extension (filename_copy
, r
+ 1, move_dir
, view_at_line_number
);
820 g_free (filename_copy
);
840 /* --------------------------------------------------------------------------------------------- */