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 tarball for details.
19 #if ENABLE_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) tcsetattr(fd, TCSANOW, argp)
33 #define getTermSettings(fd, argp) tcgetattr(fd, argp)
35 static void gotsig(int sig UNUSED_PARAM)
38 setTermSettings(cin_fileno, &initial_settings);
42 #else /* !FEATURE_USE_TERMIOS */
43 #define INIT_G() ((void)0)
44 #define setTermSettings(fd, argp) ((void)0)
45 #endif /* FEATURE_USE_TERMIOS */
47 #define CONVERTED_TAB_SIZE 8
49 int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
50 int more_main(int argc UNUSED_PARAM, char **argv)
52 int c = c; /* for gcc */
56 int please_display_more_prompt;
61 unsigned terminal_width;
62 unsigned terminal_height;
67 /* Another popular pager, most, detects when stdout
68 * is not a tty and turns into cat. This makes sense. */
69 if (!isatty(STDOUT_FILENO))
71 cin = fopen_for_read(CURRENT_TTY);
75 #if ENABLE_FEATURE_USE_TERMIOS
76 cin_fileno = fileno(cin);
77 getTermSettings(cin_fileno, &initial_settings);
78 new_settings = initial_settings;
79 new_settings.c_lflag &= ~ICANON;
80 new_settings.c_lflag &= ~ECHO;
81 new_settings.c_cc[VMIN] = 1;
82 new_settings.c_cc[VTIME] = 0;
83 setTermSettings(cin_fileno, &new_settings);
94 file = fopen_or_warn(*argv, "r");
99 fstat(fileno(file), &st);
101 please_display_more_prompt = 0;
102 /* never returns w, h <= 1 */
103 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
104 terminal_height -= 1;
108 while (spaces || (c = getc(file)) != EOF) {
113 if (input != 'r' && please_display_more_prompt) {
114 len = printf("--More-- ");
115 if (st.st_size > 0) {
116 len += printf("(%d%% of %"OFF_FMT"d bytes)",
117 (int) (ftello(file)*100 / st.st_size),
123 * We've just displayed the "--More--" prompt, so now we need
124 * to get input from the user.
128 input = tolower(input);
129 #if !ENABLE_FEATURE_USE_TERMIOS
130 printf("\033[A"); /* up cursor */
132 /* Erase the last message */
133 printf("\r%*s\r", len, "");
135 /* Due to various multibyte escape
136 * sequences, it's not ok to accept
137 * any input as a command to scroll
138 * the screen. We only allow known
139 * commands, else we show help msg. */
140 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
142 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
146 please_display_more_prompt = 0;
151 /* The user may have resized the terminal.
152 * Re-read the dimensions. */
153 #if ENABLE_FEATURE_USE_TERMIOS
154 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
155 terminal_height -= 1;
159 /* Crudely convert tabs into spaces, which are
160 * a bajillion times easier to deal with. */
162 spaces = CONVERTED_TAB_SIZE - 1;
167 * There are two input streams to worry about here:
169 * c : the character we are reading from the file being "mored"
170 * input: a character received from the keyboard
172 * If we hit a newline in the _file_ stream, we want to test and
173 * see if any characters have been hit in the _input_ stream. This
174 * allows the user to quit while in the middle of a file.
176 wrap = (++len > terminal_width);
177 if (c == '\n' || wrap) {
178 /* Then outputting this character
179 * will move us to a new line. */
180 if (++lines >= terminal_height || input == '\n')
181 please_display_more_prompt = 1;
184 if (c != '\n' && wrap) {
185 /* Then outputting this will also put a character on
186 * the beginning of that new line. Thus we first want to
187 * display the prompt (if any), so we skip the putchar()
188 * and go back to the top of the loop, without reading
189 * a new character. */
192 /* My small mind cannot fathom backspaces and UTF-8 */
197 } while (*argv && *++argv);
199 setTermSettings(cin_fileno, &initial_settings);