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