Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini more implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. |
| 6 | * |
| 7 | * Latest version blended together by Erik Andersen <andersen@lineo.com>, |
| 8 | * based on the original more implementation by Bruce, and code from the |
| 9 | * Debian boot-floppies team. |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
| 25 | */ |
| 26 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | #include "internal.h" |
| 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 | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 32 | |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 33 | static const char more_usage[] = "more [file ...]\n"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +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 |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 37 | |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 38 | #if #cpu(sparc) |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 39 | # define USE_OLD_TERMIO |
| 40 | # include <termio.h> |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 41 | # define termios termio |
| 42 | # define stty(fd,argp) ioctl(fd,TCSETAF,argp) |
| 43 | #else |
| 44 | # include <termios.h> |
| 45 | # define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 46 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 47 | |
| 48 | FILE *cin; |
| 49 | struct termios initial_settings, new_settings; |
| 50 | |
| 51 | void gotsig(int sig) { |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 52 | stty(fileno(cin), &initial_settings); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 53 | fprintf(stdout, "\n"); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 54 | exit( TRUE); |
| 55 | } |
| 56 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | #define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */ |
| 61 | #define TERMINAL_HEIGHT 24 |
| 62 | |
| 63 | |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 64 | #if defined BB_FEATURE_AUTOWIDTH |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 65 | static int terminal_width = 0, terminal_height = 0; |
| 66 | #else |
| 67 | #define terminal_width TERMINAL_WIDTH |
| 68 | #define terminal_height TERMINAL_HEIGHT |
| 69 | #endif |
| 70 | |
| 71 | |
| 72 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 73 | extern int more_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 74 | { |
Eric Andersen | 21943ce | 1999-10-13 18:04:51 +0000 | [diff] [blame] | 75 | int c, lines=0, input=0; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 76 | int next_page=0; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 77 | struct stat st; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 78 | FILE *file; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 79 | #ifdef BB_FEATURE_AUTOWIDTH |
Eric Andersen | 96bcfd3 | 1999-11-12 01:30:18 +0000 | [diff] [blame] | 80 | struct winsize win = {0,0}; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 81 | #endif |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 82 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 83 | argc--; |
| 84 | argv++; |
| 85 | |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 86 | if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) { |
| 87 | usage (more_usage); |
| 88 | } |
| 89 | do { |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 90 | if (argc==0) { |
| 91 | file = stdin; |
| 92 | } |
| 93 | else |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 94 | file = fopen(*argv, "r"); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 95 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 96 | if (file == NULL) { |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 97 | perror(*argv); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 98 | exit(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 99 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 100 | fstat(fileno(file), &st); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 102 | #ifdef BB_FEATURE_USE_TERMIOS |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 103 | cin = fopen("/dev/tty", "r"); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 104 | if (!cin) |
| 105 | cin = fopen("/dev/console", "r"); |
| 106 | #ifdef USE_OLD_TERMIO |
| 107 | ioctl(fileno(cin),TCGETA,&initial_settings); |
| 108 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 109 | tcgetattr(fileno(cin),&initial_settings); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 110 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 111 | new_settings = initial_settings; |
| 112 | new_settings.c_lflag &= ~ICANON; |
| 113 | new_settings.c_lflag &= ~ECHO; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 114 | stty(fileno(cin), &new_settings); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 115 | |
| 116 | #ifdef BB_FEATURE_AUTOWIDTH |
Eric Andersen | 08b1034 | 1999-11-19 02:38:58 +0000 | [diff] [blame] | 117 | ioctl(fileno(stdout), TIOCGWINSZ, &win); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 118 | if (win.ws_row > 4) |
| 119 | terminal_height = win.ws_row - 2; |
| 120 | if (win.ws_col > 0) |
| 121 | terminal_width = win.ws_col - 1; |
| 122 | #endif |
| 123 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 124 | (void) signal(SIGINT, gotsig); |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 125 | (void) signal(SIGQUIT, gotsig); |
| 126 | (void) signal(SIGTERM, gotsig); |
| 127 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 128 | #endif |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 129 | while ((c = getc(file)) != EOF) { |
| 130 | if ( next_page ) { |
| 131 | int len=0; |
| 132 | next_page = 0; |
| 133 | lines=0; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 134 | len = fprintf(stdout, "--More-- "); |
| 135 | if (file != stdin) { |
| 136 | len += fprintf(stdout, "(%d%% of %ld bytes)", |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 137 | (int) (100*( (double) ftell(file) / (double) st.st_size )), |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 138 | st.st_size); |
| 139 | } |
| 140 | len += fprintf(stdout, "%s", |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 141 | #ifdef BB_FEATURE_USE_TERMIOS |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 142 | "" |
| 143 | #else |
| 144 | "\n" |
| 145 | #endif |
| 146 | ); |
| 147 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 148 | fflush(stdout); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 149 | input = getc( cin); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 150 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 151 | #ifdef BB_FEATURE_USE_TERMIOS |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 152 | /* Erase the "More" message */ |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 153 | while(--len >= 0) |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 154 | putc('\b', stdout); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 155 | while(++len <= terminal_width) |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 156 | putc(' ', stdout); |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 157 | while(--len >= 0) |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 158 | putc('\b', stdout); |
| 159 | fflush(stdout); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 161 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 162 | } |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 163 | if (c == '\n' ) { |
| 164 | switch(input) { |
| 165 | case 'q': |
| 166 | goto end; |
| 167 | case '\n': |
| 168 | /* increment by just one line if we are at |
| 169 | * the end of this line*/ |
| 170 | next_page = 1; |
| 171 | break; |
| 172 | } |
| 173 | if ( ++lines == terminal_height ) |
| 174 | next_page = 1; |
| 175 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 176 | putc(c, stdout); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 177 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 178 | fclose(file); |
| 179 | fflush(stdout); |
| 180 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 181 | argv++; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 182 | } while (--argc > 0); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 183 | end: |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 184 | #ifdef BB_FEATURE_USE_TERMIOS |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 185 | gotsig(0); |
| 186 | #endif |
| 187 | exit(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 188 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 189 | |