utilunix.c(mc_tmpdir): Check return value of getpwuid() for NULL.
[midnight-commander.git] / src / ext.c
blob97a46ce3e9750485735f4d230b37d4841ce0c0a5
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <ctype.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <string.h>
29 #include <errno.h>
31 #include "global.h"
32 #include "tty.h"
33 #include "user.h"
34 #include "main.h"
35 #include "wtools.h"
36 #include "ext.h"
37 #include "view.h"
38 #include "execute.h"
40 #include "cons.saver.h"
41 #include "layout.h"
43 /* If set, we execute the file command to check the file type */
44 int use_file_to_check_type = 1;
46 /* This variable points to a copy of the mc.ext file in memory
47 * With this we avoid loading/parsing the file each time we
48 * need it
50 static char *data = NULL;
52 void
53 flush_extension_file (void)
55 if (data) {
56 g_free (data);
57 data = NULL;
61 typedef char *(*quote_func_t) (const char *name, int i);
63 static void
64 exec_extension (const char *filename, const char *data, int *move_dir,
65 int start_line)
67 char *file_name;
68 int cmd_file_fd;
69 FILE *cmd_file;
70 char *cmd = NULL;
71 int expand_prefix_found = 0;
72 int parameter_found = 0;
73 char prompt[80];
74 int run_view = 0;
75 int def_hex_mode = default_hex_mode, changed_hex_mode = 0;
76 int def_nroff_flag = default_nroff_flag, changed_nroff_flag = 0;
77 int written_nonspace = 0;
78 int is_cd = 0;
79 char buffer[1024];
80 char *p = 0;
81 char *localcopy = NULL;
82 int do_local_copy;
83 time_t localmtime = 0;
84 struct stat mystat;
85 quote_func_t quote_func = name_quote;
87 g_return_if_fail (filename != NULL);
88 g_return_if_fail (data != NULL);
90 /* Avoid making a local copy if we are doing a cd */
91 if (!vfs_file_is_local (filename))
92 do_local_copy = 1;
93 else
94 do_local_copy = 0;
97 * All commands should be run in /bin/sh regardless of user shell.
98 * To do that, create temporary shell script and run it.
99 * Sometimes it's not needed (e.g. for %cd and %view commands),
100 * but it's easier to create it anyway.
102 cmd_file_fd = mc_mkstemps (&file_name, "mcext", SCRIPT_SUFFIX);
104 if (cmd_file_fd == -1) {
105 message (1, MSG_ERROR,
106 _(" Cannot create temporary command file \n %s "),
107 unix_error_string (errno));
108 return;
110 cmd_file = fdopen (cmd_file_fd, "w");
111 fputs ("#! /bin/sh\n", cmd_file);
113 prompt[0] = 0;
114 for (; *data && *data != '\n'; data++) {
115 if (parameter_found) {
116 if (*data == '}') {
117 char *parameter;
118 parameter_found = 0;
119 parameter = input_dialog (_(" Parameter "), prompt, "");
120 if (!parameter) {
121 /* User canceled */
122 fclose (cmd_file);
123 unlink (file_name);
124 if (localcopy) {
125 mc_ungetlocalcopy (filename, localcopy, 0);
126 g_free (localcopy);
128 g_free (file_name);
129 return;
131 fputs (parameter, cmd_file);
132 written_nonspace = 1;
133 g_free (parameter);
134 } else {
135 size_t len = strlen (prompt);
137 if (len < sizeof (prompt) - 1) {
138 prompt[len] = *data;
139 prompt[len + 1] = 0;
142 } else if (expand_prefix_found) {
143 expand_prefix_found = 0;
144 if (*data == '{')
145 parameter_found = 1;
146 else {
147 int i = check_format_view (data);
148 char *v;
150 if (i) {
151 data += i - 1;
152 run_view = 1;
153 } else if ((i = check_format_cd (data)) > 0) {
154 is_cd = 1;
155 quote_func = fake_name_quote;
156 do_local_copy = 0;
157 p = buffer;
158 data += i - 1;
159 } else if ((i = check_format_var (data, &v)) > 0 && v) {
160 fputs (v, cmd_file);
161 g_free (v);
162 data += i;
163 } else {
164 char *text;
166 if (*data == 'f') {
167 if (do_local_copy) {
168 localcopy = mc_getlocalcopy (filename);
169 if (localcopy == NULL) {
170 fclose (cmd_file);
171 unlink (file_name);
172 g_free (file_name);
173 return;
175 mc_stat (localcopy, &mystat);
176 localmtime = mystat.st_mtime;
177 text = (*quote_func) (localcopy, 0);
178 } else {
179 text = (*quote_func) (filename, 0);
181 } else
182 text = expand_format (NULL, *data, !is_cd);
183 if (!is_cd)
184 fputs (text, cmd_file);
185 else {
186 strcpy (p, text);
187 p = strchr (p, 0);
189 g_free (text);
190 written_nonspace = 1;
193 } else {
194 if (*data == '%')
195 expand_prefix_found = 1;
196 else {
197 if (*data != ' ' && *data != '\t')
198 written_nonspace = 1;
199 if (is_cd)
200 *(p++) = *data;
201 else
202 fputc (*data, cmd_file);
205 } /* for */
208 * Make the script remove itself when it finishes.
209 * Don't do it for the viewer - it may need to rerun the script,
210 * so we clean up after calling view().
212 if (!run_view) {
213 fprintf (cmd_file, "\n/bin/rm -f %s\n", file_name);
216 fclose (cmd_file);
218 if ((run_view && !written_nonspace) || is_cd) {
219 unlink (file_name);
220 g_free (file_name);
221 file_name = NULL;
222 } else {
223 /* Set executable flag on the command file ... */
224 chmod (file_name, S_IRWXU);
225 /* ... but don't rely on it - run /bin/sh explicitly */
226 cmd = g_strconcat ("/bin/sh ", file_name, NULL);
229 if (run_view) {
230 altered_hex_mode = 0;
231 altered_nroff_flag = 0;
232 if (def_hex_mode != default_hex_mode)
233 changed_hex_mode = 1;
234 if (def_nroff_flag != default_nroff_flag)
235 changed_nroff_flag = 1;
237 /* If we've written whitespace only, then just load filename
238 * into view
240 if (written_nonspace) {
241 view (cmd, filename, move_dir, start_line);
242 unlink (file_name);
243 } else {
244 view (0, filename, move_dir, start_line);
246 if (changed_hex_mode && !altered_hex_mode)
247 default_hex_mode = def_hex_mode;
248 if (changed_nroff_flag && !altered_nroff_flag)
249 default_nroff_flag = def_nroff_flag;
250 repaint_screen ();
251 } else if (is_cd) {
252 char *q;
253 *p = 0;
254 p = buffer;
255 /* while (*p == ' ' && *p == '\t')
256 * p++;
258 /* Search last non-space character. Start search at the end in order
259 not to short filenames containing spaces. */
260 q = p + strlen (p) - 1;
261 while (q >= p && (*q == ' ' || *q == '\t'))
262 q--;
263 q[1] = 0;
264 do_cd (p, cd_parse_command);
265 } else {
266 shell_execute (cmd, EXECUTE_INTERNAL);
267 if (console_flag) {
268 handle_console (CONSOLE_SAVE);
269 if (output_lines && keybar_visible) {
270 show_console_contents (output_start_y,
271 LINES - keybar_visible -
272 output_lines - 1,
273 LINES - keybar_visible - 1);
279 g_free (file_name);
280 g_free (cmd);
282 if (localcopy) {
283 mc_stat (localcopy, &mystat);
284 mc_ungetlocalcopy (filename, localcopy,
285 localmtime != mystat.st_mtime);
286 g_free (localcopy);
290 #ifdef FILE_L
291 # define FILE_CMD "file -L "
292 #else
293 # define FILE_CMD "file "
294 #endif
297 * Run the "file" command on the local file.
298 * Return 1 if the data is valid, 0 otherwise, -1 for fatal errors.
300 static int
301 get_file_type_local (char *filename, char *buf, int buflen)
303 int read_bytes = 0;
305 char *tmp = name_quote (filename, 0);
306 char *command = g_strconcat (FILE_CMD, tmp, NULL);
307 FILE *f = popen (command, "r");
309 g_free (tmp);
310 g_free (command);
311 if (f != NULL) {
312 read_bytes = (fgets (buf, buflen - 1, f)
313 != NULL);
314 if (read_bytes == 0)
315 buf[0] = 0;
316 pclose (f);
317 } else {
318 return -1;
321 return (read_bytes > 0);
326 * Invoke the "file" command on the file and match its output against PTR.
327 * have_type is a flag that is set if we already have tried to determine
328 * the type of that file.
329 * Return 1 for match, 0 for no match, -1 errors.
331 static int
332 regex_check_type (char *filename, char *ptr, int *have_type)
334 int found = 0;
336 /* Following variables are valid if *have_type is 1 */
337 static char content_string[2048];
338 static int content_shift = 0;
339 static int got_data = 0;
341 if (!use_file_to_check_type) {
342 return 0;
345 if (!*have_type) {
346 char *realname; /* name used with "file" */
347 char *localfile;
348 /* Don't repeate even unsuccessful checks */
349 *have_type = 1;
351 localfile = mc_getlocalcopy (filename);
352 if (!localfile)
353 return -1;
355 realname = g_strdup (localfile);
356 got_data =
357 get_file_type_local (localfile, content_string,
358 sizeof (content_string));
359 mc_ungetlocalcopy (filename, localfile, 0);
360 g_free (localfile);
362 if (got_data > 0) {
363 char *pp;
365 /* Paranoid termination */
366 content_string[sizeof (content_string) - 1] = 0;
368 if ((pp = strchr (content_string, '\n')) != 0)
369 *pp = 0;
371 if (!strncmp (content_string, realname, strlen (realname))) {
372 /* Skip "realname: " */
373 content_shift = strlen (realname);
374 if (content_string[content_shift] == ':') {
375 /* Solaris' file prints tab(s) after ':' */
376 for (content_shift++;
377 content_string[content_shift] == ' '
378 || content_string[content_shift] == '\t';
379 content_shift++);
382 } else {
383 /* No data */
384 content_string[0] = 0;
386 g_free (realname);
389 if (got_data == -1) {
390 return -1;
393 if (content_string[0]
394 && regexp_match (ptr, content_string + content_shift, match_regex)) {
395 found = 1;
398 return found;
402 /* The second argument is action, i.e. Open, View or Edit
404 * This function returns:
406 * -1 for a failure or user interrupt
407 * 0 if no command was run
408 * 1 if some command was run
410 * If action == "View" then a parameter is checked in the form of "View:%d",
411 * if the value for %d exists, then the viewer is started up at that line number.
414 regex_command (char *filename, char *action, int *move_dir)
416 char *p, *q, *r, c;
417 int file_len = strlen (filename);
418 int found = 0;
419 int error_flag = 0;
420 int ret = 0;
421 struct stat mystat;
422 int view_at_line_number;
423 char *include_target;
424 int include_target_len;
425 int have_type = 0; /* Flag used by regex_check_type() */
427 /* Check for the special View:%d parameter */
428 if (strncmp (action, "View:", 5) == 0) {
429 view_at_line_number = atoi (action + 5);
430 action[4] = 0;
431 } else {
432 view_at_line_number = 0;
435 if (data == NULL) {
436 char *extension_file;
437 int mc_user_ext = 1;
438 int home_error = 0;
440 extension_file = concat_dir_and_file (home_dir, MC_USER_EXT);
441 if (!exist_file (extension_file)) {
442 g_free (extension_file);
443 check_stock_mc_ext:
444 extension_file = concat_dir_and_file (mc_home, MC_LIB_EXT);
445 mc_user_ext = 0;
447 data = load_file (extension_file);
448 g_free (extension_file);
449 if (data == NULL)
450 return 0;
452 if (!strstr (data, "default/")) {
453 if (!strstr (data, "regex/") && !strstr (data, "shell/")
454 && !strstr (data, "type/")) {
455 g_free (data);
456 data = NULL;
457 if (mc_user_ext) {
458 home_error = 1;
459 goto check_stock_mc_ext;
460 } else {
461 char *msg;
462 char *msg2;
463 msg =
464 g_strconcat (" ", mc_home, MC_LIB_EXT,
465 _(" file error "), NULL);
466 msg2 =
467 g_strconcat (_("Format of the "), mc_home,
468 _("mc.ext file has changed\n"
469 "with version 3.0. It seems that installation\n"
470 "failed. Please fetch a fresh new copy from the\n"
471 "Midnight Commander package."),
472 NULL);
473 message (1, msg, "%s", msg2);
474 g_free (msg);
475 g_free (msg2);
476 return 0;
480 if (home_error) {
481 char *msg;
482 char *msg2;
483 msg =
484 g_strconcat (" ~/", MC_USER_EXT, _(" file error "), NULL);
485 msg2 =
486 g_strconcat (_("Format of the "), "~/", MC_USER_EXT,
487 _(" file has changed\n"
488 "with version 3.0. You may want either to\n"
489 "copy it from "), mc_home,
490 _("mc.ext or use that\n"
491 "file as an example of how to write it.\n"),
492 mc_home,
493 _("mc.ext will be used for this moment."),
494 NULL);
495 message (1, msg, "%s", msg2);
496 g_free (msg);
497 g_free (msg2);
500 mc_stat (filename, &mystat);
502 include_target = NULL;
503 include_target_len = 0;
504 for (p = data; *p; p++) {
505 for (q = p; *q == ' ' || *q == '\t'; q++);
506 if (*q == '\n' || !*q)
507 p = q; /* empty line */
508 if (*p == '#') /* comment */
509 while (*p && *p != '\n')
510 p++;
511 if (*p == '\n')
512 continue;
513 if (!*p)
514 break;
515 if (p == q) { /* i.e. starts in the first column, should be
516 * keyword/descNL
518 found = 0;
519 q = strchr (p, '\n');
520 if (q == NULL)
521 q = strchr (p, 0);
522 c = *q;
523 *q = 0;
524 if (include_target) {
525 if ((strncmp (p, "include/", 8) == 0)
526 && (strncmp (p + 8, include_target, include_target_len)
527 == 0))
528 found = 1;
529 } else if (!strncmp (p, "regex/", 6)) {
530 p += 6;
531 /* Do not transform shell patterns, you can use shell/ for
532 * that
534 if (regexp_match (p, filename, match_regex))
535 found = 1;
536 } else if (!strncmp (p, "directory/", 10)) {
537 if (S_ISDIR (mystat.st_mode)
538 && regexp_match (p + 10, filename, match_regex))
539 found = 1;
540 } else if (!strncmp (p, "shell/", 6)) {
541 p += 6;
542 if (*p == '.' && file_len >= (q - p)) {
543 if (!strncmp (p, filename + file_len - (q - p), q - p))
544 found = 1;
545 } else {
546 if (q - p == file_len && !strncmp (p, filename, q - p))
547 found = 1;
549 } else if (!strncmp (p, "type/", 5)) {
550 int res;
551 p += 5;
552 res = regex_check_type (filename, p, &have_type);
553 if (res == 1)
554 found = 1;
555 if (res == -1)
556 error_flag = 1; /* leave it if file cannot be opened */
557 } else if (!strncmp (p, "default/", 8)) {
558 found = 1;
560 *q = c;
561 p = q;
562 if (!*p)
563 break;
564 } else { /* List of actions */
565 p = q;
566 q = strchr (p, '\n');
567 if (q == NULL)
568 q = strchr (p, 0);
569 if (found && !error_flag) {
570 r = strchr (p, '=');
571 if (r != NULL) {
572 c = *r;
573 *r = 0;
574 if (strcmp (p, "Include") == 0) {
575 char *t;
577 include_target = p + 8;
578 t = strchr (include_target, '\n');
579 if (t)
580 *t = 0;
581 include_target_len = strlen (include_target);
582 if (t)
583 *t = '\n';
585 *r = c;
586 p = q;
587 found = 0;
589 if (!*p)
590 break;
591 continue;
593 if (!strcmp (action, p)) {
594 *r = c;
595 for (p = r + 1; *p == ' ' || *p == '\t'; p++);
597 /* Empty commands just stop searching
598 * through, they don't do anything
600 * We need to copy the filename because exec_extension
601 * may end up invoking update_panels thus making the
602 * filename parameter invalid (ie, most of the time,
603 * we get filename as a pointer from current_panel->dir).
605 if (p < q) {
606 char *filename_copy = g_strdup (filename);
608 exec_extension (filename_copy, r + 1, move_dir,
609 view_at_line_number);
610 g_free (filename_copy);
612 ret = 1;
614 break;
615 } else
616 *r = c;
619 p = q;
620 if (!*p)
621 break;
624 if (error_flag)
625 return -1;
626 return ret;