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"
52 #include "charsets.h" /* get_codepage_index */
53 #include "selcodepage.h" /* do_set_codepage */
57 /* If set, we execute the file command to check the file type */
58 int use_file_to_check_type
= 1;
60 /* This variable points to a copy of the mc.ext file in memory
61 * With this we avoid loading/parsing the file each time we
64 static char *data
= NULL
;
67 flush_extension_file (void)
73 typedef char *(*quote_func_t
) (const char *name
, int quote_percent
);
76 exec_extension (const char *filename
, const char *lc_data
, int *move_dir
, int start_line
)
83 int expand_prefix_found
= 0;
84 int parameter_found
= 0;
87 int def_hex_mode
= mcview_default_hex_mode
, changed_hex_mode
= 0;
88 int def_nroff_flag
= mcview_default_nroff_flag
, changed_nroff_flag
= 0;
89 int written_nonspace
= 0;
93 char *localcopy
= NULL
;
95 time_t localmtime
= 0;
97 quote_func_t quote_func
= name_quote
;
99 g_return_if_fail (filename
!= NULL
);
100 g_return_if_fail (lc_data
!= NULL
);
102 /* Avoid making a local copy if we are doing a cd */
103 if (!vfs_file_is_local (filename
))
109 * All commands should be run in /bin/sh regardless of user shell.
110 * To do that, create temporary shell script and run it.
111 * Sometimes it's not needed (e.g. for %cd and %view commands),
112 * but it's easier to create it anyway.
114 cmd_file_fd
= mc_mkstemps (&file_name
, "mcext", SCRIPT_SUFFIX
);
116 if (cmd_file_fd
== -1)
118 message (D_ERROR
, MSG_ERROR
,
119 _("Cannot create temporary command file\n%s"), unix_error_string (errno
));
123 cmd_file
= fdopen (cmd_file_fd
, "w");
124 fputs ("#! /bin/sh\n", cmd_file
);
127 for (; *lc_data
!= '\0' && *lc_data
!= '\n'; lc_data
++)
137 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_EXT_PARAMETER
, "");
138 if (parameter
== NULL
)
145 mc_ungetlocalcopy (filename
, localcopy
, 0);
151 fputs (parameter
, cmd_file
);
152 written_nonspace
= 1;
157 size_t len
= strlen (lc_prompt
);
159 if (len
< sizeof (lc_prompt
) - 1)
161 lc_prompt
[len
] = *lc_data
;
162 lc_prompt
[len
+ 1] = '\0';
166 else if (expand_prefix_found
)
168 expand_prefix_found
= 0;
176 i
= check_format_view (lc_data
);
184 i
= check_format_cd (lc_data
);
188 quote_func
= fake_name_quote
;
195 i
= check_format_var (lc_data
, &v
);
196 if (i
> 0 && v
!= NULL
)
207 text
= expand_format (NULL
, *lc_data
, !is_cd
);
212 localcopy
= mc_getlocalcopy (filename
);
213 if (localcopy
== NULL
)
220 mc_stat (localcopy
, &mystat
);
221 localmtime
= mystat
.st_mtime
;
222 text
= quote_func (localcopy
, 0);
226 fn
= vfs_canon_and_translate (filename
);
227 text
= quote_func (fn
, 0);
233 fputs (text
, cmd_file
);
241 written_nonspace
= 1;
247 else if (*lc_data
== '%')
248 expand_prefix_found
= 1;
251 if (*lc_data
!= ' ' && *lc_data
!= '\t')
252 written_nonspace
= 1;
256 fputc (*lc_data
, cmd_file
);
261 * Make the script remove itself when it finishes.
262 * Don't do it for the viewer - it may need to rerun the script,
263 * so we clean up after calling view().
266 fprintf (cmd_file
, "\n/bin/rm -f %s\n", file_name
);
270 if ((run_view
&& !written_nonspace
) || is_cd
)
278 /* Set executable flag on the command file ... */
279 chmod (file_name
, S_IRWXU
);
280 /* ... but don't rely on it - run /bin/sh explicitly */
281 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
288 mcview_altered_hex_mode
= 0;
289 mcview_altered_nroff_flag
= 0;
290 if (def_hex_mode
!= mcview_default_hex_mode
)
291 changed_hex_mode
= 1;
292 if (def_nroff_flag
!= mcview_default_nroff_flag
)
293 changed_nroff_flag
= 1;
295 /* If we've written whitespace only, then just load filename
298 if (written_nonspace
)
300 ret
= mcview_viewer (cmd
, filename
, start_line
);
304 ret
= mcview_viewer (NULL
, filename
, start_line
);
306 if (move_dir
!= NULL
)
309 case MCVIEW_WANT_NEXT
:
312 case MCVIEW_WANT_PREV
:
319 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
320 mcview_default_hex_mode
= def_hex_mode
;
321 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
322 mcview_default_nroff_flag
= def_nroff_flag
;
331 /* while (*p == ' ' && *p == '\t')
334 /* Search last non-space character. Start search at the end in order
335 not to short filenames containing spaces. */
336 q
= p
+ strlen (p
) - 1;
337 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
340 do_cd (p
, cd_parse_command
);
344 shell_execute (cmd
, EXECUTE_INTERNAL
);
347 handle_console (CONSOLE_SAVE
);
348 if (output_lines
&& keybar_visible
)
349 show_console_contents (output_start_y
,
350 LINES
- keybar_visible
-
351 output_lines
- 1, LINES
- keybar_visible
- 1);
360 mc_stat (localcopy
, &mystat
);
361 mc_ungetlocalcopy (filename
, localcopy
, localmtime
!= mystat
.st_mtime
);
367 # define FILE_CMD "file -L "
369 # define FILE_CMD "file "
373 * Run cmd_file with args, put result into buf.
374 * If error, put '\0' into buf[0]
375 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
377 * NOTES: buf is null-terminated string.
380 get_popen_information (const char *cmd_file
, const char *args
, char *buf
, int buflen
)
382 gboolean read_bytes
= FALSE
;
386 command
= g_strconcat (cmd_file
, args
, " 2>/dev/null", (char *) NULL
);
387 f
= popen (command
, "r");
393 if (setvbuf (f
, NULL
, _IOFBF
, 0) != 0)
399 read_bytes
= (fgets (buf
, buflen
, f
) != NULL
);
401 buf
[0] = '\0'; /* Paranoid termination */
406 buf
[0] = '\0'; /* Paranoid termination */
410 buf
[buflen
- 1] = '\0';
412 return read_bytes
? 1 : 0;
416 * Run the "file" command on the local file.
417 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
420 get_file_type_local (const char *filename
, char *buf
, int buflen
)
425 tmp
= name_quote (filename
, 0);
426 ret
= get_popen_information (FILE_CMD
, tmp
, buf
, buflen
);
434 * Run the "enca" command on the local file.
435 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
438 get_file_encoding_local (const char *filename
, char *buf
, int buflen
)
440 char *tmp
, *lang
, *args
;
443 tmp
= name_quote (filename
, 0);
444 lang
= name_quote (autodetect_codeset
, 0);
445 args
= g_strconcat (" -L", lang
, " -i ", tmp
, (char *) NULL
);
447 ret
= get_popen_information ("enca", args
, buf
, buflen
);
455 #endif /* HAVE_CHARSET */
458 * Invoke the "file" command on the file and match its output against PTR.
459 * have_type is a flag that is set if we already have tried to determine
460 * the type of that file.
461 * Return 1 for match, 0 for no match, -1 errors.
464 regex_check_type (const char *filename
, const char *ptr
, int *have_type
)
468 /* Following variables are valid if *have_type is 1 */
469 static char content_string
[2048];
470 static char encoding_id
[21]; /* CSISO51INISCYRILLIC -- 20 */
471 static size_t content_shift
= 0;
472 static int got_data
= 0;
474 if (!use_file_to_check_type
)
479 char *realname
; /* name used with "file" */
483 int got_encoding_data
;
484 #endif /* HAVE_CHARSET */
486 /* Don't repeate even unsuccessful checks */
489 localfile
= mc_getlocalcopy (filename
);
490 if (localfile
== NULL
)
493 realname
= localfile
;
496 got_encoding_data
= is_autodetect_codeset_enabled
497 ? get_file_encoding_local (localfile
, encoding_id
, sizeof (encoding_id
)) : 0;
499 if (got_encoding_data
> 0)
504 pp
= strchr (encoding_id
, '\n');
508 cp_id
= get_codepage_index (encoding_id
);
510 cp_id
= default_source_codepage
;
512 do_set_codepage (cp_id
);
514 #endif /* HAVE_CHARSET */
516 mc_ungetlocalcopy (filename
, localfile
, 0);
518 got_data
= get_file_type_local (localfile
, content_string
, sizeof (content_string
));
525 pp
= strchr (content_string
, '\n');
529 real_len
= strlen (realname
);
531 if (strncmp (content_string
, realname
, real_len
) == 0)
533 /* Skip "realname: " */
534 content_shift
= real_len
;
535 if (content_string
[content_shift
] == ':')
537 /* Solaris' file prints tab(s) after ':' */
538 for (content_shift
++;
539 content_string
[content_shift
] == ' '
540 || content_string
[content_shift
] == '\t'; content_shift
++)
548 content_string
[0] = '\0';
556 if (content_string
[0] != '\0'
557 && mc_search (ptr
, content_string
+ content_shift
, MC_SEARCH_T_REGEX
))
566 /* The second argument is action, i.e. Open, View or Edit
568 * This function returns:
570 * -1 for a failure or user interrupt
571 * 0 if no command was run
572 * 1 if some command was run
574 * If action == "View" then a parameter is checked in the form of "View:%d",
575 * if the value for %d exists, then the viewer is started up at that line number.
578 regex_command (const char *filename
, const char *action
, int *move_dir
)
581 int file_len
= strlen (filename
);
586 int view_at_line_number
;
587 char *include_target
;
588 int include_target_len
;
589 int have_type
= 0; /* Flag used by regex_check_type() */
591 /* Check for the special View:%d parameter */
592 if (strncmp (action
, "View:", 5) == 0)
594 view_at_line_number
= atoi (action
+ 5);
599 view_at_line_number
= 0;
604 char *extension_file
;
608 extension_file
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_FILEBIND_FILE
, NULL
);
609 if (!exist_file (extension_file
))
611 g_free (extension_file
);
613 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
614 if (!exist_file (extension_file
))
616 g_free (extension_file
);
617 extension_file
= concat_dir_and_file (mc_home_alt
, MC_LIB_EXT
);
621 data
= load_file (extension_file
);
622 g_free (extension_file
);
626 if (!strstr (data
, "default/"))
628 if (!strstr (data
, "regex/") && !strstr (data
, "shell/") && !strstr (data
, "type/"))
635 goto check_stock_mc_ext
;
639 char *title
= g_strdup_printf (_(" %s%s file error"),
640 mc_home
, MC_LIB_EXT
);
641 message (D_ERROR
, title
, _("The format of the %smc.ext "
642 "file has changed with version 3.0. It seems that "
643 "the installation failed. Please fetch a fresh "
644 "copy from the Midnight Commander package."),
654 g_strdup_printf (_("~/%s file error"),
655 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
);
656 message (D_ERROR
, title
,
657 _("The format of the ~/%s file has "
658 "changed with version 3.0. You may either want to copy "
659 "it from %smc.ext or use that file as an example of how to write it."),
660 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE
, mc_home
);
664 mc_stat (filename
, &mystat
);
666 include_target
= NULL
;
667 include_target_len
= 0;
668 for (p
= data
; *p
; p
++)
670 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++);
671 if (*q
== '\n' || !*q
)
672 p
= q
; /* empty line */
673 if (*p
== '#') /* comment */
674 while (*p
&& *p
!= '\n')
681 { /* i.e. starts in the first column, should be
685 q
= strchr (p
, '\n');
692 if ((strncmp (p
, "include/", 8) == 0)
693 && (strncmp (p
+ 8, include_target
, include_target_len
) == 0))
696 else if (!strncmp (p
, "regex/", 6))
699 /* Do not transform shell patterns, you can use shell/ for
702 if (mc_search (p
, filename
, MC_SEARCH_T_REGEX
))
705 else if (!strncmp (p
, "directory/", 10))
707 if (S_ISDIR (mystat
.st_mode
) && mc_search (p
+ 10, filename
, MC_SEARCH_T_REGEX
))
710 else if (!strncmp (p
, "shell/", 6))
713 if (*p
== '.' && file_len
>= (q
- p
))
715 if (!strncmp (p
, filename
+ file_len
- (q
- p
), q
- p
))
720 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
724 else if (!strncmp (p
, "type/", 5))
728 res
= regex_check_type (filename
, p
, &have_type
);
732 error_flag
= 1; /* leave it if file cannot be opened */
734 else if (!strncmp (p
, "default/", 8))
744 { /* List of actions */
746 q
= strchr (p
, '\n');
749 if (found
&& !error_flag
)
756 if (strcmp (p
, "Include") == 0)
760 include_target
= p
+ 8;
761 t
= strchr (include_target
, '\n');
764 include_target_len
= strlen (include_target
);
776 if (!strcmp (action
, p
))
779 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++);
781 /* Empty commands just stop searching
782 * through, they don't do anything
784 * We need to copy the filename because exec_extension
785 * may end up invoking update_panels thus making the
786 * filename parameter invalid (ie, most of the time,
787 * we get filename as a pointer from current_panel->dir).
791 char *filename_copy
= g_strdup (filename
);
793 exec_extension (filename_copy
, r
+ 1, move_dir
, view_at_line_number
);
794 g_free (filename_copy
);