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 | * |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | * General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 32 | #include <signal.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 33 | #include <stdlib.h> |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 35 | #include <sys/ioctl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 36 | #include "busybox.h" |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 37 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 38 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 39 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 40 | static int cin_fileno; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 41 | #include <termios.h> |
| 42 | #define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 43 | #define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 44 | |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 45 | static struct termios initial_settings, new_settings; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 46 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 47 | static void set_tty_to_initial_mode(void) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | { |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 49 | setTermSettings(cin_fileno, &initial_settings); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void gotsig(int sig) |
| 53 | { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 54 | putchar('\n'); |
| 55 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 57 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 59 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 60 | int more_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | { |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 62 | int c, lines, input = 0; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 63 | int please_display_more_prompt = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | struct stat st; |
| 65 | FILE *file; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 66 | FILE *cin; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 67 | int len, page_height; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 68 | int terminal_width; |
| 69 | int terminal_height; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | argc--; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 72 | argv++; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 73 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 74 | |
| 75 | /* not use inputing from terminal if usage: more > outfile */ |
Eric Andersen | 70060d2 | 2004-03-27 10:02:48 +0000 | [diff] [blame] | 76 | if(isatty(STDOUT_FILENO)) { |
Matt Kraai | 439e3df | 2001-07-23 14:52:08 +0000 | [diff] [blame] | 77 | cin = fopen(CURRENT_TTY, "r"); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 78 | if (!cin) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | cin = bb_xfopen(CONSOLE_DEV, "r"); |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 80 | please_display_more_prompt = 2; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 81 | #ifdef CONFIG_FEATURE_USE_TERMIOS |
Rob Landley | c44bc98 | 2006-05-28 01:19:06 +0000 | [diff] [blame^] | 82 | cin_fileno = fileno(cin); |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 83 | getTermSettings(cin_fileno, &initial_settings); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 84 | new_settings = initial_settings; |
| 85 | new_settings.c_lflag &= ~ICANON; |
| 86 | new_settings.c_lflag &= ~ECHO; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 87 | new_settings.c_cc[VMIN] = 1; |
| 88 | new_settings.c_cc[VTIME] = 0; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 89 | setTermSettings(cin_fileno, &new_settings); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 90 | atexit(set_tty_to_initial_mode); |
| 91 | (void) signal(SIGINT, gotsig); |
| 92 | (void) signal(SIGQUIT, gotsig); |
| 93 | (void) signal(SIGTERM, gotsig); |
| 94 | #endif |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 95 | } else { |
| 96 | cin = stdin; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | do { |
| 100 | if (argc == 0) { |
| 101 | file = stdin; |
| 102 | } else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | file = bb_wfopen(*argv, "r"); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 104 | if(file==0) |
| 105 | goto loop; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 106 | |
Eric Andersen | 0ee0a8d | 2001-12-06 07:24:29 +0000 | [diff] [blame] | 107 | st.st_size = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | fstat(fileno(file), &st); |
| 109 | |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 110 | please_display_more_prompt &= ~1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | |
Rob Landley | c44bc98 | 2006-05-28 01:19:06 +0000 | [diff] [blame^] | 112 | get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height); |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 113 | if (terminal_height > 4) |
| 114 | terminal_height -= 2; |
| 115 | if (terminal_width > 0) |
| 116 | terminal_width -= 1; |
| 117 | |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 118 | len=0; |
| 119 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 120 | page_height = terminal_height; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | while ((c = getc(file)) != EOF) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 123 | if ((please_display_more_prompt & 3) == 3) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 124 | len = printf("--More-- "); |
Eric Andersen | 0ee0a8d | 2001-12-06 07:24:29 +0000 | [diff] [blame] | 125 | if (file != stdin && st.st_size > 0) { |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 126 | #if _FILE_OFFSET_BITS == 64 |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 127 | len += printf("(%d%% of %lld bytes)", |
Eric Andersen | 250a221 | 2001-04-05 23:26:44 +0000 | [diff] [blame] | 128 | (int) (100 * ((double) ftell(file) / |
| 129 | (double) st.st_size)), (long long)st.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 130 | #else |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 131 | len += printf("(%d%% of %ld bytes)", |
Eric Andersen | 250a221 | 2001-04-05 23:26:44 +0000 | [diff] [blame] | 132 | (int) (100 * ((double) ftell(file) / |
| 133 | (double) st.st_size)), (long)st.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 134 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | |
| 137 | fflush(stdout); |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * We've just displayed the "--More--" prompt, so now we need |
| 141 | * to get input from the user. |
| 142 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 143 | input = getc(cin); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 144 | #ifndef CONFIG_FEATURE_USE_TERMIOS |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 145 | printf("\033[A"); /* up cursor */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 146 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | /* Erase the "More" message */ |
Eric Andersen | aa21e0f | 2001-04-06 16:02:22 +0000 | [diff] [blame] | 148 | putc('\r', stdout); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 149 | while (--len >= 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | putc(' ', stdout); |
Eric Andersen | aa21e0f | 2001-04-06 16:02:22 +0000 | [diff] [blame] | 151 | putc('\r', stdout); |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 152 | len=0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 153 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 154 | page_height = terminal_height; |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 155 | please_display_more_prompt &= ~1; |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 156 | |
| 157 | if (input == 'q') |
| 158 | goto end; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 159 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 160 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 161 | /* |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 162 | * There are two input streams to worry about here: |
| 163 | * |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 164 | * c : the character we are reading from the file being "mored" |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 165 | * input : a character received from the keyboard |
| 166 | * |
| 167 | * If we hit a newline in the _file_ stream, we want to test and |
| 168 | * see if any characters have been hit in the _input_ stream. This |
| 169 | * allows the user to quit while in the middle of a file. |
| 170 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | if (c == '\n') { |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 172 | /* increment by just one line if we are at |
| 173 | * the end of this line */ |
| 174 | if (input == '\n') |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 175 | please_display_more_prompt |= 1; |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 176 | /* Adjust the terminal height for any overlap, so that |
| 177 | * no lines get lost off the top. */ |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 178 | if (len >= terminal_width) { |
Eric Andersen | 2439a59 | 2001-05-16 18:53:34 +0000 | [diff] [blame] | 179 | int quot, rem; |
| 180 | quot = len / terminal_width; |
| 181 | rem = len - (quot * terminal_width); |
| 182 | if (quot) { |
| 183 | if (rem) |
| 184 | page_height-=quot; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 185 | else |
Eric Andersen | 2439a59 | 2001-05-16 18:53:34 +0000 | [diff] [blame] | 186 | page_height-=(quot-1); |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 187 | } |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 188 | } |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 189 | if (++lines >= page_height) { |
Eric Andersen | edd580a | 2004-03-27 09:49:57 +0000 | [diff] [blame] | 190 | please_display_more_prompt |= 1; |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 191 | } |
| 192 | len=0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 193 | } |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 194 | /* |
| 195 | * If we just read a newline from the file being 'mored' and any |
| 196 | * key other than a return is hit, scroll by one page |
| 197 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | putc(c, stdout); |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 199 | len++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | } |
| 201 | fclose(file); |
| 202 | fflush(stdout); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 203 | loop: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | argv++; |
| 205 | } while (--argc > 0); |
| 206 | end: |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 207 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | } |