2 Main file for Type shell level 3 function.
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellLevel3CommandsLib.h"
12 #include <Library/ShellLib.h>
15 Display a single file to StdOut.
17 If both Ascii and UCS2 are FALSE attempt to discover the file type.
19 @param[in] Handle The handle to the file to display.
20 @param[in] Ascii TRUE to force ASCII, FALSE othewise.
21 @param[in] UCS2 TRUE to force UCS2, FALSE othewise.
23 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
24 @retval EFI_SUCCESS The operation was successful.
28 IN SHELL_FILE_HANDLE Handle
,
35 VOID
*AllocatedBuffer
;
42 ReadSize
= PcdGet32(PcdShellFileOperationSize
);
43 AllocatedBuffer
= AllocateZeroPool(ReadSize
);
44 if (AllocatedBuffer
== NULL
) {
45 return (EFI_OUT_OF_RESOURCES
);
48 Status
= ShellSetFilePosition(Handle
, 0);
49 ASSERT_EFI_ERROR(Status
);
51 while (ReadSize
== ((UINTN
)PcdGet32(PcdShellFileOperationSize
))) {
52 Buffer
= AllocatedBuffer
;
53 ZeroMem(Buffer
, ReadSize
);
54 Status
= ShellReadFile(Handle
, &ReadSize
, Buffer
);
55 if (EFI_ERROR(Status
)){
60 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
69 for (LoopVar
= 0 ; LoopVar
< LoopSize
; LoopVar
++) {
71 // The valid range of ASCII characters is 0x20-0x7E.
72 // Display "." when there is an invalid character.
74 AsciiChar
= CHAR_NULL
;
75 AsciiChar
= ((CHAR8
*)Buffer
)[LoopVar
];
76 if (AsciiChar
== '\r' || AsciiChar
== '\n') {
78 // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
79 // characters to be displayed as is.
81 if (AsciiChar
== '\n' && ((CHAR8
*)Buffer
)[LoopVar
-1] != '\r') {
83 // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
84 // was not the previous character, print CR and LF. This is because
85 // Shell 2.0 requires carriage return with line feed for displaying
86 // each new line from left.
88 ShellPrintEx (-1, -1, L
"\r\n");
93 // For all other characters which are not printable, display '.'
95 if (AsciiChar
< 0x20 || AsciiChar
>= 0x7F) {
99 ShellPrintEx (-1, -1, L
"%c", AsciiChar
);
102 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
104 // For unicode files, skip displaying the byte order marker.
106 Buffer
= ((UINT16
*)Buffer
) + 1;
107 LoopSize
= (ReadSize
/ (sizeof (CHAR16
))) - 1;
109 LoopSize
= ReadSize
/ (sizeof (CHAR16
));
112 for (LoopVar
= 0 ; LoopVar
< LoopSize
; LoopVar
++) {
114 // An invalid range of characters is 0x0-0x1F.
115 // Display "." when there is an invalid character.
117 Ucs2Char
= CHAR_NULL
;
118 Ucs2Char
= ((CHAR16
*)Buffer
)[LoopVar
];
119 if (Ucs2Char
== '\r' || Ucs2Char
== '\n') {
121 // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
122 // characters to be displayed as is.
124 if (Ucs2Char
== '\n' && ((CHAR16
*)Buffer
)[LoopVar
-1] != '\r') {
126 // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
127 // was not the previous character, print CR and LF. This is because
128 // Shell 2.0 requires carriage return with line feed for displaying
129 // each new line from left.
131 ShellPrintEx (-1, -1, L
"\r\n");
135 else if (Ucs2Char
< 0x20) {
137 // For all other characters which are not printable, display '.'
141 ShellPrintEx (-1, -1, L
"%c", Ucs2Char
);
145 if (ShellGetExecutionBreakFlag()) {
149 FreePool (AllocatedBuffer
);
150 ShellPrintEx (-1, -1, L
"\r\n");
154 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
161 Function for 'type' command.
163 @param[in] ImageHandle Handle to the Image (NULL if Internal).
164 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
168 ShellCommandRunType (
169 IN EFI_HANDLE ImageHandle
,
170 IN EFI_SYSTEM_TABLE
*SystemTable
175 CHAR16
*ProblemParam
;
177 SHELL_STATUS ShellStatus
;
179 EFI_SHELL_FILE_INFO
*FileList
;
180 EFI_SHELL_FILE_INFO
*Node
;
185 ShellStatus
= SHELL_SUCCESS
;
190 // initialize the shell lib (we must be in non-auto-init...)
192 Status
= ShellInitialize();
193 ASSERT_EFI_ERROR(Status
);
195 Status
= CommandInit();
196 ASSERT_EFI_ERROR(Status
);
199 // parse the command line
201 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
202 if (EFI_ERROR(Status
)) {
203 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
204 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"type", ProblemParam
);
205 FreePool(ProblemParam
);
206 ShellStatus
= SHELL_INVALID_PARAMETER
;
214 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
217 AsciiMode
= ShellCommandLineGetFlag(Package
, L
"-a");
218 UnicodeMode
= ShellCommandLineGetFlag(Package
, L
"-u");
220 if (AsciiMode
&& UnicodeMode
) {
221 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel3HiiHandle
, L
"type", L
"-a & -u");
222 ShellStatus
= SHELL_INVALID_PARAMETER
;
223 } else if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
225 // we insufficient parameters
227 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"type");
228 ShellStatus
= SHELL_INVALID_PARAMETER
;
231 // get a list with each file specified by parameters
232 // if parameter is a directory then add all the files below it to the list
234 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
236 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
238 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_READ
, &FileList
);
239 if (EFI_ERROR(Status
)) {
240 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", (CHAR16
*)Param
);
241 ShellStatus
= SHELL_NOT_FOUND
;
245 // make sure we completed the param parsing sucessfully...
246 // Also make sure that any previous action was sucessful
248 if (ShellStatus
== SHELL_SUCCESS
) {
250 // check that we have at least 1 file
252 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
253 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, L
"type", Param
);
257 // loop through the list and make sure we are not aborting...
259 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
260 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
261 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
264 if (ShellGetExecutionBreakFlag()) {
269 // make sure the file opened ok
271 if (EFI_ERROR(Node
->Status
)){
272 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
273 ShellStatus
= SHELL_NOT_FOUND
;
278 // make sure its not a directory
280 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
281 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_IS_DIR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
282 ShellStatus
= SHELL_NOT_FOUND
;
289 Status
= TypeFileByHandle (Node
->Handle
, AsciiMode
, UnicodeMode
);
290 if (EFI_ERROR(Status
)) {
291 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TYP_ERROR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
292 ShellStatus
= SHELL_INVALID_PARAMETER
;
294 ASSERT(ShellStatus
== SHELL_SUCCESS
);
301 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
302 Status
= ShellCloseFileMetaArg(&FileList
);
304 ASSERT_EFI_ERROR(Status
);
310 // free the command line package
312 ShellCommandLineFreeVarList (Package
);
315 if (ShellGetExecutionBreakFlag()) {
316 return (SHELL_ABORTED
);
319 return (ShellStatus
);