cppcheck: reduce variable scope.
[midnight-commander.git] / src / filemanager / usermenu.c
blobb4a818b3becef160965a71711868dbca2d48821b
1 /*
2 User Menu implementation
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2011, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file usermenu.c
29 * \brief Source: user menu implementation
32 #include <config.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
40 #include "lib/global.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 "dir.h"
57 #include "midnight.h"
58 #include "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 int debug_flag = 0;
74 static int debug_error = 0;
75 static char *menu = NULL;
77 /*** file scope functions ************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
80 /** strip file's extension */
81 static char *
82 strip_ext (char *ss)
84 register char *s = ss;
85 char *e = NULL;
86 while (*s)
88 if (*s == '.')
89 e = s;
90 if (*s == PATH_SEP && e)
91 e = NULL; /* '.' in *directory* name */
92 s++;
94 if (e)
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 char *np;
139 while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
140 p++;
141 /* support quote space .mnu */
142 while (*p && (*p != ' ' || *(p - 1) == '\\') && *p != '\t' && *p != '\n')
144 np = str_get_next_char (p);
145 if (np - p >= size)
146 break;
147 memcpy (arg, p, np - p);
148 arg += np - p;
149 size -= np - p;
150 p = np;
152 *arg = 0;
153 if (!*p || *p == '\n')
154 str_prev_char (&p);
155 return p;
158 /* --------------------------------------------------------------------------------------------- */
159 /* Tests whether the selected file in the panel is of any of the types
160 specified in argument. */
162 static int
163 test_type (WPanel * panel, char *arg)
165 int result = 0; /* False by default */
166 int st_mode = panel->dir.list[panel->selected].st.st_mode;
168 for (; *arg != 0; arg++)
170 switch (*arg)
172 case 'n': /* Not a directory */
173 result |= !S_ISDIR (st_mode);
174 break;
175 case 'r': /* Regular file */
176 result |= S_ISREG (st_mode);
177 break;
178 case 'd': /* Directory */
179 result |= S_ISDIR (st_mode);
180 break;
181 case 'l': /* Link */
182 result |= S_ISLNK (st_mode);
183 break;
184 case 'c': /* Character special */
185 result |= S_ISCHR (st_mode);
186 break;
187 case 'b': /* Block special */
188 result |= S_ISBLK (st_mode);
189 break;
190 case 'f': /* Fifo (named pipe) */
191 result |= S_ISFIFO (st_mode);
192 break;
193 case 's': /* Socket */
194 result |= S_ISSOCK (st_mode);
195 break;
196 case 'x': /* Executable */
197 result |= (st_mode & 0111) ? 1 : 0;
198 break;
199 case 't':
200 result |= panel->marked ? 1 : 0;
201 break;
202 default:
203 debug_error = 1;
204 break;
207 return result;
210 /* --------------------------------------------------------------------------------------------- */
211 /** Calculates the truth value of the next condition starting from
212 p. Returns the point after condition. */
214 static char *
215 test_condition (WEdit * edit_widget, char *p, int *condition)
217 char arg[256];
218 const mc_search_type_t search_type = easy_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
220 /* Handle one condition */
221 for (; *p != '\n' && *p != '&' && *p != '|'; p++)
223 WPanel *panel = NULL;
225 /* support quote space .mnu */
226 if ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
227 continue;
228 if (*p >= 'a')
229 panel = current_panel;
230 else if (get_other_type () == view_listing)
231 panel = other_panel;
233 *p |= 0x20;
235 switch (*p++)
237 case '!':
238 p = test_condition (edit_widget, p, condition);
239 *condition = !*condition;
240 str_prev_char (&p);
241 break;
242 case 'f': /* file name pattern */
243 p = extract_arg (p, arg, sizeof (arg));
244 #ifdef USE_INTERNAL_EDIT
245 if (edit_widget != NULL)
247 char *edit_filename;
249 edit_filename = edit_get_file_name (edit_widget);
250 *condition = mc_search (arg, DEFAULT_CHARSET, edit_filename, search_type) ? 1 : 0;
251 g_free (edit_filename);
253 else
254 #endif
255 *condition = panel != NULL &&
256 mc_search (arg, DEFAULT_CHARSET, panel->dir.list[panel->selected].fname,
257 search_type) ? 1 : 0;
258 break;
259 case 'y': /* syntax pattern */
260 #ifdef USE_INTERNAL_EDIT
261 if (edit_widget != NULL)
263 const char *syntax_type = edit_get_syntax_type (edit_widget);
264 if (syntax_type != NULL)
266 p = extract_arg (p, arg, sizeof (arg));
267 *condition =
268 mc_search (arg, DEFAULT_CHARSET, syntax_type, MC_SEARCH_T_NORMAL) ? 1 : 0;
271 #endif
272 break;
273 case 'd':
274 p = extract_arg (p, arg, sizeof (arg));
275 *condition = panel != NULL
276 && mc_search (arg, DEFAULT_CHARSET, vfs_path_as_str (panel->cwd_vpath),
277 search_type) ? 1 : 0;
278 break;
279 case 't':
280 p = extract_arg (p, arg, sizeof (arg));
281 *condition = panel != NULL && test_type (panel, arg) ? 1 : 0;
282 break;
283 case 'x': /* executable */
285 struct stat status;
287 p = extract_arg (p, arg, sizeof (arg));
288 if (stat (arg, &status) == 0)
289 *condition = is_exe (status.st_mode) ? 1 : 0;
290 else
291 *condition = 0;
292 break;
294 default:
295 debug_error = 1;
296 break;
297 } /* switch */
299 } /* while */
300 return p;
303 /* --------------------------------------------------------------------------------------------- */
304 /** General purpose condition debug output handler */
306 static void
307 debug_out (char *start, char *end, int cond)
309 static char *msg;
311 if (start == NULL && end == NULL)
313 /* Show output */
314 if (debug_flag && msg)
316 size_t len;
318 len = strlen (msg);
319 if (len != 0)
320 msg[len - 1] = 0;
321 message (D_NORMAL, _("Debug"), "%s", msg);
324 debug_flag = 0;
325 g_free (msg);
326 msg = NULL;
328 else
330 const char *type;
331 char *p;
333 /* Save debug info for later output */
334 if (!debug_flag)
335 return;
336 /* Save the result of the condition */
337 if (debug_error)
339 type = _("ERROR:");
340 debug_error = 0;
342 else if (cond)
343 type = _("True:");
344 else
345 type = _("False:");
346 /* This is for debugging, don't need to be super efficient. */
347 if (end == NULL)
348 p = g_strdup_printf ("%s %s %c \n", msg ? msg : "", type, *start);
349 else
350 p = g_strdup_printf ("%s %s %.*s \n", msg ? msg : "", type, (int) (end - start), start);
351 g_free (msg);
352 msg = p;
356 /* --------------------------------------------------------------------------------------------- */
357 /** Calculates the truth value of one lineful of conditions. Returns
358 the point just before the end of line. */
360 static char *
361 test_line (WEdit * edit_widget, char *p, int *result)
363 int condition;
364 char operator;
366 /* Repeat till end of line */
367 while (*p && *p != '\n')
369 char *debug_start, *debug_end;
371 /* support quote space .mnu */
372 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
373 p++;
374 if (!*p || *p == '\n')
375 break;
376 operator = *p++;
377 if (*p == '?')
379 debug_flag = 1;
380 p++;
382 /* support quote space .mnu */
383 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
384 p++;
385 if (!*p || *p == '\n')
386 break;
387 condition = 1; /* True by default */
389 debug_start = p;
390 p = test_condition (edit_widget, p, &condition);
391 debug_end = p;
392 /* Add one debug statement */
393 debug_out (debug_start, debug_end, condition);
395 switch (operator)
397 case '+':
398 case '=':
399 /* Assignment */
400 *result = condition;
401 break;
402 case '&': /* Logical and */
403 *result &= condition;
404 break;
405 case '|': /* Logical or */
406 *result |= condition;
407 break;
408 default:
409 debug_error = 1;
410 break;
411 } /* switch */
412 /* Add one debug statement */
413 debug_out (&operator, NULL, *result);
415 } /* while (*p != '\n') */
416 /* Report debug message */
417 debug_out (NULL, NULL, 1);
419 if (!*p || *p == '\n')
420 str_prev_char (&p);
421 return p;
424 /* --------------------------------------------------------------------------------------------- */
425 /** FIXME: recode this routine on version 3.0, it could be cleaner */
427 static void
428 execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_prompt)
430 FILE *cmd_file;
431 int cmd_file_fd;
432 int expand_prefix_found = 0;
433 char *parameter = 0;
434 gboolean do_quote = FALSE;
435 char lc_prompt[80];
436 int col;
437 vfs_path_t *file_name_vpath;
438 int run_view = 0;
440 /* Skip menu entry title line */
441 commands = strchr (commands, '\n');
442 if (!commands)
444 return;
447 cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcusr", SCRIPT_SUFFIX);
449 if (cmd_file_fd == -1)
451 message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"),
452 unix_error_string (errno));
453 vfs_path_free (file_name_vpath);
454 return;
456 cmd_file = fdopen (cmd_file_fd, "w");
457 fputs ("#! /bin/sh\n", cmd_file);
458 commands++;
460 for (col = 0; *commands; commands++)
462 if (col == 0)
464 if (*commands != ' ' && *commands != '\t')
465 break;
466 while (*commands == ' ' || *commands == '\t')
467 commands++;
468 if (*commands == 0)
469 break;
471 col++;
472 if (*commands == '\n')
473 col = 0;
474 if (parameter)
476 if (*commands == '}')
478 *parameter = 0;
479 parameter =
480 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "",
481 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD |
482 INPUT_COMPLETE_HOSTNAMES | INPUT_COMPLETE_VARIABLES |
483 INPUT_COMPLETE_USERNAMES);
484 if (!parameter || !*parameter)
486 /* User canceled */
487 fclose (cmd_file);
488 mc_unlink (file_name_vpath);
489 vfs_path_free (file_name_vpath);
490 return;
492 if (do_quote)
494 char *tmp;
496 tmp = name_quote (parameter, 0);
497 fputs (tmp, cmd_file);
498 g_free (tmp);
500 else
501 fputs (parameter, cmd_file);
502 g_free (parameter);
503 parameter = 0;
505 else
507 if (parameter < &lc_prompt[sizeof (lc_prompt) - 1])
509 *parameter++ = *commands;
513 else if (expand_prefix_found)
515 expand_prefix_found = 0;
516 if (g_ascii_isdigit ((gchar) * commands))
518 do_quote = (atoi (commands) != 0);
519 while (g_ascii_isdigit ((gchar) * commands))
520 commands++;
522 if (*commands == '{')
523 parameter = lc_prompt;
524 else
526 char *text = expand_format (edit_widget, *commands, do_quote);
527 fputs (text, cmd_file);
528 g_free (text);
531 else
533 if (*commands == '%')
535 int i = check_format_view (commands + 1);
536 if (i)
538 commands += i;
539 run_view = 1;
541 else
543 do_quote = TRUE; /* Default: Quote expanded macro */
544 expand_prefix_found = 1;
547 else
548 fputc (*commands, cmd_file);
551 fclose (cmd_file);
552 mc_chmod (file_name_vpath, S_IRWXU);
553 if (run_view)
555 mcview_viewer (vfs_path_as_str (file_name_vpath), NULL, 0);
556 dialog_switch_process_pending ();
558 else
560 /* execute the command indirectly to allow execution even
561 * on no-exec filesystems. */
562 char *cmd;
564 cmd = g_strconcat ("/bin/sh ", vfs_path_as_str (file_name_vpath), (char *) NULL);
565 if (!show_prompt)
567 if (system (cmd) == -1)
568 message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
570 else
572 shell_execute (cmd, EXECUTE_HIDE);
574 g_free (cmd);
576 mc_unlink (file_name_vpath);
577 vfs_path_free (file_name_vpath);
580 /* --------------------------------------------------------------------------------------------- */
582 ** Check owner of the menu file. Using menu file is allowed, if
583 ** owner of the menu is root or the actual user. In either case
584 ** file should not be group and word-writable.
586 ** Q. Should we apply this routine to system and home menu (and .ext files)?
589 static int
590 menu_file_own (char *path)
592 struct stat st;
594 if (stat (path, &st) == 0
595 && (!st.st_uid || (st.st_uid == geteuid ())) && ((st.st_mode & (S_IWGRP | S_IWOTH)) == 0))
597 return 1;
599 if (verbose)
601 message (D_NORMAL, _("Warning -- ignoring file"),
602 _("File %s is not owned by root or you or is world writable.\n"
603 "Using it may compromise your security"), path);
605 return 0;
608 /* --------------------------------------------------------------------------------------------- */
609 /*** public functions ****************************************************************************/
610 /* --------------------------------------------------------------------------------------------- */
612 /* Formats defined:
613 %% The % character
614 %f The current file (if non-local vfs, file will be copied locally and
615 %f will be full path to it).
616 %p The current file
617 %d The current working directory
618 %s "Selected files"; the tagged files if any, otherwise the current file
619 %t Tagged files
620 %u Tagged files (and they are untagged on return from expand_format)
621 %view Runs the commands and pipes standard output to the view command.
622 If %view is immediately followed by '{', recognize keywords
623 ascii, hex, nroff and unform
625 If the format letter is in uppercase, it refers to the other panel.
627 With a number followed the % character you can turn quoting on (default)
628 and off. For example:
629 %f quote expanded macro
630 %1f ditto
631 %0f don't quote expanded macro
633 expand_format returns a memory block that must be free()d.
636 /* Returns how many characters we should advance if %view was found */
638 check_format_view (const char *p)
640 const char *q = p;
641 if (!strncmp (p, "view", 4))
643 q += 4;
644 if (*q == '{')
646 for (q++; *q && *q != '}'; q++)
648 if (!strncmp (q, DEFAULT_CHARSET, 5))
650 mcview_default_hex_mode = 0;
651 q += 4;
653 else if (!strncmp (q, "hex", 3))
655 mcview_default_hex_mode = 1;
656 q += 2;
658 else if (!strncmp (q, "nroff", 5))
660 mcview_default_nroff_flag = 1;
661 q += 4;
663 else if (!strncmp (q, "unform", 6))
665 mcview_default_nroff_flag = 0;
666 q += 5;
669 if (*q == '}')
670 q++;
672 return q - p;
674 return 0;
677 /* --------------------------------------------------------------------------------------------- */
680 check_format_cd (const char *p)
682 return (strncmp (p, "cd", 2)) ? 0 : 3;
685 /* --------------------------------------------------------------------------------------------- */
686 /* Check if p has a "^var\{var-name\}" */
687 /* Returns the number of skipped characters (zero on not found) */
688 /* V will be set to the expanded variable name */
691 check_format_var (const char *p, char **v)
693 const char *q = p;
694 char *var_name;
696 *v = NULL;
697 if (!strncmp (p, "var{", 4))
699 const char *dots = NULL;
700 const char *value;
702 for (q += 4; *q && *q != '}'; q++)
704 if (*q == ':')
705 dots = q + 1;
707 if (!*q)
708 return 0;
710 if (!dots || dots == q + 5)
712 message (D_ERROR,
713 _("Format error on file Extensions File"),
714 !dots ? _("The %%var macro has no default")
715 : _("The %%var macro has no variable"));
716 return 0;
719 /* Copy the variable name */
720 var_name = g_strndup (p + 4, dots - 2 - (p + 3));
722 value = getenv (var_name);
723 g_free (var_name);
724 if (value)
726 *v = g_strdup (value);
727 return q - p;
729 var_name = g_strndup (dots, q - dots);
730 *v = var_name;
731 return q - p;
733 return 0;
736 /* --------------------------------------------------------------------------------------------- */
738 char *
739 expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
741 WPanel *panel = NULL;
742 char *(*quote_func) (const char *, int);
743 char *fname = NULL;
744 char *result;
745 char c_lc;
747 #ifndef USE_INTERNAL_EDIT
748 (void) edit_widget;
749 #endif
751 if (c == '%')
752 return g_strdup ("%");
754 switch (mc_global.mc_run_mode)
756 case MC_RUN_FULL:
757 if (g_ascii_islower ((gchar) c))
758 panel = current_panel;
759 else
761 if (get_other_type () != view_listing)
762 return g_strdup ("");
763 panel = other_panel;
765 fname = g_strdup (panel->dir.list[panel->selected].fname);
766 break;
768 #ifdef USE_INTERNAL_EDIT
769 case MC_RUN_EDITOR:
770 fname = edit_get_file_name (edit_widget);
771 break;
772 #endif
774 default:
775 /* other modes don't use formats */
776 return g_strdup ("");
779 if (do_quote)
780 quote_func = name_quote;
781 else
782 quote_func = fake_name_quote;
784 c_lc = g_ascii_tolower ((gchar) c);
786 switch (c_lc)
788 case 'f':
789 case 'p':
790 result = (*quote_func) (fname, 0);
791 goto ret;
792 case 'x':
793 result = (*quote_func) (extension (fname), 0);
794 goto ret;
795 case 'd':
797 char *cwd;
798 char *qstr;
800 if (panel)
801 cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
802 else
803 cwd = vfs_get_current_dir ();
805 qstr = (*quote_func) (cwd, 0);
807 g_free (cwd);
809 result = qstr;
810 goto ret;
812 case 'i': /* indent equal number cursor position in line */
813 #ifdef USE_INTERNAL_EDIT
814 if (edit_widget)
816 result = g_strnfill (edit_get_curs_col (edit_widget), ' ');
817 goto ret;
819 #endif
820 break;
821 case 'y': /* syntax type */
822 #ifdef USE_INTERNAL_EDIT
823 if (edit_widget)
825 const char *syntax_type = edit_get_syntax_type (edit_widget);
826 if (syntax_type != NULL)
828 result = g_strdup (syntax_type);
829 goto ret;
832 #endif
833 break;
834 case 'k': /* block file name */
835 case 'b': /* block file name / strip extension */
837 #ifdef USE_INTERNAL_EDIT
838 if (edit_widget)
840 char *file;
842 file = mc_config_get_full_path (EDIT_BLOCK_FILE);
843 result = (*quote_func) (file, 0);
844 g_free (file);
845 goto ret;
847 #endif
848 if (c_lc == 'b')
850 result = strip_ext ((*quote_func) (fname, 0));
851 goto ret;
853 break;
855 case 'n': /* strip extension in editor */
856 #ifdef USE_INTERNAL_EDIT
857 if (edit_widget)
859 result = strip_ext ((*quote_func) (fname, 0));
860 goto ret;
862 #endif
863 break;
864 case 'm': /* menu file name */
865 if (menu)
867 result = (*quote_func) (menu, 0);
868 goto ret;
870 break;
871 case 's':
872 if (!panel || !panel->marked)
874 result = (*quote_func) (fname, 0);
875 goto ret;
878 /* Fall through */
880 case 't':
881 case 'u':
883 GString *block;
884 int i;
886 if (panel == NULL)
888 result = g_strdup ("");
889 goto ret;
892 block = g_string_sized_new (16);
894 for (i = 0; i < panel->dir.len; i++)
895 if (panel->dir.list[i].f.marked)
897 char *tmp;
899 tmp = (*quote_func) (panel->dir.list[i].fname, 0);
900 g_string_append (block, tmp);
901 g_string_append_c (block, ' ');
902 g_free (tmp);
904 if (c_lc == 'u')
905 do_file_mark (panel, i, 0);
907 result = g_string_free (block, FALSE);
908 goto ret;
909 } /* sub case block */
910 } /* switch */
911 result = g_strdup ("% ");
912 result[1] = c;
913 ret:
914 g_free (fname);
915 return result;
918 /* --------------------------------------------------------------------------------------------- */
920 * If edit_widget is NULL then we are called from the mc menu,
921 * otherwise we are called from the mcedit menu.
924 gboolean
925 user_menu_cmd (struct WEdit * edit_widget, const char *menu_file, int selected_entry)
927 char *p;
928 char *data, **entries;
929 int max_cols, menu_lines, menu_limit;
930 int col, i, accept_entry = 1;
931 int selected, old_patterns;
932 gboolean res = FALSE;
933 gboolean interactive = TRUE;
935 if (!vfs_current_is_local ())
937 message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems"));
938 return FALSE;
940 if (menu_file != NULL)
941 menu = g_strdup (menu_file);
942 else
943 menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU);
944 if (!exist_file (menu) || !menu_file_own (menu))
946 if (menu_file != NULL)
948 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu,
949 unix_error_string (errno));
950 g_free (menu);
951 menu = NULL;
952 return FALSE;
955 g_free (menu);
956 if (edit_widget)
957 menu = mc_config_get_full_path (EDIT_HOME_MENU);
958 else
959 menu = mc_config_get_full_path (MC_USERMENU_FILE);
962 if (!exist_file (menu))
964 g_free (menu);
965 menu =
966 mc_build_filename (mc_config_get_home_dir (),
967 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL);
968 if (!exist_file (menu))
970 g_free (menu);
971 menu =
972 mc_build_filename (mc_global.sysconfig_dir,
973 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU, NULL);
974 if (!exist_file (menu))
976 g_free (menu);
977 menu = mc_build_filename
978 (mc_global.share_data_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU,
979 NULL);
985 if (!g_file_get_contents (menu, &data, NULL, NULL))
987 message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno));
988 g_free (menu);
989 menu = NULL;
990 return FALSE;
993 max_cols = 0;
994 selected = 0;
995 menu_limit = 0;
996 entries = 0;
998 /* Parse the menu file */
999 old_patterns = easy_patterns;
1000 p = check_patterns (data);
1001 for (menu_lines = col = 0; *p; str_next_char (&p))
1003 if (menu_lines >= menu_limit)
1005 char **new_entries;
1007 menu_limit += MAX_ENTRIES;
1008 new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
1010 if (new_entries == NULL)
1011 break;
1013 entries = new_entries;
1014 new_entries += menu_limit;
1015 while (--new_entries >= &entries[menu_lines])
1016 *new_entries = NULL;
1018 if (col == 0 && !entries[menu_lines])
1020 if (*p == '#')
1022 /* show prompt if first line of external script is #interactive */
1023 if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0)
1024 interactive = FALSE;
1025 /* A commented menu entry */
1026 accept_entry = 1;
1028 else if (*p == '+')
1030 if (*(p + 1) == '=')
1032 /* Combined adding and default */
1033 p = test_line (edit_widget, p + 1, &accept_entry);
1034 if (selected == 0 && accept_entry)
1035 selected = menu_lines;
1037 else
1039 /* A condition for adding the entry */
1040 p = test_line (edit_widget, p, &accept_entry);
1043 else if (*p == '=')
1045 if (*(p + 1) == '+')
1047 /* Combined adding and default */
1048 p = test_line (edit_widget, p + 1, &accept_entry);
1049 if (selected == 0 && accept_entry)
1050 selected = menu_lines;
1052 else
1054 /* A condition for making the entry default */
1055 i = 1;
1056 p = test_line (edit_widget, p, &i);
1057 if (selected == 0 && i)
1058 selected = menu_lines;
1061 else if (*p != ' ' && *p != '\t' && str_isprint (p))
1063 /* A menu entry title line */
1064 if (accept_entry)
1065 entries[menu_lines] = p;
1066 else
1067 accept_entry = 1;
1070 if (*p == '\n')
1072 if (entries[menu_lines])
1074 menu_lines++;
1075 accept_entry = 1;
1077 max_cols = max (max_cols, col);
1078 col = 0;
1080 else
1082 if (*p == '\t')
1083 *p = ' ';
1084 col++;
1088 if (menu_lines == 0)
1090 message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu);
1091 res = FALSE;
1093 else
1095 if (selected_entry >= 0)
1096 selected = selected_entry;
1097 else
1099 Listbox *listbox;
1101 max_cols = min (max (max_cols, col), MAX_ENTRY_LEN);
1103 /* Create listbox */
1104 listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"),
1105 "[Menu File Edit]");
1106 /* insert all the items found */
1107 for (i = 0; i < menu_lines; i++)
1109 p = entries[i];
1110 LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0],
1111 extract_line (p, p + MAX_ENTRY_LEN), p);
1113 /* Select the default entry */
1114 listbox_select_entry (listbox->list, selected);
1116 selected = run_listbox (listbox);
1118 if (selected >= 0)
1120 execute_menu_command (edit_widget, entries[selected], interactive);
1121 res = TRUE;
1124 do_refresh ();
1127 easy_patterns = old_patterns;
1128 g_free (menu);
1129 menu = NULL;
1130 g_free (entries);
1131 g_free (data);
1132 return res;
1135 /* --------------------------------------------------------------------------------------------- */