ec7bb1e174c247ca34133053e902c19ee793b3f7
[mirror_edk2.git] / EdkModulePkg / Universal / UserInterface / SetupBrowser / Dxe / Print.c
1 /*++
2
3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 Print.c
15
16 Abstract:
17
18 Basic Ascii AvSPrintf() function named VSPrint(). VSPrint() enables very
19 simple implemenation of SPrint() and Print() to support debug.
20
21 You can not Print more than EFI_DRIVER_LIB_MAX_PRINT_BUFFER characters at a
22 time. This makes the implementation very simple.
23
24 VSPrint, Print, SPrint format specification has the follwoing form
25
26 %type
27
28 type:
29 'S','s' - argument is an Unicode string
30 'c' - argument is an ascii character
31 '%' - Print a %
32
33 --*/
34
35 #include "print.h"
36
37 STATIC
38 UINTN
39 _IPrint (
40 IN UINTN Column,
41 IN UINTN Row,
42 IN EFI_SIMPLE_TEXT_OUT_PROTOCOL *Out,
43 IN CHAR16 *fmt,
44 IN VA_LIST args
45 )
46 //
47 // Display string worker for: Print, PrintAt, IPrint, IPrintAt
48 //
49 {
50 CHAR16 *Buffer;
51 CHAR16 *BackupBuffer;
52 UINTN Index;
53 UINTN PreviousIndex;
54
55 //
56 // For now, allocate an arbitrarily long buffer
57 //
58 Buffer = AllocateZeroPool (0x10000);
59 BackupBuffer = AllocateZeroPool (0x10000);
60 ASSERT (Buffer);
61 ASSERT (BackupBuffer);
62
63 if (Column != (UINTN) -1) {
64 Out->SetCursorPosition (Out, Column, Row);
65 }
66
67 UnicodeVSPrint (Buffer, 0x10000, fmt, args);
68
69 Out->Mode->Attribute = Out->Mode->Attribute & 0x7f;
70
71 Out->SetAttribute (Out, Out->Mode->Attribute);
72
73 Index = 0;
74 PreviousIndex = 0;
75
76 do {
77 for (; (Buffer[Index] != NARROW_CHAR) && (Buffer[Index] != WIDE_CHAR) && (Buffer[Index] != 0); Index++) {
78 BackupBuffer[Index] = Buffer[Index];
79 }
80
81 if (Buffer[Index] == 0) {
82 break;
83 }
84 //
85 // Null-terminate the temporary string
86 //
87 BackupBuffer[Index] = 0;
88
89 //
90 // Print this out, we are about to switch widths
91 //
92 Out->OutputString (Out, &BackupBuffer[PreviousIndex]);
93
94 //
95 // Preserve the current index + 1, since this is where we will start printing from next
96 //
97 PreviousIndex = Index + 1;
98
99 //
100 // We are at a narrow or wide character directive. Set attributes and strip it and print it
101 //
102 if (Buffer[Index] == NARROW_CHAR) {
103 //
104 // Preserve bits 0 - 6 and zero out the rest
105 //
106 Out->Mode->Attribute = Out->Mode->Attribute & 0x7f;
107 Out->SetAttribute (Out, Out->Mode->Attribute);
108 } else {
109 //
110 // Must be wide, set bit 7 ON
111 //
112 Out->Mode->Attribute = Out->Mode->Attribute | EFI_WIDE_ATTRIBUTE;
113 Out->SetAttribute (Out, Out->Mode->Attribute);
114 }
115
116 Index++;
117
118 } while (Buffer[Index] != 0);
119
120 //
121 // We hit the end of the string - print it
122 //
123 Out->OutputString (Out, &BackupBuffer[PreviousIndex]);
124
125 gBS->FreePool (Buffer);
126 gBS->FreePool (BackupBuffer);
127 return EFI_SUCCESS;
128 }
129
130 UINTN
131 Print (
132 IN CHAR16 *fmt,
133 ...
134 )
135 /*++
136
137 Routine Description:
138
139 Prints a formatted unicode string to the default console
140
141 Arguments:
142
143 fmt - Format string
144
145 Returns:
146
147 Length of string printed to the console
148
149 --*/
150 {
151 VA_LIST args;
152
153 VA_START (args, fmt);
154 return _IPrint ((UINTN) -1, (UINTN) -1, gST->ConOut, fmt, args);
155 }
156
157 UINTN
158 PrintString (
159 CHAR16 *String
160 )
161 /*++
162
163 Routine Description:
164
165 Prints a unicode string to the default console,
166 using L"%s" format.
167
168 Arguments:
169
170 String - String pointer.
171
172 Returns:
173
174 Length of string printed to the console
175
176 --*/
177 {
178 return Print ((CHAR16 *) L"%s", String);
179 }
180
181 UINTN
182 PrintChar (
183 CHAR16 Character
184 )
185 /*++
186
187 Routine Description:
188
189 Prints a chracter to the default console,
190 using L"%c" format.
191
192 Arguments:
193
194 Character - Character to print.
195
196 Returns:
197
198 Length of string printed to the console.
199
200 --*/
201 {
202 return Print ((CHAR16 *) L"%c", Character);
203 }
204
205 /*
206 UINTN
207 PrintToken (
208 IN EFI_HII_HANDLE Handle,
209 IN UINT16 Token,
210 IN CHAR16 *Language,
211 ...
212 )
213 {
214 VA_LIST args;
215 UINTN NumberOfHiiHandles;
216 EFI_HANDLE *HandleBuffer;
217 EFI_HII_PROTOCOL *Hii;
218
219 //
220 // There should only be one HII image
221 //
222 Status = gBS->LocateHandleBuffer (
223 ByProtocol,
224 &gEfiHiiProtocolGuid,
225 NULL,
226 &NumberOfHiiHandles,
227 &HandleBuffer
228 );
229
230 if (EFI_ERROR (Status)) {
231 return Status;
232 }
233
234 //
235 // Retrieve the Hii protocol interface
236 //
237 Status = gBS->HandleProtocol (
238 HandleBuffer[0],
239 &gEfiHiiProtocolGuid,
240 &Hii
241 );
242
243 Hii->GetString (Hii, Handle, Token, FALSE, Language,
244
245 VA_START (args, fmt);
246 return _IPrint ((UINTN) -1, (UINTN) -1, gST->ConOut, fmt, args);
247 }
248
249 */
250 UINTN
251 PrintAt (
252 IN UINTN Column,
253 IN UINTN Row,
254 IN CHAR16 *fmt,
255 ...
256 )
257 /*++
258
259 Routine Description:
260
261 Prints a formatted unicode string to the default console, at
262 the supplied cursor position
263
264 Arguments:
265
266 Column, Row - The cursor position to print the string at
267
268 fmt - Format string
269
270 Returns:
271
272 Length of string printed to the console
273
274 --*/
275 {
276 VA_LIST args;
277
278 VA_START (args, fmt);
279 return _IPrint (Column, Row, gST->ConOut, fmt, args);
280 }
281
282 UINTN
283 PrintStringAt (
284 IN UINTN Column,
285 IN UINTN Row,
286 CHAR16 *String
287 )
288 /*++
289
290 Routine Description:
291
292 Prints a unicode string to the default console, at
293 the supplied cursor position, using L"%s" format.
294
295 Arguments:
296
297 Column, Row - The cursor position to print the string at
298
299 String - String pointer.
300
301 Returns:
302
303 Length of string printed to the console
304
305 --*/
306 {
307 return PrintAt (Column, Row, (CHAR16 *) L"%s", String);
308 }
309
310 UINTN
311 PrintCharAt (
312 IN UINTN Column,
313 IN UINTN Row,
314 CHAR16 Character
315 )
316 /*++
317
318 Routine Description:
319
320 Prints a chracter to the default console, at
321 the supplied cursor position, using L"%c" format.
322
323 Arguments:
324
325 Column, Row - The cursor position to print the string at
326
327 Character - Character to print.
328
329 Returns:
330
331 Length of string printed to the console.
332
333 --*/
334 {
335 return PrintAt (Column, Row, (CHAR16 *) L"%c", Character);
336 }
337
338