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 - 2019, 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' && LoopVar
== 0) ||
82 (AsciiChar
== '\n' && ((CHAR8
*)Buffer
)[LoopVar
-1] != '\r')) {
84 // In case file begin with single line Feed or Line Feed (0xA) is
85 // encountered & Carriage Return (0xD) was not previous character,
86 // print CR and LF. This is because Shell 2.0 requires carriage
87 // return with line feed for displaying each new line from left.
89 ShellPrintEx (-1, -1, L
"\r\n");
94 // For all other characters which are not printable, display '.'
96 if (AsciiChar
< 0x20 || AsciiChar
>= 0x7F) {
100 ShellPrintEx (-1, -1, L
"%c", AsciiChar
);
103 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
105 // For unicode files, skip displaying the byte order marker.
107 Buffer
= ((UINT16
*)Buffer
) + 1;
108 LoopSize
= (ReadSize
/ (sizeof (CHAR16
))) - 1;
110 LoopSize
= ReadSize
/ (sizeof (CHAR16
));
113 for (LoopVar
= 0 ; LoopVar
< LoopSize
; LoopVar
++) {
115 // An invalid range of characters is 0x0-0x1F.
116 // Display "." when there is an invalid character.
118 Ucs2Char
= CHAR_NULL
;
119 Ucs2Char
= ((CHAR16
*)Buffer
)[LoopVar
];
120 if (Ucs2Char
== '\r' || Ucs2Char
== '\n') {
122 // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
123 // characters to be displayed as is.
125 if ((Ucs2Char
== '\n' && LoopVar
== 0) ||
126 (Ucs2Char
== '\n' && ((CHAR16
*)Buffer
)[LoopVar
-1] != '\r')) {
128 // In case file begin with single line Feed or Line Feed (0xA) is
129 // encountered & Carriage Return (0xD) was not previous character,
130 // print CR and LF. This is because Shell 2.0 requires carriage
131 // return with line feed for displaying each new line from left.
133 ShellPrintEx (-1, -1, L
"\r\n");
137 else if (Ucs2Char
< 0x20) {
139 // For all other characters which are not printable, display '.'
143 ShellPrintEx (-1, -1, L
"%c", Ucs2Char
);
147 if (ShellGetExecutionBreakFlag()) {
151 FreePool (AllocatedBuffer
);
152 ShellPrintEx (-1, -1, L
"\r\n");
156 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
163 Function for 'type' command.
165 @param[in] ImageHandle Handle to the Image (NULL if Internal).
166 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
170 ShellCommandRunType (
171 IN EFI_HANDLE ImageHandle
,
172 IN EFI_SYSTEM_TABLE
*SystemTable
177 CHAR16
*ProblemParam
;
179 SHELL_STATUS ShellStatus
;
181 EFI_SHELL_FILE_INFO
*FileList
;
182 EFI_SHELL_FILE_INFO
*Node
;
187 ShellStatus
= SHELL_SUCCESS
;
192 // initialize the shell lib (we must be in non-auto-init...)
194 Status
= ShellInitialize();
195 ASSERT_EFI_ERROR(Status
);
197 Status
= CommandInit();
198 ASSERT_EFI_ERROR(Status
);
201 // parse the command line
203 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
204 if (EFI_ERROR(Status
)) {
205 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
206 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"type", ProblemParam
);
207 FreePool(ProblemParam
);
208 ShellStatus
= SHELL_INVALID_PARAMETER
;
216 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
219 AsciiMode
= ShellCommandLineGetFlag(Package
, L
"-a");
220 UnicodeMode
= ShellCommandLineGetFlag(Package
, L
"-u");
222 if (AsciiMode
&& UnicodeMode
) {
223 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel3HiiHandle
, L
"type", L
"-a & -u");
224 ShellStatus
= SHELL_INVALID_PARAMETER
;
225 } else if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
227 // we insufficient parameters
229 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"type");
230 ShellStatus
= SHELL_INVALID_PARAMETER
;
233 // get a list with each file specified by parameters
234 // if parameter is a directory then add all the files below it to the list
236 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
238 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
240 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_READ
, &FileList
);
241 if (EFI_ERROR(Status
)) {
242 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", (CHAR16
*)Param
);
243 ShellStatus
= SHELL_NOT_FOUND
;
247 // make sure we completed the param parsing sucessfully...
248 // Also make sure that any previous action was sucessful
250 if (ShellStatus
== SHELL_SUCCESS
) {
252 // check that we have at least 1 file
254 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
255 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, L
"type", Param
);
259 // loop through the list and make sure we are not aborting...
261 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
262 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
263 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
266 if (ShellGetExecutionBreakFlag()) {
271 // make sure the file opened ok
273 if (EFI_ERROR(Node
->Status
)){
274 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
275 ShellStatus
= SHELL_NOT_FOUND
;
280 // make sure its not a directory
282 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
283 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_IS_DIR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
284 ShellStatus
= SHELL_NOT_FOUND
;
291 Status
= TypeFileByHandle (Node
->Handle
, AsciiMode
, UnicodeMode
);
292 if (EFI_ERROR(Status
)) {
293 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TYP_ERROR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
294 ShellStatus
= SHELL_INVALID_PARAMETER
;
296 ASSERT(ShellStatus
== SHELL_SUCCESS
);
303 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
304 Status
= ShellCloseFileMetaArg(&FileList
);
306 ASSERT_EFI_ERROR(Status
);
312 // free the command line package
314 ShellCommandLineFreeVarList (Package
);
317 if (ShellGetExecutionBreakFlag()) {
318 return (SHELL_ABORTED
);
321 return (ShellStatus
);