close out after $(command) finishes

This commit is contained in:
Venelin Dechkov
2024-08-22 14:42:49 +03:00
parent 0f58b9b11c
commit 9e09250902
2 changed files with 34 additions and 34 deletions
+33 -33
View File
@@ -125,11 +125,6 @@ inline std::string $(const PendingCmd& cmd)
{ {
Proc p = const_cast<PendingCmd&>(cmd).detachRedirOut(); Proc p = const_cast<PendingCmd&>(cmd).detachRedirOut();
// check output size (not portable?)
// int pipe_size;
// int rc = ioctl(p.out, FIONREAD, &pipe_size); assert(rc==0);
// ioctl(p.out, FIONREAD, &pipe_size);
// write to the string directly, todo: find a better way // write to the string directly, todo: find a better way
std::string output; std::string output;
@@ -145,7 +140,12 @@ inline std::string $(const PendingCmd& cmd)
} }
while(read_count > 0); while(read_count > 0);
// p finished? // p finished?
output.erase(i, output.size() - i); // erase the extra elements
// erase the extra elements
output.erase(i, output.size() - i);
close(p.out);
return output; return output;
} }
@@ -154,36 +154,36 @@ inline void exec(const Cmd& cmd)
exec_or_die(cmd.argv.data()); exec_or_die(cmd.argv.data());
} }
inline PendingCmd operator,(const PendingCmd& cfirst, const Cmd& second) inline PendingCmd operator,(const PendingCmd& cleft, const Cmd& right)
{ {
auto& first = const_cast<PendingCmd&>(cfirst); auto& left = const_cast<PendingCmd&>(cleft);
first(); left();
return PendingCmd(second); return PendingCmd(right);
} }
inline PendingCmd operator,(DeadProc, const Cmd& second) inline PendingCmd operator,(DeadProc, const Cmd& right)
{ {
return PendingCmd(second); return PendingCmd(right);
} }
inline PendingCmd operator|(const PendingCmd& cfirst, const Cmd& second) inline PendingCmd operator|(const PendingCmd& cleft, const Cmd& right)
{ {
auto& first = const_cast<PendingCmd&>(cfirst); auto& left = const_cast<PendingCmd&>(cleft);
fd_t firstOut = first.detachRedirOut().out; fd_t leftOut = left.detachRedirOut().out;
return PendingCmd(second, firstOut); return PendingCmd(right, leftOut);
} }
inline DeadProc operator&&(const PendingCmd& cfirst, const Cmd& csecond) inline DeadProc operator&&(const PendingCmd& cleft, const Cmd& cright)
{ {
auto& first = const_cast<PendingCmd&>(cfirst); auto& left = const_cast<PendingCmd&>(cleft);
auto& second = const_cast<Cmd&>(csecond); auto& right = const_cast<Cmd&>(cright);
DeadProc firstProc = first(); DeadProc leftProc = left();
if(firstProc) if(leftProc)
return second(); return right();
return firstProc; return leftProc;
} }
inline DeadProc operator&&(DeadProc p, const Cmd& ccmd) inline DeadProc operator&&(DeadProc p, const Cmd& ccmd)
@@ -195,16 +195,16 @@ inline DeadProc operator&&(DeadProc p, const Cmd& ccmd)
return p; return p;
} }
inline DeadProc operator||(const PendingCmd& cfirst, const Cmd& csecond) inline DeadProc operator||(const PendingCmd& cleft, const Cmd& cright)
{ {
auto& first = const_cast<PendingCmd&>(cfirst); auto& left = const_cast<PendingCmd&>(cleft);
auto& second = const_cast<Cmd&>(csecond); auto& right = const_cast<Cmd&>(cright);
DeadProc firstProc = first(); DeadProc leftProc = left();
if(!firstProc) if(!leftProc)
return second(); return right();
return firstProc; return leftProc;
} }
inline DeadProc operator||(DeadProc p, const Cmd& ccmd) inline DeadProc operator||(DeadProc p, const Cmd& ccmd)
@@ -278,8 +278,8 @@ inline PendingCmd& operator<(const PendingCmd& ccmd, fd_t fd)
return cmd; return cmd;
} }
inline PendingCmd operator&(const PendingCmd& cfirst, const Cmd& second) inline PendingCmd operator&(const PendingCmd& cleft, const Cmd& right)
{ {
const_cast<PendingCmd&>(cfirst).detach(); const_cast<PendingCmd&>(cleft).detach();
return PendingCmd(second); return PendingCmd(right);
} }
+1 -1
View File
@@ -1,5 +1,6 @@
man man
tips (sigaction SIGCHILD, SIG_IGN, SIG_DFL) tips (sigaction SIGCHILD, SIG_IGN, SIG_DFL)
make sure file descriptors are closed when no longer used
cppipe compile options from tft cppipe compile options from tft
respect CXXFLAGS LDFLAGS respect CXXFLAGS LDFLAGS
@@ -10,7 +11,6 @@ precompiled header cppipe.h
-Wno-unused-result in install.sh -Wno-unused-result in install.sh
file operations, lack of uniformity (C++ vs POSIX) file operations, lack of uniformity (C++ vs POSIX)
Check all return codes and report errors Check all return codes and report errors
close files on destruction
shaded obj shaded obj
uninstall uninstall
throw when a command couldnt be ran throw when a command couldnt be ran