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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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)
61 typedef char *(*quote_func_t
) (const char *name
, int i
);
64 exec_extension (const char *filename
, const char *data
, int *move_dir
,
71 int expand_prefix_found
= 0;
72 int parameter_found
= 0;
75 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
76 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
77 int written_nonspace
= 0;
81 char *localcopy
= NULL
;
83 time_t localmtime
= 0;
85 quote_func_t quote_func
= name_quote
;
87 g_return_if_fail (filename
!= NULL
);
88 g_return_if_fail (data
!= NULL
);
90 /* Avoid making a local copy if we are doing a cd */
91 if (!vfs_file_is_local (filename
))
97 * All commands should be run in /bin/sh regardless of user shell.
98 * To do that, create temporary shell script and run it.
99 * Sometimes it's not needed (e.g. for %cd and %view commands),
100 * but it's easier to create it anyway.
102 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
104 if (cmd_file_fd
== -1) {
105 message (1, MSG_ERROR
,
106 _(" Cannot create temporary command file \n %s "),
107 unix_error_string (errno
));
110 cmd_file
= fdopen (cmd_file_fd
, "w");
111 fputs ("#! /bin/sh\n", cmd_file
);
114 for (; *data
&& *data
!= '\n'; data
++) {
115 if (parameter_found
) {
119 parameter
= input_dialog (_(" Parameter "), prompt
, "");
125 mc_ungetlocalcopy (filename
, localcopy
, 0);
130 fputs (parameter
, cmd_file
);
131 written_nonspace
= 1;
134 int 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 int cmd_len
= strlen (file_name
) + 10;
224 /* Set executable flag on the command file ... */
225 chmod (file_name
, S_IRWXU
);
226 /* ... but don't rely on it - run /bin/sh explicitly */
227 cmd
= g_malloc (cmd_len
);
228 g_snprintf (cmd
, cmd_len
, "/bin/sh %s", file_name
);
232 altered_hex_mode
= 0;
233 altered_nroff_flag
= 0;
234 if (def_hex_mode
!= default_hex_mode
)
235 changed_hex_mode
= 1;
236 if (def_nroff_flag
!= default_nroff_flag
)
237 changed_nroff_flag
= 1;
239 /* If we've written whitespace only, then just load filename
242 if (written_nonspace
) {
243 view (cmd
, filename
, move_dir
, start_line
);
246 view (0, filename
, move_dir
, start_line
);
248 if (changed_hex_mode
&& !altered_hex_mode
)
249 default_hex_mode
= def_hex_mode
;
250 if (changed_nroff_flag
&& !altered_nroff_flag
)
251 default_nroff_flag
= def_nroff_flag
;
257 /* while (*p == ' ' && *p == '\t')
260 /* Search last non-space character. Start search at the end in order
261 not to short filenames containing spaces. */
262 q
= p
+ strlen (p
) - 1;
263 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
266 do_cd (p
, cd_parse_command
);
268 shell_execute (cmd
, EXECUTE_INTERNAL
);
270 handle_console (CONSOLE_SAVE
);
271 if (output_lines
&& keybar_visible
) {
272 show_console_contents (output_start_y
,
273 LINES
- keybar_visible
-
275 LINES
- keybar_visible
- 1);
285 mc_stat (localcopy
, &mystat
);
286 mc_ungetlocalcopy (filename
, localcopy
,
287 localmtime
!= mystat
.st_mtime
);
292 # define FILE_CMD "file -L "
294 # define FILE_CMD "file "
298 * Run the "file" command on the local file.
299 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
302 get_file_type_local (char *filename
, char *buf
, int buflen
)
306 char *tmp
= name_quote (filename
, 0);
307 char *command
= g_strconcat (FILE_CMD
, tmp
, NULL
);
308 FILE *f
= popen (command
, "r");
313 read_bytes
= (fgets (buf
, buflen
- 1, f
)
320 ** SCO 3.2 does has a buggy pclose(), so
321 ** <command> become zombie (alex)
323 waitpid (-1, NULL
, WNOHANG
);
324 #endif /* SCO_FLAVOR */
329 return (read_bytes
> 0);
335 * Read file through VFS and feed is to the "file" command.
336 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
339 get_file_type_pipe (char *filename
, char *buf
, int buflen
)
343 int pipehandle
, remotehandle
;
346 remotehandle
= mc_open (filename
, O_RDONLY
);
347 if (remotehandle
!= -1) {
348 /* 8192 is HOWMANY hardcoded value in the file-3.14
349 * sources. Tell me if any other file uses larger
350 * chunk from beginning
353 mc_doublepopen (remotehandle
, 8192, &p
, "file", "file", "-",
355 if (pipehandle
!= -1) {
358 read (pipehandle
, buf
+ read_bytes
,
359 buflen
- 1 - read_bytes
)) > 0)
361 mc_doublepclose (pipehandle
, p
);
364 mc_close (remotehandle
);
369 return (read_bytes
> 0);
371 #endif /* FILE_STDIN */
375 * Invoke the "file" command on the file and match its output against PTR.
376 * have_type is a flag that is set if we already have tried to determine
377 * the type of that file.
378 * Return 1 for match, 0 otherwise.
381 regex_check_type (char *filename
, int file_len
, char *ptr
, int *have_type
)
386 /* Following variables are valid if *have_type is 1 */
387 static char content_string
[2048];
388 static int content_shift
= 0;
389 static int got_data
= 0;
391 if (!use_file_to_check_type
) {
395 islocal
= vfs_file_is_local (filename
);
398 /* Don't repeate even unsuccessful checks */
403 get_file_type_local (filename
, content_string
,
404 sizeof (content_string
));
409 get_file_type_pipe (filename
, content_string
,
410 sizeof (content_string
));
413 /* Cannot use pipe, must make a local copy, not yet supported */
415 #endif /* !FILE_STDIN */
420 /* Paranoid termination */
421 content_string
[sizeof (content_string
) - 1] = 0;
423 if ((pp
= strchr (content_string
, '\n')) != 0)
426 if (islocal
&& !strncmp (content_string
, filename
, file_len
)) {
427 /* Skip "filename: " */
428 content_shift
= file_len
;
429 if (content_string
[content_shift
] == ':')
430 for (content_shift
++;
431 content_string
[content_shift
] == ' ';
434 && !strncmp (content_string
, "standard input:",
436 /* Skip "standard input: " */
437 for (content_shift
= 15;
438 content_string
[content_shift
] == ' ';
443 content_string
[0] = 0;
447 if (got_data
== -1) {
451 if (content_string
&& content_string
[0]
452 && regexp_match (ptr
, content_string
+ content_shift
,
461 /* The second argument is action, i.e. Open, View or Edit
463 * This function returns:
465 * -1 for a failure or user interrupt
466 * 0 if no command was run
467 * 1 if some command was run
469 * If action == "View" then a parameter is checked in the form of "View:%d",
470 * if the value for %d exists, then the viewer is started up at that line number.
473 regex_command (char *filename
, char *action
, int *move_dir
)
476 int file_len
= strlen (filename
);
482 int view_at_line_number
;
483 char *include_target
;
484 int include_target_len
;
485 int have_type
= 0; /* Flag used by regex_check_type() */
487 /* Check for the special View:%d parameter */
488 if (strncmp (action
, "View:", 5) == 0) {
489 view_at_line_number
= atoi (action
+ 5);
492 view_at_line_number
= 0;
496 char *extension_file
;
500 extension_file
= concat_dir_and_file (home_dir
, MC_USER_EXT
);
501 if (!exist_file (extension_file
)) {
502 g_free (extension_file
);
504 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
507 data
= load_file (extension_file
);
508 g_free (extension_file
);
512 if (!strstr (data
, "default/")) {
513 if (!strstr (data
, "regex/") && !strstr (data
, "shell/")
514 && !strstr (data
, "type/")) {
519 goto check_stock_mc_ext
;
524 g_strconcat (" ", mc_home
, MC_LIB_EXT
,
525 _(" file error "), NULL
);
527 g_strconcat (_("Format of the "), mc_home
,
528 _("mc.ext file has changed\n"
529 "with version 3.0. It seems that installation\n"
530 "failed. Please fetch a fresh new copy from the\n"
531 "Midnight Commander package."),
533 message (1, msg
, "%s", msg2
);
544 g_strconcat (" ~/", MC_USER_EXT
, _(" file error "), NULL
);
546 g_strconcat (_("Format of the "), "~/", MC_USER_EXT
,
547 _(" file has changed\n"
548 "with version 3.0. You may want either to\n"
549 "copy it from "), mc_home
,
550 _("mc.ext or use that\n"
551 "file as an example of how to write it.\n"),
553 _("mc.ext will be used for this moment."),
555 message (1, msg
, "%s", msg2
);
560 mc_stat (filename
, &mystat
);
562 old_patterns
= easy_patterns
;
563 easy_patterns
= 0; /* Real regular expressions are needed :) */
564 include_target
= NULL
;
565 include_target_len
= 0;
566 for (p
= data
; *p
; p
++) {
567 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
568 if (*q
== '\n' || !*q
)
569 p
= q
; /* empty line */
570 if (*p
== '#') /* comment */
571 while (*p
&& *p
!= '\n')
577 if (p
== q
) { /* i.e. starts in the first column, should be
581 q
= strchr (p
, '\n');
586 if (include_target
) {
587 if ((strncmp (p
, "include/", 8) == 0)
588 && (strncmp (p
+ 8, include_target
, include_target_len
)
591 } else if (!strncmp (p
, "regex/", 6)) {
593 /* Do not transform shell patterns, you can use shell/ for
596 if (regexp_match (p
, filename
, match_normal
))
598 } else if (!strncmp (p
, "directory/", 10)) {
599 if (S_ISDIR (mystat
.st_mode
)
600 && regexp_match (p
+ 10, filename
, match_normal
))
602 } else if (!strncmp (p
, "shell/", 6)) {
604 if (*p
== '.' && file_len
>= (q
- p
)) {
605 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
608 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
611 } else if (!strncmp (p
, "type/", 5)) {
614 res
= regex_check_type (filename
, file_len
, p
, &have_type
);
618 error_flag
= 1; /* leave it if file cannot be opened */
619 } else if (!strncmp (p
, "default/", 8)) {
626 } else { /* List of actions */
628 q
= strchr (p
, '\n');
631 if (found
&& !error_flag
) {
636 if (strcmp (p
, "Include") == 0) {
639 include_target
= p
+ 8;
640 t
= strchr (include_target
, '\n');
643 include_target_len
= strlen (include_target
);
655 if (!strcmp (action
, p
)) {
657 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
659 /* Empty commands just stop searching
660 * through, they don't do anything
662 * We need to copy the filename because exec_extension
663 * may end up invoking update_panels thus making the
664 * filename parameter invalid (ie, most of the time,
665 * we get filename as a pointer from cpanel->dir).
668 char *filename_copy
= g_strdup (filename
);
670 exec_extension (filename_copy
, r
+ 1, move_dir
,
671 view_at_line_number
);
672 g_free (filename_copy
);
686 easy_patterns
= old_patterns
;