diffviewer: Added libmc for build depends
[midnight-commander.git] / src / ext.c
blob510b50e5b8c0145d4cc7d937716e01ef0aca904f
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 #include "charsets.h" /* get_codepage_index */
52 #include "selcodepage.h" /* do_set_codepage */
53 #include "ext.h"
55 /* If set, we execute the file command to check the file type */
56 int use_file_to_check_type = 1;
58 /* This variable points to a copy of the mc.ext file in memory
59 * With this we avoid loading/parsing the file each time we
60 * need it
62 static char *data = NULL;
64 void
65 flush_extension_file (void)
67 g_free (data);
68 data = NULL;
71 typedef char *(*quote_func_t) (const char *name, int quote_percent);
73 static void
74 exec_extension (const char *filename, const char *lc_data, int *move_dir,
75 int start_line)
77 char *fn;
78 char *file_name;
79 int cmd_file_fd;
80 FILE *cmd_file;
81 char *cmd = NULL;
82 int expand_prefix_found = 0;
83 int parameter_found = 0;
84 char lc_prompt[80];
85 int run_view = 0;
86 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
87 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
88 int written_nonspace = 0;
89 int is_cd = 0;
90 char buffer[1024];
91 char *p = 0;
92 char *localcopy = NULL;
93 int do_local_copy;
94 time_t localmtime = 0;
95 struct stat mystat;
96 quote_func_t quote_func = name_quote;
98 g_return_if_fail (filename != NULL);
99 g_return_if_fail (lc_data != NULL);
101 /* Avoid making a local copy if we are doing a cd */
102 if (!vfs_file_is_local (filename))
103 do_local_copy = 1;
104 else
105 do_local_copy = 0;
108 * All commands should be run in /bin/sh regardless of user shell.
109 * To do that, create temporary shell script and run it.
110 * Sometimes it's not needed (e.g. for %cd and %view commands),
111 * but it's easier to create it anyway.
113 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
115 if (cmd_file_fd == -1) {
116 message (D_ERROR, MSG_ERROR,
117 _(" Cannot create temporary command file \n %s "),
118 unix_error_string (errno));
119 return;
121 cmd_file = fdopen (cmd_file_fd, "w");
122 fputs ("#! /bin/sh\n", cmd_file);
124 lc_prompt[0] = 0;
125 for (; *lc_data && *lc_data != '\n'; lc_data++) {
126 if (parameter_found) {
127 if (*lc_data == '}') {
128 char *parameter;
129 parameter_found = 0;
130 parameter = input_dialog (_(" Parameter "), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
131 if (!parameter) {
132 /* User canceled */
133 fclose (cmd_file);
134 unlink (file_name);
135 if (localcopy) {
136 mc_ungetlocalcopy (filename, localcopy, 0);
137 g_free (localcopy);
139 g_free (file_name);
140 return;
142 fputs (parameter, cmd_file);
143 written_nonspace = 1;
144 g_free (parameter);
145 } else {
146 size_t len = strlen (lc_prompt);
148 if (len < sizeof (lc_prompt) - 1) {
149 lc_prompt[len] = *lc_data;
150 lc_prompt[len + 1] = 0;
153 } else if (expand_prefix_found) {
154 expand_prefix_found = 0;
155 if (*lc_data == '{')
156 parameter_found = 1;
157 else {
158 int i = check_format_view (lc_data);
159 char *v;
161 if (i) {
162 lc_data += i - 1;
163 run_view = 1;
164 } else if ((i = check_format_cd (lc_data)) > 0) {
165 is_cd = 1;
166 quote_func = fake_name_quote;
167 do_local_copy = 0;
168 p = buffer;
169 lc_data += i - 1;
170 } else if ((i = check_format_var (lc_data, &v)) > 0 && v) {
171 fputs (v, cmd_file);
172 g_free (v);
173 lc_data += i;
174 } else {
175 char *text;
177 if (*lc_data == 'f') {
178 if (do_local_copy) {
179 localcopy = mc_getlocalcopy (filename);
180 if (localcopy == NULL) {
181 fclose (cmd_file);
182 unlink (file_name);
183 g_free (file_name);
184 return;
186 mc_stat (localcopy, &mystat);
187 localmtime = mystat.st_mtime;
188 text = (*quote_func) (localcopy, 0);
189 } else {
190 fn = vfs_canon_and_translate (filename);
191 text = (*quote_func) (fn, 0);
192 g_free (fn);
194 } else {
195 text = expand_format (NULL, *lc_data, !is_cd);
197 if (!is_cd)
198 fputs (text, cmd_file);
199 else {
200 strcpy (p, text);
201 p = strchr (p, 0);
203 g_free (text);
204 written_nonspace = 1;
207 } else {
208 if (*lc_data == '%')
209 expand_prefix_found = 1;
210 else {
211 if (*lc_data != ' ' && *lc_data != '\t')
212 written_nonspace = 1;
213 if (is_cd)
214 *(p++) = *lc_data;
215 else
216 fputc (*lc_data, cmd_file);
219 } /* for */
222 * Make the script remove itself when it finishes.
223 * Don't do it for the viewer - it may need to rerun the script,
224 * so we clean up after calling view().
226 if (!run_view) {
227 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
230 fclose (cmd_file);
232 if ((run_view && !written_nonspace) || is_cd) {
233 unlink (file_name);
234 g_free (file_name);
235 file_name = NULL;
236 } else {
237 /* Set executable flag on the command file ... */
238 chmod (file_name, S_IRWXU);
239 /* ... but don't rely on it - run /bin/sh explicitly */
240 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
243 if (run_view) {
244 mcview_altered_hex_mode = 0;
245 mcview_altered_nroff_flag = 0;
246 if (def_hex_mode != mcview_default_hex_mode)
247 changed_hex_mode = 1;
248 if (def_nroff_flag != mcview_default_nroff_flag)
249 changed_nroff_flag = 1;
251 /* If we've written whitespace only, then just load filename
252 * into view
254 if (written_nonspace) {
255 mcview_viewer (cmd, filename, move_dir, start_line);
256 unlink (file_name);
257 } else {
258 mcview_viewer (NULL, filename, move_dir, start_line);
260 if (changed_hex_mode && !mcview_altered_hex_mode)
261 mcview_default_hex_mode = def_hex_mode;
262 if (changed_nroff_flag && !mcview_altered_nroff_flag)
263 mcview_default_nroff_flag = def_nroff_flag;
264 repaint_screen ();
265 } else if (is_cd) {
266 char *q;
267 *p = 0;
268 p = buffer;
269 /* while (*p == ' ' && *p == '\t')
270 * p++;
272 /* Search last non-space character. Start search at the end in order
273 not to short filenames containing spaces. */
274 q = p + strlen (p) - 1;
275 while (q >= p && (*q == ' ' || *q == '\t'))
276 q--;
277 q[1] = 0;
278 do_cd (p, cd_parse_command);
279 } else {
280 shell_execute (cmd, EXECUTE_INTERNAL);
281 if (console_flag) {
282 handle_console (CONSOLE_SAVE);
283 if (output_lines && keybar_visible) {
284 show_console_contents (output_start_y,
285 LINES - keybar_visible -
286 output_lines - 1,
287 LINES - keybar_visible - 1);
293 g_free (file_name);
294 g_free (cmd);
296 if (localcopy) {
297 mc_stat (localcopy, &mystat);
298 mc_ungetlocalcopy (filename, localcopy,
299 localmtime != mystat.st_mtime);
300 g_free (localcopy);
304 #ifdef FILE_L
305 # define FILE_CMD "file -L "
306 #else
307 # define FILE_CMD "file "
308 #endif
311 * Run cmd_file with args, put result into buf.
312 * If error, put '\0' into buf[0]
313 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
315 * NOTES: buf is null-terminated string.
317 static int
318 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
320 gboolean read_bytes = FALSE;
321 char *command;
322 FILE *f;
324 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
325 f = popen (command, "r");
326 g_free (command);
328 if (f != NULL) {
329 #ifdef __QNXNTO__
330 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
331 (void) pclose (f);
332 return -1;
334 #endif
335 read_bytes = (fgets (buf, buflen, f) != NULL);
336 if (!read_bytes)
337 buf[0] = '\0'; /* Paranoid termination */
338 pclose (f);
339 } else {
340 buf[0] = '\0'; /* Paranoid termination */
341 return -1;
344 buf[buflen - 1] = '\0';
346 return read_bytes ? 1 : 0;
350 * Run the "file" command on the local file.
351 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
353 static int
354 get_file_type_local (const char *filename, char *buf, int buflen)
356 char *tmp;
357 int ret;
359 tmp = name_quote (filename, 0);
360 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
361 g_free (tmp);
363 return ret;
367 * Run the "enca" command on the local file.
368 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
370 static int
371 get_file_encoding_local (const char *filename, char *buf, int buflen)
373 char *tmp, *lang, *args;
374 int ret;
376 tmp = name_quote (filename, 0);
377 lang = name_quote (autodetect_codeset, 0);
378 args= g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
380 ret = get_popen_information ("enca", args, buf, buflen);
382 g_free (args);
383 g_free (lang);
384 g_free (tmp);
386 return ret;
390 * Invoke the "file" command on the file and match its output against PTR.
391 * have_type is a flag that is set if we already have tried to determine
392 * the type of that file.
393 * Return 1 for match, 0 for no match, -1 errors.
395 static int
396 regex_check_type (const char *filename, const char *ptr, int *have_type)
398 int found = 0;
400 /* Following variables are valid if *have_type is 1 */
401 static char content_string[2048];
402 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
403 static size_t content_shift = 0;
404 static int got_data = 0;
406 if (!use_file_to_check_type)
407 return 0;
409 if (*have_type == 0) {
410 char *realname; /* name used with "file" */
411 char *localfile;
412 int got_encoding_data;
414 /* Don't repeate even unsuccessful checks */
415 *have_type = 1;
417 localfile = mc_getlocalcopy (filename);
418 if (localfile == NULL)
419 return -1;
421 realname = localfile;
423 got_encoding_data = is_autodetect_codeset_enabled
424 ? get_file_encoding_local (localfile, encoding_id, sizeof (encoding_id))
425 : 0;
427 mc_ungetlocalcopy (filename, localfile, 0);
429 if (got_encoding_data > 0) {
430 char *pp;
431 int cp_id;
433 pp = strchr (encoding_id, '\n');
434 if (pp != NULL)
435 *pp = '\0';
437 cp_id = get_codepage_index (encoding_id);
438 if (cp_id == -1)
439 cp_id = default_source_codepage;
441 do_set_codepage (cp_id);
444 got_data = get_file_type_local (localfile, content_string, sizeof (content_string));
446 if (got_data > 0) {
447 char *pp;
449 pp = strchr (content_string, '\n');
450 if (pp != NULL)
451 *pp = '\0';
453 if (strncmp (content_string, realname, strlen (realname)) == 0) {
454 /* Skip "realname: " */
455 content_shift = strlen (realname);
456 if (content_string[content_shift] == ':') {
457 /* Solaris' file prints tab(s) after ':' */
458 for (content_shift++;
459 content_string[content_shift] == ' '
460 || content_string[content_shift] == '\t';
461 content_shift++)
465 } else {
466 /* No data */
467 content_string[0] = '\0';
469 g_free (realname);
472 if (got_data == -1)
473 return -1;
475 if (content_string[0] != '\0'
476 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX)) {
477 found = 1;
480 return found;
484 /* The second argument is action, i.e. Open, View or Edit
486 * This function returns:
488 * -1 for a failure or user interrupt
489 * 0 if no command was run
490 * 1 if some command was run
492 * If action == "View" then a parameter is checked in the form of "View:%d",
493 * if the value for %d exists, then the viewer is started up at that line number.
496 regex_command (const char *filename, const char *action, int *move_dir)
498 char *p, *q, *r, c;
499 int file_len = strlen (filename);
500 int found = 0;
501 int error_flag = 0;
502 int ret = 0;
503 struct stat mystat;
504 int view_at_line_number;
505 char *include_target;
506 int include_target_len;
507 int have_type = 0; /* Flag used by regex_check_type() */
509 /* Check for the special View:%d parameter */
510 if (strncmp (action, "View:", 5) == 0) {
511 view_at_line_number = atoi (action + 5);
512 action = "View";
513 } else {
514 view_at_line_number = 0;
517 if (data == NULL) {
518 char *extension_file;
519 int mc_user_ext = 1;
520 int home_error = 0;
522 extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
523 if (!exist_file (extension_file)) {
524 g_free (extension_file);
525 check_stock_mc_ext:
526 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
527 if (!exist_file (extension_file)) {
528 g_free (extension_file);
529 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
531 mc_user_ext = 0;
533 data = load_file (extension_file);
534 g_free (extension_file);
535 if (data == NULL)
536 return 0;
538 if (!strstr (data, "default/")) {
539 if (!strstr (data, "regex/") && !strstr (data, "shell/")
540 && !strstr (data, "type/")) {
541 g_free (data);
542 data = NULL;
543 if (mc_user_ext) {
544 home_error = 1;
545 goto check_stock_mc_ext;
546 } else {
547 char *title =
548 g_strdup_printf (_(" %s%s file error"),
549 mc_home, MC_LIB_EXT);
550 message (D_ERROR, title, _("The format of the %smc.ext "
551 "file has changed with version 3.0. It seems that "
552 "the installation failed. Please fetch a fresh "
553 "copy from the Midnight Commander package."),
554 mc_home);
555 g_free (title);
556 return 0;
560 if (home_error) {
561 char *title =
562 g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE);
563 message (D_ERROR, title, _("The format of the ~/%s file has "
564 "changed with version 3.0. You may either want to copy "
565 "it from %smc.ext or use that file as an example of how "
566 "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home);
567 g_free (title);
570 mc_stat (filename, &mystat);
572 include_target = NULL;
573 include_target_len = 0;
574 for (p = data; *p; p++) {
575 for (q = p; *q == ' ' || *q == '\t'; q++);
576 if (*q == '\n' || !*q)
577 p = q; /* empty line */
578 if (*p == '#') /* comment */
579 while (*p && *p != '\n')
580 p++;
581 if (*p == '\n')
582 continue;
583 if (!*p)
584 break;
585 if (p == q) { /* i.e. starts in the first column, should be
586 * keyword/descNL
588 found = 0;
589 q = strchr (p, '\n');
590 if (q == NULL)
591 q = strchr (p, 0);
592 c = *q;
593 *q = 0;
594 if (include_target) {
595 if ((strncmp (p, "include/", 8) == 0)
596 && (strncmp (p + 8, include_target, include_target_len)
597 == 0))
598 found = 1;
599 } else if (!strncmp (p, "regex/", 6)) {
600 p += 6;
601 /* Do not transform shell patterns, you can use shell/ for
602 * that
604 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
605 found = 1;
606 } else if (!strncmp (p, "directory/", 10)) {
607 if (S_ISDIR (mystat.st_mode)
608 && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
609 found = 1;
610 } else if (!strncmp (p, "shell/", 6)) {
611 p += 6;
612 if (*p == '.' && file_len >= (q - p)) {
613 if (!strncmp (p, filename + file_len - (q - p), q - p))
614 found = 1;
615 } else {
616 if (q - p == file_len && !strncmp (p, filename, q - p))
617 found = 1;
619 } else if (!strncmp (p, "type/", 5)) {
620 int res;
621 p += 5;
622 res = regex_check_type (filename, p, &have_type);
623 if (res == 1)
624 found = 1;
625 if (res == -1)
626 error_flag = 1; /* leave it if file cannot be opened */
627 } else if (!strncmp (p, "default/", 8)) {
628 found = 1;
630 *q = c;
631 p = q;
632 if (!*p)
633 break;
634 } else { /* List of actions */
635 p = q;
636 q = strchr (p, '\n');
637 if (q == NULL)
638 q = strchr (p, 0);
639 if (found && !error_flag) {
640 r = strchr (p, '=');
641 if (r != NULL) {
642 c = *r;
643 *r = 0;
644 if (strcmp (p, "Include") == 0) {
645 char *t;
647 include_target = p + 8;
648 t = strchr (include_target, '\n');
649 if (t)
650 *t = 0;
651 include_target_len = strlen (include_target);
652 if (t)
653 *t = '\n';
655 *r = c;
656 p = q;
657 found = 0;
659 if (!*p)
660 break;
661 continue;
663 if (!strcmp (action, p)) {
664 *r = c;
665 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
667 /* Empty commands just stop searching
668 * through, they don't do anything
670 * We need to copy the filename because exec_extension
671 * may end up invoking update_panels thus making the
672 * filename parameter invalid (ie, most of the time,
673 * we get filename as a pointer from current_panel->dir).
675 if (p < q) {
676 char *filename_copy = g_strdup (filename);
678 exec_extension (filename_copy, r + 1, move_dir,
679 view_at_line_number);
680 g_free (filename_copy);
682 ret = 1;
684 break;
685 } else
686 *r = c;
689 p = q;
690 if (!*p)
691 break;
694 if (error_flag)
695 return -1;
696 return ret;