1 /* Extension dependent execution.
2 Copyright (C) 1994, 1995 The Free Software Foundation
4 Written by: 1995 Jakub Jelinek
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
43 #include "../vfs/vfs.h"
46 #include "cons.saver.h"
50 #endif /* SCO_FLAVOR */
52 /* If set, we execute the file command to check the file type */
53 int use_file_to_check_type
= 1;
55 /* This variable points to a copy of the mc.ext file in memory
56 * With this we avoid loading/parsing the file each time we
59 static char *data
= NULL
;
62 flush_extension_file (void)
71 typedef char *(*quote_func_t
)(const char *name
, int i
);
74 quote_block (quote_func_t quote_func
, char **quoting_block
)
76 char **p
= quoting_block
;
81 for (p
= quoting_block
; *p
; p
++){
83 char *temp
= quote_func (*p
, 0);
85 temp_len
= strlen (temp
);
86 current_len
+= temp_len
+ 2;
87 result
= g_realloc (result
, current_len
);
100 exec_extension (char *filename
, char *data
, char **drops
, int *move_dir
, int start_line
)
105 int expand_prefix_found
= 0;
106 int parameter_found
= 0;
109 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
110 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
111 int written_nonspace
= 0;
115 char *localcopy
= NULL
;
117 time_t localmtime
= 0;
119 quote_func_t quote_func
= name_quote
;
121 g_return_if_fail (filename
!= NULL
);
122 g_return_if_fail (data
!= NULL
);
124 /* Avoid making a local copy if we are doing a cd */
125 if (!vfs_file_is_local(filename
))
130 /* Note: this has to be done after the getlocalcopy call,
131 * since it uses tmpnam as well
133 file_name
= g_strdup (tmpnam (NULL
));
135 /* #warning FIXME: this is ugly */
136 if ((cmd_file_fd
= open (file_name
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600)) == -1){
137 message (1, MSG_ERROR
, _(" Can't create temporary command file \n %s "),
138 unix_error_string (errno
));
141 cmd_file
= fdopen (cmd_file_fd
, "w");
142 /* FIXME: This is a hack, that makes us sure, we are using the right syntax */
143 fprintf (cmd_file
, "#!/bin/sh\n");
146 for (;*data
&& *data
!= '\n'; data
++){
147 if (parameter_found
){
151 parameter
= input_dialog (_(" Parameter "), prompt
, "");
157 mc_ungetlocalcopy (filename
, localcopy
, 0);
162 fputs (parameter
, cmd_file
);
163 written_nonspace
= 1;
166 int len
= strlen (prompt
);
168 if (len
< sizeof (prompt
) - 1){
169 prompt
[len
] = *data
;
173 } else if (expand_prefix_found
){
174 expand_prefix_found
= 0;
178 int i
= check_format_view (data
);
184 } else if ((i
= check_format_cd (data
)) > 0) {
186 quote_func
= fake_name_quote
;
190 } else if ((i
= check_format_var (data
, &v
)) > 0 && v
){
199 localcopy
= mc_getlocalcopy (filename
);
200 if (localcopy
== NULL
) {
206 mc_stat (localcopy
, &mystat
);
207 localmtime
= mystat
.st_mtime
;
208 text
= (*quote_func
) (localcopy
, 0);
210 text
= (*quote_func
) (filename
, 0);
212 } else if (*data
== 'q') {
213 text
= quote_block (quote_func
, drops
);
215 text
= expand_format (*data
, !is_cd
);
217 fputs (text
, cmd_file
);
223 written_nonspace
= 1;
228 expand_prefix_found
= 1;
230 if (*data
!= ' ' && *data
!= '\t')
231 written_nonspace
= 1;
235 fputc (*data
, cmd_file
);
239 fputc ('\n', cmd_file
);
241 chmod (file_name
, S_IRWXU
);
243 altered_hex_mode
= 0;
244 altered_nroff_flag
= 0;
245 if (def_hex_mode
!= default_hex_mode
)
246 changed_hex_mode
= 1;
247 if (def_nroff_flag
!= default_nroff_flag
)
248 changed_nroff_flag
= 1;
250 /* If we've written whitespace only, then just load filename
253 if (written_nonspace
)
254 view (file_name
, filename
, move_dir
, start_line
);
256 view (0, filename
, move_dir
, start_line
);
257 if (changed_hex_mode
&& !altered_hex_mode
)
258 default_hex_mode
= def_hex_mode
;
259 if (changed_nroff_flag
&& !altered_nroff_flag
)
260 default_nroff_flag
= def_nroff_flag
;
266 while (*p
== ' ' && *p
== '\t')
269 /* Search last non-space character. Start search at the end in order
270 not to short filenames containing spaces. */
271 q
= p
+ strlen (p
) - 1;
272 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
275 do_cd (p
, cd_parse_command
);
277 shell_execute (file_name
, EXECUTE_INTERNAL
| EXECUTE_TEMPFILE
);
280 handle_console (CONSOLE_SAVE
);
281 if (output_lines
&& keybar_visible
)
283 show_console_contents (output_start_y
,
284 LINES
-keybar_visible
-output_lines
-1,
285 LINES
-keybar_visible
-1);
291 if (vfs_current_is_local ())
292 shell_execute (file_name
, EXECUTE_INTERNAL
);
294 message (1, _(" Warning "), _(" Can't execute commands on a Virtual File System directory "));
297 #ifndef PORT_DOES_BACKGROUND_EXEC
301 mc_stat (localcopy
, &mystat
);
302 mc_ungetlocalcopy (filename
, localcopy
, localmtime
!= mystat
.st_mtime
);
308 # define FILE_CMD "file -L "
310 # define FILE_CMD "file "
313 /* The second argument is action, i.e. Open, View, Edit, Drop, or NULL if
314 * we want regex_command to return a list of all user defined actions.
315 * Third argument is space separated list of dropped files (for actions
316 * other then Drop it should be NULL);
318 * This function returns:
320 * If action != NULL, then it returns "Success" (not allocated) if it ran
321 * some command or NULL if not.
323 * If action == NULL, it returns NULL if there are no user defined commands
324 * or an allocated space separated list of user defined Actions.
326 * If action == "Icon", we are doing again something special. We return
327 * icon name and we set the variable regex_command_title to Title for
330 * If action == "View" then a parameter is checked in the form of "View:%d",
331 * if the value for %d exists, then the viewer is started up at that line number.
333 char *regex_command_title
= NULL
;
334 char *regex_command (char *filename
, char *action
, char **drops
, int *move_dir
)
336 char *extension_file
;
339 int file_len
= strlen (filename
);
341 char content_string
[2048];
342 int content_shift
= 0;
343 char *to_return
= NULL
;
347 int view_at_line_number
;
348 char *include_target
;
349 int include_target_len
;
352 int file_supports_stdin
= 1;
354 int file_supports_stdin
= 0;
357 /* Check for the special View:%d parameter */
358 if (action
&& strncmp (action
, "View:", 5) == 0){
359 view_at_line_number
= atoi (action
+ 5);
362 view_at_line_number
= 0;
364 /* Have we asked file for the file contents? */
370 buffer
= concat_dir_and_file (home_dir
, MC_USER_EXT
);
371 if (exist_file (buffer
))
372 extension_file
= buffer
;
375 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
376 if ((data
= load_file (extension_file
)) == NULL
) {
380 if (!strstr (data
, "default/")) {
381 if (!strstr (data
, "regex/") && !strstr (data
, "shell/") &&
382 !strstr (data
, "type/")) {
385 if (extension_file
== buffer
) {
387 goto check_stock_mc_ext
;
391 msg
= g_strconcat (" ", mc_home
, MC_LIB_EXT
, _(" file error"), NULL
);
392 msg2
= g_strconcat (_("Format of the "),
394 ("mc.ext file has changed\n\
395 with version 3.0. It seems that installation\n\
396 failed. Please fetch a fresh new copy from the\n\
397 Midnight Commander package or in case you don't\n\
398 have any, get it from ftp://ftp.nuclecu.unam.mx."), NULL
);
399 message (1, msg
, msg2
);
410 msg
= g_strconcat (" ~/", MC_USER_EXT
, _(" file error "), NULL
);
411 msg2
= g_strconcat (_("Format of the ~/"), MC_USER_EXT
, _(" file has changed\n\
412 with version 3.0. You may want either to\n\
413 copy it from "), mc_home
, _("mc.ext or use that\n\
414 file as an example of how to write it.\n\
415 "), mc_home
, _("mc.ext will be used for this moment."), NULL
);
416 message (1, msg
, msg2
);
422 mc_stat (filename
, &mystat
);
424 if (regex_command_title
){
425 g_free (regex_command_title
);
426 regex_command_title
= NULL
;
428 old_patterns
= easy_patterns
;
429 easy_patterns
= 0; /* Real regular expressions are needed :) */
430 include_target
= NULL
;
431 for (p
= data
; *p
; p
++) {
432 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++)
434 if (*q
== '\n' || !*q
)
435 p
= q
; /* empty line */
436 if (*p
== '#') /* comment */
437 while (*p
&& *p
!= '\n')
443 if (p
== q
) { /* i.e. starts in the first column, should be
446 if (found
&& action
== NULL
) /* We have already accumulated all
451 q
= strchr (p
, '\n');
457 if ((strncmp (p
, "include/", 8) == 0) &&
458 (strncmp (p
+8, include_target
, include_target_len
) == 0))
460 } else if (!strncmp (p
, "regex/", 6)) {
462 /* Do not transform shell patterns, you can use shell/ for
465 if (regexp_match (p
, filename
, match_normal
))
467 } else if (!strncmp (p
, "directory/", 10)) {
468 if (S_ISDIR (mystat
.st_mode
) && regexp_match (p
+10, filename
, match_normal
))
470 } else if (!strncmp (p
, "shell/", 6)) {
473 if (!strncmp (p
, filename
+ file_len
- (q
- p
),
477 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
480 } else if (!strncmp (p
, "type/", 5)) {
481 int islocal
= vfs_file_is_local (filename
);
484 if (islocal
|| file_supports_stdin
) {
486 int hasread
= use_file_to_check_type
;
488 if (asked_file
|| !use_file_to_check_type
)
489 goto match_file_output
;
493 char *tmp
= name_quote (filename
, 0);
495 g_strconcat (FILE_CMD
, tmp
, NULL
);
496 FILE *f
= popen (command
, "r");
501 hasread
= (fgets (content_string
, 2047, f
)
504 content_string
[0] = 0;
508 ** SCO 3.2 does has a buggy pclose(), so
509 ** <command> become zombie (alex)
511 waitpid(-1,NULL
,WNOHANG
);
512 #endif /* SCO_FLAVOR */
516 message (1, " Win32 ", " Unimplemented file prediction ");
518 int pipehandle
, remotehandle
;
521 remotehandle
= mc_open (filename
, O_RDONLY
);
522 if (remotehandle
!= -1) {
523 /* 8192 is HOWMANY hardcoded value in the file-3.14
524 * sources. Tell me if any other file uses larger
525 * chunk from beginning
527 pipehandle
= mc_doublepopen
528 (remotehandle
, 8192, &p
,"file", "file", "-", NULL
);
529 if (pipehandle
!= -1) {
531 while ((i
= read (pipehandle
, content_string
532 + hasread
, 2047 - hasread
)) > 0)
534 mc_doublepclose (pipehandle
, p
);
535 content_string
[hasread
] = 0;
537 mc_close (remotehandle
);
544 if ((pp
= strchr (content_string
, '\n')) != 0)
546 if (islocal
&& !strncmp (content_string
,
547 filename
, file_len
)) {
548 content_shift
= file_len
;
549 if (content_string
[content_shift
] == ':')
550 for (content_shift
++;
551 content_string
[content_shift
] == ' ';
554 && !strncmp (content_string
,
555 "standard input:", 15)) {
556 for (content_shift
= 15;
557 content_string
[content_shift
] == ' ';
560 if (content_string
&&
561 regexp_match (p
, content_string
+
562 content_shift
, match_normal
)){
567 } else if (!strncmp (p
, "default/", 8)) {
575 } else { /* List of actions */
577 q
= strchr (p
, '\n');
585 if (strcmp (p
, "Include") == 0){
588 include_target
= p
+ 8;
589 t
= strchr (include_target
, '\n');
591 include_target_len
= strlen (include_target
);
602 if (action
== NULL
) {
603 if (strcmp (p
, "Open") &&
604 strcmp (p
, "View") &&
605 strcmp (p
, "Edit") &&
606 strcmp (p
, "Drop") &&
607 strcmp (p
, "Icon") &&
608 strcmp (p
, "Include") &&
609 strcmp (p
, "Title")) {
610 /* I.e. this is a name of a user defined action */
613 if (to_return
== NULL
) {
614 to_return
= g_malloc (512);
617 *(q
++) = '='; /* Mark separator */
622 } else if (!strcmp (action
, "Icon")) {
623 if (!strcmp (p
, "Icon") && to_return
== NULL
) {
627 to_return
= g_strdup (r
+ 1);
628 } else if (!strcmp (p
, "Title") && regex_command_title
== NULL
) {
632 regex_command_title
= g_strdup (r
+ 1);
638 if (to_return
!= NULL
&& regex_command_title
!= NULL
)
640 } else if (!strcmp (action
, p
)) {
642 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++)
645 /* Empty commands just stop searching
646 * through, they don't do anything
648 * We need to copy the filename because exec_extension
649 * may end up invoking update_panels thus making the
650 * filename parameter invalid (ie, most of the time,
651 * we get filename as a pointer from cpanel->dir).
654 char *filename_copy
= g_strdup (filename
);
656 exec_extension (filename_copy
, r
+ 1, drops
, move_dir
, view_at_line_number
);
657 g_free (filename_copy
);
659 to_return
= "Success";
671 easy_patterns
= old_patterns
;