Fix segfault on switch to subshell in mcedit/mcview/mcdiffview.
[midnight-commander.git] / src / usermenu.c
blob1fecbeaac22dc67a4e610405ec0ddf63ff602d26
1 /*
2 User Menu implementation
4 Copyright (C) 1994-2020
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2013
9 Andrew Borodin <aborodin@vmail.ru>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file usermenu.c
28 * \brief Source: user menu implementation
31 #include <config.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
39 #include "lib/global.h"
40 #include "lib/fileloc.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/search.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/strutil.h"
46 #include "lib/util.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"
56 #include "src/filemanager/dir.h"
57 #include "src/filemanager/filemanager.h"
58 #include "src/filemanager/layout.h"
60 #include "usermenu.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 gboolean debug_flag = FALSE;
74 static gboolean debug_error = FALSE;
75 static char *menu = NULL;
77 /*** file scope functions ************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
80 /** strip file's extension */
81 static char *
82 strip_ext (char *ss)
84 char *s = ss;
85 char *e = NULL;
87 while (*s != '\0')
89 if (*s == '.')
90 e = s;
91 if (IS_PATH_SEP (*s) && e != NULL)
92 e = NULL; /* '.' in *directory* name */
93 s++;
95 if (e != NULL)
96 *e = '\0';
97 return ss;
100 /* --------------------------------------------------------------------------------------------- */
102 * Check for the "shell_patterns" directive. If it's found and valid,
103 * interpret it and move the pointer past the directive. Return the
104 * current pointer.
107 static char *
108 check_patterns (char *p)
110 static const char def_name[] = "shell_patterns=";
111 char *p0 = p;
113 if (strncmp (p, def_name, sizeof (def_name) - 1) != 0)
114 return p0;
116 p += sizeof (def_name) - 1;
117 if (*p == '1')
118 easy_patterns = TRUE;
119 else if (*p == '0')
120 easy_patterns = FALSE;
121 else
122 return p0;
124 /* Skip spaces */
125 p++;
126 while (whiteness (*p))
127 p++;
128 return p;
131 /* --------------------------------------------------------------------------------------------- */
132 /** Copies a whitespace separated argument from p to arg. Returns the
133 point after argument. */
135 static char *
136 extract_arg (char *p, char *arg, int size)
138 while (*p != '\0' && whiteness (*p))
139 p++;
141 /* support quote space .mnu */
142 while (*p != '\0' && (*p != ' ' || *(p - 1) == '\\') && *p != '\t' && *p != '\n')
144 char *np;
146 np = str_get_next_char (p);
147 if (np - p >= size)
148 break;
149 memcpy (arg, p, np - p);
150 arg += np - p;
151 size -= np - p;
152 p = np;
154 *arg = '\0';
155 if (*p == '\0' || *p == '\n')
156 str_prev_char (&p);
157 return p;
160 /* --------------------------------------------------------------------------------------------- */
161 /* Tests whether the selected file in the panel is of any of the types
162 specified in argument. */
164 static gboolean
165 test_type (WPanel * panel, char *arg)
167 int result = 0; /* False by default */
168 mode_t st_mode = panel->dir.list[panel->selected].st.st_mode;
170 for (; *arg != '\0'; arg++)
172 switch (*arg)
174 case 'n': /* Not a directory */
175 result |= !S_ISDIR (st_mode);
176 break;
177 case 'r': /* Regular file */
178 result |= S_ISREG (st_mode);
179 break;
180 case 'd': /* Directory */
181 result |= S_ISDIR (st_mode);
182 break;
183 case 'l': /* Link */
184 result |= S_ISLNK (st_mode);
185 break;
186 case 'c': /* Character special */
187 result |= S_ISCHR (st_mode);
188 break;
189 case 'b': /* Block special */
190 result |= S_ISBLK (st_mode);
191 break;
192 case 'f': /* Fifo (named pipe) */
193 result |= S_ISFIFO (st_mode);
194 break;
195 case 's': /* Socket */
196 result |= S_ISSOCK (st_mode);
197 break;
198 case 'x': /* Executable */
199 result |= (st_mode & 0111) != 0 ? 1 : 0;
200 break;
201 case 't':
202 result |= panel->marked != 0 ? 1 : 0;
203 break;
204 default:
205 debug_error = TRUE;
206 break;
210 return (result != 0);
213 /* --------------------------------------------------------------------------------------------- */
214 /** Calculates the truth value of the next condition starting from
215 p. Returns the point after condition. */
217 static char *
218 test_condition (const WEdit * edit_widget, char *p, gboolean * condition)
220 char arg[256];
221 const mc_search_type_t search_type = easy_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
223 /* Handle one condition */
224 for (; *p != '\n' && *p != '&' && *p != '|'; p++)
226 WPanel *panel = NULL;
228 /* support quote space .mnu */
229 if ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
230 continue;
231 if (*p >= 'a')
232 panel = current_panel;
233 else if (get_other_type () == view_listing)
234 panel = other_panel;
236 *p |= 0x20;
238 switch (*p++)
240 case '!':
241 p = test_condition (edit_widget, p, condition);
242 *condition = !*condition;
243 str_prev_char (&p);
244 break;
245 case 'f': /* file name pattern */
246 p = extract_arg (p, arg, sizeof (arg));
247 #ifdef USE_INTERNAL_EDIT
248 if (edit_widget != NULL)
250 const char *edit_filename;
252 edit_filename = edit_get_file_name (edit_widget);
253 *condition = mc_search (arg, DEFAULT_CHARSET, edit_filename, search_type);
255 else
256 #endif
257 *condition = panel != NULL &&
258 mc_search (arg, DEFAULT_CHARSET, panel->dir.list[panel->selected].fname,
259 search_type);
260 break;
261 case 'y': /* syntax pattern */
262 #ifdef USE_INTERNAL_EDIT
263 if (edit_widget != NULL)
265 const char *syntax_type;
267 syntax_type = edit_get_syntax_type (edit_widget);
268 if (syntax_type != NULL)
270 p = extract_arg (p, arg, sizeof (arg));
271 *condition = mc_search (arg, DEFAULT_CHARSET, syntax_type, MC_SEARCH_T_NORMAL);
274 #endif
275 break;
276 case 'd':
277 p = extract_arg (p, arg, sizeof (arg));
278 *condition = panel != NULL
279 && mc_search (arg, DEFAULT_CHARSET, vfs_path_as_str (panel->cwd_vpath),
280 search_type);
281 break;
282 case 't':
283 p = extract_arg (p, arg, sizeof (arg));
284 *condition = panel != NULL && test_type (panel, arg);
285 break;
286 case 'x': /* executable */
288 struct stat status;
290 p = extract_arg (p, arg, sizeof (arg));
291 *condition = stat (arg, &status) == 0 && is_exe (status.st_mode);
292 break;
294 default:
295 debug_error = TRUE;
296 break;
297 } /* switch */
298 } /* while */
299 return p;
302 /* --------------------------------------------------------------------------------------------- */
303 /** General purpose condition debug output handler */
305 static void
306 debug_out (char *start, char *end, gboolean condition)
308 static char *msg = NULL;
310 if (start == NULL && end == NULL)
312 /* Show output */
313 if (debug_flag && msg != NULL)
315 size_t len;
317 len = strlen (msg);
318 if (len != 0)
319 msg[len - 1] = '\0';
320 message (D_NORMAL, _("Debug"), "%s", msg);
323 debug_flag = FALSE;
324 MC_PTR_FREE (msg);
326 else
328 const char *type;
329 char *p;
331 /* Save debug info for later output */
332 if (!debug_flag)
333 return;
334 /* Save the result of the condition */
335 if (debug_error)
337 type = _("ERROR:");
338 debug_error = FALSE;
340 else if (condition)
341 type = _("True:");
342 else
343 type = _("False:");
344 /* This is for debugging, don't need to be super efficient. */
345 if (end == NULL)
346 p = g_strdup_printf ("%s %s %c \n", msg ? msg : "", type, *start);
347 else
348 p = g_strdup_printf ("%s %s %.*s \n", msg ? msg : "", type, (int) (end - start), start);
349 g_free (msg);
350 msg = p;
354 /* --------------------------------------------------------------------------------------------- */
355 /** Calculates the truth value of one lineful of conditions. Returns
356 the point just before the end of line. */
358 static char *
359 test_line (const WEdit * edit_widget, char *p, gboolean * result)
361 char operator;
363 /* Repeat till end of line */
364 while (*p != '\0' && *p != '\n')
366 char *debug_start, *debug_end;
367 gboolean condition = TRUE;
369 /* support quote space .mnu */
370 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
371 p++;
372 if (*p == '\0' || *p == '\n')
373 break;
374 operator = *p++;
375 if (*p == '?')
377 debug_flag = TRUE;
378 p++;
380 /* support quote space .mnu */
381 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
382 p++;
383 if (*p == '\0' || *p == '\n')
384 break;
386 debug_start = p;
387 p = test_condition (edit_widget, p, &condition);
388 debug_end = p;
389 /* Add one debug statement */
390 debug_out (debug_start, debug_end, condition);
392 switch (operator)
394 case '+':
395 case '=':
396 /* Assignment */
397 *result = condition;
398 break;
399 case '&': /* Logical and */
400 *result = *result && condition;
401 break;
402 case '|': /* Logical or */
403 *result = *result || condition;
404 break;
405 default:
406 debug_error = TRUE;
407 break;
408 } /* switch */
409 /* Add one debug statement */
410 debug_out (&operator, NULL, *result);
412 } /* while (*p != '\n') */
413 /* Report debug message */
414 debug_out (NULL, NULL, TRUE);
416 if (*p == '\0' || *p == '\n')
417 str_prev_char (&p);
418 return p;
421 /* --------------------------------------------------------------------------------------------- */
422 /** FIXME: recode this routine on version 3.0, it could be cleaner */
424 static void
425 execute_menu_command (const WEdit * edit_widget, const char *commands, gboolean show_prompt)
427 FILE *cmd_file;
428 int cmd_file_fd;
429 gboolean expand_prefix_found = FALSE;
430 char *parameter = NULL;
431 gboolean do_quote = FALSE;
432 char lc_prompt[80];
433 int col;
434 vfs_path_t *file_name_vpath;
435 gboolean run_view = FALSE;
437 /* Skip menu entry title line */
438 commands = strchr (commands, '\n');
439 if (commands == NULL)
440 return;
442 cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcusr", SCRIPT_SUFFIX);
444 if (cmd_file_fd == -1)
446 message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"),
447 unix_error_string (errno));
448 vfs_path_free (file_name_vpath);
449 return;
452 cmd_file = fdopen (cmd_file_fd, "w");
453 fputs ("#! /bin/sh\n", cmd_file);
454 commands++;
456 for (col = 0; *commands != '\0'; commands++)
458 if (col == 0)
460 if (!whitespace (*commands))
461 break;
462 while (whitespace (*commands))
463 commands++;
464 if (*commands == '\0')
465 break;
467 col++;
468 if (*commands == '\n')
469 col = 0;
470 if (parameter != NULL)
472 if (*commands == '}')
474 *parameter = '\0';
475 parameter =
476 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "",
477 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD |
478 INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES |
479 INPUT_COMPLETE_USERNAMES);
480 if (parameter == NULL || *parameter == '\0')
482 /* User canceled */
483 fclose (cmd_file);
484 mc_unlink (file_name_vpath);
485 vfs_path_free (file_name_vpath);
486 return;
488 if (do_quote)
490 char *tmp;
492 tmp = name_quote (parameter, FALSE);
493 fputs (tmp, cmd_file);
494 g_free (tmp);
496 else
497 fputs (parameter, cmd_file);
499 MC_PTR_FREE (parameter);
501 else if (parameter < lc_prompt + sizeof (lc_prompt) - 1)
502 *parameter++ = *commands;
504 else if (expand_prefix_found)
506 expand_prefix_found = FALSE;
507 if (g_ascii_isdigit ((gchar) * commands))
509 do_quote = (atoi (commands) != 0);
510 while (g_ascii_isdigit ((gchar) * commands))
511 commands++;
513 if (*commands == '{')
514 parameter = lc_prompt;
515 else
517 char *text;
519 text = expand_format (edit_widget, *commands, do_quote);
520 fputs (text, cmd_file);
521 g_free (text);
524 else if (*commands == '%')
526 int i;
528 i = check_format_view (commands + 1);
529 if (i != 0)
531 commands += i;
532 run_view = TRUE;
534 else
536 do_quote = TRUE; /* Default: Quote expanded macro */
537 expand_prefix_found = TRUE;
540 else
541 fputc (*commands, cmd_file);
544 fclose (cmd_file);
545 mc_chmod (file_name_vpath, S_IRWXU);
547 if (run_view)
549 mcview_viewer (vfs_path_as_str (file_name_vpath), NULL, 0, 0, 0);
550 dialog_switch_process_pending ();
552 else
554 /* execute the command indirectly to allow execution even
555 * on no-exec filesystems. */
556 char *cmd;
558 cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (file_name_vpath), (char *) NULL);
560 if (show_prompt)
561 shell_execute (cmd, EXECUTE_HIDE);
562 else if (system (cmd) == -1)
563 message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
565 g_free (cmd);
567 mc_unlink (file_name_vpath);
568 vfs_path_free (file_name_vpath);
571 /* --------------------------------------------------------------------------------------------- */
573 ** Check owner of the menu file. Using menu file is allowed, if
574 ** owner of the menu is root or the actual user. In either case
575 ** file should not be group and word-writable.
577 ** Q. Should we apply this routine to system and home menu (and .ext files)?
580 static gboolean
581 menu_file_own (char *path)
583 struct stat st;
585 if (stat (path, &st) == 0 && (st.st_uid == 0 || (st.st_uid == geteuid ()) != 0)
586 && ((st.st_mode & (S_IWGRP | S_IWOTH)) == 0))
587 return TRUE;
589 if (verbose)
590 message (D_NORMAL, _("Warning -- ignoring file"),
591 _("File %s is not owned by root or you or is world writable.\n"
592 "Using it may compromise your security"), path);
594 return FALSE;
597 /* --------------------------------------------------------------------------------------------- */
598 /*** public functions ****************************************************************************/
599 /* --------------------------------------------------------------------------------------------- */
601 /* Formats defined:
602 %% The % character
603 %f The current file in the active panel (if non-local vfs, file will be copied locally
604 and %f will be full path to it) or the opened file in the internal editor.
605 %p Likewise.
606 %d The current working directory
607 %s "Selected files"; the tagged files if any, otherwise the current file
608 %t Tagged files
609 %u Tagged files (and they are untagged on return from expand_format)
610 %view Runs the commands and pipes standard output to the view command.
611 If %view is immediately followed by '{', recognize keywords
612 ascii, hex, nroff and unform
614 If the format letter is in uppercase, it refers to the other panel.
616 With a number followed the % character you can turn quoting on (default)
617 and off. For example:
618 %f quote expanded macro
619 %1f ditto
620 %0f don't quote expanded macro
622 expand_format returns a memory block that must be free()d.
625 /* Returns how many characters we should advance if %view was found */
627 check_format_view (const char *p)
629 const char *q = p;
631 if (strncmp (p, "view", 4) == 0)
633 q += 4;
634 if (*q == '{')
636 for (q++; *q != '\0' && *q != '}'; q++)
638 if (strncmp (q, DEFAULT_CHARSET, 5) == 0)
640 mcview_global_flags.hex = FALSE;
641 q += 4;
643 else if (strncmp (q, "hex", 3) == 0)
645 mcview_global_flags.hex = TRUE;
646 q += 2;
648 else if (strncmp (q, "nroff", 5) == 0)
650 mcview_global_flags.nroff = TRUE;
651 q += 4;
653 else if (strncmp (q, "unform", 6) == 0)
655 mcview_global_flags.nroff = FALSE;
656 q += 5;
659 if (*q == '}')
660 q++;
662 return q - p;
664 return 0;
667 /* --------------------------------------------------------------------------------------------- */
670 check_format_cd (const char *p)
672 return (strncmp (p, "cd", 2)) != 0 ? 0 : 3;
675 /* --------------------------------------------------------------------------------------------- */
676 /* Check if p has a "^var\{var-name\}" */
677 /* Returns the number of skipped characters (zero on not found) */
678 /* V will be set to the expanded variable name */
681 check_format_var (const char *p, char **v)
683 *v = NULL;
685 if (strncmp (p, "var{", 4) == 0)
687 const char *q = p;
688 const char *dots = NULL;
689 const char *value;
690 char *var_name;
692 for (q += 4; *q != '\0' && *q != '}'; q++)
694 if (*q == ':')
695 dots = q + 1;
697 if (*q == '\0')
698 return 0;
700 if (dots == NULL || dots == q + 5)
702 message (D_ERROR,
703 _("Format error on file Extensions File"),
704 !dots ? _("The %%var macro has no default")
705 : _("The %%var macro has no variable"));
706 return 0;
709 /* Copy the variable name */
710 var_name = g_strndup (p + 4, dots - 2 - (p + 3));
711 value = getenv (var_name);
712 g_free (var_name);
714 if (value != NULL)
715 *v = g_strdup (value);
716 else
717 *v = g_strndup (dots, q - dots);
719 return q - p;
721 return 0;
724 /* --------------------------------------------------------------------------------------------- */
726 char *
727 expand_format (const WEdit * edit_widget, char c, gboolean do_quote)
729 WPanel *panel = NULL;
730 char *(*quote_func) (const char *, gboolean);
731 const char *fname = NULL;
732 char *result;
733 char c_lc;
735 #ifndef USE_INTERNAL_EDIT
736 (void) edit_widget;
737 #endif
739 if (c == '%')
740 return g_strdup ("%");
742 switch (mc_global.mc_run_mode)
744 case MC_RUN_FULL:
745 #ifdef USE_INTERNAL_EDIT
746 if (edit_widget != NULL)
747 fname = edit_get_file_name (edit_widget);
748 else
749 #endif
751 if (g_ascii_islower ((gchar) c))
752 panel = current_panel;
753 else
755 if (get_other_type () != view_listing)
756 return g_strdup ("");
757 panel = other_panel;
760 fname = panel->dir.list[panel->selected].fname;
762 break;
764 #ifdef USE_INTERNAL_EDIT
765 case MC_RUN_EDITOR:
766 fname = edit_get_file_name (edit_widget);
767 break;
768 #endif
770 default:
771 /* other modes don't use formats */
772 return g_strdup ("");
775 if (do_quote)
776 quote_func = name_quote;
777 else
778 quote_func = fake_name_quote;
780 c_lc = g_ascii_tolower ((gchar) c);
782 switch (c_lc)
784 case 'f':
785 case 'p':
786 result = quote_func (fname, FALSE);
787 goto ret;
788 case 'x':
789 result = quote_func (extension (fname), FALSE);
790 goto ret;
791 case 'd':
793 const char *cwd;
794 char *qstr;
796 if (panel != NULL)
797 cwd = vfs_path_as_str (panel->cwd_vpath);
798 else
799 cwd = vfs_get_current_dir ();
801 qstr = quote_func (cwd, FALSE);
803 result = qstr;
804 goto ret;
806 case 'c':
807 #ifdef USE_INTERNAL_EDIT
808 if (edit_widget != NULL)
810 result = g_strdup_printf ("%u", (unsigned int) edit_get_cursor_offset (edit_widget));
811 goto ret;
813 #endif
814 break;
815 case 'i': /* indent equal number cursor position in line */
816 #ifdef USE_INTERNAL_EDIT
817 if (edit_widget != NULL)
819 result = g_strnfill (edit_get_curs_col (edit_widget), ' ');
820 goto ret;
822 #endif
823 break;
824 case 'y': /* syntax type */
825 #ifdef USE_INTERNAL_EDIT
826 if (edit_widget != NULL)
828 const char *syntax_type;
830 syntax_type = edit_get_syntax_type (edit_widget);
831 if (syntax_type != NULL)
833 result = g_strdup (syntax_type);
834 goto ret;
837 #endif
838 break;
839 case 'k': /* block file name */
840 case 'b': /* block file name / strip extension */
841 #ifdef USE_INTERNAL_EDIT
842 if (edit_widget != NULL)
844 char *file;
846 file = mc_config_get_full_path (EDIT_HOME_BLOCK_FILE);
847 result = quote_func (file, FALSE);
848 g_free (file);
849 goto ret;
851 #endif
852 if (c_lc == 'b')
854 result = strip_ext (quote_func (fname, FALSE));
855 goto ret;
857 break;
858 case 'n': /* strip extension in editor */
859 #ifdef USE_INTERNAL_EDIT
860 if (edit_widget != NULL)
862 result = strip_ext (quote_func (fname, FALSE));
863 goto ret;
865 #endif
866 break;
867 case 'm': /* menu file name */
868 if (menu != NULL)
870 result = quote_func (menu, FALSE);
871 goto ret;
873 break;
874 case 's':
875 if (panel == NULL || panel->marked == 0)
877 result = quote_func (fname, FALSE);
878 goto ret;
881 MC_FALLTHROUGH;
883 case 't':
884 case 'u':
886 GString *block;
887 int i;
889 if (panel == NULL)
891 result = g_strdup ("");
892 goto ret;
895 block = g_string_sized_new (16);
897 for (i = 0; i < panel->dir.len; i++)
898 if (panel->dir.list[i].f.marked)
900 char *tmp;
902 tmp = quote_func (panel->dir.list[i].fname, FALSE);
903 g_string_append (block, tmp);
904 g_string_append_c (block, ' ');
905 g_free (tmp);
907 if (c_lc == 'u')
908 do_file_mark (panel, i, 0);
910 result = g_string_free (block, FALSE);
911 goto ret;
912 } /* sub case block */
913 default:
914 break;
915 } /* switch */
917 result = g_strdup ("% ");
918 result[1] = c;
919 ret:
920 return result;
923 /* --------------------------------------------------------------------------------------------- */
925 * If edit_widget is NULL then we are called from the mc menu,
926 * otherwise we are called from the mcedit menu.
929 gboolean
930 user_menu_cmd (const WEdit * edit_widget, const char *menu_file, int selected_entry)
932 char *p;
933 char *data, **entries;
934 int max_cols, menu_lines, menu_limit;
935 int col, i;
936 gboolean accept_entry = TRUE;
937 int selected;
938 gboolean old_patterns;
939 gboolean res = FALSE;
940 gboolean interactive = TRUE;
942 if (!vfs_current_is_local ())
944 message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems"));
945 return FALSE;
947 if (menu_file != NULL)
948 menu = g_strdup (menu_file);
949 else
950 menu = g_strdup (edit_widget != NULL ? EDIT_LOCAL_MENU : MC_LOCAL_MENU);
951 if (!exist_file (menu) || !menu_file_own (menu))
953 if (menu_file != NULL)
955 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu,
956 unix_error_string (errno));
957 MC_PTR_FREE (menu);
958 return FALSE;
961 g_free (menu);
962 if (edit_widget != NULL)
963 menu = mc_config_get_full_path (EDIT_HOME_MENU);
964 else
965 menu = mc_config_get_full_path (MC_USERMENU_FILE);
967 if (!exist_file (menu))
969 g_free (menu);
970 menu =
971 mc_build_filename (mc_config_get_home_dir (),
972 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
973 (char *) NULL);
974 if (!exist_file (menu))
976 g_free (menu);
977 menu =
978 mc_build_filename (mc_global.sysconfig_dir,
979 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
980 (char *) NULL);
981 if (!exist_file (menu))
983 g_free (menu);
984 menu =
985 mc_build_filename (mc_global.share_data_dir,
986 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
987 (char *) NULL);
993 if (!g_file_get_contents (menu, &data, NULL, NULL))
995 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu, unix_error_string (errno));
996 MC_PTR_FREE (menu);
997 return FALSE;
1000 max_cols = 0;
1001 selected = 0;
1002 menu_limit = 0;
1003 entries = NULL;
1005 /* Parse the menu file */
1006 old_patterns = easy_patterns;
1007 p = check_patterns (data);
1008 for (menu_lines = col = 0; *p != '\0'; str_next_char (&p))
1010 if (menu_lines >= menu_limit)
1012 char **new_entries;
1014 menu_limit += MAX_ENTRIES;
1015 new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
1016 if (new_entries == NULL)
1017 break;
1019 entries = new_entries;
1020 new_entries += menu_limit;
1021 while (--new_entries >= &entries[menu_lines])
1022 *new_entries = NULL;
1025 if (col == 0 && entries[menu_lines] == NULL)
1026 switch (*p)
1028 case '#':
1029 /* show prompt if first line of external script is #interactive */
1030 if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0)
1031 interactive = FALSE;
1032 /* A commented menu entry */
1033 accept_entry = TRUE;
1034 break;
1036 case '+':
1037 if (*(p + 1) == '=')
1039 /* Combined adding and default */
1040 p = test_line (edit_widget, p + 1, &accept_entry);
1041 if (selected == 0 && accept_entry)
1042 selected = menu_lines;
1044 else
1046 /* A condition for adding the entry */
1047 p = test_line (edit_widget, p, &accept_entry);
1049 break;
1051 case '=':
1052 if (*(p + 1) == '+')
1054 /* Combined adding and default */
1055 p = test_line (edit_widget, p + 1, &accept_entry);
1056 if (selected == 0 && accept_entry)
1057 selected = menu_lines;
1059 else
1061 /* A condition for making the entry default */
1062 i = 1;
1063 p = test_line (edit_widget, p, &i);
1064 if (selected == 0 && i != 0)
1065 selected = menu_lines;
1067 break;
1069 default:
1070 if (!whitespace (*p) && str_isprint (p))
1072 /* A menu entry title line */
1073 if (accept_entry)
1074 entries[menu_lines] = p;
1075 else
1076 accept_entry = TRUE;
1078 break;
1081 if (*p == '\n')
1083 if (entries[menu_lines] != NULL)
1085 menu_lines++;
1086 accept_entry = TRUE;
1088 max_cols = MAX (max_cols, col);
1089 col = 0;
1091 else
1093 if (*p == '\t')
1094 *p = ' ';
1095 col++;
1099 if (menu_lines == 0)
1101 message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu);
1102 res = FALSE;
1104 else
1106 if (selected_entry >= 0)
1107 selected = selected_entry;
1108 else
1110 Listbox *listbox;
1112 max_cols = MIN (MAX (max_cols, col), MAX_ENTRY_LEN);
1114 /* Create listbox */
1115 listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"),
1116 "[Edit Menu File]");
1117 /* insert all the items found */
1118 for (i = 0; i < menu_lines; i++)
1120 p = entries[i];
1121 LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0],
1122 extract_line (p, p + MAX_ENTRY_LEN), p, FALSE);
1124 /* Select the default entry */
1125 listbox_select_entry (listbox->list, selected);
1127 selected = run_listbox (listbox);
1129 if (selected >= 0)
1131 execute_menu_command (edit_widget, entries[selected], interactive);
1132 res = TRUE;
1135 do_refresh ();
1138 easy_patterns = old_patterns;
1139 MC_PTR_FREE (menu);
1140 g_free (entries);
1141 g_free (data);
1142 return res;
1145 /* --------------------------------------------------------------------------------------------- */