1 /* User Menu implementation
2 Copyright (C) 1994 Miguel de Icaza, Janne Kukonlehto
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #include "subshell.h" /* for subshell_pty */
36 #include "../edit/edit.h" /* BLOCK_FILE */
37 #include "../edit/edit-widget.h" /* WEdit */
39 /* For the simple listbox manager */
44 #include "view.h" /* for default_* externs */
46 #define MAX_ENTRIES 16
47 #define MAX_ENTRY_LEN 60
49 static int debug_flag
= 0;
50 static int debug_error
= 0;
51 static char *menu
= NULL
;
55 %f The current file (if non-local vfs, file will be copied locally and
56 %f will be full path to it).
58 %d The current working directory
59 %s "Selected files"; the tagged files if any, otherwise the current file
61 %u Tagged files (and they are untagged on return from expand_format)
62 %view Runs the commands and pipes standard output to the view command.
63 If %view is immediately followed by '{', recognize keywords
64 ascii, hex, nroff and unform
66 If the format letter is in uppercase, it refers to the other panel.
68 With a number followed the % character you can turn quoting on (default)
70 %f quote expanded macro
72 %0f don't quote expanded macro
74 expand_format returns a memory block that must be free()d.
77 /* Returns how many characters we should advance if %view was found */
78 int check_format_view (const char *p
)
81 if (!strncmp (p
, "view", 4)) {
84 for (q
++;*q
&& *q
!= '}';q
++) {
85 if (!strncmp (q
, "ascii", 5)) {
88 } else if (!strncmp (q
, "hex", 3)) {
91 } else if (!strncmp (q
, "nroff", 5)) {
92 default_nroff_flag
= 1;
94 } else if (!strncmp (q
, "unform", 6)) {
95 default_nroff_flag
= 0;
107 int check_format_cd (const char *p
)
109 return (strncmp (p
, "cd", 2)) ? 0 : 3;
112 /* Check if p has a "^var\{var-name\}" */
113 /* Returns the number of skipped characters (zero on not found) */
114 /* V will be set to the expanded variable name */
115 int check_format_var (const char *p
, char **v
)
120 const char *dots
= 0;
123 if (!strncmp (p
, "var{", 4)){
124 for (q
+= 4; *q
&& *q
!= '}'; q
++){
131 if (!dots
|| dots
== q
+5){
133 _(" Format error on file Extensions File "),
134 !dots
? _(" The %%var macro has no default ")
135 : _(" The %%var macro has no variable "));
139 /* Copy the variable name */
140 var_name
= g_strndup (p
+ 4, dots
-2 - (p
+3));
142 value
= getenv (var_name
);
145 *v
= g_strdup (value
);
148 var_name
= g_strndup (dots
, q
- dots
);
155 /* strip file's extension */
159 register char *s
= ss
;
163 if(*s
== PATH_SEP
&& e
) e
=NULL
; /* '.' in *directory* name */
171 expand_format (struct WEdit
*edit_widget
, char c
, int quote
)
174 char *(*quote_func
) (const char *, int);
178 return g_strdup ("%");
180 if (islower ((unsigned) c
))
181 panel
= current_panel
;
183 if (get_other_type () != view_listing
)
184 return g_strdup ("");
188 panel
= current_panel
;
191 quote_func
= name_quote
;
193 quote_func
= fake_name_quote
;
196 fname
= panel
->dir
.list
[panel
->selected
].fname
;
201 return (*quote_func
) (fname
, 0);
203 return (*quote_func
) (extension (fname
), 0);
205 return (*quote_func
) (panel
->cwd
, 0);
206 case 'i': /* indent equal number cursor position in line */
208 return g_strnfill (edit_widget
->curs_col
, ' ');
210 case 'y': /* syntax type */
211 if (edit_widget
&& edit_widget
->syntax_type
)
212 return g_strdup (edit_widget
->syntax_type
);
214 case 'k': /* block file name */
215 case 'b': /* block file name / strip extension */ {
217 char *file
= g_strconcat (home_dir
, BLOCK_FILE
, NULL
);
218 fname
= (*quote_func
) (file
, 0);
221 } else if (c
== 'b') {
222 return strip_ext ((*quote_func
) (fname
, 0));
226 case 'n': /* strip extension in editor */
228 return strip_ext ((*quote_func
) (fname
, 0));
230 case 'm': /* menu file name */
232 return (*quote_func
) (menu
, 0);
236 return (*quote_func
) (fname
, 0);
246 for (i
= 0; i
< panel
->count
; i
++)
247 if (panel
->dir
.list
[i
].f
.marked
)
248 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
250 block
= g_malloc (length
* 2 + 1);
252 for (i
= 0; i
< panel
->count
; i
++)
253 if (panel
->dir
.list
[i
].f
.marked
) {
255 (*quote_func
) (panel
->dir
.list
[i
].fname
, 0));
259 do_file_mark (panel
, i
, 0);
262 } /* sub case block */
264 return g_strdup ("%");
268 * Check for the "shell_patterns" directive. If it's found and valid,
269 * interpret it and move the pointer past the directive. Return the
273 check_patterns (char *p
)
275 static const char def_name
[] = "shell_patterns=";
278 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
281 p
+= sizeof (def_name
) - 1;
291 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
296 /* Copies a whitespace separated argument from p to arg. Returns the
297 point after argument. */
298 static char *extract_arg (char *p
, char *arg
, int size
)
300 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
302 /* support quote space .mnu */
303 while (size
> 1 && *p
&& (*p
!= ' ' || *(p
-1) == '\\') && *p
!= '\t' && *p
!= '\n') {
308 if (!*p
|| *p
== '\n')
313 /* Tests whether the selected file in the panel is of any of the types
314 specified in argument. */
315 static int test_type (WPanel
*panel
, char *arg
)
317 int result
= 0; /* False by default */
318 int st_mode
= panel
->dir
.list
[panel
->selected
].st
.st_mode
;
320 for (;*arg
!= 0; arg
++){
322 case 'n': /* Not a directory */
323 result
|= !S_ISDIR (st_mode
);
325 case 'r': /* Regular file */
326 result
|= S_ISREG (st_mode
);
328 case 'd': /* Directory */
329 result
|= S_ISDIR (st_mode
);
332 result
|= S_ISLNK (st_mode
);
334 case 'c': /* Character special */
335 result
|= S_ISCHR (st_mode
);
337 case 'b': /* Block special */
338 result
|= S_ISBLK (st_mode
);
340 case 'f': /* Fifo (named pipe) */
341 result
|= S_ISFIFO (st_mode
);
343 case 's': /* Socket */
344 result
|= S_ISSOCK (st_mode
);
346 case 'x': /* Executable */
347 result
|= (st_mode
& 0111) ? 1 : 0;
350 result
|= panel
->marked
? 1 : 0;
360 /* Calculates the truth value of the next condition starting from
361 p. Returns the point after condition. */
362 static char *test_condition (WEdit
*edit_widget
, char *p
, int *condition
)
367 /* Handle one condition */
368 for (;*p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++){
369 /* support quote space .mnu */
370 if ((*p
== ' ' && *(p
-1) != '\\') || *p
== '\t')
373 panel
= current_panel
;
375 if (get_other_type () == view_listing
)
384 p
= test_condition (edit_widget
, p
, condition
);
385 *condition
= ! *condition
;
388 case 'f': /* file name pattern */
389 p
= extract_arg (p
, arg
, sizeof (arg
));
390 *condition
= panel
&& regexp_match (arg
, panel
->dir
.list
[panel
->selected
].fname
, match_file
);
392 case 'y': /* syntax pattern */
393 if (edit_widget
&& edit_widget
->syntax_type
) {
394 p
= extract_arg (p
, arg
, sizeof (arg
));
395 *condition
= panel
&&
396 regexp_match (arg
, edit_widget
->syntax_type
, match_normal
);
400 p
= extract_arg (p
, arg
, sizeof (arg
));
401 *condition
= panel
&& regexp_match (arg
, panel
->cwd
, match_file
);
404 p
= extract_arg (p
, arg
, sizeof (arg
));
405 *condition
= panel
&& test_type (panel
, arg
);
407 case 'x': /* executable */
411 p
= extract_arg (p
, arg
, sizeof (arg
));
412 if (stat (arg
, &status
) == 0)
413 *condition
= is_exe (status
.st_mode
);
427 /* General purpose condition debug output handler */
429 debug_out (char *start
, char *end
, int cond
)
434 if (start
== NULL
&& end
== NULL
){
436 if (debug_flag
&& msg
) {
440 message (0, _(" Debug "), "%s", msg
);
449 /* Save debug info for later output */
452 /* Save the result of the condition */
454 type
= _(" ERROR: ");
460 type
= _(" False: ");
461 /* This is for debugging, don't need to be super efficient. */
463 p
= g_strdup_printf ("%s%s%c \n", msg
? msg
: "", type
, *start
);
465 p
= g_strdup_printf ("%s%s%.*s \n", msg
? msg
: "", type
,
466 (int) (end
- start
), start
);
472 /* Calculates the truth value of one lineful of conditions. Returns
473 the point just before the end of line. */
474 static char *test_line (WEdit
*edit_widget
, char *p
, int *result
)
478 char *debug_start
, *debug_end
;
480 /* Repeat till end of line */
481 while (*p
&& *p
!= '\n') {
482 /* support quote space .mnu */
483 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
485 if (!*p
|| *p
== '\n')
492 /* support quote space .mnu */
493 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
495 if (!*p
|| *p
== '\n')
497 condition
= 1; /* True by default */
500 p
= test_condition (edit_widget
, p
, &condition
);
502 /* Add one debug statement */
503 debug_out (debug_start
, debug_end
, condition
);
511 case '&': /* Logical and */
512 *result
&= condition
;
514 case '|': /* Logical or */
515 *result
|= condition
;
521 /* Add one debug statement */
522 debug_out (&operator, NULL
, *result
);
524 } /* while (*p != '\n') */
525 /* Report debug message */
526 debug_out (NULL
, NULL
, 1);
528 if (!*p
|| *p
== '\n')
533 /* FIXME: recode this routine on version 3.0, it could be cleaner */
535 execute_menu_command (WEdit
*edit_widget
, const char *commands
)
539 int expand_prefix_found
= 0;
547 /* Skip menu entry title line */
548 commands
= strchr (commands
, '\n');
553 cmd_file_fd
= mc_mkstemps (&file_name
, "mcusr", SCRIPT_SUFFIX
);
555 if (cmd_file_fd
== -1){
556 message (1, MSG_ERROR
, _(" Cannot create temporary command file \n %s "),
557 unix_error_string (errno
));
560 cmd_file
= fdopen (cmd_file_fd
, "w");
561 fputs ("#! /bin/sh\n", cmd_file
);
564 for (col
= 0; *commands
; commands
++){
566 if (*commands
!= ' ' && *commands
!= '\t')
568 while (*commands
== ' ' || *commands
== '\t')
574 if (*commands
== '\n')
577 if (*commands
== '}'){
580 parameter
= input_dialog (_(" Parameter "), prompt
, "");
581 if (!parameter
|| !*parameter
){
589 fputs (tmp
= name_quote (parameter
, 0), cmd_file
);
592 fputs (parameter
, cmd_file
);
596 if (parameter
< &prompt
[sizeof (prompt
) - 1]) {
597 *parameter
++ = *commands
;
600 } else if (expand_prefix_found
){
601 expand_prefix_found
= 0;
602 if (isdigit ((unsigned)*commands
)) {
603 do_quote
= atoi (commands
);
604 while (isdigit ((unsigned)*commands
))
607 if (*commands
== '{')
610 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
611 fputs (text
, cmd_file
);
615 if (*commands
== '%') {
616 int i
= check_format_view (commands
+ 1);
621 do_quote
= 1; /* Default: Quote expanded macro */
622 expand_prefix_found
= 1;
625 fputc (*commands
, cmd_file
);
629 chmod (file_name
, S_IRWXU
);
632 view (file_name
, 0, &run_view
, 0);
634 shell_execute (file_name
, 0);
641 ** Check owner of the menu file. Using menu file is allowed, if
642 ** owner of the menu is root or the actual user. In either case
643 ** file should not be group and word-writable.
645 ** Q. Should we apply this routine to system and home menu (and .ext files)?
648 menu_file_own(char* path
)
652 if (stat (path
, &st
) == 0
653 && (!st
.st_uid
|| (st
.st_uid
== geteuid ()))
654 && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0)
660 message (0, _(" Warning -- ignoring file "),
661 _("File %s is not owned by root or you or is world writable.\n"
662 "Using it may compromise your security"),
670 * If edit_widget is NULL then we are called from the mc menu,
671 * otherwise we are called from the mcedit menu.
674 user_menu_cmd (struct WEdit
*edit_widget
)
677 char *data
, **entries
;
678 int max_cols
, menu_lines
, menu_limit
;
679 int col
, i
, accept_entry
= 1;
680 int selected
, old_patterns
;
683 if (!vfs_current_is_local ()){
684 message (1, MSG_ERROR
,
685 _(" Cannot execute commands on non-local filesystems"));
689 menu
= g_strdup (edit_widget
? CEDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
690 if (!exist_file (menu
) || !menu_file_own (menu
)){
692 menu
= concat_dir_and_file \
693 (home_dir
, edit_widget
? CEDIT_HOME_MENU
: MC_HOME_MENU
);
694 if (!exist_file (menu
)){
696 menu
= concat_dir_and_file \
697 (mc_home
, edit_widget
? CEDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
701 if ((data
= load_file (menu
)) == NULL
){
702 message (1, MSG_ERROR
, _(" Cannot open file %s \n %s "),
703 menu
, unix_error_string (errno
));
714 /* Parse the menu file */
715 old_patterns
= easy_patterns
;
716 p
= check_patterns (data
);
717 for (menu_lines
= col
= 0; *p
; p
++){
718 if (menu_lines
>= menu_limit
){
721 menu_limit
+= MAX_ENTRIES
;
722 new_entries
= g_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
724 if (new_entries
== 0)
727 entries
= new_entries
;
728 new_entries
+= menu_limit
;
729 while (--new_entries
>= &entries
[menu_lines
])
732 if (col
== 0 && !entries
[menu_lines
]){
734 /* A commented menu entry */
736 } else if (*p
== '+'){
738 /* Combined adding and default */
739 p
= test_line (edit_widget
, p
, &accept_entry
);
740 if (selected
== 0 && accept_entry
)
741 selected
= menu_lines
;
743 /* A condition for adding the entry */
744 p
= test_line (edit_widget
, p
, &accept_entry
);
746 } else if (*p
== '='){
748 /* Combined adding and default */
749 p
= test_line (edit_widget
, p
, &accept_entry
);
750 if (selected
== 0 && accept_entry
)
751 selected
= menu_lines
;
753 /* A condition for making the entry default */
755 p
= test_line (edit_widget
, p
, &i
);
756 if (selected
== 0 && i
)
757 selected
= menu_lines
;
760 else if (*p
!= ' ' && *p
!= '\t' && is_printable (*p
)) {
761 /* A menu entry title line */
763 entries
[menu_lines
] = p
;
769 if (entries
[menu_lines
]){
773 max_cols
= max (max_cols
, col
);
782 if (menu_lines
== 0) {
783 message (1, MSG_ERROR
, _(" No suitable entries found in %s "), menu
);
786 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
789 listbox
= create_listbox_window (max_cols
+2, menu_lines
, _(" User menu "),
791 /* insert all the items found */
792 for (i
= 0; i
< menu_lines
; i
++) {
794 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
795 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
798 /* Select the default entry */
799 listbox_select_by_number (listbox
->list
, selected
);
801 selected
= run_listbox (listbox
);
803 execute_menu_command (edit_widget
, entries
[selected
]);
808 easy_patterns
= old_patterns
;