xref: /onnv-gate/usr/src/cmd/format/menu.c (revision 6590:4867b3aa761a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6590Syl194034  * Common Development and Distribution License (the "License").
6*6590Syl194034  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6590Syl194034  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * This file contains routines relating to running the menus.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include "global.h"
330Sstevel@tonic-gate #include "menu.h"
340Sstevel@tonic-gate #include "misc.h"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifdef __STDC__
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
390Sstevel@tonic-gate static int	(*find_enabled_menu_item())(struct menu_item *menu, int item);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #else	/* __STDC__ */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
440Sstevel@tonic-gate static int	(*find_enabled_menu_item())();
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #endif	/* __STDC__ */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static char	cur_title[MAXPATHLEN];
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * This routine takes a menu struct and concatenates the
520Sstevel@tonic-gate  * command names into an array of strings describing the menu.
530Sstevel@tonic-gate  * All menus have a 'quit' command at the bottom to exit the menu.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate char **
create_menu_list(menu)560Sstevel@tonic-gate create_menu_list(menu)
570Sstevel@tonic-gate 	struct	menu_item *menu;
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	register struct menu_item *mptr;
600Sstevel@tonic-gate 	register char	**cpptr;
610Sstevel@tonic-gate 	register char	**list;
620Sstevel@tonic-gate 	int		nitems;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/*
650Sstevel@tonic-gate 	 * A minimum list consists of the quit command, followed
660Sstevel@tonic-gate 	 * by a terminating null.
670Sstevel@tonic-gate 	 */
680Sstevel@tonic-gate 	nitems = 2;
690Sstevel@tonic-gate 	/*
700Sstevel@tonic-gate 	 * Count the number of active commands in the menu and allocate
710Sstevel@tonic-gate 	 * space for the array of pointers.
720Sstevel@tonic-gate 	 */
730Sstevel@tonic-gate 	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
740Sstevel@tonic-gate 		if ((*mptr->menu_state)())
750Sstevel@tonic-gate 			nitems++;
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	list = (char **)zalloc(nitems * sizeof (char *));
780Sstevel@tonic-gate 	cpptr = list;
790Sstevel@tonic-gate 	/*
800Sstevel@tonic-gate 	 * Fill in the array with the names of the menu commands.
810Sstevel@tonic-gate 	 */
820Sstevel@tonic-gate 	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
830Sstevel@tonic-gate 		if ((*mptr->menu_state)()) {
840Sstevel@tonic-gate 			*cpptr++ = mptr->menu_cmd;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * Add the 'quit' command to the end.
890Sstevel@tonic-gate 	 */
900Sstevel@tonic-gate 	*cpptr = "quit";
910Sstevel@tonic-gate 	return (list);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * This routine takes a menu list created by the above routine and
960Sstevel@tonic-gate  * prints it nicely on the screen.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate void
display_menu_list(list)990Sstevel@tonic-gate display_menu_list(list)
1000Sstevel@tonic-gate 	char	**list;
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	register char **str;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	for (str = list; *str != NULL; str++)
1050Sstevel@tonic-gate 		fmt_print("        %s\n", *str);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate  * Find the "i"th enabled menu in a menu list.  This depends
1100Sstevel@tonic-gate  * on menu_state() returning the same status as when the
1110Sstevel@tonic-gate  * original list of enabled commands was constructed.
1120Sstevel@tonic-gate  */
1130Sstevel@tonic-gate static int (*
1140Sstevel@tonic-gate find_enabled_menu_item(menu, item))()
1150Sstevel@tonic-gate 	struct menu_item	*menu;
1160Sstevel@tonic-gate 	int			item;
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	struct menu_item	*mp;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	for (mp = menu; mp->menu_cmd != NULL; mp++) {
1210Sstevel@tonic-gate 		if ((*mp->menu_state)()) {
1220Sstevel@tonic-gate 			if (item-- == 0) {
1230Sstevel@tonic-gate 				return (mp->menu_func);
1240Sstevel@tonic-gate 			}
1250Sstevel@tonic-gate 		}
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	return (NULL);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * This routine 'runs' a menu.  It repeatedly requests a command and
1330Sstevel@tonic-gate  * executes the command chosen.  It exits when the 'quit' command is
1340Sstevel@tonic-gate  * executed.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate /*ARGSUSED*/
1370Sstevel@tonic-gate void
run_menu(menu,title,prompt,display_flag)1380Sstevel@tonic-gate run_menu(menu, title, prompt, display_flag)
1390Sstevel@tonic-gate 	struct	menu_item *menu;
1400Sstevel@tonic-gate 	char	*title;
1410Sstevel@tonic-gate 	char	*prompt;
1420Sstevel@tonic-gate 	int	display_flag;
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	char		**list;
1450Sstevel@tonic-gate 	int		i;
1460Sstevel@tonic-gate 	struct		env env;
1470Sstevel@tonic-gate 	u_ioparam_t	ioparam;
1480Sstevel@tonic-gate 	int		(*f)();
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * Create the menu list and display it.
1530Sstevel@tonic-gate 	 */
1540Sstevel@tonic-gate 	list = create_menu_list(menu);
1550Sstevel@tonic-gate 	(void) strcpy(cur_title, title);
1560Sstevel@tonic-gate 	fmt_print("\n\n%s MENU:\n", title);
1570Sstevel@tonic-gate 	display_menu_list(list);
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Save the environment so a ctrl-C out of a command lands here.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	saveenv(env);
1620Sstevel@tonic-gate 	for (;;) {
1630Sstevel@tonic-gate 		/*
1640Sstevel@tonic-gate 		 * Ask the user which command they want to run.
1650Sstevel@tonic-gate 		 */
1660Sstevel@tonic-gate 		ioparam.io_charlist = list;
1670Sstevel@tonic-gate 		i = input(FIO_MSTR, prompt, '>', &ioparam,
1680Sstevel@tonic-gate 		    (int *)NULL, CMD_INPUT);
1690Sstevel@tonic-gate 		/*
1700Sstevel@tonic-gate 		 * If they choose 'quit', the party's over.
1710Sstevel@tonic-gate 		 */
1720Sstevel@tonic-gate 		if ((f = find_enabled_menu_item(menu, i)) == NULL)
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		/*
1760Sstevel@tonic-gate 		 * Mark the saved environment active so the user can now
1770Sstevel@tonic-gate 		 * do a ctrl-C to get out of the command.
1780Sstevel@tonic-gate 		 */
1790Sstevel@tonic-gate 		useenv();
1800Sstevel@tonic-gate 		/*
1810Sstevel@tonic-gate 		 * Run the command.  If it returns an error and we are
1820Sstevel@tonic-gate 		 * running out of a command file, the party's really over.
1830Sstevel@tonic-gate 		 */
1840Sstevel@tonic-gate 		if ((*f)() && option_f)
1850Sstevel@tonic-gate 			fullabort();
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * Mark the saved environment inactive so ctrl-C doesn't
1880Sstevel@tonic-gate 		 * work at the menu itself.
1890Sstevel@tonic-gate 		 */
1900Sstevel@tonic-gate 		unuseenv();
1910Sstevel@tonic-gate 		/*
1920Sstevel@tonic-gate 		 * Since menu items are dynamic, some commands
1930Sstevel@tonic-gate 		 * cause changes to occur.  Destroy the old menu,
1940Sstevel@tonic-gate 		 * and rebuild it, so we're always up-to-date.
1950Sstevel@tonic-gate 		 */
1960Sstevel@tonic-gate 		destroy_data((char *)list);
1970Sstevel@tonic-gate 		list = create_menu_list(menu);
1980Sstevel@tonic-gate 		/*
1990Sstevel@tonic-gate 		 * Redisplay menu, if we're returning to this one.
2000Sstevel@tonic-gate 		 */
2010Sstevel@tonic-gate 		if (cur_menu != last_menu) {
2020Sstevel@tonic-gate 			last_menu = cur_menu;
2030Sstevel@tonic-gate 			(void) strcpy(cur_title, title);
2040Sstevel@tonic-gate 			fmt_print("\n\n%s MENU:\n", title);
2050Sstevel@tonic-gate 			display_menu_list(list);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * Clean up the environment stack and throw away the menu list.
2100Sstevel@tonic-gate 	 */
2110Sstevel@tonic-gate 	clearenv();
2120Sstevel@tonic-gate 	destroy_data((char *)list);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * re-display the screen after exiting from shell escape
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate void
redisplay_menu_list(list)2200Sstevel@tonic-gate redisplay_menu_list(list)
2210Sstevel@tonic-gate char **list;
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	fmt_print("\n\n%s MENU:\n", cur_title);
2240Sstevel@tonic-gate 	display_menu_list(list);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * Glue to always return true.  Used for menu items which
2300Sstevel@tonic-gate  * are always enabled.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate int
2330Sstevel@tonic-gate true()
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	return (1);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*
2390Sstevel@tonic-gate  * Note: The following functions are used to enable the inclusion
2400Sstevel@tonic-gate  * of device specific options (see init_menus.c). But when we are
2410Sstevel@tonic-gate  * running non interactively with commands taken from a script file,
2420Sstevel@tonic-gate  * current disk (cur_disk, cur_type, cur_ctype) may not be defined.
2430Sstevel@tonic-gate  * They get defined when the script selects a disk using "disk" option
2440Sstevel@tonic-gate  * in the main menu. However, in the normal interactive mode, the disk
2450Sstevel@tonic-gate  * selection happens before entering the main menu.
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Return true for menu items enabled only for embedded SCSI controllers
2490Sstevel@tonic-gate  */
2500Sstevel@tonic-gate int
embedded_scsi()2510Sstevel@tonic-gate embedded_scsi()
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
2540Sstevel@tonic-gate 		return (0);
2550Sstevel@tonic-gate 	return (EMBEDDED_SCSI);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Return false for menu items disabled only for embedded SCSI controllers
2600Sstevel@tonic-gate  */
2610Sstevel@tonic-gate int
not_embedded_scsi()2620Sstevel@tonic-gate not_embedded_scsi()
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
2650Sstevel@tonic-gate 		return (0);
2660Sstevel@tonic-gate 	return (!EMBEDDED_SCSI);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Return false for menu items disabled for scsi controllers
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate int
not_scsi()2730Sstevel@tonic-gate not_scsi()
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
2760Sstevel@tonic-gate 		return (0);
2770Sstevel@tonic-gate 	return (!SCSI);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Return false for menu items disabled for efi labels
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate int
not_efi()2840Sstevel@tonic-gate not_efi()
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
2870Sstevel@tonic-gate 		return (0);
2880Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_EFI)
2890Sstevel@tonic-gate 		return (0);
2900Sstevel@tonic-gate 	return (1);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate int
disp_expert_change_expert_efi()2940Sstevel@tonic-gate disp_expert_change_expert_efi()
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
2970Sstevel@tonic-gate 		return (0);
2980Sstevel@tonic-gate 	if ((cur_disk->label_type == L_TYPE_EFI) && expert_mode)
2990Sstevel@tonic-gate 		return (1);
3000Sstevel@tonic-gate 	if (cur_disk->label_type != L_TYPE_EFI)
3010Sstevel@tonic-gate 		return (1);
3020Sstevel@tonic-gate 	return (0);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate int
disp_expand_efi()306*6590Syl194034 disp_expand_efi()
307*6590Syl194034 {
308*6590Syl194034 	if ((cur_disk == NULL) && option_f)
309*6590Syl194034 		return (0);
310*6590Syl194034 	if (cur_disk->label_type != L_TYPE_EFI)
311*6590Syl194034 		return (0);
312*6590Syl194034 	if (cur_parts == NULL)
313*6590Syl194034 		return (0);
314*6590Syl194034 	return (1);
315*6590Syl194034 }
316*6590Syl194034 
317*6590Syl194034 int
disp_all_change_expert_efi()3180Sstevel@tonic-gate disp_all_change_expert_efi()
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
3210Sstevel@tonic-gate 		return (0);
3220Sstevel@tonic-gate 	if ((cur_disk->label_type != L_TYPE_EFI) || (!expert_mode))
3230Sstevel@tonic-gate 		return (0);
3240Sstevel@tonic-gate 	return (1);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * Return true for menu items enabled scsi controllers
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate int
scsi()3310Sstevel@tonic-gate scsi()
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
3340Sstevel@tonic-gate 		return (0);
3350Sstevel@tonic-gate 	return (SCSI);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate  * Return true for menu items enabled if expert mode is enabled
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate int
scsi_expert()3430Sstevel@tonic-gate scsi_expert()
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
3460Sstevel@tonic-gate 		return (0);
3470Sstevel@tonic-gate 	return (SCSI && expert_mode);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate #if	defined(i386)
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * Return true for menu items enabled if expert mode is enabled
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate int
expert()3550Sstevel@tonic-gate expert()
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	return (expert_mode);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate #endif	/* defined(i386) */
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * Return true for menu items enabled if developer mode is enabled
3630Sstevel@tonic-gate  */
3640Sstevel@tonic-gate int
developer()3650Sstevel@tonic-gate developer()
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	return (dev_expert);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * For x86, always return true for menu items enabled
3720Sstevel@tonic-gate  *	since fdisk is already supported on these two platforms.
3730Sstevel@tonic-gate  * For Sparc, only return true for menu items enabled
3740Sstevel@tonic-gate  *	if a PCATA disk is selected.
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate int
support_fdisk_on_sparc()3770Sstevel@tonic-gate support_fdisk_on_sparc()
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate #if defined(sparc)
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * If it's a SCSI disk then we don't support fdisk and we
3820Sstevel@tonic-gate 	 * don't need to know the type cause we can ask the disk,
3830Sstevel@tonic-gate 	 * therefore we return true only if we *KNOW* it's an ATA
3840Sstevel@tonic-gate 	 * disk.
3850Sstevel@tonic-gate 	 */
3860Sstevel@tonic-gate 	if (cur_ctype && cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
3870Sstevel@tonic-gate 		return (1);
3880Sstevel@tonic-gate 	} else {
3890Sstevel@tonic-gate 		return (0);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate #elif defined(i386)
3920Sstevel@tonic-gate 	return (1);
3930Sstevel@tonic-gate #else
3940Sstevel@tonic-gate #error  No Platform defined
3950Sstevel@tonic-gate #endif /* defined(sparc) */
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate }
398