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