From 4d0a4fcefef53afdaae1f6db616123bfc7b0db08 Mon Sep 17 00:00:00 2001 From: jcarsey Date: Wed, 6 Jul 2011 22:28:36 +0000 Subject: [PATCH] Move the 2 functions associated with reading whole lines at a single time from the shell command (internal) library to the ShellLib (external) library. signed-off-by: jcarsey reviewed-by: lgrosenb git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12000 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Include/Library/ShellCommandLib.h | 58 ------- ShellPkg/Include/Library/ShellLib.h | 57 +++++++ .../UefiShellCommandLib/UefiShellCommandLib.c | 151 ------------------ ShellPkg/Library/UefiShellLib/UefiShellLib.c | 151 ++++++++++++++++++ 4 files changed, 208 insertions(+), 209 deletions(-) diff --git a/ShellPkg/Include/Library/ShellCommandLib.h b/ShellPkg/Include/Library/ShellCommandLib.h index e998ac51b9..5bc03ce256 100644 --- a/ShellPkg/Include/Library/ShellCommandLib.h +++ b/ShellPkg/Include/Library/ShellCommandLib.h @@ -646,64 +646,6 @@ ShellFileHandleEof( IN SHELL_FILE_HANDLE Handle ); -/** - Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned - buffer. The returned buffer must be callee freed. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @return The line of text from the file. - - @sa ShellFileHandleReadLine -**/ -CHAR16* -EFIAPI -ShellFileHandleReturnLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT BOOLEAN *Ascii - ); - -/** - Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Buffer The pointer to buffer to read into. - @param[in,out] Size The pointer to number of bytes in Buffer. - @param[in] Truncate If the buffer is large enough, this has no effect. - If the buffer is is too small and Truncate is TRUE, - the line will be truncated. - If the buffer is is too small and Truncate is FALSE, - then no read will occur. - - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @retval EFI_SUCCESS The operation was successful. The line is stored in - Buffer. - @retval EFI_INVALID_PARAMETER Handle was NULL. - @retval EFI_INVALID_PARAMETER Size was NULL. - @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. - Size was updated to the minimum space required. - @sa ShellFileHandleRead -**/ -EFI_STATUS -EFIAPI -ShellFileHandleReadLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT CHAR16 *Buffer, - IN OUT UINTN *Size, - IN BOOLEAN Truncate, - IN OUT BOOLEAN *Ascii - ); - typedef struct { LIST_ENTRY Link; void *Buffer; diff --git a/ShellPkg/Include/Library/ShellLib.h b/ShellPkg/Include/Library/ShellLib.h index 905a9d39ef..2ee8ae5428 100644 --- a/ShellPkg/Include/Library/ShellLib.h +++ b/ShellPkg/Include/Library/ShellLib.h @@ -1266,4 +1266,61 @@ ShellFileExists( IN CONST CHAR16 *Name ); +/** + Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned + buffer. The returned buffer must be callee freed. + + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in,out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). + + @return The line of text from the file. + + @sa ShellFileHandleReadLine +**/ +CHAR16* +EFIAPI +ShellFileHandleReturnLine( + IN SHELL_FILE_HANDLE Handle, + IN OUT BOOLEAN *Ascii + ); + +/** + Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE. + + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in,out] Buffer The pointer to buffer to read into. + @param[in,out] Size The pointer to number of bytes in Buffer. + @param[in] Truncate If the buffer is large enough, this has no effect. + If the buffer is is too small and Truncate is TRUE, + the line will be truncated. + If the buffer is is too small and Truncate is FALSE, + then no read will occur. + + @param[in,out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). + + @retval EFI_SUCCESS The operation was successful. The line is stored in + Buffer. + @retval EFI_INVALID_PARAMETER Handle was NULL. + @retval EFI_INVALID_PARAMETER Size was NULL. + @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. + Size was updated to the minimum space required. +**/ +EFI_STATUS +EFIAPI +ShellFileHandleReadLine( + IN SHELL_FILE_HANDLE Handle, + IN OUT CHAR16 *Buffer, + IN OUT UINTN *Size, + IN BOOLEAN Truncate, + IN OUT BOOLEAN *Ascii + ); + #endif // __SHELL_LIB__ diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c index 28b7dda51e..cb452d44d5 100644 --- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c +++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c @@ -1282,157 +1282,6 @@ ShellFileHandleEof( return (RetVal); } -/** - Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned - buffer. The returned buffer must be callee freed. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @return The line of text from the file. - - @sa ShellFileHandleReadLine -**/ -CHAR16* -EFIAPI -ShellFileHandleReturnLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT BOOLEAN *Ascii - ) -{ - CHAR16 *RetVal; - UINTN Size; - EFI_STATUS Status; - - Size = 0; - RetVal = NULL; - - Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); - if (Status == EFI_BUFFER_TOO_SMALL) { - RetVal = AllocateZeroPool(Size); - Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); - } - ASSERT_EFI_ERROR(Status); - if (EFI_ERROR(Status) && (RetVal != NULL)) { - FreePool(RetVal); - RetVal = NULL; - } - return (RetVal); -} - -/** - Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE. - - If the position upon start is 0, then the Ascii Boolean will be set. This should be - maintained and not changed for all operations with the same file. - - @param[in] Handle SHELL_FILE_HANDLE to read from. - @param[in,out] Buffer The pointer to buffer to read into. - @param[in,out] Size The pointer to number of bytes in Buffer. - @param[in] Truncate If the buffer is large enough, this has no effect. - If the buffer is is too small and Truncate is TRUE, - the line will be truncated. - If the buffer is is too small and Truncate is FALSE, - then no read will occur. - - @param[in,out] Ascii Boolean value for indicating whether the file is - Ascii (TRUE) or UCS2 (FALSE). - - @retval EFI_SUCCESS The operation was successful. The line is stored in - Buffer. - @retval EFI_INVALID_PARAMETER Handle was NULL. - @retval EFI_INVALID_PARAMETER Size was NULL. - @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. - Size was updated to the minimum space required. - @sa ShellFileHandleRead -**/ -EFI_STATUS -EFIAPI -ShellFileHandleReadLine( - IN SHELL_FILE_HANDLE Handle, - IN OUT CHAR16 *Buffer, - IN OUT UINTN *Size, - IN BOOLEAN Truncate, - IN OUT BOOLEAN *Ascii - ) -{ - EFI_STATUS Status; - CHAR16 CharBuffer; - UINTN CharSize; - UINTN CountSoFar; - UINT64 OriginalFilePosition; - - - if (Handle == NULL - ||Size == NULL - ){ - return (EFI_INVALID_PARAMETER); - } - if (Buffer == NULL) { - ASSERT(*Size == 0); - } else { - *Buffer = CHAR_NULL; - } - gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition); - if (OriginalFilePosition == 0) { - CharSize = sizeof(CHAR16); - Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); - ASSERT_EFI_ERROR(Status); - if (CharBuffer == gUnicodeFileTag) { - *Ascii = FALSE; - } else { - *Ascii = TRUE; - gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); - } - } - - for (CountSoFar = 0;;CountSoFar++){ - CharBuffer = 0; - if (*Ascii) { - CharSize = sizeof(CHAR8); - } else { - CharSize = sizeof(CHAR16); - } - Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); - if ( EFI_ERROR(Status) - || CharSize == 0 - || (CharBuffer == L'\n' && !(*Ascii)) - || (CharBuffer == '\n' && *Ascii) - ){ - break; - } - // - // if we have space save it... - // - if ((CountSoFar+1)*sizeof(CHAR16) < *Size){ - ASSERT(Buffer != NULL); - ((CHAR16*)Buffer)[CountSoFar] = CharBuffer; - ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL; - } - } - - // - // if we ran out of space tell when... - // - if ((CountSoFar+1)*sizeof(CHAR16) > *Size){ - *Size = (CountSoFar+1)*sizeof(CHAR16); - if (!Truncate) { - gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); - } else { - DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine")); - } - return (EFI_BUFFER_TOO_SMALL); - } - while(Buffer[StrLen(Buffer)-1] == L'\r') { - Buffer[StrLen(Buffer)-1] = CHAR_NULL; - } - - return (Status); -} /** Frees any BUFFER_LIST defined type. diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index fa86d72ce4..3c1b983a60 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -3808,3 +3808,154 @@ ShellIsHexOrDecimalNumber ( } return (FALSE); } + +/** + Function to read a single line from a SHELL_FILE_HANDLE. The \n is not included in the returned + buffer. The returned buffer must be callee freed. + + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in,out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). + + @return The line of text from the file. + + @sa ShellFileHandleReadLine +**/ +CHAR16* +EFIAPI +ShellFileHandleReturnLine( + IN SHELL_FILE_HANDLE Handle, + IN OUT BOOLEAN *Ascii + ) +{ + CHAR16 *RetVal; + UINTN Size; + EFI_STATUS Status; + + Size = 0; + RetVal = NULL; + + Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); + if (Status == EFI_BUFFER_TOO_SMALL) { + RetVal = AllocateZeroPool(Size); + Status = ShellFileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii); + } + ASSERT_EFI_ERROR(Status); + if (EFI_ERROR(Status) && (RetVal != NULL)) { + FreePool(RetVal); + RetVal = NULL; + } + return (RetVal); +} + +/** + Function to read a single line (up to but not including the \n) from a SHELL_FILE_HANDLE. + + If the position upon start is 0, then the Ascii Boolean will be set. This should be + maintained and not changed for all operations with the same file. + + @param[in] Handle SHELL_FILE_HANDLE to read from. + @param[in,out] Buffer The pointer to buffer to read into. + @param[in,out] Size The pointer to number of bytes in Buffer. + @param[in] Truncate If the buffer is large enough, this has no effect. + If the buffer is is too small and Truncate is TRUE, + the line will be truncated. + If the buffer is is too small and Truncate is FALSE, + then no read will occur. + + @param[in,out] Ascii Boolean value for indicating whether the file is + Ascii (TRUE) or UCS2 (FALSE). + + @retval EFI_SUCCESS The operation was successful. The line is stored in + Buffer. + @retval EFI_INVALID_PARAMETER Handle was NULL. + @retval EFI_INVALID_PARAMETER Size was NULL. + @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line. + Size was updated to the minimum space required. +**/ +EFI_STATUS +EFIAPI +ShellFileHandleReadLine( + IN SHELL_FILE_HANDLE Handle, + IN OUT CHAR16 *Buffer, + IN OUT UINTN *Size, + IN BOOLEAN Truncate, + IN OUT BOOLEAN *Ascii + ) +{ + EFI_STATUS Status; + CHAR16 CharBuffer; + UINTN CharSize; + UINTN CountSoFar; + UINT64 OriginalFilePosition; + + + if (Handle == NULL + ||Size == NULL + ){ + return (EFI_INVALID_PARAMETER); + } + if (Buffer == NULL) { + ASSERT(*Size == 0); + } else { + *Buffer = CHAR_NULL; + } + gEfiShellProtocol->GetFilePosition(Handle, &OriginalFilePosition); + if (OriginalFilePosition == 0) { + CharSize = sizeof(CHAR16); + Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); + ASSERT_EFI_ERROR(Status); + if (CharBuffer == gUnicodeFileTag) { + *Ascii = FALSE; + } else { + *Ascii = TRUE; + gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); + } + } + + for (CountSoFar = 0;;CountSoFar++){ + CharBuffer = 0; + if (*Ascii) { + CharSize = sizeof(CHAR8); + } else { + CharSize = sizeof(CHAR16); + } + Status = gEfiShellProtocol->ReadFile(Handle, &CharSize, &CharBuffer); + if ( EFI_ERROR(Status) + || CharSize == 0 + || (CharBuffer == L'\n' && !(*Ascii)) + || (CharBuffer == '\n' && *Ascii) + ){ + break; + } + // + // if we have space save it... + // + if ((CountSoFar+1)*sizeof(CHAR16) < *Size){ + ASSERT(Buffer != NULL); + ((CHAR16*)Buffer)[CountSoFar] = CharBuffer; + ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL; + } + } + + // + // if we ran out of space tell when... + // + if ((CountSoFar+1)*sizeof(CHAR16) > *Size){ + *Size = (CountSoFar+1)*sizeof(CHAR16); + if (!Truncate) { + gEfiShellProtocol->SetFilePosition(Handle, OriginalFilePosition); + } else { + DEBUG((DEBUG_WARN, "The line was truncated in ShellFileHandleReadLine")); + } + return (EFI_BUFFER_TOO_SMALL); + } + while(Buffer[StrLen(Buffer)-1] == L'\r') { + Buffer[StrLen(Buffer)-1] = CHAR_NULL; + } + + return (Status); +} -- 2.39.2