blob: 6518bea926d6fdec47be68a73431110e45e9795b [file] [log] [blame]
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06001#include <sys/select.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include <signal.h>
Andi Kleen61eb2eb2016-09-15 15:24:44 -07006#include <sys/ioctl.h>
Josh Poimboeuf2f4ce5e2015-12-15 09:39:38 -06007#include "pager.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +02008#include "run-command.h"
9#include "sigchain.h"
Josh Poimboeuf096d3552015-12-15 09:39:35 -060010#include "subcmd-config.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +020011
12/*
13 * This is split up from the rest of git so that we can do
14 * something different on Windows.
15 */
16
17static int spawned_pager;
Andi Kleen61eb2eb2016-09-15 15:24:44 -070018static int pager_columns;
Ingo Molnara930d2c2009-05-27 09:50:13 +020019
Josh Poimboeuf096d3552015-12-15 09:39:35 -060020void pager_init(const char *pager_env)
21{
22 subcmd_config.pager_env = pager_env;
23}
24
Ingo Molnara930d2c2009-05-27 09:50:13 +020025static void pager_preexec(void)
26{
27 /*
28 * Work around bug in "less" by not starting it until we
29 * have real input
30 */
31 fd_set in;
32
33 FD_ZERO(&in);
34 FD_SET(0, &in);
35 select(1, &in, NULL, &in, NULL);
36
37 setenv("LESS", "FRSX", 0);
38}
Ingo Molnara930d2c2009-05-27 09:50:13 +020039
40static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
41static struct child_process pager_process;
42
43static void wait_for_pager(void)
44{
45 fflush(stdout);
46 fflush(stderr);
47 /* signal EOF to pager */
48 close(1);
49 close(2);
50 finish_command(&pager_process);
51}
52
53static void wait_for_pager_signal(int signo)
54{
55 wait_for_pager();
56 sigchain_pop(signo);
57 raise(signo);
58}
59
60void setup_pager(void)
61{
Josh Poimboeuf096d3552015-12-15 09:39:35 -060062 const char *pager = getenv(subcmd_config.pager_env);
Andi Kleen61eb2eb2016-09-15 15:24:44 -070063 struct winsize sz;
Ingo Molnara930d2c2009-05-27 09:50:13 +020064
65 if (!isatty(1))
66 return;
Andi Kleen61eb2eb2016-09-15 15:24:44 -070067 if (ioctl(1, TIOCGWINSZ, &sz) == 0)
68 pager_columns = sz.ws_col;
Ingo Molnara930d2c2009-05-27 09:50:13 +020069 if (!pager)
70 pager = getenv("PAGER");
Michael Lentine21cfc5e2014-05-20 11:48:49 +020071 if (!(pager || access("/usr/bin/pager", X_OK)))
72 pager = "/usr/bin/pager";
73 if (!(pager || access("/usr/bin/less", X_OK)))
74 pager = "/usr/bin/less";
Ingo Molnara930d2c2009-05-27 09:50:13 +020075 if (!pager)
Michael Lentine21cfc5e2014-05-20 11:48:49 +020076 pager = "cat";
77 if (!*pager || !strcmp(pager, "cat"))
Ingo Molnara930d2c2009-05-27 09:50:13 +020078 return;
79
80 spawned_pager = 1; /* means we are emitting to terminal */
81
82 /* spawn the pager */
83 pager_argv[2] = pager;
84 pager_process.argv = pager_argv;
85 pager_process.in = -1;
Ingo Molnara930d2c2009-05-27 09:50:13 +020086 pager_process.preexec_cb = pager_preexec;
Ingo Molnarfde953c2009-06-27 06:06:39 +020087
Ingo Molnara930d2c2009-05-27 09:50:13 +020088 if (start_command(&pager_process))
89 return;
90
91 /* original process continues, but writes to the pipe */
92 dup2(pager_process.in, 1);
93 if (isatty(2))
94 dup2(pager_process.in, 2);
95 close(pager_process.in);
96
97 /* this makes sure that the parent terminates after the pager */
98 sigchain_push_common(wait_for_pager_signal);
99 atexit(wait_for_pager);
100}
101
102int pager_in_use(void)
103{
Josh Poimboeufa871a772015-12-13 22:18:08 -0600104 return spawned_pager;
Ingo Molnara930d2c2009-05-27 09:50:13 +0200105}
Andi Kleen61eb2eb2016-09-15 15:24:44 -0700106
107int pager_get_columns(void)
108{
109 char *s;
110
111 s = getenv("COLUMNS");
112 if (s)
113 return atoi(s);
114
115 return (pager_columns ? pager_columns : 80) - 2;
116}