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