forked from scanmem/scanmem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow_message.c
182 lines (158 loc) · 5.05 KB
/
show_message.c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Message printing helper functions.
Copyright (C) 2010 WANG Lu <coolwanglu(a)gmail.com>
This file is part of libscanmem.
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include "common.h"
#include "show_message.h"
#include "scanmem.h"
void show_info(const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
fprintf(stderr, "info: ");
vfprintf(stderr, fmt, args);
va_end (args);
}
void show_error(const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
fprintf(stderr, "error: ");
vfprintf(stderr, fmt, args);
va_end (args);
}
void show_warn(const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
fprintf(stderr, "warn: ");
vfprintf(stderr, fmt, args);
va_end (args);
}
void show_user(const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
if (!(sm_globals.options.backend))
{
vfprintf(stderr, fmt, args);
}
va_end (args);
}
void show_debug(const char *fmt, ...)
{
va_list args;
va_start (args, fmt);
if (sm_globals.options.debug)
{
fprintf(stderr, "debug: ");
vfprintf(stderr, fmt, args);
}
va_end (args);
}
FILE *get_pager(FILE *fallback_output)
{
const char *pager;
pid_t pgpid;
int pgret;
int pgpipe[2];
bool pgcmdfail = false;
FILE *retfd = NULL;
char *const emptyvec[1] = { NULL };
assert(fallback_output != NULL && fileno(fallback_output) != -1);
if (sm_globals.options.backend || !isatty(fileno(fallback_output)))
return fallback_output;
if ((pager = util_getenv("PAGER")) == NULL || *pager == '\0') {
show_warn("get_pager(): couldn't get $PAGER, falling back to `more`\n");
pager = "more";
}
if (pipe2(pgpipe, O_NONBLOCK) == -1) {
show_error("get_pager(): pipe2() error `%s`. falling back to normal output\n", strerror(errno));
return fallback_output;
}
/*
* we write here to ensure we will always
* have something to read() into pgcmdfail.
*/
write(pgpipe[1], "", 1);
/* XXX: is $PATH modified prior? */
retry:
switch ((pgpid = fork())) {
case -1:
show_warn("get_pager(): fork() failed. falling back to normal output\n");
return fallback_output;
case 0:
execvp(pager, emptyvec);
/*
* if we got here, it means execvp() failed.
* errno contains the error, so pass it back
* up to parent. we use a pipe here to let
* the parent know that we are indeed returning
* the return value of the failed execvp().
*/
char nullbuf;
/* read() to empty pipe */
read(pgpipe[0], &nullbuf, 1);
write(pgpipe[1], "1", 2);
exit(errno);
/* NOTREACHED */
default:
if (waitpid(pgpid, &pgret, 0) == -1) {
show_debug("pager: waitpid() error `%s`\n", strerror(errno));
show_warn("pager: waitpid() error. falling back to normal output\n");
return fallback_output;
} else {
pgret = WEXITSTATUS(pgret);
if (read(pgpipe[0], &pgcmdfail, 1) == -1) {
show_error("pager: pipe read() error `%s`. falling back to normal output\n", strerror(errno));
return fallback_output;
}
if (pgcmdfail) {
show_debug("pager: execvp(pg=%s) ret -> %d (%s)\n", pager, pgret, strerror(pgret));
if (!strcmp(pager, "more")) {
show_warn("pager: sh failed to execute more. falling back to normal output\n");
return fallback_output;
} else {
show_warn("pager: sh failed to execute `%s`. trying `more`...\n", pager);
pager = "more";
goto retry;
}
}
}
}
if ((retfd = popen(pager, "w")) == NULL) {
show_warn("pager: couldn't popen() pager, falling back to normal output\n");
return fallback_output;
}
/*
* we need to ignore SIGPIPE in case the pager exits wihout draining the
* pipe (less seems to do this if you exit a large listing without
* scrolling down)
*/
signal(SIGPIPE, SIG_IGN);
assert(retfd != NULL);
return retfd;
}