cppshell/include/cppshell/shell.hpp
2022-08-10 09:17:11 +03:00

51 lines
932 B
C++

#pragma once
#include <string>
#include <fstream>
#if !defined(__linux__)
#error Only linux supported
#endif
namespace cppshell {
class Shell {
public:
Shell(const std::string& shell = "bash");
void Execute(const std::string& command) {
#if defined(SIGPIPE_ALWAYS_IGNORE)
ExecuteImpl(command, false);
#else
ExecuteImpl(command, true);
#endif
}
int GetExitCodeLastCommand();
~Shell() {
#if defined(SIGPIPE_ALWAYS_IGNORE)
Destroy(false);
#else
Destroy(true);
#endif
}
private:
void ExecuteImpl(const std::string& command, bool need_ignore_sigpipe);
void Destroy(bool need_ignore_sigpipe);
void MakeUniqueDirectory();
void MakeFifoFile();
private:
std::string unique_directory_;
std::string command_transmission_file_;
pid_t pid_shell_process_;
int fd_exit_codes_transmission_;
std::ofstream command_transmission_;
FILE* exit_codes_receiving_;
};
} // namespace cppshell