blob: 4083d8eea4496734e0b9e51d07bc14ed70d1954e [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000017#include "libbb.h"
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000018#if ENABLE_FEATURE_USE_TERMIOS
19#include <termios.h>
20#endif /* FEATURE_USE_TERMIOS */
Eric Andersen3cf52d11999-10-12 22:26:06 +000021
Erik Andersen4f3f7572000-04-28 00:18:56 +000022
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000023#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000024
25struct globals {
26 int cin_fileno;
27 struct termios initial_settings;
28 struct termios new_settings;
29};
30#define G (*(struct globals*)bb_common_bufsiz1)
31//#define G (*ptr_to_globals)
32#define initial_settings (G.initial_settings)
33#define new_settings (G.new_settings )
34#define cin_fileno (G.cin_fileno )
35#define INIT_G() ((void)0)
36//#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
37
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000038#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000039#define getTermSettings(fd, argp) tcgetattr(fd, argp)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000040
41static void gotsig(int sig)
42{
Matt Kraai12f417e2001-01-18 02:57:08 +000043 putchar('\n');
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000044 setTermSettings(cin_fileno, &initial_settings);
Matt Kraai12f417e2001-01-18 02:57:08 +000045 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000046}
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000047
48#else /* !FEATURE_USE_TERMIOS */
49#define INIT_G() ((void)0)
50#define setTermSettings(fd, argp) ((void)0)
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000051#endif /* FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000052
Eric Andersen50d63601999-11-09 01:47:36 +000053
Denis Vlasenko06af2162007-02-03 17:28:39 +000054int more_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000055int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000056{
Eric Andersen57239342001-02-23 01:39:26 +000057 int c, lines, input = 0;
Eric Andersenedd580a2004-03-27 09:49:57 +000058 int please_display_more_prompt = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 struct stat st;
60 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000061 FILE *cin;
Eric Andersenffc40bf2001-02-22 21:49:32 +000062 int len, page_height;
Eric Andersenedd580a2004-03-27 09:49:57 +000063 int terminal_width;
64 int terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000066 INIT_G();
67
Eric Andersen4bea32a1999-10-06 00:30:51 +000068 argv++;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000069 /* Another popular pager, most, detects when stdout
70 * is not a tty and turns into cat. This makes sense. */
71 if (!isatty(STDOUT_FILENO))
72 return bb_cat(argv);
73 cin = fopen(CURRENT_TTY, "r");
74 if (!cin)
75 return bb_cat(argv);
Eric Andersen4bea32a1999-10-06 00:30:51 +000076
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000077#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000078 cin_fileno = fileno(cin);
79 getTermSettings(cin_fileno, &initial_settings);
80 new_settings = initial_settings;
81 new_settings.c_lflag &= ~ICANON;
82 new_settings.c_lflag &= ~ECHO;
83 new_settings.c_cc[VMIN] = 1;
84 new_settings.c_cc[VTIME] = 0;
85 setTermSettings(cin_fileno, &new_settings);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000086 signal(SIGINT, gotsig);
87 signal(SIGQUIT, gotsig);
88 signal(SIGTERM, gotsig);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000089#endif
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000090 please_display_more_prompt = 2;
Glenn L McGrath78b0e372001-06-26 02:06:08 +000091
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 do {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000093 file = stdin;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000094 if (*argv) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000095 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000096 if (!file)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000097 continue;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000098 }
Eric Andersen0ee0a8d2001-12-06 07:24:29 +000099 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 fstat(fileno(file), &st);
101
Eric Andersenedd580a2004-03-27 09:49:57 +0000102 please_display_more_prompt &= ~1;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000103 /* never returns w, h <= 1 */
Rob Landleyc44bc982006-05-28 01:19:06 +0000104 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000105 terminal_width -= 1;
106 terminal_height -= 1;
Eric Andersen8efe9672003-09-15 08:33:45 +0000107
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000108 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000109 lines = 0;
Eric Andersenffc40bf2001-02-22 21:49:32 +0000110 page_height = terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 while ((c = getc(file)) != EOF) {
Eric Andersenedd580a2004-03-27 09:49:57 +0000112 if ((please_display_more_prompt & 3) == 3) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000113 len = printf("--More-- ");
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000114 if (/*file != stdin &&*/ st.st_size > 0) {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000115 len += printf("(%d%% of %"OFF_FMT"d bytes)",
116 (int) (ftello(file)*100 / st.st_size),
117 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000120
121 /*
122 * We've just displayed the "--More--" prompt, so now we need
123 * to get input from the user.
124 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 input = getc(cin);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000126#if !ENABLE_FEATURE_USE_TERMIOS
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000127 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000128#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000129 /* Erase the "More" message */
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000130 printf("\r%*s\r", len, "");
131 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000132 lines = 0;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000133 /* Bottom line on page will become top line
134 * after one page forward. Thus -1: */
135 page_height = terminal_height - 1;
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 *
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000145 * c : the character we are reading from the file being "mored"
146 * input: a character received from the keyboard
Mark Whitleyb9913952000-06-16 00:26:51 +0000147 *
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);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000163 page_height -= (quot - 1);
164 if (rem)
165 page_height--;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000166 }
Eric Andersen57239342001-02-23 01:39:26 +0000167 if (++lines >= page_height) {
Eric Andersenedd580a2004-03-27 09:49:57 +0000168 please_display_more_prompt |= 1;
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000169 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000170 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 }
Mark Whitley4fa84e62000-06-21 22:53:16 +0000172 /*
173 * If we just read a newline from the file being 'mored' and any
174 * key other than a return is hit, scroll by one page
175 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 putc(c, stdout);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000177 /* My small mind cannot fathom tabs, backspaces,
178 * and UTF-8 */
Eric Andersenbe30a6b2001-02-22 00:22:46 +0000179 len++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 }
181 fclose(file);
182 fflush(stdout);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000183 } while (*argv && *++argv);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000184 end:
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000185 setTermSettings(cin_fileno, &initial_settings);
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000186 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187}