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 - 2011, Intel Corporation. All rights reserved. <BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellLevel3CommandsLib.h"
18 #include <Library/ShellLib.h>
21 Display a single file to StdOut.
23 If both Ascii and UCS2 are FALSE attempt to discover the file type.
25 @param[in] Handle The handle to the file to display.
26 @param[in] Ascii TRUE to force ASCII, FALSE othewise.
27 @param[in] UCS2 TRUE to force UCS2, FALSE othewise.
29 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
30 @retval EFI_SUCCESS The operation was successful.
35 IN SHELL_FILE_HANDLE Handle
,
42 VOID
*AllocatedBuffer
;
49 ReadSize
= PcdGet32(PcdShellFileOperationSize
);
50 AllocatedBuffer
= AllocateZeroPool(ReadSize
);
51 if (AllocatedBuffer
== NULL
) {
52 return (EFI_OUT_OF_RESOURCES
);
55 Status
= ShellSetFilePosition(Handle
, 0);
56 ASSERT_EFI_ERROR(Status
);
58 while (ReadSize
== ((UINTN
)PcdGet32(PcdShellFileOperationSize
))) {
59 Buffer
= AllocatedBuffer
;
60 ZeroMem(Buffer
, ReadSize
);
61 Status
= ShellReadFile(Handle
, &ReadSize
, Buffer
);
62 if (EFI_ERROR(Status
)){
67 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
76 for (LoopVar
= 0 ; LoopVar
< LoopSize
; LoopVar
++) {
78 // The valid range of ASCII characters is 0x20-0x7E.
79 // Display "." when there is an invalid character.
81 AsciiChar
= CHAR_NULL
;
82 AsciiChar
= ((CHAR8
*)Buffer
)[LoopVar
];
83 if (AsciiChar
== '\r' || AsciiChar
== '\n') {
85 // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
86 // characters to be displayed as is.
88 if (AsciiChar
== '\n' && ((CHAR8
*)Buffer
)[LoopVar
-1] != '\r') {
90 // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
91 // was not the previous character, print CR and LF. This is because
92 // Shell 2.0 requires carriage return with line feed for displaying
93 // each new line from left.
95 ShellPrintEx (-1, -1, L
"\r\n");
100 // For all other characters which are not printable, display '.'
102 if (AsciiChar
< 0x20 || AsciiChar
>= 0x7F) {
106 ShellPrintEx (-1, -1, L
"%c", AsciiChar
);
109 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
111 // For unicode files, skip displaying the byte order marker.
113 Buffer
= ((UINT16
*)Buffer
) + 1;
114 LoopSize
= (ReadSize
/ (sizeof (CHAR16
))) - 1;
116 LoopSize
= ReadSize
/ (sizeof (CHAR16
));
119 for (LoopVar
= 0 ; LoopVar
< LoopSize
; LoopVar
++) {
121 // An invalid range of characters is 0x0-0x1F.
122 // Display "." when there is an invalid character.
124 Ucs2Char
= CHAR_NULL
;
125 Ucs2Char
= ((CHAR16
*)Buffer
)[LoopVar
];
126 if (Ucs2Char
== '\r' || Ucs2Char
== '\n') {
128 // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)
129 // characters to be displayed as is.
131 if (Ucs2Char
== '\n' && ((CHAR16
*)Buffer
)[LoopVar
-1] != '\r') {
133 // In case Line Feed (0xA) is encountered & Carriage Return (0xD)
134 // was not the previous character, print CR and LF. This is because
135 // Shell 2.0 requires carriage return with line feed for displaying
136 // each new line from left.
138 ShellPrintEx (-1, -1, L
"\r\n");
142 else if (Ucs2Char
< 0x20) {
144 // For all other characters which are not printable, display '.'
148 ShellPrintEx (-1, -1, L
"%c", Ucs2Char
);
152 if (ShellGetExecutionBreakFlag()) {
156 FreePool (AllocatedBuffer
);
157 ShellPrintEx (-1, -1, L
"\r\n");
161 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
168 Function for 'type' command.
170 @param[in] ImageHandle Handle to the Image (NULL if Internal).
171 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
175 ShellCommandRunType (
176 IN EFI_HANDLE ImageHandle
,
177 IN EFI_SYSTEM_TABLE
*SystemTable
182 CHAR16
*ProblemParam
;
184 SHELL_STATUS ShellStatus
;
186 EFI_SHELL_FILE_INFO
*FileList
;
187 EFI_SHELL_FILE_INFO
*Node
;
192 ShellStatus
= SHELL_SUCCESS
;
197 // initialize the shell lib (we must be in non-auto-init...)
199 Status
= ShellInitialize();
200 ASSERT_EFI_ERROR(Status
);
202 Status
= CommandInit();
203 ASSERT_EFI_ERROR(Status
);
206 // parse the command line
208 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
209 if (EFI_ERROR(Status
)) {
210 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
211 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"type", ProblemParam
);
212 FreePool(ProblemParam
);
213 ShellStatus
= SHELL_INVALID_PARAMETER
;
221 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
224 AsciiMode
= ShellCommandLineGetFlag(Package
, L
"-a");
225 UnicodeMode
= ShellCommandLineGetFlag(Package
, L
"-u");
227 if (AsciiMode
&& UnicodeMode
) {
228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
), gShellLevel3HiiHandle
, L
"type", L
"-a & -u");
229 ShellStatus
= SHELL_INVALID_PARAMETER
;
230 } else if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
232 // we insufficient parameters
234 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
, L
"type");
235 ShellStatus
= SHELL_INVALID_PARAMETER
;
238 // get a list with each file specified by parameters
239 // if parameter is a directory then add all the files below it to the list
241 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
243 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
245 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_READ
, &FileList
);
246 if (EFI_ERROR(Status
)) {
247 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", (CHAR16
*)Param
);
248 ShellStatus
= SHELL_NOT_FOUND
;
252 // make sure we completed the param parsing sucessfully...
253 // Also make sure that any previous action was sucessful
255 if (ShellStatus
== SHELL_SUCCESS
) {
257 // check that we have at least 1 file
259 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
260 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, L
"type", Param
);
264 // loop through the list and make sure we are not aborting...
266 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
267 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
268 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
271 if (ShellGetExecutionBreakFlag()) {
276 // make sure the file opened ok
278 if (EFI_ERROR(Node
->Status
)){
279 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
280 ShellStatus
= SHELL_NOT_FOUND
;
285 // make sure its not a directory
287 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
288 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_IS_DIR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
289 ShellStatus
= SHELL_NOT_FOUND
;
296 Status
= TypeFileByHandle (Node
->Handle
, AsciiMode
, UnicodeMode
);
297 if (EFI_ERROR(Status
)) {
298 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TYP_ERROR
), gShellLevel3HiiHandle
, L
"type", Node
->FileName
);
299 ShellStatus
= SHELL_INVALID_PARAMETER
;
301 ASSERT(ShellStatus
== SHELL_SUCCESS
);
308 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
309 Status
= ShellCloseFileMetaArg(&FileList
);
311 ASSERT_EFI_ERROR(Status
);
317 // free the command line package
319 ShellCommandLineFreeVarList (Package
);
322 if (ShellGetExecutionBreakFlag()) {
323 return (SHELL_ABORTED
);
326 return (ShellStatus
);