Fix of memory leak in TreeStore::check_name destruction.
[midnight-commander.git] / src / filemanager / ext.c
blob179e616f5f7186084f0d2318eda5a7e2c2d3fc00
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 #ifdef HAVE_CHARSET
50 #include "lib/charsets.h" /* get_codepage_index */
51 #endif
53 #include "src/setup.h" /* use_file_to_check_type */
54 #include "src/execute.h"
55 #include "src/history.h"
56 #include "src/main.h" /* do_cd */
58 #include "src/consaver/cons.saver.h"
59 #include "src/viewer/mcviewer.h"
61 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h" /* do_set_codepage */
63 #endif
65 #include "usermenu.h"
67 #include "ext.h"
69 /*** global variables ****************************************************************************/
71 /*** file scope macro definitions ****************************************************************/
73 #ifdef FILE_L
74 #define FILE_CMD "file -L "
75 #else
76 #define FILE_CMD "file "
77 #endif
79 /*** file scope type declarations ****************************************************************/
81 typedef char *(*quote_func_t) (const char *name, int quote_percent);
83 /*** file scope variables ************************************************************************/
85 /* This variable points to a copy of the mc.ext file in memory
86 * With this we avoid loading/parsing the file each time we
87 * need it
89 static char *data = NULL;
91 /*** file scope functions ************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
94 static void
95 exec_extension (const char *filename, const char *lc_data, int *move_dir, int start_line)
97 vfs_path_t *file_name_vpath;
98 int cmd_file_fd;
99 FILE *cmd_file;
100 char *cmd = NULL;
101 int expand_prefix_found = 0;
102 int parameter_found = 0;
103 char lc_prompt[80];
104 int run_view = 0;
105 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
106 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
107 int written_nonspace = 0;
108 int is_cd = 0;
109 char buffer[1024];
110 char *p = 0;
111 vfs_path_t *localcopy_vpath = NULL;
112 int do_local_copy;
113 time_t localmtime = 0;
114 struct stat mystat;
115 quote_func_t quote_func = name_quote;
116 vfs_path_t *vpath;
118 g_return_if_fail (filename != NULL);
119 g_return_if_fail (lc_data != NULL);
121 vpath = vfs_path_from_str (filename);
123 /* Avoid making a local copy if we are doing a cd */
124 do_local_copy = vfs_file_is_local (vpath) ? 0 : 1;
127 * All commands should be run in /bin/sh regardless of user shell.
128 * To do that, create temporary shell script and run it.
129 * Sometimes it's not needed (e.g. for %cd and %view commands),
130 * but it's easier to create it anyway.
132 cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcext", SCRIPT_SUFFIX);
134 if (cmd_file_fd == -1)
136 message (D_ERROR, MSG_ERROR,
137 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
138 goto ret;
141 cmd_file = fdopen (cmd_file_fd, "w");
142 fputs ("#! /bin/sh\n", cmd_file);
144 lc_prompt[0] = '\0';
145 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
147 if (parameter_found)
149 if (*lc_data == '}')
151 char *parameter;
153 parameter_found = 0;
154 parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
155 if (parameter == NULL)
157 /* User canceled */
158 fclose (cmd_file);
159 mc_unlink (file_name_vpath);
160 if (localcopy_vpath != NULL)
162 mc_ungetlocalcopy (vpath, localcopy_vpath, FALSE);
163 vfs_path_free (localcopy_vpath);
165 goto ret;
167 fputs (parameter, cmd_file);
168 written_nonspace = 1;
169 g_free (parameter);
171 else
173 size_t len = strlen (lc_prompt);
175 if (len < sizeof (lc_prompt) - 1)
177 lc_prompt[len] = *lc_data;
178 lc_prompt[len + 1] = '\0';
182 else if (expand_prefix_found)
184 expand_prefix_found = 0;
185 if (*lc_data == '{')
186 parameter_found = 1;
187 else
189 int i;
190 char *v;
192 i = check_format_view (lc_data);
193 if (i != 0)
195 lc_data += i - 1;
196 run_view = 1;
198 else
200 i = check_format_cd (lc_data);
201 if (i > 0)
203 is_cd = 1;
204 quote_func = fake_name_quote;
205 do_local_copy = 0;
206 p = buffer;
207 lc_data += i - 1;
209 else
211 i = check_format_var (lc_data, &v);
212 if (i > 0 && v != NULL)
214 fputs (v, cmd_file);
215 g_free (v);
216 lc_data += i;
218 else
220 char *text;
222 if (*lc_data != 'f')
223 text = expand_format (NULL, *lc_data, !is_cd);
224 else
226 if (do_local_copy)
228 localcopy_vpath = mc_getlocalcopy (vpath);
229 if (localcopy_vpath == NULL)
231 fclose (cmd_file);
232 mc_unlink (file_name_vpath);
233 goto ret;
235 mc_stat (localcopy_vpath, &mystat);
236 localmtime = mystat.st_mtime;
237 text =
238 quote_func (vfs_path_get_last_path_str (localcopy_vpath),
241 else
243 const vfs_path_element_t *path_element;
245 path_element = vfs_path_get_by_index (vpath, -1);
246 text = quote_func (path_element->path, 0);
250 if (!is_cd)
251 fputs (text, cmd_file);
252 else
254 strcpy (p, text);
255 p = strchr (p, 0);
258 g_free (text);
259 written_nonspace = 1;
265 else if (*lc_data == '%')
266 expand_prefix_found = 1;
267 else
269 if (*lc_data != ' ' && *lc_data != '\t')
270 written_nonspace = 1;
271 if (is_cd)
272 *(p++) = *lc_data;
273 else
274 fputc (*lc_data, cmd_file);
276 } /* for */
279 * Make the script remove itself when it finishes.
280 * Don't do it for the viewer - it may need to rerun the script,
281 * so we clean up after calling view().
283 if (!run_view)
285 char *file_name;
287 file_name = vfs_path_to_str (file_name_vpath);
288 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
289 g_free (file_name);
292 fclose (cmd_file);
294 if ((run_view && !written_nonspace) || is_cd)
296 mc_unlink (file_name_vpath);
297 vfs_path_free (file_name_vpath);
298 file_name_vpath = NULL;
300 else
302 char *file_name;
304 file_name = vfs_path_to_str (file_name_vpath);
305 /* Set executable flag on the command file ... */
306 mc_chmod (file_name_vpath, S_IRWXU);
307 /* ... but don't rely on it - run /bin/sh explicitly */
308 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
309 g_free (file_name);
312 if (run_view)
314 mcview_ret_t ret;
316 mcview_altered_hex_mode = 0;
317 mcview_altered_nroff_flag = 0;
318 if (def_hex_mode != mcview_default_hex_mode)
319 changed_hex_mode = 1;
320 if (def_nroff_flag != mcview_default_nroff_flag)
321 changed_nroff_flag = 1;
323 /* If we've written whitespace only, then just load filename
324 * into view
326 if (written_nonspace)
328 ret = mcview_viewer (cmd, vpath, start_line);
329 mc_unlink (file_name_vpath);
331 else
332 ret = mcview_viewer (NULL, vpath, start_line);
334 if (move_dir != NULL)
335 switch (ret)
337 case MCVIEW_WANT_NEXT:
338 *move_dir = 1;
339 break;
340 case MCVIEW_WANT_PREV:
341 *move_dir = -1;
342 break;
343 default:
344 *move_dir = 0;
347 if (changed_hex_mode && !mcview_altered_hex_mode)
348 mcview_default_hex_mode = def_hex_mode;
349 if (changed_nroff_flag && !mcview_altered_nroff_flag)
350 mcview_default_nroff_flag = def_nroff_flag;
352 dialog_switch_process_pending ();
354 else if (is_cd)
356 char *q;
357 vfs_path_t *p_vpath;
359 *p = 0;
360 p = buffer;
361 /* while (*p == ' ' && *p == '\t')
362 * p++;
364 /* Search last non-space character. Start search at the end in order
365 not to short filenames containing spaces. */
366 q = p + strlen (p) - 1;
367 while (q >= p && (*q == ' ' || *q == '\t'))
368 q--;
369 q[1] = 0;
371 p_vpath = vfs_path_from_str_flags (p, VPF_NO_CANON);
372 do_cd (p_vpath, cd_parse_command);
373 vfs_path_free (p_vpath);
375 else
377 shell_execute (cmd, EXECUTE_INTERNAL);
378 if (mc_global.tty.console_flag != '\0')
380 handle_console (CONSOLE_SAVE);
381 if (output_lines && mc_global.keybar_visible)
382 show_console_contents (output_start_y,
383 LINES - mc_global.keybar_visible -
384 output_lines - 1, LINES - mc_global.keybar_visible - 1);
388 g_free (cmd);
390 if (localcopy_vpath != NULL)
392 mc_stat (localcopy_vpath, &mystat);
393 mc_ungetlocalcopy (vpath, localcopy_vpath, localmtime != mystat.st_mtime);
394 vfs_path_free (localcopy_vpath);
396 ret:
397 vfs_path_free (file_name_vpath);
398 vfs_path_free (vpath);
401 /* --------------------------------------------------------------------------------------------- */
403 * Run cmd_file with args, put result into buf.
404 * If error, put '\0' into buf[0]
405 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
407 * NOTES: buf is null-terminated string.
410 static int
411 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
413 gboolean read_bytes = FALSE;
414 char *command;
415 FILE *f;
417 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
418 f = popen (command, "r");
419 g_free (command);
421 if (f != NULL)
423 #ifdef __QNXNTO__
424 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
426 (void) pclose (f);
427 return -1;
429 #endif
430 read_bytes = (fgets (buf, buflen, f) != NULL);
431 if (!read_bytes)
432 buf[0] = '\0'; /* Paranoid termination */
433 pclose (f);
435 else
437 buf[0] = '\0'; /* Paranoid termination */
438 return -1;
441 buf[buflen - 1] = '\0';
443 return read_bytes ? 1 : 0;
446 /* --------------------------------------------------------------------------------------------- */
448 * Run the "file" command on the local file.
449 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
452 static int
453 get_file_type_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
455 char *tmp;
456 int ret;
458 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
459 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
460 g_free (tmp);
462 return ret;
465 /* --------------------------------------------------------------------------------------------- */
467 * Run the "enca" command on the local file.
468 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
471 #ifdef HAVE_CHARSET
472 static int
473 get_file_encoding_local (const vfs_path_t * filename_vpath, char *buf, int buflen)
475 char *tmp, *lang, *args;
476 int ret;
478 tmp = name_quote (vfs_path_get_last_path_str (filename_vpath), 0);
479 lang = name_quote (autodetect_codeset, 0);
480 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
482 ret = get_popen_information ("enca", args, buf, buflen);
484 g_free (args);
485 g_free (lang);
486 g_free (tmp);
488 return ret;
490 #endif /* HAVE_CHARSET */
492 /* --------------------------------------------------------------------------------------------- */
494 * Invoke the "file" command on the file and match its output against PTR.
495 * have_type is a flag that is set if we already have tried to determine
496 * the type of that file.
497 * Return 1 for match, 0 for no match, -1 errors.
500 static int
501 regex_check_type (const vfs_path_t * filename_vpath, const char *ptr, int *have_type)
503 int found = 0;
505 /* Following variables are valid if *have_type is 1 */
506 static char content_string[2048];
507 #ifdef HAVE_CHARSET
508 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
509 #endif
510 static size_t content_shift = 0;
511 static int got_data = 0;
513 if (!use_file_to_check_type)
514 return 0;
516 if (*have_type == 0)
518 vfs_path_t *localfile_vpath;
519 const char *realname; /* name used with "file" */
521 #ifdef HAVE_CHARSET
522 int got_encoding_data;
523 #endif /* HAVE_CHARSET */
525 /* Don't repeate even unsuccessful checks */
526 *have_type = 1;
528 localfile_vpath = mc_getlocalcopy (filename_vpath);
529 if (localfile_vpath == NULL)
530 return -1;
532 realname = vfs_path_get_last_path_str (localfile_vpath);
534 #ifdef HAVE_CHARSET
535 got_encoding_data = is_autodetect_codeset_enabled
536 ? get_file_encoding_local (localfile_vpath, encoding_id, sizeof (encoding_id)) : 0;
538 if (got_encoding_data > 0)
540 char *pp;
541 int cp_id;
543 pp = strchr (encoding_id, '\n');
544 if (pp != NULL)
545 *pp = '\0';
547 cp_id = get_codepage_index (encoding_id);
548 if (cp_id == -1)
549 cp_id = default_source_codepage;
551 do_set_codepage (cp_id);
553 #endif /* HAVE_CHARSET */
555 mc_ungetlocalcopy (filename_vpath, localfile_vpath, FALSE);
557 got_data = get_file_type_local (localfile_vpath, content_string, sizeof (content_string));
559 if (got_data > 0)
561 char *pp;
562 size_t real_len;
564 pp = strchr (content_string, '\n');
565 if (pp != NULL)
566 *pp = '\0';
568 real_len = strlen (realname);
570 if (strncmp (content_string, realname, real_len) == 0)
572 /* Skip "realname: " */
573 content_shift = real_len;
574 if (content_string[content_shift] == ':')
576 /* Solaris' file prints tab(s) after ':' */
577 for (content_shift++;
578 content_string[content_shift] == ' '
579 || content_string[content_shift] == '\t'; content_shift++)
584 else
586 /* No data */
587 content_string[0] = '\0';
589 vfs_path_free (localfile_vpath);
592 if (got_data == -1)
593 return -1;
595 if (content_string[0] != '\0'
596 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX))
598 found = 1;
601 return found;
604 /* --------------------------------------------------------------------------------------------- */
605 /*** public functions ****************************************************************************/
606 /* --------------------------------------------------------------------------------------------- */
608 void
609 flush_extension_file (void)
611 g_free (data);
612 data = NULL;
615 /* --------------------------------------------------------------------------------------------- */
617 * The second argument is action, i.e. Open, View or Edit
619 * This function returns:
621 * -1 for a failure or user interrupt
622 * 0 if no command was run
623 * 1 if some command was run
625 * If action == "View" then a parameter is checked in the form of "View:%d",
626 * if the value for %d exists, then the viewer is started up at that line number.
630 regex_command (const vfs_path_t * filename_vpath, const char *action, int *move_dir)
632 char *filename, *p, *q, *r, c;
633 size_t file_len;
634 int found = 0;
635 int error_flag = 0;
636 int ret = 0;
637 struct stat mystat;
638 int view_at_line_number;
639 char *include_target;
640 int include_target_len;
641 int have_type = 0; /* Flag used by regex_check_type() */
643 /* Check for the special View:%d parameter */
644 if (strncmp (action, "View:", 5) == 0)
646 view_at_line_number = atoi (action + 5);
647 action = "View";
649 else
651 view_at_line_number = 0;
654 if (data == NULL)
656 char *extension_file;
657 int mc_user_ext = 1;
658 int home_error = 0;
660 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
661 if (!exist_file (extension_file))
663 g_free (extension_file);
664 check_stock_mc_ext:
665 extension_file = mc_build_filename (mc_global.sysconfig_dir, MC_LIB_EXT, NULL);
666 if (!exist_file (extension_file))
668 g_free (extension_file);
669 extension_file = mc_build_filename (mc_global.share_data_dir, MC_LIB_EXT, NULL);
671 mc_user_ext = 0;
674 g_file_get_contents (extension_file, &data, NULL, NULL);
675 g_free (extension_file);
676 if (data == NULL)
677 return 0;
679 if (!strstr (data, "default/"))
681 if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/"))
683 g_free (data);
684 data = NULL;
685 if (mc_user_ext)
687 home_error = 1;
688 goto check_stock_mc_ext;
690 else
692 char *title = g_strdup_printf (_(" %s%s file error"),
693 mc_global.sysconfig_dir, MC_LIB_EXT);
694 message (D_ERROR, title, _("The format of the %smc.ext "
695 "file has changed with version 3.0. It seems that "
696 "the installation failed. Please fetch a fresh "
697 "copy from the Midnight Commander package."),
698 mc_global.sysconfig_dir);
699 g_free (title);
700 return 0;
704 if (home_error)
706 char *filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
707 char *title = g_strdup_printf (_("%s file error"), filebind_filename);
708 message (D_ERROR, title,
709 _("The format of the %s file has "
710 "changed with version 3.0. You may either want to copy "
711 "it from %smc.ext or use that file as an example of how to write it."),
712 filebind_filename, mc_global.sysconfig_dir);
713 g_free (filebind_filename);
714 g_free (title);
718 mc_stat (filename_vpath, &mystat);
720 include_target = NULL;
721 include_target_len = 0;
722 filename = vfs_path_to_str (filename_vpath);
723 file_len = vfs_path_len (filename_vpath);
725 for (p = data; *p; p++)
727 for (q = p; *q == ' ' || *q == '\t'; q++);
728 if (*q == '\n' || !*q)
729 p = q; /* empty line */
730 if (*p == '#') /* comment */
731 while (*p && *p != '\n')
732 p++;
733 if (*p == '\n')
734 continue;
735 if (!*p)
736 break;
737 if (p == q)
738 { /* i.e. starts in the first column, should be
739 * keyword/descNL
741 found = 0;
742 q = strchr (p, '\n');
743 if (q == NULL)
744 q = strchr (p, 0);
745 c = *q;
746 *q = 0;
747 if (include_target)
749 if ((strncmp (p, "include/", 8) == 0)
750 && (strncmp (p + 8, include_target, include_target_len) == 0))
751 found = 1;
753 else if (!strncmp (p, "regex/", 6))
755 p += 6;
756 /* Do not transform shell patterns, you can use shell/ for
757 * that
759 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
760 found = 1;
762 else if (!strncmp (p, "directory/", 10))
764 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
765 found = 1;
767 else if (!strncmp (p, "shell/", 6))
769 p += 6;
770 if (*p == '.' && file_len >= (size_t) (q - p))
772 if (!strncmp (p, filename + file_len - (q - p), q - p))
773 found = 1;
775 else
777 if ((size_t) (q - p) == file_len && !strncmp (p, filename, q - p))
778 found = 1;
781 else if (!strncmp (p, "type/", 5))
783 int res;
784 p += 5;
785 res = regex_check_type (filename_vpath, p, &have_type);
786 if (res == 1)
787 found = 1;
788 if (res == -1)
789 error_flag = 1; /* leave it if file cannot be opened */
791 else if (!strncmp (p, "default/", 8))
793 found = 1;
795 *q = c;
796 p = q;
797 if (!*p)
798 break;
800 else
801 { /* List of actions */
802 p = q;
803 q = strchr (p, '\n');
804 if (q == NULL)
805 q = strchr (p, 0);
806 if (found && !error_flag)
808 r = strchr (p, '=');
809 if (r != NULL)
811 c = *r;
812 *r = 0;
813 if (strcmp (p, "Include") == 0)
815 char *t;
817 include_target = p + 8;
818 t = strchr (include_target, '\n');
819 if (t)
820 *t = 0;
821 include_target_len = strlen (include_target);
822 if (t)
823 *t = '\n';
825 *r = c;
826 p = q;
827 found = 0;
829 if (!*p)
830 break;
831 continue;
833 if (!strcmp (action, p))
835 *r = c;
836 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
838 /* Empty commands just stop searching
839 * through, they don't do anything
841 * We need to copy the filename because exec_extension
842 * may end up invoking update_panels thus making the
843 * filename parameter invalid (ie, most of the time,
844 * we get filename as a pointer from current_panel->dir).
846 if (p < q)
848 char *filename_copy = g_strdup (filename);
850 exec_extension (filename_copy, r + 1, move_dir, view_at_line_number);
851 g_free (filename_copy);
853 ret = 1;
855 break;
857 else
858 *r = c;
861 p = q;
862 if (!*p)
863 break;
866 g_free (filename);
867 if (error_flag)
868 return -1;
869 return ret;
872 /* --------------------------------------------------------------------------------------------- */