blob: d00cb40b856b58824b6f441102b0e1620f157dd8 [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 *
Rob Landleyd921b2e2006-08-03 15:41:12 +000014 * Licensed under GPLv2 or later, see file License in this tarball for details.
Eric Andersen4bea32a1999-10-06 00:30:51 +000015 */
16
Eric Andersencbe31da2001-02-20 06:14:08 +000017#include "busybox.h"
Eric Andersen3cf52d11999-10-12 22:26:06 +000018
Erik Andersen4f3f7572000-04-28 00:18:56 +000019
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000020#if ENABLE_FEATURE_USE_TERMIOS
Eric Andersenedd580a2004-03-27 09:49:57 +000021static int cin_fileno;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000022#include <termios.h>
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000023#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
24#define getTermSettings(fd, argp) tcgetattr(fd, argp);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000025
Eric Andersen63a86222000-11-07 06:52:13 +000026static struct termios initial_settings, new_settings;
Eric Andersen3cf52d11999-10-12 22:26:06 +000027
Glenn L McGrath78b0e372001-06-26 02:06:08 +000028static void set_tty_to_initial_mode(void)
Erik Andersene49d5ec2000-02-08 19:58:47 +000029{
Eric Andersenedd580a2004-03-27 09:49:57 +000030 setTermSettings(cin_fileno, &initial_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000031}
32
33static void gotsig(int sig)
34{
Matt Kraai12f417e2001-01-18 02:57:08 +000035 putchar('\n');
36 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000037}
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000038#endif /* FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Eric Andersen50d63601999-11-09 01:47:36 +000040
Denis Vlasenko06af2162007-02-03 17:28:39 +000041int more_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000042int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000043{
Eric Andersen57239342001-02-23 01:39:26 +000044 int c, lines, input = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000045 int please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 struct stat st;
47 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000048 FILE *cin;
Eric Andersenffc40bf2001-02-22 21:49:32 +000049 int len, page_height;
Eric Andersenedd580a2004-03-27 09:49:57 +000050 int terminal_width;
51 int terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 argc--;
Eric Andersen4bea32a1999-10-06 00:30:51 +000054 argv++;
Eric Andersen4bea32a1999-10-06 00:30:51 +000055
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000056 cin = stdin;
57 /* use input from terminal unless we do "more >outfile" */
58 if (isatty(STDOUT_FILENO)) {
Matt Kraai439e3df2001-07-23 14:52:08 +000059 cin = fopen(CURRENT_TTY, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000060 if (!cin)
Rob Landleyd921b2e2006-08-03 15:41:12 +000061 cin = xfopen(CONSOLE_DEV, "r");
Eric Andersenedd580a2004-03-27 09:49:57 +000062 please_display_more_prompt = 2;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000063#if ENABLE_FEATURE_USE_TERMIOS
Rob Landleyc44bc982006-05-28 01:19:06 +000064 cin_fileno = fileno(cin);
Eric Andersenedd580a2004-03-27 09:49:57 +000065 getTermSettings(cin_fileno, &initial_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000066 new_settings = initial_settings;
67 new_settings.c_lflag &= ~ICANON;
68 new_settings.c_lflag &= ~ECHO;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000069 new_settings.c_cc[VMIN] = 1;
70 new_settings.c_cc[VTIME] = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000071 setTermSettings(cin_fileno, &new_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000072 atexit(set_tty_to_initial_mode);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000073 signal(SIGINT, gotsig);
74 signal(SIGQUIT, gotsig);
75 signal(SIGTERM, gotsig);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000076#endif
77 }
78
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 do {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000080 file = stdin;
81 if (argc != 0) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000082 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000083 if (!file)
84 goto loop;
85 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000086
Eric Andersen0ee0a8d2001-12-06 07:24:29 +000087 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 fstat(fileno(file), &st);
89
Eric Andersenedd580a2004-03-27 09:49:57 +000090 please_display_more_prompt &= ~1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000091
Rob Landleyc44bc982006-05-28 01:19:06 +000092 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Eric Andersen8efe9672003-09-15 08:33:45 +000093 if (terminal_height > 4)
94 terminal_height -= 2;
95 if (terminal_width > 0)
96 terminal_width -= 1;
97
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000098 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +000099 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000100 page_height = terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 while ((c = getc(file)) != EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102
Eric Andersenedd580a2004-03-27 09:49:57 +0000103 if ((please_display_more_prompt & 3) == 3) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000104 len = printf("--More-- ");
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000105 if (file != stdin && st.st_size > 0) {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000106 len += printf("(%d%% of %"OFF_FMT"d bytes)",
107 (int) (ftello(file)*100 / st.st_size),
108 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000110 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000111
112 /*
113 * We've just displayed the "--More--" prompt, so now we need
114 * to get input from the user.
115 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 input = getc(cin);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000117#if !ENABLE_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000118 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000119#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 /* Erase the "More" message */
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000121 printf("\r%*s\r", len, "");
122 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000123 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000124 page_height = terminal_height;
Eric Andersenedd580a2004-03-27 09:49:57 +0000125 please_display_more_prompt &= ~1;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000126
127 if (input == 'q')
128 goto end;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000130
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000131 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000132 * There are two input streams to worry about here:
133 *
Eric Andersen57239342001-02-23 01:39:26 +0000134 * c : the character we are reading from the file being "mored"
Mark Whitleyb9913952000-06-16 00:26:51 +0000135 * input : a character received from the keyboard
136 *
137 * If we hit a newline in the _file_ stream, we want to test and
138 * see if any characters have been hit in the _input_ stream. This
139 * allows the user to quit while in the middle of a file.
140 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 if (c == '\n') {
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000142 /* increment by just one line if we are at
143 * the end of this line */
144 if (input == '\n')
Eric Andersenedd580a2004-03-27 09:49:57 +0000145 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000146 /* Adjust the terminal height for any overlap, so that
147 * no lines get lost off the top. */
Eric Andersen57239342001-02-23 01:39:26 +0000148 if (len >= terminal_width) {
Eric Andersen2439a592001-05-16 18:53:34 +0000149 int quot, rem;
150 quot = len / terminal_width;
151 rem = len - (quot * terminal_width);
152 if (quot) {
153 if (rem)
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000154 page_height -= quot;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000155 else
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000156 page_height -= (quot - 1);
Eric Andersenffc40bf2001-02-22 21:49:32 +0000157 }
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000158 }
Eric Andersen57239342001-02-23 01:39:26 +0000159 if (++lines >= page_height) {
Eric Andersenedd580a2004-03-27 09:49:57 +0000160 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000161 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000162 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000164 /*
165 * If we just read a newline from the file being 'mored' and any
166 * key other than a return is hit, scroll by one page
167 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 putc(c, stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000169 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 }
171 fclose(file);
172 fflush(stdout);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000173 loop:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 argv++;
175 } while (--argc > 0);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000176 end:
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000177 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178}