Merge branch 'mc-4.6'
[midnight-commander.git] / src / ext.c
blob14d888fa30422b555c750521e8b98a3e819c10ff
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 #include <config.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <unistd.h>
31 #include "global.h"
32 #include "tty.h"
33 #include "user.h"
34 #include "main.h"
35 #include "wtools.h"
36 #include "ext.h"
37 #include "view.h"
38 #include "execute.h"
39 #include "history.h"
40 #include "cons.saver.h"
41 #include "layout.h"
43 /* If set, we execute the file command to check the file type */
44 int use_file_to_check_type = 1;
46 /* This variable points to a copy of the mc.ext file in memory
47 * With this we avoid loading/parsing the file each time we
48 * need it
50 static char *data = NULL;
52 void
53 flush_extension_file (void)
55 g_free (data);
56 data = NULL;
59 typedef char *(*quote_func_t) (const char *name, int quote_percent);
61 static void
62 exec_extension (const char *filename, const char *data, int *move_dir,
63 int start_line)
65 char *file_name;
66 int cmd_file_fd;
67 FILE *cmd_file;
68 char *cmd = NULL;
69 int expand_prefix_found = 0;
70 int parameter_found = 0;
71 char prompt[80];
72 int run_view = 0;
73 int def_hex_mode = default_hex_mode, changed_hex_mode = 0;
74 int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0;
75 int written_nonspace = 0;
76 int is_cd = 0;
77 char buffer[1024];
78 char *p = 0;
79 char *localcopy = NULL;
80 int do_local_copy;
81 time_t localmtime = 0;
82 struct stat mystat;
83 quote_func_t quote_func = name_quote;
85 g_return_if_fail (filename != NULL);
86 g_return_if_fail (data != NULL);
88 /* Avoid making a local copy if we are doing a cd */
89 if (!vfs_file_is_local (filename))
90 do_local_copy = 1;
91 else
92 do_local_copy = 0;
95 * All commands should be run in /bin/sh regardless of user shell.
96 * To do that, create temporary shell script and run it.
97 * Sometimes it's not needed (e.g. for %cd and %view commands),
98 * but it's easier to create it anyway.
100 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
102 if (cmd_file_fd == -1) {
103 message (D_ERROR, MSG_ERROR,
104 _(" Cannot create temporary command file \n %s "),
105 unix_error_string (errno));
106 return;
108 cmd_file = fdopen (cmd_file_fd, "w");
109 fputs ("#! /bin/sh\n", cmd_file);
111 prompt[0] = 0;
112 for (; *data && *data != '\n'; data++) {
113 if (parameter_found) {
114 if (*data == '}') {
115 char *parameter;
116 parameter_found = 0;
117 parameter = input_dialog (_(" Parameter "), prompt, MC_HISTORY_EXT_PARAMETER, "");
118 if (!parameter) {
119 /* User canceled */
120 fclose (cmd_file);
121 unlink (file_name);
122 if (localcopy) {
123 mc_ungetlocalcopy (filename, localcopy, 0);
124 g_free (localcopy);
126 g_free (file_name);
127 return;
129 fputs (parameter, cmd_file);
130 written_nonspace = 1;
131 g_free (parameter);
132 } else {
133 size_t len = strlen (prompt);
135 if (len < sizeof (prompt) - 1) {
136 prompt[len] = *data;
137 prompt[len + 1] = 0;
140 } else if (expand_prefix_found) {
141 expand_prefix_found = 0;
142 if (*data == '{')
143 parameter_found = 1;
144 else {
145 int i = check_format_view (data);
146 char *v;
148 if (i) {
149 data += i - 1;
150 run_view = 1;
151 } else if ((i = check_format_cd (data)) > 0) {
152 is_cd = 1;
153 quote_func = fake_name_quote;
154 do_local_copy = 0;
155 p = buffer;
156 data += i - 1;
157 } else if ((i = check_format_var (data, &v)) > 0 && v) {
158 fputs (v, cmd_file);
159 g_free (v);
160 data += i;
161 } else {
162 char *text;
164 if (*data == 'f') {
165 if (do_local_copy) {
166 localcopy = mc_getlocalcopy (filename);
167 if (localcopy == NULL) {
168 fclose (cmd_file);
169 unlink (file_name);
170 g_free (file_name);
171 return;
173 mc_stat (localcopy, &mystat);
174 localmtime = mystat.st_mtime;
175 text = (*quote_func) (localcopy, 0);
176 } else {
177 text = (*quote_func) (filename, 0);
179 } else
180 text = expand_format (NULL, *data, !is_cd);
181 if (!is_cd)
182 fputs (text, cmd_file);
183 else {
184 strcpy (p, text);
185 p = strchr (p, 0);
187 g_free (text);
188 written_nonspace = 1;
191 } else {
192 if (*data == '%')
193 expand_prefix_found = 1;
194 else {
195 if (*data != ' ' && *data != '\t')
196 written_nonspace = 1;
197 if (is_cd)
198 *(p++) = *data;
199 else
200 fputc (*data, cmd_file);
203 } /* for */
206 * Make the script remove itself when it finishes.
207 * Don't do it for the viewer - it may need to rerun the script,
208 * so we clean up after calling view().
210 if (!run_view) {
211 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
214 fclose (cmd_file);
216 if ((run_view && !written_nonspace) || is_cd) {
217 unlink (file_name);
218 g_free (file_name);
219 file_name = NULL;
220 } else {
221 /* Set executable flag on the command file ... */
222 chmod (file_name, S_IRWXU);
223 /* ... but don't rely on it - run /bin/sh explicitly */
224 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
227 if (run_view) {
228 altered_hex_mode = 0;
229 altered_nroff_flag = 0;
230 if (def_hex_mode != default_hex_mode)
231 changed_hex_mode = 1;
232 if (def_nroff_flag != default_nroff_flag)
233 changed_nroff_flag = 1;
235 /* If we've written whitespace only, then just load filename
236 * into view
238 if (written_nonspace) {
239 mc_internal_viewer (cmd, filename, move_dir, start_line);
240 unlink (file_name);
241 } else {
242 mc_internal_viewer (NULL, filename, move_dir, start_line);
244 if (changed_hex_mode && !altered_hex_mode)
245 default_hex_mode = def_hex_mode;
246 if (changed_nroff_flag && !altered_nroff_flag)
247 default_nroff_flag = def_nroff_flag;
248 repaint_screen ();
249 } else if (is_cd) {
250 char *q;
251 *p = 0;
252 p = buffer;
253 /* while (*p == ' ' && *p == '\t')
254 * p++;
256 /* Search last non-space character. Start search at the end in order
257 not to short filenames containing spaces. */
258 q = p + strlen (p) - 1;
259 while (q >= p && (*q == ' ' || *q == '\t'))
260 q--;
261 q[1] = 0;
262 do_cd (p, cd_parse_command);
263 } else {
264 shell_execute (cmd, EXECUTE_INTERNAL);
265 if (console_flag) {
266 handle_console (CONSOLE_SAVE);
267 if (output_lines && keybar_visible) {
268 show_console_contents (output_start_y,
269 LINES - keybar_visible -
270 output_lines - 1,
271 LINES - keybar_visible - 1);
277 g_free (file_name);
278 g_free (cmd);
280 if (localcopy) {
281 mc_stat (localcopy, &mystat);
282 mc_ungetlocalcopy (filename, localcopy,
283 localmtime != mystat.st_mtime);
284 g_free (localcopy);
288 #ifdef FILE_L
289 # define FILE_CMD "file -L "
290 #else
291 # define FILE_CMD "file "
292 #endif
295 * Run the "file" command on the local file.
296 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
298 static int
299 get_file_type_local (const char *filename, char *buf, int buflen)
301 int read_bytes = 0;
303 char *tmp = name_quote (filename, 0);
304 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) 0);
305 FILE *f = popen (command, "r");
307 g_free (tmp);
308 g_free (command);
309 if (f != NULL) {
310 #ifdef __QNXNTO__
311 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
312 (void)pclose (f);
313 return -1;
315 #endif
316 read_bytes = (fgets (buf, buflen, f)
317 != NULL);
318 if (read_bytes == 0)
319 buf[0] = 0;
320 pclose (f);
321 } else {
322 return -1;
325 return (read_bytes > 0);
330 * Invoke the "file" command on the file and match its output against PTR.
331 * have_type is a flag that is set if we already have tried to determine
332 * the type of that file.
333 * Return 1 for match, 0 for no match, -1 errors.
335 static int
336 regex_check_type (const char *filename, const char *ptr, int *have_type)
338 int found = 0;
340 /* Following variables are valid if *have_type is 1 */
341 static char content_string[2048];
342 static int content_shift = 0;
343 static int got_data = 0;
345 if (!use_file_to_check_type) {
346 return 0;
349 if (!*have_type) {
350 char *realname; /* name used with "file" */
351 char *localfile;
352 /* Don't repeate even unsuccessful checks */
353 *have_type = 1;
355 localfile = mc_getlocalcopy (filename);
356 if (!localfile)
357 return -1;
359 realname = localfile;
360 got_data =
361 get_file_type_local (localfile, content_string,
362 sizeof (content_string));
363 mc_ungetlocalcopy (filename, localfile, 0);
365 if (got_data > 0) {
366 char *pp;
368 /* Paranoid termination */
369 content_string[sizeof (content_string) - 1] = 0;
371 if ((pp = strchr (content_string, '\n')) != 0)
372 *pp = 0;
374 if (!strncmp (content_string, realname, strlen (realname))) {
375 /* Skip "realname: " */
376 content_shift = strlen (realname);
377 if (content_string[content_shift] == ':') {
378 /* Solaris' file prints tab(s) after ':' */
379 for (content_shift++;
380 content_string[content_shift] == ' '
381 || content_string[content_shift] == '\t';
382 content_shift++);
385 } else {
386 /* No data */
387 content_string[0] = 0;
389 g_free (realname);
392 if (got_data == -1) {
393 return -1;
396 if (content_string[0]
397 && regexp_match (ptr, content_string + content_shift, match_regex)) {
398 found = 1;
401 return found;
405 /* The second argument is action, i.e. Open, View or Edit
407 * This function returns:
409 * -1 for a failure or user interrupt
410 * 0 if no command was run
411 * 1 if some command was run
413 * If action == "View" then a parameter is checked in the form of "View:%d",
414 * if the value for %d exists, then the viewer is started up at that line number.
417 regex_command (const char *filename, const char *action, int *move_dir)
419 char *p, *q, *r, c;
420 int file_len = strlen (filename);
421 int found = 0;
422 int error_flag = 0;
423 int ret = 0;
424 struct stat mystat;
425 int view_at_line_number;
426 char *include_target;
427 int include_target_len;
428 int have_type = 0; /* Flag used by regex_check_type() */
430 /* Check for the special View:%d parameter */
431 if (strncmp (action, "View:", 5) == 0) {
432 view_at_line_number = atoi (action + 5);
433 action = "View";
434 } else {
435 view_at_line_number = 0;
438 if (data == NULL) {
439 char *extension_file;
440 int mc_user_ext = 1;
441 int home_error = 0;
443 extension_file = concat_dir_and_file (home_dir, MC_USER_EXT);
444 if (!exist_file (extension_file)) {
445 g_free (extension_file);
446 check_stock_mc_ext:
447 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
448 mc_user_ext = 0;
450 data = load_file (extension_file);
451 g_free (extension_file);
452 if (data == NULL)
453 return 0;
455 if (!strstr (data, "default/")) {
456 if (!strstr (data, "regex/") && !strstr (data, "shell/")
457 && !strstr (data, "type/")) {
458 g_free (data);
459 data = NULL;
460 if (mc_user_ext) {
461 home_error = 1;
462 goto check_stock_mc_ext;
463 } else {
464 char *title =
465 g_strdup_printf (_(" %s%s file error"),
466 mc_home, MC_LIB_EXT);
467 message (D_ERROR, title, _("The format of the %smc.ext "
468 "file has changed with version 3.0. It seems that "
469 "the installation failed. Please fetch a fresh "
470 "copy from the Midnight Commander package."),
471 mc_home);
472 g_free (title);
473 return 0;
477 if (home_error) {
478 char *title =
479 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT);
480 message (D_ERROR, title, _("The format of the ~/%s file has "
481 "changed with version 3.0. You may either want to copy "
482 "it from %smc.ext or use that file as an example of how "
483 "to write it."), MC_USER_EXT, mc_home);
484 g_free (title);
487 mc_stat (filename, &mystat);
489 include_target = NULL;
490 include_target_len = 0;
491 for (p = data; *p; p++) {
492 for (q = p; *q == ' ' || *q == '\t'; q++);
493 if (*q == '\n' || !*q)
494 p = q; /* empty line */
495 if (*p == '#') /* comment */
496 while (*p && *p != '\n')
497 p++;
498 if (*p == '\n')
499 continue;
500 if (!*p)
501 break;
502 if (p == q) { /* i.e. starts in the first column, should be
503 * keyword/descNL
505 found = 0;
506 q = strchr (p, '\n');
507 if (q == NULL)
508 q = strchr (p, 0);
509 c = *q;
510 *q = 0;
511 if (include_target) {
512 if ((strncmp (p, "include/", 8) == 0)
513 && (strncmp (p + 8, include_target, include_target_len)
514 == 0))
515 found = 1;
516 } else if (!strncmp (p, "regex/", 6)) {
517 p += 6;
518 /* Do not transform shell patterns, you can use shell/ for
519 * that
521 if (regexp_match (p, filename, match_regex))
522 found = 1;
523 } else if (!strncmp (p, "directory/", 10)) {
524 if (S_ISDIR (mystat.st_mode)
525 && regexp_match (p + 10, filename, match_regex))
526 found = 1;
527 } else if (!strncmp (p, "shell/", 6)) {
528 p += 6;
529 if (*p == '.' && file_len >= (q - p)) {
530 if (!strncmp (p, filename + file_len - (q - p), q - p))
531 found = 1;
532 } else {
533 if (q - p == file_len && !strncmp (p, filename, q - p))
534 found = 1;
536 } else if (!strncmp (p, "type/", 5)) {
537 int res;
538 p += 5;
539 res = regex_check_type (filename, p, &have_type);
540 if (res == 1)
541 found = 1;
542 if (res == -1)
543 error_flag = 1; /* leave it if file cannot be opened */
544 } else if (!strncmp (p, "default/", 8)) {
545 found = 1;
547 *q = c;
548 p = q;
549 if (!*p)
550 break;
551 } else { /* List of actions */
552 p = q;
553 q = strchr (p, '\n');
554 if (q == NULL)
555 q = strchr (p, 0);
556 if (found && !error_flag) {
557 r = strchr (p, '=');
558 if (r != NULL) {
559 c = *r;
560 *r = 0;
561 if (strcmp (p, "Include") == 0) {
562 char *t;
564 include_target = p + 8;
565 t = strchr (include_target, '\n');
566 if (t)
567 *t = 0;
568 include_target_len = strlen (include_target);
569 if (t)
570 *t = '\n';
572 *r = c;
573 p = q;
574 found = 0;
576 if (!*p)
577 break;
578 continue;
580 if (!strcmp (action, p)) {
581 *r = c;
582 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
584 /* Empty commands just stop searching
585 * through, they don't do anything
587 * We need to copy the filename because exec_extension
588 * may end up invoking update_panels thus making the
589 * filename parameter invalid (ie, most of the time,
590 * we get filename as a pointer from current_panel->dir).
592 if (p < q) {
593 char *filename_copy = g_strdup (filename);
595 exec_extension (filename_copy, r + 1, move_dir,
596 view_at_line_number);
597 g_free (filename_copy);
599 ret = 1;
601 break;
602 } else
603 *r = c;
606 p = q;
607 if (!*p)
608 break;
611 if (error_flag)
612 return -1;
613 return ret;