mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor: kill custom modules scripts en destroy
This commit is contained in:
@ -11,12 +11,7 @@ struct res {
|
||||
std::string out;
|
||||
};
|
||||
|
||||
inline struct res exec(const std::string cmd) {
|
||||
FILE* fp(popen(cmd.c_str(), "r"));
|
||||
if (!fp) {
|
||||
return {-1, ""};
|
||||
}
|
||||
|
||||
inline std::string read(FILE* fp) {
|
||||
std::array<char, 128> buffer = {0};
|
||||
std::string output;
|
||||
while (feof(fp) == 0) {
|
||||
@ -29,24 +24,75 @@ inline struct res exec(const std::string cmd) {
|
||||
if (!output.empty() && output[output.length() - 1] == '\n') {
|
||||
output.erase(output.length() - 1);
|
||||
}
|
||||
int exit_code = WEXITSTATUS(pclose(fp));
|
||||
return {exit_code, output};
|
||||
return output;
|
||||
}
|
||||
|
||||
inline bool forkExec(std::string cmd) {
|
||||
if (cmd == "") return true;
|
||||
inline int close(FILE* fp, pid_t pid) {
|
||||
int stat;
|
||||
|
||||
fclose(fp);
|
||||
while (waitpid(pid, &stat, 0) == -1) {
|
||||
if (errno != EINTR) {
|
||||
stat = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return stat;
|
||||
}
|
||||
|
||||
inline FILE* open(const std::string cmd, int& pid) {
|
||||
if (cmd == "") return nullptr;
|
||||
int fd[2];
|
||||
pipe(fd);
|
||||
|
||||
pid_t child_pid = fork();
|
||||
|
||||
if (child_pid < 0) {
|
||||
printf("Unable to exec cmd %s, error %s", cmd.c_str(), strerror(errno));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!child_pid) {
|
||||
::close(fd[0]);
|
||||
dup2(fd[1], 1);
|
||||
setpgid(child_pid, child_pid);
|
||||
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
||||
exit(0);
|
||||
} else {
|
||||
::close(fd[1]);
|
||||
}
|
||||
pid = child_pid;
|
||||
return fdopen(fd[0], "r");
|
||||
}
|
||||
|
||||
inline struct res exec(std::string cmd) {
|
||||
int pid;
|
||||
auto fp = command::open(cmd, pid);
|
||||
if (!fp) return {-1, ""};
|
||||
auto output = command::read(fp);
|
||||
auto stat = command::close(fp, pid);
|
||||
return {WEXITSTATUS(stat), output};
|
||||
}
|
||||
|
||||
inline int32_t forkExec(std::string cmd) {
|
||||
if (cmd == "") return -1;
|
||||
|
||||
int32_t pid = fork();
|
||||
|
||||
if (pid < 0) {
|
||||
printf("Unable to exec cmd %s, error %s", cmd.c_str(), strerror(errno));
|
||||
return false;
|
||||
return pid;
|
||||
}
|
||||
|
||||
// Child executes the command
|
||||
if (!pid) execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
||||
if (!pid) {
|
||||
setpgid(pid, pid);
|
||||
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
return true;
|
||||
return pid;
|
||||
}
|
||||
|
||||
} // namespace waybar::util::command
|
||||
|
@ -7,14 +7,13 @@ namespace waybar::util {
|
||||
struct JsonParser {
|
||||
JsonParser() : reader_(builder_.newCharReader()) {}
|
||||
|
||||
const Json::Value parse(const std::string& data, std::size_t size = 0) const {
|
||||
const Json::Value parse(const std::string& data) const {
|
||||
Json::Value root(Json::objectValue);
|
||||
if (data.empty()) {
|
||||
return root;
|
||||
}
|
||||
std::string err;
|
||||
auto data_size = size > 0 ? size : data.size();
|
||||
bool res = reader_->parse(data.c_str(), data.c_str() + data_size, &root, &err);
|
||||
bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||
if (!res) throw std::runtime_error(err);
|
||||
return root;
|
||||
}
|
||||
|
Reference in New Issue
Block a user