2 Main file for Type shell level 3 function.
4 Copyright (c) 2009 - 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
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.
15 #include "UefiShellLevel3CommandsLib.h"
17 #include <Library/ShellLib.h>
20 Display a single file to StdOut.
22 If both Ascii and UCS2 are FALSE attempt to discover the file type.
24 @param[in] Handle The handle to the file to display.
25 @param[in] Ascii TRUE to force ASCII, FALSE othewise.
26 @param[in] UCS2 TRUE to force UCS2, FALSE othewise.
28 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
29 @retval EFI_SUCCESS The operation was successful.
45 ReadSize
= PcdGet16(PcdShellFileOperationSize
);
46 Buffer
= AllocateZeroPool(ReadSize
);
48 return (EFI_OUT_OF_RESOURCES
);
51 Status
= ShellSetFilePosition(Handle
, 0);
52 ASSERT_EFI_ERROR(Status
);
54 while (ReadSize
== ((UINTN
)PcdGet16(PcdShellFileOperationSize
))){
55 ZeroMem(Buffer
, ReadSize
);
56 Status
= ShellReadFile(Handle
, &ReadSize
, Buffer
);
57 if (EFI_ERROR(Status
)){
62 if (*(UINT16
*)Buffer
== gUnicodeFileTag
) {
64 Buffer
= ((UINT16
*)Buffer
) + 1;
71 // We want to use plain Print function here! (no color support for files)
74 for (LoopVar
= 0 ; LoopVar
< ReadSize
; LoopVar
++) {
75 AsciiChar
= CHAR_NULL
;
76 AsciiChar
= ((CHAR8
*)Buffer
)[LoopVar
];
77 if (AsciiChar
== CHAR_NULL
) {
80 Print(L
"%c", AsciiChar
);
86 Print(L
"\r\n", Buffer
);
90 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
97 Function for 'type' command.
99 @param[in] ImageHandle Handle to the Image (NULL if Internal).
100 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
104 ShellCommandRunType (
105 IN EFI_HANDLE ImageHandle
,
106 IN EFI_SYSTEM_TABLE
*SystemTable
111 CHAR16
*ProblemParam
;
113 SHELL_STATUS ShellStatus
;
115 EFI_SHELL_FILE_INFO
*FileList
;
116 EFI_SHELL_FILE_INFO
*Node
;
121 ShellStatus
= SHELL_SUCCESS
;
126 // initialize the shell lib (we must be in non-auto-init...)
128 Status
= ShellInitialize();
129 ASSERT_EFI_ERROR(Status
);
131 Status
= CommandInit();
132 ASSERT_EFI_ERROR(Status
);
135 // parse the command line
137 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
138 if (EFI_ERROR(Status
)) {
139 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
140 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, ProblemParam
);
141 FreePool(ProblemParam
);
142 ShellStatus
= SHELL_INVALID_PARAMETER
;
150 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
153 AsciiMode
= ShellCommandLineGetFlag(Package
, L
"-a");
154 UnicodeMode
= ShellCommandLineGetFlag(Package
, L
"-u");
156 if (AsciiMode
&& UnicodeMode
) {
157 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"-a & -u");
158 ShellStatus
= SHELL_INVALID_PARAMETER
;
159 } else if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
161 // we insufficient parameters
163 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
);
164 ShellStatus
= SHELL_INVALID_PARAMETER
;
167 // get a list with each file specified by parameters
168 // if parameter is a directory then add all the files below it to the list
170 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
172 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
174 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_READ
, &FileList
);
175 if (EFI_ERROR(Status
)) {
176 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellLevel3HiiHandle
, (CHAR16
*)Param
);
177 ShellStatus
= SHELL_NOT_FOUND
;
181 // make sure we completed the param parsing sucessfully...
182 // Also make sure that any previous action was sucessful
184 if (ShellStatus
== SHELL_SUCCESS
) {
186 // check that we have at least 1 file
188 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
189 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, Param
);
193 // loop through the list and make sure we are not aborting...
195 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
196 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
197 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
200 // make sure the file opened ok
202 if (EFI_ERROR(Node
->Status
)){
203 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NO_OPEN
), gShellLevel3HiiHandle
, Node
->FileName
, Node
->Status
);
204 ShellStatus
= SHELL_NOT_FOUND
;
209 // make sure its not a directory
211 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
212 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_IS_DIR
), gShellLevel3HiiHandle
, Node
->FileName
);
213 ShellStatus
= SHELL_NOT_FOUND
;
220 Status
= TypeFileByHandle(Node
->Handle
, AsciiMode
, UnicodeMode
);
221 if (EFI_ERROR(Status
)) {
222 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TYP_ERROR
), gShellLevel3HiiHandle
, Node
->FileName
, Status
);
223 ShellStatus
= SHELL_INVALID_PARAMETER
;
225 ASSERT(ShellStatus
== SHELL_SUCCESS
);
232 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
233 Status
= ShellCloseFileMetaArg(&FileList
);
235 ASSERT_EFI_ERROR(Status
);
241 // free the command line package
243 ShellCommandLineFreeVarList (Package
);
246 if (ShellGetExecutionBreakFlag()) {
247 return (SHELL_ABORTED
);
250 return (ShellStatus
);