Split out libwinpty/AgentLocation.cc from libwinpty/winpty.cc

This commit is contained in:
Ryan Prichard 2016-05-24 16:58:31 -05:00
parent 6d9dda19af
commit 215885fc44
5 changed files with 102 additions and 46 deletions

69
src/libwinpty/AgentLocation.cc Executable file
View File

@ -0,0 +1,69 @@
// Copyright (c) 2011-2016 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "AgentLocation.h"
#include <windows.h>
#include <string>
#include "../shared/WinptyAssert.h"
#define AGENT_EXE L"winpty-agent.exe"
static HMODULE getCurrentModule() {
HMODULE module;
if (!GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(getCurrentModule),
&module)) {
ASSERT(false && "GetModuleHandleEx failed");
}
return module;
}
static std::wstring getModuleFileName(HMODULE module) {
const int bufsize = 4096;
wchar_t path[bufsize];
int size = GetModuleFileNameW(module, path, bufsize);
ASSERT(size != 0 && size != bufsize);
return std::wstring(path);
}
static std::wstring dirname(const std::wstring &path) {
std::wstring::size_type pos = path.find_last_of(L"\\/");
if (pos == std::wstring::npos) {
return L"";
} else {
return path.substr(0, pos);
}
}
static bool pathExists(const std::wstring &path) {
return GetFileAttributesW(path.c_str()) != 0xFFFFFFFF;
}
std::wstring findAgentProgram() {
std::wstring progDir = dirname(getModuleFileName(getCurrentModule()));
std::wstring ret = progDir + (L"\\" AGENT_EXE);
ASSERT(pathExists(ret));
return ret;
}

28
src/libwinpty/AgentLocation.h Executable file
View File

@ -0,0 +1,28 @@
// Copyright (c) 2011-2016 Ryan Prichard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#ifndef LIBWINPTY_AGENT_LOCATION_H
#define LIBWINPTY_AGENT_LOCATION_H
#include <string>
std::wstring findAgentProgram();
#endif // LIBWINPTY_AGENT_LOCATION_H

View File

@ -23,6 +23,7 @@ ALL_TARGETS += build/winpty.dll
$(eval $(call def_mingw_target,libwinpty,-DCOMPILING_WINPTY_DLL))
LIBWINPTY_OBJECTS = \
build/libwinpty/libwinpty/AgentLocation.o \
build/libwinpty/libwinpty/winpty.o \
build/libwinpty/shared/Buffer.o \
build/libwinpty/shared/DebugClient.o \

View File

@ -43,9 +43,9 @@
#include "../shared/WinptyException.h"
#include "../shared/WinptyVersion.h"
// TODO: Error handling, handle out-of-memory.
#include "AgentLocation.h"
#define AGENT_EXE L"winpty-agent.exe"
// TODO: Error handling, handle out-of-memory.
struct winpty_s {
winpty_s();
@ -57,50 +57,6 @@ winpty_s::winpty_s() : controlPipe(NULL), dataPipe(NULL)
{
}
static HMODULE getCurrentModule()
{
HMODULE module;
if (!GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(getCurrentModule),
&module)) {
ASSERT(false && "GetModuleHandleEx failed");
}
return module;
}
static std::wstring getModuleFileName(HMODULE module)
{
const int bufsize = 4096;
wchar_t path[bufsize];
int size = GetModuleFileNameW(module, path, bufsize);
ASSERT(size != 0 && size != bufsize);
return std::wstring(path);
}
static std::wstring dirname(const std::wstring &path)
{
std::wstring::size_type pos = path.find_last_of(L"\\/");
if (pos == std::wstring::npos)
return L"";
else
return path.substr(0, pos);
}
static bool pathExists(const std::wstring &path)
{
return GetFileAttributesW(path.c_str()) != 0xFFFFFFFF;
}
static std::wstring findAgentProgram()
{
std::wstring progDir = dirname(getModuleFileName(getCurrentModule()));
std::wstring ret = progDir + (L"\\" AGENT_EXE);
ASSERT(pathExists(ret));
return ret;
}
// Call ConnectNamedPipe and block, even for an overlapped pipe. If the
// pipe is overlapped, create a temporary event for use connecting.
static bool connectNamedPipe(HANDLE handle, bool overlapped)

View File

@ -122,6 +122,8 @@
},
'sources' : [
'include/winpty.h',
'libwinpty/AgentLocation.cc',
'libwinpty/AgentLocation.h',
'libwinpty/winpty.cc',
'shared/AgentMsg.h',
'shared/Buffer.h',