perf tools: Provide subcmd configuration at runtime
[deliverable/linux.git] / tools / perf / util / pager.c
CommitLineData
a930d2c0
IM
1#include "cache.h"
2#include "run-command.h"
3#include "sigchain.h"
096d3558 4#include "subcmd-config.h"
a930d2c0
IM
5
6/*
7 * This is split up from the rest of git so that we can do
8 * something different on Windows.
9 */
10
11static int spawned_pager;
12
096d3558
JP
13void pager_init(const char *pager_env)
14{
15 subcmd_config.pager_env = pager_env;
16}
17
a930d2c0
IM
18static void pager_preexec(void)
19{
20 /*
21 * Work around bug in "less" by not starting it until we
22 * have real input
23 */
24 fd_set in;
25
26 FD_ZERO(&in);
27 FD_SET(0, &in);
28 select(1, &in, NULL, &in, NULL);
29
30 setenv("LESS", "FRSX", 0);
31}
a930d2c0
IM
32
33static const char *pager_argv[] = { "sh", "-c", NULL, NULL };
34static struct child_process pager_process;
35
36static void wait_for_pager(void)
37{
38 fflush(stdout);
39 fflush(stderr);
40 /* signal EOF to pager */
41 close(1);
42 close(2);
43 finish_command(&pager_process);
44}
45
46static void wait_for_pager_signal(int signo)
47{
48 wait_for_pager();
49 sigchain_pop(signo);
50 raise(signo);
51}
52
53void setup_pager(void)
54{
096d3558 55 const char *pager = getenv(subcmd_config.pager_env);
a930d2c0
IM
56
57 if (!isatty(1))
58 return;
a930d2c0
IM
59 if (!pager)
60 pager = getenv("PAGER");
21cfc5e1
ML
61 if (!(pager || access("/usr/bin/pager", X_OK)))
62 pager = "/usr/bin/pager";
63 if (!(pager || access("/usr/bin/less", X_OK)))
64 pager = "/usr/bin/less";
a930d2c0 65 if (!pager)
21cfc5e1
ML
66 pager = "cat";
67 if (!*pager || !strcmp(pager, "cat"))
a930d2c0
IM
68 return;
69
70 spawned_pager = 1; /* means we are emitting to terminal */
71
72 /* spawn the pager */
73 pager_argv[2] = pager;
74 pager_process.argv = pager_argv;
75 pager_process.in = -1;
a930d2c0 76 pager_process.preexec_cb = pager_preexec;
fde953c1 77
a930d2c0
IM
78 if (start_command(&pager_process))
79 return;
80
81 /* original process continues, but writes to the pipe */
82 dup2(pager_process.in, 1);
83 if (isatty(2))
84 dup2(pager_process.in, 2);
85 close(pager_process.in);
86
87 /* this makes sure that the parent terminates after the pager */
88 sigchain_push_common(wait_for_pager_signal);
89 atexit(wait_for_pager);
90}
91
92int pager_in_use(void)
93{
a871a775 94 return spawned_pager;
a930d2c0 95}
This page took 0.336919 seconds and 5 git commands to generate.