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