1 /* Extension dependent execution.
2 Copyright (C) 1994, 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
39 #include "cons.saver.h"
42 /* If set, we execute the file command to check the file type */
43 int use_file_to_check_type
= 1;
45 /* This variable points to a copy of the mc.ext file in memory
46 * With this we avoid loading/parsing the file each time we
49 static char *data
= NULL
;
52 flush_extension_file (void)
58 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
61 exec_extension (const char *filename
, const char *data
, int *move_dir
,
68 int expand_prefix_found
= 0;
69 int parameter_found
= 0;
72 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
73 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
74 int written_nonspace
= 0;
78 char *localcopy
= NULL
;
80 time_t localmtime
= 0;
82 quote_func_t quote_func
= name_quote
;
84 g_return_if_fail (filename
!= NULL
);
85 g_return_if_fail (data
!= NULL
);
87 /* Avoid making a local copy if we are doing a cd */
88 if (!vfs_file_is_local (filename
))
94 * All commands should be run in /bin/sh regardless of user shell.
95 * To do that, create temporary shell script and run it.
96 * Sometimes it's not needed (e.g. for %cd and %view commands),
97 * but it's easier to create it anyway.
99 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
101 if (cmd_file_fd
== -1) {
102 message (1, MSG_ERROR
,
103 _(" Cannot create temporary command file \n %s "),
104 unix_error_string (errno
));
107 cmd_file
= fdopen (cmd_file_fd
, "w");
108 fputs ("#! /bin/sh\n", cmd_file
);
111 for (; *data
&& *data
!= '\n'; data
++) {
112 if (parameter_found
) {
116 parameter
= input_dialog (_(" Parameter "), prompt
, "");
122 mc_ungetlocalcopy (filename
, localcopy
, 0);
128 fputs (parameter
, cmd_file
);
129 written_nonspace
= 1;
132 size_t len
= strlen (prompt
);
134 if (len
< sizeof (prompt
) - 1) {
139 } else if (expand_prefix_found
) {
140 expand_prefix_found
= 0;
144 int i
= check_format_view (data
);
150 } else if ((i
= check_format_cd (data
)) > 0) {
152 quote_func
= fake_name_quote
;
156 } else if ((i
= check_format_var (data
, &v
)) > 0 && v
) {
165 localcopy
= mc_getlocalcopy (filename
);
166 if (localcopy
== NULL
) {
172 mc_stat (localcopy
, &mystat
);
173 localmtime
= mystat
.st_mtime
;
174 text
= (*quote_func
) (localcopy
, 0);
176 text
= (*quote_func
) (filename
, 0);
179 text
= expand_format (NULL
, *data
, !is_cd
);
181 fputs (text
, cmd_file
);
187 written_nonspace
= 1;
192 expand_prefix_found
= 1;
194 if (*data
!= ' ' && *data
!= '\t')
195 written_nonspace
= 1;
199 fputc (*data
, cmd_file
);
205 * Make the script remove itself when it finishes.
206 * Don't do it for the viewer - it may need to rerun the script,
207 * so we clean up after calling view().
210 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
215 if ((run_view
&& !written_nonspace
) || is_cd
) {
220 /* Set executable flag on the command file ... */
221 chmod (file_name
, S_IRWXU
);
222 /* ... but don't rely on it - run /bin/sh explicitly */
223 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
227 altered_hex_mode
= 0;
228 altered_nroff_flag
= 0;
229 if (def_hex_mode
!= default_hex_mode
)
230 changed_hex_mode
= 1;
231 if (def_nroff_flag
!= default_nroff_flag
)
232 changed_nroff_flag
= 1;
234 /* If we've written whitespace only, then just load filename
237 if (written_nonspace
) {
238 mc_internal_viewer (cmd
, filename
, move_dir
, start_line
);
241 mc_internal_viewer (NULL
, filename
, move_dir
, start_line
);
243 if (changed_hex_mode
&& !altered_hex_mode
)
244 default_hex_mode
= def_hex_mode
;
245 if (changed_nroff_flag
&& !altered_nroff_flag
)
246 default_nroff_flag
= def_nroff_flag
;
252 /* while (*p == ' ' && *p == '\t')
255 /* Search last non-space character. Start search at the end in order
256 not to short filenames containing spaces. */
257 q
= p
+ strlen (p
) - 1;
258 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
261 do_cd (p
, cd_parse_command
);
263 shell_execute (cmd
, EXECUTE_INTERNAL
);
265 handle_console (CONSOLE_SAVE
);
266 if (output_lines
&& keybar_visible
) {
267 show_console_contents (output_start_y
,
268 LINES
- keybar_visible
-
270 LINES
- keybar_visible
- 1);
280 mc_stat (localcopy
, &mystat
);
281 mc_ungetlocalcopy (filename
, localcopy
,
282 localmtime
!= mystat
.st_mtime
);
288 # define FILE_CMD "file -L "
290 # define FILE_CMD "file "
294 * Run the "file" command on the local file.
295 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
298 get_file_type_local (const char *filename
, char *buf
, int buflen
)
302 char *tmp
= name_quote (filename
, 0);
303 char *command
= g_strconcat (FILE_CMD
, tmp
, " 2>/dev/null", (char *) 0);
304 FILE *f
= popen (command
, "r");
310 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0) {
315 read_bytes
= (fgets (buf
, buflen
, f
)
324 return (read_bytes
> 0);
329 * Invoke the "file" command on the file and match its output against PTR.
330 * have_type is a flag that is set if we already have tried to determine
331 * the type of that file.
332 * Return 1 for match, 0 for no match, -1 errors.
335 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
339 /* Following variables are valid if *have_type is 1 */
340 static char content_string
[2048];
341 static int content_shift
= 0;
342 static int got_data
= 0;
344 if (!use_file_to_check_type
) {
349 char *realname
; /* name used with "file" */
351 /* Don't repeate even unsuccessful checks */
354 localfile
= mc_getlocalcopy (filename
);
358 realname
= localfile
;
360 get_file_type_local (localfile
, content_string
,
361 sizeof (content_string
));
362 mc_ungetlocalcopy (filename
, localfile
, 0);
367 /* Paranoid termination */
368 content_string
[sizeof (content_string
) - 1] = 0;
370 if ((pp
= strchr (content_string
, '\n')) != 0)
373 if (!strncmp (content_string
, realname
, strlen (realname
))) {
374 /* Skip "realname: " */
375 content_shift
= strlen (realname
);
376 if (content_string
[content_shift
] == ':') {
377 /* Solaris' file prints tab(s) after ':' */
378 for (content_shift
++;
379 content_string
[content_shift
] == ' '
380 || content_string
[content_shift
] == '\t';
386 content_string
[0] = 0;
391 if (got_data
== -1) {
395 if (content_string
[0]
396 && regexp_match (ptr
, content_string
+ content_shift
, match_regex
)) {
404 /* The second argument is action, i.e. Open, View or Edit
406 * This function returns:
408 * -1 for a failure or user interrupt
409 * 0 if no command was run
410 * 1 if some command was run
412 * If action == "View" then a parameter is checked in the form of "View:%d",
413 * if the value for %d exists, then the viewer is started up at that line number.
416 regex_command (const char *filename
, const char *action
, int *move_dir
)
419 int file_len
= strlen (filename
);
424 int view_at_line_number
;
425 char *include_target
;
426 int include_target_len
;
427 int have_type
= 0; /* Flag used by regex_check_type() */
429 /* Check for the special View:%d parameter */
430 if (strncmp (action
, "View:", 5) == 0) {
431 view_at_line_number
= atoi (action
+ 5);
434 view_at_line_number
= 0;
438 char *extension_file
;
442 extension_file
= concat_dir_and_file (home_dir
, MC_USER_EXT
);
443 if (!exist_file (extension_file
)) {
444 g_free (extension_file
);
446 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
449 data
= load_file (extension_file
);
450 g_free (extension_file
);
454 if (!strstr (data
, "default/")) {
455 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
456 && !strstr (data
, "type/")) {
461 goto check_stock_mc_ext
;
464 g_strdup_printf (_(" %s%s file error"),
465 mc_home
, MC_LIB_EXT
);
466 message (1, title
, _("The format of the %smc.ext "
467 "file has changed with version 3.0. It seems that "
468 "the installation failed. Please fetch a fresh "
469 "copy from the Midnight Commander package."),
478 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT
);
479 message (1, title
, _("The format of the ~/%s file has "
480 "changed with version 3.0. You may either want to copy "
481 "it from %smc.ext or use that file as an example of how "
482 "to write it."), MC_USER_EXT
, mc_home
);
486 mc_stat (filename
, &mystat
);
488 include_target
= NULL
;
489 include_target_len
= 0;
490 for (p
= data
; *p
; p
++) {
491 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
492 if (*q
== '\n' || !*q
)
493 p
= q
; /* empty line */
494 if (*p
== '#') /* comment */
495 while (*p
&& *p
!= '\n')
501 if (p
== q
) { /* i.e. starts in the first column, should be
505 q
= strchr (p
, '\n');
510 if (include_target
) {
511 if ((strncmp (p
, "include/", 8) == 0)
512 && (strncmp (p
+ 8, include_target
, include_target_len
)
515 } else if (!strncmp (p
, "regex/", 6)) {
517 /* Do not transform shell patterns, you can use shell/ for
520 if (regexp_match (p
, filename
, match_regex
))
522 } else if (!strncmp (p
, "directory/", 10)) {
523 if (S_ISDIR (mystat
.st_mode
)
524 && regexp_match (p
+ 10, filename
, match_regex
))
526 } else if (!strncmp (p
, "shell/", 6)) {
528 if (*p
== '.' && file_len
>= (q
- p
)) {
529 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
532 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
535 } else if (!strncmp (p
, "type/", 5)) {
538 res
= regex_check_type (filename
, p
, &have_type
);
542 error_flag
= 1; /* leave it if file cannot be opened */
543 } else if (!strncmp (p
, "default/", 8)) {
550 } else { /* List of actions */
552 q
= strchr (p
, '\n');
555 if (found
&& !error_flag
) {
560 if (strcmp (p
, "Include") == 0) {
563 include_target
= p
+ 8;
564 t
= strchr (include_target
, '\n');
567 include_target_len
= strlen (include_target
);
579 if (!strcmp (action
, p
)) {
581 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
583 /* Empty commands just stop searching
584 * through, they don't do anything
586 * We need to copy the filename because exec_extension
587 * may end up invoking update_panels thus making the
588 * filename parameter invalid (ie, most of the time,
589 * we get filename as a pointer from current_panel->dir).
592 char *filename_copy
= g_strdup (filename
);
594 exec_extension (filename_copy
, r
+ 1, move_dir
,
595 view_at_line_number
);
596 g_free (filename_copy
);