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