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 "src/editor/edit.h" /* WEdit, BLOCK_FILE */
39 #include "src/viewer/mcviewer.h" /* for default_* externs */
51 #include "dialog-switch.h"
55 #define MAX_ENTRIES 16
56 #define MAX_ENTRY_LEN 60
58 static int debug_flag
= 0;
59 static int debug_error
= 0;
60 static char *menu
= NULL
;
64 %f The current file (if non-local vfs, file will be copied locally and
65 %f will be full path to it).
67 %d The current working directory
68 %s "Selected files"; the tagged files if any, otherwise the current file
70 %u Tagged files (and they are untagged on return from expand_format)
71 %view Runs the commands and pipes standard output to the view command.
72 If %view is immediately followed by '{', recognize keywords
73 ascii, hex, nroff and unform
75 If the format letter is in uppercase, it refers to the other panel.
77 With a number followed the % character you can turn quoting on (default)
79 %f quote expanded macro
81 %0f don't quote expanded macro
83 expand_format returns a memory block that must be free()d.
86 /* Returns how many characters we should advance if %view was found */
88 check_format_view (const char *p
)
91 if (!strncmp (p
, "view", 4))
96 for (q
++; *q
&& *q
!= '}'; q
++)
98 if (!strncmp (q
, "ascii", 5))
100 mcview_default_hex_mode
= 0;
103 else if (!strncmp (q
, "hex", 3))
105 mcview_default_hex_mode
= 1;
108 else if (!strncmp (q
, "nroff", 5))
110 mcview_default_nroff_flag
= 1;
113 else if (!strncmp (q
, "unform", 6))
115 mcview_default_nroff_flag
= 0;
128 check_format_cd (const char *p
)
130 return (strncmp (p
, "cd", 2)) ? 0 : 3;
133 /* Check if p has a "^var\{var-name\}" */
134 /* Returns the number of skipped characters (zero on not found) */
135 /* V will be set to the expanded variable name */
137 check_format_var (const char *p
, char **v
)
142 const char *dots
= 0;
145 if (!strncmp (p
, "var{", 4))
147 for (q
+= 4; *q
&& *q
!= '}'; q
++)
155 if (!dots
|| dots
== q
+ 5)
158 _("Format error on file Extensions File"),
159 !dots
? _("The %%var macro has no default")
160 : _("The %%var macro has no variable"));
164 /* Copy the variable name */
165 var_name
= g_strndup (p
+ 4, dots
- 2 - (p
+ 3));
167 value
= getenv (var_name
);
171 *v
= g_strdup (value
);
174 var_name
= g_strndup (dots
, q
- dots
);
181 /* strip file's extension */
185 register char *s
= ss
;
191 if (*s
== PATH_SEP
&& e
)
192 e
= NULL
; /* '.' in *directory* name */
201 expand_format (struct WEdit
*edit_widget
, char c
, gboolean do_quote
)
203 WPanel
*panel
= NULL
;
204 char *(*quote_func
) (const char *, int);
209 #ifndef USE_INTERNAL_EDIT
214 return g_strdup ("%");
216 if (mc_run_mode
== MC_RUN_FULL
)
218 if (g_ascii_islower ((gchar
) c
))
219 panel
= current_panel
;
222 if (get_other_type () != view_listing
)
223 return g_strdup ("");
226 fname
= panel
->dir
.list
[panel
->selected
].fname
;
228 #ifdef USE_INTERNAL_EDIT
229 else if (mc_run_mode
== MC_RUN_EDITOR
)
230 fname
= str_unconst (edit_get_file_name (edit_widget
));
234 quote_func
= name_quote
;
236 quote_func
= fake_name_quote
;
238 c_lc
= g_ascii_tolower ((gchar
) c
);
244 return (*quote_func
) (fname
, 0);
246 return (*quote_func
) (extension (fname
), 0);
252 cwd
= g_malloc (MC_MAXPATHLEN
+ 1);
255 g_strlcpy (cwd
, panel
->cwd
, MC_MAXPATHLEN
+ 1);
257 mc_get_current_wd (cwd
, MC_MAXPATHLEN
+ 1);
259 qstr
= (*quote_func
) (cwd
, 0);
265 case 'i': /* indent equal number cursor position in line */
266 #ifdef USE_INTERNAL_EDIT
268 return g_strnfill (edit_get_curs_col (edit_widget
), ' ');
271 case 'y': /* syntax type */
272 #ifdef USE_INTERNAL_EDIT
275 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
276 if (syntax_type
!= NULL
)
277 return g_strdup (syntax_type
);
281 case 'k': /* block file name */
282 case 'b': /* block file name / strip extension */
284 #ifdef USE_INTERNAL_EDIT
287 char *file
= concat_dir_and_file (home_dir
, EDIT_BLOCK_FILE
);
288 fname
= (*quote_func
) (file
, 0);
294 return strip_ext ((*quote_func
) (fname
, 0));
297 case 'n': /* strip extension in editor */
298 #ifdef USE_INTERNAL_EDIT
300 return strip_ext ((*quote_func
) (fname
, 0));
303 case 'm': /* menu file name */
305 return (*quote_func
) (menu
, 0);
308 if (!panel
|| !panel
->marked
)
309 return (*quote_func
) (fname
, 0);
320 return g_strdup ("");
322 for (i
= 0; i
< panel
->count
; i
++)
323 if (panel
->dir
.list
[i
].f
.marked
)
324 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
326 block
= g_malloc (length
* 2 + 1);
328 for (i
= 0; i
< panel
->count
; i
++)
329 if (panel
->dir
.list
[i
].f
.marked
)
331 tmp
= (*quote_func
) (panel
->dir
.list
[i
].fname
, 0);
336 do_file_mark (panel
, i
, 0);
339 } /* sub case block */
341 result
= g_strdup ("% ");
347 * Check for the "shell_patterns" directive. If it's found and valid,
348 * interpret it and move the pointer past the directive. Return the
352 check_patterns (char *p
)
354 static const char def_name
[] = "shell_patterns=";
357 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
360 p
+= sizeof (def_name
) - 1;
370 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
375 /* Copies a whitespace separated argument from p to arg. Returns the
376 point after argument. */
378 extract_arg (char *p
, char *arg
, int size
)
382 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
384 /* support quote space .mnu */
385 while (*p
&& (*p
!= ' ' || *(p
- 1) == '\\') && *p
!= '\t' && *p
!= '\n')
387 np
= str_get_next_char (p
);
390 memcpy (arg
, p
, np
- p
);
396 if (!*p
|| *p
== '\n')
401 /* Tests whether the selected file in the panel is of any of the types
402 specified in argument. */
404 test_type (WPanel
* panel
, char *arg
)
406 int result
= 0; /* False by default */
407 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
409 for (; *arg
!= 0; arg
++)
413 case 'n': /* Not a directory */
414 result
|= !S_ISDIR (st_mode
);
416 case 'r': /* Regular file */
417 result
|= S_ISREG (st_mode
);
419 case 'd': /* Directory */
420 result
|= S_ISDIR (st_mode
);
423 result
|= S_ISLNK (st_mode
);
425 case 'c': /* Character special */
426 result
|= S_ISCHR (st_mode
);
428 case 'b': /* Block special */
429 result
|= S_ISBLK (st_mode
);
431 case 'f': /* Fifo (named pipe) */
432 result
|= S_ISFIFO (st_mode
);
434 case 's': /* Socket */
435 result
|= S_ISSOCK (st_mode
);
437 case 'x': /* Executable */
438 result
|= (st_mode
& 0111) ? 1 : 0;
441 result
|= panel
->marked
? 1 : 0;
451 /* Calculates the truth value of the next condition starting from
452 p. Returns the point after condition. */
454 test_condition (WEdit
* edit_widget
, char *p
, int *condition
)
458 mc_search_type_t search_type
;
462 search_type
= MC_SEARCH_T_GLOB
;
466 search_type
= MC_SEARCH_T_REGEX
;
469 /* Handle one condition */
470 for (; *p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++)
472 /* support quote space .mnu */
473 if ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
476 panel
= current_panel
;
479 if (get_other_type () == view_listing
)
489 p
= test_condition (edit_widget
, p
, condition
);
490 *condition
= !*condition
;
493 case 'f': /* file name pattern */
494 p
= extract_arg (p
, arg
, sizeof (arg
));
496 && mc_search (arg
, panel
->dir
.list
[panel
->selected
].fname
, search_type
);
498 case 'y': /* syntax pattern */
499 #ifdef USE_INTERNAL_EDIT
502 const char *syntax_type
= edit_get_syntax_type (edit_widget
);
503 if (syntax_type
!= NULL
)
505 p
= extract_arg (p
, arg
, sizeof (arg
));
506 *condition
= panel
&& mc_search (arg
, syntax_type
, MC_SEARCH_T_NORMAL
);
512 p
= extract_arg (p
, arg
, sizeof (arg
));
513 *condition
= panel
&& mc_search (arg
, panel
->cwd
, search_type
);
516 p
= extract_arg (p
, arg
, sizeof (arg
));
517 *condition
= panel
&& test_type (panel
, arg
);
519 case 'x': /* executable */
523 p
= extract_arg (p
, arg
, sizeof (arg
));
524 if (stat (arg
, &status
) == 0)
525 *condition
= is_exe (status
.st_mode
);
539 /* General purpose condition debug output handler */
541 debug_out (char *start
, char *end
, int cond
)
546 if (start
== NULL
&& end
== NULL
)
549 if (debug_flag
&& msg
)
554 message (D_NORMAL
, _("Debug"), "%s", msg
);
566 /* Save debug info for later output */
569 /* Save the result of the condition */
579 /* This is for debugging, don't need to be super efficient. */
581 p
= g_strdup_printf ("%s %s %c \n", msg
? msg
: "", type
, *start
);
583 p
= g_strdup_printf ("%s %s %.*s \n", msg
? msg
: "", type
, (int) (end
- start
), start
);
589 /* Calculates the truth value of one lineful of conditions. Returns
590 the point just before the end of line. */
592 test_line (WEdit
* edit_widget
, char *p
, int *result
)
596 char *debug_start
, *debug_end
;
598 /* Repeat till end of line */
599 while (*p
&& *p
!= '\n')
601 /* support quote space .mnu */
602 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
604 if (!*p
|| *p
== '\n')
612 /* support quote space .mnu */
613 while ((*p
== ' ' && *(p
- 1) != '\\') || *p
== '\t')
615 if (!*p
|| *p
== '\n')
617 condition
= 1; /* True by default */
620 p
= test_condition (edit_widget
, p
, &condition
);
622 /* Add one debug statement */
623 debug_out (debug_start
, debug_end
, condition
);
632 case '&': /* Logical and */
633 *result
&= condition
;
635 case '|': /* Logical or */
636 *result
|= condition
;
642 /* Add one debug statement */
643 debug_out (&operator, NULL
, *result
);
645 } /* while (*p != '\n') */
646 /* Report debug message */
647 debug_out (NULL
, NULL
, 1);
649 if (!*p
|| *p
== '\n')
654 /* FIXME: recode this routine on version 3.0, it could be cleaner */
656 execute_menu_command (WEdit
* edit_widget
, const char *commands
)
660 int expand_prefix_found
= 0;
662 gboolean do_quote
= FALSE
;
668 /* Skip menu entry title line */
669 commands
= strchr (commands
, '\n');
675 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
677 if (cmd_file_fd
== -1)
679 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary command file\n%s"),
680 unix_error_string (errno
));
683 cmd_file
= fdopen (cmd_file_fd
, "w");
684 fputs ("#! /bin/sh\n", cmd_file
);
687 for (col
= 0; *commands
; commands
++)
691 if (*commands
!= ' ' && *commands
!= '\t')
693 while (*commands
== ' ' || *commands
== '\t')
699 if (*commands
== '\n')
703 if (*commands
== '}')
708 input_dialog (_("Parameter"), lc_prompt
, MC_HISTORY_FM_MENU_EXEC_PARAM
, "");
709 if (!parameter
|| !*parameter
)
719 tmp
= name_quote (parameter
, 0);
720 fputs (tmp
, cmd_file
);
724 fputs (parameter
, cmd_file
);
730 if (parameter
< &lc_prompt
[sizeof (lc_prompt
) - 1])
732 *parameter
++ = *commands
;
736 else if (expand_prefix_found
)
738 expand_prefix_found
= 0;
739 if (g_ascii_isdigit ((gchar
) * commands
))
741 do_quote
= (atoi (commands
) != 0);
742 while (g_ascii_isdigit ((gchar
) * commands
))
745 if (*commands
== '{')
746 parameter
= lc_prompt
;
749 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
750 fputs (text
, cmd_file
);
756 if (*commands
== '%')
758 int i
= check_format_view (commands
+ 1);
766 do_quote
= TRUE
; /* Default: Quote expanded macro */
767 expand_prefix_found
= 1;
771 fputc (*commands
, cmd_file
);
775 chmod (file_name
, S_IRWXU
);
778 mcview_viewer (file_name
, NULL
, 0);
779 dialog_switch_process_pending ();
783 /* execute the command indirectly to allow execution even
784 * on no-exec filesystems. */
785 char *cmd
= g_strconcat ("/bin/sh ", file_name
, (char *) NULL
);
786 shell_execute (cmd
, EXECUTE_HIDE
);
794 ** Check owner of the menu file. Using menu file is allowed, if
795 ** owner of the menu is root or the actual user. In either case
796 ** file should not be group and word-writable.
798 ** Q. Should we apply this routine to system and home menu (and .ext files)?
801 menu_file_own (char *path
)
805 if (stat (path
, &st
) == 0
806 && (!st
.st_uid
|| (st
.st_uid
== geteuid ())) && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0))
812 message (D_NORMAL
, _("Warning -- ignoring file"),
813 _("File %s is not owned by root or you or is world writable.\n"
814 "Using it may compromise your security"), path
);
820 * If edit_widget is NULL then we are called from the mc menu,
821 * otherwise we are called from the mcedit menu.
824 user_menu_cmd (struct WEdit
*edit_widget
)
827 char *data
, **entries
;
828 int max_cols
, menu_lines
, menu_limit
;
829 int col
, i
, accept_entry
= 1;
830 int selected
, old_patterns
;
833 if (!vfs_current_is_local ())
835 message (D_ERROR
, MSG_ERROR
, "%s", _("Cannot execute commands on non-local filesystems"));
839 menu
= g_strdup (edit_widget
? EDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
840 if (!exist_file (menu
) || !menu_file_own (menu
))
844 menu
= concat_dir_and_file (home_dir
, EDIT_HOME_MENU
);
846 menu
= g_build_filename (home_dir
, MC_USERCONF_DIR
, MC_USERMENU_FILE
, NULL
);
849 if (!exist_file (menu
))
852 menu
= concat_dir_and_file (mc_home
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
853 if (!exist_file (menu
))
856 menu
= concat_dir_and_file
857 (mc_home_alt
, edit_widget
? EDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
862 data
= load_file (menu
);
865 message (D_ERROR
, MSG_ERROR
, _("Cannot open file%s\n%s"), menu
, unix_error_string (errno
));
876 /* Parse the menu file */
877 old_patterns
= easy_patterns
;
878 p
= check_patterns (data
);
879 for (menu_lines
= col
= 0; *p
; str_next_char (&p
))
881 if (menu_lines
>= menu_limit
)
885 menu_limit
+= MAX_ENTRIES
;
886 new_entries
= g_try_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
888 if (new_entries
== NULL
)
891 entries
= new_entries
;
892 new_entries
+= menu_limit
;
893 while (--new_entries
>= &entries
[menu_lines
])
896 if (col
== 0 && !entries
[menu_lines
])
900 /* A commented menu entry */
907 /* Combined adding and default */
908 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
909 if (selected
== 0 && accept_entry
)
910 selected
= menu_lines
;
914 /* A condition for adding the entry */
915 p
= test_line (edit_widget
, p
, &accept_entry
);
922 /* Combined adding and default */
923 p
= test_line (edit_widget
, p
+ 1, &accept_entry
);
924 if (selected
== 0 && accept_entry
)
925 selected
= menu_lines
;
929 /* A condition for making the entry default */
931 p
= test_line (edit_widget
, p
, &i
);
932 if (selected
== 0 && i
)
933 selected
= menu_lines
;
936 else if (*p
!= ' ' && *p
!= '\t' && str_isprint (p
))
938 /* A menu entry title line */
940 entries
[menu_lines
] = p
;
947 if (entries
[menu_lines
])
952 max_cols
= max (max_cols
, col
);
964 message (D_ERROR
, MSG_ERROR
, _("No suitable entries found in %s"), menu
);
967 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
970 listbox
= create_listbox_window (menu_lines
, max_cols
+ 2, _("User menu"),
972 /* insert all the items found */
973 for (i
= 0; i
< menu_lines
; i
++)
976 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
977 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
);
979 /* Select the default entry */
980 listbox_select_entry (listbox
->list
, selected
);
982 selected
= run_listbox (listbox
);
984 execute_menu_command (edit_widget
, entries
[selected
]);
989 easy_patterns
= old_patterns
;