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. */
20 * \brief Source: user menu implementation
32 #include "../src/tty/tty.h"
33 #include "../src/skin/skin.h"
43 #include "../src/search/search.h"
45 #include "../edit/edit.h" /* WEdit, BLOCK_FILE */
47 /* For the simple listbox manager */
51 #include "../src/viewer/mcviewer.h" /* for default_* externs */
53 #define MAX_ENTRIES 16
54 #define MAX_ENTRY_LEN 60
56 static int debug_flag
= 0;
57 static int debug_error
= 0;
58 static char *menu
= NULL
;
62 %f The current file (if non-local vfs, file will be copied locally and
63 %f will be full path to it).
65 %d The current working directory
66 %s "Selected files"; the tagged files if any, otherwise the current file
68 %u Tagged files (and they are untagged on return from expand_format)
69 %view Runs the commands and pipes standard output to the view command.
70 If %view is immediately followed by '{', recognize keywords
71 ascii, hex, nroff and unform
73 If the format letter is in uppercase, it refers to the other panel.
75 With a number followed the % character you can turn quoting on (default)
77 %f quote expanded macro
79 %0f don't quote expanded macro
81 expand_format returns a memory block that must be free()d.
84 /* Returns how many characters we should advance if %view was found */
85 int check_format_view (const char *p
)
88 if (!strncmp (p
, "view", 4)) {
91 for (q
++;*q
&& *q
!= '}';q
++) {
92 if (!strncmp (q
, "ascii", 5)) {
93 mcview_default_hex_mode
= 0;
95 } else if (!strncmp (q
, "hex", 3)) {
96 mcview_default_hex_mode
= 1;
98 } else if (!strncmp (q
, "nroff", 5)) {
99 mcview_default_nroff_flag
= 1;
101 } else if (!strncmp (q
, "unform", 6)) {
102 mcview_default_nroff_flag
= 0;
114 int check_format_cd (const char *p
)
116 return (strncmp (p
, "cd", 2)) ? 0 : 3;
119 /* Check if p has a "^var\{var-name\}" */
120 /* Returns the number of skipped characters (zero on not found) */
121 /* V will be set to the expanded variable name */
122 int check_format_var (const char *p
, char **v
)
127 const char *dots
= 0;
130 if (!strncmp (p
, "var{", 4)){
131 for (q
+= 4; *q
&& *q
!= '}'; q
++){
138 if (!dots
|| dots
== q
+5){
140 _(" Format error on file Extensions File "),
141 !dots
? _(" The %%var macro has no default ")
142 : _(" The %%var macro has no variable "));
146 /* Copy the variable name */
147 var_name
= g_strndup (p
+ 4, dots
-2 - (p
+3));
149 value
= getenv (var_name
);
152 *v
= g_strdup (value
);
155 var_name
= g_strndup (dots
, q
- dots
);
162 /* strip file's extension */
166 register char *s
= ss
;
170 if(*s
== PATH_SEP
&& e
) e
=NULL
; /* '.' in *directory* name */
178 expand_format (WEdit
*edit_widget
, char c
, int lc_quote
)
180 WPanel
*panel
= NULL
;
181 char *(*quote_func
) (const char *, int);
187 return g_strdup ("%");
189 if (edit_one_file
== NULL
) {
190 if (g_ascii_islower ((gchar
) c
))
191 panel
= current_panel
;
193 if (get_other_type () != view_listing
)
194 return g_strdup ("");
197 fname
= panel
->dir
.list
[panel
->selected
].fname
;
199 #ifdef USE_INTERNAL_EDIT
201 fname
= str_unconst (edit_get_file_name (edit_widget
));
205 quote_func
= name_quote
;
207 quote_func
= fake_name_quote
;
209 c_lc
= g_ascii_tolower ((gchar
) c
);
214 return (*quote_func
) (fname
, 0);
216 return (*quote_func
) (extension (fname
), 0);
222 cwd
= g_malloc(MC_MAXPATHLEN
+ 1);
225 g_strlcpy(cwd
, panel
->cwd
, MC_MAXPATHLEN
+ 1);
227 mc_get_current_wd(cwd
, MC_MAXPATHLEN
+ 1);
229 qstr
= (*quote_func
) (cwd
, 0);
235 case 'i': /* indent equal number cursor position in line */
236 #ifdef USE_INTERNAL_EDIT
238 return g_strnfill (edit_get_curs_col (edit_widget
), ' ');
241 case 'y': /* syntax type */
242 #ifdef USE_INTERNAL_EDIT
244 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
245 if (syntax_type
!= NULL
)
246 return g_strdup (syntax_type
);
250 case 'k': /* block file name */
251 case 'b': /* block file name / strip extension */ {
252 #ifdef USE_INTERNAL_EDIT
254 char *file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
255 fname
= (*quote_func
) (file
, 0);
261 return strip_ext ((*quote_func
) (fname
, 0));
264 case 'n': /* strip extension in editor */
265 #ifdef USE_INTERNAL_EDIT
267 return strip_ext ((*quote_func
) (fname
, 0));
270 case 'm': /* menu file name */
272 return (*quote_func
) (menu
, 0);
275 if (!panel
|| !panel
->marked
)
276 return (*quote_func
) (fname
, 0);
287 return g_strdup ("");
289 for (i
= 0; i
< panel
->count
; i
++)
290 if (panel
->dir
.list
[i
].f
.marked
)
291 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
293 block
= g_malloc (length
* 2 + 1);
295 for (i
= 0; i
< panel
->count
; i
++)
296 if (panel
->dir
.list
[i
].f
.marked
) {
298 (*quote_func
) (panel
->dir
.list
[i
].fname
, 0));
302 do_file_mark (panel
, i
, 0);
305 } /* sub case block */
307 result
= g_strdup ("% ");
313 * Check for the "shell_patterns" directive. If it's found and valid,
314 * interpret it and move the pointer past the directive. Return the
318 check_patterns (char *p
)
320 static const char def_name
[] = "shell_patterns=";
323 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
326 p
+= sizeof (def_name
) - 1;
336 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
341 /* Copies a whitespace separated argument from p to arg. Returns the
342 point after argument. */
343 static char *extract_arg (char *p
, char *arg
, int size
)
347 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
349 /* support quote space .mnu */
350 while (*p
&& (*p
!= ' ' || *(p
-1) == '\\') && *p
!= '\t' && *p
!= '\n') {
351 np
= str_get_next_char (p
);
352 if (np
- p
>= size
) break;
353 memcpy (arg
, p
, np
- p
);
359 if (!*p
|| *p
== '\n')
364 /* Tests whether the selected file in the panel is of any of the types
365 specified in argument. */
366 static int test_type (WPanel
*panel
, char *arg
)
368 int result
= 0; /* False by default */
369 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
371 for (;*arg
!= 0; arg
++){
373 case 'n': /* Not a directory */
374 result
|= !S_ISDIR (st_mode
);
376 case 'r': /* Regular file */
377 result
|= S_ISREG (st_mode
);
379 case 'd': /* Directory */
380 result
|= S_ISDIR (st_mode
);
383 result
|= S_ISLNK (st_mode
);
385 case 'c': /* Character special */
386 result
|= S_ISCHR (st_mode
);
388 case 'b': /* Block special */
389 result
|= S_ISBLK (st_mode
);
391 case 'f': /* Fifo (named pipe) */
392 result
|= S_ISFIFO (st_mode
);
394 case 's': /* Socket */
395 result
|= S_ISSOCK (st_mode
);
397 case 'x': /* Executable */
398 result
|= (st_mode
& 0111) ? 1 : 0;
401 result
|= panel
->marked
? 1 : 0;
411 /* Calculates the truth value of the next condition starting from
412 p. Returns the point after condition. */
414 test_condition (WEdit
*edit_widget
, char *p
, int *condition
)
418 mc_search_type_t search_type
;
421 search_type
= MC_SEARCH_T_GLOB
;
423 search_type
= MC_SEARCH_T_REGEX
;
426 /* Handle one condition */
427 for (;*p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++){
428 /* support quote space .mnu */
429 if ((*p
== ' ' && *(p
-1) != '\\') || *p
== '\t')
432 panel
= current_panel
;
434 if (get_other_type () == view_listing
)
443 p
= test_condition (edit_widget
, p
, condition
);
444 *condition
= ! *condition
;
447 case 'f': /* file name pattern */
448 p
= extract_arg (p
, arg
, sizeof (arg
));
449 *condition
= panel
&& mc_search (arg
, panel
->dir
.list
[panel
->selected
].fname
, search_type
);
451 case 'y': /* syntax pattern */
452 #ifdef USE_INTERNAL_EDIT
454 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
455 if (syntax_type
!= NULL
) {
456 p
= extract_arg (p
, arg
, sizeof (arg
));
457 *condition
= panel
&& mc_search (arg
, syntax_type
, MC_SEARCH_T_NORMAL
);
463 p
= extract_arg (p
, arg
, sizeof (arg
));
464 *condition
= panel
&& mc_search (arg
, panel
->cwd
, search_type
);
467 p
= extract_arg (p
, arg
, sizeof (arg
));
468 *condition
= panel
&& test_type (panel
, arg
);
470 case 'x': /* executable */
474 p
= extract_arg (p
, arg
, sizeof (arg
));
475 if (stat (arg
, &status
) == 0)
476 *condition
= is_exe (status
.st_mode
);
490 /* General purpose condition debug output handler */
492 debug_out (char *start
, char *end
, int cond
)
497 if (start
== NULL
&& end
== NULL
){
499 if (debug_flag
&& msg
) {
503 message (D_NORMAL
, _(" Debug "), "%s", msg
);
513 /* Save debug info for later output */
516 /* Save the result of the condition */
518 type
= _(" ERROR: ");
524 type
= _(" False: ");
525 /* This is for debugging, don't need to be super efficient. */
527 p
= g_strdup_printf ("%s%s%c \n", msg
? msg
: "", type
, *start
);
529 p
= g_strdup_printf ("%s%s%.*s \n", msg
? msg
: "", type
,
530 (int) (end
- start
), start
);
536 /* Calculates the truth value of one lineful of conditions. Returns
537 the point just before the end of line. */
539 test_line (WEdit
*edit_widget
, char *p
, int *result
)
543 char *debug_start
, *debug_end
;
545 /* Repeat till end of line */
546 while (*p
&& *p
!= '\n') {
547 /* support quote space .mnu */
548 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
550 if (!*p
|| *p
== '\n')
557 /* support quote space .mnu */
558 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
560 if (!*p
|| *p
== '\n')
562 condition
= 1; /* True by default */
565 p
= test_condition (edit_widget
, p
, &condition
);
567 /* Add one debug statement */
568 debug_out (debug_start
, debug_end
, condition
);
576 case '&': /* Logical and */
577 *result
&= condition
;
579 case '|': /* Logical or */
580 *result
|= condition
;
586 /* Add one debug statement */
587 debug_out (&operator, NULL
, *result
);
589 } /* while (*p != '\n') */
590 /* Report debug message */
591 debug_out (NULL
, NULL
, 1);
593 if (!*p
|| *p
== '\n')
598 /* FIXME: recode this routine on version 3.0, it could be cleaner */
600 execute_menu_command (WEdit
*edit_widget
, const char *commands
)
604 int expand_prefix_found
= 0;
612 /* Skip menu entry title line */
613 commands
= strchr (commands
, '\n');
618 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
620 if (cmd_file_fd
== -1){
621 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary command file \n %s "),
622 unix_error_string (errno
));
625 cmd_file
= fdopen (cmd_file_fd
, "w");
626 fputs ("#! /bin/sh\n", cmd_file
);
629 for (col
= 0; *commands
; commands
++){
631 if (*commands
!= ' ' && *commands
!= '\t')
633 while (*commands
== ' ' || *commands
== '\t')
639 if (*commands
== '\n')
642 if (*commands
== '}'){
645 parameter
= input_dialog (_(" Parameter "), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "");
646 if (!parameter
|| !*parameter
){
654 fputs (tmp
= name_quote (parameter
, 0), cmd_file
);
657 fputs (parameter
, cmd_file
);
661 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1]) {
662 *parameter
++ = *commands
;
665 } else if (expand_prefix_found
){
666 expand_prefix_found
= 0;
667 if (g_ascii_isdigit ((gchar
) *commands
)) {
668 do_quote
= atoi (commands
);
669 while (g_ascii_isdigit ((gchar
) *commands
))
672 if (*commands
== '{')
673 parameter
= lc_prompt
;
675 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
676 fputs (text
, cmd_file
);
680 if (*commands
== '%') {
681 int i
= check_format_view (commands
+ 1);
686 do_quote
= 1; /* Default: Quote expanded macro */
687 expand_prefix_found
= 1;
690 fputc (*commands
, cmd_file
);
694 chmod (file_name
, S_IRWXU
);
697 mcview_viewer (file_name
, NULL
, &run_view
, 0);
699 /* execute the command indirectly to allow execution even
700 * on no-exec filesystems. */
701 char *cmd
= g_strconcat("/bin/sh ", file_name
, (char *)NULL
);
702 shell_execute (cmd
, EXECUTE_HIDE
);
710 ** Check owner of the menu file. Using menu file is allowed, if
711 ** owner of the menu is root or the actual user. In either case
712 ** file should not be group and word-writable.
714 ** Q. Should we apply this routine to system and home menu (and .ext files)?
717 menu_file_own(char* path
)
721 if (stat (path
, &st
) == 0
722 && (!st
.st_uid
|| (st
.st_uid
== geteuid ()))
723 && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0)
729 message (D_NORMAL
, _(" Warning -- ignoring file "),
730 _("File %s is not owned by root or you or is world writable.\n"
731 "Using it may compromise your security"),
739 * If edit_widget is NULL then we are called from the mc menu,
740 * otherwise we are called from the mcedit menu.
743 user_menu_cmd (WEdit
*edit_widget
)
746 char *data
, **entries
;
747 int max_cols
, menu_lines
, menu_limit
;
748 int col
, i
, accept_entry
= 1;
749 int selected
, old_patterns
;
752 if (!vfs_current_is_local ()){
753 message (D_ERROR
, MSG_ERROR
,
754 _(" Cannot execute commands on non-local filesystems"));
758 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
759 if (!exist_file (menu
) || !menu_file_own (menu
)){
762 menu
= concat_dir_and_file (home_dir
, EDIT_HOME_MENU
);
764 menu
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_USERMENU_FILE
, NULL
);
767 if (!exist_file (menu
)){
769 menu
= concat_dir_and_file
770 (mc_home
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
771 if (!exist_file (menu
)) {
773 menu
= concat_dir_and_file
774 (mc_home_alt
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
779 if ((data
= load_file (menu
)) == NULL
){
780 message (D_ERROR
, MSG_ERROR
, _(" Cannot open file %s \n %s "),
781 menu
, unix_error_string (errno
));
792 /* Parse the menu file */
793 old_patterns
= easy_patterns
;
794 p
= check_patterns (data
);
795 for (menu_lines
= col
= 0; *p
; str_next_char (&p
)){
796 if (menu_lines
>= menu_limit
){
799 menu_limit
+= MAX_ENTRIES
;
800 new_entries
= g_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
802 if (new_entries
== 0)
805 entries
= new_entries
;
806 new_entries
+= menu_limit
;
807 while (--new_entries
>= &entries
[menu_lines
])
810 if (col
== 0 && !entries
[menu_lines
]){
812 /* A commented menu entry */
814 } else if (*p
== '+'){
816 /* Combined adding and default */
817 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
818 if (selected
== 0 && accept_entry
)
819 selected
= menu_lines
;
821 /* A condition for adding the entry */
822 p
= test_line (edit_widget
, p
, &accept_entry
);
824 } else if (*p
== '='){
826 /* Combined adding and default */
827 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
828 if (selected
== 0 && accept_entry
)
829 selected
= menu_lines
;
831 /* A condition for making the entry default */
833 p
= test_line (edit_widget
, p
, &i
);
834 if (selected
== 0 && i
)
835 selected
= menu_lines
;
838 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
)) {
839 /* A menu entry title line */
841 entries
[menu_lines
] = p
;
847 if (entries
[menu_lines
]){
851 max_cols
= max (max_cols
, col
);
861 message (D_ERROR
, MSG_ERROR
, _(" No suitable entries found in %s "), menu
);
863 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
866 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2,_(" User menu "),
868 /* insert all the items found */
869 for (i
= 0; i
< menu_lines
; i
++) {
871 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
872 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
874 /* Select the default entry */
875 listbox_select_by_number (listbox
->list
, selected
);
877 selected
= run_listbox (listbox
);
879 execute_menu_command (edit_widget
, entries
[selected
]);
884 easy_patterns
= old_patterns
;