about summary refs log tree commit diff
path: root/src/libmain
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmain')
-rw-r--r--src/libmain/shared.cc59
-rw-r--r--src/libmain/stack.cc2
2 files changed, 24 insertions, 37 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc
index bf5688a1e606..9c74a614e2e4 100644
--- a/src/libmain/shared.cc
+++ b/src/libmain/shared.cc
@@ -16,7 +16,6 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <signal.h>
-#include <spawn.h>
 
 extern char * * environ;
 
@@ -293,8 +292,9 @@ int handleExceptions(const string & programName, std::function<void()> fun)
 
 RunPager::RunPager()
 {
-    string pager = getEnv("PAGER");
-    if (!isatty(STDOUT_FILENO) || pager.empty()) return;
+    if (!isatty(STDOUT_FILENO)) return;
+    string pager = getEnv("PAGER", "default");
+    if (pager == "" || pager == "cat") return;
 
     /* Ignore SIGINT. The pager will handle it (and we'll get
        SIGPIPE). */
@@ -309,35 +309,18 @@ RunPager::RunPager()
     Pipe toPager;
     toPager.create();
 
-    // FIXME: should do this in the child environment.
-    if (!getenv("LESS"))
-        setenv("LESS", "FRSXMK", 1);
-
-    /* Start the pager using posix_spawn. */
-    pid_t pid_;
-    const char * argv[] = { "sh", "-c", pager.c_str(), 0 };
-
-    posix_spawn_file_actions_t fileActions;
-    int err = posix_spawn_file_actions_init(&fileActions);
-    if (err) throw SysError(err, "creating POSIX file actions");
-    err = posix_spawn_file_actions_adddup2(&fileActions, toPager.readSide, STDIN_FILENO);
-    if (err) throw SysError(err, "adding to POSIX file actions");
-
-    posix_spawnattr_t spawnAttrs;
-    err = posix_spawnattr_init(&spawnAttrs);
-    if (err) throw SysError(err, "creating POSIX spawn attrs");
-#ifdef POSIX_SPAWN_USEVFORK
-    err = posix_spawnattr_setflags(&spawnAttrs, POSIX_SPAWN_USEVFORK);
-    if (err) throw SysError(err, "setting POSIX spawn attr flag");
-#endif
-
-    err = posix_spawn(&pid_, "/bin/sh", &fileActions, &spawnAttrs, (char * const *) argv, environ);
-
-    posix_spawn_file_actions_destroy(&fileActions);
-    posix_spawnattr_destroy(&spawnAttrs);
-
-    if (err) throw SysError(err, format("running ‘%1%’") % pager);
-    pid = pid_;
+    pid = startProcess([&]() {
+        if (dup2(toPager.readSide, STDIN_FILENO) == -1)
+            throw SysError("dupping stdin");
+        if (!getenv("LESS"))
+            setenv("LESS", "FRSXMK", 1);
+        if (pager != "default")
+            execl("/bin/sh", "sh", "-c", pager.c_str(), NULL);
+        execlp("pager", "pager", NULL);
+        execlp("less", "less", NULL);
+        execlp("more", "more", NULL);
+        throw SysError(format("executing ‘%1%’") % pager);
+    });
 
     if (dup2(toPager.writeSide, STDOUT_FILENO) == -1)
         throw SysError("dupping stdout");
@@ -346,10 +329,14 @@ RunPager::RunPager()
 
 RunPager::~RunPager()
 {
-    if (pid != -1) {
-        std::cout.flush();
-        close(STDOUT_FILENO);
-        pid.wait(true);
+    try {
+        if (pid != -1) {
+            std::cout.flush();
+            close(STDOUT_FILENO);
+            pid.wait(true);
+        }
+    } catch (...) {
+        ignoreException();
     }
 }
 
diff --git a/src/libmain/stack.cc b/src/libmain/stack.cc
index b670e695d140..41b617d98be2 100644
--- a/src/libmain/stack.cc
+++ b/src/libmain/stack.cc
@@ -32,7 +32,7 @@ static void sigsegvHandler(int signo, siginfo_t * info, void * ctx)
         if (diff < 0) diff = -diff;
         if (diff < 4096) {
             char msg[] = "error: stack overflow (possible infinite recursion)\n";
-            write(2, msg, strlen(msg));
+            (void) write(2, msg, strlen(msg));
             _exit(1); // maybe abort instead?
         }
     }