summaryrefslogtreecommitdiff
path: root/usr/src/cmd/format/menu.c
blob: ea2cb1d2484a996387776e1c4466cedc00499a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This file contains routines relating to running the menus.
 */
#include <string.h>
#include "global.h"
#include "menu.h"
#include "misc.h"

#ifdef __STDC__

/* Function prototypes for ANSI C Compilers */
static int	(*find_enabled_menu_item())(struct menu_item *menu, int item);

#else	/* __STDC__ */

/* Function prototypes for non-ANSI C Compilers */
static int	(*find_enabled_menu_item())();

#endif	/* __STDC__ */

static char	cur_title[MAXPATHLEN];

/*
 * This routine takes a menu struct and concatenates the
 * command names into an array of strings describing the menu.
 * All menus have a 'quit' command at the bottom to exit the menu.
 */
char **
create_menu_list(menu)
	struct	menu_item *menu;
{
	register struct menu_item *mptr;
	register char	**cpptr;
	register char	**list;
	int		nitems;

	/*
	 * A minimum list consists of the quit command, followed
	 * by a terminating null.
	 */
	nitems = 2;
	/*
	 * Count the number of active commands in the menu and allocate
	 * space for the array of pointers.
	 */
	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
		if ((*mptr->menu_state)())
			nitems++;
	}
	list = (char **)zalloc(nitems * sizeof (char *));
	cpptr = list;
	/*
	 * Fill in the array with the names of the menu commands.
	 */
	for (mptr = menu; mptr->menu_cmd != NULL; mptr++) {
		if ((*mptr->menu_state)()) {
			*cpptr++ = mptr->menu_cmd;
		}
	}
	/*
	 * Add the 'quit' command to the end.
	 */
	*cpptr = "quit";
	return (list);
}

/*
 * This routine takes a menu list created by the above routine and
 * prints it nicely on the screen.
 */
void
display_menu_list(list)
	char	**list;
{
	register char **str;

	for (str = list; *str != NULL; str++)
		fmt_print("        %s\n", *str);
}

/*
 * Find the "i"th enabled menu in a menu list.  This depends
 * on menu_state() returning the same status as when the
 * original list of enabled commands was constructed.
 */
static int (*
find_enabled_menu_item(menu, item))()
	struct menu_item	*menu;
	int			item;
{
	struct menu_item	*mp;

	for (mp = menu; mp->menu_cmd != NULL; mp++) {
		if ((*mp->menu_state)()) {
			if (item-- == 0) {
				return (mp->menu_func);
			}
		}
	}

	return (NULL);
}

/*
 * This routine 'runs' a menu.  It repeatedly requests a command and
 * executes the command chosen.  It exits when the 'quit' command is
 * executed.
 */
/*ARGSUSED*/
void
run_menu(menu, title, prompt, display_flag)
	struct	menu_item *menu;
	char	*title;
	char	*prompt;
	int	display_flag;
{
	char		**list;
	int		i;
	struct		env env;
	u_ioparam_t	ioparam;
	int		(*f)();


	/*
	 * Create the menu list and display it.
	 */
	list = create_menu_list(menu);
	(void) strcpy(cur_title, title);
	fmt_print("\n\n%s MENU:\n", title);
	display_menu_list(list);
	/*
	 * Save the environment so a ctrl-C out of a command lands here.
	 */
	saveenv(env);
	for (;;) {
		/*
		 * Ask the user which command they want to run.
		 */
		ioparam.io_charlist = list;
		i = input(FIO_MSTR, prompt, '>', &ioparam,
		    (int *)NULL, CMD_INPUT);
		/*
		 * If they choose 'quit', the party's over.
		 */
		if ((f = find_enabled_menu_item(menu, i)) == NULL)
			break;

		/*
		 * Mark the saved environment active so the user can now
		 * do a ctrl-C to get out of the command.
		 */
		useenv();
		/*
		 * Run the command.  If it returns an error and we are
		 * running out of a command file, the party's really over.
		 */
		if ((*f)() && option_f)
			fullabort();
		/*
		 * Mark the saved environment inactive so ctrl-C doesn't
		 * work at the menu itself.
		 */
		unuseenv();
		/*
		 * Since menu items are dynamic, some commands
		 * cause changes to occur.  Destroy the old menu,
		 * and rebuild it, so we're always up-to-date.
		 */
		destroy_data((char *)list);
		list = create_menu_list(menu);
		/*
		 * Redisplay menu, if we're returning to this one.
		 */
		if (cur_menu != last_menu) {
			last_menu = cur_menu;
			(void) strcpy(cur_title, title);
			fmt_print("\n\n%s MENU:\n", title);
			display_menu_list(list);
		}
	}
	/*
	 * Clean up the environment stack and throw away the menu list.
	 */
	clearenv();
	destroy_data((char *)list);
}

/*
 * re-display the screen after exiting from shell escape
 *
 */
void
redisplay_menu_list(list)
char **list;
{
	fmt_print("\n\n%s MENU:\n", cur_title);
	display_menu_list(list);
}


/*
 * Glue to always return true.  Used for menu items which
 * are always enabled.
 */
int
true()
{
	return (1);
}

/*
 * Note: The following functions are used to enable the inclusion
 * of device specific options (see init_menus.c). But when we are
 * running non interactively with commands taken from a script file,
 * current disk (cur_disk, cur_type, cur_ctype) may not be defined.
 * They get defined when the script selects a disk using "disk" option
 * in the main menu. However, in the normal interactive mode, the disk
 * selection happens before entering the main menu.
 */
/*
 * Return true for menu items enabled only for embedded SCSI controllers
 */
int
embedded_scsi()
{
	if (cur_ctype == NULL && option_f)
		return (0);
	return (EMBEDDED_SCSI);
}

/*
 * Return false for menu items disabled only for embedded SCSI controllers
 */
int
not_embedded_scsi()
{
	if (cur_ctype == NULL && option_f)
		return (0);
	return (!EMBEDDED_SCSI);
}

/*
 * Return false for menu items disabled for scsi controllers
 */
int
not_scsi()
{
	if (cur_ctype == NULL && option_f)
		return (0);
	return (!SCSI);
}

/*
 * Return false for menu items disabled for efi labels
 */
int
not_efi()
{
	if ((cur_disk == NULL) && option_f)
		return (0);
	if (cur_disk->label_type == L_TYPE_EFI)
		return (0);
	return (1);
}

int
disp_expert_change_expert_efi()
{
	if ((cur_disk == NULL) && option_f)
		return (0);
	if ((cur_disk->label_type == L_TYPE_EFI) && expert_mode)
		return (1);
	if (cur_disk->label_type != L_TYPE_EFI)
		return (1);
	return (0);
}

int
disp_expand_efi()
{
	if ((cur_disk == NULL) && option_f)
		return (0);
	if (cur_disk->label_type != L_TYPE_EFI)
		return (0);
	if (cur_parts == NULL)
		return (0);
	return (1);
}

int
disp_all_change_expert_efi()
{
	if ((cur_disk == NULL) && option_f)
		return (0);
	if ((cur_disk->label_type != L_TYPE_EFI) || (!expert_mode))
		return (0);
	return (1);
}

/*
 * Return true for menu items enabled scsi controllers
 */
int
scsi()
{
	if (cur_ctype == NULL && option_f)
		return (0);
	return (SCSI);
}


/*
 * Return true for menu items enabled if expert mode is enabled
 */
int
scsi_expert()
{
	if (cur_ctype == NULL && option_f)
		return (0);
	return (SCSI && expert_mode);
}

#if	defined(i386)
/*
 * Return true for menu items enabled if expert mode is enabled
 */
int
expert()
{
	return (expert_mode);
}
#endif	/* defined(i386) */

/*
 * Return true for menu items enabled if developer mode is enabled
 */
int
developer()
{
	return (dev_expert);
}

/*
 * For x86, always return true for menu items enabled
 *	since fdisk is already supported on these two platforms.
 * For Sparc, only return true for menu items enabled
 *	if a PCATA disk is selected.
 */
int
support_fdisk_on_sparc()
{
#if defined(sparc)
	/*
	 * If it's a SCSI disk then we don't support fdisk and we
	 * don't need to know the type cause we can ask the disk,
	 * therefore we return true only if we *KNOW* it's an ATA
	 * disk.
	 */
	if (cur_ctype && cur_ctype->ctype_ctype == DKC_PCMCIA_ATA) {
		return (1);
	} else {
		return (0);
	}
#elif defined(i386)
	return (1);
#else
#error  No Platform defined
#endif /* defined(sparc) */

}