Skip to content

Commit

Permalink
This refactors 3 functions out of ShellCommandLib and puts them into …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jcarsey committed Jun 30, 2011
1 parent f1518f6 commit ab94587
Show file tree
Hide file tree
Showing 21 changed files with 245 additions and 173 deletions.
2 changes: 1 addition & 1 deletion ShellPkg/Application/Shell/FileHandleWrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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' '
Expand Down
2 changes: 1 addition & 1 deletion ShellPkg/Application/Shell/Shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ShellPkg/Application/Shell/Shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion ShellPkg/Application/Shell/Shell.inf
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -69,6 +69,7 @@
HiiLib
SortLib
HandleParsingLib
PathLib

[Guids]
gShellVariableGuid # ALWAYS_CONSUMED
Expand Down
12 changes: 6 additions & 6 deletions ShellPkg/Application/Shell/ShellProtocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ EfiShellGetDevicePathFromFilePath(
StrCpy(NewPath, Cwd);
if (*Path == L'\\') {
Path++;
while (ChopLastSlash(NewPath)) ;
while (PathRemoveLastItem(NewPath)) ;
}
StrCat(NewPath, Path);
DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);
Expand Down Expand Up @@ -2232,7 +2232,7 @@ EfiShellFindFiles(
}
StrCpy(PatternCopy, FilePattern);

PatternCopy = CleanPath(PatternCopy);
PatternCopy = PathCleanUpDirectories(PatternCopy);

Count = StrStr(PatternCopy, L":") - PatternCopy;
Count += 2;
Expand Down Expand Up @@ -2293,7 +2293,7 @@ EfiShellOpenFileList(
CONST CHAR16 *CurDir;
BOOLEAN Found;

ShellCommandCleanPath(Path);
PathCleanUpDirectories(Path);

Path2Size = 0;
Path2 = NULL;
Expand All @@ -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);
Expand All @@ -2324,7 +2324,7 @@ EfiShellOpenFileList(
StrnCatGrow(&Path2, NULL, Path, 0);
}

CleanPath (Path2);
PathCleanUpDirectories (Path2);

//
// do the search
Expand Down Expand Up @@ -2677,7 +2677,7 @@ EfiShellSetCurDir(
DirectoryName = StrnCatGrow(&DirectoryName, NULL, Dir, 0);
ASSERT(DirectoryName != NULL);

CleanPath(DirectoryName);
PathCleanUpDirectories(DirectoryName);

if (FileSystem == NULL) {
//
Expand Down
56 changes: 56 additions & 0 deletions ShellPkg/Include/Library/PathLib.h
Original file line number Diff line number Diff line change
@@ -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_
45 changes: 0 additions & 45 deletions ShellPkg/Include/Library/ShellCommandLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down Expand Up @@ -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_
122 changes: 122 additions & 0 deletions ShellPkg/Library/BasePathLib/BasePathLib.c
Original file line number Diff line number Diff line change
@@ -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);
}

38 changes: 38 additions & 0 deletions ShellPkg/Library/BasePathLib/BasePathLib.inf
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit ab94587

Please sign in to comment.