]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | Main file for attrib shell level 2 function.\r | |
3 | \r | |
4 | Copyright (c) 2009 - 2014, 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 "UefiShellLevel2CommandsLib.h"\r | |
16 | \r | |
17 | /**\r | |
18 | Function for 'cd' command.\r | |
19 | \r | |
20 | @param[in] ImageHandle Handle to the Image (NULL if Internal).\r | |
21 | @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r | |
22 | **/\r | |
23 | SHELL_STATUS\r | |
24 | EFIAPI\r | |
25 | ShellCommandRunCd (\r | |
26 | IN EFI_HANDLE ImageHandle,\r | |
27 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
28 | )\r | |
29 | {\r | |
30 | EFI_STATUS Status;\r | |
31 | LIST_ENTRY *Package;\r | |
32 | CONST CHAR16 *Directory;\r | |
33 | CHAR16 *Path;\r | |
34 | CHAR16 *Drive;\r | |
35 | UINTN DriveSize;\r | |
36 | CHAR16 *ProblemParam;\r | |
37 | SHELL_STATUS ShellStatus;\r | |
38 | SHELL_FILE_HANDLE Handle;\r | |
39 | CONST CHAR16 *Param1;\r | |
40 | CHAR16 *Param1Copy;\r | |
41 | CHAR16* Walker;\r | |
42 | \r | |
43 | ProblemParam = NULL;\r | |
44 | ShellStatus = SHELL_SUCCESS;\r | |
45 | Drive = NULL;\r | |
46 | DriveSize = 0;\r | |
47 | \r | |
48 | Status = CommandInit();\r | |
49 | ASSERT_EFI_ERROR(Status);\r | |
50 | \r | |
51 | //\r | |
52 | // initialize the shell lib (we must be in non-auto-init...)\r | |
53 | //\r | |
54 | Status = ShellInitialize();\r | |
55 | ASSERT_EFI_ERROR(Status);\r | |
56 | \r | |
57 | //\r | |
58 | // parse the command line\r | |
59 | //\r | |
60 | Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);\r | |
61 | if (EFI_ERROR(Status)) {\r | |
62 | if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r | |
63 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r | |
64 | FreePool(ProblemParam);\r | |
65 | ShellStatus = SHELL_INVALID_PARAMETER;\r | |
66 | } else {\r | |
67 | ASSERT(FALSE);\r | |
68 | }\r | |
69 | }\r | |
70 | \r | |
71 | //\r | |
72 | // check for "-?"\r | |
73 | //\r | |
74 | if (ShellCommandLineGetFlag(Package, L"-?")) {\r | |
75 | ASSERT(FALSE);\r | |
76 | } else if (ShellCommandLineGetRawValue(Package, 2) != NULL) {\r | |
77 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle);\r | |
78 | ShellStatus = SHELL_INVALID_PARAMETER;\r | |
79 | } else {\r | |
80 | //\r | |
81 | // remember that param 0 is the command name\r | |
82 | // If there are 0 value parameters, then print the current directory\r | |
83 | // else If there are 2 value parameters, then print the error message\r | |
84 | // else If there is 1 value paramerer , then change the directory\r | |
85 | //\r | |
86 | Param1 = ShellCommandLineGetRawValue(Package, 1);\r | |
87 | if (Param1 == NULL) {\r | |
88 | //\r | |
89 | // display the current directory\r | |
90 | //\r | |
91 | Directory = ShellGetCurrentDir(NULL);\r | |
92 | if (Directory != NULL) {\r | |
93 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, Directory);\r | |
94 | } else {\r | |
95 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r | |
96 | ShellStatus = SHELL_NOT_FOUND;\r | |
97 | }\r | |
98 | } else {\r | |
99 | Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r | |
100 | for (Walker = Param1Copy; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r | |
101 | if (*Walker == L'\"') {\r | |
102 | CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r | |
103 | }\r | |
104 | }\r | |
105 | \r | |
106 | if (Param1Copy != NULL) {\r | |
107 | Param1Copy = PathCleanUpDirectories(Param1Copy);\r | |
108 | }\r | |
109 | if (Param1Copy != NULL) {\r | |
110 | if (StrCmp(Param1Copy, L".") == 0) {\r | |
111 | //\r | |
112 | // nothing to do... change to current directory\r | |
113 | //\r | |
114 | } else if (StrCmp(Param1Copy, L"..") == 0) {\r | |
115 | //\r | |
116 | // Change up one directory...\r | |
117 | //\r | |
118 | Directory = ShellGetCurrentDir(NULL);\r | |
119 | if (Directory == NULL) {\r | |
120 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r | |
121 | ShellStatus = SHELL_NOT_FOUND;\r | |
122 | } else {\r | |
123 | Drive = GetFullyQualifiedPath(Directory);\r | |
124 | PathRemoveLastItem(Drive);\r | |
125 | }\r | |
126 | if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r | |
127 | //\r | |
128 | // change directory on current drive letter\r | |
129 | //\r | |
130 | Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r | |
131 | if (Status == EFI_NOT_FOUND) {\r | |
132 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
133 | ShellStatus = SHELL_NOT_FOUND;\r | |
134 | }\r | |
135 | }\r | |
136 | } else if (StrCmp(Param1Copy, L"\\") == 0) {\r | |
137 | //\r | |
138 | // Move to root of current drive\r | |
139 | //\r | |
140 | Directory = ShellGetCurrentDir(NULL);\r | |
141 | if (Directory == NULL) {\r | |
142 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r | |
143 | ShellStatus = SHELL_NOT_FOUND;\r | |
144 | } else {\r | |
145 | Drive = GetFullyQualifiedPath(Directory);\r | |
146 | while (PathRemoveLastItem(Drive)) ;\r | |
147 | }\r | |
148 | if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r | |
149 | //\r | |
150 | // change directory on current drive letter\r | |
151 | //\r | |
152 | Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r | |
153 | if (Status == EFI_NOT_FOUND) {\r | |
154 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
155 | ShellStatus = SHELL_NOT_FOUND;\r | |
156 | }\r | |
157 | }\r | |
158 | } else if (StrStr(Param1Copy, L":") == NULL) {\r | |
159 | //\r | |
160 | // change directory without a drive identifier\r | |
161 | //\r | |
162 | if (ShellGetCurrentDir(NULL) == NULL) {\r | |
163 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r | |
164 | ShellStatus = SHELL_NOT_FOUND;\r | |
165 | } else {\r | |
166 | ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r | |
167 | Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r | |
168 | if (*Param1Copy == L'\\') {\r | |
169 | while (PathRemoveLastItem(Drive)) ;\r | |
170 | Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r | |
171 | } else {\r | |
172 | Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r | |
173 | }\r | |
174 | //\r | |
175 | // Verify that this is a valid directory\r | |
176 | //\r | |
177 | Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r | |
178 | if (EFI_ERROR(Status)) {\r | |
179 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r | |
180 | ShellStatus = SHELL_NOT_FOUND;\r | |
181 | } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r | |
182 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r | |
183 | ShellStatus = SHELL_NOT_FOUND;\r | |
184 | }\r | |
185 | if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r | |
186 | //\r | |
187 | // change directory on current drive letter\r | |
188 | //\r | |
189 | Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r | |
190 | if (Status == EFI_NOT_FOUND) {\r | |
191 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
192 | ShellStatus = SHELL_NOT_FOUND;\r | |
193 | }\r | |
194 | }\r | |
195 | if (Handle != NULL) {\r | |
196 | gEfiShellProtocol->CloseFile(Handle);\r | |
197 | DEBUG_CODE(Handle = NULL;);\r | |
198 | }\r | |
199 | }\r | |
200 | } else {\r | |
201 | //\r | |
202 | // change directory with a drive letter\r | |
203 | //\r | |
204 | Drive = AllocateCopyPool(StrSize(Param1Copy), Param1Copy);\r | |
205 | if (Drive == NULL) {\r | |
206 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r | |
207 | ShellStatus = SHELL_OUT_OF_RESOURCES;\r | |
208 | } else {\r | |
209 | Path = StrStr(Drive, L":");\r | |
210 | ASSERT(Path != NULL);\r | |
211 | if (EFI_ERROR(ShellIsDirectory(Param1Copy))) {\r | |
212 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Param1Copy);\r | |
213 | ShellStatus = SHELL_NOT_FOUND;\r | |
214 | } else if (*(Path+1) == CHAR_NULL) {\r | |
215 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
216 | ShellStatus = SHELL_NOT_FOUND;\r | |
217 | } else {\r | |
218 | *(Path+1) = CHAR_NULL;\r | |
219 | if (Path == Drive + StrLen(Drive)) {\r | |
220 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
221 | ShellStatus = SHELL_NOT_FOUND;\r | |
222 | } else {\r | |
223 | Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r | |
224 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r | |
225 | }\r | |
226 | }\r | |
227 | if (Status == EFI_NOT_FOUND) {\r | |
228 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r | |
229 | Status = SHELL_NOT_FOUND;\r | |
230 | } else if (EFI_ERROR(Status)) {\r | |
231 | ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1Copy);\r | |
232 | Status = SHELL_NOT_FOUND;\r | |
233 | }\r | |
234 | }\r | |
235 | }\r | |
236 | }\r | |
237 | FreePool(Param1Copy);\r | |
238 | }\r | |
239 | }\r | |
240 | \r | |
241 | if (Drive != NULL) {\r | |
242 | FreePool(Drive);\r | |
243 | }\r | |
244 | //\r | |
245 | // free the command line package\r | |
246 | //\r | |
247 | ShellCommandLineFreeVarList (Package);\r | |
248 | \r | |
249 | //\r | |
250 | // return the status\r | |
251 | //\r | |
252 | return (ShellStatus);\r | |
253 | }\r | |
254 | \r |