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