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
36 #include "../src/tty/tty.h"
43 #include "cons.saver.h"
45 #include "../src/search/search.h"
46 #include "../src/viewer/mcviewer.h"
48 #include "../vfs/vfs.h"
50 /* If set, we execute the file command to check the file type */
51 int use_file_to_check_type
= 1;
53 /* This variable points to a copy of the mc.ext file in memory
54 * With this we avoid loading/parsing the file each time we
57 static char *data
= NULL
;
60 flush_extension_file (void)
66 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
69 exec_extension (const char *filename
, const char *lc_data
, int *move_dir
,
77 int expand_prefix_found
= 0;
78 int parameter_found
= 0;
81 int def_hex_mode
= mcview_default_hex_mode
, changed_hex_mode
= 0;
82 int def_nroff_flag
= mcview_default_nroff_flag
, changed_nroff_flag
= 0;
83 int written_nonspace
= 0;
87 char *localcopy
= NULL
;
89 time_t localmtime
= 0;
91 quote_func_t quote_func
= name_quote
;
93 g_return_if_fail (filename
!= NULL
);
94 g_return_if_fail (lc_data
!= NULL
);
96 /* Avoid making a local copy if we are doing a cd */
97 if (!vfs_file_is_local (filename
))
103 * All commands should be run in /bin/sh regardless of user shell.
104 * To do that, create temporary shell script and run it.
105 * Sometimes it's not needed (e.g. for %cd and %view commands),
106 * but it's easier to create it anyway.
108 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
110 if (cmd_file_fd
== -1) {
111 message (D_ERROR
, MSG_ERROR
,
112 _(" Cannot create temporary command file \n %s "),
113 unix_error_string (errno
));
116 cmd_file
= fdopen (cmd_file_fd
, "w");
117 fputs ("#! /bin/sh\n", cmd_file
);
120 for (; *lc_data
&& *lc_data
!= '\n'; lc_data
++) {
121 if (parameter_found
) {
122 if (*lc_data
== '}') {
125 parameter
= input_dialog (_(" Parameter "), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "");
131 mc_ungetlocalcopy (filename
, localcopy
, 0);
137 fputs (parameter
, cmd_file
);
138 written_nonspace
= 1;
141 size_t len
= strlen (lc_prompt
);
143 if (len
< sizeof (lc_prompt
) - 1) {
144 lc_prompt
[len
] = *lc_data
;
145 lc_prompt
[len
+ 1] = 0;
148 } else if (expand_prefix_found
) {
149 expand_prefix_found
= 0;
153 int i
= check_format_view (lc_data
);
159 } else if ((i
= check_format_cd (lc_data
)) > 0) {
161 quote_func
= fake_name_quote
;
165 } else if ((i
= check_format_var (lc_data
, &v
)) > 0 && v
) {
172 if (*lc_data
== 'f') {
174 localcopy
= mc_getlocalcopy (filename
);
175 if (localcopy
== NULL
) {
181 mc_stat (localcopy
, &mystat
);
182 localmtime
= mystat
.st_mtime
;
183 text
= (*quote_func
) (localcopy
, 0);
185 fn
= vfs_canon_and_translate (filename
);
186 text
= (*quote_func
) (fn
, 0);
190 text
= expand_format (NULL
, *lc_data
, !is_cd
);
193 fputs (text
, cmd_file
);
199 written_nonspace
= 1;
204 expand_prefix_found
= 1;
206 if (*lc_data
!= ' ' && *lc_data
!= '\t')
207 written_nonspace
= 1;
211 fputc (*lc_data
, cmd_file
);
217 * Make the script remove itself when it finishes.
218 * Don't do it for the viewer - it may need to rerun the script,
219 * so we clean up after calling view().
222 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
227 if ((run_view
&& !written_nonspace
) || is_cd
) {
232 /* Set executable flag on the command file ... */
233 chmod (file_name
, S_IRWXU
);
234 /* ... but don't rely on it - run /bin/sh explicitly */
235 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
239 mcview_altered_hex_mode
= 0;
240 mcview_altered_nroff_flag
= 0;
241 if (def_hex_mode
!= mcview_default_hex_mode
)
242 changed_hex_mode
= 1;
243 if (def_nroff_flag
!= mcview_default_nroff_flag
)
244 changed_nroff_flag
= 1;
246 /* If we've written whitespace only, then just load filename
249 if (written_nonspace
) {
250 mcview_viewer (cmd
, filename
, move_dir
, start_line
);
253 mcview_viewer (NULL
, filename
, move_dir
, start_line
);
255 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
256 mcview_default_hex_mode
= def_hex_mode
;
257 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
258 mcview_default_nroff_flag
= def_nroff_flag
;
264 /* while (*p == ' ' && *p == '\t')
267 /* Search last non-space character. Start search at the end in order
268 not to short filenames containing spaces. */
269 q
= p
+ strlen (p
) - 1;
270 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
273 do_cd (p
, cd_parse_command
);
275 shell_execute (cmd
, EXECUTE_INTERNAL
);
277 handle_console (CONSOLE_SAVE
);
278 if (output_lines
&& keybar_visible
) {
279 show_console_contents (output_start_y
,
280 LINES
- keybar_visible
-
282 LINES
- keybar_visible
- 1);
292 mc_stat (localcopy
, &mystat
);
293 mc_ungetlocalcopy (filename
, localcopy
,
294 localmtime
!= mystat
.st_mtime
);
300 # define FILE_CMD "file -L "
302 # define FILE_CMD "file "
306 * Run the "file" command on the local file.
307 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
310 get_file_type_local (const char *filename
, char *buf
, int buflen
)
314 char *tmp
= name_quote (filename
, 0);
315 char *command
= g_strconcat (FILE_CMD
, tmp
, " 2>/dev/null", (char *) NULL
);
316 FILE *f
= popen (command
, "r");
322 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0) {
327 read_bytes
= (fgets (buf
, buflen
, f
)
336 return (read_bytes
> 0);
341 * Invoke the "file" command on the file and match its output against PTR.
342 * have_type is a flag that is set if we already have tried to determine
343 * the type of that file.
344 * Return 1 for match, 0 for no match, -1 errors.
347 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
351 /* Following variables are valid if *have_type is 1 */
352 static char content_string
[2048];
353 static int content_shift
= 0;
354 static int got_data
= 0;
356 if (!use_file_to_check_type
) {
361 char *realname
; /* name used with "file" */
363 /* Don't repeate even unsuccessful checks */
366 localfile
= mc_getlocalcopy (filename
);
370 realname
= localfile
;
372 get_file_type_local (localfile
, content_string
,
373 sizeof (content_string
));
374 mc_ungetlocalcopy (filename
, localfile
, 0);
379 /* Paranoid termination */
380 content_string
[sizeof (content_string
) - 1] = 0;
382 if ((pp
= strchr (content_string
, '\n')) != 0)
385 if (!strncmp (content_string
, realname
, strlen (realname
))) {
386 /* Skip "realname: " */
387 content_shift
= strlen (realname
);
388 if (content_string
[content_shift
] == ':') {
389 /* Solaris' file prints tab(s) after ':' */
390 for (content_shift
++;
391 content_string
[content_shift
] == ' '
392 || content_string
[content_shift
] == '\t';
398 content_string
[0] = 0;
403 if (got_data
== -1) {
407 if (content_string
[0]
408 && mc_search (ptr
, content_string
+ content_shift
, MC_SEARCH_T_REGEX
)) {
416 /* The second argument is action, i.e. Open, View or Edit
418 * This function returns:
420 * -1 for a failure or user interrupt
421 * 0 if no command was run
422 * 1 if some command was run
424 * If action == "View" then a parameter is checked in the form of "View:%d",
425 * if the value for %d exists, then the viewer is started up at that line number.
428 regex_command (const char *filename
, const char *action
, int *move_dir
)
431 int file_len
= strlen (filename
);
436 int view_at_line_number
;
437 char *include_target
;
438 int include_target_len
;
439 int have_type
= 0; /* Flag used by regex_check_type() */
441 /* Check for the special View:%d parameter */
442 if (strncmp (action
, "View:", 5) == 0) {
443 view_at_line_number
= atoi (action
+ 5);
446 view_at_line_number
= 0;
450 char *extension_file
;
454 extension_file
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_FILEBIND_FILE
, NULL
);
455 if (!exist_file (extension_file
)) {
456 g_free (extension_file
);
458 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
459 if (!exist_file (extension_file
)) {
460 g_free (extension_file
);
461 extension_file
= concat_dir_and_file (mc_home_alt
, MC_LIB_EXT
);
465 data
= load_file (extension_file
);
466 g_free (extension_file
);
470 if (!strstr (data
, "default/")) {
471 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
472 && !strstr (data
, "type/")) {
477 goto check_stock_mc_ext
;
480 g_strdup_printf (_(" %s%s file error"),
481 mc_home
, MC_LIB_EXT
);
482 message (D_ERROR
, title
, _("The format of the %smc.ext "
483 "file has changed with version 3.0. It seems that "
484 "the installation failed. Please fetch a fresh "
485 "copy from the Midnight Commander package."),
494 g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
);
495 message (D_ERROR
, title
, _("The format of the ~/%s file has "
496 "changed with version 3.0. You may either want to copy "
497 "it from %smc.ext or use that file as an example of how "
498 "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
, mc_home
);
502 mc_stat (filename
, &mystat
);
504 include_target
= NULL
;
505 include_target_len
= 0;
506 for (p
= data
; *p
; p
++) {
507 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
508 if (*q
== '\n' || !*q
)
509 p
= q
; /* empty line */
510 if (*p
== '#') /* comment */
511 while (*p
&& *p
!= '\n')
517 if (p
== q
) { /* i.e. starts in the first column, should be
521 q
= strchr (p
, '\n');
526 if (include_target
) {
527 if ((strncmp (p
, "include/", 8) == 0)
528 && (strncmp (p
+ 8, include_target
, include_target_len
)
531 } else if (!strncmp (p
, "regex/", 6)) {
533 /* Do not transform shell patterns, you can use shell/ for
536 if (mc_search (p
, filename
, MC_SEARCH_T_REGEX
))
538 } else if (!strncmp (p
, "directory/", 10)) {
539 if (S_ISDIR (mystat
.st_mode
)
540 && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
542 } else if (!strncmp (p
, "shell/", 6)) {
544 if (*p
== '.' && file_len
>= (q
- p
)) {
545 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
548 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
551 } else if (!strncmp (p
, "type/", 5)) {
554 res
= regex_check_type (filename
, p
, &have_type
);
558 error_flag
= 1; /* leave it if file cannot be opened */
559 } else if (!strncmp (p
, "default/", 8)) {
566 } else { /* List of actions */
568 q
= strchr (p
, '\n');
571 if (found
&& !error_flag
) {
576 if (strcmp (p
, "Include") == 0) {
579 include_target
= p
+ 8;
580 t
= strchr (include_target
, '\n');
583 include_target_len
= strlen (include_target
);
595 if (!strcmp (action
, p
)) {
597 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
599 /* Empty commands just stop searching
600 * through, they don't do anything
602 * We need to copy the filename because exec_extension
603 * may end up invoking update_panels thus making the
604 * filename parameter invalid (ie, most of the time,
605 * we get filename as a pointer from current_panel->dir).
608 char *filename_copy
= g_strdup (filename
);
610 exec_extension (filename_copy
, r
+ 1, move_dir
,
611 view_at_line_number
);
612 g_free (filename_copy
);