blob: 2ad1e797c25fc34c0ac3b4e9d796f366e22d9dd6 [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 Andersen96bcfd31999-11-12 01:30:18 +00005 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen96bcfd31999-11-12 01:30:18 +00007 *
Eric Andersencb81e642003-07-14 21:21:08 +00008 * Latest version blended together by Erik Andersen <andersen@codepoet.org>,
Eric Andersenc7bda1c2004-03-15 08:29:22 +00009 * based on the original more implementation by Bruce, and code from the
Eric Andersen96bcfd31999-11-12 01:30:18 +000010 * Debian boot-floppies team.
Eric Andersen4bea32a1999-10-06 00:30:51 +000011 *
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +000012 * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
Glenn L McGrath78b0e372001-06-26 02:06:08 +000013 *
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
Erik Andersen4f3f7572000-04-28 00:18:56 +000038
Eric Andersenbdfd0d72001-10-24 05:00:29 +000039#ifdef CONFIG_FEATURE_USE_TERMIOS
Eric Andersenedd580a2004-03-27 09:49:57 +000040static int cin_fileno;
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{
Eric Andersenedd580a2004-03-27 09:49:57 +000049 setTermSettings(cin_fileno, &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
Rob Landleydfba7412006-03-06 20:47:33 +000060int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000061{
Eric Andersen57239342001-02-23 01:39:26 +000062 int c, lines, input = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000063 int please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 struct stat st;
65 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000066 FILE *cin;
Eric Andersenffc40bf2001-02-22 21:49:32 +000067 int len, page_height;
Eric Andersenedd580a2004-03-27 09:49:57 +000068 int terminal_width;
69 int terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000070
Erik Andersene49d5ec2000-02-08 19:58:47 +000071 argc--;
Eric Andersen4bea32a1999-10-06 00:30:51 +000072 argv++;
Eric Andersen4bea32a1999-10-06 00:30:51 +000073
Glenn L McGrath78b0e372001-06-26 02:06:08 +000074
75 /* not use inputing from terminal if usage: more > outfile */
Eric Andersen70060d22004-03-27 10:02:48 +000076 if(isatty(STDOUT_FILENO)) {
Matt Kraai439e3df2001-07-23 14:52:08 +000077 cin = fopen(CURRENT_TTY, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000078 if (!cin)
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 cin = bb_xfopen(CONSOLE_DEV, "r");
Eric Andersenedd580a2004-03-27 09:49:57 +000080 please_display_more_prompt = 2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000081#ifdef CONFIG_FEATURE_USE_TERMIOS
Rob Landleyc44bc982006-05-28 01:19:06 +000082 cin_fileno = fileno(cin);
Eric Andersenedd580a2004-03-27 09:49:57 +000083 getTermSettings(cin_fileno, &initial_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000084 new_settings = initial_settings;
85 new_settings.c_lflag &= ~ICANON;
86 new_settings.c_lflag &= ~ECHO;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000087 new_settings.c_cc[VMIN] = 1;
88 new_settings.c_cc[VTIME] = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000089 setTermSettings(cin_fileno, &new_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000090 atexit(set_tty_to_initial_mode);
91 (void) signal(SIGINT, gotsig);
92 (void) signal(SIGQUIT, gotsig);
93 (void) signal(SIGTERM, gotsig);
94#endif
Eric Andersenedd580a2004-03-27 09:49:57 +000095 } else {
96 cin = stdin;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000097 }
98
Erik Andersene49d5ec2000-02-08 19:58:47 +000099 do {
100 if (argc == 0) {
101 file = stdin;
102 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 file = bb_wfopen(*argv, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000104 if(file==0)
105 goto loop;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000106
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000107 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 fstat(fileno(file), &st);
109
Eric Andersenedd580a2004-03-27 09:49:57 +0000110 please_display_more_prompt &= ~1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111
Rob Landleyc44bc982006-05-28 01:19:06 +0000112 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Eric Andersen8efe9672003-09-15 08:33:45 +0000113 if (terminal_height > 4)
114 terminal_height -= 2;
115 if (terminal_width > 0)
116 terminal_width -= 1;
117
Eric Andersen57239342001-02-23 01:39:26 +0000118 len=0;
119 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000120 page_height = terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 while ((c = getc(file)) != EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122
Eric Andersenedd580a2004-03-27 09:49:57 +0000123 if ((please_display_more_prompt & 3) == 3) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000124 len = printf("--More-- ");
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000125 if (file != stdin && st.st_size > 0) {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000126#if _FILE_OFFSET_BITS == 64
Matt Kraai12f417e2001-01-18 02:57:08 +0000127 len += printf("(%d%% of %lld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000128 (int) (100 * ((double) ftell(file) /
129 (double) st.st_size)), (long long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000130#else
Matt Kraai12f417e2001-01-18 02:57:08 +0000131 len += printf("(%d%% of %ld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000132 (int) (100 * ((double) ftell(file) /
133 (double) st.st_size)), (long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000134#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136
137 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000138
139 /*
140 * We've just displayed the "--More--" prompt, so now we need
141 * to get input from the user.
142 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 input = getc(cin);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000144#ifndef CONFIG_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000145 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000146#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 /* Erase the "More" message */
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000148 putc('\r', stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 while (--len >= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150 putc(' ', stdout);
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000151 putc('\r', stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000152 len=0;
Eric Andersen57239342001-02-23 01:39:26 +0000153 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000154 page_height = terminal_height;
Eric Andersenedd580a2004-03-27 09:49:57 +0000155 please_display_more_prompt &= ~1;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000156
157 if (input == 'q')
158 goto end;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000160
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000161 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000162 * There are two input streams to worry about here:
163 *
Eric Andersen57239342001-02-23 01:39:26 +0000164 * c : the character we are reading from the file being "mored"
Mark Whitleyb9913952000-06-16 00:26:51 +0000165 * input : a character received from the keyboard
166 *
167 * If we hit a newline in the _file_ stream, we want to test and
168 * see if any characters have been hit in the _input_ stream. This
169 * allows the user to quit while in the middle of a file.
170 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 if (c == '\n') {
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000172 /* increment by just one line if we are at
173 * the end of this line */
174 if (input == '\n')
Eric Andersenedd580a2004-03-27 09:49:57 +0000175 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000176 /* Adjust the terminal height for any overlap, so that
177 * no lines get lost off the top. */
Eric Andersen57239342001-02-23 01:39:26 +0000178 if (len >= terminal_width) {
Eric Andersen2439a592001-05-16 18:53:34 +0000179 int quot, rem;
180 quot = len / terminal_width;
181 rem = len - (quot * terminal_width);
182 if (quot) {
183 if (rem)
184 page_height-=quot;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000185 else
Eric Andersen2439a592001-05-16 18:53:34 +0000186 page_height-=(quot-1);
Eric Andersenffc40bf2001-02-22 21:49:32 +0000187 }
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000188 }
Eric Andersen57239342001-02-23 01:39:26 +0000189 if (++lines >= page_height) {
Eric Andersenedd580a2004-03-27 09:49:57 +0000190 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000191 }
192 len=0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000194 /*
195 * If we just read a newline from the file being 'mored' and any
196 * key other than a return is hit, scroll by one page
197 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 putc(c, stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000199 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200 }
201 fclose(file);
202 fflush(stdout);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000203loop:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 argv++;
205 } while (--argc > 0);
206 end:
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000207 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208}