summaryrefslogtreecommitdiff
path: root/util-linux/more.c (plain)
blob: 31e27ab71a8ba76e853a5739cb75d490c01ff2dd
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini more implementation for busybox
4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 *
8 * Latest version blended together by Erik Andersen <andersen@codepoet.org>,
9 * based on the original more implementation by Bruce, and code from the
10 * Debian boot-floppies team.
11 *
12 * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
13 *
14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
15 */
16//config:config MORE
17//config: bool "more"
18//config: default y
19//config: help
20//config: more is a simple utility which allows you to read text one screen
21//config: sized page at a time. If you want to read text that is larger than
22//config: the screen, and you are using anything faster than a 300 baud modem,
23//config: you will probably find this utility very helpful. If you don't have
24//config: any need to reading text files, you can leave this disabled.
25
26//applet:IF_MORE(APPLET(more, BB_DIR_BIN, BB_SUID_DROP))
27
28//kbuild:lib-$(CONFIG_MORE) += more.o
29
30//usage:#define more_trivial_usage
31//usage: "[FILE]..."
32//usage:#define more_full_usage "\n\n"
33//usage: "View FILE (or stdin) one screenful at a time"
34//usage:
35//usage:#define more_example_usage
36//usage: "$ dmesg | more\n"
37
38#include "libbb.h"
39#include "common_bufsiz.h"
40
41/* Support for FEATURE_USE_TERMIOS */
42
43struct globals {
44 int cin_fileno;
45 struct termios initial_settings;
46 struct termios new_settings;
47} FIX_ALIASING;
48#define G (*(struct globals*)bb_common_bufsiz1)
49#define initial_settings (G.initial_settings)
50#define new_settings (G.new_settings )
51#define cin_fileno (G.cin_fileno )
52#define INIT_G() do { setup_common_bufsiz(); } while (0)
53
54#define setTermSettings(fd, argp) \
55do { \
56 if (ENABLE_FEATURE_USE_TERMIOS) \
57 tcsetattr(fd, TCSANOW, argp); \
58} while (0)
59#define getTermSettings(fd, argp) tcgetattr(fd, argp)
60
61static void gotsig(int sig UNUSED_PARAM)
62{
63 /* bb_putchar_stderr doesn't use stdio buffering,
64 * therefore it is safe in signal handler */
65 bb_putchar_stderr('\n');
66 setTermSettings(cin_fileno, &initial_settings);
67 _exit(EXIT_FAILURE);
68}
69
70#define CONVERTED_TAB_SIZE 8
71
72int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
73int more_main(int argc UNUSED_PARAM, char **argv)
74{
75 int c = EOF; /* for compiler */
76 int input = 0;
77 int spaces = 0;
78 int please_display_more_prompt;
79 struct stat st;
80 FILE *file;
81 FILE *cin;
82 unsigned len, lines;
83 unsigned terminal_width;
84 unsigned terminal_height;
85
86 INIT_G();
87
88 /* Parse options */
89 /* Accepted but ignored: */
90 /* -d Display help instead of ringing bell is pressed */
91 /* -f Count logical lines (IOW: long lines are not folded) */
92 /* -l Do not pause after any line containing a ^L (form feed) */
93 /* -s Squeeze blank lines into one */
94 /* -u Suppress underlining */
95 getopt32(argv, "dflsu");
96 argv += optind;
97
98 /* Another popular pager, most, detects when stdout
99 * is not a tty and turns into cat. This makes sense. */
100 if (!isatty(STDOUT_FILENO))
101 return bb_cat(argv);
102 cin = fopen_for_read(CURRENT_TTY);
103 if (!cin)
104 return bb_cat(argv);
105
106 if (ENABLE_FEATURE_USE_TERMIOS) {
107 cin_fileno = fileno(cin);
108 getTermSettings(cin_fileno, &initial_settings);
109 new_settings = initial_settings;
110 new_settings.c_lflag &= ~(ICANON | ECHO);
111 new_settings.c_cc[VMIN] = 1;
112 new_settings.c_cc[VTIME] = 0;
113 setTermSettings(cin_fileno, &new_settings);
114 bb_signals(0
115 + (1 << SIGINT)
116 + (1 << SIGQUIT)
117 + (1 << SIGTERM)
118 , gotsig);
119 }
120
121 do {
122 file = stdin;
123 if (*argv) {
124 file = fopen_or_warn(*argv, "r");
125 if (!file)
126 continue;
127 }
128 st.st_size = 0;
129 fstat(fileno(file), &st);
130
131 please_display_more_prompt = 0;
132 /* never returns w, h <= 1 */
133 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
134 terminal_height -= 1;
135
136 len = 0;
137 lines = 0;
138 while (spaces || (c = getc(file)) != EOF) {
139 int wrap;
140 if (spaces)
141 spaces--;
142 loop_top:
143 if (input != 'r' && please_display_more_prompt) {
144 len = printf("--More-- ");
145 if (st.st_size != 0) {
146 len += printf("(%u%% of %"FILESIZE_FMT"u bytes)",
147 (int) (ftello(file)*100 / st.st_size),
148 st.st_size);
149 }
150 fflush_all();
151
152 /*
153 * We've just displayed the "--More--" prompt, so now we need
154 * to get input from the user.
155 */
156 for (;;) {
157 input = getc(cin);
158 input = tolower(input);
159 if (!ENABLE_FEATURE_USE_TERMIOS)
160 printf("\033[A"); /* cursor up */
161 /* Erase the last message */
162 printf("\r%*s\r", len, "");
163
164 /* Due to various multibyte escape
165 * sequences, it's not ok to accept
166 * any input as a command to scroll
167 * the screen. We only allow known
168 * commands, else we show help msg. */
169 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
170 break;
171 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
172 }
173 len = 0;
174 lines = 0;
175 please_display_more_prompt = 0;
176
177 if (input == 'q')
178 goto end;
179
180 /* The user may have resized the terminal.
181 * Re-read the dimensions. */
182 if (ENABLE_FEATURE_USE_TERMIOS) {
183 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
184 terminal_height -= 1;
185 }
186 }
187
188 /* Crudely convert tabs into spaces, which are
189 * a bajillion times easier to deal with. */
190 if (c == '\t') {
191 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
192 c = ' ';
193 }
194
195 /*
196 * There are two input streams to worry about here:
197 *
198 * c : the character we are reading from the file being "mored"
199 * input: a character received from the keyboard
200 *
201 * If we hit a newline in the _file_ stream, we want to test and
202 * see if any characters have been hit in the _input_ stream. This
203 * allows the user to quit while in the middle of a file.
204 */
205 wrap = (++len > terminal_width);
206 if (c == '\n' || wrap) {
207 /* Then outputting this character
208 * will move us to a new line. */
209 if (++lines >= terminal_height || input == '\n')
210 please_display_more_prompt = 1;
211 len = 0;
212 }
213 if (c != '\n' && wrap) {
214 /* Then outputting this will also put a character on
215 * the beginning of that new line. Thus we first want to
216 * display the prompt (if any), so we skip the putchar()
217 * and go back to the top of the loop, without reading
218 * a new character. */
219 goto loop_top;
220 }
221 /* My small mind cannot fathom backspaces and UTF-8 */
222 putchar(c);
223 die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
224 }
225 fclose(file);
226 fflush_all();
227 } while (*argv && *++argv);
228 end:
229 setTermSettings(cin_fileno, &initial_settings);
230 return 0;
231}
232