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.
19 /* Support for FEATURE_USE_TERMIOS */
23 struct termios initial_settings;
24 struct termios new_settings;
26 #define G (*(struct globals*)bb_common_bufsiz1)
27 #define INIT_G() ((void)0)
28 #define initial_settings (G.initial_settings)
29 #define new_settings (G.new_settings )
30 #define cin_fileno (G.cin_fileno )
32 #define setTermSettings(fd, argp) do { \
33 if (ENABLE_FEATURE_USE_TERMIOS) tcsetattr(fd, TCSANOW, argp); \
35 #define getTermSettings(fd, argp) tcgetattr(fd, argp)
37 static void gotsig(int sig UNUSED_PARAM)
40 setTermSettings(cin_fileno, &initial_settings);
44 #define CONVERTED_TAB_SIZE 8
46 int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int more_main(int argc UNUSED_PARAM, char **argv)
49 int c = c; /* for compiler */
53 int please_display_more_prompt;
58 unsigned terminal_width;
59 unsigned terminal_height;
64 /* Another popular pager, most, detects when stdout
65 * is not a tty and turns into cat. This makes sense. */
66 if (!isatty(STDOUT_FILENO))
68 cin = fopen_for_read(CURRENT_TTY);
72 if (ENABLE_FEATURE_USE_TERMIOS) {
73 cin_fileno = fileno(cin);
74 getTermSettings(cin_fileno, &initial_settings);
75 new_settings = initial_settings;
76 new_settings.c_lflag &= ~ICANON;
77 new_settings.c_lflag &= ~ECHO;
78 new_settings.c_cc[VMIN] = 1;
79 new_settings.c_cc[VTIME] = 0;
80 setTermSettings(cin_fileno, &new_settings);
91 file = fopen_or_warn(*argv, "r");
96 fstat(fileno(file), &st);
98 please_display_more_prompt = 0;
99 /* never returns w, h <= 1 */
100 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
101 terminal_height -= 1;
105 while (spaces || (c = getc(file)) != EOF) {
110 if (input != 'r' && please_display_more_prompt) {
111 len = printf("--More-- ");
112 if (st.st_size > 0) {
113 len += printf("(%u%% of %"OFF_FMT"u bytes)",
114 (int) (ftello(file)*100 / st.st_size),
120 * We've just displayed the "--More--" prompt, so now we need
121 * to get input from the user.
125 input = tolower(input);
126 if (!ENABLE_FEATURE_USE_TERMIOS)
127 printf("\033[A"); /* cursor up */
128 /* Erase the last message */
129 printf("\r%*s\r", len, "");
131 /* Due to various multibyte escape
132 * sequences, it's not ok to accept
133 * any input as a command to scroll
134 * the screen. We only allow known
135 * commands, else we show help msg. */
136 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
138 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
142 please_display_more_prompt = 0;
147 /* The user may have resized the terminal.
148 * Re-read the dimensions. */
149 if (ENABLE_FEATURE_USE_TERMIOS) {
150 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
151 terminal_height -= 1;
155 /* Crudely convert tabs into spaces, which are
156 * a bajillion times easier to deal with. */
158 spaces = CONVERTED_TAB_SIZE - 1;
163 * There are two input streams to worry about here:
165 * c : the character we are reading from the file being "mored"
166 * input: a character received from the keyboard
168 * If we hit a newline in the _file_ stream, we want to test and
169 * see if any characters have been hit in the _input_ stream. This
170 * allows the user to quit while in the middle of a file.
172 wrap = (++len > terminal_width);
173 if (c == '\n' || wrap) {
174 /* Then outputting this character
175 * will move us to a new line. */
176 if (++lines >= terminal_height || input == '\n')
177 please_display_more_prompt = 1;
180 if (c != '\n' && wrap) {
181 /* Then outputting this will also put a character on
182 * the beginning of that new line. Thus we first want to
183 * display the prompt (if any), so we skip the putchar()
184 * and go back to the top of the loop, without reading
185 * a new character. */
188 /* My small mind cannot fathom backspaces and UTF-8 */
193 } while (*argv && *++argv);
195 setTermSettings(cin_fileno, &initial_settings);