This commit is contained in:
Timofey 2022-08-10 09:17:11 +03:00
commit 5659d5fdaf
5 changed files with 211 additions and 0 deletions

View file

@ -0,0 +1,51 @@
#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

View file

@ -0,0 +1,9 @@
#pragma once
struct SigpipeState {
bool sigpipe_unblock = false;
};
bool IgnoreSigpipe();
void UnignoreSigpipe();