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) */ |
| 28 | #define BB_MORE_TERM |
| 29 | |
| 30 | |
| 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include "internal.h" |
| 33 | #include <stdio.h> |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 34 | #include <fcntl.h> |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 35 | #include <signal.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 37 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 38 | static const char more_usage[] = "[file ...]"; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 40 | |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 41 | /* ED: sparc termios is broken: revert back to old termio handling. */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 42 | #ifdef BB_MORE_TERM |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | #if defined (__sparc__) |
| 46 | # define USE_OLD_TERMIO |
| 47 | # include <termio.h> |
| 48 | # include <sys/ioctl.h> |
| 49 | # define termios termio |
| 50 | # define stty(fd,argp) ioctl(fd,TCSETAF,argp) |
| 51 | #else |
| 52 | # include <termios.h> |
| 53 | # define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) |
| 54 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 55 | |
| 56 | FILE *cin; |
| 57 | struct termios initial_settings, new_settings; |
| 58 | |
| 59 | void gotsig(int sig) { |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 60 | stty(fileno(cin), &initial_settings); |
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 | 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 | 21943ce | 1999-10-13 18:04:51 +0000 | [diff] [blame] | 67 | int c, lines=0, input=0; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 68 | int next_page=0; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 69 | struct stat st; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 70 | FILE *file; |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 71 | |
| 72 | if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 73 | usage (more_usage); |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 74 | } |
| 75 | argc--; |
| 76 | argv++; |
| 77 | |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 78 | while (argc >= 0) { |
| 79 | if (argc==0) { |
| 80 | file = stdin; |
| 81 | } |
| 82 | else |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 83 | file = fopen(*argv, "r"); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 84 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 85 | if (file == NULL) { |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 86 | perror(*argv); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 87 | exit(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 89 | fstat(fileno(file), &st); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 91 | #ifdef BB_MORE_TERM |
| 92 | cin = fopen("/dev/tty", "r"); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 93 | if (!cin) |
| 94 | cin = fopen("/dev/console", "r"); |
| 95 | #ifdef USE_OLD_TERMIO |
| 96 | ioctl(fileno(cin),TCGETA,&initial_settings); |
| 97 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 98 | tcgetattr(fileno(cin),&initial_settings); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 99 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 100 | new_settings = initial_settings; |
| 101 | new_settings.c_lflag &= ~ICANON; |
| 102 | new_settings.c_lflag &= ~ECHO; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 103 | stty(fileno(cin), &new_settings); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 104 | |
| 105 | (void) signal(SIGINT, gotsig); |
| 106 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 107 | #endif |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 108 | while ((c = getc(file)) != EOF) { |
| 109 | if ( next_page ) { |
| 110 | int len=0; |
| 111 | next_page = 0; |
| 112 | lines=0; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 113 | len = fprintf(stdout, "--More-- "); |
| 114 | if (file != stdin) { |
| 115 | len += fprintf(stdout, "(%d%% of %ld bytes)", |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 116 | (int) (100*( (double) ftell(file) / (double) st.st_size )), |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 117 | st.st_size); |
| 118 | } |
| 119 | len += fprintf(stdout, "%s", |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 120 | #ifdef BB_MORE_TERM |
| 121 | "" |
| 122 | #else |
| 123 | "\n" |
| 124 | #endif |
| 125 | ); |
| 126 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 127 | fflush(stdout); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 128 | input = getc( cin); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 129 | |
| 130 | #ifdef BB_MORE_TERM |
| 131 | /* Erase the "More" message */ |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 132 | while(len-- > 0) |
| 133 | putc('\b', stdout); |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 134 | while(len++ < 79) |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 135 | putc(' ', stdout); |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 136 | while(len-- > 0) |
| 137 | putc('\b', stdout); |
| 138 | fflush(stdout); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 140 | |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 141 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 142 | if (input=='q') |
| 143 | goto end; |
| 144 | if (input==' ' && c == '\n' ) |
| 145 | next_page = 1; |
Eric Andersen | f5a3838 | 1999-10-19 22:26:25 +0000 | [diff] [blame] | 146 | if ( c == '\n' && ++lines == 24 ) |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 147 | next_page = 1; |
| 148 | putc(c, stdout); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 150 | fclose(file); |
| 151 | fflush(stdout); |
| 152 | |
| 153 | argc--; |
| 154 | argv++; |
| 155 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 156 | end: |
| 157 | #ifdef BB_MORE_TERM |
| 158 | gotsig(0); |
| 159 | #endif |
| 160 | exit(TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 161 | } |
Eric Andersen | 4bea32a | 1999-10-06 00:30:51 +0000 | [diff] [blame] | 162 | |