Files
PowerShell/host/main.cpp
T

110 lines
3.1 KiB
C++
Raw Normal View History

#include <unistd.h>
#include <string>
#include <iostream>
2015-10-25 12:03:49 -07:00
#include "coreclrutil.h"
namespace Cmdline
{
2015-10-21 15:22:57 -07:00
void printHelp()
{
2015-11-06 20:01:29 -08:00
std::cerr << "PowerShell on Linux native host" << std::endl
<< "Usage: powershell [assembly] [...]" << std::endl
2015-10-21 15:22:57 -07:00
<< std::endl
<< "What it does:" << std::endl
2015-12-07 15:59:53 -08:00
<< "- the host assumes that PSL has been published to $CORE_ROOT," << std::endl
<< "- the host will launch $CORE_ROOT/Microsoft.PowerShell.Linux.Host.dll" << std::endl
<< " if not given an explicit assembly" << std::endl
2015-10-21 15:22:57 -07:00
<< "- all additional parameters at the end of the command line are forwarded" << std::endl
<< " to the Main function in the assembly" << std::endl
<< "- the host will execute the Main function in the specified assembly" << std::endl
<< std::endl
<< "Example:" << std::endl
2015-12-07 15:59:53 -08:00
<< "CORE_ROOT=/test/coreclr ./powershell get-process" << std::endl;
}
2015-10-21 15:22:57 -07:00
struct Args
{
2015-10-21 15:22:57 -07:00
Args() :
argc(0),
argv(nullptr),
2015-11-06 19:59:04 -08:00
assembly()
2015-10-21 15:22:57 -07:00
{
}
2015-10-21 15:22:57 -07:00
int argc;
char** argv;
2015-11-06 19:59:04 -08:00
std::string assembly;
2015-10-21 15:22:57 -07:00
};
2015-11-06 19:59:04 -08:00
// checks if string ends with ".exe"
bool isExe(std::string arg)
{
2015-11-06 19:59:04 -08:00
std::size_t dot = arg.find_last_of(".");
return dot == std::string::npos ? false : arg.substr(dot) == ".exe";
}
2015-11-06 19:59:04 -08:00
// simple extraction of assembly.exe so we can run other hosts
void parseCmdline(int argc, char** argv, Args& args)
{
// index of arguments to forward (skip zeroth)
int i = 1;
// if we have any arguments
if (argc > 1)
{
// check if the first is an assembly.exe
const std::string arg = argv[i];
if (isExe(arg))
{
args.assembly.assign(arg);
++i; // don't forward the first argument
}
}
// forward arguments
args.argc = argc - i;
args.argv = &argv[i];
}
}
int main(int argc, char** argv)
{
Cmdline::Args args;
2015-11-06 19:59:04 -08:00
Cmdline::parseCmdline(argc, argv, args);
void* hostHandle;
unsigned int domainId;
2015-10-22 13:31:22 -07:00
int status = startCoreCLR(
"psl_cmdline_host",
2015-10-21 15:22:57 -07:00
&hostHandle,
&domainId);
2015-10-23 00:01:48 -07:00
if (!SUCCEEDED(status))
{
std::cerr << "could not start CoreCLR" << std::endl;
return -1;
}
2015-11-06 19:59:04 -08:00
// default to powershell.exe if no host specified
if (args.assembly.empty())
{
2015-12-07 15:59:53 -08:00
args.assembly.append(coreRoot);
2015-11-06 19:59:04 -08:00
args.assembly.append("/");
2015-12-07 15:59:53 -08:00
args.assembly.append("Microsoft.PowerShell.Linux.Host.dll");
2015-11-06 19:59:04 -08:00
}
2015-10-22 18:17:27 -07:00
// call into Main of assembly
2015-08-21 16:17:16 -07:00
unsigned int exitCode;
2015-10-22 18:17:27 -07:00
executeAssembly(
hostHandle, domainId, args.argc,
(const char**)args.argv,
2015-11-06 19:59:04 -08:00
args.assembly.c_str(),
2015-10-22 18:17:27 -07:00
&exitCode);
2015-10-22 13:31:22 -07:00
status = stopCoreCLR(hostHandle, domainId);
2015-10-23 00:01:48 -07:00
if (!SUCCEEDED(status))
{
std::cerr << "could not stop CoreCLR" << std::endl;
return -1;
}
return exitCode;
}