[go: up one dir, main page]

File: pager.c

package info (click to toggle)
uftrace 0.18.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,356 kB
  • sloc: ansic: 49,770; python: 11,181; asm: 837; makefile: 769; sh: 637; cpp: 627; javascript: 191
file content (116 lines) | stat: -rw-r--r-- 1,955 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * This code is based on Linux perf tools (so in turn git) project
 * which is released under GPL v2.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <unistd.h>

static int pager_pid;
static int pager_fd;

static int start_command(const char *argv[])
{
	int fds[2];

	if (pipe(fds) < 0)
		return -1;

	fflush(NULL);
	pager_pid = fork();
	if (pager_pid < 0) {
		close(fds[0]);
		close(fds[1]);
		return -1;
	}

	if (!pager_pid) {
		fd_set in_set;
		fd_set ex_set;

		/* child process */
		dup2(fds[0], STDIN_FILENO);
		close(fds[0]);
		close(fds[1]);

		FD_ZERO(&in_set);
		FD_ZERO(&ex_set);
		FD_SET(STDIN_FILENO, &in_set);
		FD_SET(STDIN_FILENO, &ex_set);

		/*
		 * Work around bug in "less" by not starting it
		 * until we have real input
		 */
		select(1, &in_set, NULL, &ex_set, NULL);

		setenv("LESS", "FRSX", 0);

		execvp(argv[0], (char *const *)argv);
		exit(127);
	}

	close(fds[0]);
	pager_fd = fds[1];

	return 0;
}

void wait_for_pager(void)
{
	int status;

	if (!pager_pid)
		return;

	/* signal EOF to pager */
	fclose(stdout);
	fclose(stderr);

	waitpid(pager_pid, &status, 0);
	pager_pid = 0;
}

char *setup_pager(void)
{
	char *pager = getenv("PAGER");

	if (!isatty(STDOUT_FILENO))
		return NULL;
	if (!(pager || access("/usr/bin/pager", X_OK)))
		pager = "/usr/bin/pager";
	if (!(pager || access("/usr/bin/less", X_OK)))
		pager = "/usr/bin/less";
	if (!pager)
		pager = "cat";
	if (!*pager || !strcmp(pager, "cat"))
		return NULL;

	return pager;
}

void start_pager(char *pager)
{
	const char *pager_argv[] = { "sh", "-c", NULL, NULL };

	if (pager == NULL)
		return;

	/* spawn the pager */
	pager_argv[2] = pager;

	if (start_command(pager_argv))
		return;

	/* original process continues, but writes to the pipe */
	dup2(pager_fd, STDOUT_FILENO);
	if (isatty(STDERR_FILENO))
		dup2(pager_fd, STDERR_FILENO);
	close(pager_fd);

	atexit(wait_for_pager);
}