Ticket #30 (use external clipboard utility)
[midnight-commander.git] / src / ext.c
blob8bcf21e80e86577c72a0cf2748f52f9da046e30c
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 "ext.h"
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
62 * need it
64 static char *data = NULL;
66 void
67 flush_extension_file (void)
69 g_free (data);
70 data = NULL;
73 typedef char *(*quote_func_t) (const char *name, int quote_percent);
75 static void
76 exec_extension (const char *filename, const char *lc_data, int *move_dir, int start_line)
78 char *fn;
79 char *file_name;
80 int cmd_file_fd;
81 FILE *cmd_file;
82 char *cmd = NULL;
83 int expand_prefix_found = 0;
84 int parameter_found = 0;
85 char lc_prompt[80];
86 int run_view = 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;
90 int is_cd = 0;
91 char buffer[1024];
92 char *p = 0;
93 char *localcopy = NULL;
94 int do_local_copy;
95 time_t localmtime = 0;
96 struct stat mystat;
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))
104 do_local_copy = 1;
105 else
106 do_local_copy = 0;
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));
120 return;
123 cmd_file = fdopen (cmd_file_fd, "w");
124 fputs ("#! /bin/sh\n", cmd_file);
126 lc_prompt[0] = '\0';
127 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++)
129 if (parameter_found)
131 if (*lc_data == '}')
133 char *parameter;
135 parameter_found = 0;
136 parameter =
137 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
138 if (parameter == NULL)
140 /* User canceled */
141 fclose (cmd_file);
142 unlink (file_name);
143 if (localcopy)
145 mc_ungetlocalcopy (filename, localcopy, 0);
146 g_free (localcopy);
148 g_free (file_name);
149 return;
151 fputs (parameter, cmd_file);
152 written_nonspace = 1;
153 g_free (parameter);
155 else
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;
169 if (*lc_data == '{')
170 parameter_found = 1;
171 else
173 int i;
174 char *v;
176 i = check_format_view (lc_data);
177 if (i != 0)
179 lc_data += i - 1;
180 run_view = 1;
182 else
184 i = check_format_cd (lc_data);
185 if (i > 0)
187 is_cd = 1;
188 quote_func = fake_name_quote;
189 do_local_copy = 0;
190 p = buffer;
191 lc_data += i - 1;
193 else
195 i = check_format_var (lc_data, &v);
196 if (i > 0 && v != NULL)
198 fputs (v, cmd_file);
199 g_free (v);
200 lc_data += i;
202 else
204 char *text;
206 if (*lc_data != 'f')
207 text = expand_format (NULL, *lc_data, !is_cd);
208 else
210 if (do_local_copy)
212 localcopy = mc_getlocalcopy (filename);
213 if (localcopy == NULL)
215 fclose (cmd_file);
216 unlink (file_name);
217 g_free (file_name);
218 return;
220 mc_stat (localcopy, &mystat);
221 localmtime = mystat.st_mtime;
222 text = quote_func (localcopy, 0);
224 else
226 fn = vfs_canon_and_translate (filename);
227 text = quote_func (fn, 0);
228 g_free (fn);
232 if (!is_cd)
233 fputs (text, cmd_file);
234 else
236 strcpy (p, text);
237 p = strchr (p, 0);
240 g_free (text);
241 written_nonspace = 1;
247 else if (*lc_data == '%')
248 expand_prefix_found = 1;
249 else
251 if (*lc_data != ' ' && *lc_data != '\t')
252 written_nonspace = 1;
253 if (is_cd)
254 *(p++) = *lc_data;
255 else
256 fputc (*lc_data, cmd_file);
258 } /* for */
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().
265 if (!run_view)
266 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
268 fclose (cmd_file);
270 if ((run_view && !written_nonspace) || is_cd)
272 unlink (file_name);
273 g_free (file_name);
274 file_name = NULL;
276 else
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);
284 if (run_view)
286 mcview_altered_hex_mode = 0;
287 mcview_altered_nroff_flag = 0;
288 if (def_hex_mode != mcview_default_hex_mode)
289 changed_hex_mode = 1;
290 if (def_nroff_flag != mcview_default_nroff_flag)
291 changed_nroff_flag = 1;
293 /* If we've written whitespace only, then just load filename
294 * into view
296 if (written_nonspace)
298 mcview_viewer (cmd, filename, move_dir, start_line);
299 unlink (file_name);
301 else
303 mcview_viewer (NULL, filename, move_dir, start_line);
305 if (changed_hex_mode && !mcview_altered_hex_mode)
306 mcview_default_hex_mode = def_hex_mode;
307 if (changed_nroff_flag && !mcview_altered_nroff_flag)
308 mcview_default_nroff_flag = def_nroff_flag;
309 repaint_screen ();
311 else if (is_cd)
313 char *q;
314 *p = 0;
315 p = buffer;
316 /* while (*p == ' ' && *p == '\t')
317 * p++;
319 /* Search last non-space character. Start search at the end in order
320 not to short filenames containing spaces. */
321 q = p + strlen (p) - 1;
322 while (q >= p && (*q == ' ' || *q == '\t'))
323 q--;
324 q[1] = 0;
325 do_cd (p, cd_parse_command);
327 else
329 shell_execute (cmd, EXECUTE_INTERNAL);
330 if (console_flag)
332 handle_console (CONSOLE_SAVE);
333 if (output_lines && keybar_visible)
335 show_console_contents (output_start_y,
336 LINES - keybar_visible -
337 output_lines - 1, LINES - keybar_visible - 1);
342 g_free (file_name);
343 g_free (cmd);
345 if (localcopy)
347 mc_stat (localcopy, &mystat);
348 mc_ungetlocalcopy (filename, localcopy, localmtime != mystat.st_mtime);
349 g_free (localcopy);
353 #ifdef FILE_L
354 # define FILE_CMD "file -L "
355 #else
356 # define FILE_CMD "file "
357 #endif
360 * Run cmd_file with args, put result into buf.
361 * If error, put '\0' into buf[0]
362 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
364 * NOTES: buf is null-terminated string.
366 static int
367 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
369 gboolean read_bytes = FALSE;
370 char *command;
371 FILE *f;
373 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
374 f = popen (command, "r");
375 g_free (command);
377 if (f != NULL)
379 #ifdef __QNXNTO__
380 if (setvbuf (f, NULL, _IOFBF, 0) != 0)
382 (void) pclose (f);
383 return -1;
385 #endif
386 read_bytes = (fgets (buf, buflen, f) != NULL);
387 if (!read_bytes)
388 buf[0] = '\0'; /* Paranoid termination */
389 pclose (f);
391 else
393 buf[0] = '\0'; /* Paranoid termination */
394 return -1;
397 buf[buflen - 1] = '\0';
399 return read_bytes ? 1 : 0;
403 * Run the "file" command on the local file.
404 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
406 static int
407 get_file_type_local (const char *filename, char *buf, int buflen)
409 char *tmp;
410 int ret;
412 tmp = name_quote (filename, 0);
413 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
414 g_free (tmp);
416 return ret;
419 #ifdef HAVE_CHARSET
421 * Run the "enca" command on the local file.
422 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
424 static int
425 get_file_encoding_local (const char *filename, char *buf, int buflen)
427 char *tmp, *lang, *args;
428 int ret;
430 tmp = name_quote (filename, 0);
431 lang = name_quote (autodetect_codeset, 0);
432 args = g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
434 ret = get_popen_information ("enca", args, buf, buflen);
436 g_free (args);
437 g_free (lang);
438 g_free (tmp);
440 return ret;
442 #endif /* HAVE_CHARSET */
445 * Invoke the "file" command on the file and match its output against PTR.
446 * have_type is a flag that is set if we already have tried to determine
447 * the type of that file.
448 * Return 1 for match, 0 for no match, -1 errors.
450 static int
451 regex_check_type (const char *filename, const char *ptr, int *have_type)
453 int found = 0;
455 /* Following variables are valid if *have_type is 1 */
456 static char content_string[2048];
457 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
458 static size_t content_shift = 0;
459 static int got_data = 0;
461 if (!use_file_to_check_type)
462 return 0;
464 if (*have_type == 0)
466 char *realname; /* name used with "file" */
467 char *localfile;
469 #ifdef HAVE_CHARSET
470 int got_encoding_data;
471 #endif /* HAVE_CHARSET */
473 /* Don't repeate even unsuccessful checks */
474 *have_type = 1;
476 localfile = mc_getlocalcopy (filename);
477 if (localfile == NULL)
478 return -1;
480 realname = localfile;
482 #ifdef HAVE_CHARSET
483 got_encoding_data = is_autodetect_codeset_enabled
484 ? get_file_encoding_local (localfile, encoding_id, sizeof (encoding_id)) : 0;
486 if (got_encoding_data > 0)
488 char *pp;
489 int cp_id;
491 pp = strchr (encoding_id, '\n');
492 if (pp != NULL)
493 *pp = '\0';
495 cp_id = get_codepage_index (encoding_id);
496 if (cp_id == -1)
497 cp_id = default_source_codepage;
499 do_set_codepage (cp_id);
501 #endif /* HAVE_CHARSET */
503 mc_ungetlocalcopy (filename, localfile, 0);
505 got_data = get_file_type_local (localfile, content_string, sizeof (content_string));
507 if (got_data > 0)
509 char *pp;
510 size_t real_len;
512 pp = strchr (content_string, '\n');
513 if (pp != NULL)
514 *pp = '\0';
516 real_len = strlen (realname);
518 if (strncmp (content_string, realname, real_len) == 0)
520 /* Skip "realname: " */
521 content_shift = real_len;
522 if (content_string[content_shift] == ':')
524 /* Solaris' file prints tab(s) after ':' */
525 for (content_shift++;
526 content_string[content_shift] == ' '
527 || content_string[content_shift] == '\t'; content_shift++)
532 else
534 /* No data */
535 content_string[0] = '\0';
537 g_free (realname);
540 if (got_data == -1)
541 return -1;
543 if (content_string[0] != '\0'
544 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX))
546 found = 1;
549 return found;
553 /* The second argument is action, i.e. Open, View or Edit
555 * This function returns:
557 * -1 for a failure or user interrupt
558 * 0 if no command was run
559 * 1 if some command was run
561 * If action == "View" then a parameter is checked in the form of "View:%d",
562 * if the value for %d exists, then the viewer is started up at that line number.
565 regex_command (const char *filename, const char *action, int *move_dir)
567 char *p, *q, *r, c;
568 int file_len = strlen (filename);
569 int found = 0;
570 int error_flag = 0;
571 int ret = 0;
572 struct stat mystat;
573 int view_at_line_number;
574 char *include_target;
575 int include_target_len;
576 int have_type = 0; /* Flag used by regex_check_type() */
578 /* Check for the special View:%d parameter */
579 if (strncmp (action, "View:", 5) == 0)
581 view_at_line_number = atoi (action + 5);
582 action = "View";
584 else
586 view_at_line_number = 0;
589 if (data == NULL)
591 char *extension_file;
592 int mc_user_ext = 1;
593 int home_error = 0;
595 extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
596 if (!exist_file (extension_file))
598 g_free (extension_file);
599 check_stock_mc_ext:
600 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
601 if (!exist_file (extension_file))
603 g_free (extension_file);
604 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
606 mc_user_ext = 0;
608 data = load_file (extension_file);
609 g_free (extension_file);
610 if (data == NULL)
611 return 0;
613 if (!strstr (data, "default/"))
615 if (!strstr (data, "regex/") && !strstr (data, "shell/") && !strstr (data, "type/"))
617 g_free (data);
618 data = NULL;
619 if (mc_user_ext)
621 home_error = 1;
622 goto check_stock_mc_ext;
624 else
626 char *title = g_strdup_printf (_(" %s%s file error"),
627 mc_home, MC_LIB_EXT);
628 message (D_ERROR, title, _("The format of the %smc.ext "
629 "file has changed with version 3.0. It seems that "
630 "the installation failed. Please fetch a fresh "
631 "copy from the Midnight Commander package."),
632 mc_home);
633 g_free (title);
634 return 0;
638 if (home_error)
640 char *title =
641 g_strdup_printf (_("~/%s file error"),
642 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE);
643 message (D_ERROR, title,
644 _("The format of the ~/%s file has "
645 "changed with version 3.0. You may either want to copy "
646 "it from %smc.ext or use that file as an example of how to write it."),
647 MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home);
648 g_free (title);
651 mc_stat (filename, &mystat);
653 include_target = NULL;
654 include_target_len = 0;
655 for (p = data; *p; p++)
657 for (q = p; *q == ' ' || *q == '\t'; q++);
658 if (*q == '\n' || !*q)
659 p = q; /* empty line */
660 if (*p == '#') /* comment */
661 while (*p && *p != '\n')
662 p++;
663 if (*p == '\n')
664 continue;
665 if (!*p)
666 break;
667 if (p == q)
668 { /* i.e. starts in the first column, should be
669 * keyword/descNL
671 found = 0;
672 q = strchr (p, '\n');
673 if (q == NULL)
674 q = strchr (p, 0);
675 c = *q;
676 *q = 0;
677 if (include_target)
679 if ((strncmp (p, "include/", 8) == 0)
680 && (strncmp (p + 8, include_target, include_target_len) == 0))
681 found = 1;
683 else if (!strncmp (p, "regex/", 6))
685 p += 6;
686 /* Do not transform shell patterns, you can use shell/ for
687 * that
689 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
690 found = 1;
692 else if (!strncmp (p, "directory/", 10))
694 if (S_ISDIR (mystat.st_mode) && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
695 found = 1;
697 else if (!strncmp (p, "shell/", 6))
699 p += 6;
700 if (*p == '.' && file_len >= (q - p))
702 if (!strncmp (p, filename + file_len - (q - p), q - p))
703 found = 1;
705 else
707 if (q - p == file_len && !strncmp (p, filename, q - p))
708 found = 1;
711 else if (!strncmp (p, "type/", 5))
713 int res;
714 p += 5;
715 res = regex_check_type (filename, p, &have_type);
716 if (res == 1)
717 found = 1;
718 if (res == -1)
719 error_flag = 1; /* leave it if file cannot be opened */
721 else if (!strncmp (p, "default/", 8))
723 found = 1;
725 *q = c;
726 p = q;
727 if (!*p)
728 break;
730 else
731 { /* List of actions */
732 p = q;
733 q = strchr (p, '\n');
734 if (q == NULL)
735 q = strchr (p, 0);
736 if (found && !error_flag)
738 r = strchr (p, '=');
739 if (r != NULL)
741 c = *r;
742 *r = 0;
743 if (strcmp (p, "Include") == 0)
745 char *t;
747 include_target = p + 8;
748 t = strchr (include_target, '\n');
749 if (t)
750 *t = 0;
751 include_target_len = strlen (include_target);
752 if (t)
753 *t = '\n';
755 *r = c;
756 p = q;
757 found = 0;
759 if (!*p)
760 break;
761 continue;
763 if (!strcmp (action, p))
765 *r = c;
766 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
768 /* Empty commands just stop searching
769 * through, they don't do anything
771 * We need to copy the filename because exec_extension
772 * may end up invoking update_panels thus making the
773 * filename parameter invalid (ie, most of the time,
774 * we get filename as a pointer from current_panel->dir).
776 if (p < q)
778 char *filename_copy = g_strdup (filename);
780 exec_extension (filename_copy, r + 1, move_dir, view_at_line_number);
781 g_free (filename_copy);
783 ret = 1;
785 break;
787 else
788 *r = c;
791 p = q;
792 if (!*p)
793 break;
796 if (error_flag)
797 return -1;
798 return ret;