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