]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
ShellPkg: Update behavior for GetTime() errors.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Type.c
1 /** @file
2 Main file for Type shell level 3 function.
3
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
9
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.
12
13 **/
14
15 #include "UefiShellLevel3CommandsLib.h"
16
17 #include <Library/ShellLib.h>
18
19 /**
20 Display a single file to StdOut.
21
22 If both Ascii and UCS2 are FALSE attempt to discover the file type.
23
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.
27
28 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
29 @retval EFI_SUCCESS The operation was successful.
30 **/
31 EFI_STATUS
32 EFIAPI
33 TypeFileByHandle (
34 IN EFI_HANDLE Handle,
35 BOOLEAN Ascii,
36 BOOLEAN UCS2
37 )
38 {
39 UINTN ReadSize;
40 VOID *Buffer;
41 EFI_STATUS Status;
42 UINTN LoopVar;
43 CHAR16 AsciiChar;
44
45 ReadSize = PcdGet16(PcdShellFileOperationSize);
46 Buffer = AllocateZeroPool(ReadSize);
47 if (Buffer == NULL) {
48 return (EFI_OUT_OF_RESOURCES);
49 }
50
51 Status = ShellSetFilePosition(Handle, 0);
52 ASSERT_EFI_ERROR(Status);
53
54 while (ReadSize == ((UINTN)PcdGet16(PcdShellFileOperationSize))){
55 ZeroMem(Buffer, ReadSize);
56 Status = ShellReadFile(Handle, &ReadSize, Buffer);
57 if (EFI_ERROR(Status)){
58 break;
59 }
60
61 if (!(Ascii|UCS2)){
62 if (*(UINT16*)Buffer == gUnicodeFileTag) {
63 UCS2 = TRUE;
64 Buffer = ((UINT16*)Buffer) + 1;
65 } else {
66 Ascii = TRUE;
67 }
68 }
69
70 //
71 // We want to use plain Print function here! (no color support for files)
72 //
73 if (Ascii){
74 for (LoopVar = 0 ; LoopVar < ReadSize ; LoopVar++) {
75 AsciiChar = CHAR_NULL;
76 AsciiChar = ((CHAR8*)Buffer)[LoopVar];
77 if (AsciiChar == CHAR_NULL) {
78 AsciiChar = '.';
79 }
80 Print(L"%c", AsciiChar);
81 }
82 } else {
83 Print(L"%s", Buffer);
84 }
85 }
86 Print(L"\r\n", Buffer);
87 return (Status);
88 }
89
90 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
91 {L"-a", TypeFlag},
92 {L"-u", TypeFlag},
93 {NULL, TypeMax}
94 };
95
96 /**
97 Function for 'type' command.
98
99 @param[in] ImageHandle Handle to the Image (NULL if Internal).
100 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
101 **/
102 SHELL_STATUS
103 EFIAPI
104 ShellCommandRunType (
105 IN EFI_HANDLE ImageHandle,
106 IN EFI_SYSTEM_TABLE *SystemTable
107 )
108 {
109 EFI_STATUS Status;
110 LIST_ENTRY *Package;
111 CHAR16 *ProblemParam;
112 CONST CHAR16 *Param;
113 SHELL_STATUS ShellStatus;
114 UINTN ParamCount;
115 EFI_SHELL_FILE_INFO *FileList;
116 EFI_SHELL_FILE_INFO *Node;
117 BOOLEAN AsciiMode;
118 BOOLEAN UnicodeMode;
119
120 ProblemParam = NULL;
121 ShellStatus = SHELL_SUCCESS;
122 ParamCount = 0;
123 FileList = NULL;
124
125 //
126 // initialize the shell lib (we must be in non-auto-init...)
127 //
128 Status = ShellInitialize();
129 ASSERT_EFI_ERROR(Status);
130
131 Status = CommandInit();
132 ASSERT_EFI_ERROR(Status);
133
134 //
135 // parse the command line
136 //
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;
143 } else {
144 ASSERT(FALSE);
145 }
146 } else {
147 //
148 // check for "-?"
149 //
150 if (ShellCommandLineGetFlag(Package, L"-?")) {
151 ASSERT(FALSE);
152 }
153 AsciiMode = ShellCommandLineGetFlag(Package, L"-a");
154 UnicodeMode = ShellCommandLineGetFlag(Package, L"-u");
155
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) {
160 //
161 // we insufficient parameters
162 //
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);
164 ShellStatus = SHELL_INVALID_PARAMETER;
165 } else {
166 //
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
169 //
170 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
171 ; Param != NULL
172 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
173 ){
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;
178 break;
179 }
180 //
181 // make sure we completed the param parsing sucessfully...
182 // Also make sure that any previous action was sucessful
183 //
184 if (ShellStatus == SHELL_SUCCESS) {
185 //
186 // check that we have at least 1 file
187 //
188 if (FileList == NULL || IsListEmpty(&FileList->Link)) {
189 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, Param);
190 continue;
191 } else {
192 //
193 // loop through the list and make sure we are not aborting...
194 //
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)
198 ){
199 //
200 // make sure the file opened ok
201 //
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;
205 continue;
206 }
207
208 //
209 // make sure its not a directory
210 //
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;
214 continue;
215 }
216
217 //
218 // do it
219 //
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;
224 }
225 ASSERT(ShellStatus == SHELL_SUCCESS);
226 }
227 }
228 }
229 //
230 // Free the fileList
231 //
232 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
233 Status = ShellCloseFileMetaArg(&FileList);
234 }
235 ASSERT_EFI_ERROR(Status);
236 FileList = NULL;
237 }
238 }
239
240 //
241 // free the command line package
242 //
243 ShellCommandLineFreeVarList (Package);
244 }
245
246 if (ShellGetExecutionBreakFlag()) {
247 return (SHELL_ABORTED);
248 }
249
250 return (ShellStatus);
251 }
252