Merge branch '2021_MarkFileDown_MarkFileUp'
[free-mc.git] / src / ext.c
blob81661c58ecd785ea0776afe9afc917faf6474f3a
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;
122 cmd_file = fdopen (cmd_file_fd, "w");
123 fputs ("#! /bin/sh\n", cmd_file);
125 lc_prompt[0] = '\0';
126 for (; *lc_data != '\0' && *lc_data != '\n'; lc_data++) {
127 if (parameter_found) {
128 if (*lc_data == '}') {
129 char *parameter;
131 parameter_found = 0;
132 parameter = input_dialog (_(" Parameter "), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
133 if (parameter == NULL) {
134 /* User canceled */
135 fclose (cmd_file);
136 unlink (file_name);
137 if (localcopy) {
138 mc_ungetlocalcopy (filename, localcopy, 0);
139 g_free (localcopy);
141 g_free (file_name);
142 return;
144 fputs (parameter, cmd_file);
145 written_nonspace = 1;
146 g_free (parameter);
147 } else {
148 size_t len = strlen (lc_prompt);
150 if (len < sizeof (lc_prompt) - 1) {
151 lc_prompt[len] = *lc_data;
152 lc_prompt[len + 1] = '\0';
155 } else if (expand_prefix_found) {
156 expand_prefix_found = 0;
157 if (*lc_data == '{')
158 parameter_found = 1;
159 else {
160 int i;
161 char *v;
163 i = check_format_view (lc_data);
164 if (i != 0) {
165 lc_data += i - 1;
166 run_view = 1;
167 } else {
168 i = check_format_cd (lc_data);
169 if (i > 0) {
170 is_cd = 1;
171 quote_func = fake_name_quote;
172 do_local_copy = 0;
173 p = buffer;
174 lc_data += i - 1;
175 } else {
176 i = check_format_var (lc_data, &v);
177 if (i > 0 && v != NULL) {
178 fputs (v, cmd_file);
179 g_free (v);
180 lc_data += i;
181 } else {
182 char *text;
184 if (*lc_data != 'f')
185 text = expand_format (NULL, *lc_data, !is_cd);
186 else {
187 if (do_local_copy) {
188 localcopy = mc_getlocalcopy (filename);
189 if (localcopy == NULL) {
190 fclose (cmd_file);
191 unlink (file_name);
192 g_free (file_name);
193 return;
195 mc_stat (localcopy, &mystat);
196 localmtime = mystat.st_mtime;
197 text = quote_func (localcopy, 0);
198 } else {
199 fn = vfs_canon_and_translate (filename);
200 text = quote_func (fn, 0);
201 g_free (fn);
205 if (!is_cd)
206 fputs (text, cmd_file);
207 else {
208 strcpy (p, text);
209 p = strchr (p, 0);
212 g_free (text);
213 written_nonspace = 1;
218 } else if (*lc_data == '%')
219 expand_prefix_found = 1;
220 else {
221 if (*lc_data != ' ' && *lc_data != '\t')
222 written_nonspace = 1;
223 if (is_cd)
224 *(p++) = *lc_data;
225 else
226 fputc (*lc_data, cmd_file);
228 } /* for */
231 * Make the script remove itself when it finishes.
232 * Don't do it for the viewer - it may need to rerun the script,
233 * so we clean up after calling view().
235 if (!run_view)
236 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
238 fclose (cmd_file);
240 if ((run_view && !written_nonspace) || is_cd) {
241 unlink (file_name);
242 g_free (file_name);
243 file_name = NULL;
244 } else {
245 /* Set executable flag on the command file ... */
246 chmod (file_name, S_IRWXU);
247 /* ... but don't rely on it - run /bin/sh explicitly */
248 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
251 if (run_view) {
252 mcview_altered_hex_mode = 0;
253 mcview_altered_nroff_flag = 0;
254 if (def_hex_mode != mcview_default_hex_mode)
255 changed_hex_mode = 1;
256 if (def_nroff_flag != mcview_default_nroff_flag)
257 changed_nroff_flag = 1;
259 /* If we've written whitespace only, then just load filename
260 * into view
262 if (written_nonspace) {
263 mcview_viewer (cmd, filename, move_dir, start_line);
264 unlink (file_name);
265 } else {
266 mcview_viewer (NULL, filename, move_dir, start_line);
268 if (changed_hex_mode && !mcview_altered_hex_mode)
269 mcview_default_hex_mode = def_hex_mode;
270 if (changed_nroff_flag && !mcview_altered_nroff_flag)
271 mcview_default_nroff_flag = def_nroff_flag;
272 repaint_screen ();
273 } else if (is_cd) {
274 char *q;
275 *p = 0;
276 p = buffer;
277 /* while (*p == ' ' && *p == '\t')
278 * p++;
280 /* Search last non-space character. Start search at the end in order
281 not to short filenames containing spaces. */
282 q = p + strlen (p) - 1;
283 while (q >= p && (*q == ' ' || *q == '\t'))
284 q--;
285 q[1] = 0;
286 do_cd (p, cd_parse_command);
287 } else {
288 shell_execute (cmd, EXECUTE_INTERNAL);
289 if (console_flag) {
290 handle_console (CONSOLE_SAVE);
291 if (output_lines && keybar_visible) {
292 show_console_contents (output_start_y,
293 LINES - keybar_visible -
294 output_lines - 1,
295 LINES - keybar_visible - 1);
300 g_free (file_name);
301 g_free (cmd);
303 if (localcopy) {
304 mc_stat (localcopy, &mystat);
305 mc_ungetlocalcopy (filename, localcopy,
306 localmtime != mystat.st_mtime);
307 g_free (localcopy);
311 #ifdef FILE_L
312 # define FILE_CMD "file -L "
313 #else
314 # define FILE_CMD "file "
315 #endif
318 * Run cmd_file with args, put result into buf.
319 * If error, put '\0' into buf[0]
320 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
322 * NOTES: buf is null-terminated string.
324 static int
325 get_popen_information (const char *cmd_file, const char *args, char *buf, int buflen)
327 gboolean read_bytes = FALSE;
328 char *command;
329 FILE *f;
331 command = g_strconcat (cmd_file, args, " 2>/dev/null", (char *) NULL);
332 f = popen (command, "r");
333 g_free (command);
335 if (f != NULL) {
336 #ifdef __QNXNTO__
337 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
338 (void) pclose (f);
339 return -1;
341 #endif
342 read_bytes = (fgets (buf, buflen, f) != NULL);
343 if (!read_bytes)
344 buf[0] = '\0'; /* Paranoid termination */
345 pclose (f);
346 } else {
347 buf[0] = '\0'; /* Paranoid termination */
348 return -1;
351 buf[buflen - 1] = '\0';
353 return read_bytes ? 1 : 0;
357 * Run the "file" command on the local file.
358 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
360 static int
361 get_file_type_local (const char *filename, char *buf, int buflen)
363 char *tmp;
364 int ret;
366 tmp = name_quote (filename, 0);
367 ret = get_popen_information (FILE_CMD, tmp, buf, buflen);
368 g_free (tmp);
370 return ret;
374 * Run the "enca" command on the local file.
375 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
377 static int
378 get_file_encoding_local (const char *filename, char *buf, int buflen)
380 char *tmp, *lang, *args;
381 int ret;
383 tmp = name_quote (filename, 0);
384 lang = name_quote (autodetect_codeset, 0);
385 args= g_strconcat (" -L", lang, " -i ", tmp, (char *) NULL);
387 ret = get_popen_information ("enca", args, buf, buflen);
389 g_free (args);
390 g_free (lang);
391 g_free (tmp);
393 return ret;
397 * Invoke the "file" command on the file and match its output against PTR.
398 * have_type is a flag that is set if we already have tried to determine
399 * the type of that file.
400 * Return 1 for match, 0 for no match, -1 errors.
402 static int
403 regex_check_type (const char *filename, const char *ptr, int *have_type)
405 int found = 0;
407 /* Following variables are valid if *have_type is 1 */
408 static char content_string[2048];
409 static char encoding_id[21]; /* CSISO51INISCYRILLIC -- 20 */
410 static size_t content_shift = 0;
411 static int got_data = 0;
413 if (!use_file_to_check_type)
414 return 0;
416 if (*have_type == 0) {
417 char *realname; /* name used with "file" */
418 char *localfile;
419 int got_encoding_data;
421 /* Don't repeate even unsuccessful checks */
422 *have_type = 1;
424 localfile = mc_getlocalcopy (filename);
425 if (localfile == NULL)
426 return -1;
428 realname = localfile;
430 got_encoding_data = is_autodetect_codeset_enabled
431 ? get_file_encoding_local (localfile, encoding_id, sizeof (encoding_id))
432 : 0;
434 mc_ungetlocalcopy (filename, localfile, 0);
436 if (got_encoding_data > 0) {
437 char *pp;
438 int cp_id;
440 pp = strchr (encoding_id, '\n');
441 if (pp != NULL)
442 *pp = '\0';
444 cp_id = get_codepage_index (encoding_id);
445 if (cp_id == -1)
446 cp_id = default_source_codepage;
448 do_set_codepage (cp_id);
451 got_data = get_file_type_local (localfile, content_string, sizeof (content_string));
453 if (got_data > 0) {
454 char *pp;
456 pp = strchr (content_string, '\n');
457 if (pp != NULL)
458 *pp = '\0';
460 if (strncmp (content_string, realname, strlen (realname)) == 0) {
461 /* Skip "realname: " */
462 content_shift = strlen (realname);
463 if (content_string[content_shift] == ':') {
464 /* Solaris' file prints tab(s) after ':' */
465 for (content_shift++;
466 content_string[content_shift] == ' '
467 || content_string[content_shift] == '\t';
468 content_shift++)
472 } else {
473 /* No data */
474 content_string[0] = '\0';
476 g_free (realname);
479 if (got_data == -1)
480 return -1;
482 if (content_string[0] != '\0'
483 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX)) {
484 found = 1;
487 return found;
491 /* The second argument is action, i.e. Open, View or Edit
493 * This function returns:
495 * -1 for a failure or user interrupt
496 * 0 if no command was run
497 * 1 if some command was run
499 * If action == "View" then a parameter is checked in the form of "View:%d",
500 * if the value for %d exists, then the viewer is started up at that line number.
503 regex_command (const char *filename, const char *action, int *move_dir)
505 char *p, *q, *r, c;
506 int file_len = strlen (filename);
507 int found = 0;
508 int error_flag = 0;
509 int ret = 0;
510 struct stat mystat;
511 int view_at_line_number;
512 char *include_target;
513 int include_target_len;
514 int have_type = 0; /* Flag used by regex_check_type() */
516 /* Check for the special View:%d parameter */
517 if (strncmp (action, "View:", 5) == 0) {
518 view_at_line_number = atoi (action + 5);
519 action = "View";
520 } else {
521 view_at_line_number = 0;
524 if (data == NULL) {
525 char *extension_file;
526 int mc_user_ext = 1;
527 int home_error = 0;
529 extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
530 if (!exist_file (extension_file)) {
531 g_free (extension_file);
532 check_stock_mc_ext:
533 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
534 if (!exist_file (extension_file)) {
535 g_free (extension_file);
536 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
538 mc_user_ext = 0;
540 data = load_file (extension_file);
541 g_free (extension_file);
542 if (data == NULL)
543 return 0;
545 if (!strstr (data, "default/")) {
546 if (!strstr (data, "regex/") && !strstr (data, "shell/")
547 && !strstr (data, "type/")) {
548 g_free (data);
549 data = NULL;
550 if (mc_user_ext) {
551 home_error = 1;
552 goto check_stock_mc_ext;
553 } else {
554 char *title =
555 g_strdup_printf (_(" %s%s file error"),
556 mc_home, MC_LIB_EXT);
557 message (D_ERROR, title, _("The format of the %smc.ext "
558 "file has changed with version 3.0. It seems that "
559 "the installation failed. Please fetch a fresh "
560 "copy from the Midnight Commander package."),
561 mc_home);
562 g_free (title);
563 return 0;
567 if (home_error) {
568 char *title =
569 g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE);
570 message (D_ERROR, title, _("The format of the ~/%s file has "
571 "changed with version 3.0. You may either want to copy "
572 "it from %smc.ext or use that file as an example of how "
573 "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home);
574 g_free (title);
577 mc_stat (filename, &mystat);
579 include_target = NULL;
580 include_target_len = 0;
581 for (p = data; *p; p++) {
582 for (q = p; *q == ' ' || *q == '\t'; q++);
583 if (*q == '\n' || !*q)
584 p = q; /* empty line */
585 if (*p == '#') /* comment */
586 while (*p && *p != '\n')
587 p++;
588 if (*p == '\n')
589 continue;
590 if (!*p)
591 break;
592 if (p == q) { /* i.e. starts in the first column, should be
593 * keyword/descNL
595 found = 0;
596 q = strchr (p, '\n');
597 if (q == NULL)
598 q = strchr (p, 0);
599 c = *q;
600 *q = 0;
601 if (include_target) {
602 if ((strncmp (p, "include/", 8) == 0)
603 && (strncmp (p + 8, include_target, include_target_len)
604 == 0))
605 found = 1;
606 } else if (!strncmp (p, "regex/", 6)) {
607 p += 6;
608 /* Do not transform shell patterns, you can use shell/ for
609 * that
611 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
612 found = 1;
613 } else if (!strncmp (p, "directory/", 10)) {
614 if (S_ISDIR (mystat.st_mode)
615 && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
616 found = 1;
617 } else if (!strncmp (p, "shell/", 6)) {
618 p += 6;
619 if (*p == '.' && file_len >= (q - p)) {
620 if (!strncmp (p, filename + file_len - (q - p), q - p))
621 found = 1;
622 } else {
623 if (q - p == file_len && !strncmp (p, filename, q - p))
624 found = 1;
626 } else if (!strncmp (p, "type/", 5)) {
627 int res;
628 p += 5;
629 res = regex_check_type (filename, p, &have_type);
630 if (res == 1)
631 found = 1;
632 if (res == -1)
633 error_flag = 1; /* leave it if file cannot be opened */
634 } else if (!strncmp (p, "default/", 8)) {
635 found = 1;
637 *q = c;
638 p = q;
639 if (!*p)
640 break;
641 } else { /* List of actions */
642 p = q;
643 q = strchr (p, '\n');
644 if (q == NULL)
645 q = strchr (p, 0);
646 if (found && !error_flag) {
647 r = strchr (p, '=');
648 if (r != NULL) {
649 c = *r;
650 *r = 0;
651 if (strcmp (p, "Include") == 0) {
652 char *t;
654 include_target = p + 8;
655 t = strchr (include_target, '\n');
656 if (t)
657 *t = 0;
658 include_target_len = strlen (include_target);
659 if (t)
660 *t = '\n';
662 *r = c;
663 p = q;
664 found = 0;
666 if (!*p)
667 break;
668 continue;
670 if (!strcmp (action, p)) {
671 *r = c;
672 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
674 /* Empty commands just stop searching
675 * through, they don't do anything
677 * We need to copy the filename because exec_extension
678 * may end up invoking update_panels thus making the
679 * filename parameter invalid (ie, most of the time,
680 * we get filename as a pointer from current_panel->dir).
682 if (p < q) {
683 char *filename_copy = g_strdup (filename);
685 exec_extension (filename_copy, r + 1, move_dir,
686 view_at_line_number);
687 g_free (filename_copy);
689 ret = 1;
691 break;
692 } else
693 *r = c;
696 p = q;
697 if (!*p)
698 break;
701 if (error_flag)
702 return -1;
703 return ret;