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. */
30 #include <mhl/string.h>
41 #include "cons.saver.h"
44 /* If set, we execute the file command to check the file type */
45 int use_file_to_check_type
= 1;
47 /* This variable points to a copy of the mc.ext file in memory
48 * With this we avoid loading/parsing the file each time we
51 static char *data
= NULL
;
54 flush_extension_file (void)
60 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
63 exec_extension (const char *filename
, const char *data
, int *move_dir
,
70 int expand_prefix_found
= 0;
71 int parameter_found
= 0;
74 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
75 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
76 int written_nonspace
= 0;
80 char *localcopy
= NULL
;
82 time_t localmtime
= 0;
84 quote_func_t quote_func
= name_quote
;
86 g_return_if_fail (filename
!= NULL
);
87 g_return_if_fail (data
!= NULL
);
89 /* Avoid making a local copy if we are doing a cd */
90 if (!vfs_file_is_local (filename
))
96 * All commands should be run in /bin/sh regardless of user shell.
97 * To do that, create temporary shell script and run it.
98 * Sometimes it's not needed (e.g. for %cd and %view commands),
99 * but it's easier to create it anyway.
101 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
103 if (cmd_file_fd
== -1) {
104 message (1, MSG_ERROR
,
105 _(" Cannot create temporary command file \n %s "),
106 unix_error_string (errno
));
109 cmd_file
= fdopen (cmd_file_fd
, "w");
110 fputs ("#! /bin/sh\n", cmd_file
);
113 for (; *data
&& *data
!= '\n'; data
++) {
114 if (parameter_found
) {
118 parameter
= input_dialog (_(" Parameter "), prompt
, MC_HISTORY_EXT_PARAMETER
, "");
124 mc_ungetlocalcopy (filename
, localcopy
, 0);
130 fputs (parameter
, cmd_file
);
131 written_nonspace
= 1;
134 size_t len
= strlen (prompt
);
136 if (len
< sizeof (prompt
) - 1) {
141 } else if (expand_prefix_found
) {
142 expand_prefix_found
= 0;
146 int i
= check_format_view (data
);
152 } else if ((i
= check_format_cd (data
)) > 0) {
154 quote_func
= fake_name_quote
;
158 } else if ((i
= check_format_var (data
, &v
)) > 0 && v
) {
167 localcopy
= mc_getlocalcopy (filename
);
168 if (localcopy
== NULL
) {
174 mc_stat (localcopy
, &mystat
);
175 localmtime
= mystat
.st_mtime
;
176 text
= (*quote_func
) (localcopy
, 0);
178 text
= (*quote_func
) (filename
, 0);
181 text
= expand_format (NULL
, *data
, !is_cd
);
183 fputs (text
, cmd_file
);
189 written_nonspace
= 1;
194 expand_prefix_found
= 1;
196 if (*data
!= ' ' && *data
!= '\t')
197 written_nonspace
= 1;
201 fputc (*data
, cmd_file
);
207 * Make the script remove itself when it finishes.
208 * Don't do it for the viewer - it may need to rerun the script,
209 * so we clean up after calling view().
212 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
217 if ((run_view
&& !written_nonspace
) || is_cd
) {
222 /* Set executable flag on the command file ... */
223 chmod (file_name
, S_IRWXU
);
224 /* ... but don't rely on it - run /bin/sh explicitly */
225 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
229 altered_hex_mode
= 0;
230 altered_nroff_flag
= 0;
231 if (def_hex_mode
!= default_hex_mode
)
232 changed_hex_mode
= 1;
233 if (def_nroff_flag
!= default_nroff_flag
)
234 changed_nroff_flag
= 1;
236 /* If we've written whitespace only, then just load filename
239 if (written_nonspace
) {
240 mc_internal_viewer (cmd
, filename
, move_dir
, start_line
);
243 mc_internal_viewer (NULL
, filename
, move_dir
, start_line
);
245 if (changed_hex_mode
&& !altered_hex_mode
)
246 default_hex_mode
= def_hex_mode
;
247 if (changed_nroff_flag
&& !altered_nroff_flag
)
248 default_nroff_flag
= def_nroff_flag
;
254 /* while (*p == ' ' && *p == '\t')
257 /* Search last non-space character. Start search at the end in order
258 not to short filenames containing spaces. */
259 q
= p
+ strlen (p
) - 1;
260 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
263 do_cd (p
, cd_parse_command
);
265 shell_execute (cmd
, EXECUTE_INTERNAL
);
267 handle_console (CONSOLE_SAVE
);
268 if (output_lines
&& keybar_visible
) {
269 show_console_contents (output_start_y
,
270 LINES
- keybar_visible
-
272 LINES
- keybar_visible
- 1);
282 mc_stat (localcopy
, &mystat
);
283 mc_ungetlocalcopy (filename
, localcopy
,
284 localmtime
!= mystat
.st_mtime
);
290 # define FILE_CMD "file -L "
292 # define FILE_CMD "file "
296 * Run the "file" command on the local file.
297 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
300 get_file_type_local (const char *filename
, char *buf
, int buflen
)
304 char *tmp
= name_quote (filename
, 0);
305 char *command
= g_strconcat (FILE_CMD
, tmp
, " 2>/dev/null", (char *) 0);
306 FILE *f
= popen (command
, "r");
312 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0) {
317 read_bytes
= (fgets (buf
, buflen
, f
)
326 return (read_bytes
> 0);
331 * Invoke the "file" command on the file and match its output against PTR.
332 * have_type is a flag that is set if we already have tried to determine
333 * the type of that file.
334 * Return 1 for match, 0 for no match, -1 errors.
337 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
341 /* Following variables are valid if *have_type is 1 */
342 static char content_string
[2048];
343 static int content_shift
= 0;
344 static int got_data
= 0;
346 if (!use_file_to_check_type
) {
351 char *realname
; /* name used with "file" */
353 /* Don't repeate even unsuccessful checks */
356 localfile
= mc_getlocalcopy (filename
);
360 realname
= localfile
;
362 get_file_type_local (localfile
, content_string
,
363 sizeof (content_string
));
364 mc_ungetlocalcopy (filename
, localfile
, 0);
369 /* Paranoid termination */
370 content_string
[sizeof (content_string
) - 1] = 0;
372 if ((pp
= strchr (content_string
, '\n')) != 0)
375 if (!strncmp (content_string
, realname
, strlen (realname
))) {
376 /* Skip "realname: " */
377 content_shift
= strlen (realname
);
378 if (content_string
[content_shift
] == ':') {
379 /* Solaris' file prints tab(s) after ':' */
380 for (content_shift
++;
381 content_string
[content_shift
] == ' '
382 || content_string
[content_shift
] == '\t';
388 content_string
[0] = 0;
393 if (got_data
== -1) {
397 if (content_string
[0]
398 && regexp_match (ptr
, content_string
+ content_shift
, match_regex
)) {
406 /* The second argument is action, i.e. Open, View or Edit
408 * This function returns:
410 * -1 for a failure or user interrupt
411 * 0 if no command was run
412 * 1 if some command was run
414 * If action == "View" then a parameter is checked in the form of "View:%d",
415 * if the value for %d exists, then the viewer is started up at that line number.
418 regex_command (const char *filename
, const char *action
, int *move_dir
)
421 int file_len
= strlen (filename
);
426 int view_at_line_number
;
427 char *include_target
;
428 int include_target_len
;
429 int have_type
= 0; /* Flag used by regex_check_type() */
431 /* Check for the special View:%d parameter */
432 if (strncmp (action
, "View:", 5) == 0) {
433 view_at_line_number
= atoi (action
+ 5);
436 view_at_line_number
= 0;
440 char *extension_file
;
444 extension_file
= mhl_str_dir_plus_file (home_dir
, MC_USER_EXT
);
445 if (!exist_file (extension_file
)) {
446 g_free (extension_file
);
448 extension_file
= mhl_str_dir_plus_file (mc_home
, MC_LIB_EXT
);
451 data
= load_file (extension_file
);
452 g_free (extension_file
);
456 if (!strstr (data
, "default/")) {
457 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
458 && !strstr (data
, "type/")) {
463 goto check_stock_mc_ext
;
466 g_strdup_printf (_(" %s%s file error"),
467 mc_home
, MC_LIB_EXT
);
468 message (1, title
, _("The format of the %smc.ext "
469 "file has changed with version 3.0. It seems that "
470 "the installation failed. Please fetch a fresh "
471 "copy from the Midnight Commander package."),
480 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT
);
481 message (1, title
, _("The format of the ~/%s file has "
482 "changed with version 3.0. You may either want to copy "
483 "it from %smc.ext or use that file as an example of how "
484 "to write it."), MC_USER_EXT
, mc_home
);
488 mc_stat (filename
, &mystat
);
490 include_target
= NULL
;
491 include_target_len
= 0;
492 for (p
= data
; *p
; p
++) {
493 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
494 if (*q
== '\n' || !*q
)
495 p
= q
; /* empty line */
496 if (*p
== '#') /* comment */
497 while (*p
&& *p
!= '\n')
503 if (p
== q
) { /* i.e. starts in the first column, should be
507 q
= strchr (p
, '\n');
512 if (include_target
) {
513 if ((strncmp (p
, "include/", 8) == 0)
514 && (strncmp (p
+ 8, include_target
, include_target_len
)
517 } else if (!strncmp (p
, "regex/", 6)) {
519 /* Do not transform shell patterns, you can use shell/ for
522 if (regexp_match (p
, filename
, match_regex
))
524 } else if (!strncmp (p
, "directory/", 10)) {
525 if (S_ISDIR (mystat
.st_mode
)
526 && regexp_match (p
+ 10, filename
, match_regex
))
528 } else if (!strncmp (p
, "shell/", 6)) {
530 if (*p
== '.' && file_len
>= (q
- p
)) {
531 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
534 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
537 } else if (!strncmp (p
, "type/", 5)) {
540 res
= regex_check_type (filename
, p
, &have_type
);
544 error_flag
= 1; /* leave it if file cannot be opened */
545 } else if (!strncmp (p
, "default/", 8)) {
552 } else { /* List of actions */
554 q
= strchr (p
, '\n');
557 if (found
&& !error_flag
) {
562 if (strcmp (p
, "Include") == 0) {
565 include_target
= p
+ 8;
566 t
= strchr (include_target
, '\n');
569 include_target_len
= strlen (include_target
);
581 if (!strcmp (action
, p
)) {
583 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
585 /* Empty commands just stop searching
586 * through, they don't do anything
588 * We need to copy the filename because exec_extension
589 * may end up invoking update_panels thus making the
590 * filename parameter invalid (ie, most of the time,
591 * we get filename as a pointer from current_panel->dir).
594 char *filename_copy
= g_strdup (filename
);
596 exec_extension (filename_copy
, r
+ 1, move_dir
,
597 view_at_line_number
);
598 g_free (filename_copy
);