blob: f68292e80217e0df1a32b62fa211964bd9683cc7 [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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000020#ifdef CONFIG_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>
23#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
24#define getTermSettings(fd,argp) tcgetattr(fd, argp);
25
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}
Eric Andersenbdfd0d72001-10-24 05:00:29 +000038#endif /* CONFIG_FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Eric Andersen50d63601999-11-09 01:47:36 +000040
Rob Landleydfba7412006-03-06 20:47:33 +000041int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000042{
Eric Andersen57239342001-02-23 01:39:26 +000043 int c, lines, input = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000044 int please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 struct stat st;
46 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000047 FILE *cin;
Eric Andersenffc40bf2001-02-22 21:49:32 +000048 int len, page_height;
Eric Andersenedd580a2004-03-27 09:49:57 +000049 int terminal_width;
50 int terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 argc--;
Eric Andersen4bea32a1999-10-06 00:30:51 +000053 argv++;
Eric Andersen4bea32a1999-10-06 00:30:51 +000054
Glenn L McGrath78b0e372001-06-26 02:06:08 +000055
56 /* not use inputing from terminal if usage: more > outfile */
Eric Andersen70060d22004-03-27 10:02:48 +000057 if(isatty(STDOUT_FILENO)) {
Matt Kraai439e3df2001-07-23 14:52:08 +000058 cin = fopen(CURRENT_TTY, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000059 if (!cin)
Rob Landleyd921b2e2006-08-03 15:41:12 +000060 cin = xfopen(CONSOLE_DEV, "r");
Eric Andersenedd580a2004-03-27 09:49:57 +000061 please_display_more_prompt = 2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000062#ifdef CONFIG_FEATURE_USE_TERMIOS
Rob Landleyc44bc982006-05-28 01:19:06 +000063 cin_fileno = fileno(cin);
Eric Andersenedd580a2004-03-27 09:49:57 +000064 getTermSettings(cin_fileno, &initial_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000065 new_settings = initial_settings;
66 new_settings.c_lflag &= ~ICANON;
67 new_settings.c_lflag &= ~ECHO;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000068 new_settings.c_cc[VMIN] = 1;
69 new_settings.c_cc[VTIME] = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000070 setTermSettings(cin_fileno, &new_settings);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000071 atexit(set_tty_to_initial_mode);
72 (void) signal(SIGINT, gotsig);
73 (void) signal(SIGQUIT, gotsig);
74 (void) signal(SIGTERM, gotsig);
75#endif
Eric Andersenedd580a2004-03-27 09:49:57 +000076 } else {
77 cin = stdin;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000078 }
79
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 do {
81 if (argc == 0) {
82 file = stdin;
83 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +000084 file = bb_wfopen(*argv, "r");
Glenn L McGrath78b0e372001-06-26 02:06:08 +000085 if(file==0)
86 goto loop;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000087
Eric Andersen0ee0a8d2001-12-06 07:24:29 +000088 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 fstat(fileno(file), &st);
90
Eric Andersenedd580a2004-03-27 09:49:57 +000091 please_display_more_prompt &= ~1;
Erik Andersene49d5ec2000-02-08 19:58:47 +000092
Rob Landleyc44bc982006-05-28 01:19:06 +000093 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Eric Andersen8efe9672003-09-15 08:33:45 +000094 if (terminal_height > 4)
95 terminal_height -= 2;
96 if (terminal_width > 0)
97 terminal_width -= 1;
98
Eric Andersen57239342001-02-23 01:39:26 +000099 len=0;
100 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000101 page_height = terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 while ((c = getc(file)) != EOF) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103
Eric Andersenedd580a2004-03-27 09:49:57 +0000104 if ((please_display_more_prompt & 3) == 3) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000105 len = printf("--More-- ");
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000106 if (file != stdin && st.st_size > 0) {
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000107#if _FILE_OFFSET_BITS == 64
Matt Kraai12f417e2001-01-18 02:57:08 +0000108 len += printf("(%d%% of %lld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000109 (int) (100 * ((double) ftell(file) /
110 (double) st.st_size)), (long long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000111#else
Matt Kraai12f417e2001-01-18 02:57:08 +0000112 len += printf("(%d%% of %ld bytes)",
Eric Andersen250a2212001-04-05 23:26:44 +0000113 (int) (100 * ((double) ftell(file) /
114 (double) st.st_size)), (long)st.st_size);
Eric Andersen8a2e56c2000-09-21 02:23:30 +0000115#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117
118 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000119
120 /*
121 * We've just displayed the "--More--" prompt, so now we need
122 * to get input from the user.
123 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 input = getc(cin);
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000125#ifndef CONFIG_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000126 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000127#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 /* Erase the "More" message */
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000129 putc('\r', stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 while (--len >= 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 putc(' ', stdout);
Eric Andersenaa21e0f2001-04-06 16:02:22 +0000132 putc('\r', stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000133 len=0;
Eric Andersen57239342001-02-23 01:39:26 +0000134 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000135 page_height = terminal_height;
Eric Andersenedd580a2004-03-27 09:49:57 +0000136 please_display_more_prompt &= ~1;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000137
138 if (input == 'q')
139 goto end;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000141
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000142 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000143 * There are two input streams to worry about here:
144 *
Eric Andersen57239342001-02-23 01:39:26 +0000145 * c : the character we are reading from the file being "mored"
Mark Whitleyb9913952000-06-16 00:26:51 +0000146 * input : a character received from the keyboard
147 *
148 * If we hit a newline in the _file_ stream, we want to test and
149 * see if any characters have been hit in the _input_ stream. This
150 * allows the user to quit while in the middle of a file.
151 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (c == '\n') {
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000153 /* increment by just one line if we are at
154 * the end of this line */
155 if (input == '\n')
Eric Andersenedd580a2004-03-27 09:49:57 +0000156 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000157 /* Adjust the terminal height for any overlap, so that
158 * no lines get lost off the top. */
Eric Andersen57239342001-02-23 01:39:26 +0000159 if (len >= terminal_width) {
Eric Andersen2439a592001-05-16 18:53:34 +0000160 int quot, rem;
161 quot = len / terminal_width;
162 rem = len - (quot * terminal_width);
163 if (quot) {
164 if (rem)
165 page_height-=quot;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000166 else
Eric Andersen2439a592001-05-16 18:53:34 +0000167 page_height-=(quot-1);
Eric Andersenffc40bf2001-02-22 21:49:32 +0000168 }
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000169 }
Eric Andersen57239342001-02-23 01:39:26 +0000170 if (++lines >= page_height) {
Eric Andersenedd580a2004-03-27 09:49:57 +0000171 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000172 }
173 len=0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000175 /*
176 * If we just read a newline from the file being 'mored' and any
177 * key other than a return is hit, scroll by one page
178 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 putc(c, stdout);
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000180 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 }
182 fclose(file);
183 fflush(stdout);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000184loop:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 argv++;
186 } while (--argc > 0);
187 end:
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000188 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189}