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