]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/EditInputBar.c
ShellPkg: Fixed build error 'variable set but not used'
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / EditInputBar.c
1 /** @file
2 Implements inputbar interface functions.
3
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "EditInputBar.h"
16 #include "UefiShellDebug1CommandsLib.h"
17
18 CHAR16 *mPrompt; // Input bar mPrompt string.
19 CHAR16 *mReturnString; // The returned string.
20 UINTN StringSize; // Size of mReturnString space size.
21
22 /**
23 Initialize the input bar.
24 **/
25 VOID
26 EFIAPI
27 InputBarInit (
28 VOID
29 )
30 {
31 mPrompt = NULL;
32 mReturnString = NULL;
33 StringSize = 0;
34 }
35
36 /**
37 Cleanup function for input bar.
38 **/
39 VOID
40 EFIAPI
41 InputBarCleanup (
42 VOID
43 )
44 {
45 //
46 // free input bar's prompt and input string
47 //
48 SHELL_FREE_NON_NULL (mPrompt);
49 SHELL_FREE_NON_NULL (mReturnString);
50 mPrompt = NULL;
51 mReturnString = NULL;
52 }
53
54 /**
55 Display the prompt.
56 Do the requesting of input.
57
58 @param[in] LastColumn The last printable column.
59 @param[in] LastRow The last printable row.
60 **/
61 VOID
62 EFIAPI
63 InputBarPrintInput (
64 IN UINTN LastColumn,
65 IN UINTN LastRow
66 )
67 {
68 UINTN Limit;
69 UINTN Size;
70 CHAR16 *Buffer;
71 UINTN Index;
72 UINTN mPromptLen;
73
74 mPromptLen = StrLen (mPrompt);
75 Limit = LastColumn - mPromptLen - 1;
76 Size = StrLen (mReturnString);
77
78 //
79 // check whether the mPrompt length and input length will
80 // exceed limit
81 //
82 if (Size <= Limit) {
83 Buffer = mReturnString;
84 } else {
85 Buffer = mReturnString + Size - Limit;
86 }
87
88 gST->ConOut->EnableCursor (gST->ConOut, FALSE);
89
90 ShellPrintEx (((INT32)mPromptLen), ((INT32)LastRow) - 1, L"%s", Buffer);
91 Size = StrLen (Buffer);
92
93 //
94 // print " " after mPrompt
95 //
96 for (Index = Size; Index < Limit; Index++) {
97 ShellPrintEx ((INT32)(mPromptLen + Size), ((INT32)LastRow) - 1, L" ");
98 }
99
100 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
101 gST->ConOut->SetCursorPosition (gST->ConOut, Size + mPromptLen, LastRow - 1);
102 }
103
104 typedef struct {
105 UINT32 Foreground : 4;
106 UINT32 Background : 4;
107 } INPUT_BAR_COLOR_ATTRIBUTES;
108
109 typedef union {
110 INPUT_BAR_COLOR_ATTRIBUTES Colors;
111 UINTN Data;
112 } INPUT_BAR_COLOR_UNION;
113
114
115 /**
116 The refresh function for InputBar, it will wait for user input
117
118 @param[in] LastRow The last printable row.
119 @param[in] LastColumn The last printable column.
120
121 @retval EFI_SUCCESS The operation was successful.
122 **/
123 EFI_STATUS
124 EFIAPI
125 InputBarRefresh (
126 UINTN LastRow,
127 UINTN LastColumn
128 )
129 {
130 INPUT_BAR_COLOR_UNION Orig;
131 INPUT_BAR_COLOR_UNION New;
132 EFI_INPUT_KEY Key;
133 UINTN Size;
134 EFI_STATUS Status;
135 BOOLEAN NoDisplay;
136 UINTN EventIndex;
137 UINTN CursorRow;
138 UINTN CursorCol;
139
140 //
141 // variable initialization
142 //
143 Size = 0;
144 Status = EFI_SUCCESS;
145
146 //
147 // back up the old screen attributes
148 //
149 CursorCol = gST->ConOut->Mode->CursorColumn;
150 CursorRow = gST->ConOut->Mode->CursorRow;
151 Orig.Data = gST->ConOut->Mode->Attribute;
152 New.Colors.Foreground = Orig.Colors.Background;
153 New.Colors.Background = Orig.Colors.Foreground;
154
155 gST->ConOut->SetAttribute (gST->ConOut, New.Data);
156
157 //
158 // clear input bar
159 //
160 EditorClearLine (LastRow , LastColumn, LastRow);
161
162 gST->ConOut->SetCursorPosition (gST->ConOut, 0, LastRow - 1);
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_EDIT_LIBINPUTBAR_MAININPUTBAR), gShellDebug1HiiHandle, mPrompt);
164
165 //
166 // this is a selection mPrompt, cursor will stay in edit area
167 // actually this is for search , search/replace
168 //
169 if (StrStr (mPrompt, L"Yes/No")) {
170 NoDisplay = TRUE;
171 gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);
172 gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
173 } else {
174 NoDisplay = FALSE;
175 }
176 //
177 // wait for user input
178 //
179 for (;;) {
180 gBS->WaitForEvent (1, &gST->ConIn->WaitForKey, &EventIndex);
181 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
182 if (EFI_ERROR (Status)) {
183 continue;
184 }
185 //
186 // pressed ESC
187 //
188 if (Key.ScanCode == SCAN_ESC) {
189 Size = 0;
190 Status = EFI_NOT_READY;
191 break;
192 }
193 //
194 // return pressed
195 //
196 if (Key.UnicodeChar == CHAR_LINEFEED || Key.UnicodeChar == CHAR_CARRIAGE_RETURN) {
197 break;
198 } else if (Key.UnicodeChar == CHAR_BACKSPACE) {
199 //
200 // backspace
201 //
202 if (Size > 0) {
203 Size--;
204 mReturnString[Size] = CHAR_NULL;
205 if (!NoDisplay) {
206
207 InputBarPrintInput (LastColumn, LastRow);
208
209 }
210 }
211 } else if (Key.UnicodeChar <= 127 && Key.UnicodeChar >= 32) {
212 //
213 // VALID ASCII char pressed
214 //
215 mReturnString[Size] = Key.UnicodeChar;
216
217 //
218 // should be less than specified length
219 //
220 if (Size >= StringSize) {
221 continue;
222 }
223
224 Size++;
225
226 mReturnString[Size] = CHAR_NULL;
227
228 if (!NoDisplay) {
229
230 InputBarPrintInput (LastColumn, LastRow);
231
232 } else {
233 //
234 // if just choose yes/no
235 //
236 break;
237 }
238
239 }
240 }
241
242 mReturnString[Size] = CHAR_NULL;
243
244
245 //
246 // restore screen attributes
247 //
248 gST->ConOut->SetCursorPosition (gST->ConOut, CursorCol, CursorRow);
249 gST->ConOut->SetAttribute (gST->ConOut, Orig.Data);
250
251 return Status;
252 }
253
254 /**
255 SetPrompt and wait for input.
256
257 @param[in] Str The prompt string.
258
259 @retval EFI_SUCCESS The operation was successful.
260 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
261 **/
262 EFI_STATUS
263 EFIAPI
264 InputBarSetPrompt (
265 IN CONST CHAR16 *Str
266 )
267 {
268 //
269 // FREE the old mPrompt string
270 //
271 SHELL_FREE_NON_NULL (mPrompt);
272
273 mPrompt = CatSPrint (NULL, L"%s ", Str);
274 if (mPrompt == NULL) {
275 return EFI_OUT_OF_RESOURCES;
276 }
277
278 return EFI_SUCCESS;
279 }
280
281 /**
282 Set the size of the string in characters.
283
284 @param[in] Size The max number of characters to accept.
285
286 @retval EFI_SUCCESS The operation was successful.
287 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
288 **/
289 EFI_STATUS
290 EFIAPI
291 InputBarSetStringSize (
292 UINTN Size
293 )
294 {
295 //
296 // free the old ReturnStirng
297 //
298 SHELL_FREE_NON_NULL (mReturnString);
299
300 StringSize = Size;
301 mReturnString = AllocateZeroPool ((StringSize + 1) * sizeof(mReturnString[0]));
302 if (mReturnString == NULL) {
303 return EFI_OUT_OF_RESOURCES;
304 }
305
306 return EFI_SUCCESS;
307 }
308
309 /**
310 Function to retrieve the input from the user.
311
312 @retval NULL No input has been received.
313 @return The string that was input.
314 **/
315 CONST CHAR16*
316 EFIAPI
317 InputBarGetString (
318 VOID
319 )
320 {
321 return (mReturnString);
322 }