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