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
;
42 ShellStatus
= SHELL_SUCCESS
;
46 Status
= CommandInit();
47 ASSERT_EFI_ERROR(Status
);
50 // initialize the shell lib (we must be in non-auto-init...)
52 Status
= ShellInitialize();
53 ASSERT_EFI_ERROR(Status
);
56 // parse the command line
58 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
59 if (EFI_ERROR(Status
)) {
60 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
61 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellLevel2HiiHandle
, ProblemParam
);
62 FreePool(ProblemParam
);
63 ShellStatus
= SHELL_INVALID_PARAMETER
;
72 if (ShellCommandLineGetFlag(Package
, L
"-?")) {
74 } else if (ShellCommandLineGetRawValue(Package
, 2) != NULL
) {
75 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellLevel2HiiHandle
);
76 ShellStatus
= SHELL_INVALID_PARAMETER
;
79 // remember that param 0 is the command name
80 // If there are 0 value parameters, then print the current directory
81 // else If there are 2 value parameters, then print the error message
82 // else If there is 1 value paramerer , then change the directory
84 Param1
= ShellCommandLineGetRawValue(Package
, 1);
87 // display the current directory
89 Directory
= ShellGetCurrentDir(NULL
);
90 if (Directory
!= NULL
) {
91 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_PRINT
), gShellLevel2HiiHandle
, Directory
);
93 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
94 ShellStatus
= SHELL_NOT_FOUND
;
97 if (StrCmp(Param1
, L
".") == 0) {
99 // nothing to do... change to current directory
101 } else if (StrCmp(Param1
, L
"..") == 0) {
103 // Change up one directory...
105 Directory
= ShellGetCurrentDir(NULL
);
106 if (Directory
== NULL
) {
107 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
108 ShellStatus
= SHELL_NOT_FOUND
;
110 Drive
= GetFullyQualifiedPath(Directory
);
111 ChopLastSlash(Drive
);
113 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
115 // change directory on current drive letter
117 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
118 if (Status
== EFI_NOT_FOUND
) {
119 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
120 ShellStatus
= SHELL_NOT_FOUND
;
123 } else if (StrCmp(Param1
, L
"\\") == 0) {
125 // Move to root of current drive
127 Directory
= ShellGetCurrentDir(NULL
);
128 if (Directory
== NULL
) {
129 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
130 ShellStatus
= SHELL_NOT_FOUND
;
132 Drive
= GetFullyQualifiedPath(Directory
);
133 while (ChopLastSlash(Drive
)) ;
135 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
137 // change directory on current drive letter
139 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
140 if (Status
== EFI_NOT_FOUND
) {
141 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
142 ShellStatus
= SHELL_NOT_FOUND
;
145 } else if (StrStr(Param1
, L
":") == NULL
) {
146 if (ShellGetCurrentDir(NULL
) == NULL
) {
147 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_CWD
), gShellLevel2HiiHandle
);
148 ShellStatus
= SHELL_NOT_FOUND
;
150 ASSERT((Drive
== NULL
&& DriveSize
== 0) || (Drive
!= NULL
));
151 Drive
= StrnCatGrow(&Drive
, &DriveSize
, ShellGetCurrentDir(NULL
), 0);
152 if (*Param1
== L
'\\') {
153 while (ChopLastSlash(Drive
)) ;
154 Drive
= StrnCatGrow(&Drive
, &DriveSize
, Param1
+1, 0);
156 Drive
= StrnCatGrow(&Drive
, &DriveSize
, Param1
, 0);
159 // Verify that this is a valid directory
161 Status
= gEfiShellProtocol
->OpenFileByName(Drive
, &Handle
, EFI_FILE_MODE_READ
);
162 if (EFI_ERROR(Status
)) {
163 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, Drive
);
164 ShellStatus
= SHELL_NOT_FOUND
;
165 } else if (EFI_ERROR(FileHandleIsDirectory(Handle
))) {
166 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NOT_DIR
), gShellLevel2HiiHandle
, Drive
);
167 ShellStatus
= SHELL_NOT_FOUND
;
169 if (ShellStatus
== SHELL_SUCCESS
&& Drive
!= NULL
) {
171 // change directory on current drive letter
173 Status
= gEfiShellProtocol
->SetCurDir(NULL
, Drive
);
174 if (Status
== EFI_NOT_FOUND
) {
175 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
176 ShellStatus
= SHELL_NOT_FOUND
;
179 if (Handle
!= NULL
) {
180 gEfiShellProtocol
->CloseFile(Handle
);
181 DEBUG_CODE(Handle
= NULL
;);
186 // change directory on other drive letter
188 Drive
= AllocateZeroPool(StrSize(Param1
));
190 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_NO_MEM
), gShellLevel2HiiHandle
);
191 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
193 Drive
= StrCpy(Drive
, Param1
);
194 Path
= StrStr(Drive
, L
":");
195 ASSERT(Path
!= NULL
);
196 if (*(Path
+1) == CHAR_NULL
) {
197 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
198 ShellStatus
= SHELL_NOT_FOUND
;
200 *(Path
+1) = CHAR_NULL
;
201 if (Path
== Drive
+ StrLen(Drive
)) {
202 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
203 ShellStatus
= SHELL_NOT_FOUND
;
205 Status
= gEfiShellProtocol
->SetCurDir(Drive
, Path
+2);
206 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_PRINT
), gShellLevel2HiiHandle
, ShellGetCurrentDir(Drive
));
209 if (Status
== EFI_NOT_FOUND
) {
210 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_CD_NF
), gShellLevel2HiiHandle
);
211 Status
= SHELL_NOT_FOUND
;
212 } else if (EFI_ERROR(Status
)) {
213 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_DIR_NF
), gShellLevel2HiiHandle
, Param1
);
214 Status
= SHELL_NOT_FOUND
;
225 // free the command line package
227 ShellCommandLineFreeVarList (Package
);
232 return (ShellStatus
);