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 | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 6 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 7 | * |
| 8 | * Latest version blended together by Erik Andersen <andersen@lineo.com>, |
| 9 | * based on the original more implementation by Bruce, and code from the |
| 10 | * Debian boot-floppies team. |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | * General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | */ |
| 27 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 29 | #include <fcntl.h> |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 30 | #include <signal.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 32 | #include <sys/ioctl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 34 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 35 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 36 | #ifdef BB_FEATURE_USE_TERMIOS |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 37 | # if #cpu(sparc) |
| 38 | # include <termio.h> |
| 39 | # define termios termio |
| 40 | # define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) |
| 41 | # define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) |
| 42 | # else |
| 43 | # include <termios.h> |
| 44 | # define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 45 | # define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 46 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 5c9c8b4 | 2001-01-26 06:50:46 +0000 | [diff] [blame] | 48 | static FILE *cin; |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 50 | static struct termios initial_settings, new_settings; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 51 | |
Matt Kraai | e6e8183 | 2000-12-30 07:46:23 +0000 | [diff] [blame] | 52 | static void gotsig(int sig) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | { |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 54 | setTermSettings(fileno(cin), &initial_settings); |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 55 | putchar('\n'); |
| 56 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | } |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 58 | #endif /* BB_FEATURE_USE_TERMIOS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 60 | |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 61 | static int terminal_width = 79; /* not 80 in case terminal has linefold bug */ |
| 62 | static int terminal_height = 24; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 63 | |
| 64 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 65 | extern int more_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 66 | { |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 67 | int c, lines, input = 0; |
| 68 | int please_display_more_prompt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | struct stat st; |
| 70 | FILE *file; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 71 | int len, page_height; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 73 | #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS |
Pavel Roskin | f626dcb | 2000-07-14 15:55:41 +0000 | [diff] [blame] | 74 | struct winsize win = { 0, 0, 0, 0 }; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 75 | #endif |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | argc--; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 78 | argv++; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 79 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | do { |
| 81 | if (argc == 0) { |
| 82 | file = stdin; |
| 83 | } else |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 84 | file = xfopen(*argv, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | fstat(fileno(file), &st); |
| 87 | |
| 88 | #ifdef BB_FEATURE_USE_TERMIOS |
| 89 | cin = fopen("/dev/tty", "r"); |
| 90 | if (!cin) |
Eric Andersen | 21af752 | 2001-03-23 17:09:15 +0000 | [diff] [blame] | 91 | cin = xfopen("/dev/console", "r"); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 92 | getTermSettings(fileno(cin), &initial_settings); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | new_settings = initial_settings; |
Erik Andersen | e90f404 | 2000-04-21 21:53:58 +0000 | [diff] [blame] | 94 | new_settings.c_cc[VMIN] = 1; |
| 95 | new_settings.c_cc[VTIME] = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | new_settings.c_lflag &= ~ICANON; |
| 97 | new_settings.c_lflag &= ~ECHO; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 98 | setTermSettings(fileno(cin), &new_settings); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 100 | # ifdef BB_FEATURE_AUTOWIDTH |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 101 | ioctl(fileno(stdout), TIOCGWINSZ, &win); |
| 102 | if (win.ws_row > 4) |
| 103 | terminal_height = win.ws_row - 2; |
| 104 | if (win.ws_col > 0) |
| 105 | terminal_width = win.ws_col - 1; |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 106 | # endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | |
| 108 | (void) signal(SIGINT, gotsig); |
| 109 | (void) signal(SIGQUIT, gotsig); |
| 110 | (void) signal(SIGTERM, gotsig); |
| 111 | |
| 112 | #endif |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 113 | len=0; |
| 114 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 115 | page_height = terminal_height; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 116 | please_display_more_prompt = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | while ((c = getc(file)) != EOF) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 119 | if (please_display_more_prompt) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 120 | len = printf("--More-- "); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | if (file != stdin) { |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 122 | #if _FILE_OFFSET_BITS == 64 |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 123 | len += printf("(%d%% of %lld bytes)", |
Eric Andersen | 250a221 | 2001-04-05 23:26:44 +0000 | [diff] [blame] | 124 | (int) (100 * ((double) ftell(file) / |
| 125 | (double) st.st_size)), (long long)st.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 126 | #else |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 127 | len += printf("(%d%% of %ld 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)st.st_size); |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 130 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | } |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 132 | len += printf("%s", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | #ifdef BB_FEATURE_USE_TERMIOS |
| 134 | "" |
| 135 | #else |
| 136 | "\n" |
| 137 | #endif |
| 138 | ); |
| 139 | |
| 140 | fflush(stdout); |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * We've just displayed the "--More--" prompt, so now we need |
| 144 | * to get input from the user. |
| 145 | */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 146 | #ifdef BB_FEATURE_USE_TERMIOS |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | input = getc(cin); |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 148 | #else |
| 149 | input = getc(stdin); |
| 150 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef BB_FEATURE_USE_TERMIOS |
| 153 | /* Erase the "More" message */ |
Eric Andersen | aa21e0f | 2001-04-06 16:02:22 +0000 | [diff] [blame] | 154 | putc('\r', stdout); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | while (--len >= 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | putc(' ', stdout); |
Eric Andersen | aa21e0f | 2001-04-06 16:02:22 +0000 | [diff] [blame] | 157 | putc('\r', stdout); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | fflush(stdout); |
| 159 | #endif |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 160 | len=0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 161 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 162 | page_height = terminal_height; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 163 | please_display_more_prompt = 0; |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 164 | |
| 165 | if (input == 'q') |
| 166 | goto end; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * There are two input streams to worry about here: |
| 171 | * |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 172 | * c : the character we are reading from the file being "mored" |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 173 | * input : a character received from the keyboard |
| 174 | * |
| 175 | * If we hit a newline in the _file_ stream, we want to test and |
| 176 | * see if any characters have been hit in the _input_ stream. This |
| 177 | * allows the user to quit while in the middle of a file. |
| 178 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 179 | if (c == '\n') { |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 180 | /* increment by just one line if we are at |
| 181 | * the end of this line */ |
| 182 | if (input == '\n') |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 183 | please_display_more_prompt = 1; |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 184 | /* Adjust the terminal height for any overlap, so that |
| 185 | * no lines get lost off the top. */ |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 186 | if (len >= terminal_width) { |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 187 | div_t result = div( len, terminal_width); |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 188 | if (result.quot) { |
| 189 | if (result.rem) |
| 190 | page_height-=result.quot; |
| 191 | else |
| 192 | page_height-=(result.quot-1); |
| 193 | } |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame] | 195 | if (++lines >= page_height) { |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 196 | please_display_more_prompt = 1; |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 197 | } |
| 198 | len=0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | } |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 200 | /* |
| 201 | * If we just read a newline from the file being 'mored' and any |
| 202 | * key other than a return is hit, scroll by one page |
| 203 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | putc(c, stdout); |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 205 | len++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | } |
| 207 | fclose(file); |
| 208 | fflush(stdout); |
| 209 | |
| 210 | argv++; |
| 211 | } while (--argc > 0); |
| 212 | end: |
| 213 | #ifdef BB_FEATURE_USE_TERMIOS |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 214 | setTermSettings(fileno(cin), &initial_settings); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | #endif |
Matt Kraai | d6cde0b | 2001-04-12 20:51:01 +0000 | [diff] [blame] | 216 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | } |