17ec681f3Smrg/*
27ec681f3Smrg * Copyright (c) 2018 Rob Clark <robdclark@gmail.com>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include <errno.h>
257ec681f3Smrg#include <signal.h>
267ec681f3Smrg#include <stdbool.h>
277ec681f3Smrg#include <stdio.h>
287ec681f3Smrg#include <stdlib.h>
297ec681f3Smrg#include <string.h>
307ec681f3Smrg#include <unistd.h>
317ec681f3Smrg#include <sys/types.h>
327ec681f3Smrg#include <sys/wait.h>
337ec681f3Smrg
347ec681f3Smrg#include "pager.h"
357ec681f3Smrg
367ec681f3Smrgstatic pid_t pager_pid;
377ec681f3Smrg
387ec681f3Smrgstatic void
397ec681f3Smrgpager_death(int n)
407ec681f3Smrg{
417ec681f3Smrg   exit(0);
427ec681f3Smrg}
437ec681f3Smrg
447ec681f3Smrgvoid
457ec681f3Smrgpager_open(void)
467ec681f3Smrg{
477ec681f3Smrg   int fd[2];
487ec681f3Smrg
497ec681f3Smrg   if (pipe(fd) < 0) {
507ec681f3Smrg      fprintf(stderr, "Failed to create pager pipe: %m\n");
517ec681f3Smrg      exit(-1);
527ec681f3Smrg   }
537ec681f3Smrg
547ec681f3Smrg   pager_pid = fork();
557ec681f3Smrg   if (pager_pid < 0) {
567ec681f3Smrg      fprintf(stderr, "Failed to fork pager: %m\n");
577ec681f3Smrg      exit(-1);
587ec681f3Smrg   }
597ec681f3Smrg
607ec681f3Smrg   if (pager_pid == 0) {
617ec681f3Smrg      const char *less_opts;
627ec681f3Smrg
637ec681f3Smrg      dup2(fd[0], STDIN_FILENO);
647ec681f3Smrg      close(fd[0]);
657ec681f3Smrg      close(fd[1]);
667ec681f3Smrg
677ec681f3Smrg      less_opts = "FRSMKX";
687ec681f3Smrg      setenv("LESS", less_opts, 1);
697ec681f3Smrg
707ec681f3Smrg      execlp("less", "less", NULL);
717ec681f3Smrg
727ec681f3Smrg   } else {
737ec681f3Smrg      /* we want to kill the parent process when pager exits: */
747ec681f3Smrg      signal(SIGCHLD, pager_death);
757ec681f3Smrg      dup2(fd[1], STDOUT_FILENO);
767ec681f3Smrg      close(fd[0]);
777ec681f3Smrg      close(fd[1]);
787ec681f3Smrg   }
797ec681f3Smrg}
807ec681f3Smrg
817ec681f3Smrgint
827ec681f3Smrgpager_close(void)
837ec681f3Smrg{
847ec681f3Smrg   siginfo_t status;
857ec681f3Smrg
867ec681f3Smrg   close(STDOUT_FILENO);
877ec681f3Smrg
887ec681f3Smrg   while (true) {
897ec681f3Smrg      memset(&status, 0, sizeof(status));
907ec681f3Smrg      if (waitid(P_PID, pager_pid, &status, WEXITED) < 0) {
917ec681f3Smrg         if (errno == EINTR)
927ec681f3Smrg            continue;
937ec681f3Smrg         return -errno;
947ec681f3Smrg      }
957ec681f3Smrg
967ec681f3Smrg      return 0;
977ec681f3Smrg   }
987ec681f3Smrg}
99