Home | History | Annotate | Line # | Download | only in efiboot
prompt.c revision 1.4.4.2
      1  1.4.4.2  christos /*	$NetBSD: prompt.c,v 1.4.4.2 2019/06/10 22:09:56 christos 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.2  christos 
     44  1.4.4.2  christos #define	POLL_FREQ	10
     45  1.4.4.2  christos 
     46  1.4.4.2  christos char *
     47  1.4.4.2  christos gettrailer(char *arg)
     48  1.4.4.2  christos {
     49  1.4.4.2  christos 	char *options;
     50  1.4.4.2  christos 
     51  1.4.4.2  christos 	for (options = arg; *options; options++) {
     52  1.4.4.2  christos 		switch (*options) {
     53  1.4.4.2  christos 		case ' ':
     54  1.4.4.2  christos 		case '\t':
     55  1.4.4.2  christos 			*options++ = '\0';
     56  1.4.4.2  christos 			break;
     57  1.4.4.2  christos 		default:
     58  1.4.4.2  christos 			continue;
     59  1.4.4.2  christos 		}
     60  1.4.4.2  christos 		break;
     61  1.4.4.2  christos 	}
     62  1.4.4.2  christos 	if (*options == '\0')
     63  1.4.4.2  christos 		return options;
     64  1.4.4.2  christos 
     65  1.4.4.2  christos 	/* trim leading blanks/tabs */
     66  1.4.4.2  christos 	while (*options == ' ' || *options == '\t')
     67  1.4.4.2  christos 		options++;
     68  1.4.4.2  christos 
     69  1.4.4.2  christos 	return options;
     70  1.4.4.2  christos }
     71  1.4.4.2  christos 
     72  1.4.4.2  christos char
     73  1.4.4.2  christos awaitkey(int timeout, int tell)
     74  1.4.4.2  christos {
     75  1.4.4.2  christos 	int i = timeout * POLL_FREQ;
     76  1.4.4.2  christos 	int last_secs = -1, secs;
     77  1.4.4.2  christos 	char c = 0;
     78  1.4.4.2  christos 
     79  1.4.4.2  christos 	for (;;) {
     80  1.4.4.2  christos 		if (tell) {
     81  1.4.4.2  christos 			char buf[32];
     82  1.4.4.2  christos 			int len;
     83  1.4.4.2  christos 
     84  1.4.4.2  christos 			secs = (i + POLL_FREQ - 1) / POLL_FREQ;
     85  1.4.4.2  christos 			if (secs != last_secs) {
     86  1.4.4.2  christos 				len = snprintf(buf, sizeof(buf), "%d seconds. ", (i + POLL_FREQ - 1) / POLL_FREQ);
     87  1.4.4.2  christos 				if (len > 0 && len < sizeof(buf)) {
     88  1.4.4.2  christos 					char *p = buf;
     89  1.4.4.2  christos 					printf("%s", buf);
     90  1.4.4.2  christos 					while (*p)
     91  1.4.4.2  christos 						*p++ = '\b';
     92  1.4.4.2  christos 					printf("%s", buf);
     93  1.4.4.2  christos 				}
     94  1.4.4.2  christos 				last_secs = secs;
     95  1.4.4.2  christos 			}
     96  1.4.4.2  christos 		}
     97  1.4.4.2  christos 		if (ischar()) {
     98  1.4.4.2  christos 			c = getchar();
     99  1.4.4.2  christos 			if (c == 0)
    100  1.4.4.2  christos 				c = -1;
    101  1.4.4.2  christos 			goto out;
    102  1.4.4.2  christos 		}
    103  1.4.4.2  christos 		if (--i > 0) {
    104  1.4.4.2  christos 			efi_delay(1000000 / POLL_FREQ);
    105  1.4.4.2  christos 		} else {
    106  1.4.4.2  christos 			break;
    107  1.4.4.2  christos 		}
    108  1.4.4.2  christos 	}
    109  1.4.4.2  christos 
    110  1.4.4.2  christos out:
    111  1.4.4.2  christos 	if (tell)
    112  1.4.4.2  christos 		printf("0 seconds.     \n");
    113  1.4.4.2  christos 
    114  1.4.4.2  christos 	return c;
    115  1.4.4.2  christos }
    116  1.4.4.2  christos 
    117  1.4.4.2  christos void
    118  1.4.4.2  christos docommand(char *arg)
    119  1.4.4.2  christos {
    120  1.4.4.2  christos 	char *options;
    121  1.4.4.2  christos 	int i;
    122  1.4.4.2  christos 
    123  1.4.4.2  christos 	options = gettrailer(arg);
    124  1.4.4.2  christos 
    125  1.4.4.2  christos 	for (i = 0; commands[i].c_name != NULL; i++) {
    126  1.4.4.2  christos 		if (strcmp(arg, commands[i].c_name) == 0) {
    127  1.4.4.2  christos 			(*commands[i].c_fn)(options);
    128  1.4.4.2  christos 			return;
    129  1.4.4.2  christos 		}
    130  1.4.4.2  christos 	}
    131  1.4.4.2  christos 
    132  1.4.4.2  christos 	printf("unknown command\n");
    133  1.4.4.2  christos 	command_help(NULL);
    134  1.4.4.2  christos }
    135  1.4.4.2  christos 
    136  1.4.4.2  christos __dead void
    137  1.4.4.2  christos bootprompt(void)
    138  1.4.4.2  christos {
    139  1.4.4.2  christos 	char input[80];
    140  1.4.4.2  christos 
    141  1.4.4.2  christos 	for (;;) {
    142  1.4.4.2  christos 		char *c = input;
    143  1.4.4.2  christos 
    144  1.4.4.2  christos 		input[0] = '\0';
    145  1.4.4.2  christos 		printf("> ");
    146  1.4.4.2  christos 		kgets(input, sizeof(input));
    147  1.4.4.2  christos 
    148  1.4.4.2  christos 		/*
    149  1.4.4.2  christos 		 * Skip leading whitespace.
    150  1.4.4.2  christos 		 */
    151  1.4.4.2  christos 		while (*c == ' ')
    152  1.4.4.2  christos 			c++;
    153  1.4.4.2  christos 		if (*c)
    154  1.4.4.2  christos 			docommand(c);
    155  1.4.4.2  christos 	}
    156  1.4.4.2  christos }
    157