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);
186 #ifndef USE_INTERNAL_EDIT
191 return g_strdup ("%");
193 if (edit_one_file
== NULL
) {
194 if (g_ascii_islower ((gchar
) c
))
195 panel
= current_panel
;
197 if (get_other_type () != view_listing
)
198 return g_strdup ("");
201 fname
= panel
->dir
.list
[panel
->selected
].fname
;
203 #ifdef USE_INTERNAL_EDIT
205 fname
= str_unconst (edit_get_file_name (edit_widget
));
209 quote_func
= name_quote
;
211 quote_func
= fake_name_quote
;
213 c_lc
= g_ascii_tolower ((gchar
) c
);
218 return (*quote_func
) (fname
, 0);
220 return (*quote_func
) (extension (fname
), 0);
226 cwd
= g_malloc(MC_MAXPATHLEN
+ 1);
229 g_strlcpy(cwd
, panel
->cwd
, MC_MAXPATHLEN
+ 1);
231 mc_get_current_wd(cwd
, MC_MAXPATHLEN
+ 1);
233 qstr
= (*quote_func
) (cwd
, 0);
239 case 'i': /* indent equal number cursor position in line */
240 #ifdef USE_INTERNAL_EDIT
242 return g_strnfill (edit_get_curs_col (edit_widget
), ' ');
245 case 'y': /* syntax type */
246 #ifdef USE_INTERNAL_EDIT
248 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
249 if (syntax_type
!= NULL
)
250 return g_strdup (syntax_type
);
254 case 'k': /* block file name */
255 case 'b': /* block file name / strip extension */ {
256 #ifdef USE_INTERNAL_EDIT
258 char *file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
259 fname
= (*quote_func
) (file
, 0);
265 return strip_ext ((*quote_func
) (fname
, 0));
268 case 'n': /* strip extension in editor */
269 #ifdef USE_INTERNAL_EDIT
271 return strip_ext ((*quote_func
) (fname
, 0));
274 case 'm': /* menu file name */
276 return (*quote_func
) (menu
, 0);
279 if (!panel
|| !panel
->marked
)
280 return (*quote_func
) (fname
, 0);
291 return g_strdup ("");
293 for (i
= 0; i
< panel
->count
; i
++)
294 if (panel
->dir
.list
[i
].f
.marked
)
295 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
297 block
= g_malloc (length
* 2 + 1);
299 for (i
= 0; i
< panel
->count
; i
++)
300 if (panel
->dir
.list
[i
].f
.marked
) {
302 (*quote_func
) (panel
->dir
.list
[i
].fname
, 0));
306 do_file_mark (panel
, i
, 0);
309 } /* sub case block */
311 result
= g_strdup ("% ");
317 * Check for the "shell_patterns" directive. If it's found and valid,
318 * interpret it and move the pointer past the directive. Return the
322 check_patterns (char *p
)
324 static const char def_name
[] = "shell_patterns=";
327 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
330 p
+= sizeof (def_name
) - 1;
340 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
345 /* Copies a whitespace separated argument from p to arg. Returns the
346 point after argument. */
347 static char *extract_arg (char *p
, char *arg
, int size
)
351 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
353 /* support quote space .mnu */
354 while (*p
&& (*p
!= ' ' || *(p
-1) == '\\') && *p
!= '\t' && *p
!= '\n') {
355 np
= str_get_next_char (p
);
356 if (np
- p
>= size
) break;
357 memcpy (arg
, p
, np
- p
);
363 if (!*p
|| *p
== '\n')
368 /* Tests whether the selected file in the panel is of any of the types
369 specified in argument. */
370 static int test_type (WPanel
*panel
, char *arg
)
372 int result
= 0; /* False by default */
373 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
375 for (;*arg
!= 0; arg
++){
377 case 'n': /* Not a directory */
378 result
|= !S_ISDIR (st_mode
);
380 case 'r': /* Regular file */
381 result
|= S_ISREG (st_mode
);
383 case 'd': /* Directory */
384 result
|= S_ISDIR (st_mode
);
387 result
|= S_ISLNK (st_mode
);
389 case 'c': /* Character special */
390 result
|= S_ISCHR (st_mode
);
392 case 'b': /* Block special */
393 result
|= S_ISBLK (st_mode
);
395 case 'f': /* Fifo (named pipe) */
396 result
|= S_ISFIFO (st_mode
);
398 case 's': /* Socket */
399 result
|= S_ISSOCK (st_mode
);
401 case 'x': /* Executable */
402 result
|= (st_mode
& 0111) ? 1 : 0;
405 result
|= panel
->marked
? 1 : 0;
415 /* Calculates the truth value of the next condition starting from
416 p. Returns the point after condition. */
418 test_condition (WEdit
*edit_widget
, char *p
, int *condition
)
422 mc_search_type_t search_type
;
425 search_type
= MC_SEARCH_T_GLOB
;
427 search_type
= MC_SEARCH_T_REGEX
;
430 /* Handle one condition */
431 for (;*p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++){
432 /* support quote space .mnu */
433 if ((*p
== ' ' && *(p
-1) != '\\') || *p
== '\t')
436 panel
= current_panel
;
438 if (get_other_type () == view_listing
)
447 p
= test_condition (edit_widget
, p
, condition
);
448 *condition
= ! *condition
;
451 case 'f': /* file name pattern */
452 p
= extract_arg (p
, arg
, sizeof (arg
));
453 *condition
= panel
&& mc_search (arg
, panel
->dir
.list
[panel
->selected
].fname
, search_type
);
455 case 'y': /* syntax pattern */
456 #ifdef USE_INTERNAL_EDIT
458 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
459 if (syntax_type
!= NULL
) {
460 p
= extract_arg (p
, arg
, sizeof (arg
));
461 *condition
= panel
&& mc_search (arg
, syntax_type
, MC_SEARCH_T_NORMAL
);
467 p
= extract_arg (p
, arg
, sizeof (arg
));
468 *condition
= panel
&& mc_search (arg
, panel
->cwd
, search_type
);
471 p
= extract_arg (p
, arg
, sizeof (arg
));
472 *condition
= panel
&& test_type (panel
, arg
);
474 case 'x': /* executable */
478 p
= extract_arg (p
, arg
, sizeof (arg
));
479 if (stat (arg
, &status
) == 0)
480 *condition
= is_exe (status
.st_mode
);
494 /* General purpose condition debug output handler */
496 debug_out (char *start
, char *end
, int cond
)
501 if (start
== NULL
&& end
== NULL
){
503 if (debug_flag
&& msg
) {
507 message (D_NORMAL
, _(" Debug "), "%s", msg
);
517 /* Save debug info for later output */
520 /* Save the result of the condition */
522 type
= _(" ERROR: ");
528 type
= _(" False: ");
529 /* This is for debugging, don't need to be super efficient. */
531 p
= g_strdup_printf ("%s%s%c \n", msg
? msg
: "", type
, *start
);
533 p
= g_strdup_printf ("%s%s%.*s \n", msg
? msg
: "", type
,
534 (int) (end
- start
), start
);
540 /* Calculates the truth value of one lineful of conditions. Returns
541 the point just before the end of line. */
543 test_line (WEdit
*edit_widget
, char *p
, int *result
)
547 char *debug_start
, *debug_end
;
549 /* Repeat till end of line */
550 while (*p
&& *p
!= '\n') {
551 /* support quote space .mnu */
552 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
554 if (!*p
|| *p
== '\n')
561 /* support quote space .mnu */
562 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
564 if (!*p
|| *p
== '\n')
566 condition
= 1; /* True by default */
569 p
= test_condition (edit_widget
, p
, &condition
);
571 /* Add one debug statement */
572 debug_out (debug_start
, debug_end
, condition
);
580 case '&': /* Logical and */
581 *result
&= condition
;
583 case '|': /* Logical or */
584 *result
|= condition
;
590 /* Add one debug statement */
591 debug_out (&operator, NULL
, *result
);
593 } /* while (*p != '\n') */
594 /* Report debug message */
595 debug_out (NULL
, NULL
, 1);
597 if (!*p
|| *p
== '\n')
602 /* FIXME: recode this routine on version 3.0, it could be cleaner */
604 execute_menu_command (WEdit
*edit_widget
, const char *commands
)
608 int expand_prefix_found
= 0;
616 /* Skip menu entry title line */
617 commands
= strchr (commands
, '\n');
622 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
624 if (cmd_file_fd
== -1){
625 message (D_ERROR
, MSG_ERROR
, _(" Cannot create temporary command file \n %s "),
626 unix_error_string (errno
));
629 cmd_file
= fdopen (cmd_file_fd
, "w");
630 fputs ("#! /bin/sh\n", cmd_file
);
633 for (col
= 0; *commands
; commands
++){
635 if (*commands
!= ' ' && *commands
!= '\t')
637 while (*commands
== ' ' || *commands
== '\t')
643 if (*commands
== '\n')
646 if (*commands
== '}'){
649 parameter
= input_dialog (_(" Parameter "), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "");
650 if (!parameter
|| !*parameter
){
658 fputs (tmp
= name_quote (parameter
, 0), cmd_file
);
661 fputs (parameter
, cmd_file
);
665 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1]) {
666 *parameter
++ = *commands
;
669 } else if (expand_prefix_found
){
670 expand_prefix_found
= 0;
671 if (g_ascii_isdigit ((gchar
) *commands
)) {
672 do_quote
= atoi (commands
);
673 while (g_ascii_isdigit ((gchar
) *commands
))
676 if (*commands
== '{')
677 parameter
= lc_prompt
;
679 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
680 fputs (text
, cmd_file
);
684 if (*commands
== '%') {
685 int i
= check_format_view (commands
+ 1);
690 do_quote
= 1; /* Default: Quote expanded macro */
691 expand_prefix_found
= 1;
694 fputc (*commands
, cmd_file
);
698 chmod (file_name
, S_IRWXU
);
701 mcview_viewer (file_name
, NULL
, &run_view
, 0);
703 /* execute the command indirectly to allow execution even
704 * on no-exec filesystems. */
705 char *cmd
= g_strconcat("/bin/sh ", file_name
, (char *)NULL
);
706 shell_execute (cmd
, EXECUTE_HIDE
);
714 ** Check owner of the menu file. Using menu file is allowed, if
715 ** owner of the menu is root or the actual user. In either case
716 ** file should not be group and word-writable.
718 ** Q. Should we apply this routine to system and home menu (and .ext files)?
721 menu_file_own(char* path
)
725 if (stat (path
, &st
) == 0
726 && (!st
.st_uid
|| (st
.st_uid
== geteuid ()))
727 && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0)
733 message (D_NORMAL
, _(" Warning -- ignoring file "),
734 _("File %s is not owned by root or you or is world writable.\n"
735 "Using it may compromise your security"),
743 * If edit_widget is NULL then we are called from the mc menu,
744 * otherwise we are called from the mcedit menu.
747 user_menu_cmd (WEdit
*edit_widget
)
750 char *data
, **entries
;
751 int max_cols
, menu_lines
, menu_limit
;
752 int col
, i
, accept_entry
= 1;
753 int selected
, old_patterns
;
756 if (!vfs_current_is_local ()){
757 message (D_ERROR
, MSG_ERROR
,
758 _(" Cannot execute commands on non-local filesystems"));
762 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
763 if (!exist_file (menu
) || !menu_file_own (menu
)){
766 menu
= concat_dir_and_file (home_dir
, EDIT_HOME_MENU
);
768 menu
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_USERMENU_FILE
, NULL
);
771 if (!exist_file (menu
)){
773 menu
= concat_dir_and_file
774 (mc_home
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
775 if (!exist_file (menu
)) {
777 menu
= concat_dir_and_file
778 (mc_home_alt
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
783 if ((data
= load_file (menu
)) == NULL
){
784 message (D_ERROR
, MSG_ERROR
, _(" Cannot open file %s \n %s "),
785 menu
, unix_error_string (errno
));
796 /* Parse the menu file */
797 old_patterns
= easy_patterns
;
798 p
= check_patterns (data
);
799 for (menu_lines
= col
= 0; *p
; str_next_char (&p
)){
800 if (menu_lines
>= menu_limit
){
803 menu_limit
+= MAX_ENTRIES
;
804 new_entries
= g_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
806 if (new_entries
== 0)
809 entries
= new_entries
;
810 new_entries
+= menu_limit
;
811 while (--new_entries
>= &entries
[menu_lines
])
814 if (col
== 0 && !entries
[menu_lines
]){
816 /* A commented menu entry */
818 } else if (*p
== '+'){
820 /* Combined adding and default */
821 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
822 if (selected
== 0 && accept_entry
)
823 selected
= menu_lines
;
825 /* A condition for adding the entry */
826 p
= test_line (edit_widget
, p
, &accept_entry
);
828 } else if (*p
== '='){
830 /* Combined adding and default */
831 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
832 if (selected
== 0 && accept_entry
)
833 selected
= menu_lines
;
835 /* A condition for making the entry default */
837 p
= test_line (edit_widget
, p
, &i
);
838 if (selected
== 0 && i
)
839 selected
= menu_lines
;
842 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
)) {
843 /* A menu entry title line */
845 entries
[menu_lines
] = p
;
851 if (entries
[menu_lines
]){
855 max_cols
= max (max_cols
, col
);
865 message (D_ERROR
, MSG_ERROR
, _(" No suitable entries found in %s "), menu
);
867 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
870 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2,_(" User menu "),
872 /* insert all the items found */
873 for (i
= 0; i
< menu_lines
; i
++) {
875 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
876 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
878 /* Select the default entry */
879 listbox_select_by_number (listbox
->list
, selected
);
881 selected
= run_listbox (listbox
);
883 execute_menu_command (edit_widget
, entries
[selected
]);
888 easy_patterns
= old_patterns
;