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
31 #include "lib/global.h"
32 #include "lib/tty/tty.h"
34 #include "lib/search.h"
35 #include "lib/vfs/mc-vfs/vfs.h"
36 #include "lib/strutil.h"
38 #include "lib/widget.h"
40 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
41 #include "src/viewer/mcviewer.h" /* for default_* externs */
43 #include "src/execute.h"
44 #include "src/setup.h"
45 #include "src/history.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define MAX_ENTRIES 16
58 #define MAX_ENTRY_LEN 60
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 static int debug_flag
= 0;
65 static int debug_error
= 0;
66 static char *menu
= NULL
;
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 /** strip file's extension */
75 register char *s
= ss
;
81 if (*s
== PATH_SEP
&& e
)
82 e
= NULL
; /* '.' in *directory* name */
90 /* --------------------------------------------------------------------------------------------- */
92 * Check for the "shell_patterns" directive. If it's found and valid,
93 * interpret it and move the pointer past the directive. Return the
98 check_patterns (char *p
)
100 static const char def_name
[] = "shell_patterns=";
103 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
106 p
+= sizeof (def_name
) - 1;
116 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
121 /* --------------------------------------------------------------------------------------------- */
122 /** Copies a whitespace separated argument from p to arg. Returns the
123 point after argument. */
126 extract_arg (char *p
, char *arg
, int size
)
130 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
132 /* support quote space .mnu */
133 while (*p
&& (*p
!= ' ' || *(p
- 1) == '\\') && *p
!= '\t' && *p
!= '\n')
135 np
= str_get_next_char (p
);
138 memcpy (arg
, p
, np
- p
);
144 if (!*p
|| *p
== '\n')
149 /* --------------------------------------------------------------------------------------------- */
150 /* Tests whether the selected file in the panel is of any of the types
151 specified in argument. */
154 test_type (WPanel
* panel
, char *arg
)
156 int result
= 0; /* False by default */
157 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
159 for (; *arg
!= 0; arg
++)
163 case 'n': /* Not a directory */
164 result
|= !S_ISDIR (st_mode
);
166 case 'r': /* Regular file */
167 result
|= S_ISREG (st_mode
);
169 case 'd': /* Directory */
170 result
|= S_ISDIR (st_mode
);
173 result
|= S_ISLNK (st_mode
);
175 case 'c': /* Character special */
176 result
|= S_ISCHR (st_mode
);
178 case 'b': /* Block special */
179 result
|= S_ISBLK (st_mode
);
181 case 'f': /* Fifo (named pipe) */
182 result
|= S_ISFIFO (st_mode
);
184 case 's': /* Socket */
185 result
|= S_ISSOCK (st_mode
);
187 case 'x': /* Executable */
188 result
|= (st_mode
& 0111) ? 1 : 0;
191 result
|= panel
->marked
? 1 : 0;
201 /* --------------------------------------------------------------------------------------------- */
202 /** Calculates the truth value of the next condition starting from
203 p. Returns the point after condition. */
206 test_condition (WEdit
* edit_widget
, char *p
, int *condition
)
209 const mc_search_type_t search_type
= easy_patterns
? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
211 /* Handle one condition */
212 for (; *p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++)
214 WPanel
*panel
= NULL
;
216 /* support quote space .mnu */
217 if ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
220 panel
= current_panel
;
221 else if (get_other_type () == view_listing
)
229 p
= test_condition (edit_widget
, p
, condition
);
230 *condition
= !*condition
;
233 case 'f': /* file name pattern */
234 p
= extract_arg (p
, arg
, sizeof (arg
));
235 #ifdef USE_INTERNAL_EDIT
236 if (edit_widget
!= NULL
)
237 *condition
= mc_search (arg
, edit_get_file_name (edit_widget
), search_type
) ? 1 : 0;
240 *condition
= panel
!= NULL
&&
241 mc_search (arg
, panel
->dir
.list
[panel
->selected
].fname
, search_type
) ? 1 : 0;
243 case 'y': /* syntax pattern */
244 #ifdef USE_INTERNAL_EDIT
245 if (edit_widget
!= NULL
)
247 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
248 if (syntax_type
!= NULL
)
250 p
= extract_arg (p
, arg
, sizeof (arg
));
251 *condition
= mc_search (arg
, syntax_type
, MC_SEARCH_T_NORMAL
) ? 1 : 0;
257 p
= extract_arg (p
, arg
, sizeof (arg
));
258 *condition
= panel
!= NULL
&& mc_search (arg
, panel
->cwd
, search_type
) ? 1 : 0;
261 p
= extract_arg (p
, arg
, sizeof (arg
));
262 *condition
= panel
!= NULL
&& test_type (panel
, arg
) ? 1 : 0;
264 case 'x': /* executable */
268 p
= extract_arg (p
, arg
, sizeof (arg
));
269 if (stat (arg
, &status
) == 0)
270 *condition
= is_exe (status
.st_mode
) ? 1 : 0;
284 /* --------------------------------------------------------------------------------------------- */
285 /** General purpose condition debug output handler */
288 debug_out (char *start
, char *end
, int cond
)
293 if (start
== NULL
&& end
== NULL
)
296 if (debug_flag
&& msg
)
301 message (D_NORMAL
, _("Debug"), "%s", msg
);
313 /* Save debug info for later output */
316 /* Save the result of the condition */
326 /* This is for debugging, don't need to be super efficient. */
328 p
= g_strdup_printf ("%s %s %c \n", msg
? msg
: "", type
, *start
);
330 p
= g_strdup_printf ("%s %s %.*s \n", msg
? msg
: "", type
, (int) (end
- start
), start
);
336 /* --------------------------------------------------------------------------------------------- */
337 /** Calculates the truth value of one lineful of conditions. Returns
338 the point just before the end of line. */
341 test_line (WEdit
* edit_widget
, char *p
, int *result
)
345 char *debug_start
, *debug_end
;
347 /* Repeat till end of line */
348 while (*p
&& *p
!= '\n')
350 /* support quote space .mnu */
351 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
353 if (!*p
|| *p
== '\n')
361 /* support quote space .mnu */
362 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
364 if (!*p
|| *p
== '\n')
366 condition
= 1; /* True by default */
369 p
= test_condition (edit_widget
, p
, &condition
);
371 /* Add one debug statement */
372 debug_out (debug_start
, debug_end
, condition
);
381 case '&': /* Logical and */
382 *result
&= condition
;
384 case '|': /* Logical or */
385 *result
|= condition
;
391 /* Add one debug statement */
392 debug_out (&operator, NULL
, *result
);
394 } /* while (*p != '\n') */
395 /* Report debug message */
396 debug_out (NULL
, NULL
, 1);
398 if (!*p
|| *p
== '\n')
403 /* --------------------------------------------------------------------------------------------- */
404 /** FIXME: recode this routine on version 3.0, it could be cleaner */
407 execute_menu_command (WEdit
* edit_widget
, const char *commands
, gboolean show_prompt
)
411 int expand_prefix_found
= 0;
413 gboolean do_quote
= FALSE
;
419 /* Skip menu entry title line */
420 commands
= strchr (commands
, '\n');
426 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
428 if (cmd_file_fd
== -1)
430 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary command file\n%s"),
431 unix_error_string (errno
));
434 cmd_file
= fdopen (cmd_file_fd
, "w");
435 fputs ("#! /bin/sh\n", cmd_file
);
438 for (col
= 0; *commands
; commands
++)
442 if (*commands
!= ' ' && *commands
!= '\t')
444 while (*commands
== ' ' || *commands
== '\t')
450 if (*commands
== '\n')
454 if (*commands
== '}')
459 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "");
460 if (!parameter
|| !*parameter
)
470 tmp
= name_quote (parameter
, 0);
471 fputs (tmp
, cmd_file
);
475 fputs (parameter
, cmd_file
);
481 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1])
483 *parameter
++ = *commands
;
487 else if (expand_prefix_found
)
489 expand_prefix_found
= 0;
490 if (g_ascii_isdigit ((gchar
) * commands
))
492 do_quote
= (atoi (commands
) != 0);
493 while (g_ascii_isdigit ((gchar
) * commands
))
496 if (*commands
== '{')
497 parameter
= lc_prompt
;
500 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
501 fputs (text
, cmd_file
);
507 if (*commands
== '%')
509 int i
= check_format_view (commands
+ 1);
517 do_quote
= TRUE
; /* Default: Quote expanded macro */
518 expand_prefix_found
= 1;
522 fputc (*commands
, cmd_file
);
526 chmod (file_name
, S_IRWXU
);
529 mcview_viewer (file_name
, NULL
, 0);
530 dialog_switch_process_pending ();
534 /* execute the command indirectly to allow execution even
535 * on no-exec filesystems. */
536 char *cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
539 if (system (cmd
) == -1)
540 message (D_ERROR
, MSG_ERROR
, "%s", _("Error calling program"));
544 shell_execute (cmd
, EXECUTE_HIDE
);
552 /* --------------------------------------------------------------------------------------------- */
554 ** Check owner of the menu file. Using menu file is allowed, if
555 ** owner of the menu is root or the actual user. In either case
556 ** file should not be group and word-writable.
558 ** Q. Should we apply this routine to system and home menu (and .ext files)?
562 menu_file_own (char *path
)
566 if (stat (path
, &st
) == 0
567 && (!st
.st_uid
|| (st
.st_uid
== geteuid ())) && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0))
573 message (D_NORMAL
, _("Warning -- ignoring file"),
574 _("File %s is not owned by root or you or is world writable.\n"
575 "Using it may compromise your security"), path
);
580 /* --------------------------------------------------------------------------------------------- */
581 /*** public functions ****************************************************************************/
582 /* --------------------------------------------------------------------------------------------- */
586 %f The current file (if non-local vfs, file will be copied locally and
587 %f will be full path to it).
589 %d The current working directory
590 %s "Selected files"; the tagged files if any, otherwise the current file
592 %u Tagged files (and they are untagged on return from expand_format)
593 %view Runs the commands and pipes standard output to the view command.
594 If %view is immediately followed by '{', recognize keywords
595 ascii, hex, nroff and unform
597 If the format letter is in uppercase, it refers to the other panel.
599 With a number followed the % character you can turn quoting on (default)
600 and off. For example:
601 %f quote expanded macro
603 %0f don't quote expanded macro
605 expand_format returns a memory block that must be free()d.
608 /* Returns how many characters we should advance if %view was found */
610 check_format_view (const char *p
)
613 if (!strncmp (p
, "view", 4))
618 for (q
++; *q
&& *q
!= '}'; q
++)
620 if (!strncmp (q
, "ascii", 5))
622 mcview_default_hex_mode
= 0;
625 else if (!strncmp (q
, "hex", 3))
627 mcview_default_hex_mode
= 1;
630 else if (!strncmp (q
, "nroff", 5))
632 mcview_default_nroff_flag
= 1;
635 else if (!strncmp (q
, "unform", 6))
637 mcview_default_nroff_flag
= 0;
649 /* --------------------------------------------------------------------------------------------- */
652 check_format_cd (const char *p
)
654 return (strncmp (p
, "cd", 2)) ? 0 : 3;
657 /* --------------------------------------------------------------------------------------------- */
658 /* Check if p has a "^var\{var-name\}" */
659 /* Returns the number of skipped characters (zero on not found) */
660 /* V will be set to the expanded variable name */
663 check_format_var (const char *p
, char **v
)
668 const char *dots
= 0;
671 if (!strncmp (p
, "var{", 4))
673 for (q
+= 4; *q
&& *q
!= '}'; q
++)
681 if (!dots
|| dots
== q
+ 5)
684 _("Format error on file Extensions File"),
685 !dots
? _("The %%var macro has no default")
686 : _("The %%var macro has no variable"));
690 /* Copy the variable name */
691 var_name
= g_strndup (p
+ 4, dots
- 2 - (p
+ 3));
693 value
= getenv (var_name
);
697 *v
= g_strdup (value
);
700 var_name
= g_strndup (dots
, q
- dots
);
707 /* --------------------------------------------------------------------------------------------- */
710 expand_format (struct WEdit
*edit_widget
, char c
, gboolean do_quote
)
712 WPanel
*panel
= NULL
;
713 char *(*quote_func
) (const char *, int);
718 #ifndef USE_INTERNAL_EDIT
723 return g_strdup ("%");
725 if (mc_run_mode
== MC_RUN_FULL
)
727 if (g_ascii_islower ((gchar
) c
))
728 panel
= current_panel
;
731 if (get_other_type () != view_listing
)
732 return g_strdup ("");
735 fname
= panel
->dir
.list
[panel
->selected
].fname
;
737 #ifdef USE_INTERNAL_EDIT
738 else if (mc_run_mode
== MC_RUN_EDITOR
)
739 fname
= (char *) edit_get_file_name (edit_widget
);
743 quote_func
= name_quote
;
745 quote_func
= fake_name_quote
;
747 c_lc
= g_ascii_tolower ((gchar
) c
);
753 return (*quote_func
) (fname
, 0);
755 return (*quote_func
) (extension (fname
), 0);
761 cwd
= g_malloc (MC_MAXPATHLEN
+ 1);
764 g_strlcpy (cwd
, panel
->cwd
, MC_MAXPATHLEN
+ 1);
766 mc_get_current_wd (cwd
, MC_MAXPATHLEN
+ 1);
768 qstr
= (*quote_func
) (cwd
, 0);
774 case 'i': /* indent equal number cursor position in line */
775 #ifdef USE_INTERNAL_EDIT
777 return g_strnfill (edit_get_curs_col (edit_widget
), ' ');
780 case 'y': /* syntax type */
781 #ifdef USE_INTERNAL_EDIT
784 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
785 if (syntax_type
!= NULL
)
786 return g_strdup (syntax_type
);
790 case 'k': /* block file name */
791 case 'b': /* block file name / strip extension */
793 #ifdef USE_INTERNAL_EDIT
796 char *file
= concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE
);
797 fname
= (*quote_func
) (file
, 0);
803 return strip_ext ((*quote_func
) (fname
, 0));
806 case 'n': /* strip extension in editor */
807 #ifdef USE_INTERNAL_EDIT
809 return strip_ext ((*quote_func
) (fname
, 0));
812 case 'm': /* menu file name */
814 return (*quote_func
) (menu
, 0);
817 if (!panel
|| !panel
->marked
)
818 return (*quote_func
) (fname
, 0);
829 return g_strdup ("");
831 for (i
= 0; i
< panel
->count
; i
++)
832 if (panel
->dir
.list
[i
].f
.marked
)
833 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
835 block
= g_malloc (length
* 2 + 1);
837 for (i
= 0; i
< panel
->count
; i
++)
838 if (panel
->dir
.list
[i
].f
.marked
)
840 tmp
= (*quote_func
) (panel
->dir
.list
[i
].fname
, 0);
845 do_file_mark (panel
, i
, 0);
848 } /* sub case block */
850 result
= g_strdup ("% ");
855 /* --------------------------------------------------------------------------------------------- */
857 * If edit_widget is NULL then we are called from the mc menu,
858 * otherwise we are called from the mcedit menu.
862 user_menu_cmd (struct WEdit
*edit_widget
, const char *menu_file
, int selected_entry
)
865 char *data
, **entries
;
866 int max_cols
, menu_lines
, menu_limit
;
867 int col
, i
, accept_entry
= 1;
868 int selected
, old_patterns
;
870 gboolean res
= FALSE
;
871 gboolean interactive
= FALSE
;
873 if (!vfs_current_is_local ())
875 message (D_ERROR
, MSG_ERROR
, "%s", _("Cannot execute commands on non-local filesystems"));
878 if (menu_file
!= NULL
)
879 menu
= g_strdup (menu_file
);
881 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
882 if (!exist_file (menu
) || !menu_file_own (menu
))
884 if (menu_file
!= NULL
)
886 message (D_ERROR
, MSG_ERROR
, _("Cannot open file%s\n%s"), menu
, unix_error_string (errno
));
894 menu
= concat_dir_and_file (mc_config_get_data_path (), EDIT_HOME_MENU
);
896 menu
= g_build_filename (mc_config_get_data_path (), MC_USERMENU_FILE
, NULL
);
899 if (!exist_file (menu
))
903 concat_dir_and_file (mc_config_get_home_dir (),
904 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
905 if (!exist_file (menu
))
909 concat_dir_and_file (mc_sysconfig_dir
,
910 edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
911 if (!exist_file (menu
))
914 menu
= concat_dir_and_file
915 (mc_share_data_dir
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
921 if (!g_file_get_contents (menu
, &data
, NULL
, NULL
))
923 message (D_ERROR
, MSG_ERROR
, _("Cannot open file%s\n%s"), menu
, unix_error_string (errno
));
934 /* Parse the menu file */
935 old_patterns
= easy_patterns
;
936 p
= check_patterns (data
);
937 for (menu_lines
= col
= 0; *p
; str_next_char (&p
))
939 if (menu_lines
>= menu_limit
)
943 menu_limit
+= MAX_ENTRIES
;
944 new_entries
= g_try_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
946 if (new_entries
== NULL
)
949 entries
= new_entries
;
950 new_entries
+= menu_limit
;
951 while (--new_entries
>= &entries
[menu_lines
])
954 if (col
== 0 && !entries
[menu_lines
])
958 /* show prompt if first line of external script is #interactive */
959 if (selected_entry
>= 0 && strncmp (p
, "#interactive", 12) == 0)
961 /* A commented menu entry */
968 /* Combined adding and default */
969 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
970 if (selected
== 0 && accept_entry
)
971 selected
= menu_lines
;
975 /* A condition for adding the entry */
976 p
= test_line (edit_widget
, p
, &accept_entry
);
983 /* Combined adding and default */
984 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
985 if (selected
== 0 && accept_entry
)
986 selected
= menu_lines
;
990 /* A condition for making the entry default */
992 p
= test_line (edit_widget
, p
, &i
);
993 if (selected
== 0 && i
)
994 selected
= menu_lines
;
997 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
))
999 /* A menu entry title line */
1001 entries
[menu_lines
] = p
;
1008 if (entries
[menu_lines
])
1013 max_cols
= max (max_cols
, col
);
1024 if (menu_lines
== 0)
1026 message (D_ERROR
, MSG_ERROR
, _("No suitable entries found in %s"), menu
);
1031 if (selected_entry
>= 0)
1032 selected
= selected_entry
;
1035 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
1037 /* Create listbox */
1038 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2, _("User menu"),
1039 "[Menu File Edit]");
1040 /* insert all the items found */
1041 for (i
= 0; i
< menu_lines
; i
++)
1044 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
1045 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
1047 /* Select the default entry */
1048 listbox_select_entry (listbox
->list
, selected
);
1050 selected
= run_listbox (listbox
);
1054 execute_menu_command (edit_widget
, entries
[selected
], interactive
);
1061 easy_patterns
= old_patterns
;
1069 /* --------------------------------------------------------------------------------------------- */