1 /* User Menu implementation
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include <mhl/memory.h>
27 #include <mhl/string.h>
41 #include "../edit/edit.h" /* BLOCK_FILE */
42 #include "../edit/edit-widget.h" /* WEdit */
44 /* For the simple listbox manager */
49 #include "view.h" /* for default_* externs */
51 #define MAX_ENTRIES 16
52 #define MAX_ENTRY_LEN 60
54 static int debug_flag
= 0;
55 static int debug_error
= 0;
56 static char *menu
= NULL
;
60 %f The current file (if non-local vfs, file will be copied locally and
61 %f will be full path to it).
63 %d The current working directory
64 %s "Selected files"; the tagged files if any, otherwise the current file
66 %u Tagged files (and they are untagged on return from expand_format)
67 %view Runs the commands and pipes standard output to the view command.
68 If %view is immediately followed by '{', recognize keywords
69 ascii, hex, nroff and unform
71 If the format letter is in uppercase, it refers to the other panel.
73 With a number followed the % character you can turn quoting on (default)
75 %f quote expanded macro
77 %0f don't quote expanded macro
79 expand_format returns a memory block that must be free()d.
82 /* Returns how many characters we should advance if %view was found */
83 int check_format_view (const char *p
)
86 if (!strncmp (p
, "view", 4)) {
89 for (q
++;*q
&& *q
!= '}';q
++) {
90 if (!strncmp (q
, "ascii", 5)) {
93 } else if (!strncmp (q
, "hex", 3)) {
96 } else if (!strncmp (q
, "nroff", 5)) {
97 default_nroff_flag
= 1;
99 } else if (!strncmp (q
, "unform", 6)) {
100 default_nroff_flag
= 0;
112 int check_format_cd (const char *p
)
114 return (strncmp (p
, "cd", 2)) ? 0 : 3;
117 /* Check if p has a "^var\{var-name\}" */
118 /* Returns the number of skipped characters (zero on not found) */
119 /* V will be set to the expanded variable name */
120 int check_format_var (const char *p
, char **v
)
125 const char *dots
= 0;
128 if (!strncmp (p
, "var{", 4)){
129 for (q
+= 4; *q
&& *q
!= '}'; q
++){
136 if (!dots
|| dots
== q
+5){
138 _(" Format error on file Extensions File "),
139 !dots
? _(" The %%var macro has no default ")
140 : _(" The %%var macro has no variable "));
144 /* Copy the variable name */
145 var_name
= g_strndup (p
+ 4, dots
-2 - (p
+3));
147 value
= getenv (var_name
);
150 *v
= g_strdup (value
);
153 var_name
= g_strndup (dots
, q
- dots
);
160 /* strip file's extension */
164 register char *s
= ss
;
168 if(*s
== PATH_SEP
&& e
) e
=NULL
; /* '.' in *directory* name */
176 expand_format (struct WEdit
*edit_widget
, char c
, int quote
)
178 WPanel
*panel
= NULL
;
179 char *(*quote_func
) (const char *, int);
185 return g_strdup ("%");
187 if (edit_one_file
!= NULL
)
188 fname
= edit_widget
->filename
;
190 if (islower ((unsigned char) c
))
191 panel
= current_panel
;
193 if (get_other_type () != view_listing
)
194 return g_strdup ("");
197 fname
= panel
->dir
.list
[panel
->selected
].fname
;
201 quote_func
= name_quote
;
203 quote_func
= fake_name_quote
;
205 c_lc
= tolower ((unsigned char) c
);
210 return (*quote_func
) (fname
, 0);
212 return (*quote_func
) (extension (fname
), 0);
218 cwd
= g_malloc(MC_MAXPATHLEN
+ 1);
221 g_strlcpy(cwd
, panel
->cwd
, MC_MAXPATHLEN
+ 1);
223 mc_get_current_wd(cwd
, MC_MAXPATHLEN
+ 1);
225 qstr
= (*quote_func
) (cwd
, 0);
231 case 'i': /* indent equal number cursor position in line */
233 return g_strnfill (edit_widget
->curs_col
, ' ');
235 case 'y': /* syntax type */
236 if (edit_widget
&& edit_widget
->syntax_type
)
237 return g_strdup (edit_widget
->syntax_type
);
239 case 'k': /* block file name */
240 case 'b': /* block file name / strip extension */ {
242 char *file
= g_strconcat (home_dir
, PATH_SEP_STR BLOCK_FILE
, (char *) NULL
);
243 fname
= (*quote_func
) (file
, 0);
246 } else if (c_lc
== 'b') {
247 return strip_ext ((*quote_func
) (fname
, 0));
251 case 'n': /* strip extension in editor */
253 return strip_ext ((*quote_func
) (fname
, 0));
255 case 'm': /* menu file name */
257 return (*quote_func
) (menu
, 0);
260 if (!panel
|| !panel
->marked
)
261 return (*quote_func
) (fname
, 0);
272 return g_strdup ("");
274 for (i
= 0; i
< panel
->count
; i
++)
275 if (panel
->dir
.list
[i
].f
.marked
)
276 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
278 block
= g_malloc (length
* 2 + 1);
280 for (i
= 0; i
< panel
->count
; i
++)
281 if (panel
->dir
.list
[i
].f
.marked
) {
283 (*quote_func
) (panel
->dir
.list
[i
].fname
, 0));
287 do_file_mark (panel
, i
, 0);
290 } /* sub case block */
292 result
= g_strdup ("% ");
298 * Check for the "shell_patterns" directive. If it's found and valid,
299 * interpret it and move the pointer past the directive. Return the
303 check_patterns (char *p
)
305 static const char def_name
[] = "shell_patterns=";
308 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
311 p
+= sizeof (def_name
) - 1;
321 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
326 /* Copies a whitespace separated argument from p to arg. Returns the
327 point after argument. */
328 static char *extract_arg (char *p
, char *arg
, int size
)
330 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
332 /* support quote space .mnu */
333 while (size
> 1 && *p
&& (*p
!= ' ' || *(p
-1) == '\\') && *p
!= '\t' && *p
!= '\n') {
338 if (!*p
|| *p
== '\n')
343 /* Tests whether the selected file in the panel is of any of the types
344 specified in argument. */
345 static int test_type (WPanel
*panel
, char *arg
)
347 int result
= 0; /* False by default */
348 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
350 for (;*arg
!= 0; arg
++){
352 case 'n': /* Not a directory */
353 result
|= !S_ISDIR (st_mode
);
355 case 'r': /* Regular file */
356 result
|= S_ISREG (st_mode
);
358 case 'd': /* Directory */
359 result
|= S_ISDIR (st_mode
);
362 result
|= S_ISLNK (st_mode
);
364 case 'c': /* Character special */
365 result
|= S_ISCHR (st_mode
);
367 case 'b': /* Block special */
368 result
|= S_ISBLK (st_mode
);
370 case 'f': /* Fifo (named pipe) */
371 result
|= S_ISFIFO (st_mode
);
373 case 's': /* Socket */
374 result
|= S_ISSOCK (st_mode
);
376 case 'x': /* Executable */
377 result
|= (st_mode
& 0111) ? 1 : 0;
380 result
|= panel
->marked
? 1 : 0;
390 /* Calculates the truth value of the next condition starting from
391 p. Returns the point after condition. */
392 static char *test_condition (WEdit
*edit_widget
, char *p
, int *condition
)
397 /* Handle one condition */
398 for (;*p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++){
399 /* support quote space .mnu */
400 if ((*p
== ' ' && *(p
-1) != '\\') || *p
== '\t')
403 panel
= current_panel
;
405 if (get_other_type () == view_listing
)
414 p
= test_condition (edit_widget
, p
, condition
);
415 *condition
= ! *condition
;
418 case 'f': /* file name pattern */
419 p
= extract_arg (p
, arg
, sizeof (arg
));
420 *condition
= panel
&& regexp_match (arg
, panel
->dir
.list
[panel
->selected
].fname
, match_file
);
422 case 'y': /* syntax pattern */
423 if (edit_widget
&& edit_widget
->syntax_type
) {
424 p
= extract_arg (p
, arg
, sizeof (arg
));
425 *condition
= panel
&&
426 regexp_match (arg
, edit_widget
->syntax_type
, match_normal
);
430 p
= extract_arg (p
, arg
, sizeof (arg
));
431 *condition
= panel
&& regexp_match (arg
, panel
->cwd
, match_file
);
434 p
= extract_arg (p
, arg
, sizeof (arg
));
435 *condition
= panel
&& test_type (panel
, arg
);
437 case 'x': /* executable */
441 p
= extract_arg (p
, arg
, sizeof (arg
));
442 if (stat (arg
, &status
) == 0)
443 *condition
= is_exe (status
.st_mode
);
457 /* General purpose condition debug output handler */
459 debug_out (char *start
, char *end
, int cond
)
464 if (start
== NULL
&& end
== NULL
){
466 if (debug_flag
&& msg
) {
470 message (D_NORMAL
, _(" Debug "), "%s", msg
);
480 /* Save debug info for later output */
483 /* Save the result of the condition */
485 type
= _(" ERROR: ");
491 type
= _(" False: ");
492 /* This is for debugging, don't need to be super efficient. */
494 p
= g_strdup_printf ("%s%s%c \n", msg
? msg
: "", type
, *start
);
496 p
= g_strdup_printf ("%s%s%.*s \n", msg
? msg
: "", type
,
497 (int) (end
- start
), start
);
503 /* Calculates the truth value of one lineful of conditions. Returns
504 the point just before the end of line. */
505 static char *test_line (WEdit
*edit_widget
, char *p
, int *result
)
509 char *debug_start
, *debug_end
;
511 /* Repeat till end of line */
512 while (*p
&& *p
!= '\n') {
513 /* support quote space .mnu */
514 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
516 if (!*p
|| *p
== '\n')
523 /* support quote space .mnu */
524 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
526 if (!*p
|| *p
== '\n')
528 condition
= 1; /* True by default */
531 p
= test_condition (edit_widget
, p
, &condition
);
533 /* Add one debug statement */
534 debug_out (debug_start
, debug_end
, condition
);
542 case '&': /* Logical and */
543 *result
&= condition
;
545 case '|': /* Logical or */
546 *result
|= condition
;
552 /* Add one debug statement */
553 debug_out (&operator, NULL
, *result
);
555 } /* while (*p != '\n') */
556 /* Report debug message */
557 debug_out (NULL
, NULL
, 1);
559 if (!*p
|| *p
== '\n')
564 /* FIXME: recode this routine on version 3.0, it could be cleaner */
566 execute_menu_command (WEdit
*edit_widget
, const char *commands
)
570 int expand_prefix_found
= 0;
578 /* Skip menu entry title line */
579 commands
= strchr (commands
, '\n');
584 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
586 if (cmd_file_fd
== -1){
587 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary command file \n %s "),
588 unix_error_string (errno
));
591 cmd_file
= fdopen (cmd_file_fd
, "w");
592 fputs ("#! /bin/sh\n", cmd_file
);
595 for (col
= 0; *commands
; commands
++){
597 if (*commands
!= ' ' && *commands
!= '\t')
599 while (*commands
== ' ' || *commands
== '\t')
605 if (*commands
== '\n')
608 if (*commands
== '}'){
611 parameter
= input_dialog (_(" Parameter "), prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "");
612 if (!parameter
|| !*parameter
){
620 fputs (tmp
= name_quote (parameter
, 0), cmd_file
);
623 fputs (parameter
, cmd_file
);
627 if (parameter
< &prompt
[sizeof (prompt
) - 1]) {
628 *parameter
++ = *commands
;
631 } else if (expand_prefix_found
){
632 expand_prefix_found
= 0;
633 if (isdigit ((unsigned char) *commands
)) {
634 do_quote
= atoi (commands
);
635 while (isdigit ((unsigned char) *commands
))
638 if (*commands
== '{')
641 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
642 fputs (text
, cmd_file
);
646 if (*commands
== '%') {
647 int i
= check_format_view (commands
+ 1);
652 do_quote
= 1; /* Default: Quote expanded macro */
653 expand_prefix_found
= 1;
656 fputc (*commands
, cmd_file
);
660 chmod (file_name
, S_IRWXU
);
663 mc_internal_viewer (file_name
, NULL
, &run_view
, 0);
665 /* execute the command indirectly to allow execution even
666 * on no-exec filesystems. */
667 char *cmd
= g_strconcat("/bin/sh ", file_name
, (char *)NULL
);
668 shell_execute (cmd
, EXECUTE_HIDE
);
676 ** Check owner of the menu file. Using menu file is allowed, if
677 ** owner of the menu is root or the actual user. In either case
678 ** file should not be group and word-writable.
680 ** Q. Should we apply this routine to system and home menu (and .ext files)?
683 menu_file_own(char* path
)
687 if (stat (path
, &st
) == 0
688 && (!st
.st_uid
|| (st
.st_uid
== geteuid ()))
689 && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0)
695 message (D_NORMAL
, _(" Warning -- ignoring file "),
696 _("File %s is not owned by root or you or is world writable.\n"
697 "Using it may compromise your security"),
705 * If edit_widget is NULL then we are called from the mc menu,
706 * otherwise we are called from the mcedit menu.
709 user_menu_cmd (struct WEdit
*edit_widget
)
712 char *data
, **entries
;
713 int max_cols
, menu_lines
, menu_limit
;
714 int col
, i
, accept_entry
= 1;
715 int selected
, old_patterns
;
718 if (!vfs_current_is_local ()){
719 message (D_ERROR
, MSG_ERROR
,
720 _(" Cannot execute commands on non-local filesystems"));
724 menu
= g_strdup (edit_widget
? CEDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
725 if (!exist_file (menu
) || !menu_file_own (menu
)){
727 menu
= mhl_str_dir_plus_file \
728 (home_dir
, edit_widget
? CEDIT_HOME_MENU
: MC_HOME_MENU
);
729 if (!exist_file (menu
)){
731 menu
= mhl_str_dir_plus_file \
732 (mc_home
, edit_widget
? CEDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
736 if ((data
= load_file (menu
)) == NULL
){
737 message (D_ERROR
, MSG_ERROR
, _(" Cannot open file %s \n %s "),
738 menu
, unix_error_string (errno
));
749 /* Parse the menu file */
750 old_patterns
= easy_patterns
;
751 p
= check_patterns (data
);
752 for (menu_lines
= col
= 0; *p
; p
++){
753 if (menu_lines
>= menu_limit
){
756 menu_limit
+= MAX_ENTRIES
;
757 new_entries
= g_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
759 if (new_entries
== 0)
762 entries
= new_entries
;
763 new_entries
+= menu_limit
;
764 while (--new_entries
>= &entries
[menu_lines
])
767 if (col
== 0 && !entries
[menu_lines
]){
769 /* A commented menu entry */
771 } else if (*p
== '+'){
773 /* Combined adding and default */
774 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
775 if (selected
== 0 && accept_entry
)
776 selected
= menu_lines
;
778 /* A condition for adding the entry */
779 p
= test_line (edit_widget
, p
, &accept_entry
);
781 } else if (*p
== '='){
783 /* Combined adding and default */
784 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
785 if (selected
== 0 && accept_entry
)
786 selected
= menu_lines
;
788 /* A condition for making the entry default */
790 p
= test_line (edit_widget
, p
, &i
);
791 if (selected
== 0 && i
)
792 selected
= menu_lines
;
795 else if (*p
!= ' ' && *p
!= '\t' && is_printable (*p
)) {
796 /* A menu entry title line */
798 entries
[menu_lines
] = p
;
804 if (entries
[menu_lines
]){
808 max_cols
= max (max_cols
, col
);
817 if (menu_lines
== 0) {
818 message (D_ERROR
, MSG_ERROR
, _(" No suitable entries found in %s "), menu
);
821 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
824 listbox
= create_listbox_window (max_cols
+2, menu_lines
, _(" User menu "),
826 /* insert all the items found */
827 for (i
= 0; i
< menu_lines
; i
++) {
829 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
830 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
833 /* Select the default entry */
834 listbox_select_by_number (listbox
->list
, selected
);
836 selected
= run_listbox (listbox
);
838 execute_menu_command (edit_widget
, entries
[selected
]);
843 easy_patterns
= old_patterns
;