about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-20T13·12+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-20T13·12+0200
commit392430b2c4ceb2e476abe2b3acc928581b2a1445 (patch)
tree30db8572f28c8def55f3b4dcce988a6ed4c778e9 /src
parent894fa5e42dd952caa702794964a13845ccf6f29a (diff)
nix-store -l: Automatically pipe output into $PAGER
Diffstat (limited to 'src')
-rw-r--r--src/libmain/shared.cc40
-rw-r--r--src/libmain/shared.hh12
-rw-r--r--src/libutil/util.cc4
-rw-r--r--src/nix-store/nix-store.cc2
4 files changed, 56 insertions, 2 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index 9ac9d2773852..ba75847fdb63 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -285,4 +285,44 @@ int handleExceptions(const string & programName, std::function<void()> fun)
 }
 
 
+RunPager::RunPager()
+{
+    string pager = getEnv("PAGER");
+    if (!isatty(STDOUT_FILENO) || pager.empty()) return;
+
+    /* Ignore SIGINT. The pager will handle it (and we'll get
+       SIGPIPE). */
+    struct sigaction act;
+    act.sa_handler = SIG_IGN;
+    act.sa_flags = 0;
+    sigemptyset(&act.sa_mask);
+    if (sigaction(SIGINT, &act, 0)) throw SysError("ignoring SIGINT");
+
+    restoreSIGPIPE();
+
+    Pipe toPager;
+    toPager.create();
+
+    pid = startProcess([&]() {
+        if (dup2(toPager.readSide, STDIN_FILENO) == -1)
+            throw SysError("dupping stdin");
+        execl("/bin/sh", "sh", "-c", pager.c_str(), NULL);
+        throw SysError(format("executing `%1%'") % pager);
+    });
+
+    if (dup2(toPager.writeSide, STDOUT_FILENO) == -1)
+        throw SysError("dupping stdout");
+
+}
+
+
+RunPager::~RunPager()
+{
+    if (pid != -1) {
+        close(STDOUT_FILENO);
+        pid.wait(true);
+    }
+}
+
+
 }
diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh
index c74e7cbc197d..c56203daefa2 100644
--- a/src/libmain/shared.hh
+++ b/src/libmain/shared.hh
@@ -69,6 +69,18 @@ template<class N> N getIntArg(const string & opt,
 /* Show the manual page for the specified program. */
 void showManPage(const string & name);
 
+/* The constructor of this class starts a pager if stdout is a
+   terminal and $PAGER is set. Stdout is redirected to the pager. */
+class RunPager
+{
+public:
+    RunPager();
+    ~RunPager();
+
+private:
+    Pid pid;
+};
+
 extern volatile ::sig_atomic_t blockInt;
 
 }
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index ed283fb8c8f6..14aab7cded32 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -931,11 +931,11 @@ void closeOnExec(int fd)
 
 void restoreSIGPIPE()
 {
-    struct sigaction act, oact;
+    struct sigaction act;
     act.sa_handler = SIG_DFL;
     act.sa_flags = 0;
     sigemptyset(&act.sa_mask);
-    if (sigaction(SIGPIPE, &act, &oact)) throw SysError("resetting SIGPIPE");
+    if (sigaction(SIGPIPE, &act, 0)) throw SysError("resetting SIGPIPE");
 }
 
 
diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc
index 6a297b429239..abe4c7e3923f 100644
--- a/src/nix-store/nix-store.cc
+++ b/src/nix-store/nix-store.cc
@@ -459,6 +459,8 @@ static void opReadLog(Strings opFlags, Strings opArgs)
 {
     if (!opFlags.empty()) throw UsageError("unknown flag");
 
+    RunPager pager;
+
     foreach (Strings::iterator, i, opArgs) {
         Path path = useDeriver(followLinksToStorePath(*i));