Moved src/util.[ch] into lib, also moved unixcompat.h and utilunix.c.
[midnight-commander.git] / src / ext.c
blobc4b6484fac116d2fa349588232a24a563c1c91c3
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 "user.h"
38 #include "main.h"
39 #include "wtools.h"
40 #include "ext.h"
41 #include "execute.h"
42 #include "history.h"
43 #include "cons.saver.h"
44 #include "layout.h"
45 #include "lib/search/search.h"
46 #include "src/viewer/mcviewer.h"
48 #include "lib/vfs/mc-vfs/vfs.h"
50 /* If set, we execute the file command to check the file type */
51 int use_file_to_check_type = 1;
53 /* This variable points to a copy of the mc.ext file in memory
54 * With this we avoid loading/parsing the file each time we
55 * need it
57 static char *data = NULL;
59 void
60 flush_extension_file (void)
62 g_free (data);
63 data = NULL;
66 typedef char *(*quote_func_t) (const char *name, int quote_percent);
68 static void
69 exec_extension (const char *filename, const char *lc_data, int *move_dir,
70 int start_line)
72 char *fn;
73 char *file_name;
74 int cmd_file_fd;
75 FILE *cmd_file;
76 char *cmd = NULL;
77 int expand_prefix_found = 0;
78 int parameter_found = 0;
79 char lc_prompt[80];
80 int run_view = 0;
81 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0;
82 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0;
83 int written_nonspace = 0;
84 int is_cd = 0;
85 char buffer[1024];
86 char *p = 0;
87 char *localcopy = NULL;
88 int do_local_copy;
89 time_t localmtime = 0;
90 struct stat mystat;
91 quote_func_t quote_func = name_quote;
93 g_return_if_fail (filename != NULL);
94 g_return_if_fail (lc_data != NULL);
96 /* Avoid making a local copy if we are doing a cd */
97 if (!vfs_file_is_local (filename))
98 do_local_copy = 1;
99 else
100 do_local_copy = 0;
103 * All commands should be run in /bin/sh regardless of user shell.
104 * To do that, create temporary shell script and run it.
105 * Sometimes it's not needed (e.g. for %cd and %view commands),
106 * but it's easier to create it anyway.
108 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
110 if (cmd_file_fd == -1) {
111 message (D_ERROR, MSG_ERROR,
112 _(" Cannot create temporary command file \n %s "),
113 unix_error_string (errno));
114 return;
116 cmd_file = fdopen (cmd_file_fd, "w");
117 fputs ("#! /bin/sh\n", cmd_file);
119 lc_prompt[0] = 0;
120 for (; *lc_data && *lc_data != '\n'; lc_data++) {
121 if (parameter_found) {
122 if (*lc_data == '}') {
123 char *parameter;
124 parameter_found = 0;
125 parameter = input_dialog (_(" Parameter "), lc_prompt, MC_HISTORY_EXT_PARAMETER, "");
126 if (!parameter) {
127 /* User canceled */
128 fclose (cmd_file);
129 unlink (file_name);
130 if (localcopy) {
131 mc_ungetlocalcopy (filename, localcopy, 0);
132 g_free (localcopy);
134 g_free (file_name);
135 return;
137 fputs (parameter, cmd_file);
138 written_nonspace = 1;
139 g_free (parameter);
140 } else {
141 size_t len = strlen (lc_prompt);
143 if (len < sizeof (lc_prompt) - 1) {
144 lc_prompt[len] = *lc_data;
145 lc_prompt[len + 1] = 0;
148 } else if (expand_prefix_found) {
149 expand_prefix_found = 0;
150 if (*lc_data == '{')
151 parameter_found = 1;
152 else {
153 int i = check_format_view (lc_data);
154 char *v;
156 if (i) {
157 lc_data += i - 1;
158 run_view = 1;
159 } else if ((i = check_format_cd (lc_data)) > 0) {
160 is_cd = 1;
161 quote_func = fake_name_quote;
162 do_local_copy = 0;
163 p = buffer;
164 lc_data += i - 1;
165 } else if ((i = check_format_var (lc_data, &v)) > 0 && v) {
166 fputs (v, cmd_file);
167 g_free (v);
168 lc_data += i;
169 } else {
170 char *text;
172 if (*lc_data == 'f') {
173 if (do_local_copy) {
174 localcopy = mc_getlocalcopy (filename);
175 if (localcopy == NULL) {
176 fclose (cmd_file);
177 unlink (file_name);
178 g_free (file_name);
179 return;
181 mc_stat (localcopy, &mystat);
182 localmtime = mystat.st_mtime;
183 text = (*quote_func) (localcopy, 0);
184 } else {
185 fn = vfs_canon_and_translate (filename);
186 text = (*quote_func) (fn, 0);
187 g_free (fn);
189 } else {
190 text = expand_format (NULL, *lc_data, !is_cd);
192 if (!is_cd)
193 fputs (text, cmd_file);
194 else {
195 strcpy (p, text);
196 p = strchr (p, 0);
198 g_free (text);
199 written_nonspace = 1;
202 } else {
203 if (*lc_data == '%')
204 expand_prefix_found = 1;
205 else {
206 if (*lc_data != ' ' && *lc_data != '\t')
207 written_nonspace = 1;
208 if (is_cd)
209 *(p++) = *lc_data;
210 else
211 fputc (*lc_data, cmd_file);
214 } /* for */
217 * Make the script remove itself when it finishes.
218 * Don't do it for the viewer - it may need to rerun the script,
219 * so we clean up after calling view().
221 if (!run_view) {
222 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
225 fclose (cmd_file);
227 if ((run_view && !written_nonspace) || is_cd) {
228 unlink (file_name);
229 g_free (file_name);
230 file_name = NULL;
231 } else {
232 /* Set executable flag on the command file ... */
233 chmod (file_name, S_IRWXU);
234 /* ... but don't rely on it - run /bin/sh explicitly */
235 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
238 if (run_view) {
239 mcview_altered_hex_mode = 0;
240 mcview_altered_nroff_flag = 0;
241 if (def_hex_mode != mcview_default_hex_mode)
242 changed_hex_mode = 1;
243 if (def_nroff_flag != mcview_default_nroff_flag)
244 changed_nroff_flag = 1;
246 /* If we've written whitespace only, then just load filename
247 * into view
249 if (written_nonspace) {
250 mcview_viewer (cmd, filename, move_dir, start_line);
251 unlink (file_name);
252 } else {
253 mcview_viewer (NULL, filename, move_dir, start_line);
255 if (changed_hex_mode && !mcview_altered_hex_mode)
256 mcview_default_hex_mode = def_hex_mode;
257 if (changed_nroff_flag && !mcview_altered_nroff_flag)
258 mcview_default_nroff_flag = def_nroff_flag;
259 repaint_screen ();
260 } else if (is_cd) {
261 char *q;
262 *p = 0;
263 p = buffer;
264 /* while (*p == ' ' && *p == '\t')
265 * p++;
267 /* Search last non-space character. Start search at the end in order
268 not to short filenames containing spaces. */
269 q = p + strlen (p) - 1;
270 while (q >= p && (*q == ' ' || *q == '\t'))
271 q--;
272 q[1] = 0;
273 do_cd (p, cd_parse_command);
274 } else {
275 shell_execute (cmd, EXECUTE_INTERNAL);
276 if (console_flag) {
277 handle_console (CONSOLE_SAVE);
278 if (output_lines && keybar_visible) {
279 show_console_contents (output_start_y,
280 LINES - keybar_visible -
281 output_lines - 1,
282 LINES - keybar_visible - 1);
288 g_free (file_name);
289 g_free (cmd);
291 if (localcopy) {
292 mc_stat (localcopy, &mystat);
293 mc_ungetlocalcopy (filename, localcopy,
294 localmtime != mystat.st_mtime);
295 g_free (localcopy);
299 #ifdef FILE_L
300 # define FILE_CMD "file -L "
301 #else
302 # define FILE_CMD "file "
303 #endif
306 * Run the "file" command on the local file.
307 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
309 static int
310 get_file_type_local (const char *filename, char *buf, int buflen)
312 int read_bytes = 0;
314 char *tmp = name_quote (filename, 0);
315 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) NULL);
316 FILE *f = popen (command, "r");
318 g_free (tmp);
319 g_free (command);
320 if (f != NULL) {
321 #ifdef __QNXNTO__
322 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
323 (void)pclose (f);
324 return -1;
326 #endif
327 read_bytes = (fgets (buf, buflen, f)
328 != NULL);
329 if (read_bytes == 0)
330 buf[0] = 0;
331 pclose (f);
332 } else {
333 return -1;
336 return (read_bytes > 0);
341 * Invoke the "file" command on the file and match its output against PTR.
342 * have_type is a flag that is set if we already have tried to determine
343 * the type of that file.
344 * Return 1 for match, 0 for no match, -1 errors.
346 static int
347 regex_check_type (const char *filename, const char *ptr, int *have_type)
349 int found = 0;
351 /* Following variables are valid if *have_type is 1 */
352 static char content_string[2048];
353 static int content_shift = 0;
354 static int got_data = 0;
356 if (!use_file_to_check_type) {
357 return 0;
360 if (!*have_type) {
361 char *realname; /* name used with "file" */
362 char *localfile;
363 /* Don't repeate even unsuccessful checks */
364 *have_type = 1;
366 localfile = mc_getlocalcopy (filename);
367 if (!localfile)
368 return -1;
370 realname = localfile;
371 got_data =
372 get_file_type_local (localfile, content_string,
373 sizeof (content_string));
374 mc_ungetlocalcopy (filename, localfile, 0);
376 if (got_data > 0) {
377 char *pp;
379 /* Paranoid termination */
380 content_string[sizeof (content_string) - 1] = 0;
382 if ((pp = strchr (content_string, '\n')) != 0)
383 *pp = 0;
385 if (!strncmp (content_string, realname, strlen (realname))) {
386 /* Skip "realname: " */
387 content_shift = strlen (realname);
388 if (content_string[content_shift] == ':') {
389 /* Solaris' file prints tab(s) after ':' */
390 for (content_shift++;
391 content_string[content_shift] == ' '
392 || content_string[content_shift] == '\t';
393 content_shift++);
396 } else {
397 /* No data */
398 content_string[0] = 0;
400 g_free (realname);
403 if (got_data == -1) {
404 return -1;
407 if (content_string[0]
408 && mc_search (ptr, content_string + content_shift, MC_SEARCH_T_REGEX)) {
409 found = 1;
412 return found;
416 /* The second argument is action, i.e. Open, View or Edit
418 * This function returns:
420 * -1 for a failure or user interrupt
421 * 0 if no command was run
422 * 1 if some command was run
424 * If action == "View" then a parameter is checked in the form of "View:%d",
425 * if the value for %d exists, then the viewer is started up at that line number.
428 regex_command (const char *filename, const char *action, int *move_dir)
430 char *p, *q, *r, c;
431 int file_len = strlen (filename);
432 int found = 0;
433 int error_flag = 0;
434 int ret = 0;
435 struct stat mystat;
436 int view_at_line_number;
437 char *include_target;
438 int include_target_len;
439 int have_type = 0; /* Flag used by regex_check_type() */
441 /* Check for the special View:%d parameter */
442 if (strncmp (action, "View:", 5) == 0) {
443 view_at_line_number = atoi (action + 5);
444 action = "View";
445 } else {
446 view_at_line_number = 0;
449 if (data == NULL) {
450 char *extension_file;
451 int mc_user_ext = 1;
452 int home_error = 0;
454 extension_file = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
455 if (!exist_file (extension_file)) {
456 g_free (extension_file);
457 check_stock_mc_ext:
458 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
459 if (!exist_file (extension_file)) {
460 g_free (extension_file);
461 extension_file = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
463 mc_user_ext = 0;
465 data = load_file (extension_file);
466 g_free (extension_file);
467 if (data == NULL)
468 return 0;
470 if (!strstr (data, "default/")) {
471 if (!strstr (data, "regex/") && !strstr (data, "shell/")
472 && !strstr (data, "type/")) {
473 g_free (data);
474 data = NULL;
475 if (mc_user_ext) {
476 home_error = 1;
477 goto check_stock_mc_ext;
478 } else {
479 char *title =
480 g_strdup_printf (_(" %s%s file error"),
481 mc_home, MC_LIB_EXT);
482 message (D_ERROR, title, _("The format of the %smc.ext "
483 "file has changed with version 3.0. It seems that "
484 "the installation failed. Please fetch a fresh "
485 "copy from the Midnight Commander package."),
486 mc_home);
487 g_free (title);
488 return 0;
492 if (home_error) {
493 char *title =
494 g_strdup_printf (_(" ~/%s file error "), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE);
495 message (D_ERROR, title, _("The format of the ~/%s file has "
496 "changed with version 3.0. You may either want to copy "
497 "it from %smc.ext or use that file as an example of how "
498 "to write it."), MC_USERCONF_DIR PATH_SEP_STR MC_FILEBIND_FILE, mc_home);
499 g_free (title);
502 mc_stat (filename, &mystat);
504 include_target = NULL;
505 include_target_len = 0;
506 for (p = data; *p; p++) {
507 for (q = p; *q == ' ' || *q == '\t'; q++);
508 if (*q == '\n' || !*q)
509 p = q; /* empty line */
510 if (*p == '#') /* comment */
511 while (*p && *p != '\n')
512 p++;
513 if (*p == '\n')
514 continue;
515 if (!*p)
516 break;
517 if (p == q) { /* i.e. starts in the first column, should be
518 * keyword/descNL
520 found = 0;
521 q = strchr (p, '\n');
522 if (q == NULL)
523 q = strchr (p, 0);
524 c = *q;
525 *q = 0;
526 if (include_target) {
527 if ((strncmp (p, "include/", 8) == 0)
528 && (strncmp (p + 8, include_target, include_target_len)
529 == 0))
530 found = 1;
531 } else if (!strncmp (p, "regex/", 6)) {
532 p += 6;
533 /* Do not transform shell patterns, you can use shell/ for
534 * that
536 if (mc_search (p, filename, MC_SEARCH_T_REGEX))
537 found = 1;
538 } else if (!strncmp (p, "directory/", 10)) {
539 if (S_ISDIR (mystat.st_mode)
540 && mc_search (p + 10, filename, MC_SEARCH_T_REGEX))
541 found = 1;
542 } else if (!strncmp (p, "shell/", 6)) {
543 p += 6;
544 if (*p == '.' && file_len >= (q - p)) {
545 if (!strncmp (p, filename + file_len - (q - p), q - p))
546 found = 1;
547 } else {
548 if (q - p == file_len && !strncmp (p, filename, q - p))
549 found = 1;
551 } else if (!strncmp (p, "type/", 5)) {
552 int res;
553 p += 5;
554 res = regex_check_type (filename, p, &have_type);
555 if (res == 1)
556 found = 1;
557 if (res == -1)
558 error_flag = 1; /* leave it if file cannot be opened */
559 } else if (!strncmp (p, "default/", 8)) {
560 found = 1;
562 *q = c;
563 p = q;
564 if (!*p)
565 break;
566 } else { /* List of actions */
567 p = q;
568 q = strchr (p, '\n');
569 if (q == NULL)
570 q = strchr (p, 0);
571 if (found && !error_flag) {
572 r = strchr (p, '=');
573 if (r != NULL) {
574 c = *r;
575 *r = 0;
576 if (strcmp (p, "Include") == 0) {
577 char *t;
579 include_target = p + 8;
580 t = strchr (include_target, '\n');
581 if (t)
582 *t = 0;
583 include_target_len = strlen (include_target);
584 if (t)
585 *t = '\n';
587 *r = c;
588 p = q;
589 found = 0;
591 if (!*p)
592 break;
593 continue;
595 if (!strcmp (action, p)) {
596 *r = c;
597 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
599 /* Empty commands just stop searching
600 * through, they don't do anything
602 * We need to copy the filename because exec_extension
603 * may end up invoking update_panels thus making the
604 * filename parameter invalid (ie, most of the time,
605 * we get filename as a pointer from current_panel->dir).
607 if (p < q) {
608 char *filename_copy = g_strdup (filename);
610 exec_extension (filename_copy, r + 1, move_dir,
611 view_at_line_number);
612 g_free (filename_copy);
614 ret = 1;
616 break;
617 } else
618 *r = c;
621 p = q;
622 if (!*p)
623 break;
626 if (error_flag)
627 return -1;
628 return ret;