Merge branch '2123_crash_while_copy'
[midnight-commander.git] / src / ext.c
blobf5795e278f8e106e6a21ce2562bee310f6a7d6c8
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
6 1994 Miguel de Icaza
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. */
22 /** \file ext.c
23 * \brief Source: extension dependent execution
26 #include <config.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
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"
45 #include "user.h"
46 #include "main.h"
47 #include "wtools.h"
48 #include "execute.h"
49 #include "history.h"
50 #include "layout.h"
51 #ifdef HAVE_CHARSET
52 #include "charsets.h" /* get_codepage_index */
53 #include "selcodepage.h" /* do_set_codepage */
54 #endif
55 #include "dialog-switch.h"
56 #include "ext.h"
58 /* If set, we execute the file command to check the file type */
59 int use_file_to_check_type = 1;
61 /* This variable points to a copy of the mc.ext file in memory
62 * With this we avoid loading/parsing the file each time we
63 * need it
65 static char *data = NULL;
67 void
68 flush_extension_file (void)
70 g_free (data);
71 data = NULL;
74 typedef char *(*quote_func_t) (const char *name, int quote_percent);
76 static void
77 exec_extension (const char *filename, const char *lc_data, int *move_dir, int start_line)
79 char *fn;
80 char *file_name;
81 int cmd_file_fd;
82 FILE *cmd_file;
83 char *cmd = NULL;
84 int expand_prefix_found = 0;
85 int parameter_found = 0;
86 char lc_prompt[80];
87 int run_view = 0;
88 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
89 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
90 int written_nonspace = 0;
91 int is_cd = 0;
92 char buffer[1024];
93 char *p = 0;
94 char *localcopy = NULL;
95 int do_local_copy;
96 time_t localmtime = 0;
97 struct stat mystat;
98 quote_func_t quote_func = name_quote;
100 g_return_if_fail (filename != NULL);
101 g_return_if_fail (lc_data != NULL);
103 /* Avoid making a local copy if we are doing a cd */
104 if (!vfs_file_is_local (filename))
105 do_local_copy = 1;
106 else
107 do_local_copy = 0;
110 * All commands should be run in /bin/sh regardless of user shell.
111 * To do that, create temporary shell script and run it.
112 * Sometimes it's not needed (e.g. for %cd and %view commands),
113 * but it's easier to create it anyway.
115 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
117 if (cmd_file_fd == -1)
119 message (D_ERROR, MSG_ERROR,
120 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
121 return;
124 cmd_file = fdopen (cmd_file_fd, "w");
125 fputs ("#! /bin/sh\n", cmd_file);
127 lc_prompt[0] = '\0';
128 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
130 if (parameter_found)
132 if (*lc_data == '}')
134 char *parameter;
136 parameter_found = 0;
137 parameter =
138 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
139 if (parameter == NULL)
141 /* User canceled */
142 fclose (cmd_file);
143 unlink (file_name);
144 if (localcopy)
146 mc_ungetlocalcopy (filename, localcopy, 0);
147 g_free (localcopy);
149 g_free (file_name);
150 return;
152 fputs (parameter, cmd_file);
153 written_nonspace = 1;
154 g_free (parameter);
156 else
158 size_t len = strlen (lc_prompt);
160 if (len < sizeof (lc_prompt) - 1)
162 lc_prompt[len] = *lc_data;
163 lc_prompt[len + 1] = '\0';
167 else if (expand_prefix_found)
169 expand_prefix_found = 0;
170 if (*lc_data == '{')
171 parameter_found = 1;
172 else
174 int i;
175 char *v;
177 i = check_format_view (lc_data);
178 if (i != 0)
180 lc_data += i - 1;
181 run_view = 1;
183 else
185 i = check_format_cd (lc_data);
186 if (i > 0)
188 is_cd = 1;
189 quote_func = fake_name_quote;
190 do_local_copy = 0;
191 p = buffer;
192 lc_data += i - 1;
194 else
196 i = check_format_var (lc_data, &v);
197 if (i > 0 && v != NULL)
199 fputs (v, cmd_file);
200 g_free (v);
201 lc_data += i;
203 else
205 char *text;
207 if (*lc_data != 'f')
208 text = expand_format (NULL, *lc_data, !is_cd);
209 else
211 if (do_local_copy)
213 localcopy = mc_getlocalcopy (filename);
214 if (localcopy == NULL)
216 fclose (cmd_file);
217 unlink (file_name);
218 g_free (file_name);
219 return;
221 mc_stat (localcopy, &mystat);
222 localmtime = mystat.st_mtime;
223 text = quote_func (localcopy, 0);
225 else
227 fn = vfs_canon_and_translate (filename);
228 text = quote_func (fn, 0);
229 g_free (fn);
233 if (!is_cd)
234 fputs (text, cmd_file);
235 else
237 strcpy (p, text);
238 p = strchr (p, 0);
241 g_free (text);
242 written_nonspace = 1;
248 else if (*lc_data == '%')
249 expand_prefix_found = 1;
250 else
252 if (*lc_data != ' ' && *lc_data != '\t')
253 written_nonspace = 1;
254 if (is_cd)
255 *(p++) = *lc_data;
256 else
257 fputc (*lc_data, cmd_file);
259 } /* for */
262 * Make the script remove itself when it finishes.
263 * Don't do it for the viewer - it may need to rerun the script,
264 * so we clean up after calling view().
266 if (!run_view)
267 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
269 fclose (cmd_file);
271 if ((run_view && !written_nonspace) || is_cd)
273 unlink (file_name);
274 g_free (file_name);
275 file_name = NULL;
277 else
279 /* Set executable flag on the command file ... */
280 chmod (file_name, S_IRWXU);
281 /* ... but don't rely on it - run /bin/sh explicitly */
282 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
285 if (run_view)
287 mcview_ret_t ret;
289 mcview_altered_hex_mode = 0;
290 mcview_altered_nroff_flag = 0;
291 if (def_hex_mode != mcview_default_hex_mode)
292 changed_hex_mode = 1;
293 if (def_nroff_flag != mcview_default_nroff_flag)
294 changed_nroff_flag = 1;
296 /* If we've written whitespace only, then just load filename
297 * into view
299 if (written_nonspace)
301 ret = mcview_viewer (cmd, filename, start_line);
302 unlink (file_name);
304 else
305 ret = mcview_viewer (NULL, filename, start_line);
307 if (move_dir != NULL)
308 switch (ret)
310 case MCVIEW_WANT_NEXT:
311 *move_dir = 1;
312 break;
313 case MCVIEW_WANT_PREV:
314 *move_dir = -1;
315 break;
316 default:
317 *move_dir = 0;
320 if (changed_hex_mode && !mcview_altered_hex_mode)
321 mcview_default_hex_mode = def_hex_mode;
322 if (changed_nroff_flag && !mcview_altered_nroff_flag)
323 mcview_default_nroff_flag = def_nroff_flag;
325 dialog_switch_process_pending ();
327 else if (is_cd)
329 char *q;
330 *p = 0;
331 p = buffer;
332 /* while (*p == ' ' && *p == '\t')
333 * p++;
335 /* Search last non-space character. Start search at the end in order
336 not to short filenames containing spaces. */
337 q = p + strlen (p) - 1;
338 while (q >= p && (*q == ' ' || *q == '\t'))
339 q--;
340 q[1] = 0;
341 do_cd (p, cd_parse_command);
343 else
345 shell_execute (cmd, EXECUTE_INTERNAL);
346 if (console_flag)
348 handle_console (CONSOLE_SAVE);
349 if (output_lines && keybar_visible)
350 show_console_contents (output_start_y,
351 LINES - keybar_visible -
352 output_lines - 1, LINES - keybar_visible - 1);
356 g_free (file_name);
357 g_free (cmd);
359 if (localcopy)
361 mc_stat (localcopy, &mystat);
362 mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime);
363 g_free (localcopy);
367 #ifdef FILE_L
368 # define FILE_CMD "file -L "
369 #else
370 # define FILE_CMD "file "
371 #endif
374 * Run cmd_file with args, put result into buf.
375 * If error, put '\0' into buf[0]
376 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
378 * NOTES: buf is null-terminated string.
380 static int
381 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
383 gboolean read_bytes = FALSE;
384 char *command;
385 FILE *f;
387 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
388 f = popen (command, "r");
389 g_free (command);
391 if (f != NULL)
393 #ifdef __QNXNTO__
394 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
396 (void) pclose (f);
397 return -1;
399 #endif
400 read_bytes = (fgets (buf, buflen, f) != NULL);
401 if (!read_bytes)
402 buf[0] = '\0'; /* Paranoid termination */
403 pclose (f);
405 else
407 buf[0] = '\0'; /* Paranoid termination */
408 return -1;
411 buf[buflen - 1] = '\0';
413 return read_bytes ? 1 : 0;
417 * Run the "file" command on the local file.
418 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
420 static int
421 get_file_type_local (const char *filename, char *buf, int buflen)
423 char *tmp;
424 int ret;
426 tmp = name_quote (filename, 0);
427 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
428 g_free (tmp);
430 return ret;
433 #ifdef HAVE_CHARSET
435 * Run the "enca" command on the local file.
436 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
438 static int
439 get_file_encoding_local (const char *filename, char *buf, int buflen)
441 char *tmp, *lang, *args;
442 int ret;
444 tmp = name_quote (filename, 0);
445 lang = name_quote (autodetect_codeset, 0);
446 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
448 ret = get_popen_information ("enca", args, buf, buflen);
450 g_free (args);
451 g_free (lang);
452 g_free (tmp);
454 return ret;
456 #endif /* HAVE_CHARSET */
459 * Invoke the "file" command on the file and match its output against PTR.
460 * have_type is a flag that is set if we already have tried to determine
461 * the type of that file.
462 * Return 1 for match, 0 for no match, -1 errors.
464 static int
465 regex_check_type (const char *filename, const char *ptr, int *have_type)
467 int found = 0;
469 /* Following variables are valid if *have_type is 1 */
470 static char content_string[2048];
471 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
472 static size_t content_shift = 0;
473 static int got_data = 0;
475 if (!use_file_to_check_type)
476 return 0;
478 if (*have_type == 0)
480 char *realname; /* name used with "file" */
481 char *localfile;
483 #ifdef HAVE_CHARSET
484 int got_encoding_data;
485 #endif /* HAVE_CHARSET */
487 /* Don't repeate even unsuccessful checks */
488 *have_type = 1;
490 localfile = mc_getlocalcopy (filename);
491 if (localfile == NULL)
492 return -1;
494 realname = localfile;
496 #ifdef HAVE_CHARSET
497 got_encoding_data = is_autodetect_codeset_enabled
498 ? get_file_encoding_local (localfile, encoding_id, sizeof (encoding_id)) : 0;
500 if (got_encoding_data > 0)
502 char *pp;
503 int cp_id;
505 pp = strchr (encoding_id, '\n');
506 if (pp != NULL)
507 *pp = '\0';
509 cp_id = get_codepage_index (encoding_id);
510 if (cp_id == -1)
511 cp_id = default_source_codepage;
513 do_set_codepage (cp_id);
515 #endif /* HAVE_CHARSET */
517 mc_ungetlocalcopy (filename, localfile, 0);
519 got_data = get_file_type_local (localfile, content_string, sizeof (content_string));
521 if (got_data > 0)
523 char *pp;
524 size_t real_len;
526 pp = strchr (content_string, '\n');
527 if (pp != NULL)
528 *pp = '\0';
530 real_len = strlen (realname);
532 if (strncmp (content_string, realname, real_len) == 0)
534 /* Skip "realname: " */
535 content_shift = real_len;
536 if (content_string[content_shift] == ':')
538 /* Solaris' file prints tab(s) after ':' */
539 for (content_shift++;
540 content_string[content_shift] == ' '
541 || content_string[content_shift] == '\t'; content_shift++)
546 else
548 /* No data */
549 content_string[0] = '\0';
551 g_free (realname);
554 if (got_data == -1)
555 return -1;
557 if (content_string[0] != '\0'
558 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX))
560 found = 1;
563 return found;
567 /* The second argument is action, i.e. Open, View or Edit
569 * This function returns:
571 * -1 for a failure or user interrupt
572 * 0 if no command was run
573 * 1 if some command was run
575 * If action == "View" then a parameter is checked in the form of "View:%d",
576 * if the value for %d exists, then the viewer is started up at that line number.
579 regex_command (const char *filename, const char *action, int *move_dir)
581 char *p, *q, *r, c;
582 int file_len = strlen (filename);
583 int found = 0;
584 int error_flag = 0;
585 int ret = 0;
586 struct stat mystat;
587 int view_at_line_number;
588 char *include_target;
589 int include_target_len;
590 int have_type = 0; /* Flag used by regex_check_type() */
592 /* Check for the special View:%d parameter */
593 if (strncmp (action, "View:", 5) == 0)
595 view_at_line_number = atoi (action + 5);
596 action = "View";
598 else
600 view_at_line_number = 0;
603 if (data == NULL)
605 char *extension_file;
606 int mc_user_ext = 1;
607 int home_error = 0;
609 extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
610 if (!exist_file (extension_file))
612 g_free (extension_file);
613 check_stock_mc_ext:
614 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
615 if (!exist_file (extension_file))
617 g_free (extension_file);
618 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
620 mc_user_ext = 0;
622 data = load_file (extension_file);
623 g_free (extension_file);
624 if (data == NULL)
625 return 0;
627 if (!strstr (data, "default/"))
629 if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/"))
631 g_free (data);
632 data = NULL;
633 if (mc_user_ext)
635 home_error = 1;
636 goto check_stock_mc_ext;
638 else
640 char *title = g_strdup_printf (_(" %s%s file error"),
641 mc_home, MC_LIB_EXT);
642 message (D_ERROR, title, _("The format of the %smc.ext "
643 "file has changed with version 3.0. It seems that "
644 "the installation failed. Please fetch a fresh "
645 "copy from the Midnight Commander package."),
646 mc_home);
647 g_free (title);
648 return 0;
652 if (home_error)
654 char *title =
655 g_strdup_printf (_("~/%s file error"),
656 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE);
657 message (D_ERROR, title,
658 _("The format of the ~/%s file has "
659 "changed with version 3.0. You may either want to copy "
660 "it from %smc.ext or use that file as an example of how to write it."),
661 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home);
662 g_free (title);
665 mc_stat (filename, &mystat);
667 include_target = NULL;
668 include_target_len = 0;
669 for (p = data; *p; p++)
671 for (q = p; *q == ' ' || *q == '\t'; q++);
672 if (*q == '\n' || !*q)
673 p = q; /* empty line */
674 if (*p == '#') /* comment */
675 while (*p && *p != '\n')
676 p++;
677 if (*p == '\n')
678 continue;
679 if (!*p)
680 break;
681 if (p == q)
682 { /* i.e. starts in the first column, should be
683 * keyword/descNL
685 found = 0;
686 q = strchr (p, '\n');
687 if (q == NULL)
688 q = strchr (p, 0);
689 c = *q;
690 *q = 0;
691 if (include_target)
693 if ((strncmp (p, "include/", 8) == 0)
694 && (strncmp (p + 8, include_target, include_target_len) == 0))
695 found = 1;
697 else if (!strncmp (p, "regex/", 6))
699 p += 6;
700 /* Do not transform shell patterns, you can use shell/ for
701 * that
703 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
704 found = 1;
706 else if (!strncmp (p, "directory/", 10))
708 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
709 found = 1;
711 else if (!strncmp (p, "shell/", 6))
713 p += 6;
714 if (*p == '.' && file_len >= (q - p))
716 if (!strncmp (p, filename + file_len - (q - p), q - p))
717 found = 1;
719 else
721 if (q - p == file_len && !strncmp (p, filename, q - p))
722 found = 1;
725 else if (!strncmp (p, "type/", 5))
727 int res;
728 p += 5;
729 res = regex_check_type (filename, p, &have_type);
730 if (res == 1)
731 found = 1;
732 if (res == -1)
733 error_flag = 1; /* leave it if file cannot be opened */
735 else if (!strncmp (p, "default/", 8))
737 found = 1;
739 *q = c;
740 p = q;
741 if (!*p)
742 break;
744 else
745 { /* List of actions */
746 p = q;
747 q = strchr (p, '\n');
748 if (q == NULL)
749 q = strchr (p, 0);
750 if (found && !error_flag)
752 r = strchr (p, '=');
753 if (r != NULL)
755 c = *r;
756 *r = 0;
757 if (strcmp (p, "Include") == 0)
759 char *t;
761 include_target = p + 8;
762 t = strchr (include_target, '\n');
763 if (t)
764 *t = 0;
765 include_target_len = strlen (include_target);
766 if (t)
767 *t = '\n';
769 *r = c;
770 p = q;
771 found = 0;
773 if (!*p)
774 break;
775 continue;
777 if (!strcmp (action, p))
779 *r = c;
780 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
782 /* Empty commands just stop searching
783 * through, they don't do anything
785 * We need to copy the filename because exec_extension
786 * may end up invoking update_panels thus making the
787 * filename parameter invalid (ie, most of the time,
788 * we get filename as a pointer from current_panel->dir).
790 if (p < q)
792 char *filename_copy = g_strdup (filename);
794 exec_extension (filename_copy, r + 1, move_dir, view_at_line_number);
795 g_free (filename_copy);
797 ret = 1;
799 break;
801 else
802 *r = c;
805 p = q;
806 if (!*p)
807 break;
810 if (error_flag)
811 return -1;
812 return ret;