2 User Menu implementation
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: user menu implementation
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
43 #include "lib/search.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
50 #include "src/viewer/mcviewer.h" /* for default_* externs */
52 #include "src/execute.h"
53 #include "src/setup.h"
54 #include "src/history.h"
62 /*** global variables ****************************************************************************/
64 /*** file scope macro definitions ****************************************************************/
66 #define MAX_ENTRIES 16
67 #define MAX_ENTRY_LEN 60
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 static int debug_flag
= 0;
74 static int debug_error
= 0;
75 static char *menu
= NULL
;
77 /*** file scope functions ************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
80 /** strip file's extension */
84 register char *s
= ss
;
90 if (*s
== PATH_SEP
&& e
)
91 e
= NULL
; /* '.' in *directory* name */
99 /* --------------------------------------------------------------------------------------------- */
101 * Check for the "shell_patterns" directive. If it's found and valid,
102 * interpret it and move the pointer past the directive. Return the
107 check_patterns (char *p
)
109 static const char def_name
[] = "shell_patterns=";
112 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
115 p
+= sizeof (def_name
) - 1;
125 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
130 /* --------------------------------------------------------------------------------------------- */
131 /** Copies a whitespace separated argument from p to arg. Returns the
132 point after argument. */
135 extract_arg (char *p
, char *arg
, int size
)
139 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
141 /* support quote space .mnu */
142 while (*p
&& (*p
!= ' ' || *(p
- 1) == '\\') && *p
!= '\t' && *p
!= '\n')
144 np
= str_get_next_char (p
);
147 memcpy (arg
, p
, np
- p
);
153 if (!*p
|| *p
== '\n')
158 /* --------------------------------------------------------------------------------------------- */
159 /* Tests whether the selected file in the panel is of any of the types
160 specified in argument. */
163 test_type (WPanel
* panel
, char *arg
)
165 int result
= 0; /* False by default */
166 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
168 for (; *arg
!= 0; arg
++)
172 case 'n': /* Not a directory */
173 result
|= !S_ISDIR (st_mode
);
175 case 'r': /* Regular file */
176 result
|= S_ISREG (st_mode
);
178 case 'd': /* Directory */
179 result
|= S_ISDIR (st_mode
);
182 result
|= S_ISLNK (st_mode
);
184 case 'c': /* Character special */
185 result
|= S_ISCHR (st_mode
);
187 case 'b': /* Block special */
188 result
|= S_ISBLK (st_mode
);
190 case 'f': /* Fifo (named pipe) */
191 result
|= S_ISFIFO (st_mode
);
193 case 's': /* Socket */
194 result
|= S_ISSOCK (st_mode
);
196 case 'x': /* Executable */
197 result
|= (st_mode
& 0111) ? 1 : 0;
200 result
|= panel
->marked
? 1 : 0;
210 /* --------------------------------------------------------------------------------------------- */
211 /** Calculates the truth value of the next condition starting from
212 p. Returns the point after condition. */
215 test_condition (WEdit
* edit_widget
, char *p
, int *condition
)
218 const mc_search_type_t search_type
= easy_patterns
? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
220 /* Handle one condition */
221 for (; *p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++)
223 WPanel
*panel
= NULL
;
225 /* support quote space .mnu */
226 if ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
229 panel
= current_panel
;
230 else if (get_other_type () == view_listing
)
238 p
= test_condition (edit_widget
, p
, condition
);
239 *condition
= !*condition
;
242 case 'f': /* file name pattern */
243 p
= extract_arg (p
, arg
, sizeof (arg
));
244 #ifdef USE_INTERNAL_EDIT
245 if (edit_widget
!= NULL
)
249 edit_filename
= edit_get_file_name (edit_widget
);
250 *condition
= mc_search (arg
, DEFAULT_CHARSET
, edit_filename
, search_type
) ? 1 : 0;
251 g_free (edit_filename
);
255 *condition
= panel
!= NULL
&&
256 mc_search (arg
, DEFAULT_CHARSET
, panel
->dir
.list
[panel
->selected
].fname
,
257 search_type
) ? 1 : 0;
259 case 'y': /* syntax pattern */
260 #ifdef USE_INTERNAL_EDIT
261 if (edit_widget
!= NULL
)
263 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
264 if (syntax_type
!= NULL
)
266 p
= extract_arg (p
, arg
, sizeof (arg
));
268 mc_search (arg
, DEFAULT_CHARSET
, syntax_type
, MC_SEARCH_T_NORMAL
) ? 1 : 0;
274 p
= extract_arg (p
, arg
, sizeof (arg
));
275 *condition
= panel
!= NULL
276 && mc_search (arg
, DEFAULT_CHARSET
, vfs_path_as_str (panel
->cwd_vpath
),
277 search_type
) ? 1 : 0;
280 p
= extract_arg (p
, arg
, sizeof (arg
));
281 *condition
= panel
!= NULL
&& test_type (panel
, arg
) ? 1 : 0;
283 case 'x': /* executable */
287 p
= extract_arg (p
, arg
, sizeof (arg
));
288 if (stat (arg
, &status
) == 0)
289 *condition
= is_exe (status
.st_mode
) ? 1 : 0;
303 /* --------------------------------------------------------------------------------------------- */
304 /** General purpose condition debug output handler */
307 debug_out (char *start
, char *end
, int cond
)
311 if (start
== NULL
&& end
== NULL
)
314 if (debug_flag
&& msg
)
321 message (D_NORMAL
, _("Debug"), "%s", msg
);
333 /* Save debug info for later output */
336 /* Save the result of the condition */
346 /* This is for debugging, don't need to be super efficient. */
348 p
= g_strdup_printf ("%s %s %c \n", msg
? msg
: "", type
, *start
);
350 p
= g_strdup_printf ("%s %s %.*s \n", msg
? msg
: "", type
, (int) (end
- start
), start
);
356 /* --------------------------------------------------------------------------------------------- */
357 /** Calculates the truth value of one lineful of conditions. Returns
358 the point just before the end of line. */
361 test_line (WEdit
* edit_widget
, char *p
, int *result
)
366 /* Repeat till end of line */
367 while (*p
&& *p
!= '\n')
369 char *debug_start
, *debug_end
;
371 /* support quote space .mnu */
372 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
374 if (!*p
|| *p
== '\n')
382 /* support quote space .mnu */
383 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
385 if (!*p
|| *p
== '\n')
387 condition
= 1; /* True by default */
390 p
= test_condition (edit_widget
, p
, &condition
);
392 /* Add one debug statement */
393 debug_out (debug_start
, debug_end
, condition
);
402 case '&': /* Logical and */
403 *result
&= condition
;
405 case '|': /* Logical or */
406 *result
|= condition
;
412 /* Add one debug statement */
413 debug_out (&operator, NULL
, *result
);
415 } /* while (*p != '\n') */
416 /* Report debug message */
417 debug_out (NULL
, NULL
, 1);
419 if (!*p
|| *p
== '\n')
424 /* --------------------------------------------------------------------------------------------- */
425 /** FIXME: recode this routine on version 3.0, it could be cleaner */
428 execute_menu_command (WEdit
* edit_widget
, const char *commands
, gboolean show_prompt
)
432 int expand_prefix_found
= 0;
434 gboolean do_quote
= FALSE
;
437 vfs_path_t
*file_name_vpath
;
440 /* Skip menu entry title line */
441 commands
= strchr (commands
, '\n');
447 cmd_file_fd
= mc_mkstemps (&file_name_vpath
, "mcusr", SCRIPT_SUFFIX
);
449 if (cmd_file_fd
== -1)
451 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary command file\n%s"),
452 unix_error_string (errno
));
453 vfs_path_free (file_name_vpath
);
456 cmd_file
= fdopen (cmd_file_fd
, "w");
457 fputs ("#! /bin/sh\n", cmd_file
);
460 for (col
= 0; *commands
; commands
++)
464 if (*commands
!= ' ' && *commands
!= '\t')
466 while (*commands
== ' ' || *commands
== '\t')
472 if (*commands
== '\n')
476 if (*commands
== '}')
480 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "",
481 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
|
482 INPUT_COMPLETE_HOSTNAMES
| INPUT_COMPLETE_VARIABLES
|
483 INPUT_COMPLETE_USERNAMES
);
484 if (!parameter
|| !*parameter
)
488 mc_unlink (file_name_vpath
);
489 vfs_path_free (file_name_vpath
);
496 tmp
= name_quote (parameter
, 0);
497 fputs (tmp
, cmd_file
);
501 fputs (parameter
, cmd_file
);
507 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1])
509 *parameter
++ = *commands
;
513 else if (expand_prefix_found
)
515 expand_prefix_found
= 0;
516 if (g_ascii_isdigit ((gchar
) * commands
))
518 do_quote
= (atoi (commands
) != 0);
519 while (g_ascii_isdigit ((gchar
) * commands
))
522 if (*commands
== '{')
523 parameter
= lc_prompt
;
526 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
527 fputs (text
, cmd_file
);
533 if (*commands
== '%')
535 int i
= check_format_view (commands
+ 1);
543 do_quote
= TRUE
; /* Default: Quote expanded macro */
544 expand_prefix_found
= 1;
548 fputc (*commands
, cmd_file
);
552 mc_chmod (file_name_vpath
, S_IRWXU
);
555 mcview_viewer (vfs_path_as_str (file_name_vpath
), NULL
, 0);
556 dialog_switch_process_pending ();
560 /* execute the command indirectly to allow execution even
561 * on no-exec filesystems. */
564 cmd
= g_strconcat ("/bin/sh ", vfs_path_as_str (file_name_vpath
), (char *) NULL
);
567 if (system (cmd
) == -1)
568 message (D_ERROR
, MSG_ERROR
, "%s", _("Error calling program"));
572 shell_execute (cmd
, EXECUTE_HIDE
);
576 mc_unlink (file_name_vpath
);
577 vfs_path_free (file_name_vpath
);
580 /* --------------------------------------------------------------------------------------------- */
582 ** Check owner of the menu file. Using menu file is allowed, if
583 ** owner of the menu is root or the actual user. In either case
584 ** file should not be group and word-writable.
586 ** Q. Should we apply this routine to system and home menu (and .ext files)?
590 menu_file_own (char *path
)
594 if (stat (path
, &st
) == 0
595 && (!st
.st_uid
|| (st
.st_uid
== geteuid ())) && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0))
601 message (D_NORMAL
, _("Warning -- ignoring file"),
602 _("File %s is not owned by root or you or is world writable.\n"
603 "Using it may compromise your security"), path
);
608 /* --------------------------------------------------------------------------------------------- */
609 /*** public functions ****************************************************************************/
610 /* --------------------------------------------------------------------------------------------- */
614 %f The current file (if non-local vfs, file will be copied locally and
615 %f will be full path to it).
617 %d The current working directory
618 %s "Selected files"; the tagged files if any, otherwise the current file
620 %u Tagged files (and they are untagged on return from expand_format)
621 %view Runs the commands and pipes standard output to the view command.
622 If %view is immediately followed by '{', recognize keywords
623 ascii, hex, nroff and unform
625 If the format letter is in uppercase, it refers to the other panel.
627 With a number followed the % character you can turn quoting on (default)
628 and off. For example:
629 %f quote expanded macro
631 %0f don't quote expanded macro
633 expand_format returns a memory block that must be free()d.
636 /* Returns how many characters we should advance if %view was found */
638 check_format_view (const char *p
)
641 if (!strncmp (p
, "view", 4))
646 for (q
++; *q
&& *q
!= '}'; q
++)
648 if (!strncmp (q
, DEFAULT_CHARSET
, 5))
650 mcview_default_hex_mode
= 0;
653 else if (!strncmp (q
, "hex", 3))
655 mcview_default_hex_mode
= 1;
658 else if (!strncmp (q
, "nroff", 5))
660 mcview_default_nroff_flag
= 1;
663 else if (!strncmp (q
, "unform", 6))
665 mcview_default_nroff_flag
= 0;
677 /* --------------------------------------------------------------------------------------------- */
680 check_format_cd (const char *p
)
682 return (strncmp (p
, "cd", 2)) ? 0 : 3;
685 /* --------------------------------------------------------------------------------------------- */
686 /* Check if p has a "^var\{var-name\}" */
687 /* Returns the number of skipped characters (zero on not found) */
688 /* V will be set to the expanded variable name */
691 check_format_var (const char *p
, char **v
)
697 if (!strncmp (p
, "var{", 4))
699 const char *dots
= NULL
;
702 for (q
+= 4; *q
&& *q
!= '}'; q
++)
710 if (!dots
|| dots
== q
+ 5)
713 _("Format error on file Extensions File"),
714 !dots
? _("The %%var macro has no default")
715 : _("The %%var macro has no variable"));
719 /* Copy the variable name */
720 var_name
= g_strndup (p
+ 4, dots
- 2 - (p
+ 3));
722 value
= getenv (var_name
);
726 *v
= g_strdup (value
);
729 var_name
= g_strndup (dots
, q
- dots
);
736 /* --------------------------------------------------------------------------------------------- */
739 expand_format (struct WEdit
*edit_widget
, char c
, gboolean do_quote
)
741 WPanel
*panel
= NULL
;
742 char *(*quote_func
) (const char *, int);
747 #ifndef USE_INTERNAL_EDIT
752 return g_strdup ("%");
754 switch (mc_global
.mc_run_mode
)
757 if (g_ascii_islower ((gchar
) c
))
758 panel
= current_panel
;
761 if (get_other_type () != view_listing
)
762 return g_strdup ("");
765 fname
= g_strdup (panel
->dir
.list
[panel
->selected
].fname
);
768 #ifdef USE_INTERNAL_EDIT
770 fname
= edit_get_file_name (edit_widget
);
775 /* other modes don't use formats */
776 return g_strdup ("");
780 quote_func
= name_quote
;
782 quote_func
= fake_name_quote
;
784 c_lc
= g_ascii_tolower ((gchar
) c
);
790 result
= (*quote_func
) (fname
, 0);
793 result
= (*quote_func
) (extension (fname
), 0);
801 cwd
= g_strdup (vfs_path_as_str (panel
->cwd_vpath
));
803 cwd
= vfs_get_current_dir ();
805 qstr
= (*quote_func
) (cwd
, 0);
812 case 'i': /* indent equal number cursor position in line */
813 #ifdef USE_INTERNAL_EDIT
816 result
= g_strnfill (edit_get_curs_col (edit_widget
), ' ');
821 case 'y': /* syntax type */
822 #ifdef USE_INTERNAL_EDIT
825 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
826 if (syntax_type
!= NULL
)
828 result
= g_strdup (syntax_type
);
834 case 'k': /* block file name */
835 case 'b': /* block file name / strip extension */
837 #ifdef USE_INTERNAL_EDIT
842 file
= mc_config_get_full_path (EDIT_BLOCK_FILE
);
843 result
= (*quote_func
) (file
, 0);
850 result
= strip_ext ((*quote_func
) (fname
, 0));
855 case 'n': /* strip extension in editor */
856 #ifdef USE_INTERNAL_EDIT
859 result
= strip_ext ((*quote_func
) (fname
, 0));
864 case 'm': /* menu file name */
867 result
= (*quote_func
) (menu
, 0);
872 if (!panel
|| !panel
->marked
)
874 result
= (*quote_func
) (fname
, 0);
888 result
= g_strdup ("");
892 block
= g_string_sized_new (16);
894 for (i
= 0; i
< panel
->dir
.len
; i
++)
895 if (panel
->dir
.list
[i
].f
.marked
)
899 tmp
= (*quote_func
) (panel
->dir
.list
[i
].fname
, 0);
900 g_string_append (block
, tmp
);
901 g_string_append_c (block
, ' ');
905 do_file_mark (panel
, i
, 0);
907 result
= g_string_free (block
, FALSE
);
909 } /* sub case block */
911 result
= g_strdup ("% ");
918 /* --------------------------------------------------------------------------------------------- */
920 * If edit_widget is NULL then we are called from the mc menu,
921 * otherwise we are called from the mcedit menu.
925 user_menu_cmd (struct WEdit
* edit_widget
, const char *menu_file
, int selected_entry
)
928 char *data
, **entries
;
929 int max_cols
, menu_lines
, menu_limit
;
930 int col
, i
, accept_entry
= 1;
931 int selected
, old_patterns
;
932 gboolean res
= FALSE
;
933 gboolean interactive
= TRUE
;
935 if (!vfs_current_is_local ())
937 message (D_ERROR
, MSG_ERROR
, "%s", _("Cannot execute commands on non-local filesystems"));
940 if (menu_file
!= NULL
)
941 menu
= g_strdup (menu_file
);
943 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
944 if (!exist_file (menu
) || !menu_file_own (menu
))
946 if (menu_file
!= NULL
)
948 message (D_ERROR
, MSG_ERROR
, _("Cannot open file %s\n%s"), menu
,
949 unix_error_string (errno
));
957 menu
= mc_config_get_full_path (EDIT_HOME_MENU
);
959 menu
= mc_config_get_full_path (MC_USERMENU_FILE
);
962 if (!exist_file (menu
))
966 mc_build_filename (mc_config_get_home_dir (),
967 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
, NULL
);
968 if (!exist_file (menu
))
972 mc_build_filename (mc_global
.sysconfig_dir
,
973 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
, NULL
);
974 if (!exist_file (menu
))
977 menu
= mc_build_filename
978 (mc_global
.share_data_dir
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
,
985 if (!g_file_get_contents (menu
, &data
, NULL
, NULL
))
987 message (D_ERROR
, MSG_ERROR
, _("Cannot open file%s\n%s"), menu
, unix_error_string (errno
));
998 /* Parse the menu file */
999 old_patterns
= easy_patterns
;
1000 p
= check_patterns (data
);
1001 for (menu_lines
= col
= 0; *p
; str_next_char (&p
))
1003 if (menu_lines
>= menu_limit
)
1007 menu_limit
+= MAX_ENTRIES
;
1008 new_entries
= g_try_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
1010 if (new_entries
== NULL
)
1013 entries
= new_entries
;
1014 new_entries
+= menu_limit
;
1015 while (--new_entries
>= &entries
[menu_lines
])
1016 *new_entries
= NULL
;
1018 if (col
== 0 && !entries
[menu_lines
])
1022 /* show prompt if first line of external script is #interactive */
1023 if (selected_entry
>= 0 && strncmp (p
, "#silent", 7) == 0)
1024 interactive
= FALSE
;
1025 /* A commented menu entry */
1030 if (*(p
+ 1) == '=')
1032 /* Combined adding and default */
1033 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
1034 if (selected
== 0 && accept_entry
)
1035 selected
= menu_lines
;
1039 /* A condition for adding the entry */
1040 p
= test_line (edit_widget
, p
, &accept_entry
);
1045 if (*(p
+ 1) == '+')
1047 /* Combined adding and default */
1048 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
1049 if (selected
== 0 && accept_entry
)
1050 selected
= menu_lines
;
1054 /* A condition for making the entry default */
1056 p
= test_line (edit_widget
, p
, &i
);
1057 if (selected
== 0 && i
)
1058 selected
= menu_lines
;
1061 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
))
1063 /* A menu entry title line */
1065 entries
[menu_lines
] = p
;
1072 if (entries
[menu_lines
])
1077 max_cols
= max (max_cols
, col
);
1088 if (menu_lines
== 0)
1090 message (D_ERROR
, MSG_ERROR
, _("No suitable entries found in %s"), menu
);
1095 if (selected_entry
>= 0)
1096 selected
= selected_entry
;
1101 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
1103 /* Create listbox */
1104 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2, _("User menu"),
1105 "[Menu File Edit]");
1106 /* insert all the items found */
1107 for (i
= 0; i
< menu_lines
; i
++)
1110 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
1111 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
1113 /* Select the default entry */
1114 listbox_select_entry (listbox
->list
, selected
);
1116 selected
= run_listbox (listbox
);
1120 execute_menu_command (edit_widget
, entries
[selected
], interactive
);
1127 easy_patterns
= old_patterns
;
1135 /* --------------------------------------------------------------------------------------------- */