Skip to content

Commit 13adbf7

Browse files
committed
remove unneeded environment option from cpp-subprocess
1 parent 2088777 commit 13adbf7

File tree

1 file changed

+2
-145
lines changed

1 file changed

+2
-145
lines changed

src/util/subprocess.hpp

Lines changed: 2 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ class OSError: public std::runtime_error
154154
{}
155155
};
156156

157-
//--------------------------------------------------------------------
158-
159-
//Environment Variable types
160-
#ifndef _MSC_VER
161-
using env_string_t = std::string;
162-
using env_char_t = char;
163-
#else
164-
using env_string_t = std::wstring;
165-
using env_char_t = wchar_t;
166-
#endif
167-
using env_map_t = std::map<env_string_t, env_string_t>;
168-
using env_vector_t = std::vector<env_char_t>;
169-
170157
//--------------------------------------------------------------------
171158
namespace util
172159
{
@@ -305,100 +292,6 @@ namespace util
305292
if (!SetHandleInformation(*child_handle, HANDLE_FLAG_INHERIT, 0))
306293
throw OSError("SetHandleInformation", 0);
307294
}
308-
309-
// env_map_t MapFromWindowsEnvironment()
310-
// * Imports current Environment in a C-string table
311-
// * Parses the strings by splitting on the first "=" per line
312-
// * Creates a map of the variables
313-
// * Returns the map
314-
inline env_map_t MapFromWindowsEnvironment(){
315-
wchar_t *variable_strings_ptr;
316-
wchar_t *environment_strings_ptr;
317-
std::wstring delimiter(L"=");
318-
int del_len = delimiter.length();
319-
env_map_t mapped_environment;
320-
321-
// Get a pointer to the environment block.
322-
environment_strings_ptr = GetEnvironmentStringsW();
323-
// If the returned pointer is NULL, exit.
324-
if (environment_strings_ptr == NULL)
325-
{
326-
throw OSError("GetEnvironmentStringsW", 0);
327-
}
328-
329-
// Variable strings are separated by NULL byte, and the block is
330-
// terminated by a NULL byte.
331-
332-
variable_strings_ptr = (wchar_t *) environment_strings_ptr;
333-
334-
//Since the environment map ends with a null, we can loop until we find it.
335-
while (*variable_strings_ptr)
336-
{
337-
// Create a string from Variable String
338-
env_string_t current_line(variable_strings_ptr);
339-
// Find the first "equals" sign.
340-
auto pos = current_line.find(delimiter);
341-
// Assuming it's not missing ...
342-
if(pos!=std::wstring::npos){
343-
// ... parse the key and value.
344-
env_string_t key = current_line.substr(0, pos);
345-
env_string_t value = current_line.substr(pos + del_len);
346-
// Map the entry.
347-
mapped_environment[key] = value;
348-
}
349-
// Jump to next line in the environment map.
350-
variable_strings_ptr += std::wcslen(variable_strings_ptr) + 1;
351-
}
352-
// We're done with the old environment map buffer.
353-
FreeEnvironmentStringsW(environment_strings_ptr);
354-
355-
// Return the map.
356-
return mapped_environment;
357-
}
358-
359-
// env_vector_t WindowsEnvironmentVectorFromMap(const env_map_t &source_map)
360-
// * Creates a vector buffer for the new environment string table
361-
// * Copies in the mapped variables
362-
// * Returns the vector
363-
inline env_vector_t WindowsEnvironmentVectorFromMap(const env_map_t &source_map)
364-
{
365-
// Make a new environment map buffer.
366-
env_vector_t environment_map_buffer;
367-
// Give it some space.
368-
environment_map_buffer.reserve(4096);
369-
370-
// And fill'er up.
371-
for(auto kv: source_map){
372-
// Create the line
373-
env_string_t current_line(kv.first); current_line += L"="; current_line += kv.second;
374-
// Add the line to the buffer.
375-
std::copy(current_line.begin(), current_line.end(), std::back_inserter(environment_map_buffer));
376-
// Append a null
377-
environment_map_buffer.push_back(0);
378-
}
379-
// Append one last null because of how Windows does it's environment maps.
380-
environment_map_buffer.push_back(0);
381-
382-
return environment_map_buffer;
383-
}
384-
385-
// env_vector_t CreateUpdatedWindowsEnvironmentVector(const env_map_t &changes_map)
386-
// * Merges host environment with new mapped variables
387-
// * Creates and returns string vector based on map
388-
inline env_vector_t CreateUpdatedWindowsEnvironmentVector(const env_map_t &changes_map){
389-
// Import the environment map
390-
env_map_t environment_map = MapFromWindowsEnvironment();
391-
// Merge in the changes with overwrite
392-
for(auto& it: changes_map)
393-
{
394-
environment_map[it.first] = it.second;
395-
}
396-
// Create a Windows-usable Environment Map Buffer
397-
env_vector_t environment_map_strings_vector = WindowsEnvironmentVectorFromMap(environment_map);
398-
399-
return environment_map_strings_vector;
400-
}
401-
402295
#endif
403296

404297
/*!
@@ -656,22 +549,6 @@ struct executable: string_arg
656549
executable(T&& arg): string_arg(std::forward<T>(arg)) {}
657550
};
658551

659-
/*!
660-
* Option to specify environment variables required by
661-
* the spawned process.
662-
*
663-
* Eg: environment{{ {"K1", "V1"}, {"K2", "V2"},... }}
664-
*/
665-
struct environment
666-
{
667-
environment(env_map_t&& env):
668-
env_(std::move(env)) {}
669-
explicit environment(const env_map_t& env):
670-
env_(env) {}
671-
env_map_t env_;
672-
};
673-
674-
675552
/*!
676553
* Used for redirecting input/output/error
677554
*/
@@ -887,7 +764,6 @@ struct ArgumentDeducer
887764
ArgumentDeducer(Popen* p): popen_(p) {}
888765

889766
void set_option(executable&& exe);
890-
void set_option(environment&& env);
891767
void set_option(input&& inp);
892768
void set_option(output&& out);
893769
void set_option(error&& err);
@@ -1214,7 +1090,6 @@ class Popen
12141090
#endif
12151091

12161092
std::string exe_name_;
1217-
env_map_t env_;
12181093

12191094
// Command in string format
12201095
std::string args_;
@@ -1335,13 +1210,6 @@ inline void Popen::kill(int sig_num)
13351210
inline void Popen::execute_process() noexcept(false)
13361211
{
13371212
#ifdef __USING_WINDOWS__
1338-
void* environment_string_table_ptr = nullptr;
1339-
env_vector_t environment_string_vector;
1340-
if(this->env_.size()){
1341-
environment_string_vector = util::CreateUpdatedWindowsEnvironmentVector(env_);
1342-
environment_string_table_ptr = (void*)environment_string_vector.data();
1343-
}
1344-
13451213
if (exe_name_.length()) {
13461214
this->vargs_.insert(this->vargs_.begin(), this->exe_name_);
13471215
this->populate_c_argv();
@@ -1388,7 +1256,7 @@ inline void Popen::execute_process() noexcept(false)
13881256
NULL, // primary thread security attributes
13891257
TRUE, // handles are inherited
13901258
creation_flags, // creation flags
1391-
environment_string_table_ptr, // use provided environment
1259+
NULL, // use parent's environment
13921260
NULL, // use parent's current directory
13931261
&siStartInfo, // STARTUPINFOW pointer
13941262
&piProcInfo); // receives PROCESS_INFORMATION
@@ -1493,10 +1361,6 @@ namespace detail {
14931361
popen_->exe_name_ = std::move(exe.arg_value);
14941362
}
14951363

1496-
inline void ArgumentDeducer::set_option(environment&& env) {
1497-
popen_->env_ = std::move(env.env_);
1498-
}
1499-
15001364
inline void ArgumentDeducer::set_option(input&& inp) {
15011365
if (inp.rd_ch_ != -1) popen_->stream_.read_from_parent_ = inp.rd_ch_;
15021366
if (inp.wr_ch_ != -1) popen_->stream_.write_to_child_ = inp.wr_ch_;
@@ -1566,14 +1430,7 @@ namespace detail {
15661430
close(stream.err_write_);
15671431

15681432
// Replace the current image with the executable
1569-
if (parent_->env_.size()) {
1570-
for (auto& kv : parent_->env_) {
1571-
setenv(kv.first.c_str(), kv.second.c_str(), 1);
1572-
}
1573-
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
1574-
} else {
1575-
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
1576-
}
1433+
sys_ret = execvp(parent_->exe_name_.c_str(), parent_->cargv_.data());
15771434

15781435
if (sys_ret == -1) throw OSError("execve failed", errno);
15791436

0 commit comments

Comments
 (0)