Update base version to 4.6.1.
[midnight-commander.git] / src / ext.c
blob6f7af7d06a38c9bea2101d7e61760a837c2f7207
1 /* Extension dependent execution.
2 Copyright (C) 1994, 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
5 1994 Miguel de Icaza
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include <config.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <string.h>
28 #include <unistd.h>
30 #include "global.h"
31 #include "tty.h"
32 #include "user.h"
33 #include "main.h"
34 #include "wtools.h"
35 #include "ext.h"
36 #include "view.h"
37 #include "execute.h"
39 #include "cons.saver.h"
40 #include "layout.h"
42 /* If set, we execute the file command to check the file type */
43 int use_file_to_check_type = 1;
45 /* This variable points to a copy of the mc.ext file in memory
46 * With this we avoid loading/parsing the file each time we
47 * need it
49 static char *data = NULL;
51 void
52 flush_extension_file (void)
54 g_free (data);
55 data = NULL;
58 typedef char *(*quote_func_t) (const char *name, int quote_percent);
60 static void
61 exec_extension (const char *filename, const char *data, int *move_dir,
62 int start_line)
64 char *file_name;
65 int cmd_file_fd;
66 FILE *cmd_file;
67 char *cmd = NULL;
68 int expand_prefix_found = 0;
69 int parameter_found = 0;
70 char prompt[80];
71 int run_view = 0;
72 int def_hex_mode = default_hex_mode, changed_hex_mode = 0;
73 int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0;
74 int written_nonspace = 0;
75 int is_cd = 0;
76 char buffer[1024];
77 char *p = 0;
78 char *localcopy = NULL;
79 int do_local_copy;
80 time_t localmtime = 0;
81 struct stat mystat;
82 quote_func_t quote_func = name_quote;
84 g_return_if_fail (filename != NULL);
85 g_return_if_fail (data != NULL);
87 /* Avoid making a local copy if we are doing a cd */
88 if (!vfs_file_is_local (filename))
89 do_local_copy = 1;
90 else
91 do_local_copy = 0;
94 * All commands should be run in /bin/sh regardless of user shell.
95 * To do that, create temporary shell script and run it.
96 * Sometimes it's not needed (e.g. for %cd and %view commands),
97 * but it's easier to create it anyway.
99 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
101 if (cmd_file_fd == -1) {
102 message (1, MSG_ERROR,
103 _(" Cannot create temporary command file \n %s "),
104 unix_error_string (errno));
105 return;
107 cmd_file = fdopen (cmd_file_fd, "w");
108 fputs ("#! /bin/sh\n", cmd_file);
110 prompt[0] = 0;
111 for (; *data && *data != '\n'; data++) {
112 if (parameter_found) {
113 if (*data == '}') {
114 char *parameter;
115 parameter_found = 0;
116 parameter = input_dialog (_(" Parameter "), prompt, "");
117 if (!parameter) {
118 /* User canceled */
119 fclose (cmd_file);
120 unlink (file_name);
121 if (localcopy) {
122 mc_ungetlocalcopy (filename, localcopy, 0);
123 g_free (localcopy);
125 g_free (file_name);
126 return;
128 fputs (parameter, cmd_file);
129 written_nonspace = 1;
130 g_free (parameter);
131 } else {
132 size_t len = strlen (prompt);
134 if (len < sizeof (prompt) - 1) {
135 prompt[len] = *data;
136 prompt[len + 1] = 0;
139 } else if (expand_prefix_found) {
140 expand_prefix_found = 0;
141 if (*data == '{')
142 parameter_found = 1;
143 else {
144 int i = check_format_view (data);
145 char *v;
147 if (i) {
148 data += i - 1;
149 run_view = 1;
150 } else if ((i = check_format_cd (data)) > 0) {
151 is_cd = 1;
152 quote_func = fake_name_quote;
153 do_local_copy = 0;
154 p = buffer;
155 data += i - 1;
156 } else if ((i = check_format_var (data, &v)) > 0 && v) {
157 fputs (v, cmd_file);
158 g_free (v);
159 data += i;
160 } else {
161 char *text;
163 if (*data == 'f') {
164 if (do_local_copy) {
165 localcopy = mc_getlocalcopy (filename);
166 if (localcopy == NULL) {
167 fclose (cmd_file);
168 unlink (file_name);
169 g_free (file_name);
170 return;
172 mc_stat (localcopy, &mystat);
173 localmtime = mystat.st_mtime;
174 text = (*quote_func) (localcopy, 0);
175 } else {
176 text = (*quote_func) (filename, 0);
178 } else
179 text = expand_format (NULL, *data, !is_cd);
180 if (!is_cd)
181 fputs (text, cmd_file);
182 else {
183 strcpy (p, text);
184 p = strchr (p, 0);
186 g_free (text);
187 written_nonspace = 1;
190 } else {
191 if (*data == '%')
192 expand_prefix_found = 1;
193 else {
194 if (*data != ' ' && *data != '\t')
195 written_nonspace = 1;
196 if (is_cd)
197 *(p++) = *data;
198 else
199 fputc (*data, cmd_file);
202 } /* for */
205 * Make the script remove itself when it finishes.
206 * Don't do it for the viewer - it may need to rerun the script,
207 * so we clean up after calling view().
209 if (!run_view) {
210 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
213 fclose (cmd_file);
215 if ((run_view && !written_nonspace) || is_cd) {
216 unlink (file_name);
217 g_free (file_name);
218 file_name = NULL;
219 } else {
220 /* Set executable flag on the command file ... */
221 chmod (file_name, S_IRWXU);
222 /* ... but don't rely on it - run /bin/sh explicitly */
223 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
226 if (run_view) {
227 altered_hex_mode = 0;
228 altered_nroff_flag = 0;
229 if (def_hex_mode != default_hex_mode)
230 changed_hex_mode = 1;
231 if (def_nroff_flag != default_nroff_flag)
232 changed_nroff_flag = 1;
234 /* If we've written whitespace only, then just load filename
235 * into view
237 if (written_nonspace) {
238 view (cmd, filename, move_dir, start_line);
239 unlink (file_name);
240 } else {
241 view (0, filename, move_dir, start_line);
243 if (changed_hex_mode && !altered_hex_mode)
244 default_hex_mode = def_hex_mode;
245 if (changed_nroff_flag && !altered_nroff_flag)
246 default_nroff_flag = def_nroff_flag;
247 repaint_screen ();
248 } else if (is_cd) {
249 char *q;
250 *p = 0;
251 p = buffer;
252 /* while (*p == ' ' && *p == '\t')
253 * p++;
255 /* Search last non-space character. Start search at the end in order
256 not to short filenames containing spaces. */
257 q = p + strlen (p) - 1;
258 while (q >= p && (*q == ' ' || *q == '\t'))
259 q--;
260 q[1] = 0;
261 do_cd (p, cd_parse_command);
262 } else {
263 shell_execute (cmd, EXECUTE_INTERNAL);
264 if (console_flag) {
265 handle_console (CONSOLE_SAVE);
266 if (output_lines && keybar_visible) {
267 show_console_contents (output_start_y,
268 LINES - keybar_visible -
269 output_lines - 1,
270 LINES - keybar_visible - 1);
276 g_free (file_name);
277 g_free (cmd);
279 if (localcopy) {
280 mc_stat (localcopy, &mystat);
281 mc_ungetlocalcopy (filename, localcopy,
282 localmtime != mystat.st_mtime);
283 g_free (localcopy);
287 #ifdef FILE_L
288 # define FILE_CMD "file -L "
289 #else
290 # define FILE_CMD "file "
291 #endif
294 * Run the "file" command on the local file.
295 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
297 static int
298 get_file_type_local (const char *filename, char *buf, int buflen)
300 int read_bytes = 0;
302 char *tmp = name_quote (filename, 0);
303 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) 0);
304 FILE *f = popen (command, "r");
306 g_free (tmp);
307 g_free (command);
308 if (f != NULL) {
309 #ifdef __QNXNTO__
310 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
311 (void)pclose (f);
312 return -1;
314 #endif
315 read_bytes = (fgets (buf, buflen, f)
316 != NULL);
317 if (read_bytes == 0)
318 buf[0] = 0;
319 pclose (f);
320 } else {
321 return -1;
324 return (read_bytes > 0);
329 * Invoke the "file" command on the file and match its output against PTR.
330 * have_type is a flag that is set if we already have tried to determine
331 * the type of that file.
332 * Return 1 for match, 0 for no match, -1 errors.
334 static int
335 regex_check_type (const char *filename, const char *ptr, int *have_type)
337 int found = 0;
339 /* Following variables are valid if *have_type is 1 */
340 static char content_string[2048];
341 static int content_shift = 0;
342 static int got_data = 0;
344 if (!use_file_to_check_type) {
345 return 0;
348 if (!*have_type) {
349 char *realname; /* name used with "file" */
350 char *localfile;
351 /* Don't repeate even unsuccessful checks */
352 *have_type = 1;
354 localfile = mc_getlocalcopy (filename);
355 if (!localfile)
356 return -1;
358 realname = localfile;
359 got_data =
360 get_file_type_local (localfile, content_string,
361 sizeof (content_string));
362 mc_ungetlocalcopy (filename, localfile, 0);
364 if (got_data > 0) {
365 char *pp;
367 /* Paranoid termination */
368 content_string[sizeof (content_string) - 1] = 0;
370 if ((pp = strchr (content_string, '\n')) != 0)
371 *pp = 0;
373 if (!strncmp (content_string, realname, strlen (realname))) {
374 /* Skip "realname: " */
375 content_shift = strlen (realname);
376 if (content_string[content_shift] == ':') {
377 /* Solaris' file prints tab(s) after ':' */
378 for (content_shift++;
379 content_string[content_shift] == ' '
380 || content_string[content_shift] == '\t';
381 content_shift++);
384 } else {
385 /* No data */
386 content_string[0] = 0;
388 g_free (realname);
391 if (got_data == -1) {
392 return -1;
395 if (content_string[0]
396 && regexp_match (ptr, content_string + content_shift, match_regex)) {
397 found = 1;
400 return found;
404 /* The second argument is action, i.e. Open, View or Edit
406 * This function returns:
408 * -1 for a failure or user interrupt
409 * 0 if no command was run
410 * 1 if some command was run
412 * If action == "View" then a parameter is checked in the form of "View:%d",
413 * if the value for %d exists, then the viewer is started up at that line number.
416 regex_command (const char *filename, const char *action, int *move_dir)
418 char *p, *q, *r, c;
419 int file_len = strlen (filename);
420 int found = 0;
421 int error_flag = 0;
422 int ret = 0;
423 struct stat mystat;
424 int view_at_line_number;
425 char *include_target;
426 int include_target_len;
427 int have_type = 0; /* Flag used by regex_check_type() */
429 /* Check for the special View:%d parameter */
430 if (strncmp (action, "View:", 5) == 0) {
431 view_at_line_number = atoi (action + 5);
432 action = "View";
433 } else {
434 view_at_line_number = 0;
437 if (data == NULL) {
438 char *extension_file;
439 int mc_user_ext = 1;
440 int home_error = 0;
442 extension_file = concat_dir_and_file (home_dir, MC_USER_EXT);
443 if (!exist_file (extension_file)) {
444 g_free (extension_file);
445 check_stock_mc_ext:
446 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
447 mc_user_ext = 0;
449 data = load_file (extension_file);
450 g_free (extension_file);
451 if (data == NULL)
452 return 0;
454 if (!strstr (data, "default/")) {
455 if (!strstr (data, "regex/") && !strstr (data, "shell/")
456 && !strstr (data, "type/")) {
457 g_free (data);
458 data = NULL;
459 if (mc_user_ext) {
460 home_error = 1;
461 goto check_stock_mc_ext;
462 } else {
463 char *title =
464 g_strdup_printf (_(" %s%s file error"),
465 mc_home, MC_LIB_EXT);
466 message (1, title, _("The format of the %smc.ext "
467 "file has changed with version 3.0. It seems that "
468 "the installation failed. Please fetch a fresh "
469 "copy from the Midnight Commander package."),
470 mc_home);
471 g_free (title);
472 return 0;
476 if (home_error) {
477 char *title =
478 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT);
479 message (1, title, _("The format of the ~/%s file has "
480 "changed with version 3.0. You may either want to copy "
481 "it from %smc.ext or use that file as an example of how "
482 "to write it."), MC_USER_EXT, mc_home);
483 g_free (title);
486 mc_stat (filename, &mystat);
488 include_target = NULL;
489 include_target_len = 0;
490 for (p = data; *p; p++) {
491 for (q = p; *q == ' ' || *q == '\t'; q++);
492 if (*q == '\n' || !*q)
493 p = q; /* empty line */
494 if (*p == '#') /* comment */
495 while (*p && *p != '\n')
496 p++;
497 if (*p == '\n')
498 continue;
499 if (!*p)
500 break;
501 if (p == q) { /* i.e. starts in the first column, should be
502 * keyword/descNL
504 found = 0;
505 q = strchr (p, '\n');
506 if (q == NULL)
507 q = strchr (p, 0);
508 c = *q;
509 *q = 0;
510 if (include_target) {
511 if ((strncmp (p, "include/", 8) == 0)
512 && (strncmp (p + 8, include_target, include_target_len)
513 == 0))
514 found = 1;
515 } else if (!strncmp (p, "regex/", 6)) {
516 p += 6;
517 /* Do not transform shell patterns, you can use shell/ for
518 * that
520 if (regexp_match (p, filename, match_regex))
521 found = 1;
522 } else if (!strncmp (p, "directory/", 10)) {
523 if (S_ISDIR (mystat.st_mode)
524 && regexp_match (p + 10, filename, match_regex))
525 found = 1;
526 } else if (!strncmp (p, "shell/", 6)) {
527 p += 6;
528 if (*p == '.' && file_len >= (q - p)) {
529 if (!strncmp (p, filename + file_len - (q - p), q - p))
530 found = 1;
531 } else {
532 if (q - p == file_len && !strncmp (p, filename, q - p))
533 found = 1;
535 } else if (!strncmp (p, "type/", 5)) {
536 int res;
537 p += 5;
538 res = regex_check_type (filename, p, &have_type);
539 if (res == 1)
540 found = 1;
541 if (res == -1)
542 error_flag = 1; /* leave it if file cannot be opened */
543 } else if (!strncmp (p, "default/", 8)) {
544 found = 1;
546 *q = c;
547 p = q;
548 if (!*p)
549 break;
550 } else { /* List of actions */
551 p = q;
552 q = strchr (p, '\n');
553 if (q == NULL)
554 q = strchr (p, 0);
555 if (found && !error_flag) {
556 r = strchr (p, '=');
557 if (r != NULL) {
558 c = *r;
559 *r = 0;
560 if (strcmp (p, "Include") == 0) {
561 char *t;
563 include_target = p + 8;
564 t = strchr (include_target, '\n');
565 if (t)
566 *t = 0;
567 include_target_len = strlen (include_target);
568 if (t)
569 *t = '\n';
571 *r = c;
572 p = q;
573 found = 0;
575 if (!*p)
576 break;
577 continue;
579 if (!strcmp (action, p)) {
580 *r = c;
581 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
583 /* Empty commands just stop searching
584 * through, they don't do anything
586 * We need to copy the filename because exec_extension
587 * may end up invoking update_panels thus making the
588 * filename parameter invalid (ie, most of the time,
589 * we get filename as a pointer from current_panel->dir).
591 if (p < q) {
592 char *filename_copy = g_strdup (filename);
594 exec_extension (filename_copy, r + 1, move_dir,
595 view_at_line_number);
596 g_free (filename_copy);
598 ret = 1;
600 break;
601 } else
602 *r = c;
605 p = q;
606 if (!*p)
607 break;
610 if (error_flag)
611 return -1;
612 return ret;