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
|
/* $NetBSD: bootmenu.c,v 1.5 2016/06/11 06:58:42 dholland Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/reboot.h>
#include <sys/bootblock.h>
#include "boot.h"
#include "unixdev.h"
#include "bootmenu.h"
#include "pathnames.h"
#define MENUFORMAT_AUTO 0
#define MENUFORMAT_NUMBER 1
#define MENUFORMAT_LETTER 2
void
parsebootconf(const char *conf)
{
perform_bootcfg(conf, &bootcfg_do_noop, 0);
}
/*
* doboottypemenu will render the menu and parse any user input
*/
static int
getchoicefrominput(char *input, int def)
{
int choice = -1;
if (*input == '\0' || *input == '\r' || *input == '\n')
choice = def;
else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
choice = (*input) - 'A';
else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
choice = (*input) - 'a';
else if (isdigit(*input)) {
choice = atoi(input) - 1;
if (choice < 0 || choice >= bootcfg_info.nummenu)
choice = -1;
}
return choice;
}
void
doboottypemenu(void)
{
char input[80], *ic, *oc;
int choice;
printf("\n");
/* Display menu */
if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
for (choice = 0; choice < bootcfg_info.nummenu; choice++)
printf(" %c. %s\n", choice + 'A',
bootcfg_info.desc[choice]);
} else {
/* Can't use %2d format string with libsa */
for (choice = 0; choice < bootcfg_info.nummenu; choice++)
printf(" %s%d. %s\n",
(choice < 9) ? " " : "",
choice + 1,
bootcfg_info.desc[choice]);
}
choice = -1;
for (;;) {
input[0] = '\0';
if (bootcfg_info.timeout < 0) {
if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
printf("\nOption: [%c]:",
bootcfg_info.def + 'A');
else
printf("\nOption: [%d]:",
bootcfg_info.def + 1);
kgets(input, sizeof(input));
choice = getchoicefrominput(input, bootcfg_info.def);
} else if (bootcfg_info.timeout == 0)
choice = bootcfg_info.def;
else {
printf("\nChoose an option; RETURN for default; "
"SPACE to stop countdown.\n");
if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
printf("Option %c will be chosen in ",
bootcfg_info.def + 'A');
else
printf("Option %d will be chosen in ",
bootcfg_info.def + 1);
input[0] = awaitkey(bootcfg_info.timeout, 1);
input[1] = '\0';
choice = getchoicefrominput(input, bootcfg_info.def);
/* If invalid key pressed, drop to menu */
if (choice == -1)
bootcfg_info.timeout = -1;
}
if (choice < 0)
continue;
if (!strcmp(bootcfg_info.command[choice], "prompt")) {
printf("type \"?\" or \"help\" for help.\n");
bootmenu(); /* does not return */
} else {
ic = bootcfg_info.command[choice];
/* Split command string at ; into separate commands */
do {
oc = input;
/* Look for ; separator */
for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
*oc++ = *ic;
if (*input == '\0')
continue;
/* Strip out any trailing spaces */
oc--;
for (; *oc == ' ' && oc > input; oc--);
*++oc = '\0';
if (*ic == COMMAND_SEPARATOR)
ic++;
/* Stop silly command strings like ;;; */
if (*input != '\0')
docommand(input);
/* Skip leading spaces */
for (; *ic == ' '; ic++);
} while (*ic);
}
}
}
|