]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Type.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for Type shell level 3 function.\r
3\r
4 Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved. <BR>\r
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "UefiShellLevel3CommandsLib.h"\r
16\r
17#include <Library/ShellLib.h>\r
18\r
19EFI_STATUS\r
20EFIAPI\r
21TypeFileByHandle (\r
22 IN EFI_HANDLE Handle,\r
23 BOOLEAN Ascii,\r
24 BOOLEAN UCS2\r
25 )\r
26{\r
27 UINTN ReadSize;\r
28 VOID *Buffer;\r
29 EFI_STATUS Status;\r
30 UINTN LoopVar;\r
31 CHAR16 AsciiChar;\r
32\r
33 ReadSize = PcdGet16(PcdShellFileOperationSize);\r
34 Buffer = AllocatePool(ReadSize);\r
35 if (Buffer == NULL) {\r
36 return (EFI_OUT_OF_RESOURCES);\r
37 }\r
38\r
39 Status = ShellSetFilePosition(Handle, 0);\r
40 ASSERT_EFI_ERROR(Status);\r
41\r
42 while (ReadSize == ((UINTN)PcdGet16(PcdShellFileOperationSize))){\r
43 ZeroMem(Buffer, ReadSize);\r
44 Status = ShellReadFile(Handle, &ReadSize, Buffer);\r
45 if (EFI_ERROR(Status)){\r
46 break;\r
47 }\r
48\r
49 if (!(Ascii|UCS2)){\r
50 if (*(UINT16*)Buffer == UnicodeFileTag) {\r
51 UCS2 = TRUE;\r
52 Buffer = ((UINT16*)Buffer) + 1;\r
53 } else {\r
54 Ascii = TRUE;\r
55 }\r
56 }\r
57\r
58 //\r
59 // We want to use plain Print function here! (no color support for files)\r
60 //\r
61 if (Ascii){\r
62 for (LoopVar = 0 ; LoopVar < ReadSize ; LoopVar++) {\r
63 AsciiChar = CHAR_NULL;\r
64 AsciiChar = ((CHAR8*)Buffer)[LoopVar];\r
65 if (AsciiChar == CHAR_NULL) {\r
66 AsciiChar = '.';\r
67 }\r
68 Print(L"%c", AsciiChar);\r
69 }\r
70 } else {\r
71 Print(L"%s", Buffer);\r
72 }\r
73 }\r
74 Status = Print(L"\r\n", Buffer);\r
75 return (Status);\r
76}\r
77\r
78STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
79 {L"-a", TypeFlag},\r
80 {L"-u", TypeFlag},\r
81 {NULL, TypeMax}\r
82 };\r
83\r
84/**\r
85 Function for 'type' command.\r
86\r
87 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
88 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
89**/\r
90SHELL_STATUS\r
91EFIAPI\r
92ShellCommandRunType (\r
93 IN EFI_HANDLE ImageHandle,\r
94 IN EFI_SYSTEM_TABLE *SystemTable\r
95 )\r
96{\r
97 EFI_STATUS Status;\r
98 LIST_ENTRY *Package;\r
99 CHAR16 *ProblemParam;\r
100 CONST CHAR16 *Param;\r
101 SHELL_STATUS ShellStatus;\r
102 UINTN ParamCount;\r
103 EFI_SHELL_FILE_INFO *FileList;\r
104 EFI_SHELL_FILE_INFO *Node;\r
105 BOOLEAN AsciiMode;\r
106 BOOLEAN UnicodeMode;\r
107\r
108 ProblemParam = NULL;\r
109 ShellStatus = SHELL_SUCCESS;\r
110 ParamCount = 0;\r
111 FileList = NULL;\r
112\r
113 //\r
114 // initialize the shell lib (we must be in non-auto-init...)\r
115 //\r
116 Status = ShellInitialize();\r
117 ASSERT_EFI_ERROR(Status);\r
118\r
119 Status = CommandInit();\r
120 ASSERT_EFI_ERROR(Status);\r
121\r
122 //\r
123 // parse the command line\r
124 //\r
125 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
126 if (EFI_ERROR(Status)) {\r
127 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
128 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, ProblemParam);\r
129 FreePool(ProblemParam);\r
130 ShellStatus = SHELL_INVALID_PARAMETER;\r
131 } else {\r
132 ASSERT(FALSE);\r
133 }\r
134 } else {\r
135 //\r
136 // check for "-?"\r
137 //\r
138 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
139 ASSERT(FALSE);\r
140 }\r
141 AsciiMode = ShellCommandLineGetFlag(Package, L"-a");\r
142 UnicodeMode = ShellCommandLineGetFlag(Package, L"-u");\r
143\r
144 if (AsciiMode && UnicodeMode) {\r
145 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"-a & -u");\r
146 ShellStatus = SHELL_INVALID_PARAMETER;\r
147 } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
148 //\r
149 // we insufficient parameters\r
150 //\r
151 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);\r
152 ShellStatus = SHELL_INVALID_PARAMETER;\r
153 } else {\r
154 //\r
155 // get a list with each file specified by parameters\r
156 // if parameter is a directory then add all the files below it to the list\r
157 //\r
158 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
159 ; Param != NULL\r
160 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
161 ){\r
162 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_READ, &FileList);\r
163 if (EFI_ERROR(Status)) {\r
164 ShellStatus = SHELL_NOT_FOUND;\r
165 break;\r
166 }\r
167 //\r
168 // make sure we completed the param parsing sucessfully...\r
169 // Also make sure that any previous action was sucessful\r
170 //\r
171 if (ShellStatus == SHELL_SUCCESS) {\r
172 //\r
173 // check that we have at least 1 file\r
174 //\r
175 if (FileList == NULL || IsListEmpty(&FileList->Link)) {\r
176 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, Param);\r
177 continue;\r
178 } else {\r
179 //\r
180 // loop through the list and make sure we are not aborting...\r
181 //\r
182 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
183 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
184 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
185 ){\r
186 //\r
187 // make sure the file opened ok\r
188 //\r
189 if (EFI_ERROR(Node->Status)){\r
190 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Node->FileName, Node->Status);\r
191 ShellStatus = SHELL_NOT_FOUND;\r
192 continue;\r
193 }\r
194\r
195 //\r
196 // make sure its not a directory\r
197 //\r
198 if (FileHandleIsDirectory(Node->Handle) == EFI_SUCCESS) {\r
199 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_IS_DIR), gShellLevel3HiiHandle, Node->FileName);\r
200 ShellStatus = SHELL_NOT_FOUND;\r
201 continue;\r
202 }\r
203\r
204 //\r
205 // do it\r
206 //\r
207 Status = TypeFileByHandle(Node->Handle, AsciiMode, UnicodeMode);\r
208 if (EFI_ERROR(Status)) {\r
209 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TYP_ERROR), gShellLevel3HiiHandle, Node->FileName, Status);\r
210 ShellStatus = SHELL_INVALID_PARAMETER;\r
211 }\r
212 ASSERT(ShellStatus == SHELL_SUCCESS);\r
213 }\r
214 }\r
215 }\r
216 //\r
217 // Free the fileList\r
218 //\r
219 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
220 Status = ShellCloseFileMetaArg(&FileList);\r
221 }\r
222 ASSERT_EFI_ERROR(Status);\r
223 FileList = NULL;\r
224 }\r
225 }\r
226\r
227 //\r
228 // free the command line package\r
229 //\r
230 ShellCommandLineFreeVarList (Package);\r
231 }\r
232\r
233 if (ShellGetExecutionBreakFlag()) {\r
234 return (SHELL_ABORTED);\r
235 }\r
236\r
237 return (ShellStatus);\r
238}\r
239\r