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