Cleanup: usermenu.c: use const edit_widget.
[midnight-commander.git] / src / filemanager / usermenu.c
blob5ba3917a3a3f12280100f645cf966534f4a8f7a4
1 /*
2 User Menu implementation
4 Copyright (C) 1994-2016
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/tty/tty.h"
41 #include "lib/skin.h"
42 #include "lib/search.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/strutil.h"
45 #include "lib/util.h"
46 #include "lib/widget.h"
48 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
49 #include "src/viewer/mcviewer.h" /* for default_* externs */
51 #include "src/execute.h"
52 #include "src/setup.h"
53 #include "src/history.h"
55 #include "dir.h"
56 #include "midnight.h"
57 #include "layout.h"
59 #include "usermenu.h"
61 /*** global variables ****************************************************************************/
63 /*** file scope macro definitions ****************************************************************/
65 #define MAX_ENTRIES 16
66 #define MAX_ENTRY_LEN 60
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 static gboolean debug_flag = FALSE;
73 static gboolean debug_error = FALSE;
74 static char *menu = NULL;
76 /*** file scope functions ************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
79 /** strip file's extension */
80 static char *
81 strip_ext (char *ss)
83 char *s = ss;
84 char *e = NULL;
86 while (*s != '\0')
88 if (*s == '.')
89 e = s;
90 if (IS_PATH_SEP (*s) && e != NULL)
91 e = NULL; /* '.' in *directory* name */
92 s++;
94 if (e != NULL)
95 *e = '\0';
96 return ss;
99 /* --------------------------------------------------------------------------------------------- */
101 * Check for the "shell_patterns" directive. If it's found and valid,
102 * interpret it and move the pointer past the directive. Return the
103 * current pointer.
106 static char *
107 check_patterns (char *p)
109 static const char def_name[] = "shell_patterns=";
110 char *p0 = p;
112 if (strncmp (p, def_name, sizeof (def_name) - 1) != 0)
113 return p0;
115 p += sizeof (def_name) - 1;
116 if (*p == '1')
117 easy_patterns = 1;
118 else if (*p == '0')
119 easy_patterns = 0;
120 else
121 return p0;
123 /* Skip spaces */
124 p++;
125 while (*p == '\n' || *p == '\t' || *p == ' ')
126 p++;
127 return p;
130 /* --------------------------------------------------------------------------------------------- */
131 /** Copies a whitespace separated argument from p to arg. Returns the
132 point after argument. */
134 static char *
135 extract_arg (char *p, char *arg, int size)
137 while (*p != '\0' && (*p == ' ' || *p == '\t' || *p == '\n'))
138 p++;
140 /* support quote space .mnu */
141 while (*p != '\0' && (*p != ' ' || *(p - 1) == '\\') && *p != '\t' && *p != '\n')
143 char *np;
145 np = str_get_next_char (p);
146 if (np - p >= size)
147 break;
148 memcpy (arg, p, np - p);
149 arg += np - p;
150 size -= np - p;
151 p = np;
153 *arg = '\0';
154 if (*p == '\0' || *p == '\n')
155 str_prev_char (&p);
156 return p;
159 /* --------------------------------------------------------------------------------------------- */
160 /* Tests whether the selected file in the panel is of any of the types
161 specified in argument. */
163 static gboolean
164 test_type (WPanel * panel, char *arg)
166 int result = 0; /* False by default */
167 mode_t st_mode = panel->dir.list[panel->selected].st.st_mode;
169 for (; *arg != '\0'; arg++)
171 switch (*arg)
173 case 'n': /* Not a directory */
174 result |= !S_ISDIR (st_mode);
175 break;
176 case 'r': /* Regular file */
177 result |= S_ISREG (st_mode);
178 break;
179 case 'd': /* Directory */
180 result |= S_ISDIR (st_mode);
181 break;
182 case 'l': /* Link */
183 result |= S_ISLNK (st_mode);
184 break;
185 case 'c': /* Character special */
186 result |= S_ISCHR (st_mode);
187 break;
188 case 'b': /* Block special */
189 result |= S_ISBLK (st_mode);
190 break;
191 case 'f': /* Fifo (named pipe) */
192 result |= S_ISFIFO (st_mode);
193 break;
194 case 's': /* Socket */
195 result |= S_ISSOCK (st_mode);
196 break;
197 case 'x': /* Executable */
198 result |= (st_mode & 0111) != 0 ? 1 : 0;
199 break;
200 case 't':
201 result |= panel->marked != 0 ? 1 : 0;
202 break;
203 default:
204 debug_error = TRUE;
205 break;
209 return (result != 0);
212 /* --------------------------------------------------------------------------------------------- */
213 /** Calculates the truth value of the next condition starting from
214 p. Returns the point after condition. */
216 static char *
217 test_condition (const WEdit * edit_widget, char *p, gboolean * condition)
219 char arg[256];
220 const mc_search_type_t search_type = easy_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
222 /* Handle one condition */
223 for (; *p != '\n' && *p != '&' && *p != '|'; p++)
225 WPanel *panel = NULL;
227 /* support quote space .mnu */
228 if ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
229 continue;
230 if (*p >= 'a')
231 panel = current_panel;
232 else if (get_other_type () == view_listing)
233 panel = other_panel;
235 *p |= 0x20;
237 switch (*p++)
239 case '!':
240 p = test_condition (edit_widget, p, condition);
241 *condition = !*condition;
242 str_prev_char (&p);
243 break;
244 case 'f': /* file name pattern */
245 p = extract_arg (p, arg, sizeof (arg));
246 #ifdef USE_INTERNAL_EDIT
247 if (edit_widget != NULL)
249 char *edit_filename;
251 edit_filename = edit_get_file_name (edit_widget);
252 *condition = mc_search (arg, DEFAULT_CHARSET, edit_filename, search_type);
253 g_free (edit_filename);
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;
451 cmd_file = fdopen (cmd_file_fd, "w");
452 fputs ("#! /bin/sh\n", cmd_file);
453 commands++;
455 for (col = 0; *commands != '\0'; commands++)
457 if (col == 0)
459 if (*commands != ' ' && *commands != '\t')
460 break;
461 while (*commands == ' ' || *commands == '\t')
462 commands++;
463 if (*commands == '\0')
464 break;
466 col++;
467 if (*commands == '\n')
468 col = 0;
469 if (parameter != NULL)
471 if (*commands == '}')
473 *parameter = '\0';
474 parameter =
475 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "",
476 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD |
477 INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES |
478 INPUT_COMPLETE_USERNAMES);
479 if (parameter == NULL || *parameter == '\0')
481 /* User canceled */
482 fclose (cmd_file);
483 mc_unlink (file_name_vpath);
484 vfs_path_free (file_name_vpath);
485 return;
487 if (do_quote)
489 char *tmp;
491 tmp = name_quote (parameter, FALSE);
492 fputs (tmp, cmd_file);
493 g_free (tmp);
495 else
496 fputs (parameter, cmd_file);
498 MC_PTR_FREE (parameter);
500 else if (parameter < lc_prompt + sizeof (lc_prompt) - 1)
501 *parameter++ = *commands;
503 else if (expand_prefix_found)
505 expand_prefix_found = FALSE;
506 if (g_ascii_isdigit ((gchar) * commands))
508 do_quote = (atoi (commands) != 0);
509 while (g_ascii_isdigit ((gchar) * commands))
510 commands++;
512 if (*commands == '{')
513 parameter = lc_prompt;
514 else
516 char *text;
518 text = expand_format (edit_widget, *commands, do_quote);
519 fputs (text, cmd_file);
520 g_free (text);
523 else
525 if (*commands == '%')
527 int i;
529 i = check_format_view (commands + 1);
530 if (i != 0)
532 commands += i;
533 run_view = TRUE;
535 else
537 do_quote = TRUE; /* Default: Quote expanded macro */
538 expand_prefix_found = TRUE;
541 else
542 fputc (*commands, cmd_file);
545 fclose (cmd_file);
546 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);
559 if (!show_prompt)
561 if (system (cmd) == -1)
562 message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
564 else
566 shell_execute (cmd, EXECUTE_HIDE);
568 g_free (cmd);
570 mc_unlink (file_name_vpath);
571 vfs_path_free (file_name_vpath);
574 /* --------------------------------------------------------------------------------------------- */
576 ** Check owner of the menu file. Using menu file is allowed, if
577 ** owner of the menu is root or the actual user. In either case
578 ** file should not be group and word-writable.
580 ** Q. Should we apply this routine to system and home menu (and .ext files)?
583 static gboolean
584 menu_file_own (char *path)
586 struct stat st;
588 if (stat (path, &st) == 0 && (st.st_uid == 0 || (st.st_uid == geteuid ()) != 0)
589 && ((st.st_mode & (S_IWGRP | S_IWOTH)) == 0))
590 return TRUE;
592 if (verbose)
593 message (D_NORMAL, _("Warning -- ignoring file"),
594 _("File %s is not owned by root or you or is world writable.\n"
595 "Using it may compromise your security"), path);
597 return FALSE;
600 /* --------------------------------------------------------------------------------------------- */
601 /*** public functions ****************************************************************************/
602 /* --------------------------------------------------------------------------------------------- */
604 /* Formats defined:
605 %% The % character
606 %f The current file (if non-local vfs, file will be copied locally and
607 %f will be full path to it).
608 %p The current file
609 %d The current working directory
610 %s "Selected files"; the tagged files if any, otherwise the current file
611 %t Tagged files
612 %u Tagged files (and they are untagged on return from expand_format)
613 %view Runs the commands and pipes standard output to the view command.
614 If %view is immediately followed by '{', recognize keywords
615 ascii, hex, nroff and unform
617 If the format letter is in uppercase, it refers to the other panel.
619 With a number followed the % character you can turn quoting on (default)
620 and off. For example:
621 %f quote expanded macro
622 %1f ditto
623 %0f don't quote expanded macro
625 expand_format returns a memory block that must be free()d.
628 /* Returns how many characters we should advance if %view was found */
630 check_format_view (const char *p)
632 const char *q = p;
634 if (strncmp (p, "view", 4) == 0)
636 q += 4;
637 if (*q == '{')
639 for (q++; *q != '\0' && *q != '}'; q++)
641 if (strncmp (q, DEFAULT_CHARSET, 5) == 0)
643 mcview_default_hex_mode = 0;
644 q += 4;
646 else if (strncmp (q, "hex", 3) == 0)
648 mcview_default_hex_mode = 1;
649 q += 2;
651 else if (strncmp (q, "nroff", 5) == 0)
653 mcview_default_nroff_flag = 1;
654 q += 4;
656 else if (strncmp (q, "unform", 6) == 0)
658 mcview_default_nroff_flag = 0;
659 q += 5;
662 if (*q == '}')
663 q++;
665 return q - p;
667 return 0;
670 /* --------------------------------------------------------------------------------------------- */
673 check_format_cd (const char *p)
675 return (strncmp (p, "cd", 2)) != 0 ? 0 : 3;
678 /* --------------------------------------------------------------------------------------------- */
679 /* Check if p has a "^var\{var-name\}" */
680 /* Returns the number of skipped characters (zero on not found) */
681 /* V will be set to the expanded variable name */
684 check_format_var (const char *p, char **v)
686 const char *q = p;
687 char *var_name;
689 *v = NULL;
690 if (strncmp (p, "var{", 4) == 0)
692 const char *dots = NULL;
693 const char *value;
695 for (q += 4; *q != '\0' && *q != '}'; q++)
697 if (*q == ':')
698 dots = q + 1;
700 if (*q == '\0')
701 return 0;
703 if (dots == NULL || dots == q + 5)
705 message (D_ERROR,
706 _("Format error on file Extensions File"),
707 !dots ? _("The %%var macro has no default")
708 : _("The %%var macro has no variable"));
709 return 0;
712 /* Copy the variable name */
713 var_name = g_strndup (p + 4, dots - 2 - (p + 3));
715 value = getenv (var_name);
716 g_free (var_name);
717 if (value != NULL)
719 *v = g_strdup (value);
720 return q - p;
722 var_name = g_strndup (dots, q - dots);
723 *v = var_name;
724 return q - p;
726 return 0;
729 /* --------------------------------------------------------------------------------------------- */
731 char *
732 expand_format (const struct WEdit *edit_widget, char c, gboolean do_quote)
734 WPanel *panel = NULL;
735 char *(*quote_func) (const char *, gboolean);
736 char *fname = NULL;
737 char *result;
738 char c_lc;
740 #ifndef USE_INTERNAL_EDIT
741 (void) edit_widget;
742 #endif
744 if (c == '%')
745 return g_strdup ("%");
747 switch (mc_global.mc_run_mode)
749 case MC_RUN_FULL:
750 if (g_ascii_islower ((gchar) c))
751 panel = current_panel;
752 else
754 if (get_other_type () != view_listing)
755 return g_strdup ("");
756 panel = other_panel;
758 fname = g_strdup (panel->dir.list[panel->selected].fname);
759 break;
761 #ifdef USE_INTERNAL_EDIT
762 case MC_RUN_EDITOR:
763 fname = edit_get_file_name (edit_widget);
764 break;
765 #endif
767 default:
768 /* other modes don't use formats */
769 return g_strdup ("");
772 if (do_quote)
773 quote_func = name_quote;
774 else
775 quote_func = fake_name_quote;
777 c_lc = g_ascii_tolower ((gchar) c);
779 switch (c_lc)
781 case 'f':
782 case 'p':
783 result = quote_func (fname, FALSE);
784 goto ret;
785 case 'x':
786 result = quote_func (extension (fname), FALSE);
787 goto ret;
788 case 'd':
790 const char *cwd;
791 char *qstr;
793 if (panel != NULL)
794 cwd = vfs_path_as_str (panel->cwd_vpath);
795 else
796 cwd = vfs_get_current_dir ();
798 qstr = quote_func (cwd, FALSE);
800 result = qstr;
801 goto ret;
803 case 'i': /* indent equal number cursor position in line */
804 #ifdef USE_INTERNAL_EDIT
805 if (edit_widget != NULL)
807 result = g_strnfill (edit_get_curs_col (edit_widget), ' ');
808 goto ret;
810 #endif
811 break;
812 case 'y': /* syntax type */
813 #ifdef USE_INTERNAL_EDIT
814 if (edit_widget != NULL)
816 const char *syntax_type;
818 syntax_type = edit_get_syntax_type (edit_widget);
819 if (syntax_type != NULL)
821 result = g_strdup (syntax_type);
822 goto ret;
825 #endif
826 break;
827 case 'k': /* block file name */
828 case 'b': /* block file name / strip extension */
829 #ifdef USE_INTERNAL_EDIT
830 if (edit_widget != NULL)
832 char *file;
834 file = mc_config_get_full_path (EDIT_BLOCK_FILE);
835 result = quote_func (file, FALSE);
836 g_free (file);
837 goto ret;
839 #endif
840 if (c_lc == 'b')
842 result = strip_ext (quote_func (fname, FALSE));
843 goto ret;
845 break;
846 case 'n': /* strip extension in editor */
847 #ifdef USE_INTERNAL_EDIT
848 if (edit_widget != NULL)
850 result = strip_ext (quote_func (fname, FALSE));
851 goto ret;
853 #endif
854 break;
855 case 'm': /* menu file name */
856 if (menu != NULL)
858 result = quote_func (menu, FALSE);
859 goto ret;
861 break;
862 case 's':
863 if (panel == NULL || panel->marked == 0)
865 result = quote_func (fname, FALSE);
866 goto ret;
869 /* Fall through */
871 case 't':
872 case 'u':
874 GString *block;
875 int i;
877 if (panel == NULL)
879 result = g_strdup ("");
880 goto ret;
883 block = g_string_sized_new (16);
885 for (i = 0; i < panel->dir.len; i++)
886 if (panel->dir.list[i].f.marked)
888 char *tmp;
890 tmp = quote_func (panel->dir.list[i].fname, FALSE);
891 g_string_append (block, tmp);
892 g_string_append_c (block, ' ');
893 g_free (tmp);
895 if (c_lc == 'u')
896 do_file_mark (panel, i, 0);
898 result = g_string_free (block, FALSE);
899 goto ret;
900 } /* sub case block */
901 default:
902 break;
903 } /* switch */
905 result = g_strdup ("% ");
906 result[1] = c;
907 ret:
908 g_free (fname);
909 return result;
912 /* --------------------------------------------------------------------------------------------- */
914 * If edit_widget is NULL then we are called from the mc menu,
915 * otherwise we are called from the mcedit menu.
918 gboolean
919 user_menu_cmd (const struct WEdit * edit_widget, const char *menu_file, int selected_entry)
921 char *p;
922 char *data, **entries;
923 int max_cols, menu_lines, menu_limit;
924 int col, i;
925 gboolean accept_entry = TRUE;
926 int selected, old_patterns;
927 gboolean res = FALSE;
928 gboolean interactive = TRUE;
930 if (!vfs_current_is_local ())
932 message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems"));
933 return FALSE;
935 if (menu_file != NULL)
936 menu = g_strdup (menu_file);
937 else
938 menu = g_strdup (edit_widget != NULL ? EDIT_LOCAL_MENU : MC_LOCAL_MENU);
939 if (!exist_file (menu) || !menu_file_own (menu))
941 if (menu_file != NULL)
943 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu,
944 unix_error_string (errno));
945 MC_PTR_FREE (menu);
946 return FALSE;
949 g_free (menu);
950 if (edit_widget != NULL)
951 menu = mc_config_get_full_path (EDIT_HOME_MENU);
952 else
953 menu = mc_config_get_full_path (MC_USERMENU_FILE);
956 if (!exist_file (menu))
958 g_free (menu);
959 menu =
960 mc_build_filename (mc_config_get_home_dir (),
961 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL);
962 if (!exist_file (menu))
964 g_free (menu);
965 menu =
966 mc_build_filename (mc_global.sysconfig_dir,
967 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
968 NULL);
969 if (!exist_file (menu))
971 g_free (menu);
972 menu =
973 mc_build_filename (mc_global.share_data_dir,
974 edit_widget != NULL ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
975 NULL);
981 if (!g_file_get_contents (menu, &data, NULL, NULL))
983 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu, unix_error_string (errno));
984 MC_PTR_FREE (menu);
985 return FALSE;
988 max_cols = 0;
989 selected = 0;
990 menu_limit = 0;
991 entries = NULL;
993 /* Parse the menu file */
994 old_patterns = easy_patterns;
995 p = check_patterns (data);
996 for (menu_lines = col = 0; *p != '\0'; str_next_char (&p))
998 if (menu_lines >= menu_limit)
1000 char **new_entries;
1002 menu_limit += MAX_ENTRIES;
1003 new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
1005 if (new_entries == NULL)
1006 break;
1008 entries = new_entries;
1009 new_entries += menu_limit;
1010 while (--new_entries >= &entries[menu_lines])
1011 *new_entries = NULL;
1013 if (col == 0 && entries[menu_lines] == NULL)
1015 if (*p == '#')
1017 /* show prompt if first line of external script is #interactive */
1018 if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0)
1019 interactive = FALSE;
1020 /* A commented menu entry */
1021 accept_entry = TRUE;
1023 else if (*p == '+')
1025 if (*(p + 1) == '=')
1027 /* Combined adding and default */
1028 p = test_line (edit_widget, p + 1, &accept_entry);
1029 if (selected == 0 && accept_entry)
1030 selected = menu_lines;
1032 else
1034 /* A condition for adding the entry */
1035 p = test_line (edit_widget, p, &accept_entry);
1038 else if (*p == '=')
1040 if (*(p + 1) == '+')
1042 /* Combined adding and default */
1043 p = test_line (edit_widget, p + 1, &accept_entry);
1044 if (selected == 0 && accept_entry)
1045 selected = menu_lines;
1047 else
1049 /* A condition for making the entry default */
1050 i = 1;
1051 p = test_line (edit_widget, p, &i);
1052 if (selected == 0 && i != 0)
1053 selected = menu_lines;
1056 else if (*p != ' ' && *p != '\t' && str_isprint (p))
1058 /* A menu entry title line */
1059 if (accept_entry)
1060 entries[menu_lines] = p;
1061 else
1062 accept_entry = TRUE;
1065 if (*p == '\n')
1067 if (entries[menu_lines] != NULL)
1069 menu_lines++;
1070 accept_entry = TRUE;
1072 max_cols = max (max_cols, col);
1073 col = 0;
1075 else
1077 if (*p == '\t')
1078 *p = ' ';
1079 col++;
1083 if (menu_lines == 0)
1085 message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu);
1086 res = FALSE;
1088 else
1090 if (selected_entry >= 0)
1091 selected = selected_entry;
1092 else
1094 Listbox *listbox;
1096 max_cols = min (max (max_cols, col), MAX_ENTRY_LEN);
1098 /* Create listbox */
1099 listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"),
1100 "[Edit Menu File]");
1101 /* insert all the items found */
1102 for (i = 0; i < menu_lines; i++)
1104 p = entries[i];
1105 LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0],
1106 extract_line (p, p + MAX_ENTRY_LEN), p, FALSE);
1108 /* Select the default entry */
1109 listbox_select_entry (listbox->list, selected);
1111 selected = run_listbox (listbox);
1113 if (selected >= 0)
1115 execute_menu_command (edit_widget, entries[selected], interactive);
1116 res = TRUE;
1119 do_refresh ();
1122 easy_patterns = old_patterns;
1123 MC_PTR_FREE (menu);
1124 g_free (entries);
1125 g_free (data);
1126 return res;
1129 /* --------------------------------------------------------------------------------------------- */