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 | * |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file License in this tarball for details. |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 17 | #include "libbb.h" |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 18 | #if ENABLE_FEATURE_USE_TERMIOS |
| 19 | #include <termios.h> |
| 20 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 21 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 22 | |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 23 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 24 | |
| 25 | struct globals { |
| 26 | int cin_fileno; |
| 27 | struct termios initial_settings; |
| 28 | struct termios new_settings; |
| 29 | }; |
| 30 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 31 | #define INIT_G() ((void)0) |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 32 | #define initial_settings (G.initial_settings) |
| 33 | #define new_settings (G.new_settings ) |
| 34 | #define cin_fileno (G.cin_fileno ) |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 36 | #define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp) |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 37 | #define getTermSettings(fd, argp) tcgetattr(fd, argp) |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 39 | static void gotsig(int sig ATTRIBUTE_UNUSED) |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 40 | { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 41 | bb_putchar('\n'); |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 42 | setTermSettings(cin_fileno, &initial_settings); |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 43 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | } |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 45 | |
| 46 | #else /* !FEATURE_USE_TERMIOS */ |
| 47 | #define INIT_G() ((void)0) |
| 48 | #define setTermSettings(fd, argp) ((void)0) |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 49 | #endif /* FEATURE_USE_TERMIOS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 51 | #define CONVERTED_TAB_SIZE 8 |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 54 | int more_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 56 | int c = c; /* for gcc */ |
| 57 | int lines; |
| 58 | int input = 0; |
| 59 | int spaces = 0; |
| 60 | int please_display_more_prompt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | struct stat st; |
| 62 | FILE *file; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 63 | FILE *cin; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 64 | int len; |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 65 | unsigned terminal_width; |
| 66 | unsigned terminal_height; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 68 | INIT_G(); |
| 69 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 70 | argv++; |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 71 | /* Another popular pager, most, detects when stdout |
| 72 | * is not a tty and turns into cat. This makes sense. */ |
| 73 | if (!isatty(STDOUT_FILENO)) |
| 74 | return bb_cat(argv); |
| 75 | cin = fopen(CURRENT_TTY, "r"); |
| 76 | if (!cin) |
| 77 | return bb_cat(argv); |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 79 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 80 | cin_fileno = fileno(cin); |
| 81 | getTermSettings(cin_fileno, &initial_settings); |
| 82 | new_settings = initial_settings; |
| 83 | new_settings.c_lflag &= ~ICANON; |
| 84 | new_settings.c_lflag &= ~ECHO; |
| 85 | new_settings.c_cc[VMIN] = 1; |
| 86 | new_settings.c_cc[VTIME] = 0; |
| 87 | setTermSettings(cin_fileno, &new_settings); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 88 | bb_signals(0 |
| 89 | + (1 << SIGINT) |
| 90 | + (1 << SIGQUIT) |
| 91 | + (1 << SIGTERM) |
| 92 | , gotsig); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 93 | #endif |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 94 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 95 | do { |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 96 | file = stdin; |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 97 | if (*argv) { |
Denis Vlasenko | ddec5af | 2006-10-26 23:25:17 +0000 | [diff] [blame] | 98 | file = fopen_or_warn(*argv, "r"); |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 99 | if (!file) |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 100 | continue; |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 101 | } |
Eric Andersen | 0ee0a8d | 2001-12-06 07:24:29 +0000 | [diff] [blame] | 102 | st.st_size = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 103 | fstat(fileno(file), &st); |
| 104 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 105 | please_display_more_prompt = 0; |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 106 | /* never returns w, h <= 1 */ |
Rob Landley | c44bc98 | 2006-05-28 01:19:06 +0000 | [diff] [blame] | 107 | get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height); |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 108 | terminal_height -= 1; |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 110 | len = 0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 111 | lines = 0; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 112 | while (spaces || (c = getc(file)) != EOF) { |
| 113 | int wrap; |
| 114 | if (spaces) |
| 115 | spaces--; |
| 116 | loop_top: |
| 117 | if (input != 'r' && please_display_more_prompt) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 118 | len = printf("--More-- "); |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 119 | if (st.st_size > 0) { |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 120 | len += printf("(%d%% of %"OFF_FMT"d bytes)", |
| 121 | (int) (ftello(file)*100 / st.st_size), |
| 122 | st.st_size); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | fflush(stdout); |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * We've just displayed the "--More--" prompt, so now we need |
| 128 | * to get input from the user. |
| 129 | */ |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 130 | for (;;) { |
| 131 | input = getc(cin); |
| 132 | input = tolower(input); |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 133 | #if !ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 134 | printf("\033[A"); /* up cursor */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 135 | #endif |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 136 | /* Erase the last message */ |
| 137 | printf("\r%*s\r", len, ""); |
| 138 | |
| 139 | /* Due to various multibyte escape |
| 140 | * sequences, it's not ok to accept |
| 141 | * any input as a command to scroll |
| 142 | * the screen. We only allow known |
| 143 | * commands, else we show help msg. */ |
| 144 | if (input == ' ' || input == '\n' || input == 'q' || input == 'r') |
| 145 | break; |
| 146 | len = printf("(Enter:next line Space:next page Q:quit R:show the rest)"); |
| 147 | } |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 148 | len = 0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 149 | lines = 0; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 150 | please_display_more_prompt = 0; |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 151 | |
| 152 | if (input == 'q') |
| 153 | goto end; |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 154 | |
| 155 | /* The user may have resized the terminal. |
| 156 | * Re-read the dimensions. */ |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 157 | #if ENABLE_FEATURE_USE_TERMIOS |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 158 | get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height); |
| 159 | terminal_height -= 1; |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 160 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 163 | /* Crudely convert tabs into spaces, which are |
| 164 | * a bajillion times easier to deal with. */ |
| 165 | if (c == '\t') { |
| 166 | spaces = CONVERTED_TAB_SIZE - 1; |
| 167 | c = ' '; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 168 | } |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 169 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 170 | /* |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 171 | * There are two input streams to worry about here: |
| 172 | * |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 173 | * c : the character we are reading from the file being "mored" |
| 174 | * input: a character received from the keyboard |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 175 | * |
| 176 | * If we hit a newline in the _file_ stream, we want to test and |
| 177 | * see if any characters have been hit in the _input_ stream. This |
| 178 | * allows the user to quit while in the middle of a file. |
| 179 | */ |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 180 | wrap = (++len > terminal_width); |
| 181 | if (c == '\n' || wrap) { |
| 182 | /* Then outputting this character |
| 183 | * will move us to a new line. */ |
| 184 | if (++lines >= terminal_height || input == '\n') |
| 185 | please_display_more_prompt = 1; |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 186 | len = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | } |
Denis Vlasenko | 033e592 | 2007-08-15 20:42:52 +0000 | [diff] [blame] | 188 | if (c != '\n' && wrap) { |
| 189 | /* Then outputting this will also put a character on |
| 190 | * the beginning of that new line. Thus we first want to |
| 191 | * display the prompt (if any), so we skip the putchar() |
| 192 | * and go back to the top of the loop, without reading |
| 193 | * a new character. */ |
| 194 | goto loop_top; |
| 195 | } |
| 196 | /* My small mind cannot fathom backspaces and UTF-8 */ |
| 197 | putchar(c); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | } |
| 199 | fclose(file); |
| 200 | fflush(stdout); |
Denis Vlasenko | 4eb8b93 | 2007-03-10 16:32:14 +0000 | [diff] [blame] | 201 | } while (*argv && *++argv); |
Denis Vlasenko | b15b7f7 | 2006-12-10 01:57:29 +0000 | [diff] [blame] | 202 | end: |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 203 | setTermSettings(cin_fileno, &initial_settings); |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 204 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | } |