2 Main file for attrib shell level 2 function.
4 Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellLevel2CommandsLib.h"
18 Function for 'cd' command.
20 @param[in] ImageHandle Handle to the Image (NULL if Internal).
21 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
26 IN EFI_HANDLE ImageHandle
,
27 IN EFI_SYSTEM_TABLE
*SystemTable
32 CONST CHAR16
*Directory
;
37 SHELL_STATUS ShellStatus
;
38 SHELL_FILE_HANDLE Handle
;
43 ShellStatus
= SHELL_SUCCESS
;
47 Status
= CommandInit();
48 ASSERT_EFI_ERROR(Status
);
51 // initialize the shell lib (we must be in non-auto-init...)
53 Status
= ShellInitialize();
54 ASSERT_EFI_ERROR(Status
);
57 // parse the command line
59 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
60 if (EFI_ERROR(Status
)) {
61 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
62 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
63 FreePool(ProblemParam
);
64 ShellStatus
= SHELL_INVALID_PARAMETER
;
73 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
75 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
76 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel2HiiHandle
);
77 ShellStatus
= SHELL_INVALID_PARAMETER
;
80 // remember that param 0 is the command name
81 // If there are 0 value parameters, then print the current directory
82 // else If there are 2 value parameters, then print the error message
83 // else If there is 1 value paramerer , then change the directory
85 Param1
= ShellCommandLineGetRawValue(Package
, 1);
88 // display the current directory
90 Directory
= ShellGetCurrentDir(NULL
);
91 if (Directory
!= NULL
) {
92 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_PRINT
), gShellLevel2HiiHandle
, Directory
);
94 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
95 ShellStatus
= SHELL_NOT_FOUND
;
98 Param1Copy
= CatSPrint(NULL
, L
"%s", Param1
, NULL
);
99 Param1Copy
= PathCleanUpDirectories(Param1Copy
);
100 if (StrCmp(Param1Copy
, L
".") == 0) {
102 // nothing to do... change to current directory
104 } else if (StrCmp(Param1Copy
, L
"..") == 0) {
106 // Change up one directory...
108 Directory
= ShellGetCurrentDir(NULL
);
109 if (Directory
== NULL
) {
110 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
111 ShellStatus
= SHELL_NOT_FOUND
;
113 Drive
= GetFullyQualifiedPath(Directory
);
114 PathRemoveLastItem(Drive
);
116 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
118 // change directory on current drive letter
120 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
121 if (Status
== EFI_NOT_FOUND
) {
122 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
123 ShellStatus
= SHELL_NOT_FOUND
;
126 } else if (StrCmp(Param1Copy
, L
"\\") == 0) {
128 // Move to root of current drive
130 Directory
= ShellGetCurrentDir(NULL
);
131 if (Directory
== NULL
) {
132 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
133 ShellStatus
= SHELL_NOT_FOUND
;
135 Drive
= GetFullyQualifiedPath(Directory
);
136 while (PathRemoveLastItem(Drive
)) ;
138 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
140 // change directory on current drive letter
142 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
143 if (Status
== EFI_NOT_FOUND
) {
144 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
145 ShellStatus
= SHELL_NOT_FOUND
;
148 } else if (StrStr(Param1Copy
, L
":") == NULL
) {
149 if (ShellGetCurrentDir(NULL
) == NULL
) {
150 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
151 ShellStatus
= SHELL_NOT_FOUND
;
153 ASSERT((Drive
== NULL
&& DriveSize
== 0) || (Drive
!= NULL
));
154 Drive
= StrnCatGrow(&Drive
, &DriveSize
, ShellGetCurrentDir(NULL
), 0);
155 if (*Param1Copy
== L
'\\') {
156 while (PathRemoveLastItem(Drive
)) ;
157 Drive
= StrnCatGrow(&Drive
, &DriveSize
, Param1Copy
+1, 0);
159 Drive
= StrnCatGrow(&Drive
, &DriveSize
, Param1Copy
, 0);
162 // Verify that this is a valid directory
164 Status
= gEfiShellProtocol
->OpenFileByName(Drive
, &Handle
, EFI_FILE_MODE_READ
);
165 if (EFI_ERROR(Status
)) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, Drive
);
167 ShellStatus
= SHELL_NOT_FOUND
;
168 } else if (EFI_ERROR(FileHandleIsDirectory(Handle
))) {
169 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, Drive
);
170 ShellStatus
= SHELL_NOT_FOUND
;
172 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
174 // change directory on current drive letter
176 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
177 if (Status
== EFI_NOT_FOUND
) {
178 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
179 ShellStatus
= SHELL_NOT_FOUND
;
182 if (Handle
!= NULL
) {
183 gEfiShellProtocol
->CloseFile(Handle
);
184 DEBUG_CODE(Handle
= NULL
;);
189 // change directory on other drive letter
191 Drive
= AllocateZeroPool(StrSize(Param1Copy
));
193 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_MEM
), gShellLevel2HiiHandle
);
194 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
196 Drive
= StrCpy(Drive
, Param1Copy
);
197 Path
= StrStr(Drive
, L
":");
198 ASSERT(Path
!= NULL
);
199 if (*(Path
+1) == CHAR_NULL
) {
200 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
201 ShellStatus
= SHELL_NOT_FOUND
;
203 *(Path
+1) = CHAR_NULL
;
204 if (Path
== Drive
+ StrLen(Drive
)) {
205 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
206 ShellStatus
= SHELL_NOT_FOUND
;
208 Status
= gEfiShellProtocol
->SetCurDir(Drive
, Path
+2);
209 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_PRINT
), gShellLevel2HiiHandle
, ShellGetCurrentDir(Drive
));
212 if (Status
== EFI_NOT_FOUND
) {
213 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
214 Status
= SHELL_NOT_FOUND
;
215 } else if (EFI_ERROR(Status
)) {
216 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, Param1Copy
);
217 Status
= SHELL_NOT_FOUND
;
221 FreePool(Param1Copy
);
229 // free the command line package
231 ShellCommandLineFreeVarList (Package
);
236 return (ShellStatus
);