xref: /netbsd/sys/external/bsd/gnu-efi/dist/lib/console.c (revision 01d0c315)
1 /*	$NetBSD: console.c,v 1.1.1.1 2014/04/01 16:16:06 jakllsch Exp $	*/
2 
3 /*++
4 
5 Copyright (c) 1998  Intel Corporation
6 
7 Module Name:
8 
9     console.c
10 
11 Abstract:
12 
13 
14 
15 
16 Revision History
17 
18 --*/
19 
20 #include "lib.h"
21 
22 
23 
24 VOID
Output(IN CHAR16 * Str)25 Output (
26     IN CHAR16   *Str
27     )
28 // Write a string to the console at the current cursor location
29 {
30     uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, Str);
31 }
32 
33 
34 VOID
Input(IN CHAR16 * Prompt OPTIONAL,OUT CHAR16 * InStr,IN UINTN StrLen)35 Input (
36     IN CHAR16    *Prompt OPTIONAL,
37     OUT CHAR16   *InStr,
38     IN UINTN     StrLen
39     )
40 // Input a string at the current cursor location, for StrLen
41 {
42     IInput (
43         ST->ConOut,
44         ST->ConIn,
45         Prompt,
46         InStr,
47         StrLen
48         );
49 }
50 
51 VOID
IInput(IN SIMPLE_TEXT_OUTPUT_INTERFACE * ConOut,IN SIMPLE_INPUT_INTERFACE * ConIn,IN CHAR16 * Prompt OPTIONAL,OUT CHAR16 * InStr,IN UINTN StrLen)52 IInput (
53     IN SIMPLE_TEXT_OUTPUT_INTERFACE     *ConOut,
54     IN SIMPLE_INPUT_INTERFACE           *ConIn,
55     IN CHAR16                           *Prompt OPTIONAL,
56     OUT CHAR16                          *InStr,
57     IN UINTN                            StrLen
58     )
59 // Input a string at the current cursor location, for StrLen
60 {
61     EFI_INPUT_KEY                   Key;
62     EFI_STATUS                      Status;
63     UINTN                           Len;
64 
65     if (Prompt) {
66         ConOut->OutputString (ConOut, Prompt);
67     }
68 
69     Len = 0;
70     for (; ;) {
71         WaitForSingleEvent (ConIn->WaitForKey, 0);
72 
73         Status = uefi_call_wrapper(ConIn->ReadKeyStroke, 2, ConIn, &Key);
74         if (EFI_ERROR(Status)) {
75             DEBUG((D_ERROR, "Input: error return from ReadKey %x\n", Status));
76             break;
77         }
78 
79         if (Key.UnicodeChar == '\n' ||
80             Key.UnicodeChar == '\r') {
81             break;
82         }
83 
84         if (Key.UnicodeChar == '\b') {
85             if (Len) {
86                 uefi_call_wrapper(ConOut->OutputString, 2, ConOut, L"\b \b");
87                 Len -= 1;
88             }
89             continue;
90         }
91 
92         if (Key.UnicodeChar >= ' ') {
93             if (Len < StrLen-1) {
94                 InStr[Len] = Key.UnicodeChar;
95 
96                 InStr[Len+1] = 0;
97                 uefi_call_wrapper(ConOut->OutputString, 2, ConOut, &InStr[Len]);
98 
99                 Len += 1;
100             }
101             continue;
102         }
103     }
104 
105     InStr[Len] = 0;
106 }
107