Rework strutils for usage GString instread of self-made buffers.
[midnight-commander.git] / src / ext.c
blob527be99b9b4f7d745c9790c5a479ebbace6933f6
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 "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"
38 #include "history.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 *fn;
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 fn = vfs_canon_and_translate (filename);
178 text = (*quote_func) (fn, 0);
179 g_free (fn);
181 } else {
182 text = expand_format (NULL, *data, !is_cd);
184 if (!is_cd)
185 fputs (text, cmd_file);
186 else {
187 strcpy (p, text);
188 p = strchr (p, 0);
190 g_free (text);
191 written_nonspace = 1;
194 } else {
195 if (*data == '%')
196 expand_prefix_found = 1;
197 else {
198 if (*data != ' ' && *data != '\t')
199 written_nonspace = 1;
200 if (is_cd)
201 *(p++) = *data;
202 else
203 fputc (*data, cmd_file);
206 } /* for */
209 * Make the script remove itself when it finishes.
210 * Don't do it for the viewer - it may need to rerun the script,
211 * so we clean up after calling view().
213 if (!run_view) {
214 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
217 fclose (cmd_file);
219 if ((run_view && !written_nonspace) || is_cd) {
220 unlink (file_name);
221 g_free (file_name);
222 file_name = NULL;
223 } else {
224 /* Set executable flag on the command file ... */
225 chmod (file_name, S_IRWXU);
226 /* ... but don't rely on it - run /bin/sh explicitly */
227 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
230 if (run_view) {
231 altered_hex_mode = 0;
232 altered_nroff_flag = 0;
233 if (def_hex_mode != default_hex_mode)
234 changed_hex_mode = 1;
235 if (def_nroff_flag != default_nroff_flag)
236 changed_nroff_flag = 1;
238 /* If we've written whitespace only, then just load filename
239 * into view
241 if (written_nonspace) {
242 mc_internal_viewer (cmd, filename, move_dir, start_line);
243 unlink (file_name);
244 } else {
245 mc_internal_viewer (NULL, filename, move_dir, start_line);
247 if (changed_hex_mode && !altered_hex_mode)
248 default_hex_mode = def_hex_mode;
249 if (changed_nroff_flag && !altered_nroff_flag)
250 default_nroff_flag = def_nroff_flag;
251 repaint_screen ();
252 } else if (is_cd) {
253 char *q;
254 *p = 0;
255 p = buffer;
256 /* while (*p == ' ' && *p == '\t')
257 * p++;
259 /* Search last non-space character. Start search at the end in order
260 not to short filenames containing spaces. */
261 q = p + strlen (p) - 1;
262 while (q >= p && (*q == ' ' || *q == '\t'))
263 q--;
264 q[1] = 0;
265 do_cd (p, cd_parse_command);
266 } else {
267 shell_execute (cmd, EXECUTE_INTERNAL);
268 if (console_flag) {
269 handle_console (CONSOLE_SAVE);
270 if (output_lines && keybar_visible) {
271 show_console_contents (output_start_y,
272 LINES - keybar_visible -
273 output_lines - 1,
274 LINES - keybar_visible - 1);
280 g_free (file_name);
281 g_free (cmd);
283 if (localcopy) {
284 mc_stat (localcopy, &mystat);
285 mc_ungetlocalcopy (filename, localcopy,
286 localmtime != mystat.st_mtime);
287 g_free (localcopy);
291 #ifdef FILE_L
292 # define FILE_CMD "file -L "
293 #else
294 # define FILE_CMD "file "
295 #endif
298 * Run the "file" command on the local file.
299 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
301 static int
302 get_file_type_local (const char *filename, char *buf, int buflen)
304 int read_bytes = 0;
306 char *tmp = name_quote (filename, 0);
307 char *command = g_strconcat (FILE_CMD, tmp, " 2>/dev/null", (char *) 0);
308 FILE *f = popen (command, "r");
310 g_free (tmp);
311 g_free (command);
312 if (f != NULL) {
313 #ifdef __QNXNTO__
314 if (setvbuf (f, NULL, _IOFBF, 0) != 0) {
315 (void)pclose (f);
316 return -1;
318 #endif
319 read_bytes = (fgets (buf, buflen, f)
320 != NULL);
321 if (read_bytes == 0)
322 buf[0] = 0;
323 pclose (f);
324 } else {
325 return -1;
328 return (read_bytes > 0);
333 * Invoke the "file" command on the file and match its output against PTR.
334 * have_type is a flag that is set if we already have tried to determine
335 * the type of that file.
336 * Return 1 for match, 0 for no match, -1 errors.
338 static int
339 regex_check_type (const char *filename, const char *ptr, int *have_type)
341 int found = 0;
343 /* Following variables are valid if *have_type is 1 */
344 static char content_string[2048];
345 static int content_shift = 0;
346 static int got_data = 0;
348 if (!use_file_to_check_type) {
349 return 0;
352 if (!*have_type) {
353 char *realname; /* name used with "file" */
354 char *localfile;
355 /* Don't repeate even unsuccessful checks */
356 *have_type = 1;
358 localfile = mc_getlocalcopy (filename);
359 if (!localfile)
360 return -1;
362 realname = localfile;
363 got_data =
364 get_file_type_local (localfile, content_string,
365 sizeof (content_string));
366 mc_ungetlocalcopy (filename, localfile, 0);
368 if (got_data > 0) {
369 char *pp;
371 /* Paranoid termination */
372 content_string[sizeof (content_string) - 1] = 0;
374 if ((pp = strchr (content_string, '\n')) != 0)
375 *pp = 0;
377 if (!strncmp (content_string, realname, strlen (realname))) {
378 /* Skip "realname: " */
379 content_shift = strlen (realname);
380 if (content_string[content_shift] == ':') {
381 /* Solaris' file prints tab(s) after ':' */
382 for (content_shift++;
383 content_string[content_shift] == ' '
384 || content_string[content_shift] == '\t';
385 content_shift++);
388 } else {
389 /* No data */
390 content_string[0] = 0;
392 g_free (realname);
395 if (got_data == -1) {
396 return -1;
399 if (content_string[0]
400 && regexp_match (ptr, content_string + content_shift, match_regex)) {
401 found = 1;
404 return found;
408 /* The second argument is action, i.e. Open, View or Edit
410 * This function returns:
412 * -1 for a failure or user interrupt
413 * 0 if no command was run
414 * 1 if some command was run
416 * If action == "View" then a parameter is checked in the form of "View:%d",
417 * if the value for %d exists, then the viewer is started up at that line number.
420 regex_command (const char *filename, const char *action, int *move_dir)
422 char *p, *q, *r, c;
423 int file_len = strlen (filename);
424 int found = 0;
425 int error_flag = 0;
426 int ret = 0;
427 struct stat mystat;
428 int view_at_line_number;
429 char *include_target;
430 int include_target_len;
431 int have_type = 0; /* Flag used by regex_check_type() */
433 /* Check for the special View:%d parameter */
434 if (strncmp (action, "View:", 5) == 0) {
435 view_at_line_number = atoi (action + 5);
436 action = "View";
437 } else {
438 view_at_line_number = 0;
441 if (data == NULL) {
442 char *extension_file;
443 int mc_user_ext = 1;
444 int home_error = 0;
446 extension_file = concat_dir_and_file (home_dir, MC_USER_EXT);
447 if (!exist_file (extension_file)) {
448 g_free (extension_file);
449 check_stock_mc_ext:
450 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
451 mc_user_ext = 0;
453 data = load_file (extension_file);
454 g_free (extension_file);
455 if (data == NULL)
456 return 0;
458 if (!strstr (data, "default/")) {
459 if (!strstr (data, "regex/") && !strstr (data, "shell/")
460 && !strstr (data, "type/")) {
461 g_free (data);
462 data = NULL;
463 if (mc_user_ext) {
464 home_error = 1;
465 goto check_stock_mc_ext;
466 } else {
467 char *title =
468 g_strdup_printf (_(" %s%s file error"),
469 mc_home, MC_LIB_EXT);
470 message (D_ERROR, title, _("The format of the %smc.ext "
471 "file has changed with version 3.0. It seems that "
472 "the installation failed. Please fetch a fresh "
473 "copy from the Midnight Commander package."),
474 mc_home);
475 g_free (title);
476 return 0;
480 if (home_error) {
481 char *title =
482 g_strdup_printf (_(" ~/%s file error "), MC_USER_EXT);
483 message (D_ERROR, title, _("The format of the ~/%s file has "
484 "changed with version 3.0. You may either want to copy "
485 "it from %smc.ext or use that file as an example of how "
486 "to write it."), MC_USER_EXT, mc_home);
487 g_free (title);
490 mc_stat (filename, &mystat);
492 include_target = NULL;
493 include_target_len = 0;
494 for (p = data; *p; p++) {
495 for (q = p; *q == ' ' || *q == '\t'; q++);
496 if (*q == '\n' || !*q)
497 p = q; /* empty line */
498 if (*p == '#') /* comment */
499 while (*p && *p != '\n')
500 p++;
501 if (*p == '\n')
502 continue;
503 if (!*p)
504 break;
505 if (p == q) { /* i.e. starts in the first column, should be
506 * keyword/descNL
508 found = 0;
509 q = strchr (p, '\n');
510 if (q == NULL)
511 q = strchr (p, 0);
512 c = *q;
513 *q = 0;
514 if (include_target) {
515 if ((strncmp (p, "include/", 8) == 0)
516 && (strncmp (p + 8, include_target, include_target_len)
517 == 0))
518 found = 1;
519 } else if (!strncmp (p, "regex/", 6)) {
520 p += 6;
521 /* Do not transform shell patterns, you can use shell/ for
522 * that
524 if (regexp_match (p, filename, match_regex))
525 found = 1;
526 } else if (!strncmp (p, "directory/", 10)) {
527 if (S_ISDIR (mystat.st_mode)
528 && regexp_match (p + 10, filename, match_regex))
529 found = 1;
530 } else if (!strncmp (p, "shell/", 6)) {
531 p += 6;
532 if (*p == '.' && file_len >= (q - p)) {
533 if (!strncmp (p, filename + file_len - (q - p), q - p))
534 found = 1;
535 } else {
536 if (q - p == file_len && !strncmp (p, filename, q - p))
537 found = 1;
539 } else if (!strncmp (p, "type/", 5)) {
540 int res;
541 p += 5;
542 res = regex_check_type (filename, p, &have_type);
543 if (res == 1)
544 found = 1;
545 if (res == -1)
546 error_flag = 1; /* leave it if file cannot be opened */
547 } else if (!strncmp (p, "default/", 8)) {
548 found = 1;
550 *q = c;
551 p = q;
552 if (!*p)
553 break;
554 } else { /* List of actions */
555 p = q;
556 q = strchr (p, '\n');
557 if (q == NULL)
558 q = strchr (p, 0);
559 if (found && !error_flag) {
560 r = strchr (p, '=');
561 if (r != NULL) {
562 c = *r;
563 *r = 0;
564 if (strcmp (p, "Include") == 0) {
565 char *t;
567 include_target = p + 8;
568 t = strchr (include_target, '\n');
569 if (t)
570 *t = 0;
571 include_target_len = strlen (include_target);
572 if (t)
573 *t = '\n';
575 *r = c;
576 p = q;
577 found = 0;
579 if (!*p)
580 break;
581 continue;
583 if (!strcmp (action, p)) {
584 *r = c;
585 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
587 /* Empty commands just stop searching
588 * through, they don't do anything
590 * We need to copy the filename because exec_extension
591 * may end up invoking update_panels thus making the
592 * filename parameter invalid (ie, most of the time,
593 * we get filename as a pointer from current_panel->dir).
595 if (p < q) {
596 char *filename_copy = g_strdup (filename);
598 exec_extension (filename_copy, r + 1, move_dir,
599 view_at_line_number);
600 g_free (filename_copy);
602 ret = 1;
604 break;
605 } else
606 *r = c;
609 p = q;
610 if (!*p)
611 break;
614 if (error_flag)
615 return -1;
616 return ret;