]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg: Update Level2 profile commands response output
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cd.c
CommitLineData
a405b86d 1/** @file\r
2 Main file for attrib shell level 2 function.\r
3\r
099e8ff5 4 Copyright (c) 2015, Hewlett-Packard Development Company, L.P.<BR>\r
cea5e3b9 5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
a405b86d 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
1fc3749d 41 CHAR16 *Param1Copy;\r
0960ba17 42 CHAR16* Walker;\r
a405b86d 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
099e8ff5 64 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cd", ProblemParam); \r
a405b86d 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
099e8ff5 78 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 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
099e8ff5 96 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 97 ShellStatus = SHELL_NOT_FOUND;\r
98 }\r
99 } else {\r
1fc3749d 100 Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
0960ba17
QS
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
beab0fc5 107 if (Param1Copy != NULL) {\r
108 Param1Copy = PathCleanUpDirectories(Param1Copy);\r
109 }\r
110 if (Param1Copy != NULL) {\r
111 if (StrCmp(Param1Copy, L".") == 0) {\r
a405b86d 112 //\r
beab0fc5 113 // nothing to do... change to current directory\r
a405b86d 114 //\r
beab0fc5 115 } else if (StrCmp(Param1Copy, L"..") == 0) {\r
a405b86d 116 //\r
beab0fc5 117 // Change up one directory...\r
a405b86d 118 //\r
beab0fc5 119 Directory = ShellGetCurrentDir(NULL);\r
120 if (Directory == NULL) {\r
099e8ff5 121 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 122 ShellStatus = SHELL_NOT_FOUND;\r
a405b86d 123 } else {\r
beab0fc5 124 Drive = GetFullyQualifiedPath(Directory);\r
125 PathRemoveLastItem(Drive);\r
a405b86d 126 }\r
beab0fc5 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
099e8ff5 133 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 134 ShellStatus = SHELL_NOT_FOUND;\r
135 }\r
136 }\r
137 } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
a405b86d 138 //\r
beab0fc5 139 // Move to root of current drive\r
a405b86d 140 //\r
beab0fc5 141 Directory = ShellGetCurrentDir(NULL);\r
142 if (Directory == NULL) {\r
099e8ff5 143 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 144 ShellStatus = SHELL_NOT_FOUND;\r
beab0fc5 145 } else {\r
146 Drive = GetFullyQualifiedPath(Directory);\r
147 while (PathRemoveLastItem(Drive)) ;\r
a405b86d 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
099e8ff5 155 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
a405b86d 156 ShellStatus = SHELL_NOT_FOUND;\r
157 }\r
158 }\r
beab0fc5 159 } else if (StrStr(Param1Copy, L":") == NULL) {\r
668a5781 160 //\r
161 // change directory without a drive identifier\r
162 //\r
beab0fc5 163 if (ShellGetCurrentDir(NULL) == NULL) {\r
099e8ff5 164 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 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
099e8ff5 180 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Drive); \r
beab0fc5 181 ShellStatus = SHELL_NOT_FOUND;\r
182 } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
099e8ff5 183 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Drive); \r
beab0fc5 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
099e8ff5 192 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 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
a405b86d 200 }\r
9ea69f8a 201 } else {\r
beab0fc5 202 //\r
668a5781 203 // change directory with a drive letter\r
beab0fc5 204 //\r
cea5e3b9 205 Drive = AllocateCopyPool(StrSize(Param1Copy), Param1Copy);\r
beab0fc5 206 if (Drive == NULL) {\r
099e8ff5 207 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 208 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
9ea69f8a 209 } else {\r
beab0fc5 210 Path = StrStr(Drive, L":");\r
211 ASSERT(Path != NULL);\r
668a5781 212 if (EFI_ERROR(ShellIsDirectory(Param1Copy))) {\r
099e8ff5 213 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cd", Param1Copy); \r
668a5781 214 ShellStatus = SHELL_NOT_FOUND;\r
215 } else if (*(Path+1) == CHAR_NULL) {\r
099e8ff5 216 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
b54fd049 217 ShellStatus = SHELL_NOT_FOUND;\r
218 } else {\r
beab0fc5 219 *(Path+1) = CHAR_NULL;\r
220 if (Path == Drive + StrLen(Drive)) {\r
099e8ff5 221 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 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
099e8ff5 229 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle, L"cd"); \r
beab0fc5 230 Status = SHELL_NOT_FOUND;\r
231 } else if (EFI_ERROR(Status)) {\r
099e8ff5 232 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cd", Param1Copy); \r
beab0fc5 233 Status = SHELL_NOT_FOUND;\r
b54fd049 234 }\r
9ea69f8a 235 }\r
a405b86d 236 }\r
237 }\r
1fc3749d 238 FreePool(Param1Copy);\r
a405b86d 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