blob: 8ae2661ab64bf7e9c45247e254cd910be9431135 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen4bea32a1999-10-06 00:30:51 +00002/*
3 * Mini more implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Eric Andersen96bcfd31999-11-12 01:30:18 +00006 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7 *
8 * Latest version blended together by Erik Andersen <andersen@lineo.com>,
9 * based on the original more implementation by Bruce, and code from the
10 * Debian boot-floppies team.
Eric Andersen4bea32a1999-10-06 00:30:51 +000011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
Eric Andersencc8ed391999-10-05 16:24:54 +000028#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 Andersened3ef502001-01-27 08:24:39 +000031#include <stdlib.h>
Eric Andersen50d63601999-11-09 01:47:36 +000032#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000033#include "busybox.h"
Eric Andersen3cf52d11999-10-12 22:26:06 +000034
Erik Andersen1d1d9502000-04-21 01:26:49 +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
Mark Whitley4fa84e62000-06-21 22:53:16 +000037# if #cpu(sparc)
38# include <termio.h>
39# define termios termio
40# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
41# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
42# else
43# include <termios.h>
44# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
45# define getTermSettings(fd,argp) tcgetattr(fd, argp);
46# endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000047
Eric Andersen5c9c8b42001-01-26 06:50:46 +000048static FILE *cin;
Erik Andersen4f3f7572000-04-28 00:18:56 +000049
Eric Andersen63a86222000-11-07 06:52:13 +000050static struct termios initial_settings, new_settings;
Eric Andersen3cf52d11999-10-12 22:26:06 +000051
Matt Kraaie6e81832000-12-30 07:46:23 +000052static void gotsig(int sig)
Erik Andersene49d5ec2000-02-08 19:58:47 +000053{
Erik Andersen1d1d9502000-04-21 01:26:49 +000054 setTermSettings(fileno(cin), &initial_settings);
Matt Kraai12f417e2001-01-18 02:57:08 +000055 putchar('\n');
56 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057}
Mark Whitley4fa84e62000-06-21 22:53:16 +000058#endif /* BB_FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000059
Eric Andersen50d63601999-11-09 01:47:36 +000060
Mark Whitley4fa84e62000-06-21 22:53:16 +000061static int terminal_width = 79; /* not 80 in case terminal has linefold bug */
62static int terminal_height = 24;
Eric Andersen50d63601999-11-09 01:47:36 +000063
64
Eric Andersen4bea32a1999-10-06 00:30:51 +000065extern int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000066{
Eric Andersen57239342001-02-23 01:39:26 +000067 int c, lines, input = 0;
68 int please_display_more_prompt;
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 struct stat st;
70 FILE *file;
Eric Andersenffc40bf2001-02-22 21:49:32 +000071 int len, page_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000072
Erik Andersen4f3f7572000-04-28 00:18:56 +000073#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
Pavel Roskinf626dcb2000-07-14 15:55:41 +000074 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen50d63601999-11-09 01:47:36 +000075#endif
Eric Andersen4bea32a1999-10-06 00:30:51 +000076
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 argc--;
Eric Andersen4bea32a1999-10-06 00:30:51 +000078 argv++;
Eric Andersen4bea32a1999-10-06 00:30:51 +000079
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 do {
81 if (argc == 0) {
82 file = stdin;
83 } else
Matt Kraaibbaef662000-09-27 02:43:35 +000084 file = xfopen(*argv, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000085
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 fstat(fileno(file), &st);
87
88#ifdef BB_FEATURE_USE_TERMIOS
89 cin = fopen("/dev/tty", "r");
90 if (!cin)
Eric Andersen21af7522001-03-23 17:09:15 +000091 cin = xfopen("/dev/console", "r");
Erik Andersen1d1d9502000-04-21 01:26:49 +000092 getTermSettings(fileno(cin), &initial_settings);
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 new_settings = initial_settings;
Erik Andersene90f4042000-04-21 21:53:58 +000094 new_settings.c_cc[VMIN] = 1;
95 new_settings.c_cc[VTIME] = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 new_settings.c_lflag &= ~ICANON;
97 new_settings.c_lflag &= ~ECHO;
Erik Andersen1d1d9502000-04-21 01:26:49 +000098 setTermSettings(fileno(cin), &new_settings);
Erik Andersene49d5ec2000-02-08 19:58:47 +000099
Mark Whitley4fa84e62000-06-21 22:53:16 +0000100# ifdef BB_FEATURE_AUTOWIDTH
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 ioctl(fileno(stdout), TIOCGWINSZ, &win);
102 if (win.ws_row > 4)
103 terminal_height = win.ws_row - 2;
104 if (win.ws_col > 0)
105 terminal_width = win.ws_col - 1;
Mark Whitley4fa84e62000-06-21 22:53:16 +0000106# endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107
108 (void) signal(SIGINT, gotsig);
109 (void) signal(SIGQUIT, gotsig);
110 (void) signal(SIGTERM, gotsig);
111
112#endif
Eric Andersen57239342001-02-23 01:39:26 +0000113 len=0;
114 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000115 page_height = terminal_height;
Eric Andersen57239342001-02-23 01:39:26 +0000116 please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 while ((c = getc(file)) != EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000119 if (please_display_more_prompt) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000120 len = printf("--More-- ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 if (file != stdin) {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000122#if _FILE_OFFSET_BITS == 64
Matt Kraai12f417e2001-01-18 02:57:08 +0000123 len += printf("(%d%% of %lld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000124 (int) (100 * ((double) ftell(file) /
125 (double) st.st_size)), (long long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000126#else
Matt Kraai12f417e2001-01-18 02:57:08 +0000127 len += printf("(%d%% of %ld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000128 (int) (100 * ((double) ftell(file) /
129 (double) st.st_size)), (long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000130#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 }
Matt Kraai12f417e2001-01-18 02:57:08 +0000132 len += printf("%s",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133#ifdef BB_FEATURE_USE_TERMIOS
134 ""
135#else
136 "\n"
137#endif
138 );
139
140 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000141
142 /*
143 * We've just displayed the "--More--" prompt, so now we need
144 * to get input from the user.
145 */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000146#ifdef BB_FEATURE_USE_TERMIOS
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 input = getc(cin);
Erik Andersen4f3f7572000-04-28 00:18:56 +0000148#else
149 input = getc(stdin);
150#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151
152#ifdef BB_FEATURE_USE_TERMIOS
153 /* Erase the "More" message */
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000154 putc('\r', stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 while (--len >= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 putc(' ', stdout);
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000157 putc('\r', stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 fflush(stdout);
159#endif
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000160 len=0;
Eric Andersen57239342001-02-23 01:39:26 +0000161 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000162 page_height = terminal_height;
Eric Andersen57239342001-02-23 01:39:26 +0000163 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000164
165 if (input == 'q')
166 goto end;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000168
169 /*
170 * There are two input streams to worry about here:
171 *
Eric Andersen57239342001-02-23 01:39:26 +0000172 * c : the character we are reading from the file being "mored"
Mark Whitleyb9913952000-06-16 00:26:51 +0000173 * input : a character received from the keyboard
174 *
175 * If we hit a newline in the _file_ stream, we want to test and
176 * see if any characters have been hit in the _input_ stream. This
177 * allows the user to quit while in the middle of a file.
178 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 if (c == '\n') {
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000180 /* increment by just one line if we are at
181 * the end of this line */
182 if (input == '\n')
Mark Whitleyb9913952000-06-16 00:26:51 +0000183 please_display_more_prompt = 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000184 /* Adjust the terminal height for any overlap, so that
185 * no lines get lost off the top. */
Eric Andersen57239342001-02-23 01:39:26 +0000186 if (len >= terminal_width) {
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000187 div_t result = div( len, terminal_width);
Eric Andersenffc40bf2001-02-22 21:49:32 +0000188 if (result.quot) {
189 if (result.rem)
190 page_height-=result.quot;
191 else
192 page_height-=(result.quot-1);
193 }
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000194 }
Eric Andersen57239342001-02-23 01:39:26 +0000195 if (++lines >= page_height) {
Mark Whitleyb9913952000-06-16 00:26:51 +0000196 please_display_more_prompt = 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000197 }
198 len=0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000200 /*
201 * If we just read a newline from the file being 'mored' and any
202 * key other than a return is hit, scroll by one page
203 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 putc(c, stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000205 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 }
207 fclose(file);
208 fflush(stdout);
209
210 argv++;
211 } while (--argc > 0);
212 end:
213#ifdef BB_FEATURE_USE_TERMIOS
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000214 setTermSettings(fileno(cin), &initial_settings);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215#endif
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000216 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217}