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"
40 #include "consaver/cons.saver.h"
41 #include "viewer/mcviewer.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
53 /* If set, we execute the file command to check the file type */
54 int use_file_to_check_type
= 1;
56 /* This variable points to a copy of the mc.ext file in memory
57 * With this we avoid loading/parsing the file each time we
60 static char *data
= NULL
;
63 flush_extension_file (void)
69 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
72 exec_extension (const char *filename
, const char *lc_data
, int *move_dir
,
80 int expand_prefix_found
= 0;
81 int parameter_found
= 0;
84 int def_hex_mode
= mcview_default_hex_mode
, changed_hex_mode
= 0;
85 int def_nroff_flag
= mcview_default_nroff_flag
, changed_nroff_flag
= 0;
86 int written_nonspace
= 0;
90 char *localcopy
= NULL
;
92 time_t localmtime
= 0;
94 quote_func_t quote_func
= name_quote
;
96 g_return_if_fail (filename
!= NULL
);
97 g_return_if_fail (lc_data
!= NULL
);
99 /* Avoid making a local copy if we are doing a cd */
100 if (!vfs_file_is_local (filename
))
106 * All commands should be run in /bin/sh regardless of user shell.
107 * To do that, create temporary shell script and run it.
108 * Sometimes it's not needed (e.g. for %cd and %view commands),
109 * but it's easier to create it anyway.
111 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
113 if (cmd_file_fd
== -1) {
114 message (D_ERROR
, MSG_ERROR
,
115 _(" Cannot create temporary command file \n %s "),
116 unix_error_string (errno
));
119 cmd_file
= fdopen (cmd_file_fd
, "w");
120 fputs ("#! /bin/sh\n", cmd_file
);
123 for (; *lc_data
&& *lc_data
!= '\n'; lc_data
++) {
124 if (parameter_found
) {
125 if (*lc_data
== '}') {
128 parameter
= input_dialog (_(" Parameter "), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "");
134 mc_ungetlocalcopy (filename
, localcopy
, 0);
140 fputs (parameter
, cmd_file
);
141 written_nonspace
= 1;
144 size_t len
= strlen (lc_prompt
);
146 if (len
< sizeof (lc_prompt
) - 1) {
147 lc_prompt
[len
] = *lc_data
;
148 lc_prompt
[len
+ 1] = 0;
151 } else if (expand_prefix_found
) {
152 expand_prefix_found
= 0;
156 int i
= check_format_view (lc_data
);
162 } else if ((i
= check_format_cd (lc_data
)) > 0) {
164 quote_func
= fake_name_quote
;
168 } else if ((i
= check_format_var (lc_data
, &v
)) > 0 && v
) {
175 if (*lc_data
== 'f') {
177 localcopy
= mc_getlocalcopy (filename
);
178 if (localcopy
== NULL
) {
184 mc_stat (localcopy
, &mystat
);
185 localmtime
= mystat
.st_mtime
;
186 text
= (*quote_func
) (localcopy
, 0);
188 fn
= vfs_canon_and_translate (filename
);
189 text
= (*quote_func
) (fn
, 0);
193 text
= expand_format (NULL
, *lc_data
, !is_cd
);
196 fputs (text
, cmd_file
);
202 written_nonspace
= 1;
207 expand_prefix_found
= 1;
209 if (*lc_data
!= ' ' && *lc_data
!= '\t')
210 written_nonspace
= 1;
214 fputc (*lc_data
, cmd_file
);
220 * Make the script remove itself when it finishes.
221 * Don't do it for the viewer - it may need to rerun the script,
222 * so we clean up after calling view().
225 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
230 if ((run_view
&& !written_nonspace
) || is_cd
) {
235 /* Set executable flag on the command file ... */
236 chmod (file_name
, S_IRWXU
);
237 /* ... but don't rely on it - run /bin/sh explicitly */
238 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
242 mcview_altered_hex_mode
= 0;
243 mcview_altered_nroff_flag
= 0;
244 if (def_hex_mode
!= mcview_default_hex_mode
)
245 changed_hex_mode
= 1;
246 if (def_nroff_flag
!= mcview_default_nroff_flag
)
247 changed_nroff_flag
= 1;
249 /* If we've written whitespace only, then just load filename
252 if (written_nonspace
) {
253 mcview_viewer (cmd
, filename
, move_dir
, start_line
);
256 mcview_viewer (NULL
, filename
, move_dir
, start_line
);
258 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
259 mcview_default_hex_mode
= def_hex_mode
;
260 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
261 mcview_default_nroff_flag
= def_nroff_flag
;
267 /* while (*p == ' ' && *p == '\t')
270 /* Search last non-space character. Start search at the end in order
271 not to short filenames containing spaces. */
272 q
= p
+ strlen (p
) - 1;
273 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
276 do_cd (p
, cd_parse_command
);
278 shell_execute (cmd
, EXECUTE_INTERNAL
);
280 handle_console (CONSOLE_SAVE
);
281 if (output_lines
&& keybar_visible
) {
282 show_console_contents (output_start_y
,
283 LINES
- keybar_visible
-
285 LINES
- keybar_visible
- 1);
295 mc_stat (localcopy
, &mystat
);
296 mc_ungetlocalcopy (filename
, localcopy
,
297 localmtime
!= mystat
.st_mtime
);
303 # define FILE_CMD "file -L "
305 # define FILE_CMD "file "
309 * Run the "file" command on the local file.
310 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
313 get_file_type_local (const char *filename
, char *buf
, int buflen
)
317 char *tmp
= name_quote (filename
, 0);
318 char *command
= g_strconcat (FILE_CMD
, tmp
, " 2>/dev/null", (char *) NULL
);
319 FILE *f
= popen (command
, "r");
325 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0) {
330 read_bytes
= (fgets (buf
, buflen
, f
)
339 return (read_bytes
> 0);
344 * Invoke the "file" command on the file and match its output against PTR.
345 * have_type is a flag that is set if we already have tried to determine
346 * the type of that file.
347 * Return 1 for match, 0 for no match, -1 errors.
350 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
354 /* Following variables are valid if *have_type is 1 */
355 static char content_string
[2048];
356 static int content_shift
= 0;
357 static int got_data
= 0;
359 if (!use_file_to_check_type
) {
364 char *realname
; /* name used with "file" */
366 /* Don't repeate even unsuccessful checks */
369 localfile
= mc_getlocalcopy (filename
);
373 realname
= localfile
;
375 get_file_type_local (localfile
, content_string
,
376 sizeof (content_string
));
377 mc_ungetlocalcopy (filename
, localfile
, 0);
382 /* Paranoid termination */
383 content_string
[sizeof (content_string
) - 1] = 0;
385 if ((pp
= strchr (content_string
, '\n')) != 0)
388 if (!strncmp (content_string
, realname
, strlen (realname
))) {
389 /* Skip "realname: " */
390 content_shift
= strlen (realname
);
391 if (content_string
[content_shift
] == ':') {
392 /* Solaris' file prints tab(s) after ':' */
393 for (content_shift
++;
394 content_string
[content_shift
] == ' '
395 || content_string
[content_shift
] == '\t';
401 content_string
[0] = 0;
406 if (got_data
== -1) {
410 if (content_string
[0]
411 && mc_search (ptr
, content_string
+ content_shift
, MC_SEARCH_T_REGEX
)) {
419 /* The second argument is action, i.e. Open, View or Edit
421 * This function returns:
423 * -1 for a failure or user interrupt
424 * 0 if no command was run
425 * 1 if some command was run
427 * If action == "View" then a parameter is checked in the form of "View:%d",
428 * if the value for %d exists, then the viewer is started up at that line number.
431 regex_command (const char *filename
, const char *action
, int *move_dir
)
434 int file_len
= strlen (filename
);
439 int view_at_line_number
;
440 char *include_target
;
441 int include_target_len
;
442 int have_type
= 0; /* Flag used by regex_check_type() */
444 /* Check for the special View:%d parameter */
445 if (strncmp (action
, "View:", 5) == 0) {
446 view_at_line_number
= atoi (action
+ 5);
449 view_at_line_number
= 0;
453 char *extension_file
;
457 extension_file
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_FILEBIND_FILE
, NULL
);
458 if (!exist_file (extension_file
)) {
459 g_free (extension_file
);
461 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
462 if (!exist_file (extension_file
)) {
463 g_free (extension_file
);
464 extension_file
= concat_dir_and_file (mc_home_alt
, MC_LIB_EXT
);
468 data
= load_file (extension_file
);
469 g_free (extension_file
);
473 if (!strstr (data
, "default/")) {
474 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
475 && !strstr (data
, "type/")) {
480 goto check_stock_mc_ext
;
483 g_strdup_printf (_(" %s%s file error"),
484 mc_home
, MC_LIB_EXT
);
485 message (D_ERROR
, title
, _("The format of the %smc.ext "
486 "file has changed with version 3.0. It seems that "
487 "the installation failed. Please fetch a fresh "
488 "copy from the Midnight Commander package."),
497 g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
);
498 message (D_ERROR
, title
, _("The format of the ~/%s file has "
499 "changed with version 3.0. You may either want to copy "
500 "it from %smc.ext or use that file as an example of how "
501 "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
, mc_home
);
505 mc_stat (filename
, &mystat
);
507 include_target
= NULL
;
508 include_target_len
= 0;
509 for (p
= data
; *p
; p
++) {
510 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
511 if (*q
== '\n' || !*q
)
512 p
= q
; /* empty line */
513 if (*p
== '#') /* comment */
514 while (*p
&& *p
!= '\n')
520 if (p
== q
) { /* i.e. starts in the first column, should be
524 q
= strchr (p
, '\n');
529 if (include_target
) {
530 if ((strncmp (p
, "include/", 8) == 0)
531 && (strncmp (p
+ 8, include_target
, include_target_len
)
534 } else if (!strncmp (p
, "regex/", 6)) {
536 /* Do not transform shell patterns, you can use shell/ for
539 if (mc_search (p
, filename
, MC_SEARCH_T_REGEX
))
541 } else if (!strncmp (p
, "directory/", 10)) {
542 if (S_ISDIR (mystat
.st_mode
)
543 && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
545 } else if (!strncmp (p
, "shell/", 6)) {
547 if (*p
== '.' && file_len
>= (q
- p
)) {
548 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
551 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
554 } else if (!strncmp (p
, "type/", 5)) {
557 res
= regex_check_type (filename
, p
, &have_type
);
561 error_flag
= 1; /* leave it if file cannot be opened */
562 } else if (!strncmp (p
, "default/", 8)) {
569 } else { /* List of actions */
571 q
= strchr (p
, '\n');
574 if (found
&& !error_flag
) {
579 if (strcmp (p
, "Include") == 0) {
582 include_target
= p
+ 8;
583 t
= strchr (include_target
, '\n');
586 include_target_len
= strlen (include_target
);
598 if (!strcmp (action
, p
)) {
600 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
602 /* Empty commands just stop searching
603 * through, they don't do anything
605 * We need to copy the filename because exec_extension
606 * may end up invoking update_panels thus making the
607 * filename parameter invalid (ie, most of the time,
608 * we get filename as a pointer from current_panel->dir).
611 char *filename_copy
= g_strdup (filename
);
613 exec_extension (filename_copy
, r
+ 1, move_dir
,
614 view_at_line_number
);
615 g_free (filename_copy
);