From 9e092509026d27f6b8fe7c26aa0164dd54fe8d71 Mon Sep 17 00:00:00 2001 From: Venelin Dechkov Date: Thu, 22 Aug 2024 14:42:08 +0300 Subject: [PATCH] close out after $(command) finishes --- src/commands.inl | 66 ++++++++++++++++++++++++------------------------ todo.txt | 2 +- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/commands.inl b/src/commands.inl index 7cb1dd5..05161bb 100644 --- a/src/commands.inl +++ b/src/commands.inl @@ -125,11 +125,6 @@ inline std::string $(const PendingCmd& cmd) { Proc p = const_cast(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 std::string output; @@ -145,7 +140,12 @@ inline std::string $(const PendingCmd& cmd) } while(read_count > 0); // 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; } @@ -154,36 +154,36 @@ inline void exec(const Cmd& cmd) 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(cfirst); - first(); - return PendingCmd(second); + auto& left = const_cast(cleft); + left(); + 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(cfirst); - fd_t firstOut = first.detachRedirOut().out; - return PendingCmd(second, firstOut); + auto& left = const_cast(cleft); + fd_t leftOut = left.detachRedirOut().out; + 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(cfirst); - auto& second = const_cast(csecond); - DeadProc firstProc = first(); + auto& left = const_cast(cleft); + auto& right = const_cast(cright); + DeadProc leftProc = left(); - if(firstProc) - return second(); + if(leftProc) + return right(); - return firstProc; + return leftProc; } inline DeadProc operator&&(DeadProc p, const Cmd& ccmd) @@ -195,16 +195,16 @@ inline DeadProc operator&&(DeadProc p, const Cmd& ccmd) return p; } -inline DeadProc operator||(const PendingCmd& cfirst, const Cmd& csecond) +inline DeadProc operator||(const PendingCmd& cleft, const Cmd& cright) { - auto& first = const_cast(cfirst); - auto& second = const_cast(csecond); - DeadProc firstProc = first(); + auto& left = const_cast(cleft); + auto& right = const_cast(cright); + DeadProc leftProc = left(); - if(!firstProc) - return second(); + if(!leftProc) + return right(); - return firstProc; + return leftProc; } inline DeadProc operator||(DeadProc p, const Cmd& ccmd) @@ -278,8 +278,8 @@ inline PendingCmd& operator<(const PendingCmd& ccmd, fd_t fd) return cmd; } -inline PendingCmd operator&(const PendingCmd& cfirst, const Cmd& second) +inline PendingCmd operator&(const PendingCmd& cleft, const Cmd& right) { - const_cast(cfirst).detach(); - return PendingCmd(second); + const_cast(cleft).detach(); + return PendingCmd(right); } diff --git a/todo.txt b/todo.txt index c3d9278..6d9f298 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ man tips (sigaction SIGCHILD, SIG_IGN, SIG_DFL) +make sure file descriptors are closed when no longer used cppipe compile options from tft respect CXXFLAGS LDFLAGS @@ -10,7 +11,6 @@ precompiled header cppipe.h -Wno-unused-result in install.sh file operations, lack of uniformity (C++ vs POSIX) Check all return codes and report errors -close files on destruction shaded obj uninstall throw when a command couldnt be ran