blob: 821427dda35b206a89c7f5b6f9ef7fbb3e56fc7b [file] [log] [blame]
Eric Andersen4bea32a1999-10-06 00:30:51 +00001/*
2 * Mini more implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
Eric Andersen96bcfd31999-11-12 01:30:18 +00005 * 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 Andersen4bea32a1999-10-06 00:30:51 +000010 *
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 Andersencc8ed391999-10-05 16:24:54 +000027#include "internal.h"
28#include <stdio.h>
Eric Andersenf5a38381999-10-19 22:26:25 +000029#include <fcntl.h>
Eric Andersen4bea32a1999-10-06 00:30:51 +000030#include <signal.h>
Eric Andersen50d63601999-11-09 01:47:36 +000031#include <sys/ioctl.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000032
Eric Andersend73dc5b1999-11-10 23:13:02 +000033static const char more_usage[] = "more [file ...]\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Eric Andersenf5a38381999-10-19 22:26:25 +000035/* ED: sparc termios is broken: revert back to old termio handling. */
Eric Andersen50d63601999-11-09 01:47:36 +000036#ifdef BB_FEATURE_USE_TERMIOS
Eric Andersenf5a38381999-10-19 22:26:25 +000037
Eric Andersenfbb39c81999-11-08 17:00:52 +000038#if #cpu(sparc)
Eric Andersenf5a38381999-10-19 22:26:25 +000039# define USE_OLD_TERMIO
40# include <termio.h>
Eric Andersenf5a38381999-10-19 22:26:25 +000041# 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 Andersen3cf52d11999-10-12 22:26:06 +000047
48 FILE *cin;
49 struct termios initial_settings, new_settings;
50
51 void gotsig(int sig) {
Eric Andersenf5a38381999-10-19 22:26:25 +000052 stty(fileno(cin), &initial_settings);
Eric Andersen50d63601999-11-09 01:47:36 +000053 fprintf(stdout, "\n");
Eric Andersen3cf52d11999-10-12 22:26:06 +000054 exit( TRUE);
55 }
56#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000057
Eric Andersen50d63601999-11-09 01:47:36 +000058
59
60#define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */
61#define TERMINAL_HEIGHT 24
62
63
Eric Andersen96bcfd31999-11-12 01:30:18 +000064#if defined BB_FEATURE_AUTOWIDTH
Eric Andersen50d63601999-11-09 01:47:36 +000065static 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 Andersen4bea32a1999-10-06 00:30:51 +000073extern int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000074{
Eric Andersen21943ce1999-10-13 18:04:51 +000075 int c, lines=0, input=0;
Eric Andersenf5a38381999-10-19 22:26:25 +000076 int next_page=0;
Eric Andersen4bea32a1999-10-06 00:30:51 +000077 struct stat st;
Eric Andersenf5a38381999-10-19 22:26:25 +000078 FILE *file;
Eric Andersen50d63601999-11-09 01:47:36 +000079#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersen96bcfd31999-11-12 01:30:18 +000080 struct winsize win = {0,0};
Eric Andersen50d63601999-11-09 01:47:36 +000081#endif
Eric Andersen4bea32a1999-10-06 00:30:51 +000082
Eric Andersen4bea32a1999-10-06 00:30:51 +000083 argc--;
84 argv++;
85
Eric Andersenfbb39c81999-11-08 17:00:52 +000086 if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) {
87 usage (more_usage);
88 }
89 do {
Eric Andersenf5a38381999-10-19 22:26:25 +000090 if (argc==0) {
91 file = stdin;
92 }
93 else
Eric Andersen9d3aba71999-10-06 09:04:55 +000094 file = fopen(*argv, "r");
Eric Andersenf5a38381999-10-19 22:26:25 +000095
Eric Andersen4bea32a1999-10-06 00:30:51 +000096 if (file == NULL) {
Eric Andersenef8b6c71999-10-20 08:05:35 +000097 perror(*argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000098 exit(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000099 }
Eric Andersen4bea32a1999-10-06 00:30:51 +0000100 fstat(fileno(file), &st);
Eric Andersencc8ed391999-10-05 16:24:54 +0000101
Eric Andersen50d63601999-11-09 01:47:36 +0000102#ifdef BB_FEATURE_USE_TERMIOS
Eric Andersen3cf52d11999-10-12 22:26:06 +0000103 cin = fopen("/dev/tty", "r");
Eric Andersenf5a38381999-10-19 22:26:25 +0000104 if (!cin)
105 cin = fopen("/dev/console", "r");
106#ifdef USE_OLD_TERMIO
107 ioctl(fileno(cin),TCGETA,&initial_settings);
108#else
Eric Andersen3cf52d11999-10-12 22:26:06 +0000109 tcgetattr(fileno(cin),&initial_settings);
Eric Andersenf5a38381999-10-19 22:26:25 +0000110#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000111 new_settings = initial_settings;
112 new_settings.c_lflag &= ~ICANON;
113 new_settings.c_lflag &= ~ECHO;
Eric Andersenf5a38381999-10-19 22:26:25 +0000114 stty(fileno(cin), &new_settings);
Eric Andersen50d63601999-11-09 01:47:36 +0000115
116#ifdef BB_FEATURE_AUTOWIDTH
Eric Andersen08b10341999-11-19 02:38:58 +0000117 ioctl(fileno(stdout), TIOCGWINSZ, &win);
Eric Andersen50d63601999-11-09 01:47:36 +0000118 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 Andersen3cf52d11999-10-12 22:26:06 +0000124 (void) signal(SIGINT, gotsig);
Eric Andersenfbb39c81999-11-08 17:00:52 +0000125 (void) signal(SIGQUIT, gotsig);
126 (void) signal(SIGTERM, gotsig);
127
Eric Andersen3cf52d11999-10-12 22:26:06 +0000128#endif
Eric Andersen4bea32a1999-10-06 00:30:51 +0000129 while ((c = getc(file)) != EOF) {
130 if ( next_page ) {
131 int len=0;
132 next_page = 0;
133 lines=0;
Eric Andersenf5a38381999-10-19 22:26:25 +0000134 len = fprintf(stdout, "--More-- ");
135 if (file != stdin) {
136 len += fprintf(stdout, "(%d%% of %ld bytes)",
Eric Andersen4bea32a1999-10-06 00:30:51 +0000137 (int) (100*( (double) ftell(file) / (double) st.st_size )),
Eric Andersenf5a38381999-10-19 22:26:25 +0000138 st.st_size);
139 }
140 len += fprintf(stdout, "%s",
Eric Andersen50d63601999-11-09 01:47:36 +0000141#ifdef BB_FEATURE_USE_TERMIOS
Eric Andersen3cf52d11999-10-12 22:26:06 +0000142 ""
143#else
144 "\n"
145#endif
146 );
147
Eric Andersen4bea32a1999-10-06 00:30:51 +0000148 fflush(stdout);
Eric Andersenf5a38381999-10-19 22:26:25 +0000149 input = getc( cin);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000150
Eric Andersen50d63601999-11-09 01:47:36 +0000151#ifdef BB_FEATURE_USE_TERMIOS
Eric Andersen3cf52d11999-10-12 22:26:06 +0000152 /* Erase the "More" message */
Eric Andersen50d63601999-11-09 01:47:36 +0000153 while(--len >= 0)
Eric Andersen4bea32a1999-10-06 00:30:51 +0000154 putc('\b', stdout);
Eric Andersen50d63601999-11-09 01:47:36 +0000155 while(++len <= terminal_width)
Eric Andersen9d3aba71999-10-06 09:04:55 +0000156 putc(' ', stdout);
Eric Andersen50d63601999-11-09 01:47:36 +0000157 while(--len >= 0)
Eric Andersen4bea32a1999-10-06 00:30:51 +0000158 putc('\b', stdout);
159 fflush(stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000160#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000161
Eric Andersen4bea32a1999-10-06 00:30:51 +0000162 }
Eric Andersen50d63601999-11-09 01:47:36 +0000163 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 Andersen4bea32a1999-10-06 00:30:51 +0000176 putc(c, stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000177 }
Eric Andersen4bea32a1999-10-06 00:30:51 +0000178 fclose(file);
179 fflush(stdout);
180
Eric Andersen4bea32a1999-10-06 00:30:51 +0000181 argv++;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000182 } while (--argc > 0);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000183end:
Eric Andersen50d63601999-11-09 01:47:36 +0000184#ifdef BB_FEATURE_USE_TERMIOS
Eric Andersen3cf52d11999-10-12 22:26:06 +0000185 gotsig(0);
186#endif
187 exit(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000188}
Eric Andersen4bea32a1999-10-06 00:30:51 +0000189