Ticket #1395 (Copying to fish is broken)
[midnight-commander.git] / src / ext.c
blobb79bc0784ff25467e103647495da53ef61e5725a
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 "global.h"
36 #include "tty.h"
37 #include "user.h"
38 #include "main.h"
39 #include "wtools.h"
40 #include "ext.h"
41 #include "view.h"
42 #include "execute.h"
43 #include "history.h"
44 #include "cons.saver.h"
45 #include "layout.h"
46 #include "../src/search/search.h"
48 /* If set, we execute the file command to check the file type */
49 int use_file_to_check_type = 1;
51 /* This variable points to a copy of the mc.ext file in memory
52 * With this we avoid loading/parsing the file each time we
53 * need it
55 static char *data = NULL;
57 void
58 flush_extension_file (void)
60 g_free (data);
61 data = NULL;
64 typedef char *(*quote_func_t) (const char *name, int quote_percent);
66 static void
67 exec_extension (const char *filename, const char *data, int *move_dir,
68 int start_line)
70 char *fn;
71 char *file_name;
72 int cmd_file_fd;
73 FILE *cmd_file;
74 char *cmd = NULL;
75 int expand_prefix_found = 0;
76 int parameter_found = 0;
77 char prompt[80];
78 int run_view = 0;
79 int def_hex_mode = default_hex_mode, changed_hex_mode = 0;
80 int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0;
81 int written_nonspace = 0;
82 int is_cd = 0;
83 char buffer[1024];
84 char *p = 0;
85 char *localcopy = NULL;
86 int do_local_copy;
87 time_t localmtime = 0;
88 struct stat mystat;
89 quote_func_t quote_func = name_quote;
91 g_return_if_fail (filename != NULL);
92 g_return_if_fail (data != NULL);
94 /* Avoid making a local copy if we are doing a cd */
95 if (!vfs_file_is_local (filename))
96 do_local_copy = 1;
97 else
98 do_local_copy = 0;
101 * All commands should be run in /bin/sh regardless of user shell.
102 * To do that, create temporary shell script and run it.
103 * Sometimes it's not needed (e.g. for %cd and %view commands),
104 * but it's easier to create it anyway.
106 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
108 if (cmd_file_fd == -1) {
109 message (D_ERROR, MSG_ERROR,
110 _(" Cannot create temporary command file \n %s "),
111 unix_error_string (errno));
112 return;
114 cmd_file = fdopen (cmd_file_fd, "w");
115 fputs ("#! /bin/sh\n", cmd_file);
117 prompt[0] = 0;
118 for (; *data && *data != '\n'; data++) {
119 if (parameter_found) {
120 if (*data == '}') {
121 char *parameter;
122 parameter_found = 0;
123 parameter = input_dialog (_(" Parameter "), prompt, MC_HISTORY_EXT_PARAMETER, "");
124 if (!parameter) {
125 /* User canceled */
126 fclose (cmd_file);
127 unlink (file_name);
128 if (localcopy) {
129 mc_ungetlocalcopy (filename, localcopy, 0);
130 g_free (localcopy);
132 g_free (file_name);
133 return;
135 fputs (parameter, cmd_file);
136 written_nonspace = 1;
137 g_free (parameter);
138 } else {
139 size_t len = strlen (prompt);
141 if (len < sizeof (prompt) - 1) {
142 prompt[len] = *data;
143 prompt[len + 1] = 0;
146 } else if (expand_prefix_found) {
147 expand_prefix_found = 0;
148 if (*data == '{')
149 parameter_found = 1;
150 else {
151 int i = check_format_view (data);
152 char *v;
154 if (i) {
155 data += i - 1;
156 run_view = 1;
157 } else if ((i = check_format_cd (data)) > 0) {
158 is_cd = 1;
159 quote_func = fake_name_quote;
160 do_local_copy = 0;
161 p = buffer;
162 data += i - 1;
163 } else if ((i = check_format_var (data, &v)) > 0 && v) {
164 fputs (v, cmd_file);
165 g_free (v);
166 data += i;
167 } else {
168 char *text;
170 if (*data == 'f') {
171 if (do_local_copy) {
172 localcopy = mc_getlocalcopy (filename);
173 if (localcopy == NULL) {
174 fclose (cmd_file);
175 unlink (file_name);
176 g_free (file_name);
177 return;
179 mc_stat (localcopy, &mystat);
180 localmtime = mystat.st_mtime;
181 text = (*quote_func) (localcopy, 0);
182 } else {
183 fn = vfs_canon_and_translate (filename);
184 text = (*quote_func) (fn, 0);
185 g_free (fn);
187 } else {
188 text = expand_format (NULL, *data, !is_cd);
190 if (!is_cd)
191 fputs (text, cmd_file);
192 else {
193 strcpy (p, text);
194 p = strchr (p, 0);
196 g_free (text);
197 written_nonspace = 1;
200 } else {
201 if (*data == '%')
202 expand_prefix_found = 1;
203 else {
204 if (*data != ' ' && *data != '\t')
205 written_nonspace = 1;
206 if (is_cd)
207 *(p++) = *data;
208 else
209 fputc (*data, cmd_file);
212 } /* for */
215 * Make the script remove itself when it finishes.
216 * Don't do it for the viewer - it may need to rerun the script,
217 * so we clean up after calling view().
219 if (!run_view) {
220 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
223 fclose (cmd_file);
225 if ((run_view && !written_nonspace) || is_cd) {
226 unlink (file_name);
227 g_free (file_name);
228 file_name = NULL;
229 } else {
230 /* Set executable flag on the command file ... */
231 chmod (file_name, S_IRWXU);
232 /* ... but don't rely on it - run /bin/sh explicitly */
233 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
236 if (run_view) {
237 altered_hex_mode = 0;
238 altered_nroff_flag = 0;
239 if (def_hex_mode != default_hex_mode)
240 changed_hex_mode = 1;
241 if (def_nroff_flag != default_nroff_flag)
242 changed_nroff_flag = 1;
244 /* If we've written whitespace only, then just load filename
245 * into view
247 if (written_nonspace) {
248 mc_internal_viewer (cmd, filename, move_dir, start_line);
249 unlink (file_name);
250 } else {
251 mc_internal_viewer (NULL, filename, move_dir, start_line);
253 if (changed_hex_mode && !altered_hex_mode)
254 default_hex_mode = def_hex_mode;
255 if (changed_nroff_flag && !altered_nroff_flag)
256 default_nroff_flag = def_nroff_flag;
257 repaint_screen ();
258 } else if (is_cd) {
259 char *q;
260 *p = 0;
261 p = buffer;
262 /* while (*p == ' ' && *p == '\t')
263 * p++;
265 /* Search last non-space character. Start search at the end in order
266 not to short filenames containing spaces. */
267 q = p + strlen (p) - 1;
268 while (q >= p && (*q == ' ' || *q == '\t'))
269 q--;
270 q[1] = 0;
271 do_cd (p, cd_parse_command);
272 } else {
273 shell_execute (cmd, EXECUTE_INTERNAL);
274 if (console_flag) {
275 handle_console (CONSOLE_SAVE);
276 if (output_lines && keybar_visible) {
277 show_console_contents (output_start_y,
278 LINES - keybar_visible -
279 output_lines - 1,
280 LINES - keybar_visible - 1);
286 g_free (file_name);
287 g_free (cmd);
289 if (localcopy) {
290 mc_stat (localcopy, &mystat);
291 mc_ungetlocalcopy (filename, localcopy,
292 localmtime != mystat.st_mtime);
293 g_free (localcopy);
297 #ifdef FILE_L
298 # define FILE_CMD "file -L "
299 #else
300 # define FILE_CMD "file "
301 #endif
304 * Run the "file" command on the local file.
305 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
307 static int
308 get_file_type_local (const char *filename, char *buf, int buflen)
310 int read_bytes = 0;
312 char *tmp = name_quote (filename, 0);
313 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) 0);
314 FILE *f = popen (command, "r");
316 g_free (tmp);
317 g_free (command);
318 if (f != NULL) {
319 #ifdef __QNXNTO__
320 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
321 (void)pclose (f);
322 return -1;
324 #endif
325 read_bytes = (fgets (buf, buflen, f)
326 != NULL);
327 if (read_bytes == 0)
328 buf[0] = 0;
329 pclose (f);
330 } else {
331 return -1;
334 return (read_bytes > 0);
339 * Invoke the "file" command on the file and match its output against PTR.
340 * have_type is a flag that is set if we already have tried to determine
341 * the type of that file.
342 * Return 1 for match, 0 for no match, -1 errors.
344 static int
345 regex_check_type (const char *filename, const char *ptr, int *have_type)
347 int found = 0;
349 /* Following variables are valid if *have_type is 1 */
350 static char content_string[2048];
351 static int content_shift = 0;
352 static int got_data = 0;
354 if (!use_file_to_check_type) {
355 return 0;
358 if (!*have_type) {
359 char *realname; /* name used with "file" */
360 char *localfile;
361 /* Don't repeate even unsuccessful checks */
362 *have_type = 1;
364 localfile = mc_getlocalcopy (filename);
365 if (!localfile)
366 return -1;
368 realname = localfile;
369 got_data =
370 get_file_type_local (localfile, content_string,
371 sizeof (content_string));
372 mc_ungetlocalcopy (filename, localfile, 0);
374 if (got_data > 0) {
375 char *pp;
377 /* Paranoid termination */
378 content_string[sizeof (content_string) - 1] = 0;
380 if ((pp = strchr (content_string, '\n')) != 0)
381 *pp = 0;
383 if (!strncmp (content_string, realname, strlen (realname))) {
384 /* Skip "realname: " */
385 content_shift = strlen (realname);
386 if (content_string[content_shift] == ':') {
387 /* Solaris' file prints tab(s) after ':' */
388 for (content_shift++;
389 content_string[content_shift] == ' '
390 || content_string[content_shift] == '\t';
391 content_shift++);
394 } else {
395 /* No data */
396 content_string[0] = 0;
398 g_free (realname);
401 if (got_data == -1) {
402 return -1;
405 if (content_string[0]
406 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX)) {
407 found = 1;
410 return found;
414 /* The second argument is action, i.e. Open, View or Edit
416 * This function returns:
418 * -1 for a failure or user interrupt
419 * 0 if no command was run
420 * 1 if some command was run
422 * If action == "View" then a parameter is checked in the form of "View:%d",
423 * if the value for %d exists, then the viewer is started up at that line number.
426 regex_command (const char *filename, const char *action, int *move_dir)
428 char *p, *q, *r, c;
429 int file_len = strlen (filename);
430 int found = 0;
431 int error_flag = 0;
432 int ret = 0;
433 struct stat mystat;
434 int view_at_line_number;
435 char *include_target;
436 int include_target_len;
437 int have_type = 0; /* Flag used by regex_check_type() */
439 /* Check for the special View:%d parameter */
440 if (strncmp (action, "View:", 5) == 0) {
441 view_at_line_number = atoi (action + 5);
442 action = "View";
443 } else {
444 view_at_line_number = 0;
447 if (data == NULL) {
448 char *extension_file;
449 int mc_user_ext = 1;
450 int home_error = 0;
452 extension_file = concat_dir_and_file (home_dir, MC_USER_EXT);
453 if (!exist_file (extension_file)) {
454 g_free (extension_file);
455 check_stock_mc_ext:
456 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
457 if (!exist_file (extension_file)) {
458 g_free (extension_file);
459 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
461 mc_user_ext = 0;
463 data = load_file (extension_file);
464 g_free (extension_file);
465 if (data == NULL)
466 return 0;
468 if (!strstr (data, "default/")) {
469 if (!strstr (data, "regex/") && !strstr (data, "shell/")
470 && !strstr (data, "type/")) {
471 g_free (data);
472 data = NULL;
473 if (mc_user_ext) {
474 home_error = 1;
475 goto check_stock_mc_ext;
476 } else {
477 char *title =
478 g_strdup_printf (_(" %s%s file error"),
479 mc_home, MC_LIB_EXT);
480 message (D_ERROR, title, _("The format of the %smc.ext "
481 "file has changed with version 3.0. It seems that "
482 "the installation failed. Please fetch a fresh "
483 "copy from the Midnight Commander package."),
484 mc_home);
485 g_free (title);
486 return 0;
490 if (home_error) {
491 char *title =
492 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT);
493 message (D_ERROR, title, _("The format of the ~/%s file has "
494 "changed with version 3.0. You may either want to copy "
495 "it from %smc.ext or use that file as an example of how "
496 "to write it."), MC_USER_EXT, mc_home);
497 g_free (title);
500 mc_stat (filename, &mystat);
502 include_target = NULL;
503 include_target_len = 0;
504 for (p = data; *p; p++) {
505 for (q = p; *q == ' ' || *q == '\t'; q++);
506 if (*q == '\n' || !*q)
507 p = q; /* empty line */
508 if (*p == '#') /* comment */
509 while (*p && *p != '\n')
510 p++;
511 if (*p == '\n')
512 continue;
513 if (!*p)
514 break;
515 if (p == q) { /* i.e. starts in the first column, should be
516 * keyword/descNL
518 found = 0;
519 q = strchr (p, '\n');
520 if (q == NULL)
521 q = strchr (p, 0);
522 c = *q;
523 *q = 0;
524 if (include_target) {
525 if ((strncmp (p, "include/", 8) == 0)
526 && (strncmp (p + 8, include_target, include_target_len)
527 == 0))
528 found = 1;
529 } else if (!strncmp (p, "regex/", 6)) {
530 p += 6;
531 /* Do not transform shell patterns, you can use shell/ for
532 * that
534 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
535 found = 1;
536 } else if (!strncmp (p, "directory/", 10)) {
537 if (S_ISDIR (mystat.st_mode)
538 && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
539 found = 1;
540 } else if (!strncmp (p, "shell/", 6)) {
541 p += 6;
542 if (*p == '.' && file_len >= (q - p)) {
543 if (!strncmp (p, filename + file_len - (q - p), q - p))
544 found = 1;
545 } else {
546 if (q - p == file_len && !strncmp (p, filename, q - p))
547 found = 1;
549 } else if (!strncmp (p, "type/", 5)) {
550 int res;
551 p += 5;
552 res = regex_check_type (filename, p, &have_type);
553 if (res == 1)
554 found = 1;
555 if (res == -1)
556 error_flag = 1; /* leave it if file cannot be opened */
557 } else if (!strncmp (p, "default/", 8)) {
558 found = 1;
560 *q = c;
561 p = q;
562 if (!*p)
563 break;
564 } else { /* List of actions */
565 p = q;
566 q = strchr (p, '\n');
567 if (q == NULL)
568 q = strchr (p, 0);
569 if (found && !error_flag) {
570 r = strchr (p, '=');
571 if (r != NULL) {
572 c = *r;
573 *r = 0;
574 if (strcmp (p, "Include") == 0) {
575 char *t;
577 include_target = p + 8;
578 t = strchr (include_target, '\n');
579 if (t)
580 *t = 0;
581 include_target_len = strlen (include_target);
582 if (t)
583 *t = '\n';
585 *r = c;
586 p = q;
587 found = 0;
589 if (!*p)
590 break;
591 continue;
593 if (!strcmp (action, p)) {
594 *r = c;
595 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
597 /* Empty commands just stop searching
598 * through, they don't do anything
600 * We need to copy the filename because exec_extension
601 * may end up invoking update_panels thus making the
602 * filename parameter invalid (ie, most of the time,
603 * we get filename as a pointer from current_panel->dir).
605 if (p < q) {
606 char *filename_copy = g_strdup (filename);
608 exec_extension (filename_copy, r + 1, move_dir,
609 view_at_line_number);
610 g_free (filename_copy);
612 ret = 1;
614 break;
615 } else
616 *r = c;
619 p = q;
620 if (!*p)
621 break;
624 if (error_flag)
625 return -1;
626 return ret;