2 Main file for Type shell level 3 function.
4 Copyright (c) 2009 - 2010, 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>
33 ReadSize
= PcdGet16(PcdShellFileOperationSize
);
34 Buffer
= AllocatePool(ReadSize
);
36 return (EFI_OUT_OF_RESOURCES
);
39 Status
= ShellSetFilePosition(Handle
, 0);
40 ASSERT_EFI_ERROR(Status
);
42 while (ReadSize
== ((UINTN
)PcdGet16(PcdShellFileOperationSize
))){
43 ZeroMem(Buffer
, ReadSize
);
44 Status
= ShellReadFile(Handle
, &ReadSize
, Buffer
);
45 if (EFI_ERROR(Status
)){
50 if (*(UINT16
*)Buffer
== UnicodeFileTag
) {
52 Buffer
= ((UINT16
*)Buffer
) + 1;
59 // We want to use plain Print function here! (no color support for files)
62 for (LoopVar
= 0 ; LoopVar
< ReadSize
; LoopVar
++) {
63 AsciiChar
= CHAR_NULL
;
64 AsciiChar
= ((CHAR8
*)Buffer
)[LoopVar
];
65 if (AsciiChar
== CHAR_NULL
) {
68 Print(L
"%c", AsciiChar
);
74 Status
= Print(L
"\r\n", Buffer
);
78 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
85 Function for 'type' command.
87 @param[in] ImageHandle Handle to the Image (NULL if Internal).
88 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
93 IN EFI_HANDLE ImageHandle
,
94 IN EFI_SYSTEM_TABLE
*SystemTable
101 SHELL_STATUS ShellStatus
;
103 EFI_SHELL_FILE_INFO
*FileList
;
104 EFI_SHELL_FILE_INFO
*Node
;
109 ShellStatus
= SHELL_SUCCESS
;
114 // initialize the shell lib (we must be in non-auto-init...)
116 Status
= ShellInitialize();
117 ASSERT_EFI_ERROR(Status
);
119 Status
= CommandInit();
120 ASSERT_EFI_ERROR(Status
);
123 // parse the command line
125 Status
= ShellCommandLineParse (ParamList
, &Package
, &ProblemParam
, TRUE
);
126 if (EFI_ERROR(Status
)) {
127 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
128 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, ProblemParam
);
129 FreePool(ProblemParam
);
130 ShellStatus
= SHELL_INVALID_PARAMETER
;
138 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
141 AsciiMode
= ShellCommandLineGetFlag(Package
, L
"-a");
142 UnicodeMode
= ShellCommandLineGetFlag(Package
, L
"-u");
144 if (AsciiMode
&& UnicodeMode
) {
145 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel3HiiHandle
, L
"-a & -u");
146 ShellStatus
= SHELL_INVALID_PARAMETER
;
147 } else if (ShellCommandLineGetRawValue(Package
, 1) == NULL
) {
149 // we insufficient parameters
151 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellLevel3HiiHandle
);
152 ShellStatus
= SHELL_INVALID_PARAMETER
;
155 // get a list with each file specified by parameters
156 // if parameter is a directory then add all the files below it to the list
158 for ( ParamCount
= 1, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
160 ; ParamCount
++, Param
= ShellCommandLineGetRawValue(Package
, ParamCount
)
162 Status
= ShellOpenFileMetaArg((CHAR16
*)Param
, EFI_FILE_MODE_READ
, &FileList
);
163 if (EFI_ERROR(Status
)) {
164 ShellStatus
= SHELL_NOT_FOUND
;
168 // make sure we completed the param parsing sucessfully...
169 // Also make sure that any previous action was sucessful
171 if (ShellStatus
== SHELL_SUCCESS
) {
173 // check that we have at least 1 file
175 if (FileList
== NULL
|| IsListEmpty(&FileList
->Link
)) {
176 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NF
), gShellLevel3HiiHandle
, Param
);
180 // loop through the list and make sure we are not aborting...
182 for ( Node
= (EFI_SHELL_FILE_INFO
*)GetFirstNode(&FileList
->Link
)
183 ; !IsNull(&FileList
->Link
, &Node
->Link
) && !ShellGetExecutionBreakFlag()
184 ; Node
= (EFI_SHELL_FILE_INFO
*)GetNextNode(&FileList
->Link
, &Node
->Link
)
187 // make sure the file opened ok
189 if (EFI_ERROR(Node
->Status
)){
190 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_NO_OPEN
), gShellLevel3HiiHandle
, Node
->FileName
, Node
->Status
);
191 ShellStatus
= SHELL_NOT_FOUND
;
196 // make sure its not a directory
198 if (FileHandleIsDirectory(Node
->Handle
) == EFI_SUCCESS
) {
199 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_IS_DIR
), gShellLevel3HiiHandle
, Node
->FileName
);
200 ShellStatus
= SHELL_NOT_FOUND
;
207 Status
= TypeFileByHandle(Node
->Handle
, AsciiMode
, UnicodeMode
);
208 if (EFI_ERROR(Status
)) {
209 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_TYP_ERROR
), gShellLevel3HiiHandle
, Node
->FileName
, Status
);
210 ShellStatus
= SHELL_INVALID_PARAMETER
;
212 ASSERT(ShellStatus
== SHELL_SUCCESS
);
219 if (FileList
!= NULL
&& !IsListEmpty(&FileList
->Link
)) {
220 Status
= ShellCloseFileMetaArg(&FileList
);
222 ASSERT_EFI_ERROR(Status
);
228 // free the command line package
230 ShellCommandLineFreeVarList (Package
);
233 if (ShellGetExecutionBreakFlag()) {
234 return (SHELL_ABORTED
);
237 return (ShellStatus
);