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#include "libbb.h"
39#include "common_bufsiz.h"
40
41struct globals {
42 int tty_fileno;
43 unsigned terminal_width;
44 unsigned terminal_height;
45 struct termios initial_settings;
46} FIX_ALIASING;
47#define G (*(struct globals*)bb_common_bufsiz1)
48#define INIT_G() do { setup_common_bufsiz(); } while (0)
49
50static void get_wh(void)
51{
52
53 get_terminal_width_height(G.tty_fileno, &G.terminal_width, &G.terminal_height);
54 G.terminal_height -= 1;
55}
56
57static void tcsetattr_tty_TCSANOW(struct termios *settings)
58{
59 tcsetattr(G.tty_fileno, TCSANOW, settings);
60}
61
62static void gotsig(int sig UNUSED_PARAM)
63{
64
65
66 bb_putchar_stderr('\n');
67 tcsetattr_tty_TCSANOW(&G.initial_settings);
68 _exit(EXIT_FAILURE);
69}
70
71#define CONVERTED_TAB_SIZE 8
72
73int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
74int more_main(int argc UNUSED_PARAM, char **argv)
75{
76 int c = c;
77 int input = 0;
78 int spaces = 0;
79 int please_display_more_prompt;
80 FILE *tty;
81
82 INIT_G();
83
84
85
86
87
88
89
90
91 getopt32(argv, "dflsu");
92 argv += optind;
93
94
95
96 if (!isatty(STDOUT_FILENO))
97 return bb_cat(argv);
98 tty = fopen_for_read(CURRENT_TTY);
99 if (!tty)
100 return bb_cat(argv);
101
102 G.tty_fileno = fileno(tty);
103
104
105 set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0);
106 bb_signals(BB_FATAL_SIGS, gotsig);
107
108 do {
109 struct stat st;
110 FILE *file;
111 int len;
112 int lines;
113
114 file = stdin;
115 if (*argv) {
116 file = fopen_or_warn(*argv, "r");
117 if (!file)
118 continue;
119 }
120 st.st_size = 0;
121 fstat(fileno(file), &st);
122
123 get_wh();
124
125 please_display_more_prompt = 0;
126 len = 0;
127 lines = 0;
128 for (;;) {
129 int wrap;
130
131 if (spaces)
132 spaces--;
133 else {
134 c = getc(file);
135 if (c == EOF) break;
136 }
137 loop_top:
138 if (input != 'r' && please_display_more_prompt) {
139 len = printf("--More-- ");
140 if (st.st_size != 0) {
141 uoff_t d = (uoff_t)st.st_size / 100;
142 if (d == 0)
143 d = 1;
144 len += printf("(%u%% of %"OFF_FMT"u bytes)",
145 (int) ((uoff_t)ftello(file) / d),
146 st.st_size);
147 }
148
149
150
151
152
153 for (;;) {
154 fflush_all();
155 input = getc(tty);
156 input = tolower(input);
157
158 printf("\r%*s\r", len, "");
159
160 if (input == 'q')
161 goto end;
162
163
164
165
166
167 if (input == ' ' || input == '\n' || input == 'r')
168 break;
169 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
170 }
171 len = 0;
172 lines = 0;
173 please_display_more_prompt = 0;
174
175
176
177 get_wh();
178 }
179
180
181
182 if (c == '\t') {
183 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
184 c = ' ';
185 }
186
187
188
189
190
191
192
193
194
195
196
197 wrap = (++len > G.terminal_width);
198 if (c == '\n' || wrap) {
199
200
201 if (++lines >= G.terminal_height || input == '\n')
202 please_display_more_prompt = 1;
203 len = 0;
204 }
205 if (c != '\n' && wrap) {
206
207
208
209
210
211 goto loop_top;
212 }
213
214 putchar(c);
215 die_if_ferror_stdout();
216 }
217 fclose(file);
218 fflush_all();
219 } while (*argv && *++argv);
220 end:
221 tcsetattr_tty_TCSANOW(&G.initial_settings);
222 return 0;
223}
224