Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Commands to get and set environment variables #265

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Core/NWNXCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,35 @@ void NWNXCore::InitialSetupPlugins()
}
}

void NWNXCore::InitialSetupCommands()
{
m_services->m_commands->RegisterCommand("set", [](std::string& s)
{
size_t equals = s.find('=');
if (equals != std::string::npos)
{
std::string varname = s.substr(0, equals);
std::string value = s.substr(equals+1, std::string::npos);
if (setenv(varname.c_str(), value.c_str(), 1) == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is problematic. setenv is not thread safe. I think a safer design would be to extend the Config service so that you can change config settings that way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thread safety is not an issue since this all runs on the main thread. But you make a valid point that they are better suited for the Config service. I'm thinking map<string varname, string override_value> that you can set through the command line, and some way for plugins to register a callback when a config option changes.
The big advantage there would be that changes would not persist across NWNX reboots.
Adding a WIP tag here.

std::printf("Environment variable '%s' set to '%s'\n", varname.c_str(), value.c_str());
else
std::printf("Unable to set environment variable '%s' to '%s'\n", varname.c_str(), value.c_str());
}
else
{
std::printf("Usage: set <varname>=<value>\n");
}
});
m_services->m_commands->RegisterCommand("get", [](std::string& s)
{
char *value = std::getenv(s.c_str());
if (value)
std::printf("%s=%s\n", s.c_str(), value);
else
std::printf("Variable %s does not exist\n", s.c_str());
});
}

void NWNXCore::UnloadPlugins()
{
using PairType = std::pair<Services::Plugins::RegistrationToken, std::unique_ptr<Services::ProxyServiceList>>;
Expand Down Expand Up @@ -400,6 +429,7 @@ void NWNXCore::CreateServerHandler(API::CAppManager* app)
{
g_core->InitialSetupHooks();
g_core->InitialSetupPlugins();
g_core->InitialSetupCommands();
}
catch (const std::runtime_error& ex)
{
Expand Down
1 change: 1 addition & 0 deletions Core/NWNXCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class NWNXCore
void InitialSetupHooks();
void InitialVersionCheck();
void InitialSetupPlugins();
void InitialSetupCommands();

void UnloadPlugins();
void UnloadPlugin(std::pair<NWNXLib::Services::Plugins::RegistrationToken,
Expand Down