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 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 16 | //config:config MORE |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 17 | //config: bool "more (7 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 18 | //config: default y |
| 19 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 25 | |
| 26 | //applet:IF_MORE(APPLET(more, BB_DIR_BIN, BB_SUID_DROP)) |
| 27 | |
| 28 | //kbuild:lib-$(CONFIG_MORE) += more.o |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 29 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 38 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 39 | #include "common_bufsiz.h" |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 40 | |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 41 | struct globals { |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 42 | int tty_fileno; |
| 43 | unsigned terminal_width; |
| 44 | unsigned terminal_height; |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 45 | struct termios initial_settings; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 46 | } FIX_ALIASING; |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 47 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 48 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 49 | |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 50 | static void get_wh(void) |
| 51 | { |
| 52 | /* never returns w, h <= 1 */ |
| 53 | get_terminal_width_height(G.tty_fileno, &G.terminal_width, &G.terminal_height); |
| 54 | G.terminal_height -= 1; |
| 55 | } |
| 56 | |
| 57 | static void tcsetattr_tty_TCSANOW(struct termios *settings) |
| 58 | { |
| 59 | tcsetattr(G.tty_fileno, TCSANOW, settings); |
| 60 | } |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 62 | static void gotsig(int sig UNUSED_PARAM) |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 63 | { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 64 | /* bb_putchar_stderr doesn't use stdio buffering, |
| 65 | * therefore it is safe in signal handler */ |
| 66 | bb_putchar_stderr('\n'); |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 67 | tcsetattr_tty_TCSANOW(&G.initial_settings); |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 68 | _exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | } |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 71 | #define CONVERTED_TAB_SIZE 8 |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 73 | int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 74 | int more_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | { |
Denys Vlasenko | d8b989f | 2010-07-12 03:14:17 +0200 | [diff] [blame] | 76 | int c = c; /* for compiler */ |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 77 | int input = 0; |
| 78 | int spaces = 0; |
| 79 | int please_display_more_prompt; |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 80 | FILE *tty; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 82 | INIT_G(); |
| 83 | |
Denys Vlasenko | 713b513 | 2016-10-11 15:29:38 +0200 | [diff] [blame] | 84 | /* Parse options */ |
| 85 | /* Accepted but ignored: */ |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 86 | /* -d Display help instead of ringing bell */ |
Natanael Copa | 3636d52 | 2022-11-21 14:09:57 +0100 | [diff] [blame] | 87 | /* -e Exit immediately after writing the last line */ |
Denys Vlasenko | 713b513 | 2016-10-11 15:29:38 +0200 | [diff] [blame] | 88 | /* -f Count logical lines (IOW: long lines are not folded) */ |
| 89 | /* -l Do not pause after any line containing a ^L (form feed) */ |
| 90 | /* -s Squeeze blank lines into one */ |
| 91 | /* -u Suppress underlining */ |
Natanael Copa | 3636d52 | 2022-11-21 14:09:57 +0100 | [diff] [blame] | 92 | getopt32(argv, "deflsu"); |
Denys Vlasenko | 713b513 | 2016-10-11 15:29:38 +0200 | [diff] [blame] | 93 | argv += optind; |
| 94 | |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 95 | /* Another popular pager, most, detects when stdout |
| 96 | * is not a tty and turns into cat. This makes sense. */ |
| 97 | if (!isatty(STDOUT_FILENO)) |
| 98 | return bb_cat(argv); |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 99 | tty = fopen_for_read(CURRENT_TTY); |
| 100 | if (!tty) |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 101 | return bb_cat(argv); |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 102 | |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 103 | G.tty_fileno = fileno(tty); |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 104 | |
| 105 | /* Turn on unbuffered input; turn off echoing */ |
| 106 | set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0); |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 107 | bb_signals(BB_FATAL_SIGS, gotsig); |
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 { |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 110 | struct stat st; |
| 111 | FILE *file; |
| 112 | int len; |
| 113 | int lines; |
| 114 | |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 115 | file = stdin; |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 116 | if (*argv) { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 117 | file = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 118 | if (!file) |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 119 | continue; |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 120 | } |
Eric Andersen | 0ee0a8d | 2001-12-06 07:24:29 +0000 | [diff] [blame] | 121 | st.st_size = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | fstat(fileno(file), &st); |
| 123 | |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 124 | get_wh(); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 125 | |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 126 | please_display_more_prompt = 0; |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 127 | len = 0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 128 | lines = 0; |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 129 | for (;;) { |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 130 | int wrap; |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 131 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 132 | if (spaces) |
| 133 | spaces--; |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 134 | else { |
| 135 | c = getc(file); |
| 136 | if (c == EOF) break; |
| 137 | } |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 138 | loop_top: |
| 139 | if (input != 'r' && please_display_more_prompt) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 140 | len = printf("--More-- "); |
Denys Vlasenko | 82e785c | 2011-03-07 02:01:52 +0100 | [diff] [blame] | 141 | if (st.st_size != 0) { |
| 142 | uoff_t d = (uoff_t)st.st_size / 100; |
| 143 | if (d == 0) |
| 144 | d = 1; |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 145 | len += printf("(%u%% of %"OFF_FMT"u bytes)", |
Denys Vlasenko | 82e785c | 2011-03-07 02:01:52 +0100 | [diff] [blame] | 146 | (int) ((uoff_t)ftello(file) / d), |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 147 | st.st_size); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 148 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * We've just displayed the "--More--" prompt, so now we need |
| 152 | * to get input from the user. |
| 153 | */ |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 154 | for (;;) { |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 155 | fflush_all(); |
| 156 | input = getc(tty); |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 157 | input = tolower(input); |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 158 | /* Erase the last message */ |
| 159 | printf("\r%*s\r", len, ""); |
| 160 | |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 161 | if (input == 'q') |
| 162 | goto end; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 163 | /* Due to various multibyte escape |
| 164 | * sequences, it's not ok to accept |
| 165 | * any input as a command to scroll |
| 166 | * the screen. We only allow known |
| 167 | * commands, else we show help msg. */ |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 168 | if (input == ' ' || input == '\n' || input == 'r') |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 169 | break; |
| 170 | len = printf("(Enter:next line Space:next page Q:quit R:show the rest)"); |
| 171 | } |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 172 | len = 0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 173 | lines = 0; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 174 | please_display_more_prompt = 0; |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 176 | /* The user may have resized the terminal. |
| 177 | * Re-read the dimensions. */ |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 178 | get_wh(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 179 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 181 | /* Crudely convert tabs into spaces, which are |
| 182 | * a bajillion times easier to deal with. */ |
| 183 | if (c == '\t') { |
Denys Vlasenko | 82e785c | 2011-03-07 02:01:52 +0100 | [diff] [blame] | 184 | spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 185 | c = ' '; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 186 | } |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 187 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 188 | /* |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 189 | * There are two input streams to worry about here: |
| 190 | * |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 191 | * c : the character we are reading from the file being "mored" |
| 192 | * input: a character received from the keyboard |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 193 | * |
| 194 | * If we hit a newline in the _file_ stream, we want to test and |
| 195 | * see if any characters have been hit in the _input_ stream. This |
| 196 | * allows the user to quit while in the middle of a file. |
| 197 | */ |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 198 | wrap = (++len > G.terminal_width); |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 199 | if (c == '\n' || wrap) { |
| 200 | /* Then outputting this character |
| 201 | * will move us to a new line. */ |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 202 | if (++lines >= G.terminal_height || input == '\n') |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 203 | please_display_more_prompt = 1; |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 204 | len = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | } |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 206 | if (c != '\n' && wrap) { |
| 207 | /* Then outputting this will also put a character on |
| 208 | * the beginning of that new line. Thus we first want to |
| 209 | * display the prompt (if any), so we skip the putchar() |
| 210 | * and go back to the top of the loop, without reading |
| 211 | * a new character. */ |
| 212 | goto loop_top; |
| 213 | } |
| 214 | /* My small mind cannot fathom backspaces and UTF-8 */ |
| 215 | putchar(c); |
Denys Vlasenko | 0d1d0f6 | 2010-12-13 14:31:59 +0100 | [diff] [blame] | 216 | die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | } |
| 218 | fclose(file); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 219 | fflush_all(); |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 220 | } while (*argv && *++argv); |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 221 | end: |
Denys Vlasenko | c5891fe | 2017-01-11 10:38:52 +0100 | [diff] [blame] | 222 | tcsetattr_tty_TCSANOW(&G.initial_settings); |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 223 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | } |