Added two macro examples:
[zuwinko.git] / src / filemanager / usermenu.c
blobc093776a9c1c62ee357f866fdbf2b44f28632ccd
1 /* User Menu implementation
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /** \file usermenu.c
20 * \brief Source: user menu implementation
23 #include <config.h>
25 #include <ctype.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include "lib/global.h"
32 #include "lib/tty/tty.h"
33 #include "lib/skin.h"
34 #include "lib/search.h"
35 #include "lib/vfs/mc-vfs/vfs.h"
36 #include "lib/strutil.h"
37 #include "lib/util.h"
38 #include "lib/widget.h"
40 #include "src/editor/edit.h" /* WEdit, BLOCK_FILE */
41 #include "src/viewer/mcviewer.h" /* for default_* externs */
43 #include "src/execute.h"
44 #include "src/setup.h"
45 #include "src/history.h"
47 #include "dir.h"
48 #include "midnight.h"
49 #include "layout.h"
51 #include "usermenu.h"
53 /*** global variables ****************************************************************************/
55 /*** file scope macro definitions ****************************************************************/
57 #define MAX_ENTRIES 16
58 #define MAX_ENTRY_LEN 60
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 static int debug_flag = 0;
65 static int debug_error = 0;
66 static char *menu = NULL;
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 /** strip file's extension */
72 static char *
73 strip_ext (char *ss)
75 register char *s = ss;
76 char *e = NULL;
77 while (*s)
79 if (*s == '.')
80 e = s;
81 if (*s == PATH_SEP && e)
82 e = NULL; /* '.' in *directory* name */
83 s++;
85 if (e)
86 *e = 0;
87 return ss;
90 /* --------------------------------------------------------------------------------------------- */
91 /**
92 * Check for the "shell_patterns" directive. If it's found and valid,
93 * interpret it and move the pointer past the directive. Return the
94 * current pointer.
97 static char *
98 check_patterns (char *p)
100 static const char def_name[] = "shell_patterns=";
101 char *p0 = p;
103 if (strncmp (p, def_name, sizeof (def_name) - 1) != 0)
104 return p0;
106 p += sizeof (def_name) - 1;
107 if (*p == '1')
108 easy_patterns = 1;
109 else if (*p == '0')
110 easy_patterns = 0;
111 else
112 return p0;
114 /* Skip spaces */
115 p++;
116 while (*p == '\n' || *p == '\t' || *p == ' ')
117 p++;
118 return p;
121 /* --------------------------------------------------------------------------------------------- */
122 /** Copies a whitespace separated argument from p to arg. Returns the
123 point after argument. */
125 static char *
126 extract_arg (char *p, char *arg, int size)
128 char *np;
130 while (*p && (*p == ' ' || *p == '\t' || *p == '\n'))
131 p++;
132 /* support quote space .mnu */
133 while (*p && (*p != ' ' || *(p - 1) == '\\') && *p != '\t' && *p != '\n')
135 np = str_get_next_char (p);
136 if (np - p >= size)
137 break;
138 memcpy (arg, p, np - p);
139 arg += np - p;
140 size -= np - p;
141 p = np;
143 *arg = 0;
144 if (!*p || *p == '\n')
145 str_prev_char (&p);
146 return p;
149 /* --------------------------------------------------------------------------------------------- */
150 /* Tests whether the selected file in the panel is of any of the types
151 specified in argument. */
153 static int
154 test_type (WPanel * panel, char *arg)
156 int result = 0; /* False by default */
157 int st_mode = panel->dir.list[panel->selected].st.st_mode;
159 for (; *arg != 0; arg++)
161 switch (*arg)
163 case 'n': /* Not a directory */
164 result |= !S_ISDIR (st_mode);
165 break;
166 case 'r': /* Regular file */
167 result |= S_ISREG (st_mode);
168 break;
169 case 'd': /* Directory */
170 result |= S_ISDIR (st_mode);
171 break;
172 case 'l': /* Link */
173 result |= S_ISLNK (st_mode);
174 break;
175 case 'c': /* Character special */
176 result |= S_ISCHR (st_mode);
177 break;
178 case 'b': /* Block special */
179 result |= S_ISBLK (st_mode);
180 break;
181 case 'f': /* Fifo (named pipe) */
182 result |= S_ISFIFO (st_mode);
183 break;
184 case 's': /* Socket */
185 result |= S_ISSOCK (st_mode);
186 break;
187 case 'x': /* Executable */
188 result |= (st_mode & 0111) ? 1 : 0;
189 break;
190 case 't':
191 result |= panel->marked ? 1 : 0;
192 break;
193 default:
194 debug_error = 1;
195 break;
198 return result;
201 /* --------------------------------------------------------------------------------------------- */
202 /** Calculates the truth value of the next condition starting from
203 p. Returns the point after condition. */
205 static char *
206 test_condition (WEdit * edit_widget, char *p, int *condition)
208 char arg[256];
209 const mc_search_type_t search_type = easy_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
211 /* Handle one condition */
212 for (; *p != '\n' && *p != '&' && *p != '|'; p++)
214 WPanel *panel = NULL;
216 /* support quote space .mnu */
217 if ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
218 continue;
219 if (*p >= 'a')
220 panel = current_panel;
221 else if (get_other_type () == view_listing)
222 panel = other_panel;
224 *p |= 0x20;
226 switch (*p++)
228 case '!':
229 p = test_condition (edit_widget, p, condition);
230 *condition = !*condition;
231 str_prev_char (&p);
232 break;
233 case 'f': /* file name pattern */
234 p = extract_arg (p, arg, sizeof (arg));
235 #ifdef USE_INTERNAL_EDIT
236 if (edit_widget != NULL)
237 *condition = mc_search (arg, edit_get_file_name (edit_widget), search_type) ? 1 : 0;
238 else
239 #endif
240 *condition = panel != NULL &&
241 mc_search (arg, panel->dir.list[panel->selected].fname, search_type) ? 1 : 0;
242 break;
243 case 'y': /* syntax pattern */
244 #ifdef USE_INTERNAL_EDIT
245 if (edit_widget != NULL)
247 const char *syntax_type = edit_get_syntax_type (edit_widget);
248 if (syntax_type != NULL)
250 p = extract_arg (p, arg, sizeof (arg));
251 *condition = mc_search (arg, syntax_type, MC_SEARCH_T_NORMAL) ? 1 : 0;
254 #endif
255 break;
256 case 'd':
257 p = extract_arg (p, arg, sizeof (arg));
258 *condition = panel != NULL && mc_search (arg, panel->cwd, search_type) ? 1 : 0;
259 break;
260 case 't':
261 p = extract_arg (p, arg, sizeof (arg));
262 *condition = panel != NULL && test_type (panel, arg) ? 1 : 0;
263 break;
264 case 'x': /* executable */
266 struct stat status;
268 p = extract_arg (p, arg, sizeof (arg));
269 if (stat (arg, &status) == 0)
270 *condition = is_exe (status.st_mode) ? 1 : 0;
271 else
272 *condition = 0;
273 break;
275 default:
276 debug_error = 1;
277 break;
278 } /* switch */
280 } /* while */
281 return p;
284 /* --------------------------------------------------------------------------------------------- */
285 /** General purpose condition debug output handler */
287 static void
288 debug_out (char *start, char *end, int cond)
290 static char *msg;
291 int len;
293 if (start == NULL && end == NULL)
295 /* Show output */
296 if (debug_flag && msg)
298 len = strlen (msg);
299 if (len)
300 msg[len - 1] = 0;
301 message (D_NORMAL, _("Debug"), "%s", msg);
304 debug_flag = 0;
305 g_free (msg);
306 msg = NULL;
308 else
310 const char *type;
311 char *p;
313 /* Save debug info for later output */
314 if (!debug_flag)
315 return;
316 /* Save the result of the condition */
317 if (debug_error)
319 type = _("ERROR:");
320 debug_error = 0;
322 else if (cond)
323 type = _("True:");
324 else
325 type = _("False:");
326 /* This is for debugging, don't need to be super efficient. */
327 if (end == NULL)
328 p = g_strdup_printf ("%s %s %c \n", msg ? msg : "", type, *start);
329 else
330 p = g_strdup_printf ("%s %s %.*s \n", msg ? msg : "", type, (int) (end - start), start);
331 g_free (msg);
332 msg = p;
336 /* --------------------------------------------------------------------------------------------- */
337 /** Calculates the truth value of one lineful of conditions. Returns
338 the point just before the end of line. */
340 static char *
341 test_line (WEdit * edit_widget, char *p, int *result)
343 int condition;
344 char operator;
345 char *debug_start, *debug_end;
347 /* Repeat till end of line */
348 while (*p && *p != '\n')
350 /* support quote space .mnu */
351 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
352 p++;
353 if (!*p || *p == '\n')
354 break;
355 operator = *p++;
356 if (*p == '?')
358 debug_flag = 1;
359 p++;
361 /* support quote space .mnu */
362 while ((*p == ' ' && *(p - 1) != '\\') || *p == '\t')
363 p++;
364 if (!*p || *p == '\n')
365 break;
366 condition = 1; /* True by default */
368 debug_start = p;
369 p = test_condition (edit_widget, p, &condition);
370 debug_end = p;
371 /* Add one debug statement */
372 debug_out (debug_start, debug_end, condition);
374 switch (operator)
376 case '+':
377 case '=':
378 /* Assignment */
379 *result = condition;
380 break;
381 case '&': /* Logical and */
382 *result &= condition;
383 break;
384 case '|': /* Logical or */
385 *result |= condition;
386 break;
387 default:
388 debug_error = 1;
389 break;
390 } /* switch */
391 /* Add one debug statement */
392 debug_out (&operator, NULL, *result);
394 } /* while (*p != '\n') */
395 /* Report debug message */
396 debug_out (NULL, NULL, 1);
398 if (!*p || *p == '\n')
399 str_prev_char (&p);
400 return p;
403 /* --------------------------------------------------------------------------------------------- */
404 /** FIXME: recode this routine on version 3.0, it could be cleaner */
406 static void
407 execute_menu_command (WEdit * edit_widget, const char *commands, gboolean show_prompt)
409 FILE *cmd_file;
410 int cmd_file_fd;
411 int expand_prefix_found = 0;
412 char *parameter = 0;
413 gboolean do_quote = FALSE;
414 char lc_prompt[80];
415 int col;
416 char *file_name;
417 int run_view = 0;
419 /* Skip menu entry title line */
420 commands = strchr (commands, '\n');
421 if (!commands)
423 return;
426 cmd_file_fd = mc_mkstemps (&file_name, "mcusr", SCRIPT_SUFFIX);
428 if (cmd_file_fd == -1)
430 message (D_ERROR, MSG_ERROR, _("Cannot create temporary command file\n%s"),
431 unix_error_string (errno));
432 return;
434 cmd_file = fdopen (cmd_file_fd, "w");
435 fputs ("#! /bin/sh\n", cmd_file);
436 commands++;
438 for (col = 0; *commands; commands++)
440 if (col == 0)
442 if (*commands != ' ' && *commands != '\t')
443 break;
444 while (*commands == ' ' || *commands == '\t')
445 commands++;
446 if (*commands == 0)
447 break;
449 col++;
450 if (*commands == '\n')
451 col = 0;
452 if (parameter)
454 if (*commands == '}')
456 char *tmp;
457 *parameter = 0;
458 parameter =
459 input_dialog (_("Parameter"), lc_prompt, MC_HISTORY_FM_MENU_EXEC_PARAM, "");
460 if (!parameter || !*parameter)
462 /* User canceled */
463 fclose (cmd_file);
464 unlink (file_name);
465 g_free (file_name);
466 return;
468 if (do_quote)
470 tmp = name_quote (parameter, 0);
471 fputs (tmp, cmd_file);
472 g_free (tmp);
474 else
475 fputs (parameter, cmd_file);
476 g_free (parameter);
477 parameter = 0;
479 else
481 if (parameter < &lc_prompt[sizeof (lc_prompt) - 1])
483 *parameter++ = *commands;
487 else if (expand_prefix_found)
489 expand_prefix_found = 0;
490 if (g_ascii_isdigit ((gchar) * commands))
492 do_quote = (atoi (commands) != 0);
493 while (g_ascii_isdigit ((gchar) * commands))
494 commands++;
496 if (*commands == '{')
497 parameter = lc_prompt;
498 else
500 char *text = expand_format (edit_widget, *commands, do_quote);
501 fputs (text, cmd_file);
502 g_free (text);
505 else
507 if (*commands == '%')
509 int i = check_format_view (commands + 1);
510 if (i)
512 commands += i;
513 run_view = 1;
515 else
517 do_quote = TRUE; /* Default: Quote expanded macro */
518 expand_prefix_found = 1;
521 else
522 fputc (*commands, cmd_file);
525 fclose (cmd_file);
526 chmod (file_name, S_IRWXU);
527 if (run_view)
529 mcview_viewer (file_name, NULL, 0);
530 dialog_switch_process_pending ();
532 else
534 /* execute the command indirectly to allow execution even
535 * on no-exec filesystems. */
536 char *cmd = g_strconcat ("/bin/sh ", file_name, (char *) NULL);
537 if (!show_prompt)
539 if (system (cmd) == -1)
540 message (D_ERROR, MSG_ERROR, "%s", _("Error calling program"));
542 else
544 shell_execute (cmd, EXECUTE_HIDE);
546 g_free (cmd);
548 unlink (file_name);
549 g_free (file_name);
552 /* --------------------------------------------------------------------------------------------- */
554 ** Check owner of the menu file. Using menu file is allowed, if
555 ** owner of the menu is root or the actual user. In either case
556 ** file should not be group and word-writable.
558 ** Q. Should we apply this routine to system and home menu (and .ext files)?
561 static int
562 menu_file_own (char *path)
564 struct stat st;
566 if (stat (path, &st) == 0
567 && (!st.st_uid || (st.st_uid == geteuid ())) && ((st.st_mode & (S_IWGRP | S_IWOTH)) == 0))
569 return 1;
571 if (verbose)
573 message (D_NORMAL, _("Warning -- ignoring file"),
574 _("File %s is not owned by root or you or is world writable.\n"
575 "Using it may compromise your security"), path);
577 return 0;
580 /* --------------------------------------------------------------------------------------------- */
581 /*** public functions ****************************************************************************/
582 /* --------------------------------------------------------------------------------------------- */
584 /* Formats defined:
585 %% The % character
586 %f The current file (if non-local vfs, file will be copied locally and
587 %f will be full path to it).
588 %p The current file
589 %d The current working directory
590 %s "Selected files"; the tagged files if any, otherwise the current file
591 %t Tagged files
592 %u Tagged files (and they are untagged on return from expand_format)
593 %view Runs the commands and pipes standard output to the view command.
594 If %view is immediately followed by '{', recognize keywords
595 ascii, hex, nroff and unform
597 If the format letter is in uppercase, it refers to the other panel.
599 With a number followed the % character you can turn quoting on (default)
600 and off. For example:
601 %f quote expanded macro
602 %1f ditto
603 %0f don't quote expanded macro
605 expand_format returns a memory block that must be free()d.
608 /* Returns how many characters we should advance if %view was found */
610 check_format_view (const char *p)
612 const char *q = p;
613 if (!strncmp (p, "view", 4))
615 q += 4;
616 if (*q == '{')
618 for (q++; *q && *q != '}'; q++)
620 if (!strncmp (q, "ascii", 5))
622 mcview_default_hex_mode = 0;
623 q += 4;
625 else if (!strncmp (q, "hex", 3))
627 mcview_default_hex_mode = 1;
628 q += 2;
630 else if (!strncmp (q, "nroff", 5))
632 mcview_default_nroff_flag = 1;
633 q += 4;
635 else if (!strncmp (q, "unform", 6))
637 mcview_default_nroff_flag = 0;
638 q += 5;
641 if (*q == '}')
642 q++;
644 return q - p;
646 return 0;
649 /* --------------------------------------------------------------------------------------------- */
652 check_format_cd (const char *p)
654 return (strncmp (p, "cd", 2)) ? 0 : 3;
657 /* --------------------------------------------------------------------------------------------- */
658 /* Check if p has a "^var\{var-name\}" */
659 /* Returns the number of skipped characters (zero on not found) */
660 /* V will be set to the expanded variable name */
663 check_format_var (const char *p, char **v)
665 const char *q = p;
666 char *var_name;
667 const char *value;
668 const char *dots = 0;
670 *v = 0;
671 if (!strncmp (p, "var{", 4))
673 for (q += 4; *q && *q != '}'; q++)
675 if (*q == ':')
676 dots = q + 1;
678 if (!*q)
679 return 0;
681 if (!dots || dots == q + 5)
683 message (D_ERROR,
684 _("Format error on file Extensions File"),
685 !dots ? _("The %%var macro has no default")
686 : _("The %%var macro has no variable"));
687 return 0;
690 /* Copy the variable name */
691 var_name = g_strndup (p + 4, dots - 2 - (p + 3));
693 value = getenv (var_name);
694 g_free (var_name);
695 if (value)
697 *v = g_strdup (value);
698 return q - p;
700 var_name = g_strndup (dots, q - dots);
701 *v = var_name;
702 return q - p;
704 return 0;
707 /* --------------------------------------------------------------------------------------------- */
709 char *
710 expand_format (struct WEdit *edit_widget, char c, gboolean do_quote)
712 WPanel *panel = NULL;
713 char *(*quote_func) (const char *, int);
714 char *fname = NULL;
715 char *result;
716 char c_lc;
718 #ifndef USE_INTERNAL_EDIT
719 (void) edit_widget;
720 #endif
722 if (c == '%')
723 return g_strdup ("%");
725 if (mc_run_mode == MC_RUN_FULL)
727 if (g_ascii_islower ((gchar) c))
728 panel = current_panel;
729 else
731 if (get_other_type () != view_listing)
732 return g_strdup ("");
733 panel = other_panel;
735 fname = panel->dir.list[panel->selected].fname;
737 #ifdef USE_INTERNAL_EDIT
738 else if (mc_run_mode == MC_RUN_EDITOR)
739 fname = (char *) edit_get_file_name (edit_widget);
740 #endif
742 if (do_quote)
743 quote_func = name_quote;
744 else
745 quote_func = fake_name_quote;
747 c_lc = g_ascii_tolower ((gchar) c);
749 switch (c_lc)
751 case 'f':
752 case 'p':
753 return (*quote_func) (fname, 0);
754 case 'x':
755 return (*quote_func) (extension (fname), 0);
756 case 'd':
758 char *cwd;
759 char *qstr;
761 cwd = g_malloc (MC_MAXPATHLEN + 1);
763 if (panel)
764 g_strlcpy (cwd, panel->cwd, MC_MAXPATHLEN + 1);
765 else
766 mc_get_current_wd (cwd, MC_MAXPATHLEN + 1);
768 qstr = (*quote_func) (cwd, 0);
770 g_free (cwd);
772 return qstr;
774 case 'i': /* indent equal number cursor position in line */
775 #ifdef USE_INTERNAL_EDIT
776 if (edit_widget)
777 return g_strnfill (edit_get_curs_col (edit_widget), ' ');
778 #endif
779 break;
780 case 'y': /* syntax type */
781 #ifdef USE_INTERNAL_EDIT
782 if (edit_widget)
784 const char *syntax_type = edit_get_syntax_type (edit_widget);
785 if (syntax_type != NULL)
786 return g_strdup (syntax_type);
788 #endif
789 break;
790 case 'k': /* block file name */
791 case 'b': /* block file name / strip extension */
793 #ifdef USE_INTERNAL_EDIT
794 if (edit_widget)
796 char *file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE);
797 fname = (*quote_func) (file, 0);
798 g_free (file);
799 return fname;
801 #endif
802 if (c_lc == 'b')
803 return strip_ext ((*quote_func) (fname, 0));
804 break;
806 case 'n': /* strip extension in editor */
807 #ifdef USE_INTERNAL_EDIT
808 if (edit_widget)
809 return strip_ext ((*quote_func) (fname, 0));
810 #endif
811 break;
812 case 'm': /* menu file name */
813 if (menu)
814 return (*quote_func) (menu, 0);
815 break;
816 case 's':
817 if (!panel || !panel->marked)
818 return (*quote_func) (fname, 0);
820 /* Fall through */
822 case 't':
823 case 'u':
825 int length = 2, i;
826 char *block, *tmp;
828 if (!panel)
829 return g_strdup ("");
831 for (i = 0; i < panel->count; i++)
832 if (panel->dir.list[i].f.marked)
833 length += strlen (panel->dir.list[i].fname) + 1; /* for space */
835 block = g_malloc (length * 2 + 1);
836 *block = 0;
837 for (i = 0; i < panel->count; i++)
838 if (panel->dir.list[i].f.marked)
840 tmp = (*quote_func) (panel->dir.list[i].fname, 0);
841 strcat (block, tmp);
842 g_free (tmp);
843 strcat (block, " ");
844 if (c_lc == 'u')
845 do_file_mark (panel, i, 0);
847 return block;
848 } /* sub case block */
849 } /* switch */
850 result = g_strdup ("% ");
851 result[1] = c;
852 return result;
855 /* --------------------------------------------------------------------------------------------- */
857 * If edit_widget is NULL then we are called from the mc menu,
858 * otherwise we are called from the mcedit menu.
861 gboolean
862 user_menu_cmd (struct WEdit *edit_widget, const char *menu_file, int selected_entry)
864 char *p;
865 char *data, **entries;
866 int max_cols, menu_lines, menu_limit;
867 int col, i, accept_entry = 1;
868 int selected, old_patterns;
869 Listbox *listbox;
870 gboolean res = FALSE;
871 gboolean interactive = FALSE;
873 if (!vfs_current_is_local ())
875 message (D_ERROR, MSG_ERROR, "%s", _("Cannot execute commands on non-local filesystems"));
876 return FALSE;
878 if (menu_file != NULL)
879 menu = g_strdup (menu_file);
880 else
881 menu = g_strdup (edit_widget ? EDIT_LOCAL_MENU : MC_LOCAL_MENU);
882 if (!exist_file (menu) || !menu_file_own (menu))
884 if (menu_file != NULL)
886 message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno));
887 g_free (menu);
888 menu = NULL;
889 return FALSE;
892 g_free (menu);
893 if (edit_widget)
894 menu = concat_dir_and_file (mc_config_get_data_path (), EDIT_HOME_MENU);
895 else
896 menu = g_build_filename (mc_config_get_data_path (), MC_USERMENU_FILE, NULL);
899 if (!exist_file (menu))
901 g_free (menu);
902 menu =
903 concat_dir_and_file (mc_config_get_home_dir (),
904 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
905 if (!exist_file (menu))
907 g_free (menu);
908 menu =
909 concat_dir_and_file (mc_sysconfig_dir,
910 edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
911 if (!exist_file (menu))
913 g_free (menu);
914 menu = concat_dir_and_file
915 (mc_share_data_dir, edit_widget ? EDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
921 if (!g_file_get_contents (menu, &data, NULL, NULL))
923 message (D_ERROR, MSG_ERROR, _("Cannot open file%s\n%s"), menu, unix_error_string (errno));
924 g_free (menu);
925 menu = NULL;
926 return FALSE;
929 max_cols = 0;
930 selected = 0;
931 menu_limit = 0;
932 entries = 0;
934 /* Parse the menu file */
935 old_patterns = easy_patterns;
936 p = check_patterns (data);
937 for (menu_lines = col = 0; *p; str_next_char (&p))
939 if (menu_lines >= menu_limit)
941 char **new_entries;
943 menu_limit += MAX_ENTRIES;
944 new_entries = g_try_realloc (entries, sizeof (new_entries[0]) * menu_limit);
946 if (new_entries == NULL)
947 break;
949 entries = new_entries;
950 new_entries += menu_limit;
951 while (--new_entries >= &entries[menu_lines])
952 *new_entries = NULL;
954 if (col == 0 && !entries[menu_lines])
956 if (*p == '#')
958 /* show prompt if first line of external script is #interactive */
959 if (selected_entry >= 0 && strncmp (p, "#interactive", 12) == 0)
960 interactive = TRUE;
961 /* A commented menu entry */
962 accept_entry = 1;
964 else if (*p == '+')
966 if (*(p + 1) == '=')
968 /* Combined adding and default */
969 p = test_line (edit_widget, p + 1, &accept_entry);
970 if (selected == 0 && accept_entry)
971 selected = menu_lines;
973 else
975 /* A condition for adding the entry */
976 p = test_line (edit_widget, p, &accept_entry);
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 making the entry default */
991 i = 1;
992 p = test_line (edit_widget, p, &i);
993 if (selected == 0 && i)
994 selected = menu_lines;
997 else if (*p != ' ' && *p != '\t' && str_isprint (p))
999 /* A menu entry title line */
1000 if (accept_entry)
1001 entries[menu_lines] = p;
1002 else
1003 accept_entry = 1;
1006 if (*p == '\n')
1008 if (entries[menu_lines])
1010 menu_lines++;
1011 accept_entry = 1;
1013 max_cols = max (max_cols, col);
1014 col = 0;
1016 else
1018 if (*p == '\t')
1019 *p = ' ';
1020 col++;
1024 if (menu_lines == 0)
1026 message (D_ERROR, MSG_ERROR, _("No suitable entries found in %s"), menu);
1027 res = FALSE;
1029 else
1031 if (selected_entry >= 0)
1032 selected = selected_entry;
1033 else
1035 max_cols = min (max (max_cols, col), MAX_ENTRY_LEN);
1037 /* Create listbox */
1038 listbox = create_listbox_window (menu_lines, max_cols + 2, _("User menu"),
1039 "[Menu File Edit]");
1040 /* insert all the items found */
1041 for (i = 0; i < menu_lines; i++)
1043 p = entries[i];
1044 LISTBOX_APPEND_TEXT (listbox, (unsigned char) p[0],
1045 extract_line (p, p + MAX_ENTRY_LEN), p);
1047 /* Select the default entry */
1048 listbox_select_entry (listbox->list, selected);
1050 selected = run_listbox (listbox);
1052 if (selected >= 0)
1054 execute_menu_command (edit_widget, entries[selected], interactive);
1055 res = TRUE;
1058 do_refresh ();
1061 easy_patterns = old_patterns;
1062 g_free (menu);
1063 menu = NULL;
1064 g_free (entries);
1065 g_free (data);
1066 return res;
1069 /* --------------------------------------------------------------------------------------------- */