This refactors 3 functions out of ShellCommandLib and puts them into a new library (but as 2 functions instead of 3). This allows for users outside of the shell itself to have access to these functions.

1) Remove the 3 functions out of the shell's internal library (ShellCommandLib)
2) Add a new library class (PathLib)
3) Add an instance of this class (BasePathLib)
4) Change all internal shell callers to use this new library class.

signed-off-by: jcarsey
reviewed-by: jljusten

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@11936 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
jcarsey 2011-06-30 22:58:53 +00:00
parent f1518f6970
commit ab94587a7d
21 changed files with 245 additions and 173 deletions

View File

@ -522,7 +522,7 @@ FileInterfaceStdInRead(
}
StrCat(TabStr, L"*");
FoundFileList = NULL;
// TabStr = CleanPath(TabStr);
// TabStr = PathCleanUpDirectories(TabStr);
Status = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FoundFileList);
for ( TempStr = CurrentString
; *TempStr == L' '

View File

@ -859,7 +859,7 @@ DoStartupScript(
*TempSpot = CHAR_NULL;
}
FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);
ChopLastSlash(FileStringPath);
PathRemoveLastItem(FileStringPath);
FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);
Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);
FreePool(FileStringPath);

View File

@ -44,6 +44,7 @@
#include <Library/HiiLib.h>
#include <Library/PrintLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/PathLib.h>
#include "ShellParametersProtocol.h"
#include "ShellProtocol.h"

View File

@ -1,7 +1,7 @@
## @file
# This is the shell application
#
# Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
@ -69,6 +69,7 @@
HiiLib
SortLib
HandleParsingLib
PathLib
[Guids]
gShellVariableGuid # ALWAYS_CONSUMED

View File

@ -529,7 +529,7 @@ EfiShellGetDevicePathFromFilePath(
StrCpy(NewPath, Cwd);
if (*Path == L'\\') {
Path++;
while (ChopLastSlash(NewPath)) ;
while (PathRemoveLastItem(NewPath)) ;
}
StrCat(NewPath, Path);
DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);
@ -2232,7 +2232,7 @@ EfiShellFindFiles(
}
StrCpy(PatternCopy, FilePattern);
PatternCopy = CleanPath(PatternCopy);
PatternCopy = PathCleanUpDirectories(PatternCopy);
Count = StrStr(PatternCopy, L":") - PatternCopy;
Count += 2;
@ -2293,7 +2293,7 @@ EfiShellOpenFileList(
CONST CHAR16 *CurDir;
BOOLEAN Found;
ShellCommandCleanPath(Path);
PathCleanUpDirectories(Path);
Path2Size = 0;
Path2 = NULL;
@ -2315,7 +2315,7 @@ EfiShellOpenFileList(
StrnCatGrow(&Path2, &Path2Size, CurDir, 0);
if (*Path == L'\\') {
Path++;
while (ChopLastSlash(Path2)) ;
while (PathRemoveLastItem(Path2)) ;
}
ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
StrnCatGrow(&Path2, &Path2Size, Path, 0);
@ -2324,7 +2324,7 @@ EfiShellOpenFileList(
StrnCatGrow(&Path2, NULL, Path, 0);
}
CleanPath (Path2);
PathCleanUpDirectories (Path2);
//
// do the search
@ -2677,7 +2677,7 @@ EfiShellSetCurDir(
DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0);
ASSERT(DirectoryName != NULL);
CleanPath(DirectoryName);
PathCleanUpDirectories(DirectoryName);
if (FileSystem == NULL) {
//

View File

@ -0,0 +1,56 @@
/** @file
Provides interface to path manipulation functions.
Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#ifndef _PATH_LIB_
#define _PATH_LIB_
#include <Base.h>
#include <Library/BaseMemoryLib.h>
/**
Removes the last directory or file entry in a path by changing the last
L'\' to a CHAR_NULL.
@param[in,out] Path The pointer to the path to modify.
@retval FALSE Nothing was found to remove.
@retval TRUE A directory or file was removed.
**/
BOOLEAN
EFIAPI
PathRemoveLastItem(
IN OUT CHAR16 *Path
);
/**
Function to clean up paths.
- Single periods in the path are removed.
- Double periods in the path are removed along with a single parent directory.
- Forward slashes L'/' are converted to backward slashes L'\'.
This will be done inline and the existing buffer may be larger than required
upon completion.
@param[in] Path The pointer to the string containing the path.
@retval NULL An error occured.
@return Path in all other instances.
**/
CHAR16*
EFIAPI
PathCleanUpDirectories(
IN CHAR16 *Path
);
#endif //_PATH_LIB_

View File

@ -572,20 +572,6 @@ ShellCommandCreateInitialMappingsAndPaths(
VOID
);
/**
Function to standardize the directory indicators to \ characters.
@param[in,out] Path The pointer to the path string to fix.
@retval NULL The operation failed.
@return The Path pointer.
**/
CHAR16*
EFIAPI
ShellCommandCleanPath (
IN OUT CHAR16 *Path
);
/**
Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
@ -734,35 +720,4 @@ FreeBufferList (
IN BUFFER_LIST *List
);
/**
Chops off last directory or file entry in a path by changing the last '\' to a CHAR_NULL
@param[in,out] PathToReturn The pointer to the path to modify.
@retval FALSE No directory was found to chop off.
@retval TRUE A directory was chopped off.
**/
BOOLEAN
EFIAPI
ChopLastSlash(
IN OUT CHAR16 *PathToReturn
);
/**
Function to clean up paths. Removes the following items:
single periods in the path (no need for the current directory tag)
double periods in the path and removes a single parent directory.
This will be done inline and the resultant string may be be 'too big'.
@param[in] PathToReturn The pointer to the string containing the path.
@return PathToReturn is always returned.
**/
CHAR16*
EFIAPI
CleanPath(
IN CHAR16 *PathToReturn
);
#endif //_SHELL_COMMAND_LIB_

View File

@ -0,0 +1,122 @@
/** @file
Provides interface to path manipulation functions.
Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Base.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PathLib.h>
#include <Library/BaseLib.h>
/**
Removes the last directory or file entry in a path by changing the last
L'\' to a CHAR_NULL.
@param[in,out] Path The pointer to the path to modify.
@retval FALSE Nothing was found to remove.
@retval TRUE A directory or file was removed.
**/
BOOLEAN
EFIAPI
PathRemoveLastItem(
IN OUT CHAR16 *Path
)
{
CHAR16 *Walker;
CHAR16 *LastSlash;
//
// get directory name from path... ('chop' off extra)
//
for ( Walker = Path, LastSlash = NULL
; Walker != NULL && *Walker != CHAR_NULL
; Walker++
){
if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
LastSlash = Walker+1;
}
}
if (LastSlash != NULL) {
*LastSlash = CHAR_NULL;
return (TRUE);
}
return (FALSE);
}
/**
Function to clean up paths.
- Single periods in the path are removed.
- Double periods in the path are removed along with a single parent directory.
- Forward slashes L'/' are converted to backward slashes L'\'.
This will be done inline and the existing buffer may be larger than required
upon completion.
@param[in] Path The pointer to the string containing the path.
@retval NULL An error occured.
@return Path in all other instances.
**/
CHAR16*
EFIAPI
PathCleanUpDirectories(
IN CHAR16 *Path
)
{
CHAR16 *TempString;
UINTN TempSize;
if (Path==NULL) {
return(NULL);
}
//
// Fix up the / vs \
//
for (TempString = Path ; TempString != NULL && *TempString != CHAR_NULL ; TempString++) {
if (*TempString == L'/') {
*TempString = L'\\';
}
}
//
// Fix up the ..
//
while ((TempString = StrStr(Path, L"\\..\\")) != NULL) {
*TempString = CHAR_NULL;
TempString += 4;
PathRemoveLastItem(Path);
TempSize = StrSize(TempString);
CopyMem(Path+StrLen(Path), TempString, TempSize);
}
if ((TempString = StrStr(Path, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {
*TempString = CHAR_NULL;
PathRemoveLastItem(Path);
}
//
// Fix up the .
//
while ((TempString = StrStr(Path, L"\\.\\")) != NULL) {
*TempString = CHAR_NULL;
TempString += 2;
TempSize = StrSize(TempString);
CopyMem(Path+StrLen(Path), TempString, TempSize);
}
if ((TempString = StrStr(Path, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {
*TempString = CHAR_NULL;
}
return (Path);
}

View File

@ -0,0 +1,38 @@
## @file
# Provides interface to path manipulation functions.
#
# Copyright (c) 2011, Intel Corporation. All rights reserved. <BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
#
##
[Defines]
INF_VERSION = 0x00010006
BASE_NAME = BasePathLib
FILE_GUID = ED244F93-B97A-4a17-83E0-A03CF2A7F7B4
MODULE_TYPE = UEFI_DRIVER
VERSION_STRING = 1.0
LIBRARY_CLASS = PathLib|UEFI_APPLICATION UEFI_DRIVER
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#
[Sources.common]
BasePathLib.c
[Packages]
MdePkg/MdePkg.dec
ShellPkg/ShellPkg.dec
[LibraryClasses]
BaseMemoryLib
BaseLib

View File

@ -1122,30 +1122,6 @@ ShellCommandCreateInitialMappingsAndPaths(
return (EFI_SUCCESS);
}
/**
Function to make sure all directory delimeters are backslashes.
@param[in,out] Path The path to modify.
@return Path.
**/
CHAR16*
EFIAPI
ShellCommandCleanPath (
IN OUT CHAR16 *Path
)
{
CHAR16 *Path2;
for (Path2 = Path ; Path2 != NULL && *Path2 != CHAR_NULL ; Path2++) {
if (*Path2 == L'/') {
*Path2 = L'\\';
}
}
return (Path);
}
/**
Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
@ -1489,85 +1465,3 @@ FreeBufferList (
}
}
/**
Chops off last directory or file entry in a path leaving the trailing slash
@param[in,out] PathToReturn The path to modify.
@retval FALSE No directory was found to chop off.
@retval TRUE A directory was chopped off.
**/
BOOLEAN
EFIAPI
ChopLastSlash(
IN OUT CHAR16 *PathToReturn
)
{
CHAR16 *Walker;
CHAR16 *LastSlash;
//
// get directory name from path... ('chop' off extra)
//
for ( Walker = PathToReturn, LastSlash = NULL
; Walker != NULL && *Walker != CHAR_NULL
; Walker++
){
if (*Walker == L'\\' && *(Walker + 1) != CHAR_NULL) {
LastSlash = Walker+1;
}
}
if (LastSlash != NULL) {
*LastSlash = CHAR_NULL;
return (TRUE);
}
return (FALSE);
}
/**
Function to clean up paths. Removes the following items:
single periods in the path (no need for the current directory tag)
double periods in the path and removes a single parent directory.
This will be done inline and the resultant string may be be 'too big'.
@param[in] PathToReturn The pointer to the string containing the path.
@return PathToReturn is always returned.
**/
CHAR16*
EFIAPI
CleanPath(
IN CHAR16 *PathToReturn
)
{
CHAR16 *TempString;
UINTN TempSize;
if (PathToReturn==NULL) {
return(NULL);
}
//
// Fix up the directory name
//
while ((TempString = StrStr(PathToReturn, L"\\..\\")) != NULL) {
*TempString = CHAR_NULL;
TempString += 4;
ChopLastSlash(PathToReturn);
TempSize = StrSize(TempString);
CopyMem(PathToReturn+StrLen(PathToReturn), TempString, TempSize);
}
if ((TempString = StrStr(PathToReturn, L"\\..")) != NULL && *(TempString + 3) == CHAR_NULL) {
*TempString = CHAR_NULL;
ChopLastSlash(PathToReturn);
}
while ((TempString = StrStr(PathToReturn, L"\\.\\")) != NULL) {
*TempString = CHAR_NULL;
TempString += 2;
TempSize = StrSize(TempString);
CopyMem(PathToReturn+StrLen(PathToReturn), TempString, TempSize);
}
if ((TempString = StrStr(PathToReturn, L"\\.")) != NULL && *(TempString + 2) == CHAR_NULL) {
*TempString = CHAR_NULL;
}
return (PathToReturn);
}

View File

@ -14,6 +14,7 @@
#include "UefiShellLevel1CommandsLib.h"
#include <Library/PrintLib.h>
#include <Library/PathLib.h>
typedef enum {
EndTagOr,
@ -95,10 +96,7 @@ IsValidProfile (
CONST CHAR16 *TempLocation;
ProfilesString = ShellGetEnvironmentVariable(L"profiles");
//
// According to the Shell spec this is a required environment variable.
//
ASSERT(ProfileString != NULL);
ASSERT(ProfilesString != NULL);
TempLocation = StrStr(ProfilesString, String);
if ((TempLocation != NULL) && (*(TempLocation-1) == L';') && (*(TempLocation+StrLen(String)) == L';')) {
return (TRUE);

View File

@ -50,6 +50,7 @@
UefiBootServicesTableLib
SortLib
PrintLib
PathLib
[Pcd.common]
gEfiShellPkgTokenSpaceGuid.PcdShellSupportLevel # ALWAYS_CONSUMED

View File

@ -108,7 +108,7 @@ ShellCommandRunCd (
ShellStatus = SHELL_NOT_FOUND;
} else {
Drive = GetFullyQualifiedPath(Directory);
ChopLastSlash(Drive);
PathRemoveLastItem(Drive);
}
if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {
//
@ -130,7 +130,7 @@ ShellCommandRunCd (
ShellStatus = SHELL_NOT_FOUND;
} else {
Drive = GetFullyQualifiedPath(Directory);
while (ChopLastSlash(Drive)) ;
while (PathRemoveLastItem(Drive)) ;
}
if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {
//
@ -150,7 +150,7 @@ ShellCommandRunCd (
ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));
Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);
if (*Param1 == L'\\') {
while (ChopLastSlash(Drive)) ;
while (PathRemoveLastItem(Drive)) ;
Drive = StrnCatGrow(&Drive, &DriveSize, Param1+1, 0);
} else {
Drive = StrnCatGrow(&Drive, &DriveSize, Param1, 0);

View File

@ -416,7 +416,7 @@ ValidateAndCopyFiles(
break;
}
CleanPath(DestPath);
PathCleanUpDirectories(DestPath);
ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);
@ -619,7 +619,7 @@ ShellCommandRunCp (
// now copy them all...
//
if (FileList != NULL && !IsListEmpty(&FileList->Link)) {
ShellStatus = ProcessValidateAndCopyFiles(FileList, ShellCommandCleanPath((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);
ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);
Status = ShellCloseFileMetaArg(&FileList);
if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);

View File

@ -67,7 +67,7 @@ PrintLsOutput(
CorrectedPath = StrnCatGrow(&CorrectedPath, NULL, Path, 0);
ASSERT(CorrectedPath != NULL);
ShellCommandCleanPath(CorrectedPath);
PathCleanUpDirectories(CorrectedPath);
Status = ShellOpenFileMetaArg((CHAR16*)CorrectedPath, EFI_FILE_MODE_READ, &ListHead);
if (EFI_ERROR(Status)) {

View File

@ -148,7 +148,7 @@ GetDestinationLocation(
return (SHELL_OUT_OF_RESOURCES);
}
StrCpy(DestPath, Cwd);
while (ChopLastSlash(DestPath)) ;
while (PathRemoveLastItem(DestPath)) ;
*DestPathPointer = DestPath;
return (SHELL_SUCCESS);
}
@ -268,7 +268,7 @@ ValidateAndMoveFiles(
if (ShellStatus != SHELL_SUCCESS) {
return (ShellStatus);
}
DestPath = CleanPath(DestPath);
DestPath = PathCleanUpDirectories(DestPath);
HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_MV_OUTPUT), NULL);
HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);

View File

@ -196,7 +196,7 @@ GetFullyQualifiedPath(
}
StrnCatGrow(&PathToReturn, &Size, Path, 0);
CleanPath(PathToReturn);
PathCleanUpDirectories(PathToReturn);
while (PathToReturn[StrLen(PathToReturn)-1] == L'*') {
PathToReturn[StrLen(PathToReturn)-1] = CHAR_NULL;

View File

@ -46,6 +46,7 @@
#include <Library/HiiLib.h>
#include <Library/SortLib.h>
#include <Library/FileHandleLib.h>
#include <Library/PathLib.h>
extern CONST CHAR16 mFileName[];
extern EFI_HANDLE gShellLevel2HiiHandle;

View File

@ -64,6 +64,7 @@
PcdLib
HiiLib
HandleParsingLib
PathLib
[Protocols]
gEfiUnicodeCollation2ProtocolGuid # ALWAYS_CONSUMED

View File

@ -41,6 +41,9 @@
## @libraryclass Provides advanced parsing functions
HandleParsingLib|Include/Library/HandleParsingLib.h
## @libraryclass Provides path manipulation functions
PathLib|Include/Library/PathLib.h
[Guids]
gEfiShellEnvironment2ExtGuid = {0xd2c18636, 0x40e5, 0x4eb5, {0xa3, 0x1b, 0x36, 0x69, 0x5f, 0xd4, 0x2c, 0x87}}

View File

@ -48,6 +48,7 @@
HandleParsingLib|ShellPkg/Library/UefiHandleParsingLib/UefiHandleParsingLib.inf
PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf
PathLib|ShellPkg/Library/BasePathLib/BasePathLib.inf
[LibraryClasses.ARM]
#