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. */
40 #include "cons.saver.h"
43 /* If set, we execute the file command to check the file type */
44 int use_file_to_check_type
= 1;
46 /* This variable points to a copy of the mc.ext file in memory
47 * With this we avoid loading/parsing the file each time we
50 static char *data
= NULL
;
53 flush_extension_file (void)
59 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
62 exec_extension (const char *filename
, const char *data
, int *move_dir
,
69 int expand_prefix_found
= 0;
70 int parameter_found
= 0;
73 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
74 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
75 int written_nonspace
= 0;
79 char *localcopy
= NULL
;
81 time_t localmtime
= 0;
83 quote_func_t quote_func
= name_quote
;
85 g_return_if_fail (filename
!= NULL
);
86 g_return_if_fail (data
!= NULL
);
88 /* Avoid making a local copy if we are doing a cd */
89 if (!vfs_file_is_local (filename
))
95 * All commands should be run in /bin/sh regardless of user shell.
96 * To do that, create temporary shell script and run it.
97 * Sometimes it's not needed (e.g. for %cd and %view commands),
98 * but it's easier to create it anyway.
100 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
102 if (cmd_file_fd
== -1) {
103 message (1, MSG_ERROR
,
104 _(" Cannot create temporary command file \n %s "),
105 unix_error_string (errno
));
108 cmd_file
= fdopen (cmd_file_fd
, "w");
109 fputs ("#! /bin/sh\n", cmd_file
);
112 for (; *data
&& *data
!= '\n'; data
++) {
113 if (parameter_found
) {
117 parameter
= input_dialog (_(" Parameter "), prompt
, MC_HISTORY_EXT_PARAMETER
, "");
123 mc_ungetlocalcopy (filename
, localcopy
, 0);
129 fputs (parameter
, cmd_file
);
130 written_nonspace
= 1;
133 size_t len
= strlen (prompt
);
135 if (len
< sizeof (prompt
) - 1) {
140 } else if (expand_prefix_found
) {
141 expand_prefix_found
= 0;
145 int i
= check_format_view (data
);
151 } else if ((i
= check_format_cd (data
)) > 0) {
153 quote_func
= fake_name_quote
;
157 } else if ((i
= check_format_var (data
, &v
)) > 0 && v
) {
166 localcopy
= mc_getlocalcopy (filename
);
167 if (localcopy
== NULL
) {
173 mc_stat (localcopy
, &mystat
);
174 localmtime
= mystat
.st_mtime
;
175 text
= (*quote_func
) (localcopy
, 0);
177 text
= (*quote_func
) (filename
, 0);
180 text
= expand_format (NULL
, *data
, !is_cd
);
182 fputs (text
, cmd_file
);
188 written_nonspace
= 1;
193 expand_prefix_found
= 1;
195 if (*data
!= ' ' && *data
!= '\t')
196 written_nonspace
= 1;
200 fputc (*data
, cmd_file
);
206 * Make the script remove itself when it finishes.
207 * Don't do it for the viewer - it may need to rerun the script,
208 * so we clean up after calling view().
211 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
216 if ((run_view
&& !written_nonspace
) || is_cd
) {
221 /* Set executable flag on the command file ... */
222 chmod (file_name
, S_IRWXU
);
223 /* ... but don't rely on it - run /bin/sh explicitly */
224 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
228 altered_hex_mode
= 0;
229 altered_nroff_flag
= 0;
230 if (def_hex_mode
!= default_hex_mode
)
231 changed_hex_mode
= 1;
232 if (def_nroff_flag
!= default_nroff_flag
)
233 changed_nroff_flag
= 1;
235 /* If we've written whitespace only, then just load filename
238 if (written_nonspace
) {
239 mc_internal_viewer (cmd
, filename
, move_dir
, start_line
);
242 mc_internal_viewer (NULL
, filename
, move_dir
, start_line
);
244 if (changed_hex_mode
&& !altered_hex_mode
)
245 default_hex_mode
= def_hex_mode
;
246 if (changed_nroff_flag
&& !altered_nroff_flag
)
247 default_nroff_flag
= def_nroff_flag
;
253 /* while (*p == ' ' && *p == '\t')
256 /* Search last non-space character. Start search at the end in order
257 not to short filenames containing spaces. */
258 q
= p
+ strlen (p
) - 1;
259 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
262 do_cd (p
, cd_parse_command
);
264 shell_execute (cmd
, EXECUTE_INTERNAL
);
266 handle_console (CONSOLE_SAVE
);
267 if (output_lines
&& keybar_visible
) {
268 show_console_contents (output_start_y
,
269 LINES
- keybar_visible
-
271 LINES
- keybar_visible
- 1);
281 mc_stat (localcopy
, &mystat
);
282 mc_ungetlocalcopy (filename
, localcopy
,
283 localmtime
!= mystat
.st_mtime
);
289 # define FILE_CMD "file -L "
291 # define FILE_CMD "file "
295 * Run the "file" command on the local file.
296 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
299 get_file_type_local (const char *filename
, char *buf
, int buflen
)
303 char *tmp
= name_quote (filename
, 0);
304 char *command
= g_strconcat (FILE_CMD
, tmp
, " 2>/dev/null", (char *) 0);
305 FILE *f
= popen (command
, "r");
311 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0) {
316 read_bytes
= (fgets (buf
, buflen
, f
)
325 return (read_bytes
> 0);
330 * Invoke the "file" command on the file and match its output against PTR.
331 * have_type is a flag that is set if we already have tried to determine
332 * the type of that file.
333 * Return 1 for match, 0 for no match, -1 errors.
336 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
340 /* Following variables are valid if *have_type is 1 */
341 static char content_string
[2048];
342 static int content_shift
= 0;
343 static int got_data
= 0;
345 if (!use_file_to_check_type
) {
350 char *realname
; /* name used with "file" */
352 /* Don't repeate even unsuccessful checks */
355 localfile
= mc_getlocalcopy (filename
);
359 realname
= localfile
;
361 get_file_type_local (localfile
, content_string
,
362 sizeof (content_string
));
363 mc_ungetlocalcopy (filename
, localfile
, 0);
368 /* Paranoid termination */
369 content_string
[sizeof (content_string
) - 1] = 0;
371 if ((pp
= strchr (content_string
, '\n')) != 0)
374 if (!strncmp (content_string
, realname
, strlen (realname
))) {
375 /* Skip "realname: " */
376 content_shift
= strlen (realname
);
377 if (content_string
[content_shift
] == ':') {
378 /* Solaris' file prints tab(s) after ':' */
379 for (content_shift
++;
380 content_string
[content_shift
] == ' '
381 || content_string
[content_shift
] == '\t';
387 content_string
[0] = 0;
392 if (got_data
== -1) {
396 if (content_string
[0]
397 && regexp_match (ptr
, content_string
+ content_shift
, match_regex
)) {
405 /* The second argument is action, i.e. Open, View or Edit
407 * This function returns:
409 * -1 for a failure or user interrupt
410 * 0 if no command was run
411 * 1 if some command was run
413 * If action == "View" then a parameter is checked in the form of "View:%d",
414 * if the value for %d exists, then the viewer is started up at that line number.
417 regex_command (const char *filename
, const char *action
, int *move_dir
)
420 int file_len
= strlen (filename
);
425 int view_at_line_number
;
426 char *include_target
;
427 int include_target_len
;
428 int have_type
= 0; /* Flag used by regex_check_type() */
430 /* Check for the special View:%d parameter */
431 if (strncmp (action
, "View:", 5) == 0) {
432 view_at_line_number
= atoi (action
+ 5);
435 view_at_line_number
= 0;
439 char *extension_file
;
443 extension_file
= concat_dir_and_file (home_dir
, MC_USER_EXT
);
444 if (!exist_file (extension_file
)) {
445 g_free (extension_file
);
447 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
450 data
= load_file (extension_file
);
451 g_free (extension_file
);
455 if (!strstr (data
, "default/")) {
456 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
457 && !strstr (data
, "type/")) {
462 goto check_stock_mc_ext
;
465 g_strdup_printf (_(" %s%s file error"),
466 mc_home
, MC_LIB_EXT
);
467 message (1, title
, _("The format of the %smc.ext "
468 "file has changed with version 3.0. It seems that "
469 "the installation failed. Please fetch a fresh "
470 "copy from the Midnight Commander package."),
479 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT
);
480 message (1, title
, _("The format of the ~/%s file has "
481 "changed with version 3.0. You may either want to copy "
482 "it from %smc.ext or use that file as an example of how "
483 "to write it."), MC_USER_EXT
, mc_home
);
487 mc_stat (filename
, &mystat
);
489 include_target
= NULL
;
490 include_target_len
= 0;
491 for (p
= data
; *p
; p
++) {
492 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
493 if (*q
== '\n' || !*q
)
494 p
= q
; /* empty line */
495 if (*p
== '#') /* comment */
496 while (*p
&& *p
!= '\n')
502 if (p
== q
) { /* i.e. starts in the first column, should be
506 q
= strchr (p
, '\n');
511 if (include_target
) {
512 if ((strncmp (p
, "include/", 8) == 0)
513 && (strncmp (p
+ 8, include_target
, include_target_len
)
516 } else if (!strncmp (p
, "regex/", 6)) {
518 /* Do not transform shell patterns, you can use shell/ for
521 if (regexp_match (p
, filename
, match_regex
))
523 } else if (!strncmp (p
, "directory/", 10)) {
524 if (S_ISDIR (mystat
.st_mode
)
525 && regexp_match (p
+ 10, filename
, match_regex
))
527 } else if (!strncmp (p
, "shell/", 6)) {
529 if (*p
== '.' && file_len
>= (q
- p
)) {
530 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
533 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
536 } else if (!strncmp (p
, "type/", 5)) {
539 res
= regex_check_type (filename
, p
, &have_type
);
543 error_flag
= 1; /* leave it if file cannot be opened */
544 } else if (!strncmp (p
, "default/", 8)) {
551 } else { /* List of actions */
553 q
= strchr (p
, '\n');
556 if (found
&& !error_flag
) {
561 if (strcmp (p
, "Include") == 0) {
564 include_target
= p
+ 8;
565 t
= strchr (include_target
, '\n');
568 include_target_len
= strlen (include_target
);
580 if (!strcmp (action
, p
)) {
582 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
584 /* Empty commands just stop searching
585 * through, they don't do anything
587 * We need to copy the filename because exec_extension
588 * may end up invoking update_panels thus making the
589 * filename parameter invalid (ie, most of the time,
590 * we get filename as a pointer from current_panel->dir).
593 char *filename_copy
= g_strdup (filename
);
595 exec_extension (filename_copy
, r
+ 1, move_dir
,
596 view_at_line_number
);
597 g_free (filename_copy
);