Reorder fields in mc_global struct and change type for some of them.
[midnight-commander.git] / src / filemanager / ext.c
blobca6196582ae232683e7d2abe9f62647e5220da5c
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 char *file_name;
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 char *localcopy = 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 if (!vfs_file_is_local (vpath))
123 do_local_copy = 1;
124 else
125 do_local_copy = 0;
128 * All commands should be run in /bin/sh regardless of user shell.
129 * To do that, create temporary shell script and run it.
130 * Sometimes it's not needed (e.g. for %cd and %view commands),
131 * but it's easier to create it anyway.
133 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
135 if (cmd_file_fd == -1)
137 message (D_ERROR, MSG_ERROR,
138 _("Cannot create temporary command file\n%s"), unix_error_string (errno));
139 return;
142 cmd_file = fdopen (cmd_file_fd, "w");
143 fputs ("#! /bin/sh\n", cmd_file);
145 lc_prompt[0] = '\0';
146 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
148 if (parameter_found)
150 if (*lc_data == '}')
152 char *parameter;
154 parameter_found = 0;
155 parameter = input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
156 if (parameter == NULL)
158 /* User canceled */
159 fclose (cmd_file);
160 unlink (file_name);
161 if (localcopy)
163 mc_ungetlocalcopy (filename, localcopy, 0);
164 g_free (localcopy);
166 g_free (file_name);
167 vfs_path_free (vpath);
168 return;
170 fputs (parameter, cmd_file);
171 written_nonspace = 1;
172 g_free (parameter);
174 else
176 size_t len = strlen (lc_prompt);
178 if (len < sizeof (lc_prompt) - 1)
180 lc_prompt[len] = *lc_data;
181 lc_prompt[len + 1] = '\0';
185 else if (expand_prefix_found)
187 expand_prefix_found = 0;
188 if (*lc_data == '{')
189 parameter_found = 1;
190 else
192 int i;
193 char *v;
195 i = check_format_view (lc_data);
196 if (i != 0)
198 lc_data += i - 1;
199 run_view = 1;
201 else
203 i = check_format_cd (lc_data);
204 if (i > 0)
206 is_cd = 1;
207 quote_func = fake_name_quote;
208 do_local_copy = 0;
209 p = buffer;
210 lc_data += i - 1;
212 else
214 i = check_format_var (lc_data, &v);
215 if (i > 0 && v != NULL)
217 fputs (v, cmd_file);
218 g_free (v);
219 lc_data += i;
221 else
223 char *text;
225 if (*lc_data != 'f')
226 text = expand_format (NULL, *lc_data, !is_cd);
227 else
229 if (do_local_copy)
231 localcopy = mc_getlocalcopy (filename);
232 if (localcopy == NULL)
234 fclose (cmd_file);
235 unlink (file_name);
236 g_free (file_name);
237 vfs_path_free (vpath);
238 return;
240 mc_stat (localcopy, &mystat);
241 localmtime = mystat.st_mtime;
242 text = quote_func (localcopy, 0);
244 else
246 vfs_path_element_t *path_element;
247 path_element = vfs_path_get_by_index (vpath, -1);
248 text = quote_func (path_element->path, 0);
252 if (!is_cd)
253 fputs (text, cmd_file);
254 else
256 strcpy (p, text);
257 p = strchr (p, 0);
260 g_free (text);
261 written_nonspace = 1;
267 else if (*lc_data == '%')
268 expand_prefix_found = 1;
269 else
271 if (*lc_data != ' ' && *lc_data != '\t')
272 written_nonspace = 1;
273 if (is_cd)
274 *(p++) = *lc_data;
275 else
276 fputc (*lc_data, cmd_file);
278 } /* for */
281 * Make the script remove itself when it finishes.
282 * Don't do it for the viewer - it may need to rerun the script,
283 * so we clean up after calling view().
285 if (!run_view)
286 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
288 fclose (cmd_file);
290 if ((run_view && !written_nonspace) || is_cd)
292 unlink (file_name);
293 g_free (file_name);
294 file_name = NULL;
296 else
298 /* Set executable flag on the command file ... */
299 chmod (file_name, S_IRWXU);
300 /* ... but don't rely on it - run /bin/sh explicitly */
301 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
304 if (run_view)
306 mcview_ret_t ret;
308 mcview_altered_hex_mode = 0;
309 mcview_altered_nroff_flag = 0;
310 if (def_hex_mode != mcview_default_hex_mode)
311 changed_hex_mode = 1;
312 if (def_nroff_flag != mcview_default_nroff_flag)
313 changed_nroff_flag = 1;
315 /* If we've written whitespace only, then just load filename
316 * into view
318 if (written_nonspace)
320 ret = mcview_viewer (cmd, filename, start_line);
321 unlink (file_name);
323 else
324 ret = mcview_viewer (NULL, filename, start_line);
326 if (move_dir != NULL)
327 switch (ret)
329 case MCVIEW_WANT_NEXT:
330 *move_dir = 1;
331 break;
332 case MCVIEW_WANT_PREV:
333 *move_dir = -1;
334 break;
335 default:
336 *move_dir = 0;
339 if (changed_hex_mode && !mcview_altered_hex_mode)
340 mcview_default_hex_mode = def_hex_mode;
341 if (changed_nroff_flag && !mcview_altered_nroff_flag)
342 mcview_default_nroff_flag = def_nroff_flag;
344 dialog_switch_process_pending ();
346 else if (is_cd)
348 char *q;
349 *p = 0;
350 p = buffer;
351 /* while (*p == ' ' && *p == '\t')
352 * p++;
354 /* Search last non-space character. Start search at the end in order
355 not to short filenames containing spaces. */
356 q = p + strlen (p) - 1;
357 while (q >= p && (*q == ' ' || *q == '\t'))
358 q--;
359 q[1] = 0;
360 do_cd (p, cd_parse_command);
362 else
364 shell_execute (cmd, EXECUTE_INTERNAL);
365 if (mc_global.tty.console_flag != '\0')
367 handle_console (CONSOLE_SAVE);
368 if (output_lines && mc_global.keybar_visible)
369 show_console_contents (output_start_y,
370 LINES - mc_global.keybar_visible -
371 output_lines - 1, LINES - mc_global.keybar_visible - 1);
375 g_free (file_name);
376 g_free (cmd);
378 if (localcopy)
380 mc_stat (localcopy, &mystat);
381 mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime);
382 g_free (localcopy);
384 vfs_path_free (vpath);
387 /* --------------------------------------------------------------------------------------------- */
389 * Run cmd_file with args, put result into buf.
390 * If error, put '\0' into buf[0]
391 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
393 * NOTES: buf is null-terminated string.
396 static int
397 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
399 gboolean read_bytes = FALSE;
400 char *command;
401 FILE *f;
403 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
404 f = popen (command, "r");
405 g_free (command);
407 if (f != NULL)
409 #ifdef __QNXNTO__
410 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
412 (void) pclose (f);
413 return -1;
415 #endif
416 read_bytes = (fgets (buf, buflen, f) != NULL);
417 if (!read_bytes)
418 buf[0] = '\0'; /* Paranoid termination */
419 pclose (f);
421 else
423 buf[0] = '\0'; /* Paranoid termination */
424 return -1;
427 buf[buflen - 1] = '\0';
429 return read_bytes ? 1 : 0;
432 /* --------------------------------------------------------------------------------------------- */
434 * Run the "file" command on the local file.
435 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
438 static int
439 get_file_type_local (const char *filename, char *buf, int buflen)
441 char *tmp;
442 int ret;
444 tmp = name_quote (filename, 0);
445 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
446 g_free (tmp);
448 return ret;
451 /* --------------------------------------------------------------------------------------------- */
453 * Run the "enca" command on the local file.
454 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
457 #ifdef HAVE_CHARSET
458 static int
459 get_file_encoding_local (const char *filename, char *buf, int buflen)
461 char *tmp, *lang, *args;
462 int ret;
464 tmp = name_quote (filename, 0);
465 lang = name_quote (autodetect_codeset, 0);
466 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
468 ret = get_popen_information ("enca", args, buf, buflen);
470 g_free (args);
471 g_free (lang);
472 g_free (tmp);
474 return ret;
476 #endif /* HAVE_CHARSET */
478 /* --------------------------------------------------------------------------------------------- */
480 * Invoke the "file" command on the file and match its output against PTR.
481 * have_type is a flag that is set if we already have tried to determine
482 * the type of that file.
483 * Return 1 for match, 0 for no match, -1 errors.
486 static int
487 regex_check_type (const char *filename, const char *ptr, int *have_type)
489 int found = 0;
491 /* Following variables are valid if *have_type is 1 */
492 static char content_string[2048];
493 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
494 static size_t content_shift = 0;
495 static int got_data = 0;
497 if (!use_file_to_check_type)
498 return 0;
500 if (*have_type == 0)
502 char *realname; /* name used with "file" */
503 char *localfile;
505 #ifdef HAVE_CHARSET
506 int got_encoding_data;
507 #endif /* HAVE_CHARSET */
509 /* Don't repeate even unsuccessful checks */
510 *have_type = 1;
512 localfile = mc_getlocalcopy (filename);
513 if (localfile == NULL)
514 return -1;
516 realname = localfile;
518 #ifdef HAVE_CHARSET
519 got_encoding_data = is_autodetect_codeset_enabled
520 ? get_file_encoding_local (localfile, encoding_id, sizeof (encoding_id)) : 0;
522 if (got_encoding_data > 0)
524 char *pp;
525 int cp_id;
527 pp = strchr (encoding_id, '\n');
528 if (pp != NULL)
529 *pp = '\0';
531 cp_id = get_codepage_index (encoding_id);
532 if (cp_id == -1)
533 cp_id = default_source_codepage;
535 do_set_codepage (cp_id);
537 #endif /* HAVE_CHARSET */
539 mc_ungetlocalcopy (filename, localfile, 0);
541 got_data = get_file_type_local (localfile, content_string, sizeof (content_string));
543 if (got_data > 0)
545 char *pp;
546 size_t real_len;
548 pp = strchr (content_string, '\n');
549 if (pp != NULL)
550 *pp = '\0';
552 real_len = strlen (realname);
554 if (strncmp (content_string, realname, real_len) == 0)
556 /* Skip "realname: " */
557 content_shift = real_len;
558 if (content_string[content_shift] == ':')
560 /* Solaris' file prints tab(s) after ':' */
561 for (content_shift++;
562 content_string[content_shift] == ' '
563 || content_string[content_shift] == '\t'; content_shift++)
568 else
570 /* No data */
571 content_string[0] = '\0';
573 g_free (realname);
576 if (got_data == -1)
577 return -1;
579 if (content_string[0] != '\0'
580 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX))
582 found = 1;
585 return found;
588 /* --------------------------------------------------------------------------------------------- */
589 /*** public functions ****************************************************************************/
590 /* --------------------------------------------------------------------------------------------- */
592 void
593 flush_extension_file (void)
595 g_free (data);
596 data = NULL;
599 /* --------------------------------------------------------------------------------------------- */
601 * The second argument is action, i.e. Open, View or Edit
603 * This function returns:
605 * -1 for a failure or user interrupt
606 * 0 if no command was run
607 * 1 if some command was run
609 * If action == "View" then a parameter is checked in the form of "View:%d",
610 * if the value for %d exists, then the viewer is started up at that line number.
614 regex_command (const char *filename, const char *action, int *move_dir)
616 char *p, *q, *r, c;
617 int file_len = strlen (filename);
618 int found = 0;
619 int error_flag = 0;
620 int ret = 0;
621 struct stat mystat;
622 int view_at_line_number;
623 char *include_target;
624 int include_target_len;
625 int have_type = 0; /* Flag used by regex_check_type() */
627 /* Check for the special View:%d parameter */
628 if (strncmp (action, "View:", 5) == 0)
630 view_at_line_number = atoi (action + 5);
631 action = "View";
633 else
635 view_at_line_number = 0;
638 if (data == NULL)
640 char *extension_file;
641 int mc_user_ext = 1;
642 int home_error = 0;
644 extension_file = mc_config_get_full_path (MC_FILEBIND_FILE);
645 if (!exist_file (extension_file))
647 g_free (extension_file);
648 check_stock_mc_ext:
649 extension_file = concat_dir_and_file (mc_global.sysconfig_dir, MC_LIB_EXT);
650 if (!exist_file (extension_file))
652 g_free (extension_file);
653 extension_file = concat_dir_and_file (mc_global.share_data_dir, MC_LIB_EXT);
655 mc_user_ext = 0;
658 g_file_get_contents (extension_file, &data, NULL, NULL);
659 g_free (extension_file);
660 if (data == NULL)
661 return 0;
663 if (!strstr (data, "default/"))
665 if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/"))
667 g_free (data);
668 data = NULL;
669 if (mc_user_ext)
671 home_error = 1;
672 goto check_stock_mc_ext;
674 else
676 char *title = g_strdup_printf (_(" %s%s file error"),
677 mc_global.sysconfig_dir, MC_LIB_EXT);
678 message (D_ERROR, title, _("The format of the %smc.ext "
679 "file has changed with version 3.0. It seems that "
680 "the installation failed. Please fetch a fresh "
681 "copy from the Midnight Commander package."),
682 mc_global.sysconfig_dir);
683 g_free (title);
684 return 0;
688 if (home_error)
690 char *filebind_filename = mc_config_get_full_path (MC_FILEBIND_FILE);
691 char *title = g_strdup_printf (_("%s file error"), filebind_filename);
692 message (D_ERROR, title,
693 _("The format of the %s file has "
694 "changed with version 3.0. You may either want to copy "
695 "it from %smc.ext or use that file as an example of how to write it."),
696 filebind_filename, mc_global.sysconfig_dir);
697 g_free (filebind_filename);
698 g_free (title);
701 mc_stat (filename, &mystat);
703 include_target = NULL;
704 include_target_len = 0;
705 for (p = data; *p; p++)
707 for (q = p; *q == ' ' || *q == '\t'; q++);
708 if (*q == '\n' || !*q)
709 p = q; /* empty line */
710 if (*p == '#') /* comment */
711 while (*p && *p != '\n')
712 p++;
713 if (*p == '\n')
714 continue;
715 if (!*p)
716 break;
717 if (p == q)
718 { /* i.e. starts in the first column, should be
719 * keyword/descNL
721 found = 0;
722 q = strchr (p, '\n');
723 if (q == NULL)
724 q = strchr (p, 0);
725 c = *q;
726 *q = 0;
727 if (include_target)
729 if ((strncmp (p, "include/", 8) == 0)
730 && (strncmp (p + 8, include_target, include_target_len) == 0))
731 found = 1;
733 else if (!strncmp (p, "regex/", 6))
735 p += 6;
736 /* Do not transform shell patterns, you can use shell/ for
737 * that
739 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
740 found = 1;
742 else if (!strncmp (p, "directory/", 10))
744 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
745 found = 1;
747 else if (!strncmp (p, "shell/", 6))
749 p += 6;
750 if (*p == '.' && file_len >= (q - p))
752 if (!strncmp (p, filename + file_len - (q - p), q - p))
753 found = 1;
755 else
757 if (q - p == file_len && !strncmp (p, filename, q - p))
758 found = 1;
761 else if (!strncmp (p, "type/", 5))
763 int res;
764 p += 5;
765 res = regex_check_type (filename, p, &have_type);
766 if (res == 1)
767 found = 1;
768 if (res == -1)
769 error_flag = 1; /* leave it if file cannot be opened */
771 else if (!strncmp (p, "default/", 8))
773 found = 1;
775 *q = c;
776 p = q;
777 if (!*p)
778 break;
780 else
781 { /* List of actions */
782 p = q;
783 q = strchr (p, '\n');
784 if (q == NULL)
785 q = strchr (p, 0);
786 if (found && !error_flag)
788 r = strchr (p, '=');
789 if (r != NULL)
791 c = *r;
792 *r = 0;
793 if (strcmp (p, "Include") == 0)
795 char *t;
797 include_target = p + 8;
798 t = strchr (include_target, '\n');
799 if (t)
800 *t = 0;
801 include_target_len = strlen (include_target);
802 if (t)
803 *t = '\n';
805 *r = c;
806 p = q;
807 found = 0;
809 if (!*p)
810 break;
811 continue;
813 if (!strcmp (action, p))
815 *r = c;
816 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
818 /* Empty commands just stop searching
819 * through, they don't do anything
821 * We need to copy the filename because exec_extension
822 * may end up invoking update_panels thus making the
823 * filename parameter invalid (ie, most of the time,
824 * we get filename as a pointer from current_panel->dir).
826 if (p < q)
828 char *filename_copy = g_strdup (filename);
830 exec_extension (filename_copy, r + 1, move_dir, view_at_line_number);
831 g_free (filename_copy);
833 ret = 1;
835 break;
837 else
838 *r = c;
841 p = q;
842 if (!*p)
843 break;
846 if (error_flag)
847 return -1;
848 return ret;
851 /* --------------------------------------------------------------------------------------------- */