blob: 5fe1da423ebc408d335ce6799a4b1db3452d0992 [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 *
Glenn L McGrath78b0e372001-06-26 02:06:08 +000012 * Termios corrects by Vladimir Oleynik <vodz@usa.net>
13 *
Eric Andersen4bea32a1999-10-06 00:30:51 +000014 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
Eric Andersencc8ed391999-10-05 16:24:54 +000030#include <stdio.h>
Eric Andersenf5a38381999-10-19 22:26:25 +000031#include <fcntl.h>
Eric Andersen4bea32a1999-10-06 00:30:51 +000032#include <signal.h>
Eric Andersened3ef502001-01-27 08:24:39 +000033#include <stdlib.h>
Glenn L McGrath78b0e372001-06-26 02:06:08 +000034#include <unistd.h>
Eric Andersen50d63601999-11-09 01:47:36 +000035#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000036#include "busybox.h"
Eric Andersen3cf52d11999-10-12 22:26:06 +000037
Eric Andersen5c9c8b42001-01-26 06:50:46 +000038static FILE *cin;
Erik Andersen4f3f7572000-04-28 00:18:56 +000039
Eric Andersenbdfd0d72001-10-24 05:00:29 +000040#ifdef CONFIG_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +000041#include <termios.h>
42#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
43#define getTermSettings(fd,argp) tcgetattr(fd, argp);
44
Eric Andersen63a86222000-11-07 06:52:13 +000045static struct termios initial_settings, new_settings;
Eric Andersen3cf52d11999-10-12 22:26:06 +000046
Glenn L McGrath78b0e372001-06-26 02:06:08 +000047static void set_tty_to_initial_mode(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +000048{
Erik Andersen1d1d9502000-04-21 01:26:49 +000049 setTermSettings(fileno(cin), &initial_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000050}
51
52static void gotsig(int sig)
53{
Matt Kraai12f417e2001-01-18 02:57:08 +000054 putchar('\n');
55 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000056}
Eric Andersenbdfd0d72001-10-24 05:00:29 +000057#endif /* CONFIG_FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000058
Eric Andersen50d63601999-11-09 01:47:36 +000059
Mark Whitley4fa84e62000-06-21 22:53:16 +000060static int terminal_width = 79; /* not 80 in case terminal has linefold bug */
61static int terminal_height = 24;
Eric Andersen50d63601999-11-09 01:47:36 +000062
63
Eric Andersen4bea32a1999-10-06 00:30:51 +000064extern int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000065{
Eric Andersen57239342001-02-23 01:39:26 +000066 int c, lines, input = 0;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000067 int please_display_more_prompt = -1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 struct stat st;
69 FILE *file;
Eric Andersenffc40bf2001-02-22 21:49:32 +000070 int len, page_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000071
Eric Andersenbdfd0d72001-10-24 05:00:29 +000072#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS
Pavel Roskinf626dcb2000-07-14 15:55:41 +000073 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen50d63601999-11-09 01:47:36 +000074#endif
Eric Andersen4bea32a1999-10-06 00:30:51 +000075
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 argc--;
Eric Andersen4bea32a1999-10-06 00:30:51 +000077 argv++;
Eric Andersen4bea32a1999-10-06 00:30:51 +000078
Glenn L McGrath78b0e372001-06-26 02:06:08 +000079
80 /* not use inputing from terminal if usage: more > outfile */
81 if(isatty(fileno(stdout))) {
Matt Kraai439e3df2001-07-23 14:52:08 +000082 cin = fopen(CURRENT_TTY, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000083 if (!cin)
Matt Kraai439e3df2001-07-23 14:52:08 +000084 cin = xfopen(CONSOLE_DEV, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000085 please_display_more_prompt = 0;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000086#ifdef CONFIG_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +000087 getTermSettings(fileno(cin), &initial_settings);
88 new_settings = initial_settings;
89 new_settings.c_lflag &= ~ICANON;
90 new_settings.c_lflag &= ~ECHO;
91#ifndef linux
92 /* Hmm, in linux c_cc[] not parsed if set ~ICANON */
93 new_settings.c_cc[VMIN] = 1;
94 new_settings.c_cc[VTIME] = 0;
95#endif
96 setTermSettings(fileno(cin), &new_settings);
97 atexit(set_tty_to_initial_mode);
98 (void) signal(SIGINT, gotsig);
99 (void) signal(SIGQUIT, gotsig);
100 (void) signal(SIGTERM, gotsig);
101#endif
102 }
103
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104 do {
105 if (argc == 0) {
106 file = stdin;
107 } else
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000108 file = wfopen(*argv, "r");
109 if(file==0)
110 goto loop;
111
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 fstat(fileno(file), &st);
113
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000114 if(please_display_more_prompt>0)
115 please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000117#if defined CONFIG_FEATURE_AUTOWIDTH && defined CONFIG_FEATURE_USE_TERMIOS
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 ioctl(fileno(stdout), TIOCGWINSZ, &win);
119 if (win.ws_row > 4)
120 terminal_height = win.ws_row - 2;
121 if (win.ws_col > 0)
122 terminal_width = win.ws_col - 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123#endif
Eric Andersen57239342001-02-23 01:39:26 +0000124 len=0;
125 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000126 page_height = terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 while ((c = getc(file)) != EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000129 if (please_display_more_prompt>0) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000130 len = printf("--More-- ");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (file != stdin) {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000132#if _FILE_OFFSET_BITS == 64
Matt Kraai12f417e2001-01-18 02:57:08 +0000133 len += printf("(%d%% of %lld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000134 (int) (100 * ((double) ftell(file) /
135 (double) st.st_size)), (long long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000136#else
Matt Kraai12f417e2001-01-18 02:57:08 +0000137 len += printf("(%d%% of %ld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000138 (int) (100 * ((double) ftell(file) /
139 (double) st.st_size)), (long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000140#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142
143 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000144
145 /*
146 * We've just displayed the "--More--" prompt, so now we need
147 * to get input from the user.
148 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 input = getc(cin);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#ifndef CONFIG_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000151 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000152#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 /* 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);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000159 len=0;
Eric Andersen57239342001-02-23 01:39:26 +0000160 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000161 page_height = terminal_height;
Eric Andersen57239342001-02-23 01:39:26 +0000162 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000163
164 if (input == 'q')
165 goto end;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000166 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000167
168 /*
169 * There are two input streams to worry about here:
170 *
Eric Andersen57239342001-02-23 01:39:26 +0000171 * c : the character we are reading from the file being "mored"
Mark Whitleyb9913952000-06-16 00:26:51 +0000172 * input : a character received from the keyboard
173 *
174 * If we hit a newline in the _file_ stream, we want to test and
175 * see if any characters have been hit in the _input_ stream. This
176 * allows the user to quit while in the middle of a file.
177 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178 if (c == '\n') {
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000179 /* increment by just one line if we are at
180 * the end of this line */
181 if (input == '\n')
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000182 if(please_display_more_prompt==0)
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 Andersen2439a592001-05-16 18:53:34 +0000187 int quot, rem;
188 quot = len / terminal_width;
189 rem = len - (quot * terminal_width);
190 if (quot) {
191 if (rem)
192 page_height-=quot;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000193 else
Eric Andersen2439a592001-05-16 18:53:34 +0000194 page_height-=(quot-1);
Eric Andersenffc40bf2001-02-22 21:49:32 +0000195 }
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000196 }
Eric Andersen57239342001-02-23 01:39:26 +0000197 if (++lines >= page_height) {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000198 if(please_display_more_prompt==0)
Mark Whitleyb9913952000-06-16 00:26:51 +0000199 please_display_more_prompt = 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000200 }
201 len=0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000203 /*
204 * If we just read a newline from the file being 'mored' and any
205 * key other than a return is hit, scroll by one page
206 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000207 putc(c, stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000208 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 }
210 fclose(file);
211 fflush(stdout);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000212loop:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 argv++;
214 } while (--argc > 0);
215 end:
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000216 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217}