]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Type.c
1 /** @file
2 Main file for Type shell level 3 function.
3
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
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 EFI_STATUS
20 EFIAPI
21 TypeFileByHandle (
22 IN EFI_HANDLE Handle,
23 BOOLEAN Ascii,
24 BOOLEAN UCS2
25 )
26 {
27 UINTN ReadSize;
28 VOID *Buffer;
29 EFI_STATUS Status;
30 UINTN LoopVar;
31 CHAR16 AsciiChar;
32
33 ReadSize = PcdGet16(PcdShellFileOperationSize);
34 Buffer = AllocatePool(ReadSize);
35 if (Buffer == NULL) {
36 return (EFI_OUT_OF_RESOURCES);
37 }
38
39 Status = ShellSetFilePosition(Handle, 0);
40 ASSERT_EFI_ERROR(Status);
41
42 while (ReadSize == ((UINTN)PcdGet16(PcdShellFileOperationSize))){
43 ZeroMem(Buffer, ReadSize);
44 Status = ShellReadFile(Handle, &ReadSize, Buffer);
45 if (EFI_ERROR(Status)){
46 break;
47 }
48
49 if (!(Ascii|UCS2)){
50 if (*(UINT16*)Buffer == UnicodeFileTag) {
51 UCS2 = TRUE;
52 Buffer = ((UINT16*)Buffer) + 1;
53 } else {
54 Ascii = TRUE;
55 }
56 }
57
58 //
59 // We want to use plain Print function here! (no color support for files)
60 //
61 if (Ascii){
62 for (LoopVar = 0 ; LoopVar < ReadSize ; LoopVar++) {
63 AsciiChar = CHAR_NULL;
64 AsciiChar = ((CHAR8*)Buffer)[LoopVar];
65 if (AsciiChar == CHAR_NULL) {
66 AsciiChar = '.';
67 }
68 Print(L"%c", AsciiChar);
69 }
70 } else {
71 Print(L"%s", Buffer);
72 }
73 }
74 Status = Print(L"\r\n", Buffer);
75 return (Status);
76 }
77
78 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
79 {L"-a", TypeFlag},
80 {L"-u", TypeFlag},
81 {NULL, TypeMax}
82 };
83
84 /**
85 Function for 'type' command.
86
87 @param[in] ImageHandle Handle to the Image (NULL if Internal).
88 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
89 **/
90 SHELL_STATUS
91 EFIAPI
92 ShellCommandRunType (
93 IN EFI_HANDLE ImageHandle,
94 IN EFI_SYSTEM_TABLE *SystemTable
95 )
96 {
97 EFI_STATUS Status;
98 LIST_ENTRY *Package;
99 CHAR16 *ProblemParam;
100 CONST CHAR16 *Param;
101 SHELL_STATUS ShellStatus;
102 UINTN ParamCount;
103 EFI_SHELL_FILE_INFO *FileList;
104 EFI_SHELL_FILE_INFO *Node;
105 BOOLEAN AsciiMode;
106 BOOLEAN UnicodeMode;
107
108 ProblemParam = NULL;
109 ShellStatus = SHELL_SUCCESS;
110 ParamCount = 0;
111 FileList = NULL;
112
113 //
114 // initialize the shell lib (we must be in non-auto-init...)
115 //
116 Status = ShellInitialize();
117 ASSERT_EFI_ERROR(Status);
118
119 Status = CommandInit();
120 ASSERT_EFI_ERROR(Status);
121
122 //
123 // parse the command line
124 //
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;
131 } else {
132 ASSERT(FALSE);
133 }
134 } else {
135 //
136 // check for "-?"
137 //
138 if (ShellCommandLineGetFlag(Package, L"-?")) {
139 ASSERT(FALSE);
140 }
141 AsciiMode = ShellCommandLineGetFlag(Package, L"-a");
142 UnicodeMode = ShellCommandLineGetFlag(Package, L"-u");
143
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) {
148 //
149 // we insufficient parameters
150 //
151 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);
152 ShellStatus = SHELL_INVALID_PARAMETER;
153 } else {
154 //
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
157 //
158 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)
159 ; Param != NULL
160 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)
161 ){
162 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_READ, &FileList);
163 if (EFI_ERROR(Status)) {
164 ShellStatus = SHELL_NOT_FOUND;
165 break;
166 }
167 //
168 // make sure we completed the param parsing sucessfully...
169 // Also make sure that any previous action was sucessful
170 //
171 if (ShellStatus == SHELL_SUCCESS) {
172 //
173 // check that we have at least 1 file
174 //
175 if (FileList == NULL || IsListEmpty(&FileList->Link)) {
176 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, Param);
177 continue;
178 } else {
179 //
180 // loop through the list and make sure we are not aborting...
181 //
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)
185 ){
186 //
187 // make sure the file opened ok
188 //
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;
192 continue;
193 }
194
195 //
196 // make sure its not a directory
197 //
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;
201 continue;
202 }
203
204 //
205 // do it
206 //
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;
211 }
212 ASSERT(ShellStatus == SHELL_SUCCESS);
213 }
214 }
215 }
216 //
217 // Free the fileList
218 //
219 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
220 Status = ShellCloseFileMetaArg(&FileList);
221 }
222 ASSERT_EFI_ERROR(Status);
223 FileList = NULL;
224 }
225 }
226
227 //
228 // free the command line package
229 //
230 ShellCommandLineFreeVarList (Package);
231 }
232
233 if (ShellGetExecutionBreakFlag()) {
234 return (SHELL_ABORTED);
235 }
236
237 return (ShellStatus);
238 }
239