Merge commit '2cedd8f0ecbd2b29bf0aac72bb8b7413b0326938' into merges
[unleashed.git] / usr / src / cmd / format / menu.c
blob528304532756534e2248c718e8a955bae79f9889
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * This file contains routines relating to running the menus.
31 #include <string.h>
32 #include "global.h"
33 #include "menu.h"
34 #include "misc.h"
36 #ifdef __STDC__
38 /* Function prototypes for ANSI C Compilers */
39 static int (*find_enabled_menu_item())(struct menu_item *menu, int item);
41 #else /* __STDC__ */
43 /* Function prototypes for non-ANSI C Compilers */
44 static int (*find_enabled_menu_item())();
46 #endif /* __STDC__ */
48 static char cur_title[MAXPATHLEN];
51 * This routine takes a menu struct and concatenates the
52 * command names into an array of strings describing the menu.
53 * All menus have a 'quit' command at the bottom to exit the menu.
55 char **
56 create_menu_list(menu)
57 struct menu_item *menu;
59 register struct menu_item *mptr;
60 register char **cpptr;
61 register char **list;
62 int nitems;
65 * A minimum list consists of the quit command, followed
66 * by a terminating null.
68 nitems = 2;
70 * Count the number of active commands in the menu and allocate
71 * space for the array of pointers.
73 for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
74 if ((*mptr->menu_state)())
75 nitems++;
77 list = (char **)zalloc(nitems * sizeof (char *));
78 cpptr = list;
80 * Fill in the array with the names of the menu commands.
82 for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
83 if ((*mptr->menu_state)()) {
84 *cpptr++ = mptr->menu_cmd;
88 * Add the 'quit' command to the end.
90 *cpptr = "quit";
91 return (list);
95 * This routine takes a menu list created by the above routine and
96 * prints it nicely on the screen.
98 void
99 display_menu_list(list)
100 char **list;
102 register char **str;
104 for (str = list; *str != NULL; str++)
105 fmt_print(" %s\n", *str);
109 * Find the "i"th enabled menu in a menu list. This depends
110 * on menu_state() returning the same status as when the
111 * original list of enabled commands was constructed.
113 static int (*
114 find_enabled_menu_item(menu, item))()
115 struct menu_item *menu;
116 int item;
118 struct menu_item *mp;
120 for (mp = menu; mp->menu_cmd != NULL; mp++) {
121 if ((*mp->menu_state)()) {
122 if (item-- == 0) {
123 return (mp->menu_func);
128 return (NULL);
132 * This routine 'runs' a menu. It repeatedly requests a command and
133 * executes the command chosen. It exits when the 'quit' command is
134 * executed.
136 /*ARGSUSED*/
137 void
138 run_menu(menu, title, prompt, display_flag)
139 struct menu_item *menu;
140 char *title;
141 char *prompt;
142 int display_flag;
144 char **list;
145 int i;
146 struct env env;
147 u_ioparam_t ioparam;
148 int (*f)();
152 * Create the menu list and display it.
154 list = create_menu_list(menu);
155 (void) strcpy(cur_title, title);
156 fmt_print("\n\n%s MENU:\n", title);
157 display_menu_list(list);
159 * Save the environment so a ctrl-C out of a command lands here.
161 saveenv(env);
162 for (;;) {
164 * Ask the user which command they want to run.
166 ioparam.io_charlist = list;
167 i = input(FIO_MSTR, prompt, '>', &ioparam, NULL, CMD_INPUT);
169 * If they choose 'quit', the party's over.
171 if ((f = find_enabled_menu_item(menu, i)) == NULL)
172 break;
175 * Mark the saved environment active so the user can now
176 * do a ctrl-C to get out of the command.
178 useenv();
180 * Run the command. If it returns an error and we are
181 * running out of a command file, the party's really over.
183 if ((*f)() && option_f)
184 fullabort();
186 * Mark the saved environment inactive so ctrl-C doesn't
187 * work at the menu itself.
189 unuseenv();
191 * Since menu items are dynamic, some commands
192 * cause changes to occur. Destroy the old menu,
193 * and rebuild it, so we're always up-to-date.
195 destroy_data((char *)list);
196 list = create_menu_list(menu);
198 * Redisplay menu, if we're returning to this one.
200 if (cur_menu != last_menu) {
201 last_menu = cur_menu;
202 (void) strcpy(cur_title, title);
203 fmt_print("\n\n%s MENU:\n", title);
204 display_menu_list(list);
208 * Clean up the environment stack and throw away the menu list.
210 clearenv();
211 destroy_data((char *)list);
215 * re-display the screen after exiting from shell escape
218 void
219 redisplay_menu_list(list)
220 char **list;
222 fmt_print("\n\n%s MENU:\n", cur_title);
223 display_menu_list(list);
228 * Glue to always return true. Used for menu items which
229 * are always enabled.
232 truefxn()
234 return (1);
238 * Note: The following functions are used to enable the inclusion
239 * of device specific options (see init_menus.c). But when we are
240 * running non interactively with commands taken from a script file,
241 * current disk (cur_disk, cur_type, cur_ctype) may not be defined.
242 * They get defined when the script selects a disk using "disk" option
243 * in the main menu. However, in the normal interactive mode, the disk
244 * selection happens before entering the main menu.
247 * Return true for menu items enabled only for embedded SCSI controllers
250 embedded_scsi()
252 if (cur_ctype == NULL && option_f)
253 return (0);
254 return (EMBEDDED_SCSI);
258 * Return false for menu items disabled only for embedded SCSI controllers
261 not_embedded_scsi()
263 if (cur_ctype == NULL && option_f)
264 return (0);
265 return (!EMBEDDED_SCSI);
269 * Return false for menu items disabled for scsi controllers
272 not_scsi()
274 if (cur_ctype == NULL && option_f)
275 return (0);
276 return (!SCSI);
280 * Return false for menu items disabled for efi labels
283 not_efi()
285 if ((cur_disk == NULL) && option_f)
286 return (0);
287 if (cur_disk->label_type == L_TYPE_EFI)
288 return (0);
289 return (1);
293 disp_expert_change_expert_efi()
295 if ((cur_disk == NULL) && option_f)
296 return (0);
297 if ((cur_disk->label_type == L_TYPE_EFI) && expert_mode)
298 return (1);
299 if (cur_disk->label_type != L_TYPE_EFI)
300 return (1);
301 return (0);
305 disp_expand_efi()
307 if ((cur_disk == NULL) && option_f)
308 return (0);
309 if (cur_disk->label_type != L_TYPE_EFI)
310 return (0);
311 if (cur_parts == NULL)
312 return (0);
313 return (1);
317 disp_all_change_expert_efi()
319 if ((cur_disk == NULL) && option_f)
320 return (0);
321 if ((cur_disk->label_type != L_TYPE_EFI) || (!expert_mode))
322 return (0);
323 return (1);
327 * Return true for menu items enabled scsi controllers
330 scsi()
332 if (cur_ctype == NULL && option_f)
333 return (0);
334 return (SCSI);
339 * Return true for menu items enabled if expert mode is enabled
342 scsi_expert()
344 if (cur_ctype == NULL && option_f)
345 return (0);
346 return (SCSI && expert_mode);
349 #if defined(i386)
351 * Return true for menu items enabled if expert mode is enabled
354 expert()
356 return (expert_mode);
358 #endif /* defined(i386) */
361 * Return true for menu items enabled if developer mode is enabled
364 developer()
366 return (dev_expert);
370 * For x86, always return true for menu items enabled
371 * since fdisk is already supported on these two platforms.
372 * For Sparc, only return true for menu items enabled
373 * if a PCATA disk is selected.
376 support_fdisk_on_sparc()
378 #if defined(sparc)
380 * If it's a SCSI disk then we don't support fdisk and we
381 * don't need to know the type cause we can ask the disk,
382 * therefore we return true only if we *KNOW* it's an ATA
383 * disk.
385 if (cur_ctype && cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
386 return (1);
387 } else {
388 return (0);
390 #elif defined(i386)
391 return (1);
392 #else
393 #error No Platform defined
394 #endif /* defined(sparc) */