Home | History | Annotate | Line # | Download | only in efiboot
prompt.c revision 1.4.4.3
      1 /*	$NetBSD: prompt.c,v 1.4.4.3 2020/04/08 14:09:02 martin Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996, 1997
      5  * 	Matthias Drochner.  All rights reserved.
      6  * Copyright (c) 1996, 1997
      7  * 	Perry E. Metzger.  All rights reserved.
      8  * Copyright (c) 1997
      9  *	Jason R. Thorpe.  All rights reserved
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgements:
     21  *	This product includes software developed for the NetBSD Project
     22  *	by Matthias Drochner.
     23  *	This product includes software developed for the NetBSD Project
     24  *	by Perry E. Metzger.
     25  * 4. The names of the authors may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include "efiboot.h"
     41 
     42 #include <lib/libsa/net.h>
     43 
     44 #define	POLL_FREQ	10
     45 
     46 char *
     47 gettrailer(char *arg)
     48 {
     49 	char *options;
     50 
     51 	for (options = arg; *options; options++) {
     52 		switch (*options) {
     53 		case ' ':
     54 		case '\t':
     55 			*options++ = '\0';
     56 			break;
     57 		default:
     58 			continue;
     59 		}
     60 		break;
     61 	}
     62 	if (*options == '\0')
     63 		return options;
     64 
     65 	/* trim leading blanks/tabs */
     66 	while (*options == ' ' || *options == '\t')
     67 		options++;
     68 
     69 	return options;
     70 }
     71 
     72 char
     73 awaitkey(int timeout, int tell)
     74 {
     75 	int i = timeout * POLL_FREQ;
     76 	int last_secs = -1, secs;
     77 	int last_len = -1, n;
     78 	char buf[32];
     79 	char c = 0;
     80 
     81 	for (;;) {
     82 		if (tell) {
     83 			int len;
     84 
     85 			secs = (i + POLL_FREQ - 1) / POLL_FREQ;
     86 			if (secs != last_secs) {
     87 				if (last_len != -1) {
     88 					char *p = buf;
     89 					for (n = 0; n < last_len; n++)
     90 						*p++ = '\b';
     91 					*p = '\0';
     92 					printf("%s", buf);
     93 				}
     94 				len = snprintf(buf, sizeof(buf), "%d seconds. ", (i + POLL_FREQ - 1) / POLL_FREQ);
     95 				if (len > 0 && len < sizeof(buf))
     96 					printf("%s", buf);
     97 				last_len = len;
     98 				last_secs = secs;
     99 			}
    100 		}
    101 		if (ischar()) {
    102 			c = getchar();
    103 			if (c == 0)
    104 				c = -1;
    105 			goto out;
    106 		}
    107 		if (--i > 0) {
    108 			efi_delay(1000000 / POLL_FREQ);
    109 		} else {
    110 			break;
    111 		}
    112 	}
    113 
    114 out:
    115 	if (tell) {
    116 		if (last_len != -1) {
    117 			char *p = buf;
    118 			for (n = 0; n < last_len; n++)
    119 				*p++ = '\b';
    120 			*p = '\0';
    121 			printf("%s", buf);
    122 		}
    123 		printf("0 seconds.     \n");
    124 	}
    125 
    126 	return c;
    127 }
    128 
    129 void
    130 docommand(char *arg)
    131 {
    132 	char *options;
    133 	int i;
    134 
    135 	options = gettrailer(arg);
    136 
    137 	for (i = 0; commands[i].c_name != NULL; i++) {
    138 		if (strcmp(arg, commands[i].c_name) == 0) {
    139 			(*commands[i].c_fn)(options);
    140 			return;
    141 		}
    142 	}
    143 
    144 	printf("unknown command\n");
    145 	command_help(NULL);
    146 }
    147 
    148 __dead void
    149 bootprompt(void)
    150 {
    151 	char input[80];
    152 
    153 	for (;;) {
    154 		char *c = input;
    155 
    156 		input[0] = '\0';
    157 		printf("> ");
    158 		kgets(input, sizeof(input));
    159 
    160 		/*
    161 		 * Skip leading whitespace.
    162 		 */
    163 		while (*c == ' ')
    164 			c++;
    165 		if (*c)
    166 			docommand(c);
    167 	}
    168 }
    169