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. */
31 #include "subshell.h" /* for subshell_pty */
36 #include "edit/edit.h" /* BLOCK_FILE */
37 #include "edit/edit-widget.h" /* WEdit */
38 #include "../vfs/vfs.h"
40 /* For the simple listbox manager */
45 #include "view.h" /* for default_* externs */
47 #define MAX_ENTRIES 16
48 #define MAX_ENTRY_LEN 60
50 static int debug_flag
= 0;
51 static int debug_error
= 0;
52 static char *menu
= NULL
;
56 %f The current file (if non-local vfs, file will be copied locally and
57 %f will be full path to it).
59 %d The current working directory
60 %s "Selected files"; the tagged files if any, otherwise the current file
62 %u Tagged files (and they are untagged on return from expand_format)
63 %view Runs the commands and pipes standard output to the view command.
64 If %view is immediately followed by '{', recognize keywords
65 ascii, hex, nroff and unform
67 If the format letter is in uppercase, it refers to the other panel.
69 With a number followed the % character you can turn quoting on (default)
71 %f quote expanded macro
73 %0f don't quote expanded macro
75 expand_format returns a memory block that must be free()d.
78 /* Returns how many characters we should advance if %view was found */
79 int check_format_view (const char *p
)
82 if (!strncmp (p
, "view", 4)) {
85 for (q
++;*q
&& *q
!= '}';q
++) {
86 if (!strncmp (q
, "ascii", 5)) {
89 } else if (!strncmp (q
, "hex", 3)) {
92 } else if (!strncmp (q
, "nroff", 5)) {
93 default_nroff_flag
= 1;
95 } else if (!strncmp (q
, "unform", 6)) {
96 default_nroff_flag
= 0;
108 int check_format_cd (const char *p
)
110 return (strncmp (p
, "cd", 2)) ? 0 : 3;
113 /* Check if p has a "^var\{var-name\}" */
114 /* Returns the number of skipped characters (zero on not found) */
115 /* V will be set to the expanded variable name */
116 int check_format_var (const char *p
, char **v
)
121 const char *dots
= 0;
124 if (!strncmp (p
, "var{", 4)){
125 for (q
+= 4; *q
&& *q
!= '}'; q
++){
132 if (!dots
|| dots
== q
+5){
134 _(" Format error on file Extensions File "),
135 !dots
? _(" The %%var macro has no default ")
136 : _(" The %%var macro has no variable "));
140 /* Copy the variable name */
141 var_name
= g_malloc (dots
- p
);
142 strncpy (var_name
, p
+4, dots
-2 - (p
+3));
143 var_name
[dots
-2 - (p
+3)] = 0;
145 value
= getenv (var_name
);
148 *v
= g_strdup (value
);
151 var_name
= g_malloc (q
- dots
+ 1);
152 strncpy (var_name
, dots
, q
- dots
+ 1);
153 var_name
[q
-dots
] = 0;
160 /* strip file's extension */
164 register char *s
= ss
;
168 if(*s
== PATH_SEP
&& e
) e
=NULL
; /* '.' in *directory* name */
176 expand_format (struct WEdit
*edit_widget
, char c
, int quote
)
179 char *(*quote_func
) (const char *, int);
183 return g_strdup ("%");
185 if (islower ((unsigned) c
))
188 if (get_other_type () != view_listing
)
189 return g_strdup ("");
196 quote_func
= name_quote
;
198 quote_func
= fake_name_quote
;
201 fname
= panel
->dir
.list
[panel
->selected
].fname
;
206 return (*quote_func
) (fname
, 0);
208 return (*quote_func
) (extension (fname
), 0);
210 return (*quote_func
) (panel
->cwd
, 0);
211 case 'i': /* indent equal number cursor position in line */
213 return g_strnfill (edit_widget
->curs_col
, ' ');
215 case 'y': /* syntax type */
216 if (edit_widget
&& edit_widget
->syntax_type
)
217 return g_strdup (edit_widget
->syntax_type
);
219 case 'k': /* block file name */
220 case 'b': /* block file name / strip extension */ {
222 char *file
= g_strconcat (home_dir
, BLOCK_FILE
, NULL
);
223 fname
= (*quote_func
) (file
, 0);
226 } else if (c
== 'b') {
227 return strip_ext ((*quote_func
) (fname
, 0));
231 case 'n': /* strip extension in editor */
233 return strip_ext ((*quote_func
) (fname
, 0));
235 case 'm': /* menu file name */
237 return (*quote_func
) (menu
, 0);
241 return (*quote_func
) (fname
, 0);
251 for (i
= 0; i
< panel
->count
; i
++)
252 if (panel
->dir
.list
[i
].f
.marked
)
253 length
+= strlen (panel
->dir
.list
[i
].fname
) + 1; /* for space */
255 block
= g_malloc (length
* 2 + 1);
257 for (i
= 0; i
< panel
->count
; i
++)
258 if (panel
->dir
.list
[i
].f
.marked
) {
260 (*quote_func
) (panel
->dir
.list
[i
].fname
, 0));
264 do_file_mark (panel
, i
, 0);
267 } /* sub case block */
269 return g_strdup ("");
273 * Check for the "shell_patterns" directive. If it's found and valid,
274 * interpret it and move the pointer past the directive. Return the
278 check_patterns (char *p
)
280 static const char def_name
[] = "shell_patterns=";
283 if (strncmp (p
, def_name
, sizeof (def_name
) - 1) != 0)
286 p
+= sizeof (def_name
) - 1;
296 while (*p
== '\n' || *p
== '\t' || *p
== ' ')
301 /* Copies a whitespace separated argument from p to arg. Returns the
302 point after argument. */
303 static char *extract_arg (char *p
, char *arg
)
305 while (*p
&& (*p
== ' ' || *p
== '\t' || *p
== '\n'))
307 /* support quote space .mnu */
308 while (*p
&& (*p
!= ' ' || *(p
-1) == '\\') && *p
!= '\t' && *p
!= '\n')
311 if (!*p
|| *p
== '\n')
316 /* Tests whether the selected file in the panel is of any of the types
317 specified in argument. */
318 static int test_type (WPanel
*panel
, char *arg
)
320 int result
= 0; /* False by default */
321 int st_mode
= panel
->dir
.list
[panel
->selected
].buf
.st_mode
;
323 for (;*arg
!= 0; arg
++){
325 case 'n': /* Not a directory */
326 result
|= !S_ISDIR (st_mode
);
328 case 'r': /* Regular file */
329 result
|= S_ISREG (st_mode
);
331 case 'd': /* Directory */
332 result
|= S_ISDIR (st_mode
);
335 result
|= S_ISLNK (st_mode
);
337 case 'c': /* Character special */
338 result
|= S_ISCHR (st_mode
);
340 case 'b': /* Block special */
341 result
|= S_ISBLK (st_mode
);
343 case 'f': /* Fifo (named pipe) */
344 result
|= S_ISFIFO (st_mode
);
346 case 's': /* Socket */
347 result
|= S_ISSOCK (st_mode
);
349 case 'x': /* Executable */
350 result
|= (st_mode
& 0111) ? 1 : 0;
353 result
|= panel
->marked
? 1 : 0;
363 /* Calculates the truth value of the next condition starting from
364 p. Returns the point after condition. */
365 static char *test_condition (WEdit
*edit_widget
, char *p
, int *condition
)
370 /* Handle one condition */
371 for (;*p
!= '\n' && *p
!= '&' && *p
!= '|'; p
++){
372 /* support quote space .mnu */
373 if ((*p
== ' ' && *(p
-1) != '\\') || *p
== '\t')
378 if (get_other_type () == view_listing
)
387 p
= test_condition (edit_widget
, p
, condition
);
388 *condition
= ! *condition
;
391 case 'f': /* file name pattern */
392 p
= extract_arg (p
, arg
);
393 *condition
= panel
&& regexp_match (arg
, panel
->dir
.list
[panel
->selected
].fname
, match_file
);
395 case 'y': /* syntax pattern */
396 if (edit_widget
&& edit_widget
->syntax_type
) {
397 p
= extract_arg (p
, arg
);
398 *condition
= panel
&&
399 regexp_match (arg
, edit_widget
->syntax_type
, match_normal
);
403 p
= extract_arg (p
, arg
);
404 *condition
= panel
&& regexp_match (arg
, panel
->cwd
, match_file
);
407 p
= extract_arg (p
, arg
);
408 *condition
= panel
&& test_type (panel
, arg
);
410 case 'x': /* executable */
414 p
= extract_arg (p
, arg
);
415 if (stat (arg
, &status
) == 0)
416 *condition
= is_exe (status
.st_mode
);
430 /* General purpose condition debug output handler */
432 debug_out (char *start
, char *end
, int cond
)
434 static char msg
[256];
437 if (start
== NULL
&& end
== NULL
){
448 message (0, _(" Debug "), "%s", msg
);
452 /* Save debug info for later output */
455 /* Save the result of the condition */
457 strcat (msg
, _(" ERROR: "));
461 strcat (msg
, _(" True: "));
463 strcat (msg
, _(" False: "));
464 /* Copy condition statement */
467 /* Copy one character */
471 /* Copy many characters */
473 msg
[len
++] = *start
++;
481 /* Calculates the truth value of one lineful of conditions. Returns
482 the point just before the end of line. */
483 static char *test_line (WEdit
*edit_widget
, char *p
, int *result
)
487 char *debug_start
, *debug_end
;
490 debug_out (NULL
, NULL
, 0);
491 /* Repeat till end of line */
492 while (*p
&& *p
!= '\n') {
493 /* support quote space .mnu */
494 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
496 if (!*p
|| *p
== '\n')
503 /* support quote space .mnu */
504 while ((*p
== ' ' && *(p
-1) != '\\' ) || *p
== '\t')
506 if (!*p
|| *p
== '\n')
508 condition
= 1; /* True by default */
511 p
= test_condition (edit_widget
, p
, &condition
);
513 /* Add one debug statement */
514 debug_out (debug_start
, debug_end
, condition
);
522 case '&': /* Logical and */
523 *result
&= condition
;
525 case '|': /* Logical or */
526 *result
|= condition
;
532 /* Add one debug statement */
533 debug_out (&operator, NULL
, *result
);
535 } /* while (*p != '\n') */
536 /* Report debug message */
537 debug_out (NULL
, NULL
, 1);
539 if (!*p
|| *p
== '\n')
544 /* FIXME: recode this routine on version 3.0, it could be cleaner */
546 execute_menu_command (WEdit
*edit_widget
, char *commands
)
550 int expand_prefix_found
= 0;
559 /* Skip menu entry title line */
560 commands
= strchr (commands
, '\n');
565 cmd_file_fd
= mc_mkstemps(&file_name
, "mcusr", SCRIPT_SUFFIX
);
567 if (cmd_file_fd
== -1){
568 message (1, MSG_ERROR
, _(" Cannot create temporary command file \n %s "),
569 unix_error_string (errno
));
572 cmd_file
= fdopen (cmd_file_fd
, "w");
575 for (col
= 0; *commands
; commands
++){
577 if (*commands
!= ' ' && *commands
!= '\t')
579 while (*commands
== ' ' || *commands
== '\t')
583 if (*commands
== '\n')
586 if (*commands
== '}'){
589 parameter
= input_dialog (_(" Parameter "), prompt
, "");
590 if (!parameter
|| !*parameter
){
598 fputs (tmp
= name_quote (parameter
, 0), cmd_file
);
601 fputs (parameter
, cmd_file
);
605 if (parameter
< &prompt
[sizeof (prompt
) - 1]) {
606 *parameter
++ = *commands
;
609 } else if (expand_prefix_found
){
610 expand_prefix_found
= 0;
611 if (isdigit ((unsigned)*commands
)) {
612 do_quote
= atoi (commands
);
613 while (isdigit ((unsigned)*commands
))
616 if (*commands
== '{')
619 char *text
= expand_format (edit_widget
, *commands
, do_quote
);
620 fputs (text
, cmd_file
);
624 if (*commands
== '%') {
625 do_quote
= 1; /* Default: Quote expanded macro */
626 expand_prefix_found
= 1;
628 fputc (*commands
, cmd_file
);
632 chmod (file_name
, S_IRWXU
);
639 ** Check owner of the menu file. Using menu file is allowed, if
640 ** owner of the menu is root or the actual user. In either case
641 ** file should not be group and word-writable.
643 ** Q. Should we apply this routine to system and home menu (and .ext files)?
646 menu_file_own(char* path
)
650 if (stat (path
, &st
) == 0
651 && (!st
.st_uid
|| (st
.st_uid
== geteuid ()))
652 && ((st
.st_mode
& (S_IWGRP
| S_IWOTH
)) == 0)
658 message (0, _(" Warning -- ignoring file "),
659 _("File %s is not owned by root or you or is world writable.\n"
660 "Using it may compromise your security"),
668 * If edit_widget is NULL then we are called from the mc menu,
669 * otherwise we are called from the mcedit menu.
672 user_menu_cmd (struct WEdit
*edit_widget
)
675 char *data
, **entries
;
676 int max_cols
, menu_lines
, menu_limit
;
677 int col
, i
, accept_entry
= 1;
678 int selected
, old_patterns
;
681 if (!vfs_current_is_local ()){
682 message (1, MSG_ERROR
,
683 _(" Cannot execute commands on non-local filesystems"));
687 menu
= g_strdup (edit_widget
? CEDIT_LOCAL_MENU
: MC_LOCAL_MENU
);
688 if (!exist_file (menu
) || !menu_file_own (menu
)){
690 menu
= concat_dir_and_file \
691 (home_dir
, edit_widget
? CEDIT_HOME_MENU
: MC_HOME_MENU
);
692 if (!exist_file (menu
)){
694 menu
= concat_dir_and_file \
695 (mc_home
, edit_widget
? CEDIT_GLOBAL_MENU
: MC_GLOBAL_MENU
);
699 if ((data
= load_file (menu
)) == NULL
){
700 message (1, MSG_ERROR
, _(" Cannot open file %s \n %s "),
701 menu
, unix_error_string (errno
));
712 /* Parse the menu file */
713 old_patterns
= easy_patterns
;
714 p
= check_patterns (data
);
715 for (menu_lines
= col
= 0; *p
; p
++){
716 if (menu_lines
>= menu_limit
){
719 menu_limit
+= MAX_ENTRIES
;
720 new_entries
= g_realloc (entries
, sizeof (new_entries
[0]) * menu_limit
);
722 if (new_entries
== 0)
725 entries
= new_entries
;
726 new_entries
+= menu_limit
;
727 while (--new_entries
>= &entries
[menu_lines
])
730 if (col
== 0 && !entries
[menu_lines
]){
732 /* A commented menu entry */
734 } else if (*p
== '+'){
736 /* Combined adding and default */
737 p
= test_line (edit_widget
, p
, &accept_entry
);
738 if (selected
== 0 && accept_entry
)
739 selected
= menu_lines
;
741 /* A condition for adding the entry */
742 p
= test_line (edit_widget
, p
, &accept_entry
);
744 } else if (*p
== '='){
746 /* Combined adding and default */
747 p
= test_line (edit_widget
, p
, &accept_entry
);
748 if (selected
== 0 && accept_entry
)
749 selected
= menu_lines
;
751 /* A condition for making the entry default */
753 p
= test_line (edit_widget
, p
, &i
);
754 if (selected
== 0 && i
)
755 selected
= menu_lines
;
758 else if (*p
!= ' ' && *p
!= '\t' && is_printable (*p
)) {
759 /* A menu entry title line */
761 entries
[menu_lines
] = p
;
767 if (entries
[menu_lines
]){
771 max_cols
= max (max_cols
, col
);
780 if (menu_lines
== 0) {
781 message (1, MSG_ERROR
, _(" No suitable entries found in %s "), menu
);
784 max_cols
= min (max (max_cols
, col
), MAX_ENTRY_LEN
);
787 listbox
= create_listbox_window (max_cols
+2, menu_lines
, _(" User menu "),
789 /* insert all the items found */
790 for (i
= 0; i
< menu_lines
; i
++) {
792 LISTBOX_APPEND_TEXT (listbox
, (unsigned char) p
[0],
793 extract_line (p
, p
+ MAX_ENTRY_LEN
), p
796 /* Select the default entry */
797 listbox_select_by_number (listbox
->list
, selected
);
799 selected
= run_listbox (listbox
);
801 execute_menu_command (edit_widget
, entries
[selected
]);
806 easy_patterns
= old_patterns
;