2024-04-04 23:17:25 +00:00
|
|
|
#pragma once
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstring>
|
2024-04-04 23:42:49 +00:00
|
|
|
#ifdef __linux__
|
2024-04-04 23:17:25 +00:00
|
|
|
#include <sys/wait.h>
|
2024-04-04 23:39:09 +00:00
|
|
|
#endif
|
2024-04-04 23:17:25 +00:00
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class Shell {
|
|
|
|
private:
|
|
|
|
int to_shell[2];
|
|
|
|
int from_shell[2];
|
|
|
|
pid_t shell_pid;
|
2024-04-08 07:44:54 +00:00
|
|
|
bool success;
|
2024-04-04 23:17:25 +00:00
|
|
|
|
2024-04-04 23:42:49 +00:00
|
|
|
#ifdef __linux__
|
2024-04-04 23:17:25 +00:00
|
|
|
void setNonBlocking(int fd) {
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
2024-04-04 23:39:09 +00:00
|
|
|
#endif
|
2024-04-04 23:17:25 +00:00
|
|
|
|
2024-04-08 23:48:45 +00:00
|
|
|
void writeCommand(const std::string& command);
|
2024-04-04 23:17:25 +00:00
|
|
|
std::string readOutput();
|
|
|
|
|
|
|
|
public:
|
|
|
|
Shell();
|
|
|
|
~Shell();
|
|
|
|
std::string exec(std::string cmd);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
extern std::unique_ptr<Shell> shell;
|