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. */
47 #include "../vfs/vfs.h"
50 #include "cons.saver.h"
54 #endif /* SCO_FLAVOR */
58 /* If set, we execute the file command to check the file type */
59 int use_file_to_check_type
= 1;
61 /* This variable points to a copy of the mc.ext file in memory
62 * With this we avoid loading/parsing the file each time we
65 static char *data
= NULL
;
69 __declspec(dllimport
) __stdcall
unsigned GetTempPathA(unsigned,char*);
71 static char tmpcmdfilename
[255];
72 char *gettmpcmdname(){
75 memset(TmpPath
,0,255);
76 memset(tmpcmdfilename
,0,255);
77 GetTempPathA(255,TmpPath
);
79 sprintf(tmpcmdfilename
,"%stmp%d.bat",TmpPath
,i
);
80 if((fd
=_open(tmpcmdfilename
,_O_RDONLY
)) != -1)
82 else if (errno
== ENOENT
)
85 return tmpcmdfilename
;
90 flush_extension_file (void)
99 typedef char *(*quote_func_t
)(const char *name
, int i
);
102 quote_block (quote_func_t quote_func
, char **quoting_block
)
104 char **p
= quoting_block
;
109 for (p
= quoting_block
; *p
; p
++){
111 char *temp
= quote_func (*p
, 0);
113 temp_len
= strlen (temp
);
114 current_len
+= temp_len
+ 2;
115 result
= realloc (result
, current_len
);
120 tail
+= temp_len
+ 1;
128 exec_extension (char *filename
, char *data
, char **drops
, int *move_dir
, int start_line
)
133 int expand_prefix_found
= 0;
134 int parameter_found
= 0;
137 int def_hex_mode
= default_hex_mode
, changed_hex_mode
= 0;
138 int def_nroff_flag
= default_nroff_flag
, changed_nroff_flag
= 0;
139 int written_nonspace
= 0;
143 char *localcopy
= NULL
;
145 time_t localmtime
= 0;
147 quote_func_t quote_func
= name_quote
;
149 /* Avoid making a local copy if we are doing a cd */
150 if (!vfs_file_is_local(filename
))
155 /* Note: this has to be done after the getlocalcopy call,
156 * since it uses tmpnam as well
159 file_name
= strdup (gettmpcmdname ());
161 file_name
= strdup (tmpnam (NULL
));
163 if ((cmd_file_fd
= open (file_name
, O_RDWR
| O_CREAT
| O_TRUNC
| O_EXCL
, 0600)) == -1){
164 message (1, MSG_ERROR
, _(" Can't create temporary command file \n %s "),
165 unix_error_string (errno
));
168 cmd_file
= fdopen (cmd_file_fd
, "w");
170 fprintf (cmd_file
, "REM #!%s\n", shell
);
172 fprintf (cmd_file
, "#!%s\n", shell
);
175 for (;*data
&& *data
!= '\n'; data
++){
176 if (parameter_found
){
180 parameter
= input_dialog (_(" Parameter "), prompt
, "");
186 mc_ungetlocalcopy (filename
, localcopy
, 0);
191 fputs (parameter
, cmd_file
);
192 written_nonspace
= 1;
195 int len
= strlen (prompt
);
197 if (len
< sizeof (prompt
) - 1){
198 prompt
[len
] = *data
;
202 } else if (expand_prefix_found
){
203 expand_prefix_found
= 0;
207 int i
= check_format_view (data
);
213 } else if ((i
= check_format_cd (data
)) > 0) {
215 quote_func
= fake_name_quote
;
219 } else if ((i
= check_format_var (data
, &v
)) > 0 && v
){
228 localcopy
= mc_getlocalcopy (filename
);
229 if (localcopy
== NULL
) {
235 mc_stat (localcopy
, &mystat
);
236 localmtime
= mystat
.st_mtime
;
237 text
= (*quote_func
) (localcopy
, 0);
239 text
= (*quote_func
) (filename
, 0);
241 } else if (*data
== 'q') {
242 text
= quote_block (quote_func
, drops
);
244 text
= expand_format (*data
, !is_cd
);
246 fputs (text
, cmd_file
);
252 written_nonspace
= 1;
257 expand_prefix_found
= 1;
259 if (*data
!= ' ' && *data
!= '\t')
260 written_nonspace
= 1;
264 fputc (*data
, cmd_file
);
268 fputc ('\n', cmd_file
);
270 chmod (file_name
, S_IRWXU
);
272 altered_hex_mode
= 0;
273 altered_nroff_flag
= 0;
274 if (def_hex_mode
!= default_hex_mode
)
275 changed_hex_mode
= 1;
276 if (def_nroff_flag
!= default_nroff_flag
)
277 changed_nroff_flag
= 1;
279 /* If we've written whitespace only, then just load filename
282 if (written_nonspace
)
283 view (file_name
, filename
, move_dir
, start_line
);
285 view (0, filename
, move_dir
, start_line
);
286 if (changed_hex_mode
&& !altered_hex_mode
)
287 default_hex_mode
= def_hex_mode
;
288 if (changed_nroff_flag
&& !altered_nroff_flag
)
289 default_nroff_flag
= def_nroff_flag
;
295 while (*p
== ' ' && *p
== '\t')
298 /* Search last non-space character. Start search at the end in order
299 not to short filenames containing spaces. */
300 q
= p
+ strlen (p
) - 1;
301 while (q
>= p
&& (*q
== ' ' || *q
== '\t'))
304 do_cd (p
, cd_parse_command
);
306 shell_execute (file_name
, EXECUTE_INTERNAL
| EXECUTE_TEMPFILE
);
309 handle_console (CONSOLE_SAVE
);
310 if (output_lines
&& keybar_visible
)
312 show_console_contents (output_start_y
,
313 LINES
-keybar_visible
-output_lines
-1,
314 LINES
-keybar_visible
-1);
320 if (vfs_current_is_local ())
321 shell_execute (file_name
, EXECUTE_INTERNAL
);
323 message (1, _(" Warning "), _(" Can't execute commands on a Virtual File System directory "));
326 #ifndef PORT_DOES_BACKGROUND_EXEC
330 mc_stat (localcopy
, &mystat
);
331 mc_ungetlocalcopy (filename
, localcopy
, localmtime
!= mystat
.st_mtime
);
337 # define FILE_CMD "file -L "
339 # define FILE_CMD "file "
342 /* The second argument is action, i.e. Open, View, Edit, Drop, or NULL if
343 * we want regex_command to return a list of all user defined actions.
344 * Third argument is space separated list of dropped files (for actions
345 * other then Drop it should be NULL);
347 * This function returns:
349 * If action != NULL, then it returns "Success" (not allocated) if it ran
350 * some command or NULL if not.
352 * If action == NULL, it returns NULL if there are no user defined commands
353 * or an allocated space separated list of user defined Actions.
355 * If action == "Icon", we are doing again something special. We return
356 * icon name and we set the variable regex_command_title to Title for
359 * If action == "View" then a parameter is checked in the form of "View:%d",
360 * if the value for %d exists, then the viewer is started up at that line number.
362 char *regex_command_title
= NULL
;
363 char *regex_command (char *filename
, char *action
, char **drops
, int *move_dir
)
365 char *extension_file
;
368 int file_len
= strlen (filename
);
370 char content_string
[2048];
371 int content_shift
= 0;
372 char *to_return
= NULL
;
376 int view_at_line_number
;
377 char *include_target
;
378 int include_target_len
;
381 int file_supports_stdin
= 1;
383 int file_supports_stdin
= 0;
386 /* Check for the special View:%d parameter */
387 if (action
&& strncmp (action
, "View:", 5) == 0){
388 view_at_line_number
= atoi (action
+ 5);
391 view_at_line_number
= 0;
393 /* Have we asked file for the file contents? */
399 buffer
= concat_dir_and_file (home_dir
, MC_USER_EXT
);
400 if (exist_file (buffer
))
401 extension_file
= buffer
;
404 extension_file
= concat_dir_and_file (mc_home
, MC_LIB_EXT
);
405 if ((data
= load_file (extension_file
)) == NULL
) {
409 if (!strstr (data
, "default/")) {
410 if (!strstr (data
, "regex/") && !strstr (data
, "shell/") &&
411 !strstr (data
, "type/")) {
414 if (extension_file
== buffer
) {
416 goto check_stock_mc_ext
;
420 msg
= copy_strings(" ", mc_home
, MC_LIB_EXT
, _(" file error"), NULL
);
421 msg2
= copy_strings(_("Format of the "),
423 ("mc.ext file has changed\n\
424 with version 3.0. It seems that installation\n\
425 failed. Please fetch a fresh new copy from the\n\
426 Midnight Commander package or in case you don't\n\
427 have any, get it from ftp://ftp.nuclecu.unam.mx."), 0);
428 message (1, msg
, msg2
);
439 msg
= copy_strings(" ~/", MC_USER_EXT
, _(" file error "), NULL
);
440 msg2
= copy_strings(_("Format of the ~/"), MC_USER_EXT
, _(" file has changed\n\
441 with version 3.0. You may want either to\n\
442 copy it from "), mc_home
, _("mc.ext or use that\n\
443 file as an example of how to write it.\n\
444 "), mc_home
, _("mc.ext will be used for this moment."), 0);
445 message (1, msg
, msg2
);
451 mc_stat (filename
, &mystat
);
453 if (regex_command_title
){
454 free (regex_command_title
);
455 regex_command_title
= NULL
;
457 old_patterns
= easy_patterns
;
458 easy_patterns
= 0; /* Real regular expressions are needed :) */
459 include_target
= NULL
;
460 for (p
= data
; *p
; p
++) {
461 for (q
= p
; *q
== ' ' || *q
== '\t'; q
++)
463 if (*q
== '\n' || !*q
)
464 p
= q
; /* empty line */
465 if (*p
== '#') /* comment */
466 while (*p
&& *p
!= '\n')
472 if (p
== q
) { /* i.e. starts in the first column, should be
475 if (found
&& action
== NULL
) /* We have already accumulated all
480 q
= strchr (p
, '\n');
486 if ((strncmp (p
, "include/", 8) == 0) &&
487 (strncmp (p
+8, include_target
, include_target_len
) == 0))
489 } else if (!strncmp (p
, "regex/", 6)) {
491 /* Do not transform shell patterns, you can use shell/ for
494 if (regexp_match (p
, filename
, match_normal
))
496 } else if (!strncmp (p
, "directory/", 10)) {
497 if (S_ISDIR (mystat
.st_mode
) && regexp_match (p
+10, filename
, match_normal
))
499 } else if (!strncmp (p
, "shell/", 6)) {
502 if (!strncmp (p
, filename
+ file_len
- (q
- p
),
506 if (q
- p
== file_len
&& !strncmp (p
, filename
, q
- p
))
509 } else if (!strncmp (p
, "type/", 5)) {
510 int islocal
= vfs_file_is_local (filename
);
513 if (islocal
|| file_supports_stdin
) {
515 int hasread
= use_file_to_check_type
;
517 if (asked_file
|| !use_file_to_check_type
)
518 goto match_file_output
;
522 char *tmp
= name_quote (filename
, 0);
524 copy_strings (FILE_CMD
, tmp
, NULL
);
525 FILE *f
= popen (command
, "r");
530 hasread
= (fgets (content_string
, 2047, f
)
533 content_string
[0] = 0;
537 ** SCO 3.2 does has a buggy pclose(), so
538 ** <command> become zombie (alex)
540 waitpid(-1,NULL
,WNOHANG
);
541 #endif /* SCO_FLAVOR */
545 message (1, " Win32 ", " Unimplemented file prediction ");
547 int pipehandle
, remotehandle
;
550 remotehandle
= mc_open (filename
, O_RDONLY
);
551 if (remotehandle
!= -1) {
552 /* 8192 is HOWMANY hardcoded value in the file-3.14
553 * sources. Tell me if any other file uses larger
554 * chunk from beginning
556 pipehandle
= mc_doublepopen
557 (remotehandle
, 8192, &p
,"file", "file", "-", NULL
);
558 if (pipehandle
!= -1) {
560 while ((i
= read (pipehandle
, content_string
561 + hasread
, 2047 - hasread
)) > 0)
563 mc_doublepclose (pipehandle
, p
);
564 content_string
[hasread
] = 0;
566 mc_close (remotehandle
);
573 if ((pp
= strchr (content_string
, '\n')) != 0)
575 if (islocal
&& !strncmp (content_string
,
576 filename
, file_len
)) {
577 content_shift
= file_len
;
578 if (content_string
[content_shift
] == ':')
579 for (content_shift
++;
580 content_string
[content_shift
] == ' ';
583 && !strncmp (content_string
,
584 "standard input:", 15)) {
585 for (content_shift
= 15;
586 content_string
[content_shift
] == ' ';
589 if (content_string
&&
590 regexp_match (p
, content_string
+
591 content_shift
, match_normal
)){
596 } else if (!strncmp (p
, "default/", 8)) {
604 } else { /* List of actions */
606 q
= strchr (p
, '\n');
614 if (strcmp (p
, "Include") == 0){
617 include_target
= p
+ 8;
618 t
= strchr (include_target
, '\n');
620 include_target_len
= strlen (include_target
);
631 if (action
== NULL
) {
632 if (strcmp (p
, "Open") &&
633 strcmp (p
, "View") &&
634 strcmp (p
, "Edit") &&
635 strcmp (p
, "Drop") &&
636 strcmp (p
, "Icon") &&
637 strcmp (p
, "Include") &&
638 strcmp (p
, "Title")) {
639 /* I.e. this is a name of a user defined action */
642 if (to_return
== NULL
) {
643 to_return
= xmalloc (512, "Action list");
646 *(q
++) = '='; /* Mark separator */
651 } else if (!strcmp (action
, "Icon")) {
652 if (!strcmp (p
, "Icon") && to_return
== NULL
) {
656 to_return
= strdup (r
+ 1);
657 } else if (!strcmp (p
, "Title") && regex_command_title
== NULL
) {
661 regex_command_title
= strdup (r
+ 1);
667 if (to_return
!= NULL
&& regex_command_title
!= NULL
)
669 } else if (!strcmp (action
, p
)) {
671 for (p
= r
+ 1; *p
== ' ' || *p
== '\t'; p
++)
674 /* Empty commands just stop searching
675 * through, they don't do anything
677 * We need to copy the filename because exec_extension
678 * may end up invoking update_panels thus making the
679 * filename parameter invalid (ie, most of the time,
680 * we get filename as a pointer from cpanel->dir).
683 char *filename_copy
= strdup (filename
);
684 exec_extension (filename_copy
, r
+ 1, drops
, move_dir
, view_at_line_number
);
685 free (filename_copy
);
687 to_return
= "Success";
699 easy_patterns
= old_patterns
;