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" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 34 | #define BB_DECLARE_EXTERN |
| 35 | #define bb_need_help |
| 36 | #include "messages.c" |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 37 | |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 38 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 39 | #ifdef BB_FEATURE_USE_TERMIOS |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 40 | # if #cpu(sparc) |
| 41 | # include <termio.h> |
| 42 | # define termios termio |
| 43 | # define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) |
| 44 | # define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) |
| 45 | # else |
| 46 | # include <termios.h> |
| 47 | # define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 48 | # define getTermSettings(fd,argp) tcgetattr(fd, argp); |
| 49 | # endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 50 | |
Eric Andersen | 5c9c8b4 | 2001-01-26 06:50:46 +0000 | [diff] [blame] | 51 | static FILE *cin; |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 52 | |
Eric Andersen | 63a8622 | 2000-11-07 06:52:13 +0000 | [diff] [blame] | 53 | static struct termios initial_settings, new_settings; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 54 | |
Matt Kraai | e6e8183 | 2000-12-30 07:46:23 +0000 | [diff] [blame] | 55 | static void gotsig(int sig) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | { |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 57 | setTermSettings(fileno(cin), &initial_settings); |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 58 | putchar('\n'); |
| 59 | exit(EXIT_FAILURE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | } |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 61 | #endif /* BB_FEATURE_USE_TERMIOS */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 62 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 63 | |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 64 | static int terminal_width = 79; /* not 80 in case terminal has linefold bug */ |
| 65 | static int terminal_height = 24; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 66 | |
| 67 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 68 | extern int more_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 69 | { |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 70 | int c, lines, input = 0; |
| 71 | int please_display_more_prompt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | struct stat st; |
| 73 | FILE *file; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 74 | int len, page_height; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 75 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 76 | #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS |
Pavel Roskin | f626dcb | 2000-07-14 15:55:41 +0000 | [diff] [blame] | 77 | struct winsize win = { 0, 0, 0, 0 }; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 78 | #endif |
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 | argc--; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 81 | argv++; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 82 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | do { |
| 84 | if (argc == 0) { |
| 85 | file = stdin; |
| 86 | } else |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 87 | file = xfopen(*argv, "r"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 89 | fstat(fileno(file), &st); |
| 90 | |
| 91 | #ifdef BB_FEATURE_USE_TERMIOS |
| 92 | cin = fopen("/dev/tty", "r"); |
| 93 | if (!cin) |
| 94 | cin = fopen("/dev/console", "r"); |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 95 | getTermSettings(fileno(cin), &initial_settings); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | new_settings = initial_settings; |
Erik Andersen | e90f404 | 2000-04-21 21:53:58 +0000 | [diff] [blame] | 97 | new_settings.c_cc[VMIN] = 1; |
| 98 | new_settings.c_cc[VTIME] = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | new_settings.c_lflag &= ~ICANON; |
| 100 | new_settings.c_lflag &= ~ECHO; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 101 | setTermSettings(fileno(cin), &new_settings); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 103 | # ifdef BB_FEATURE_AUTOWIDTH |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | ioctl(fileno(stdout), TIOCGWINSZ, &win); |
| 105 | if (win.ws_row > 4) |
| 106 | terminal_height = win.ws_row - 2; |
| 107 | if (win.ws_col > 0) |
| 108 | terminal_width = win.ws_col - 1; |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 109 | # endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | |
| 111 | (void) signal(SIGINT, gotsig); |
| 112 | (void) signal(SIGQUIT, gotsig); |
| 113 | (void) signal(SIGTERM, gotsig); |
| 114 | |
| 115 | #endif |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 116 | len=0; |
| 117 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 118 | page_height = terminal_height; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 119 | please_display_more_prompt = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | while ((c = getc(file)) != EOF) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 121 | |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 122 | if (please_display_more_prompt) { |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 123 | len = printf("--More-- "); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | if (file != stdin) { |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 125 | #if _FILE_OFFSET_BITS == 64 |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 126 | len += printf("(%d%% of %lld bytes)", |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 127 | #else |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 128 | len += printf("(%d%% of %ld bytes)", |
Eric Andersen | 8a2e56c | 2000-09-21 02:23:30 +0000 | [diff] [blame] | 129 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 130 | (int) (100 * |
| 131 | ((double) ftell(file) / |
| 132 | (double) st.st_size)), |
| 133 | st.st_size); |
| 134 | } |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 135 | len += printf("%s", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 136 | #ifdef BB_FEATURE_USE_TERMIOS |
| 137 | "" |
| 138 | #else |
| 139 | "\n" |
| 140 | #endif |
| 141 | ); |
| 142 | |
| 143 | fflush(stdout); |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * We've just displayed the "--More--" prompt, so now we need |
| 147 | * to get input from the user. |
| 148 | */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 149 | #ifdef BB_FEATURE_USE_TERMIOS |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | input = getc(cin); |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 151 | #else |
| 152 | input = getc(stdin); |
| 153 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | |
| 155 | #ifdef BB_FEATURE_USE_TERMIOS |
| 156 | /* Erase the "More" message */ |
| 157 | while (--len >= 0) |
| 158 | putc('\b', stdout); |
| 159 | while (++len <= terminal_width) |
| 160 | putc(' ', stdout); |
| 161 | while (--len >= 0) |
| 162 | putc('\b', stdout); |
| 163 | fflush(stdout); |
| 164 | #endif |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 165 | len=0; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 166 | lines = 0; |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 167 | page_height = terminal_height; |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 168 | please_display_more_prompt = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | } |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * There are two input streams to worry about here: |
| 173 | * |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 174 | * c : the character we are reading from the file being "mored" |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 175 | * input : a character received from the keyboard |
| 176 | * |
| 177 | * If we hit a newline in the _file_ stream, we want to test and |
| 178 | * see if any characters have been hit in the _input_ stream. This |
| 179 | * allows the user to quit while in the middle of a file. |
| 180 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | if (c == '\n') { |
| 182 | switch (input) { |
| 183 | case 'q': |
| 184 | goto end; |
| 185 | case '\n': |
| 186 | /* increment by just one line if we are at |
| 187 | * the end of this line*/ |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 188 | please_display_more_prompt = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | break; |
| 190 | } |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 191 | /* Adjust the terminal height for any overlap, so that |
| 192 | * no lines get lost off the top. */ |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 193 | if (len >= terminal_width) { |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 194 | div_t result = div( len, terminal_width); |
Eric Andersen | ffc40bf | 2001-02-22 21:49:32 +0000 | [diff] [blame] | 195 | if (result.quot) { |
| 196 | if (result.rem) |
| 197 | page_height-=result.quot; |
| 198 | else |
| 199 | page_height-=(result.quot-1); |
| 200 | } |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 201 | } |
Eric Andersen | 5723934 | 2001-02-23 01:39:26 +0000 | [diff] [blame^] | 202 | if (++lines >= page_height) { |
Mark Whitley | b991395 | 2000-06-16 00:26:51 +0000 | [diff] [blame] | 203 | please_display_more_prompt = 1; |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 204 | } |
| 205 | len=0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | } |
Mark Whitley | 4fa84e6 | 2000-06-21 22:53:16 +0000 | [diff] [blame] | 207 | /* |
| 208 | * If we just read a newline from the file being 'mored' and any |
| 209 | * key other than a return is hit, scroll by one page |
| 210 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | putc(c, stdout); |
Eric Andersen | be30a6b | 2001-02-22 00:22:46 +0000 | [diff] [blame] | 212 | len++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 213 | } |
| 214 | fclose(file); |
| 215 | fflush(stdout); |
| 216 | |
| 217 | argv++; |
| 218 | } while (--argc > 0); |
| 219 | end: |
| 220 | #ifdef BB_FEATURE_USE_TERMIOS |
| 221 | gotsig(0); |
| 222 | #endif |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 223 | return(TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 224 | } |