Home | History | Annotate | Line # | Download | only in zboot
bootmenu.c revision 1.2.20.1
      1  1.2.20.1     tls /*	$NetBSD: bootmenu.c,v 1.2.20.1 2014/08/10 06:54:12 tls 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 isnum(c) ((c) >= '0' && (c) <= '9')
     39       1.1  nonaka 
     40       1.1  nonaka #define MENUFORMAT_AUTO	  0
     41       1.1  nonaka #define MENUFORMAT_NUMBER 1
     42       1.1  nonaka #define MENUFORMAT_LETTER 2
     43       1.1  nonaka 
     44       1.1  nonaka int
     45       1.1  nonaka atoi(const char *in)
     46       1.1  nonaka {
     47       1.1  nonaka 	char *c;
     48       1.1  nonaka 	int ret;
     49       1.1  nonaka 
     50       1.1  nonaka 	ret = 0;
     51       1.1  nonaka 	c = (char *)in;
     52       1.1  nonaka 	if (*c == '-')
     53       1.1  nonaka 		c++;
     54       1.1  nonaka 	for (; isnum(*c); c++)
     55       1.1  nonaka 		ret = (ret * 10) + (*c - '0');
     56       1.1  nonaka 
     57       1.1  nonaka 	return (*in == '-') ? -ret : ret;
     58       1.1  nonaka }
     59       1.1  nonaka 
     60       1.1  nonaka void
     61       1.1  nonaka parsebootconf(const char *conf)
     62       1.1  nonaka {
     63  1.2.20.1     tls 	perform_bootcfg(conf, &bootcfg_do_noop, 0);
     64       1.1  nonaka }
     65       1.1  nonaka 
     66       1.1  nonaka /*
     67       1.1  nonaka  * doboottypemenu will render the menu and parse any user input
     68       1.1  nonaka  */
     69       1.1  nonaka static int
     70       1.1  nonaka getchoicefrominput(char *input, int def)
     71       1.1  nonaka {
     72       1.1  nonaka 	int choice = -1;
     73       1.1  nonaka 
     74       1.1  nonaka 	if (*input == '\0' || *input == '\r' || *input == '\n')
     75       1.1  nonaka 		choice = def;
     76  1.2.20.1     tls 	else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
     77       1.1  nonaka 		choice = (*input) - 'A';
     78  1.2.20.1     tls 	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
     79       1.1  nonaka 		choice = (*input) - 'a';
     80       1.1  nonaka 	else if (isnum(*input)) {
     81       1.1  nonaka 		choice = atoi(input) - 1;
     82  1.2.20.1     tls 		if (choice < 0 || choice >= bootcfg_info.nummenu)
     83       1.1  nonaka 			choice = -1;
     84       1.1  nonaka 	}
     85       1.1  nonaka 	return choice;
     86       1.1  nonaka }
     87       1.1  nonaka 
     88       1.1  nonaka void
     89       1.1  nonaka doboottypemenu(void)
     90       1.1  nonaka {
     91       1.1  nonaka 	char input[80], *ic, *oc;
     92       1.1  nonaka 	int choice;
     93       1.1  nonaka 
     94       1.1  nonaka 	printf("\n");
     95       1.1  nonaka 	/* Display menu */
     96  1.2.20.1     tls 	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
     97  1.2.20.1     tls 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
     98       1.1  nonaka 			printf("    %c. %s\n", choice + 'A',
     99  1.2.20.1     tls 			    bootcfg_info.desc[choice]);
    100       1.1  nonaka 	} else {
    101       1.1  nonaka 		/* Can't use %2d format string with libsa */
    102  1.2.20.1     tls 		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
    103       1.1  nonaka 			printf("    %s%d. %s\n",
    104       1.1  nonaka 			    (choice < 9) ?  " " : "",
    105       1.1  nonaka 			    choice + 1,
    106  1.2.20.1     tls 			    bootcfg_info.desc[choice]);
    107       1.1  nonaka 	}
    108       1.1  nonaka 	choice = -1;
    109       1.1  nonaka 	for (;;) {
    110       1.1  nonaka 		input[0] = '\0';
    111       1.1  nonaka 
    112  1.2.20.1     tls 		if (bootcfg_info.timeout < 0) {
    113  1.2.20.1     tls 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    114       1.1  nonaka 				printf("\nOption: [%c]:",
    115  1.2.20.1     tls 				    bootcfg_info.def + 'A');
    116       1.1  nonaka 			else
    117       1.1  nonaka 				printf("\nOption: [%d]:",
    118  1.2.20.1     tls 				    bootcfg_info.def + 1);
    119       1.1  nonaka 
    120       1.1  nonaka 			gets(input);
    121  1.2.20.1     tls 			choice = getchoicefrominput(input, bootcfg_info.def);
    122  1.2.20.1     tls 		} else if (bootcfg_info.timeout == 0)
    123  1.2.20.1     tls 			choice = bootcfg_info.def;
    124       1.1  nonaka 		else  {
    125       1.1  nonaka 			printf("\nChoose an option; RETURN for default; "
    126       1.1  nonaka 			       "SPACE to stop countdown.\n");
    127  1.2.20.1     tls 			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
    128       1.1  nonaka 				printf("Option %c will be chosen in ",
    129  1.2.20.1     tls 				    bootcfg_info.def + 'A');
    130       1.1  nonaka 			else
    131       1.1  nonaka 				printf("Option %d will be chosen in ",
    132  1.2.20.1     tls 				    bootcfg_info.def + 1);
    133  1.2.20.1     tls 			input[0] = awaitkey(bootcfg_info.timeout, 1);
    134       1.1  nonaka 			input[1] = '\0';
    135  1.2.20.1     tls 			choice = getchoicefrominput(input, bootcfg_info.def);
    136       1.1  nonaka 			/* If invalid key pressed, drop to menu */
    137       1.1  nonaka 			if (choice == -1)
    138  1.2.20.1     tls 				bootcfg_info.timeout = -1;
    139       1.1  nonaka 		}
    140       1.1  nonaka 		if (choice < 0)
    141       1.1  nonaka 			continue;
    142  1.2.20.1     tls 		if (!strcmp(bootcfg_info.command[choice], "prompt")) {
    143       1.1  nonaka 			printf("type \"?\" or \"help\" for help.\n");
    144       1.1  nonaka 			bootmenu(); /* does not return */
    145       1.1  nonaka 		} else {
    146  1.2.20.1     tls 			ic = bootcfg_info.command[choice];
    147       1.1  nonaka 			/* Split command string at ; into separate commands */
    148       1.1  nonaka 			do {
    149       1.1  nonaka 				oc = input;
    150       1.1  nonaka 				/* Look for ; separator */
    151       1.1  nonaka 				for (; *ic && *ic != COMMAND_SEPARATOR; ic++)
    152       1.1  nonaka 					*oc++ = *ic;
    153       1.1  nonaka 				if (*input == '\0')
    154       1.1  nonaka 					continue;
    155       1.1  nonaka 				/* Strip out any trailing spaces */
    156       1.1  nonaka 				oc--;
    157       1.1  nonaka 				for (; *oc == ' ' && oc > input; oc--);
    158       1.1  nonaka 				*++oc = '\0';
    159       1.1  nonaka 				if (*ic == COMMAND_SEPARATOR)
    160       1.1  nonaka 					ic++;
    161       1.1  nonaka 				/* Stop silly command strings like ;;; */
    162       1.1  nonaka 				if (*input != '\0')
    163       1.1  nonaka 					docommand(input);
    164       1.1  nonaka 				/* Skip leading spaces */
    165       1.1  nonaka 				for (; *ic == ' '; ic++);
    166       1.1  nonaka 			} while (*ic);
    167       1.1  nonaka 		}
    168       1.1  nonaka 	}
    169       1.1  nonaka }
    170