xref: /onnv-gate/usr/src/cmd/format/menu.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1991-2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file contains routines relating to running the menus.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include "global.h"
34*0Sstevel@tonic-gate #include "menu.h"
35*0Sstevel@tonic-gate #include "misc.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #ifdef __STDC__
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /* Function prototypes for ANSI C Compilers */
40*0Sstevel@tonic-gate static int	(*find_enabled_menu_item())(struct menu_item *menu, int item);
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #else	/* __STDC__ */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /* Function prototypes for non-ANSI C Compilers */
45*0Sstevel@tonic-gate static int	(*find_enabled_menu_item())();
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #endif	/* __STDC__ */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate static char	cur_title[MAXPATHLEN];
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /*
52*0Sstevel@tonic-gate  * This routine takes a menu struct and concatenates the
53*0Sstevel@tonic-gate  * command names into an array of strings describing the menu.
54*0Sstevel@tonic-gate  * All menus have a 'quit' command at the bottom to exit the menu.
55*0Sstevel@tonic-gate  */
56*0Sstevel@tonic-gate char **
57*0Sstevel@tonic-gate create_menu_list(menu)
58*0Sstevel@tonic-gate 	struct	menu_item *menu;
59*0Sstevel@tonic-gate {
60*0Sstevel@tonic-gate 	register struct menu_item *mptr;
61*0Sstevel@tonic-gate 	register char	**cpptr;
62*0Sstevel@tonic-gate 	register char	**list;
63*0Sstevel@tonic-gate 	int		nitems;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	/*
66*0Sstevel@tonic-gate 	 * A minimum list consists of the quit command, followed
67*0Sstevel@tonic-gate 	 * by a terminating null.
68*0Sstevel@tonic-gate 	 */
69*0Sstevel@tonic-gate 	nitems = 2;
70*0Sstevel@tonic-gate 	/*
71*0Sstevel@tonic-gate 	 * Count the number of active commands in the menu and allocate
72*0Sstevel@tonic-gate 	 * space for the array of pointers.
73*0Sstevel@tonic-gate 	 */
74*0Sstevel@tonic-gate 	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
75*0Sstevel@tonic-gate 		if ((*mptr->menu_state)())
76*0Sstevel@tonic-gate 			nitems++;
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 	list = (char **)zalloc(nitems * sizeof (char *));
79*0Sstevel@tonic-gate 	cpptr = list;
80*0Sstevel@tonic-gate 	/*
81*0Sstevel@tonic-gate 	 * Fill in the array with the names of the menu commands.
82*0Sstevel@tonic-gate 	 */
83*0Sstevel@tonic-gate 	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
84*0Sstevel@tonic-gate 		if ((*mptr->menu_state)()) {
85*0Sstevel@tonic-gate 			*cpptr++ = mptr->menu_cmd;
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	/*
89*0Sstevel@tonic-gate 	 * Add the 'quit' command to the end.
90*0Sstevel@tonic-gate 	 */
91*0Sstevel@tonic-gate 	*cpptr = "quit";
92*0Sstevel@tonic-gate 	return (list);
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * This routine takes a menu list created by the above routine and
97*0Sstevel@tonic-gate  * prints it nicely on the screen.
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate void
100*0Sstevel@tonic-gate display_menu_list(list)
101*0Sstevel@tonic-gate 	char	**list;
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	register char **str;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	for (str = list; *str != NULL; str++)
106*0Sstevel@tonic-gate 		fmt_print("        %s\n", *str);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * Find the "i"th enabled menu in a menu list.  This depends
111*0Sstevel@tonic-gate  * on menu_state() returning the same status as when the
112*0Sstevel@tonic-gate  * original list of enabled commands was constructed.
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate static int (*
115*0Sstevel@tonic-gate find_enabled_menu_item(menu, item))()
116*0Sstevel@tonic-gate 	struct menu_item	*menu;
117*0Sstevel@tonic-gate 	int			item;
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	struct menu_item	*mp;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	for (mp = menu; mp->menu_cmd != NULL; mp++) {
122*0Sstevel@tonic-gate 		if ((*mp->menu_state)()) {
123*0Sstevel@tonic-gate 			if (item-- == 0) {
124*0Sstevel@tonic-gate 				return (mp->menu_func);
125*0Sstevel@tonic-gate 			}
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	return (NULL);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate  * This routine 'runs' a menu.  It repeatedly requests a command and
134*0Sstevel@tonic-gate  * executes the command chosen.  It exits when the 'quit' command is
135*0Sstevel@tonic-gate  * executed.
136*0Sstevel@tonic-gate  */
137*0Sstevel@tonic-gate /*ARGSUSED*/
138*0Sstevel@tonic-gate void
139*0Sstevel@tonic-gate run_menu(menu, title, prompt, display_flag)
140*0Sstevel@tonic-gate 	struct	menu_item *menu;
141*0Sstevel@tonic-gate 	char	*title;
142*0Sstevel@tonic-gate 	char	*prompt;
143*0Sstevel@tonic-gate 	int	display_flag;
144*0Sstevel@tonic-gate {
145*0Sstevel@tonic-gate 	char		**list;
146*0Sstevel@tonic-gate 	int		i;
147*0Sstevel@tonic-gate 	struct		env env;
148*0Sstevel@tonic-gate 	u_ioparam_t	ioparam;
149*0Sstevel@tonic-gate 	int		(*f)();
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/*
153*0Sstevel@tonic-gate 	 * Create the menu list and display it.
154*0Sstevel@tonic-gate 	 */
155*0Sstevel@tonic-gate 	list = create_menu_list(menu);
156*0Sstevel@tonic-gate 	(void) strcpy(cur_title, title);
157*0Sstevel@tonic-gate 	fmt_print("\n\n%s MENU:\n", title);
158*0Sstevel@tonic-gate 	display_menu_list(list);
159*0Sstevel@tonic-gate 	/*
160*0Sstevel@tonic-gate 	 * Save the environment so a ctrl-C out of a command lands here.
161*0Sstevel@tonic-gate 	 */
162*0Sstevel@tonic-gate 	saveenv(env);
163*0Sstevel@tonic-gate 	for (;;) {
164*0Sstevel@tonic-gate 		/*
165*0Sstevel@tonic-gate 		 * Ask the user which command they want to run.
166*0Sstevel@tonic-gate 		 */
167*0Sstevel@tonic-gate 		ioparam.io_charlist = list;
168*0Sstevel@tonic-gate 		i = input(FIO_MSTR, prompt, '>', &ioparam,
169*0Sstevel@tonic-gate 		    (int *)NULL, CMD_INPUT);
170*0Sstevel@tonic-gate 		/*
171*0Sstevel@tonic-gate 		 * If they choose 'quit', the party's over.
172*0Sstevel@tonic-gate 		 */
173*0Sstevel@tonic-gate 		if ((f = find_enabled_menu_item(menu, i)) == NULL)
174*0Sstevel@tonic-gate 			break;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 		/*
177*0Sstevel@tonic-gate 		 * Mark the saved environment active so the user can now
178*0Sstevel@tonic-gate 		 * do a ctrl-C to get out of the command.
179*0Sstevel@tonic-gate 		 */
180*0Sstevel@tonic-gate 		useenv();
181*0Sstevel@tonic-gate 		/*
182*0Sstevel@tonic-gate 		 * Run the command.  If it returns an error and we are
183*0Sstevel@tonic-gate 		 * running out of a command file, the party's really over.
184*0Sstevel@tonic-gate 		 */
185*0Sstevel@tonic-gate 		if ((*f)() && option_f)
186*0Sstevel@tonic-gate 			fullabort();
187*0Sstevel@tonic-gate 		/*
188*0Sstevel@tonic-gate 		 * Mark the saved environment inactive so ctrl-C doesn't
189*0Sstevel@tonic-gate 		 * work at the menu itself.
190*0Sstevel@tonic-gate 		 */
191*0Sstevel@tonic-gate 		unuseenv();
192*0Sstevel@tonic-gate 		/*
193*0Sstevel@tonic-gate 		 * Since menu items are dynamic, some commands
194*0Sstevel@tonic-gate 		 * cause changes to occur.  Destroy the old menu,
195*0Sstevel@tonic-gate 		 * and rebuild it, so we're always up-to-date.
196*0Sstevel@tonic-gate 		 */
197*0Sstevel@tonic-gate 		destroy_data((char *)list);
198*0Sstevel@tonic-gate 		list = create_menu_list(menu);
199*0Sstevel@tonic-gate 		/*
200*0Sstevel@tonic-gate 		 * Redisplay menu, if we're returning to this one.
201*0Sstevel@tonic-gate 		 */
202*0Sstevel@tonic-gate 		if (cur_menu != last_menu) {
203*0Sstevel@tonic-gate 			last_menu = cur_menu;
204*0Sstevel@tonic-gate 			(void) strcpy(cur_title, title);
205*0Sstevel@tonic-gate 			fmt_print("\n\n%s MENU:\n", title);
206*0Sstevel@tonic-gate 			display_menu_list(list);
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	/*
210*0Sstevel@tonic-gate 	 * Clean up the environment stack and throw away the menu list.
211*0Sstevel@tonic-gate 	 */
212*0Sstevel@tonic-gate 	clearenv();
213*0Sstevel@tonic-gate 	destroy_data((char *)list);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate /*
217*0Sstevel@tonic-gate  * re-display the screen after exiting from shell escape
218*0Sstevel@tonic-gate  *
219*0Sstevel@tonic-gate  */
220*0Sstevel@tonic-gate void
221*0Sstevel@tonic-gate redisplay_menu_list(list)
222*0Sstevel@tonic-gate char **list;
223*0Sstevel@tonic-gate {
224*0Sstevel@tonic-gate 	fmt_print("\n\n%s MENU:\n", cur_title);
225*0Sstevel@tonic-gate 	display_menu_list(list);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate /*
230*0Sstevel@tonic-gate  * Glue to always return true.  Used for menu items which
231*0Sstevel@tonic-gate  * are always enabled.
232*0Sstevel@tonic-gate  */
233*0Sstevel@tonic-gate int
234*0Sstevel@tonic-gate true()
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	return (1);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate  * Note: The following functions are used to enable the inclusion
241*0Sstevel@tonic-gate  * of device specific options (see init_menus.c). But when we are
242*0Sstevel@tonic-gate  * running non interactively with commands taken from a script file,
243*0Sstevel@tonic-gate  * current disk (cur_disk, cur_type, cur_ctype) may not be defined.
244*0Sstevel@tonic-gate  * They get defined when the script selects a disk using "disk" option
245*0Sstevel@tonic-gate  * in the main menu. However, in the normal interactive mode, the disk
246*0Sstevel@tonic-gate  * selection happens before entering the main menu.
247*0Sstevel@tonic-gate  */
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate  * Return true for menu items enabled only for embedded SCSI controllers
250*0Sstevel@tonic-gate  */
251*0Sstevel@tonic-gate int
252*0Sstevel@tonic-gate embedded_scsi()
253*0Sstevel@tonic-gate {
254*0Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
255*0Sstevel@tonic-gate 		return (0);
256*0Sstevel@tonic-gate 	return (EMBEDDED_SCSI);
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * Return false for menu items disabled only for embedded SCSI controllers
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate int
263*0Sstevel@tonic-gate not_embedded_scsi()
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
266*0Sstevel@tonic-gate 		return (0);
267*0Sstevel@tonic-gate 	return (!EMBEDDED_SCSI);
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate  * Return false for menu items disabled for scsi controllers
272*0Sstevel@tonic-gate  */
273*0Sstevel@tonic-gate int
274*0Sstevel@tonic-gate not_scsi()
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
277*0Sstevel@tonic-gate 		return (0);
278*0Sstevel@tonic-gate 	return (!SCSI);
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate /*
282*0Sstevel@tonic-gate  * Return false for menu items disabled for efi labels
283*0Sstevel@tonic-gate  */
284*0Sstevel@tonic-gate int
285*0Sstevel@tonic-gate not_efi()
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
288*0Sstevel@tonic-gate 		return (0);
289*0Sstevel@tonic-gate 	if (cur_disk->label_type == L_TYPE_EFI)
290*0Sstevel@tonic-gate 		return (0);
291*0Sstevel@tonic-gate 	return (1);
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate int
295*0Sstevel@tonic-gate disp_expert_change_expert_efi()
296*0Sstevel@tonic-gate {
297*0Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
298*0Sstevel@tonic-gate 		return (0);
299*0Sstevel@tonic-gate 	if ((cur_disk->label_type == L_TYPE_EFI) && expert_mode)
300*0Sstevel@tonic-gate 		return (1);
301*0Sstevel@tonic-gate 	if (cur_disk->label_type != L_TYPE_EFI)
302*0Sstevel@tonic-gate 		return (1);
303*0Sstevel@tonic-gate 	return (0);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate int
307*0Sstevel@tonic-gate disp_all_change_expert_efi()
308*0Sstevel@tonic-gate {
309*0Sstevel@tonic-gate 	if ((cur_disk == NULL) && option_f)
310*0Sstevel@tonic-gate 		return (0);
311*0Sstevel@tonic-gate 	if ((cur_disk->label_type != L_TYPE_EFI) || (!expert_mode))
312*0Sstevel@tonic-gate 		return (0);
313*0Sstevel@tonic-gate 	return (1);
314*0Sstevel@tonic-gate }
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate /*
317*0Sstevel@tonic-gate  * Return true for menu items enabled scsi controllers
318*0Sstevel@tonic-gate  */
319*0Sstevel@tonic-gate int
320*0Sstevel@tonic-gate scsi()
321*0Sstevel@tonic-gate {
322*0Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
323*0Sstevel@tonic-gate 		return (0);
324*0Sstevel@tonic-gate 	return (SCSI);
325*0Sstevel@tonic-gate }
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate /*
329*0Sstevel@tonic-gate  * Return true for menu items enabled if expert mode is enabled
330*0Sstevel@tonic-gate  */
331*0Sstevel@tonic-gate int
332*0Sstevel@tonic-gate scsi_expert()
333*0Sstevel@tonic-gate {
334*0Sstevel@tonic-gate 	if (cur_ctype == NULL && option_f)
335*0Sstevel@tonic-gate 		return (0);
336*0Sstevel@tonic-gate 	return (SCSI && expert_mode);
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate #if	defined(i386)
340*0Sstevel@tonic-gate /*
341*0Sstevel@tonic-gate  * Return true for menu items enabled if expert mode is enabled
342*0Sstevel@tonic-gate  */
343*0Sstevel@tonic-gate int
344*0Sstevel@tonic-gate expert()
345*0Sstevel@tonic-gate {
346*0Sstevel@tonic-gate 	return (expert_mode);
347*0Sstevel@tonic-gate }
348*0Sstevel@tonic-gate #endif	/* defined(i386) */
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate /*
351*0Sstevel@tonic-gate  * Return true for menu items enabled if developer mode is enabled
352*0Sstevel@tonic-gate  */
353*0Sstevel@tonic-gate int
354*0Sstevel@tonic-gate developer()
355*0Sstevel@tonic-gate {
356*0Sstevel@tonic-gate 	return (dev_expert);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate /*
360*0Sstevel@tonic-gate  * For x86, always return true for menu items enabled
361*0Sstevel@tonic-gate  *	since fdisk is already supported on these two platforms.
362*0Sstevel@tonic-gate  * For Sparc, only return true for menu items enabled
363*0Sstevel@tonic-gate  *	if a PCATA disk is selected.
364*0Sstevel@tonic-gate  */
365*0Sstevel@tonic-gate int
366*0Sstevel@tonic-gate support_fdisk_on_sparc()
367*0Sstevel@tonic-gate {
368*0Sstevel@tonic-gate #if defined(sparc)
369*0Sstevel@tonic-gate 	/*
370*0Sstevel@tonic-gate 	 * If it's a SCSI disk then we don't support fdisk and we
371*0Sstevel@tonic-gate 	 * don't need to know the type cause we can ask the disk,
372*0Sstevel@tonic-gate 	 * therefore we return true only if we *KNOW* it's an ATA
373*0Sstevel@tonic-gate 	 * disk.
374*0Sstevel@tonic-gate 	 */
375*0Sstevel@tonic-gate 	if (cur_ctype && cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
376*0Sstevel@tonic-gate 		return (1);
377*0Sstevel@tonic-gate 	} else {
378*0Sstevel@tonic-gate 		return (0);
379*0Sstevel@tonic-gate 	}
380*0Sstevel@tonic-gate #elif defined(i386)
381*0Sstevel@tonic-gate 	return (1);
382*0Sstevel@tonic-gate #else
383*0Sstevel@tonic-gate #error  No Platform defined
384*0Sstevel@tonic-gate #endif /* defined(sparc) */
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate }
387