]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c
ShellPkg: Fix for the ‘mv’ command when destination starts with ‘\’.
[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
345cd235 4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved. <BR>\r
a405b86d 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
345cd235 19/**\r
20 Display a single file to StdOut.\r
21\r
22 If both Ascii and UCS2 are FALSE attempt to discover the file type.\r
23\r
24 @param[in] Handle The handle to the file to display.\r
25 @param[in] Ascii TRUE to force ASCII, FALSE othewise.\r
26 @param[in] UCS2 TRUE to force UCS2, FALSE othewise.\r
27\r
28 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
29 @retval EFI_SUCCESS The operation was successful.\r
30**/\r
a405b86d 31EFI_STATUS\r
32EFIAPI\r
33TypeFileByHandle (\r
34 IN EFI_HANDLE Handle,\r
35 BOOLEAN Ascii,\r
36 BOOLEAN UCS2\r
37 )\r
38{\r
39 UINTN ReadSize;\r
40 VOID *Buffer;\r
41 EFI_STATUS Status;\r
42 UINTN LoopVar;\r
43 CHAR16 AsciiChar;\r
44\r
433a21cb 45 ReadSize = PcdGet32(PcdShellFileOperationSize);\r
345cd235 46 Buffer = AllocateZeroPool(ReadSize);\r
a405b86d 47 if (Buffer == NULL) {\r
48 return (EFI_OUT_OF_RESOURCES);\r
49 }\r
50\r
51 Status = ShellSetFilePosition(Handle, 0);\r
52 ASSERT_EFI_ERROR(Status);\r
53\r
433a21cb 54 while (ReadSize == ((UINTN)PcdGet32(PcdShellFileOperationSize))){\r
a405b86d 55 ZeroMem(Buffer, ReadSize);\r
56 Status = ShellReadFile(Handle, &ReadSize, Buffer);\r
57 if (EFI_ERROR(Status)){\r
58 break;\r
59 }\r
60\r
61 if (!(Ascii|UCS2)){\r
345cd235 62 if (*(UINT16*)Buffer == gUnicodeFileTag) {\r
a405b86d 63 UCS2 = TRUE;\r
64 Buffer = ((UINT16*)Buffer) + 1;\r
65 } else {\r
66 Ascii = TRUE;\r
67 }\r
68 }\r
69\r
70 //\r
71 // We want to use plain Print function here! (no color support for files)\r
72 //\r
73 if (Ascii){\r
74 for (LoopVar = 0 ; LoopVar < ReadSize ; LoopVar++) {\r
75 AsciiChar = CHAR_NULL;\r
76 AsciiChar = ((CHAR8*)Buffer)[LoopVar];\r
77 if (AsciiChar == CHAR_NULL) {\r
78 AsciiChar = '.';\r
79 }\r
80 Print(L"%c", AsciiChar);\r
81 }\r
82 } else {\r
83 Print(L"%s", Buffer);\r
84 }\r
85 }\r
345cd235 86 Print(L"\r\n", Buffer);\r
a405b86d 87 return (Status);\r
88}\r
89\r
90STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
91 {L"-a", TypeFlag},\r
92 {L"-u", TypeFlag},\r
93 {NULL, TypeMax}\r
94 };\r
95\r
96/**\r
97 Function for 'type' command.\r
98\r
99 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
100 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
101**/\r
102SHELL_STATUS\r
103EFIAPI\r
104ShellCommandRunType (\r
105 IN EFI_HANDLE ImageHandle,\r
106 IN EFI_SYSTEM_TABLE *SystemTable\r
107 )\r
108{\r
109 EFI_STATUS Status;\r
110 LIST_ENTRY *Package;\r
111 CHAR16 *ProblemParam;\r
112 CONST CHAR16 *Param;\r
113 SHELL_STATUS ShellStatus;\r
114 UINTN ParamCount;\r
115 EFI_SHELL_FILE_INFO *FileList;\r
116 EFI_SHELL_FILE_INFO *Node;\r
117 BOOLEAN AsciiMode;\r
118 BOOLEAN UnicodeMode;\r
119\r
120 ProblemParam = NULL;\r
121 ShellStatus = SHELL_SUCCESS;\r
122 ParamCount = 0;\r
123 FileList = NULL;\r
124\r
125 //\r
126 // initialize the shell lib (we must be in non-auto-init...)\r
127 //\r
128 Status = ShellInitialize();\r
129 ASSERT_EFI_ERROR(Status);\r
130\r
131 Status = CommandInit();\r
132 ASSERT_EFI_ERROR(Status);\r
133\r
134 //\r
135 // parse the command line\r
136 //\r
137 Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
138 if (EFI_ERROR(Status)) {\r
139 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
140 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, ProblemParam);\r
141 FreePool(ProblemParam);\r
142 ShellStatus = SHELL_INVALID_PARAMETER;\r
143 } else {\r
144 ASSERT(FALSE);\r
145 }\r
146 } else {\r
147 //\r
148 // check for "-?"\r
149 //\r
150 if (ShellCommandLineGetFlag(Package, L"-?")) {\r
151 ASSERT(FALSE);\r
152 }\r
153 AsciiMode = ShellCommandLineGetFlag(Package, L"-a");\r
154 UnicodeMode = ShellCommandLineGetFlag(Package, L"-u");\r
155\r
156 if (AsciiMode && UnicodeMode) {\r
157 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"-a & -u");\r
158 ShellStatus = SHELL_INVALID_PARAMETER;\r
159 } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
160 //\r
161 // we insufficient parameters\r
162 //\r
163 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel3HiiHandle);\r
164 ShellStatus = SHELL_INVALID_PARAMETER;\r
165 } else {\r
166 //\r
167 // get a list with each file specified by parameters\r
168 // if parameter is a directory then add all the files below it to the list\r
169 //\r
170 for ( ParamCount = 1, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
171 ; Param != NULL\r
172 ; ParamCount++, Param = ShellCommandLineGetRawValue(Package, ParamCount)\r
173 ){\r
174 Status = ShellOpenFileMetaArg((CHAR16*)Param, EFI_FILE_MODE_READ, &FileList);\r
175 if (EFI_ERROR(Status)) {\r
345cd235 176 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellLevel3HiiHandle, (CHAR16*)Param);\r
a405b86d 177 ShellStatus = SHELL_NOT_FOUND;\r
178 break;\r
179 }\r
180 //\r
181 // make sure we completed the param parsing sucessfully...\r
182 // Also make sure that any previous action was sucessful\r
183 //\r
184 if (ShellStatus == SHELL_SUCCESS) {\r
185 //\r
186 // check that we have at least 1 file\r
187 //\r
188 if (FileList == NULL || IsListEmpty(&FileList->Link)) {\r
189 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel3HiiHandle, Param);\r
190 continue;\r
191 } else {\r
192 //\r
193 // loop through the list and make sure we are not aborting...\r
194 //\r
195 for ( Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&FileList->Link)\r
196 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
197 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
198 ){\r
199 //\r
200 // make sure the file opened ok\r
201 //\r
202 if (EFI_ERROR(Node->Status)){\r
203 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NO_OPEN), gShellLevel3HiiHandle, Node->FileName, Node->Status);\r
204 ShellStatus = SHELL_NOT_FOUND;\r
205 continue;\r
206 }\r
207\r
208 //\r
209 // make sure its not a directory\r
210 //\r
211 if (FileHandleIsDirectory(Node->Handle) == EFI_SUCCESS) {\r
212 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_IS_DIR), gShellLevel3HiiHandle, Node->FileName);\r
213 ShellStatus = SHELL_NOT_FOUND;\r
214 continue;\r
215 }\r
216\r
217 //\r
218 // do it\r
219 //\r
220 Status = TypeFileByHandle(Node->Handle, AsciiMode, UnicodeMode);\r
221 if (EFI_ERROR(Status)) {\r
222 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TYP_ERROR), gShellLevel3HiiHandle, Node->FileName, Status);\r
223 ShellStatus = SHELL_INVALID_PARAMETER;\r
224 }\r
225 ASSERT(ShellStatus == SHELL_SUCCESS);\r
226 }\r
227 }\r
228 }\r
229 //\r
230 // Free the fileList\r
231 //\r
232 if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
233 Status = ShellCloseFileMetaArg(&FileList);\r
234 }\r
235 ASSERT_EFI_ERROR(Status);\r
236 FileList = NULL;\r
237 }\r
238 }\r
239\r
240 //\r
241 // free the command line package\r
242 //\r
243 ShellCommandLineFreeVarList (Package);\r
244 }\r
245\r
246 if (ShellGetExecutionBreakFlag()) {\r
247 return (SHELL_ABORTED);\r
248 }\r
249\r
250 return (ShellStatus);\r
251}\r
252\r