Changed input parameters of mc_mkstemp() and mc_tempdir() functions
[midnight-commander.git] / src / filemanager / usermenu.c
blobba000672633ffb0caf3d7723578920cb630734e0
1 /*
2 User Menu implementation
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file usermenu.c
25 * \brief Source: user menu implementation
28 #include <config.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
36 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/search.h"
40 #include "lib/vfs/vfs.h"
41 #include "lib/strutil.h"
42 #include "lib/util.h"
43 #include "lib/widget.h"
45 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
46 #include "src/viewer/mcviewer.h" /* for default_* externs */
48 #include "src/execute.h"
49 #include "src/setup.h"
50 #include "src/history.h"
52 #include "dir.h"
53 #include "midnight.h"
54 #include "layout.h"
56 #include "usermenu.h"
58 /*** global variables ****************************************************************************/
60 /*** file scope macro definitions ****************************************************************/
62 #define MAX_ENTRIES 16
63 #define MAX_ENTRY_LEN 60
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 static int debug_flag = 0;
70 static int debug_error = 0;
71 static char *menu = NULL;
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 /** strip file's extension */
77 static char *
78 strip_ext (char *ss)
80 register char *s = ss;
81 char *e = NULL;
82 while (*s)
84 if (*s == '.')
85 e = s;
86 if (*s == PATH_SEP && e)
87 e = NULL; /* '.' in *directory* name */
88 s++;
90 if (e)
91 *e = 0;
92 return ss;
95 /* --------------------------------------------------------------------------------------------- */
96 /**
97 * Check for the "shell_patterns" directive. If it's found and valid,
98 * interpret it and move the pointer past the directive. Return the
99 * current pointer.
102 static char *
103 check_patterns (char *p)
105 static const char def_name[] = "shell_patterns=";
106 char *p0 = p;
108 if (strncmp (p, def_name, sizeof (def_name) - 1) != 0)
109 return p0;
111 p += sizeof (def_name) - 1;
112 if (*p == '1')
113 easy_patterns = 1;
114 else if (*p == '0')
115 easy_patterns = 0;
116 else
117 return p0;
119 /* Skip spaces */
120 p++;
121 while (*p == '\n' || *p == '\t' || *p == ' ')
122 p++;
123 return p;
126 /* --------------------------------------------------------------------------------------------- */
127 /** Copies a whitespace separated argument from p to arg. Returns the
128 point after argument. */
130 static char *
131 extract_arg (char *p, char *arg, int size)
133 char *np;
135 while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
136 p++;
137 /* support quote space .mnu */
138 while (*p && (*p != ' ' || *(p - 1) == '\\') && *p != '\t' && *p != '\n')
140 np = str_get_next_char (p);
141 if (np - p >= size)
142 break;
143 memcpy (arg, p, np - p);
144 arg += np - p;
145 size -= np - p;
146 p = np;
148 *arg = 0;
149 if (!*p || *p == '\n')
150 str_prev_char (&p);
151 return p;
154 /* --------------------------------------------------------------------------------------------- */
155 /* Tests whether the selected file in the panel is of any of the types
156 specified in argument. */
158 static int
159 test_type (WPanel * panel, char *arg)
161 int result = 0; /* False by default */
162 int st_mode = panel->dir.list[panel->selected].st.st_mode;
164 for (; *arg != 0; arg++)
166 switch (*arg)
168 case 'n': /* Not a directory */
169 result |= !S_ISDIR (st_mode);
170 break;
171 case 'r': /* Regular file */
172 result |= S_ISREG (st_mode);
173 break;
174 case 'd': /* Directory */
175 result |= S_ISDIR (st_mode);
176 break;
177 case 'l': /* Link */
178 result |= S_ISLNK (st_mode);
179 break;
180 case 'c': /* Character special */
181 result |= S_ISCHR (st_mode);
182 break;
183 case 'b': /* Block special */
184 result |= S_ISBLK (st_mode);
185 break;
186 case 'f': /* Fifo (named pipe) */
187 result |= S_ISFIFO (st_mode);
188 break;
189 case 's': /* Socket */
190 result |= S_ISSOCK (st_mode);
191 break;
192 case 'x': /* Executable */
193 result |= (st_mode & 0111) ? 1 : 0;
194 break;
195 case 't':
196 result |= panel->marked ? 1 : 0;
197 break;
198 default:
199 debug_error = 1;
200 break;
203 return result;
206 /* --------------------------------------------------------------------------------------------- */
207 /** Calculates the truth value of the next condition starting from
208 p. Returns the point after condition. */
210 static char *
211 test_condition (WEdit * edit_widget, char *p, int *condition)
213 char arg[256];
214 const mc_search_type_t search_type = easy_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
216 /* Handle one condition */
217 for (; *p != '\n' && *p != '&' && *p != '|'; p++)
219 WPanel *panel = NULL;
221 /* support quote space .mnu */
222 if ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
223 continue;
224 if (*p >= 'a')
225 panel = current_panel;
226 else if (get_other_type () == view_listing)
227 panel = other_panel;
229 *p |= 0x20;
231 switch (*p++)
233 case '!':
234 p = test_condition (edit_widget, p, condition);
235 *condition = !*condition;
236 str_prev_char (&p);
237 break;
238 case 'f': /* file name pattern */
239 p = extract_arg (p, arg, sizeof (arg));
240 #ifdef USE_INTERNAL_EDIT
241 if (edit_widget != NULL)
242 *condition = mc_search (arg, edit_get_file_name (edit_widget), search_type) ? 1 : 0;
243 else
244 #endif
245 *condition = panel != NULL &&
246 mc_search (arg, panel->dir.list[panel->selected].fname, search_type) ? 1 : 0;
247 break;
248 case 'y': /* syntax pattern */
249 #ifdef USE_INTERNAL_EDIT
250 if (edit_widget != NULL)
252 const char *syntax_type = edit_get_syntax_type (edit_widget);
253 if (syntax_type != NULL)
255 p = extract_arg (p, arg, sizeof (arg));
256 *condition = mc_search (arg, syntax_type, MC_SEARCH_T_NORMAL) ? 1 : 0;
259 #endif
260 break;
261 case 'd':
262 p = extract_arg (p, arg, sizeof (arg));
263 *condition = panel != NULL && mc_search (arg, panel->cwd, search_type) ? 1 : 0;
264 break;
265 case 't':
266 p = extract_arg (p, arg, sizeof (arg));
267 *condition = panel != NULL && test_type (panel, arg) ? 1 : 0;
268 break;
269 case 'x': /* executable */
271 struct stat status;
273 p = extract_arg (p, arg, sizeof (arg));
274 if (stat (arg, &status) == 0)
275 *condition = is_exe (status.st_mode) ? 1 : 0;
276 else
277 *condition = 0;
278 break;
280 default:
281 debug_error = 1;
282 break;
283 } /* switch */
285 } /* while */
286 return p;
289 /* --------------------------------------------------------------------------------------------- */
290 /** General purpose condition debug output handler */
292 static void
293 debug_out (char *start, char *end, int cond)
295 static char *msg;
296 int len;
298 if (start == NULL && end == NULL)
300 /* Show output */
301 if (debug_flag && msg)
303 len = strlen (msg);
304 if (len)
305 msg[len - 1] = 0;
306 message (D_NORMAL, _("Debug"), "%s", msg);
309 debug_flag = 0;
310 g_free (msg);
311 msg = NULL;
313 else
315 const char *type;
316 char *p;
318 /* Save debug info for later output */
319 if (!debug_flag)
320 return;
321 /* Save the result of the condition */
322 if (debug_error)
324 type = _("ERROR:");
325 debug_error = 0;
327 else if (cond)
328 type = _("True:");
329 else
330 type = _("False:");
331 /* This is for debugging, don't need to be super efficient. */
332 if (end == NULL)
333 p = g_strdup_printf ("%s %s %c \n", msg ? msg : "", type, *start);
334 else
335 p = g_strdup_printf ("%s %s %.*s \n", msg ? msg : "", type, (int) (end - start), start);
336 g_free (msg);
337 msg = p;
341 /* --------------------------------------------------------------------------------------------- */
342 /** Calculates the truth value of one lineful of conditions. Returns
343 the point just before the end of line. */
345 static char *
346 test_line (WEdit * edit_widget, char *p, int *result)
348 int condition;
349 char operator;
350 char *debug_start, *debug_end;
352 /* Repeat till end of line */
353 while (*p && *p != '\n')
355 /* support quote space .mnu */
356 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
357 p++;
358 if (!*p || *p == '\n')
359 break;
360 operator = *p++;
361 if (*p == '?')
363 debug_flag = 1;
364 p++;
366 /* support quote space .mnu */
367 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
368 p++;
369 if (!*p || *p == '\n')
370 break;
371 condition = 1; /* True by default */
373 debug_start = p;
374 p = test_condition (edit_widget, p, &condition);
375 debug_end = p;
376 /* Add one debug statement */
377 debug_out (debug_start, debug_end, condition);
379 switch (operator)
381 case '+':
382 case '=':
383 /* Assignment */
384 *result = condition;
385 break;
386 case '&': /* Logical and */
387 *result &= condition;
388 break;
389 case '|': /* Logical or */
390 *result |= condition;
391 break;
392 default:
393 debug_error = 1;
394 break;
395 } /* switch */
396 /* Add one debug statement */
397 debug_out (&operator, NULL, *result);
399 } /* while (*p != '\n') */
400 /* Report debug message */
401 debug_out (NULL, NULL, 1);
403 if (!*p || *p == '\n')
404 str_prev_char (&p);
405 return p;
408 /* --------------------------------------------------------------------------------------------- */
409 /** FIXME: recode this routine on version 3.0, it could be cleaner */
411 static void
412 execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_prompt)
414 FILE *cmd_file;
415 int cmd_file_fd;
416 int expand_prefix_found = 0;
417 char *parameter = 0;
418 gboolean do_quote = FALSE;
419 char lc_prompt[80];
420 int col;
421 vfs_path_t *file_name_vpath;
422 int run_view = 0;
424 /* Skip menu entry title line */
425 commands = strchr (commands, '\n');
426 if (!commands)
428 return;
431 cmd_file_fd = mc_mkstemps (&file_name_vpath, "mcusr", SCRIPT_SUFFIX);
433 if (cmd_file_fd == -1)
435 message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"),
436 unix_error_string (errno));
437 vfs_path_free (file_name_vpath);
438 return;
440 cmd_file = fdopen (cmd_file_fd, "w");
441 fputs ("#! /bin/sh\n", cmd_file);
442 commands++;
444 for (col = 0; *commands; commands++)
446 if (col == 0)
448 if (*commands != ' ' && *commands != '\t')
449 break;
450 while (*commands == ' ' || *commands == '\t')
451 commands++;
452 if (*commands == 0)
453 break;
455 col++;
456 if (*commands == '\n')
457 col = 0;
458 if (parameter)
460 if (*commands == '}')
462 char *tmp;
463 *parameter = 0;
464 parameter =
465 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "");
466 if (!parameter || !*parameter)
468 /* User canceled */
469 fclose (cmd_file);
470 mc_unlink (file_name_vpath);
471 vfs_path_free (file_name_vpath);
472 return;
474 if (do_quote)
476 tmp = name_quote (parameter, 0);
477 fputs (tmp, cmd_file);
478 g_free (tmp);
480 else
481 fputs (parameter, cmd_file);
482 g_free (parameter);
483 parameter = 0;
485 else
487 if (parameter < &lc_prompt[sizeof (lc_prompt) - 1])
489 *parameter++ = *commands;
493 else if (expand_prefix_found)
495 expand_prefix_found = 0;
496 if (g_ascii_isdigit ((gchar) * commands))
498 do_quote = (atoi (commands) != 0);
499 while (g_ascii_isdigit ((gchar) * commands))
500 commands++;
502 if (*commands == '{')
503 parameter = lc_prompt;
504 else
506 char *text = expand_format (edit_widget, *commands, do_quote);
507 fputs (text, cmd_file);
508 g_free (text);
511 else
513 if (*commands == '%')
515 int i = check_format_view (commands + 1);
516 if (i)
518 commands += i;
519 run_view = 1;
521 else
523 do_quote = TRUE; /* Default: Quote expanded macro */
524 expand_prefix_found = 1;
527 else
528 fputc (*commands, cmd_file);
531 fclose (cmd_file);
532 mc_chmod (file_name_vpath, S_IRWXU);
533 if (run_view)
535 char *file_name;
537 file_name = vfs_path_to_str (file_name_vpath);
538 mcview_viewer (file_name, NULL, 0);
539 g_free (file_name);
540 dialog_switch_process_pending ();
542 else
544 /* execute the command indirectly to allow execution even
545 * on no-exec filesystems. */
546 char *file_name, *cmd;
548 file_name = vfs_path_to_str (file_name_vpath);
549 cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
550 g_free (file_name);
551 if (!show_prompt)
553 if (system (cmd) == -1)
554 message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
556 else
558 shell_execute (cmd, EXECUTE_HIDE);
560 g_free (cmd);
562 mc_unlink (file_name_vpath);
563 vfs_path_free (file_name_vpath);
566 /* --------------------------------------------------------------------------------------------- */
568 ** Check owner of the menu file. Using menu file is allowed, if
569 ** owner of the menu is root or the actual user. In either case
570 ** file should not be group and word-writable.
572 ** Q. Should we apply this routine to system and home menu (and .ext files)?
575 static int
576 menu_file_own (char *path)
578 struct stat st;
580 if (stat (path, &st) == 0
581 && (!st.st_uid || (st.st_uid == geteuid ())) && ((st.st_mode & (S_IWGRP | S_IWOTH)) == 0))
583 return 1;
585 if (verbose)
587 message (D_NORMAL, _("Warning -- ignoring file"),
588 _("File %s is not owned by root or you or is world writable.\n"
589 "Using it may compromise your security"), path);
591 return 0;
594 /* --------------------------------------------------------------------------------------------- */
595 /*** public functions ****************************************************************************/
596 /* --------------------------------------------------------------------------------------------- */
598 /* Formats defined:
599 %% The % character
600 %f The current file (if non-local vfs, file will be copied locally and
601 %f will be full path to it).
602 %p The current file
603 %d The current working directory
604 %s "Selected files"; the tagged files if any, otherwise the current file
605 %t Tagged files
606 %u Tagged files (and they are untagged on return from expand_format)
607 %view Runs the commands and pipes standard output to the view command.
608 If %view is immediately followed by '{', recognize keywords
609 ascii, hex, nroff and unform
611 If the format letter is in uppercase, it refers to the other panel.
613 With a number followed the % character you can turn quoting on (default)
614 and off. For example:
615 %f quote expanded macro
616 %1f ditto
617 %0f don't quote expanded macro
619 expand_format returns a memory block that must be free()d.
622 /* Returns how many characters we should advance if %view was found */
624 check_format_view (const char *p)
626 const char *q = p;
627 if (!strncmp (p, "view", 4))
629 q += 4;
630 if (*q == '{')
632 for (q++; *q && *q != '}'; q++)
634 if (!strncmp (q, "ascii", 5))
636 mcview_default_hex_mode = 0;
637 q += 4;
639 else if (!strncmp (q, "hex", 3))
641 mcview_default_hex_mode = 1;
642 q += 2;
644 else if (!strncmp (q, "nroff", 5))
646 mcview_default_nroff_flag = 1;
647 q += 4;
649 else if (!strncmp (q, "unform", 6))
651 mcview_default_nroff_flag = 0;
652 q += 5;
655 if (*q == '}')
656 q++;
658 return q - p;
660 return 0;
663 /* --------------------------------------------------------------------------------------------- */
666 check_format_cd (const char *p)
668 return (strncmp (p, "cd", 2)) ? 0 : 3;
671 /* --------------------------------------------------------------------------------------------- */
672 /* Check if p has a "^var\{var-name\}" */
673 /* Returns the number of skipped characters (zero on not found) */
674 /* V will be set to the expanded variable name */
677 check_format_var (const char *p, char **v)
679 const char *q = p;
680 char *var_name;
681 const char *value;
682 const char *dots = 0;
684 *v = 0;
685 if (!strncmp (p, "var{", 4))
687 for (q += 4; *q && *q != '}'; q++)
689 if (*q == ':')
690 dots = q + 1;
692 if (!*q)
693 return 0;
695 if (!dots || dots == q + 5)
697 message (D_ERROR,
698 _("Format error on file Extensions File"),
699 !dots ? _("The %%var macro has no default")
700 : _("The %%var macro has no variable"));
701 return 0;
704 /* Copy the variable name */
705 var_name = g_strndup (p + 4, dots - 2 - (p + 3));
707 value = getenv (var_name);
708 g_free (var_name);
709 if (value)
711 *v = g_strdup (value);
712 return q - p;
714 var_name = g_strndup (dots, q - dots);
715 *v = var_name;
716 return q - p;
718 return 0;
721 /* --------------------------------------------------------------------------------------------- */
723 char *
724 expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
726 WPanel *panel = NULL;
727 char *(*quote_func) (const char *, int);
728 char *fname = NULL;
729 char *result;
730 char c_lc;
732 #ifndef USE_INTERNAL_EDIT
733 (void) edit_widget;
734 #endif
736 if (c == '%')
737 return g_strdup ("%");
739 if (mc_global.mc_run_mode == MC_RUN_FULL)
741 if (g_ascii_islower ((gchar) c))
742 panel = current_panel;
743 else
745 if (get_other_type () != view_listing)
746 return g_strdup ("");
747 panel = other_panel;
749 fname = panel->dir.list[panel->selected].fname;
751 #ifdef USE_INTERNAL_EDIT
752 else if (mc_global.mc_run_mode == MC_RUN_EDITOR)
753 fname = (char *) edit_get_file_name (edit_widget);
754 #endif
756 if (do_quote)
757 quote_func = name_quote;
758 else
759 quote_func = fake_name_quote;
761 c_lc = g_ascii_tolower ((gchar) c);
763 switch (c_lc)
765 case 'f':
766 case 'p':
767 return (*quote_func) (fname, 0);
768 case 'x':
769 return (*quote_func) (extension (fname), 0);
770 case 'd':
772 char *cwd;
773 char *qstr;
775 cwd = g_malloc (MC_MAXPATHLEN + 1);
777 if (panel)
778 g_strlcpy (cwd, panel->cwd, MC_MAXPATHLEN + 1);
779 else
780 mc_get_current_wd (cwd, MC_MAXPATHLEN + 1);
782 qstr = (*quote_func) (cwd, 0);
784 g_free (cwd);
786 return qstr;
788 case 'i': /* indent equal number cursor position in line */
789 #ifdef USE_INTERNAL_EDIT
790 if (edit_widget)
791 return g_strnfill (edit_get_curs_col (edit_widget), ' ');
792 #endif
793 break;
794 case 'y': /* syntax type */
795 #ifdef USE_INTERNAL_EDIT
796 if (edit_widget)
798 const char *syntax_type = edit_get_syntax_type (edit_widget);
799 if (syntax_type != NULL)
800 return g_strdup (syntax_type);
802 #endif
803 break;
804 case 'k': /* block file name */
805 case 'b': /* block file name / strip extension */
807 #ifdef USE_INTERNAL_EDIT
808 if (edit_widget)
810 char *file = mc_config_get_full_path (EDIT_BLOCK_FILE);
811 fname = (*quote_func) (file, 0);
812 g_free (file);
813 return fname;
815 #endif
816 if (c_lc == 'b')
817 return strip_ext ((*quote_func) (fname, 0));
818 break;
820 case 'n': /* strip extension in editor */
821 #ifdef USE_INTERNAL_EDIT
822 if (edit_widget)
823 return strip_ext ((*quote_func) (fname, 0));
824 #endif
825 break;
826 case 'm': /* menu file name */
827 if (menu)
828 return (*quote_func) (menu, 0);
829 break;
830 case 's':
831 if (!panel || !panel->marked)
832 return (*quote_func) (fname, 0);
834 /* Fall through */
836 case 't':
837 case 'u':
839 GString *block;
840 int i;
842 if (panel == NULL)
843 return g_strdup ("");
845 block = g_string_sized_new (16);
847 for (i = 0; i < panel->count; i++)
848 if (panel->dir.list[i].f.marked)
850 char *tmp;
852 tmp = (*quote_func) (panel->dir.list[i].fname, 0);
853 g_string_append (block, tmp);
854 g_string_append_c (block, ' ');
855 g_free (tmp);
857 if (c_lc == 'u')
858 do_file_mark (panel, i, 0);
861 return g_string_free (block, FALSE);
862 } /* sub case block */
863 } /* switch */
864 result = g_strdup ("% ");
865 result[1] = c;
866 return result;
869 /* --------------------------------------------------------------------------------------------- */
871 * If edit_widget is NULL then we are called from the mc menu,
872 * otherwise we are called from the mcedit menu.
875 gboolean
876 user_menu_cmd (struct WEdit * edit_widget, const char *menu_file, int selected_entry)
878 char *p;
879 char *data, **entries;
880 int max_cols, menu_lines, menu_limit;
881 int col, i, accept_entry = 1;
882 int selected, old_patterns;
883 Listbox *listbox;
884 gboolean res = FALSE;
885 gboolean interactive = TRUE;
887 if (!vfs_current_is_local ())
889 message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems"));
890 return FALSE;
892 if (menu_file != NULL)
893 menu = g_strdup (menu_file);
894 else
895 menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU);
896 if (!exist_file (menu) || !menu_file_own (menu))
898 if (menu_file != NULL)
900 message (D_ERROR, MSG_ERROR, _("Cannot open file %s\n%s"), menu,
901 unix_error_string (errno));
902 g_free (menu);
903 menu = NULL;
904 return FALSE;
907 g_free (menu);
908 if (edit_widget)
909 menu = mc_config_get_full_path (EDIT_HOME_MENU);
910 else
911 menu = mc_config_get_full_path (MC_USERMENU_FILE);
914 if (!exist_file (menu))
916 g_free (menu);
917 menu =
918 concat_dir_and_file (mc_config_get_home_dir (),
919 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
920 if (!exist_file (menu))
922 g_free (menu);
923 menu =
924 concat_dir_and_file (mc_global.sysconfig_dir,
925 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
926 if (!exist_file (menu))
928 g_free (menu);
929 menu = concat_dir_and_file
930 (mc_global.share_data_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
936 if (!g_file_get_contents (menu, &data, NULL, NULL))
938 message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno));
939 g_free (menu);
940 menu = NULL;
941 return FALSE;
944 max_cols = 0;
945 selected = 0;
946 menu_limit = 0;
947 entries = 0;
949 /* Parse the menu file */
950 old_patterns = easy_patterns;
951 p = check_patterns (data);
952 for (menu_lines = col = 0; *p; str_next_char (&p))
954 if (menu_lines >= menu_limit)
956 char **new_entries;
958 menu_limit += MAX_ENTRIES;
959 new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
961 if (new_entries == NULL)
962 break;
964 entries = new_entries;
965 new_entries += menu_limit;
966 while (--new_entries >= &entries[menu_lines])
967 *new_entries = NULL;
969 if (col == 0 && !entries[menu_lines])
971 if (*p == '#')
973 /* show prompt if first line of external script is #interactive */
974 if (selected_entry >= 0 && strncmp (p, "#silent", 7) == 0)
975 interactive = FALSE;
976 /* A commented menu entry */
977 accept_entry = 1;
979 else if (*p == '+')
981 if (*(p + 1) == '=')
983 /* Combined adding and default */
984 p = test_line (edit_widget, p + 1, &accept_entry);
985 if (selected == 0 && accept_entry)
986 selected = menu_lines;
988 else
990 /* A condition for adding the entry */
991 p = test_line (edit_widget, p, &accept_entry);
994 else if (*p == '=')
996 if (*(p + 1) == '+')
998 /* Combined adding and default */
999 p = test_line (edit_widget, p + 1, &accept_entry);
1000 if (selected == 0 && accept_entry)
1001 selected = menu_lines;
1003 else
1005 /* A condition for making the entry default */
1006 i = 1;
1007 p = test_line (edit_widget, p, &i);
1008 if (selected == 0 && i)
1009 selected = menu_lines;
1012 else if (*p != ' ' && *p != '\t' && str_isprint (p))
1014 /* A menu entry title line */
1015 if (accept_entry)
1016 entries[menu_lines] = p;
1017 else
1018 accept_entry = 1;
1021 if (*p == '\n')
1023 if (entries[menu_lines])
1025 menu_lines++;
1026 accept_entry = 1;
1028 max_cols = max (max_cols, col);
1029 col = 0;
1031 else
1033 if (*p == '\t')
1034 *p = ' ';
1035 col++;
1039 if (menu_lines == 0)
1041 message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu);
1042 res = FALSE;
1044 else
1046 if (selected_entry >= 0)
1047 selected = selected_entry;
1048 else
1050 max_cols = min (max (max_cols, col), MAX_ENTRY_LEN);
1052 /* Create listbox */
1053 listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"),
1054 "[Menu File Edit]");
1055 /* insert all the items found */
1056 for (i = 0; i < menu_lines; i++)
1058 p = entries[i];
1059 LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0],
1060 extract_line (p, p + MAX_ENTRY_LEN), p);
1062 /* Select the default entry */
1063 listbox_select_entry (listbox->list, selected);
1065 selected = run_listbox (listbox);
1067 if (selected >= 0)
1069 execute_menu_command (edit_widget, entries[selected], interactive);
1070 res = TRUE;
1073 do_refresh ();
1076 easy_patterns = old_patterns;
1077 g_free (menu);
1078 menu = NULL;
1079 g_free (entries);
1080 g_free (data);
1081 return res;
1084 /* --------------------------------------------------------------------------------------------- */