bootmenu.c revision 1.5 1 1.5 dholland /* $NetBSD: bootmenu.c,v 1.5 2016/06/11 06:58:42 dholland Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * Redistribution and use in source and binary forms, with or without
8 1.1 nonaka * modification, are permitted provided that the following conditions
9 1.1 nonaka * are met:
10 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
11 1.1 nonaka * notice, this list of conditions and the following disclaimer.
12 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
14 1.1 nonaka * documentation and/or other materials provided with the distribution.
15 1.1 nonaka *
16 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 nonaka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 nonaka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 nonaka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 nonaka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 nonaka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 nonaka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 nonaka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 nonaka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 nonaka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 nonaka * POSSIBILITY OF SUCH DAMAGE.
27 1.1 nonaka */
28 1.1 nonaka
29 1.1 nonaka #include <sys/types.h>
30 1.1 nonaka #include <sys/reboot.h>
31 1.1 nonaka #include <sys/bootblock.h>
32 1.1 nonaka
33 1.1 nonaka #include "boot.h"
34 1.1 nonaka #include "unixdev.h"
35 1.1 nonaka #include "bootmenu.h"
36 1.1 nonaka #include "pathnames.h"
37 1.1 nonaka
38 1.1 nonaka #define MENUFORMAT_AUTO 0
39 1.1 nonaka #define MENUFORMAT_NUMBER 1
40 1.1 nonaka #define MENUFORMAT_LETTER 2
41 1.1 nonaka
42 1.1 nonaka void
43 1.1 nonaka parsebootconf(const char *conf)
44 1.1 nonaka {
45 1.3 rtr perform_bootcfg(conf, &bootcfg_do_noop, 0);
46 1.1 nonaka }
47 1.1 nonaka
48 1.1 nonaka /*
49 1.1 nonaka * doboottypemenu will render the menu and parse any user input
50 1.1 nonaka */
51 1.1 nonaka static int
52 1.1 nonaka getchoicefrominput(char *input, int def)
53 1.1 nonaka {
54 1.1 nonaka int choice = -1;
55 1.1 nonaka
56 1.1 nonaka if (*input == '\0' || *input == '\r' || *input == '\n')
57 1.1 nonaka choice = def;
58 1.3 rtr else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
59 1.1 nonaka choice = (*input) - 'A';
60 1.3 rtr else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
61 1.1 nonaka choice = (*input) - 'a';
62 1.4 isaki else if (isdigit(*input)) {
63 1.1 nonaka choice = atoi(input) - 1;
64 1.3 rtr if (choice < 0 || choice >= bootcfg_info.nummenu)
65 1.1 nonaka choice = -1;
66 1.1 nonaka }
67 1.1 nonaka return choice;
68 1.1 nonaka }
69 1.1 nonaka
70 1.1 nonaka void
71 1.1 nonaka doboottypemenu(void)
72 1.1 nonaka {
73 1.1 nonaka char input[80], *ic, *oc;
74 1.1 nonaka int choice;
75 1.1 nonaka
76 1.1 nonaka printf("\n");
77 1.1 nonaka /* Display menu */
78 1.3 rtr if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
79 1.3 rtr for (choice = 0; choice < bootcfg_info.nummenu; choice++)
80 1.1 nonaka printf(" %c. %s\n", choice + 'A',
81 1.3 rtr bootcfg_info.desc[choice]);
82 1.1 nonaka } else {
83 1.1 nonaka /* Can't use %2d format string with libsa */
84 1.3 rtr for (choice = 0; choice < bootcfg_info.nummenu; choice++)
85 1.1 nonaka printf(" %s%d. %s\n",
86 1.1 nonaka (choice < 9) ? " " : "",
87 1.1 nonaka choice + 1,
88 1.3 rtr bootcfg_info.desc[choice]);
89 1.1 nonaka }
90 1.1 nonaka choice = -1;
91 1.1 nonaka for (;;) {
92 1.1 nonaka input[0] = '\0';
93 1.1 nonaka
94 1.3 rtr if (bootcfg_info.timeout < 0) {
95 1.3 rtr if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
96 1.1 nonaka printf("\nOption: [%c]:",
97 1.3 rtr bootcfg_info.def + 'A');
98 1.1 nonaka else
99 1.1 nonaka printf("\nOption: [%d]:",
100 1.3 rtr bootcfg_info.def + 1);
101 1.1 nonaka
102 1.5 dholland kgets(input, sizeof(input));
103 1.3 rtr choice = getchoicefrominput(input, bootcfg_info.def);
104 1.3 rtr } else if (bootcfg_info.timeout == 0)
105 1.3 rtr choice = bootcfg_info.def;
106 1.1 nonaka else {
107 1.1 nonaka printf("\nChoose an option; RETURN for default; "
108 1.1 nonaka "SPACE to stop countdown.\n");
109 1.3 rtr if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
110 1.1 nonaka printf("Option %c will be chosen in ",
111 1.3 rtr bootcfg_info.def + 'A');
112 1.1 nonaka else
113 1.1 nonaka printf("Option %d will be chosen in ",
114 1.3 rtr bootcfg_info.def + 1);
115 1.3 rtr input[0] = awaitkey(bootcfg_info.timeout, 1);
116 1.1 nonaka input[1] = '\0';
117 1.3 rtr choice = getchoicefrominput(input, bootcfg_info.def);
118 1.1 nonaka /* If invalid key pressed, drop to menu */
119 1.1 nonaka if (choice == -1)
120 1.3 rtr bootcfg_info.timeout = -1;
121 1.1 nonaka }
122 1.1 nonaka if (choice < 0)
123 1.1 nonaka continue;
124 1.3 rtr if (!strcmp(bootcfg_info.command[choice], "prompt")) {
125 1.1 nonaka printf("type \"?\" or \"help\" for help.\n");
126 1.1 nonaka bootmenu(); /* does not return */
127 1.1 nonaka } else {
128 1.3 rtr ic = bootcfg_info.command[choice];
129 1.1 nonaka /* Split command string at ; into separate commands */
130 1.1 nonaka do {
131 1.1 nonaka oc = input;
132 1.1 nonaka /* Look for ; separator */
133 1.1 nonaka for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
134 1.1 nonaka *oc++ = *ic;
135 1.1 nonaka if (*input == '\0')
136 1.1 nonaka continue;
137 1.1 nonaka /* Strip out any trailing spaces */
138 1.1 nonaka oc--;
139 1.1 nonaka for (; *oc == ' ' && oc > input; oc--);
140 1.1 nonaka *++oc = '\0';
141 1.1 nonaka if (*ic == COMMAND_SEPARATOR)
142 1.1 nonaka ic++;
143 1.1 nonaka /* Stop silly command strings like ;;; */
144 1.1 nonaka if (*input != '\0')
145 1.1 nonaka docommand(input);
146 1.1 nonaka /* Skip leading spaces */
147 1.1 nonaka for (; *ic == ' '; ic++);
148 1.1 nonaka } while (*ic);
149 1.1 nonaka }
150 1.1 nonaka }
151 1.1 nonaka }
152