2 User Menu implementation
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * \brief Source: user menu implementation
36 #include "lib/global.h"
37 #include "lib/tty/tty.h"
39 #include "lib/search.h"
40 #include "lib/vfs/vfs.h"
41 #include "lib/strutil.h"
43 #include "lib/widget.h"
45 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
46 #include "src/viewer/mcviewer.h" /* for default_* externs */
48 #include "src/execute.h"
49 #include "src/setup.h"
50 #include "src/history.h"
58 /*** global variables ****************************************************************************/
60 /*** file scope macro definitions ****************************************************************/
62 #define MAX_ENTRIES 16
63 #define MAX_ENTRY_LEN 60
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 static int debug_flag
= 0;
70 static int debug_error
= 0;
71 static char *menu
= NULL
;
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 /** strip file's extension */
80 register char *s
= ss
;
86 if (*s
== PATH_SEP
&& e
)
87 e
= NULL
; /* '.' in *directory* name */
95 /* --------------------------------------------------------------------------------------------- */
97 * Check for the "shell_patterns" directive. If it's found and valid,
98 * interpret it and move the pointer past the directive. Return the
103 check_patterns (char *p
)
105 static const char def_name
[] = "shell_patterns=";
108 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
111 p
+= sizeof (def_name
) - 1;
121 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
126 /* --------------------------------------------------------------------------------------------- */
127 /** Copies a whitespace separated argument from p to arg. Returns the
128 point after argument. */
131 extract_arg (char *p
, char *arg
, int size
)
135 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
137 /* support quote space .mnu */
138 while (*p
&& (*p
!= ' ' || *(p
- 1) == '\\') && *p
!= '\t' && *p
!= '\n')
140 np
= str_get_next_char (p
);
143 memcpy (arg
, p
, np
- p
);
149 if (!*p
|| *p
== '\n')
154 /* --------------------------------------------------------------------------------------------- */
155 /* Tests whether the selected file in the panel is of any of the types
156 specified in argument. */
159 test_type (WPanel
* panel
, char *arg
)
161 int result
= 0; /* False by default */
162 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
164 for (; *arg
!= 0; arg
++)
168 case 'n': /* Not a directory */
169 result
|= !S_ISDIR (st_mode
);
171 case 'r': /* Regular file */
172 result
|= S_ISREG (st_mode
);
174 case 'd': /* Directory */
175 result
|= S_ISDIR (st_mode
);
178 result
|= S_ISLNK (st_mode
);
180 case 'c': /* Character special */
181 result
|= S_ISCHR (st_mode
);
183 case 'b': /* Block special */
184 result
|= S_ISBLK (st_mode
);
186 case 'f': /* Fifo (named pipe) */
187 result
|= S_ISFIFO (st_mode
);
189 case 's': /* Socket */
190 result
|= S_ISSOCK (st_mode
);
192 case 'x': /* Executable */
193 result
|= (st_mode
& 0111) ? 1 : 0;
196 result
|= panel
->marked
? 1 : 0;
206 /* --------------------------------------------------------------------------------------------- */
207 /** Calculates the truth value of the next condition starting from
208 p. Returns the point after condition. */
211 test_condition (WEdit
* edit_widget
, char *p
, int *condition
)
214 const mc_search_type_t search_type
= easy_patterns
? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
216 /* Handle one condition */
217 for (; *p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++)
219 WPanel
*panel
= NULL
;
221 /* support quote space .mnu */
222 if ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
225 panel
= current_panel
;
226 else if (get_other_type () == view_listing
)
234 p
= test_condition (edit_widget
, p
, condition
);
235 *condition
= !*condition
;
238 case 'f': /* file name pattern */
239 p
= extract_arg (p
, arg
, sizeof (arg
));
240 #ifdef USE_INTERNAL_EDIT
241 if (edit_widget
!= NULL
)
245 edit_filename
= edit_get_file_name (edit_widget
);
246 *condition
= mc_search (arg
, edit_filename
, search_type
) ? 1 : 0;
247 g_free (edit_filename
);
251 *condition
= panel
!= NULL
&&
252 mc_search (arg
, panel
->dir
.list
[panel
->selected
].fname
, search_type
) ? 1 : 0;
254 case 'y': /* syntax pattern */
255 #ifdef USE_INTERNAL_EDIT
256 if (edit_widget
!= NULL
)
258 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
259 if (syntax_type
!= NULL
)
261 p
= extract_arg (p
, arg
, sizeof (arg
));
262 *condition
= mc_search (arg
, syntax_type
, MC_SEARCH_T_NORMAL
) ? 1 : 0;
268 p
= extract_arg (p
, arg
, sizeof (arg
));
272 cwd_str
= vfs_path_to_str (panel
->cwd_vpath
);
273 *condition
= panel
!= NULL
&& mc_search (arg
, cwd_str
, search_type
) ? 1 : 0;
278 p
= extract_arg (p
, arg
, sizeof (arg
));
279 *condition
= panel
!= NULL
&& test_type (panel
, arg
) ? 1 : 0;
281 case 'x': /* executable */
285 p
= extract_arg (p
, arg
, sizeof (arg
));
286 if (stat (arg
, &status
) == 0)
287 *condition
= is_exe (status
.st_mode
) ? 1 : 0;
301 /* --------------------------------------------------------------------------------------------- */
302 /** General purpose condition debug output handler */
305 debug_out (char *start
, char *end
, int cond
)
310 if (start
== NULL
&& end
== NULL
)
313 if (debug_flag
&& msg
)
318 message (D_NORMAL
, _("Debug"), "%s", msg
);
330 /* Save debug info for later output */
333 /* Save the result of the condition */
343 /* This is for debugging, don't need to be super efficient. */
345 p
= g_strdup_printf ("%s %s %c \n", msg
? msg
: "", type
, *start
);
347 p
= g_strdup_printf ("%s %s %.*s \n", msg
? msg
: "", type
, (int) (end
- start
), start
);
353 /* --------------------------------------------------------------------------------------------- */
354 /** Calculates the truth value of one lineful of conditions. Returns
355 the point just before the end of line. */
358 test_line (WEdit
* edit_widget
, char *p
, int *result
)
362 char *debug_start
, *debug_end
;
364 /* Repeat till end of line */
365 while (*p
&& *p
!= '\n')
367 /* support quote space .mnu */
368 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
370 if (!*p
|| *p
== '\n')
378 /* support quote space .mnu */
379 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
381 if (!*p
|| *p
== '\n')
383 condition
= 1; /* True by default */
386 p
= test_condition (edit_widget
, p
, &condition
);
388 /* Add one debug statement */
389 debug_out (debug_start
, debug_end
, condition
);
398 case '&': /* Logical and */
399 *result
&= condition
;
401 case '|': /* Logical or */
402 *result
|= condition
;
408 /* Add one debug statement */
409 debug_out (&operator, NULL
, *result
);
411 } /* while (*p != '\n') */
412 /* Report debug message */
413 debug_out (NULL
, NULL
, 1);
415 if (!*p
|| *p
== '\n')
420 /* --------------------------------------------------------------------------------------------- */
421 /** FIXME: recode this routine on version 3.0, it could be cleaner */
424 execute_menu_command (WEdit
* edit_widget
, const char *commands
, gboolean show_prompt
)
428 int expand_prefix_found
= 0;
430 gboolean do_quote
= FALSE
;
433 vfs_path_t
*file_name_vpath
;
436 /* Skip menu entry title line */
437 commands
= strchr (commands
, '\n');
443 cmd_file_fd
= mc_mkstemps (&file_name_vpath
, "mcusr", SCRIPT_SUFFIX
);
445 if (cmd_file_fd
== -1)
447 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary command file\n%s"),
448 unix_error_string (errno
));
449 vfs_path_free (file_name_vpath
);
452 cmd_file
= fdopen (cmd_file_fd
, "w");
453 fputs ("#! /bin/sh\n", cmd_file
);
456 for (col
= 0; *commands
; commands
++)
460 if (*commands
!= ' ' && *commands
!= '\t')
462 while (*commands
== ' ' || *commands
== '\t')
468 if (*commands
== '\n')
472 if (*commands
== '}')
477 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "",
478 INPUT_COMPLETE_DEFAULT
);
479 if (!parameter
|| !*parameter
)
483 mc_unlink (file_name_vpath
);
484 vfs_path_free (file_name_vpath
);
489 tmp
= name_quote (parameter
, 0);
490 fputs (tmp
, cmd_file
);
494 fputs (parameter
, cmd_file
);
500 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1])
502 *parameter
++ = *commands
;
506 else if (expand_prefix_found
)
508 expand_prefix_found
= 0;
509 if (g_ascii_isdigit ((gchar
) * commands
))
511 do_quote
= (atoi (commands
) != 0);
512 while (g_ascii_isdigit ((gchar
) * commands
))
515 if (*commands
== '{')
516 parameter
= lc_prompt
;
519 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
520 fputs (text
, cmd_file
);
526 if (*commands
== '%')
528 int i
= check_format_view (commands
+ 1);
536 do_quote
= TRUE
; /* Default: Quote expanded macro */
537 expand_prefix_found
= 1;
541 fputc (*commands
, cmd_file
);
545 mc_chmod (file_name_vpath
, S_IRWXU
);
550 file_name
= vfs_path_to_str (file_name_vpath
);
551 mcview_viewer (file_name
, NULL
, 0);
553 dialog_switch_process_pending ();
557 /* execute the command indirectly to allow execution even
558 * on no-exec filesystems. */
559 char *file_name
, *cmd
;
561 file_name
= vfs_path_to_str (file_name_vpath
);
562 cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
566 if (system (cmd
) == -1)
567 message (D_ERROR
, MSG_ERROR
, "%s", _("Error calling program"));
571 shell_execute (cmd
, EXECUTE_HIDE
);
575 mc_unlink (file_name_vpath
);
576 vfs_path_free (file_name_vpath
);
579 /* --------------------------------------------------------------------------------------------- */
581 ** Check owner of the menu file. Using menu file is allowed, if
582 ** owner of the menu is root or the actual user. In either case
583 ** file should not be group and word-writable.
585 ** Q. Should we apply this routine to system and home menu (and .ext files)?
589 menu_file_own (char *path
)
593 if (stat (path
, &st
) == 0
594 && (!st
.st_uid
|| (st
.st_uid
== geteuid ())) && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0))
600 message (D_NORMAL
, _("Warning -- ignoring file"),
601 _("File %s is not owned by root or you or is world writable.\n"
602 "Using it may compromise your security"), path
);
607 /* --------------------------------------------------------------------------------------------- */
608 /*** public functions ****************************************************************************/
609 /* --------------------------------------------------------------------------------------------- */
613 %f The current file (if non-local vfs, file will be copied locally and
614 %f will be full path to it).
616 %d The current working directory
617 %s "Selected files"; the tagged files if any, otherwise the current file
619 %u Tagged files (and they are untagged on return from expand_format)
620 %view Runs the commands and pipes standard output to the view command.
621 If %view is immediately followed by '{', recognize keywords
622 ascii, hex, nroff and unform
624 If the format letter is in uppercase, it refers to the other panel.
626 With a number followed the % character you can turn quoting on (default)
627 and off. For example:
628 %f quote expanded macro
630 %0f don't quote expanded macro
632 expand_format returns a memory block that must be free()d.
635 /* Returns how many characters we should advance if %view was found */
637 check_format_view (const char *p
)
640 if (!strncmp (p
, "view", 4))
645 for (q
++; *q
&& *q
!= '}'; q
++)
647 if (!strncmp (q
, "ascii", 5))
649 mcview_default_hex_mode
= 0;
652 else if (!strncmp (q
, "hex", 3))
654 mcview_default_hex_mode
= 1;
657 else if (!strncmp (q
, "nroff", 5))
659 mcview_default_nroff_flag
= 1;
662 else if (!strncmp (q
, "unform", 6))
664 mcview_default_nroff_flag
= 0;
676 /* --------------------------------------------------------------------------------------------- */
679 check_format_cd (const char *p
)
681 return (strncmp (p
, "cd", 2)) ? 0 : 3;
684 /* --------------------------------------------------------------------------------------------- */
685 /* Check if p has a "^var\{var-name\}" */
686 /* Returns the number of skipped characters (zero on not found) */
687 /* V will be set to the expanded variable name */
690 check_format_var (const char *p
, char **v
)
695 const char *dots
= 0;
698 if (!strncmp (p
, "var{", 4))
700 for (q
+= 4; *q
&& *q
!= '}'; q
++)
708 if (!dots
|| dots
== q
+ 5)
711 _("Format error on file Extensions File"),
712 !dots
? _("The %%var macro has no default")
713 : _("The %%var macro has no variable"));
717 /* Copy the variable name */
718 var_name
= g_strndup (p
+ 4, dots
- 2 - (p
+ 3));
720 value
= getenv (var_name
);
724 *v
= g_strdup (value
);
727 var_name
= g_strndup (dots
, q
- dots
);
734 /* --------------------------------------------------------------------------------------------- */
737 expand_format (struct WEdit
*edit_widget
, char c
, gboolean do_quote
)
739 WPanel
*panel
= NULL
;
740 char *(*quote_func
) (const char *, int);
745 #ifndef USE_INTERNAL_EDIT
750 return g_strdup ("%");
752 switch (mc_global
.mc_run_mode
)
755 if (g_ascii_islower ((gchar
) c
))
756 panel
= current_panel
;
759 if (get_other_type () != view_listing
)
760 return g_strdup ("");
763 fname
= g_strdup (panel
->dir
.list
[panel
->selected
].fname
);
766 #ifdef USE_INTERNAL_EDIT
768 fname
= edit_get_file_name (edit_widget
);
773 /* other modes don't use formats */
774 return g_strdup ("");
778 quote_func
= name_quote
;
780 quote_func
= fake_name_quote
;
782 c_lc
= g_ascii_tolower ((gchar
) c
);
788 result
= (*quote_func
) (fname
, 0);
791 result
= (*quote_func
) (extension (fname
), 0);
799 cwd
= vfs_path_to_str (panel
->cwd_vpath
);
801 cwd
= vfs_get_current_dir ();
803 qstr
= (*quote_func
) (cwd
, 0);
810 case 'i': /* indent equal number cursor position in line */
811 #ifdef USE_INTERNAL_EDIT
814 result
= g_strnfill (edit_get_curs_col (edit_widget
), ' ');
819 case 'y': /* syntax type */
820 #ifdef USE_INTERNAL_EDIT
823 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
824 if (syntax_type
!= NULL
)
826 result
= g_strdup (syntax_type
);
832 case 'k': /* block file name */
833 case 'b': /* block file name / strip extension */
835 #ifdef USE_INTERNAL_EDIT
840 file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
841 result
= (*quote_func
) (file
, 0);
848 result
= strip_ext ((*quote_func
) (fname
, 0));
853 case 'n': /* strip extension in editor */
854 #ifdef USE_INTERNAL_EDIT
857 result
= strip_ext ((*quote_func
) (fname
, 0));
862 case 'm': /* menu file name */
865 result
= (*quote_func
) (menu
, 0);
870 if (!panel
|| !panel
->marked
)
872 result
= (*quote_func
) (fname
, 0);
886 result
= g_strdup ("");
890 block
= g_string_sized_new (16);
892 for (i
= 0; i
< panel
->count
; i
++)
893 if (panel
->dir
.list
[i
].f
.marked
)
897 tmp
= (*quote_func
) (panel
->dir
.list
[i
].fname
, 0);
898 g_string_append (block
, tmp
);
899 g_string_append_c (block
, ' ');
903 do_file_mark (panel
, i
, 0);
905 result
= g_string_free (block
, FALSE
);
907 } /* sub case block */
909 result
= g_strdup ("% ");
916 /* --------------------------------------------------------------------------------------------- */
918 * If edit_widget is NULL then we are called from the mc menu,
919 * otherwise we are called from the mcedit menu.
923 user_menu_cmd (struct WEdit
* edit_widget
, const char *menu_file
, int selected_entry
)
926 char *data
, **entries
;
927 int max_cols
, menu_lines
, menu_limit
;
928 int col
, i
, accept_entry
= 1;
929 int selected
, old_patterns
;
931 gboolean res
= FALSE
;
932 gboolean interactive
= TRUE
;
934 if (!vfs_current_is_local ())
936 message (D_ERROR
, MSG_ERROR
, "%s", _("Cannot execute commands on non-local filesystems"));
939 if (menu_file
!= NULL
)
940 menu
= g_strdup (menu_file
);
942 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
943 if (!exist_file (menu
) || !menu_file_own (menu
))
945 if (menu_file
!= NULL
)
947 message (D_ERROR
, MSG_ERROR
, _("Cannot open file %s\n%s"), menu
,
948 unix_error_string (errno
));
956 menu
= mc_config_get_full_path (EDIT_HOME_MENU
);
958 menu
= mc_config_get_full_path (MC_USERMENU_FILE
);
961 if (!exist_file (menu
))
965 mc_build_filename (mc_config_get_home_dir (),
966 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
, NULL
);
967 if (!exist_file (menu
))
971 mc_build_filename (mc_global
.sysconfig_dir
,
972 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
, NULL
);
973 if (!exist_file (menu
))
976 menu
= mc_build_filename
977 (mc_global
.share_data_dir
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
,
984 if (!g_file_get_contents (menu
, &data
, NULL
, NULL
))
986 message (D_ERROR
, MSG_ERROR
, _("Cannot open file%s\n%s"), menu
, unix_error_string (errno
));
997 /* Parse the menu file */
998 old_patterns
= easy_patterns
;
999 p
= check_patterns (data
);
1000 for (menu_lines
= col
= 0; *p
; str_next_char (&p
))
1002 if (menu_lines
>= menu_limit
)
1006 menu_limit
+= MAX_ENTRIES
;
1007 new_entries
= g_try_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
1009 if (new_entries
== NULL
)
1012 entries
= new_entries
;
1013 new_entries
+= menu_limit
;
1014 while (--new_entries
>= &entries
[menu_lines
])
1015 *new_entries
= NULL
;
1017 if (col
== 0 && !entries
[menu_lines
])
1021 /* show prompt if first line of external script is #interactive */
1022 if (selected_entry
>= 0 && strncmp (p
, "#silent", 7) == 0)
1023 interactive
= FALSE
;
1024 /* A commented menu entry */
1029 if (*(p
+ 1) == '=')
1031 /* Combined adding and default */
1032 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
1033 if (selected
== 0 && accept_entry
)
1034 selected
= menu_lines
;
1038 /* A condition for adding the entry */
1039 p
= test_line (edit_widget
, p
, &accept_entry
);
1044 if (*(p
+ 1) == '+')
1046 /* Combined adding and default */
1047 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
1048 if (selected
== 0 && accept_entry
)
1049 selected
= menu_lines
;
1053 /* A condition for making the entry default */
1055 p
= test_line (edit_widget
, p
, &i
);
1056 if (selected
== 0 && i
)
1057 selected
= menu_lines
;
1060 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
))
1062 /* A menu entry title line */
1064 entries
[menu_lines
] = p
;
1071 if (entries
[menu_lines
])
1076 max_cols
= max (max_cols
, col
);
1087 if (menu_lines
== 0)
1089 message (D_ERROR
, MSG_ERROR
, _("No suitable entries found in %s"), menu
);
1094 if (selected_entry
>= 0)
1095 selected
= selected_entry
;
1098 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
1100 /* Create listbox */
1101 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2, _("User menu"),
1102 "[Menu File Edit]");
1103 /* insert all the items found */
1104 for (i
= 0; i
< menu_lines
; i
++)
1107 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
1108 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
1110 /* Select the default entry */
1111 listbox_select_entry (listbox
->list
, selected
);
1113 selected
= run_listbox (listbox
);
1117 execute_menu_command (edit_widget
, entries
[selected
], interactive
);
1124 easy_patterns
= old_patterns
;
1132 /* --------------------------------------------------------------------------------------------- */