1 /* vi: set sw=4 ts=4: */
3 * Mini more implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
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.
12 * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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.
26 //applet:IF_MORE(APPLET(more, BB_DIR_BIN, BB_SUID_DROP))
28 //kbuild:lib-$(CONFIG_MORE) += more.o
30 //usage:#define more_trivial_usage
32 //usage:#define more_full_usage "\n\n"
33 //usage: "View FILE (or stdin) one screenful at a time"
35 //usage:#define more_example_usage
36 //usage: "$ dmesg | more\n"
39 #include "common_bufsiz.h"
41 /* Support for FEATURE_USE_TERMIOS */
45 struct termios initial_settings;
46 struct termios new_settings;
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)
54 #define setTermSettings(fd, argp) \
56 if (ENABLE_FEATURE_USE_TERMIOS) \
57 tcsetattr(fd, TCSANOW, argp); \
59 #define getTermSettings(fd, argp) tcgetattr(fd, argp)
61 static void gotsig(int sig UNUSED_PARAM)
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);
70 #define CONVERTED_TAB_SIZE 8
72 int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
73 int more_main(int argc UNUSED_PARAM, char **argv)
75 int c = c; /* for compiler */
79 int please_display_more_prompt;
84 unsigned terminal_width;
85 unsigned terminal_height;
90 /* Accepted but ignored: */
91 /* -d Display help instead of ringing bell is pressed */
92 /* -f Count logical lines (IOW: long lines are not folded) */
93 /* -l Do not pause after any line containing a ^L (form feed) */
94 /* -s Squeeze blank lines into one */
95 /* -u Suppress underlining */
96 getopt32(argv, "dflsu");
99 /* Another popular pager, most, detects when stdout
100 * is not a tty and turns into cat. This makes sense. */
101 if (!isatty(STDOUT_FILENO))
103 cin = fopen_for_read(CURRENT_TTY);
107 if (ENABLE_FEATURE_USE_TERMIOS) {
108 cin_fileno = fileno(cin);
109 getTermSettings(cin_fileno, &initial_settings);
110 new_settings = initial_settings;
111 new_settings.c_lflag &= ~(ICANON | ECHO);
112 new_settings.c_cc[VMIN] = 1;
113 new_settings.c_cc[VTIME] = 0;
114 setTermSettings(cin_fileno, &new_settings);
125 file = fopen_or_warn(*argv, "r");
130 fstat(fileno(file), &st);
132 please_display_more_prompt = 0;
133 /* never returns w, h <= 1 */
134 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
135 terminal_height -= 1;
139 while (spaces || (c = getc(file)) != EOF) {
144 if (input != 'r' && please_display_more_prompt) {
145 len = printf("--More-- ");
146 if (st.st_size != 0) {
147 uoff_t d = (uoff_t)st.st_size / 100;
150 len += printf("(%u%% of %"OFF_FMT"u bytes)",
151 (int) ((uoff_t)ftello(file) / d),
157 * We've just displayed the "--More--" prompt, so now we need
158 * to get input from the user.
162 input = tolower(input);
163 if (!ENABLE_FEATURE_USE_TERMIOS)
164 printf("\033[A"); /* cursor up */
165 /* Erase the last message */
166 printf("\r%*s\r", len, "");
168 /* Due to various multibyte escape
169 * sequences, it's not ok to accept
170 * any input as a command to scroll
171 * the screen. We only allow known
172 * commands, else we show help msg. */
173 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
175 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
179 please_display_more_prompt = 0;
184 /* The user may have resized the terminal.
185 * Re-read the dimensions. */
186 if (ENABLE_FEATURE_USE_TERMIOS) {
187 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
188 terminal_height -= 1;
192 /* Crudely convert tabs into spaces, which are
193 * a bajillion times easier to deal with. */
195 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
200 * There are two input streams to worry about here:
202 * c : the character we are reading from the file being "mored"
203 * input: a character received from the keyboard
205 * If we hit a newline in the _file_ stream, we want to test and
206 * see if any characters have been hit in the _input_ stream. This
207 * allows the user to quit while in the middle of a file.
209 wrap = (++len > terminal_width);
210 if (c == '\n' || wrap) {
211 /* Then outputting this character
212 * will move us to a new line. */
213 if (++lines >= terminal_height || input == '\n')
214 please_display_more_prompt = 1;
217 if (c != '\n' && wrap) {
218 /* Then outputting this will also put a character on
219 * the beginning of that new line. Thus we first want to
220 * display the prompt (if any), so we skip the putchar()
221 * and go back to the top of the loop, without reading
222 * a new character. */
225 /* My small mind cannot fathom backspaces and UTF-8 */
227 die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
231 } while (*argv && *++argv);
233 setTermSettings(cin_fileno, &initial_settings);