vfs_path: Fixed broken relative paths processing.
[midnight-commander.git] / src / filemanager / ext.c
blob6fb9914209c5d0bb1a246ed9c2a5fe02a7e057e1
1 /*
2 Extension dependent execution.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Jakub Jelinek, 1995
10 Miguel de Icaza, 1994
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file ext.c
29 * \brief Source: extension dependent execution
32 #include <config.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/search.h"
44 #include "lib/fileloc.h"
45 #include "lib/mcconfig.h"
46 #include "lib/util.h"
47 #include "lib/vfs/vfs.h"
48 #include "lib/widget.h"
49 #include "lib/charsets.h" /* get_codepage_index */
51 #include "src/setup.h" /* use_file_to_check_type */
52 #include "src/execute.h"
53 #include "src/history.h"
54 #include "src/main.h" /* do_cd */
56 #include "src/consaver/cons.saver.h"
57 #include "src/viewer/mcviewer.h"
59 #ifdef HAVE_CHARSET
60 #include "src/selcodepage.h" /* do_set_codepage */
61 #endif
63 #include "usermenu.h"
65 #include "ext.h"
67 /*** global variables ****************************************************************************/
69 /*** file scope macro definitions ****************************************************************/
71 #ifdef FILE_L
72 #define FILE_CMD "file -L "
73 #else
74 #define FILE_CMD "file "
75 #endif
77 /*** file scope type declarations ****************************************************************/
79 typedef char *(*quote_func_t) (const char *name, int quote_percent);
81 /*** file scope variables ************************************************************************/
83 /* This variable points to a copy of the mc.ext file in memory
84 * With this we avoid loading/parsing the file each time we
85 * need it
87 static char *data = NULL;
89 /*** file scope functions ************************************************************************/
90 /* --------------------------------------------------------------------------------------------- */
92 static void
93 exec_extension (const char *filename, const char *lc_data, int *move_dir, int start_line)
95 vfs_path_t *file_name_vpath;
96 int cmd_file_fd;
97 FILE *cmd_file;
98 char *cmd = NULL;
99 int expand_prefix_found = 0;
100 int parameter_found = 0;
101 char lc_prompt[80];
102 int run_view = 0;
103 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
104 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
105 int written_nonspace = 0;
106 int is_cd = 0;
107 char buffer[1024];
108 char *p = 0;
109 vfs_path_t *localcopy_vpath = NULL;
110 int do_local_copy;
111 time_t localmtime = 0;
112 struct stat mystat;
113 quote_func_t quote_func = name_quote;
114 vfs_path_t *vpath;
116 g_return_if_fail (filename != NULL);
117 g_return_if_fail (lc_data != NULL);
119 vpath = vfs_path_from_str (filename);
121 /* Avoid making a local copy if we are doing a cd */
122 do_local_copy = vfs_file_is_local (vpath) ? 0 : 1;
125 * All commands should be run in /bin/sh regardless of user shell.
126 * To do that, create temporary shell script and run it.
127 * Sometimes it's not needed (e.g. for %cd and %view commands),
128 * but it's easier to create it anyway.
130 cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcext", SCRIPT_SUFFIX);
132 if (cmd_file_fd == -1)
134 message (D_ERROR, MSG_ERROR,
135 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
136 goto ret;
139 cmd_file = fdopen (cmd_file_fd, "w");
140 fputs ("#! /bin/sh\n", cmd_file);
142 lc_prompt[0] = '\0';
143 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
145 if (parameter_found)
147 if (*lc_data == '}')
149 char *parameter;
151 parameter_found = 0;
152 parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
153 if (parameter == NULL)
155 /* User canceled */
156 fclose (cmd_file);
157 mc_unlink (file_name_vpath);
158 if (localcopy_vpath != NULL)
160 mc_ungetlocalcopy (vpath, localcopy_vpath, FALSE);
161 vfs_path_free (localcopy_vpath);
163 goto ret;
165 fputs (parameter, cmd_file);
166 written_nonspace = 1;
167 g_free (parameter);
169 else
171 size_t len = strlen (lc_prompt);
173 if (len < sizeof (lc_prompt) - 1)
175 lc_prompt[len] = *lc_data;
176 lc_prompt[len + 1] = '\0';
180 else if (expand_prefix_found)
182 expand_prefix_found = 0;
183 if (*lc_data == '{')
184 parameter_found = 1;
185 else
187 int i;
188 char *v;
190 i = check_format_view (lc_data);
191 if (i != 0)
193 lc_data += i - 1;
194 run_view = 1;
196 else
198 i = check_format_cd (lc_data);
199 if (i > 0)
201 is_cd = 1;
202 quote_func = fake_name_quote;
203 do_local_copy = 0;
204 p = buffer;
205 lc_data += i - 1;
207 else
209 i = check_format_var (lc_data, &v);
210 if (i > 0 && v != NULL)
212 fputs (v, cmd_file);
213 g_free (v);
214 lc_data += i;
216 else
218 char *text;
220 if (*lc_data != 'f')
221 text = expand_format (NULL, *lc_data, !is_cd);
222 else
224 if (do_local_copy)
226 localcopy_vpath = mc_getlocalcopy (vpath);
227 if (localcopy_vpath == NULL)
229 fclose (cmd_file);
230 mc_unlink (file_name_vpath);
231 goto ret;
233 mc_stat (localcopy_vpath, &mystat);
234 localmtime = mystat.st_mtime;
235 text =
236 quote_func (vfs_path_get_last_path_str (localcopy_vpath),
239 else
241 const vfs_path_element_t *path_element;
243 path_element = vfs_path_get_by_index (vpath, -1);
244 text = quote_func (path_element->path, 0);
248 if (!is_cd)
249 fputs (text, cmd_file);
250 else
252 strcpy (p, text);
253 p = strchr (p, 0);
256 g_free (text);
257 written_nonspace = 1;
263 else if (*lc_data == '%')
264 expand_prefix_found = 1;
265 else
267 if (*lc_data != ' ' && *lc_data != '\t')
268 written_nonspace = 1;
269 if (is_cd)
270 *(p++) = *lc_data;
271 else
272 fputc (*lc_data, cmd_file);
274 } /* for */
277 * Make the script remove itself when it finishes.
278 * Don't do it for the viewer - it may need to rerun the script,
279 * so we clean up after calling view().
281 if (!run_view)
283 char *file_name;
285 file_name = vfs_path_to_str (file_name_vpath);
286 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
287 g_free (file_name);
290 fclose (cmd_file);
292 if ((run_view && !written_nonspace) || is_cd)
294 mc_unlink (file_name_vpath);
295 vfs_path_free (file_name_vpath);
296 file_name_vpath = NULL;
298 else
300 char *file_name;
302 file_name = vfs_path_to_str (file_name_vpath);
303 /* Set executable flag on the command file ... */
304 mc_chmod (file_name_vpath, S_IRWXU);
305 /* ... but don't rely on it - run /bin/sh explicitly */
306 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
307 g_free (file_name);
310 if (run_view)
312 mcview_ret_t ret;
314 mcview_altered_hex_mode = 0;
315 mcview_altered_nroff_flag = 0;
316 if (def_hex_mode != mcview_default_hex_mode)
317 changed_hex_mode = 1;
318 if (def_nroff_flag != mcview_default_nroff_flag)
319 changed_nroff_flag = 1;
321 /* If we've written whitespace only, then just load filename
322 * into view
324 if (written_nonspace)
326 ret = mcview_viewer (cmd, vpath, start_line);
327 mc_unlink (file_name_vpath);
329 else
330 ret = mcview_viewer (NULL, vpath, start_line);
332 if (move_dir != NULL)
333 switch (ret)
335 case MCVIEW_WANT_NEXT:
336 *move_dir = 1;
337 break;
338 case MCVIEW_WANT_PREV:
339 *move_dir = -1;
340 break;
341 default:
342 *move_dir = 0;
345 if (changed_hex_mode && !mcview_altered_hex_mode)
346 mcview_default_hex_mode = def_hex_mode;
347 if (changed_nroff_flag && !mcview_altered_nroff_flag)
348 mcview_default_nroff_flag = def_nroff_flag;
350 dialog_switch_process_pending ();
352 else if (is_cd)
354 char *q;
355 vfs_path_t *p_vpath;
357 *p = 0;
358 p = buffer;
359 /* while (*p == ' ' && *p == '\t')
360 * p++;
362 /* Search last non-space character. Start search at the end in order
363 not to short filenames containing spaces. */
364 q = p + strlen (p) - 1;
365 while (q >= p && (*q == ' ' || *q == '\t'))
366 q--;
367 q[1] = 0;
369 p_vpath = vfs_path_from_str_flags (p, VPF_NO_CANON);
370 do_cd (p_vpath, cd_parse_command);
371 vfs_path_free (p_vpath);
373 else
375 shell_execute (cmd, EXECUTE_INTERNAL);
376 if (mc_global.tty.console_flag != '\0')
378 handle_console (CONSOLE_SAVE);
379 if (output_lines && mc_global.keybar_visible)
380 show_console_contents (output_start_y,
381 LINES - mc_global.keybar_visible -
382 output_lines - 1, LINES - mc_global.keybar_visible - 1);
386 g_free (cmd);
388 if (localcopy_vpath != NULL)
390 mc_stat (localcopy_vpath, &mystat);
391 mc_ungetlocalcopy (vpath, localcopy_vpath, localmtime != mystat.st_mtime);
392 vfs_path_free (localcopy_vpath);
394 ret:
395 vfs_path_free (file_name_vpath);
396 vfs_path_free (vpath);
399 /* --------------------------------------------------------------------------------------------- */
401 * Run cmd_file with args, put result into buf.
402 * If error, put '\0' into buf[0]
403 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
405 * NOTES: buf is null-terminated string.
408 static int
409 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
411 gboolean read_bytes = FALSE;
412 char *command;
413 FILE *f;
415 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
416 f = popen (command, "r");
417 g_free (command);
419 if (f != NULL)
421 #ifdef __QNXNTO__
422 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
424 (void) pclose (f);
425 return -1;
427 #endif
428 read_bytes = (fgets (buf, buflen, f) != NULL);
429 if (!read_bytes)
430 buf[0] = '\0'; /* Paranoid termination */
431 pclose (f);
433 else
435 buf[0] = '\0'; /* Paranoid termination */
436 return -1;
439 buf[buflen - 1] = '\0';
441 return read_bytes ? 1 : 0;
444 /* --------------------------------------------------------------------------------------------- */
446 * Run the "file" command on the local file.
447 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
450 static int
451 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
453 char *tmp;
454 int ret;
456 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
457 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
458 g_free (tmp);
460 return ret;
463 /* --------------------------------------------------------------------------------------------- */
465 * Run the "enca" command on the local file.
466 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
469 #ifdef HAVE_CHARSET
470 static int
471 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
473 char *tmp, *lang, *args;
474 int ret;
476 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
477 lang = name_quote (autodetect_codeset, 0);
478 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
480 ret = get_popen_information ("enca", args, buf, buflen);
482 g_free (args);
483 g_free (lang);
484 g_free (tmp);
486 return ret;
488 #endif /* HAVE_CHARSET */
490 /* --------------------------------------------------------------------------------------------- */
492 * Invoke the "file" command on the file and match its output against PTR.
493 * have_type is a flag that is set if we already have tried to determine
494 * the type of that file.
495 * Return 1 for match, 0 for no match, -1 errors.
498 static int
499 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type)
501 int found = 0;
503 /* Following variables are valid if *have_type is 1 */
504 static char content_string[2048];
505 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
506 static size_t content_shift = 0;
507 static int got_data = 0;
509 if (!use_file_to_check_type)
510 return 0;
512 if (*have_type == 0)
514 vfs_path_t *localfile_vpath;
515 const char *realname; /* name used with "file" */
517 #ifdef HAVE_CHARSET
518 int got_encoding_data;
519 #endif /* HAVE_CHARSET */
521 /* Don't repeate even unsuccessful checks */
522 *have_type = 1;
524 localfile_vpath = mc_getlocalcopy (filename_vpath);
525 if (localfile_vpath == NULL)
526 return -1;
528 realname = vfs_path_get_last_path_str (localfile_vpath);
530 #ifdef HAVE_CHARSET
531 got_encoding_data = is_autodetect_codeset_enabled
532 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
534 if (got_encoding_data > 0)
536 char *pp;
537 int cp_id;
539 pp = strchr (encoding_id, '\n');
540 if (pp != NULL)
541 *pp = '\0';
543 cp_id = get_codepage_index (encoding_id);
544 if (cp_id == -1)
545 cp_id = default_source_codepage;
547 do_set_codepage (cp_id);
549 #endif /* HAVE_CHARSET */
551 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
553 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
555 if (got_data > 0)
557 char *pp;
558 size_t real_len;
560 pp = strchr (content_string, '\n');
561 if (pp != NULL)
562 *pp = '\0';
564 real_len = strlen (realname);
566 if (strncmp (content_string, realname, real_len) == 0)
568 /* Skip "realname: " */
569 content_shift = real_len;
570 if (content_string[content_shift] == ':')
572 /* Solaris' file prints tab(s) after ':' */
573 for (content_shift++;
574 content_string[content_shift] == ' '
575 || content_string[content_shift] == '\t'; content_shift++)
580 else
582 /* No data */
583 content_string[0] = '\0';
585 vfs_path_free (localfile_vpath);
588 if (got_data == -1)
589 return -1;
591 if (content_string[0] != '\0'
592 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX))
594 found = 1;
597 return found;
600 /* --------------------------------------------------------------------------------------------- */
601 /*** public functions ****************************************************************************/
602 /* --------------------------------------------------------------------------------------------- */
604 void
605 flush_extension_file (void)
607 g_free (data);
608 data = NULL;
611 /* --------------------------------------------------------------------------------------------- */
613 * The second argument is action, i.e. Open, View or Edit
615 * This function returns:
617 * -1 for a failure or user interrupt
618 * 0 if no command was run
619 * 1 if some command was run
621 * If action == "View" then a parameter is checked in the form of "View:%d",
622 * if the value for %d exists, then the viewer is started up at that line number.
626 regex_command (const vfs_path_t * filename_vpath, const char *action, int *move_dir)
628 char *filename, *p, *q, *r, c;
629 size_t file_len;
630 int found = 0;
631 int error_flag = 0;
632 int ret = 0;
633 struct stat mystat;
634 int view_at_line_number;
635 char *include_target;
636 int include_target_len;
637 int have_type = 0; /* Flag used by regex_check_type() */
639 /* Check for the special View:%d parameter */
640 if (strncmp (action, "View:", 5) == 0)
642 view_at_line_number = atoi (action + 5);
643 action = "View";
645 else
647 view_at_line_number = 0;
650 if (data == NULL)
652 char *extension_file;
653 int mc_user_ext = 1;
654 int home_error = 0;
656 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
657 if (!exist_file (extension_file))
659 g_free (extension_file);
660 check_stock_mc_ext:
661 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
662 if (!exist_file (extension_file))
664 g_free (extension_file);
665 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
667 mc_user_ext = 0;
670 g_file_get_contents (extension_file, &data, NULL, NULL);
671 g_free (extension_file);
672 if (data == NULL)
673 return 0;
675 if (!strstr (data, "default/"))
677 if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/"))
679 g_free (data);
680 data = NULL;
681 if (mc_user_ext)
683 home_error = 1;
684 goto check_stock_mc_ext;
686 else
688 char *title = g_strdup_printf (_(" %s%s file error"),
689 mc_global.sysconfig_dir, MC_LIB_EXT);
690 message (D_ERROR, title, _("The format of the %smc.ext "
691 "file has changed with version 3.0. It seems that "
692 "the installation failed. Please fetch a fresh "
693 "copy from the Midnight Commander package."),
694 mc_global.sysconfig_dir);
695 g_free (title);
696 return 0;
700 if (home_error)
702 char *filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
703 char *title = g_strdup_printf (_("%s file error"), filebind_filename);
704 message (D_ERROR, title,
705 _("The format of the %s file has "
706 "changed with version 3.0. You may either want to copy "
707 "it from %smc.ext or use that file as an example of how to write it."),
708 filebind_filename, mc_global.sysconfig_dir);
709 g_free (filebind_filename);
710 g_free (title);
714 mc_stat (filename_vpath, &mystat);
716 include_target = NULL;
717 include_target_len = 0;
718 filename = vfs_path_to_str (filename_vpath);
719 file_len = vfs_path_len (filename_vpath);
721 for (p = data; *p; p++)
723 for (q = p; *q == ' ' || *q == '\t'; q++);
724 if (*q == '\n' || !*q)
725 p = q; /* empty line */
726 if (*p == '#') /* comment */
727 while (*p && *p != '\n')
728 p++;
729 if (*p == '\n')
730 continue;
731 if (!*p)
732 break;
733 if (p == q)
734 { /* i.e. starts in the first column, should be
735 * keyword/descNL
737 found = 0;
738 q = strchr (p, '\n');
739 if (q == NULL)
740 q = strchr (p, 0);
741 c = *q;
742 *q = 0;
743 if (include_target)
745 if ((strncmp (p, "include/", 8) == 0)
746 && (strncmp (p + 8, include_target, include_target_len) == 0))
747 found = 1;
749 else if (!strncmp (p, "regex/", 6))
751 p += 6;
752 /* Do not transform shell patterns, you can use shell/ for
753 * that
755 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
756 found = 1;
758 else if (!strncmp (p, "directory/", 10))
760 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
761 found = 1;
763 else if (!strncmp (p, "shell/", 6))
765 p += 6;
766 if (*p == '.' && file_len >= (size_t)(q - p))
768 if (!strncmp (p, filename + file_len - (q - p), q - p))
769 found = 1;
771 else
773 if ((size_t)(q - p) == file_len && !strncmp (p, filename, q - p))
774 found = 1;
777 else if (!strncmp (p, "type/", 5))
779 int res;
780 p += 5;
781 res = regex_check_type (filename_vpath, p, &have_type);
782 if (res == 1)
783 found = 1;
784 if (res == -1)
785 error_flag = 1; /* leave it if file cannot be opened */
787 else if (!strncmp (p, "default/", 8))
789 found = 1;
791 *q = c;
792 p = q;
793 if (!*p)
794 break;
796 else
797 { /* List of actions */
798 p = q;
799 q = strchr (p, '\n');
800 if (q == NULL)
801 q = strchr (p, 0);
802 if (found && !error_flag)
804 r = strchr (p, '=');
805 if (r != NULL)
807 c = *r;
808 *r = 0;
809 if (strcmp (p, "Include") == 0)
811 char *t;
813 include_target = p + 8;
814 t = strchr (include_target, '\n');
815 if (t)
816 *t = 0;
817 include_target_len = strlen (include_target);
818 if (t)
819 *t = '\n';
821 *r = c;
822 p = q;
823 found = 0;
825 if (!*p)
826 break;
827 continue;
829 if (!strcmp (action, p))
831 *r = c;
832 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
834 /* Empty commands just stop searching
835 * through, they don't do anything
837 * We need to copy the filename because exec_extension
838 * may end up invoking update_panels thus making the
839 * filename parameter invalid (ie, most of the time,
840 * we get filename as a pointer from current_panel->dir).
842 if (p < q)
844 char *filename_copy = g_strdup (filename);
846 exec_extension (filename_copy, r + 1, move_dir, view_at_line_number);
847 g_free (filename_copy);
849 ret = 1;
851 break;
853 else
854 *r = c;
857 p = q;
858 if (!*p)
859 break;
862 g_free (filename);
863 if (error_flag)
864 return -1;
865 return ret;
868 /* --------------------------------------------------------------------------------------------- */