blob: 95cbdd994bb5f894a0a93959b1a66f86e72afdb5 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen4bea32a1999-10-06 00:30:51 +000015 */
16
Pere Orga5bc8c002011-04-11 03:29:49 +020017//usage:#define more_trivial_usage
18//usage: "[FILE]..."
19//usage:#define more_full_usage "\n\n"
20//usage: "View FILE (or stdin) one screenful at a time"
21//usage:
22//usage:#define more_example_usage
23//usage: "$ dmesg | more\n"
24
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000025#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020026#include "common_bufsiz.h"
Erik Andersen4f3f7572000-04-28 00:18:56 +000027
Denys Vlasenko53f17912009-06-05 14:55:26 +020028/* Support for FEATURE_USE_TERMIOS */
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000029
30struct globals {
31 int cin_fileno;
32 struct termios initial_settings;
33 struct termios new_settings;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010034} FIX_ALIASING;
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000035#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000036#define initial_settings (G.initial_settings)
37#define new_settings (G.new_settings )
38#define cin_fileno (G.cin_fileno )
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020039#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000040
Marek Polacek7b181072010-10-28 21:34:56 +020041#define setTermSettings(fd, argp) \
42do { \
43 if (ENABLE_FEATURE_USE_TERMIOS) \
44 tcsetattr(fd, TCSANOW, argp); \
45} while (0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000046#define getTermSettings(fd, argp) tcgetattr(fd, argp)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000047
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048static void gotsig(int sig UNUSED_PARAM)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000049{
Marek Polacek7b181072010-10-28 21:34:56 +020050 /* bb_putchar_stderr doesn't use stdio buffering,
51 * therefore it is safe in signal handler */
52 bb_putchar_stderr('\n');
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000053 setTermSettings(cin_fileno, &initial_settings);
Marek Polacek7b181072010-10-28 21:34:56 +020054 _exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000055}
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000056
Denis Vlasenko033e5922007-08-15 20:42:52 +000057#define CONVERTED_TAB_SIZE 8
Eric Andersen50d63601999-11-09 01:47:36 +000058
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000059int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000060int more_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000061{
Denys Vlasenkod8b989f2010-07-12 03:14:17 +020062 int c = c; /* for compiler */
Denis Vlasenko033e5922007-08-15 20:42:52 +000063 int lines;
64 int input = 0;
65 int spaces = 0;
66 int please_display_more_prompt;
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 struct stat st;
68 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000069 FILE *cin;
Denis Vlasenko033e5922007-08-15 20:42:52 +000070 int len;
Denis Vlasenko55995022008-05-18 22:28:26 +000071 unsigned terminal_width;
72 unsigned terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000073
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000074 INIT_G();
75
Eric Andersen4bea32a1999-10-06 00:30:51 +000076 argv++;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000077 /* Another popular pager, most, detects when stdout
78 * is not a tty and turns into cat. This makes sense. */
79 if (!isatty(STDOUT_FILENO))
80 return bb_cat(argv);
Denis Vlasenko5415c852008-07-21 23:05:26 +000081 cin = fopen_for_read(CURRENT_TTY);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000082 if (!cin)
83 return bb_cat(argv);
Eric Andersen4bea32a1999-10-06 00:30:51 +000084
Denys Vlasenko53f17912009-06-05 14:55:26 +020085 if (ENABLE_FEATURE_USE_TERMIOS) {
86 cin_fileno = fileno(cin);
87 getTermSettings(cin_fileno, &initial_settings);
88 new_settings = initial_settings;
Denys Vlasenko6e8861b2012-01-15 23:00:13 +010089 new_settings.c_lflag &= ~(ICANON | ECHO);
Denys Vlasenko53f17912009-06-05 14:55:26 +020090 new_settings.c_cc[VMIN] = 1;
91 new_settings.c_cc[VTIME] = 0;
92 setTermSettings(cin_fileno, &new_settings);
93 bb_signals(0
94 + (1 << SIGINT)
95 + (1 << SIGQUIT)
96 + (1 << SIGTERM)
97 , gotsig);
98 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +000099
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 do {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000101 file = stdin;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000102 if (*argv) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000103 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000104 if (!file)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000105 continue;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +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
Denis Vlasenko033e5922007-08-15 20:42:52 +0000110 please_display_more_prompt = 0;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000111 /* never returns w, h <= 1 */
Rob Landleyc44bc982006-05-28 01:19:06 +0000112 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000113 terminal_height -= 1;
Eric Andersen8efe9672003-09-15 08:33:45 +0000114
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000115 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000116 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000117 while (spaces || (c = getc(file)) != EOF) {
118 int wrap;
119 if (spaces)
120 spaces--;
121 loop_top:
122 if (input != 'r' && please_display_more_prompt) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000123 len = printf("--More-- ");
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100124 if (st.st_size != 0) {
125 uoff_t d = (uoff_t)st.st_size / 100;
126 if (d == 0)
127 d = 1;
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100128 len += printf("(%u%% of %"OFF_FMT"u bytes)",
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100129 (int) ((uoff_t)ftello(file) / d),
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000130 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100132 fflush_all();
Mark Whitleyb9913952000-06-16 00:26:51 +0000133
134 /*
135 * We've just displayed the "--More--" prompt, so now we need
136 * to get input from the user.
137 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000138 for (;;) {
139 input = getc(cin);
140 input = tolower(input);
Denys Vlasenko53f17912009-06-05 14:55:26 +0200141 if (!ENABLE_FEATURE_USE_TERMIOS)
142 printf("\033[A"); /* cursor up */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000143 /* Erase the last message */
144 printf("\r%*s\r", len, "");
145
146 /* Due to various multibyte escape
147 * sequences, it's not ok to accept
148 * any input as a command to scroll
149 * the screen. We only allow known
150 * commands, else we show help msg. */
151 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
152 break;
153 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
154 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000155 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000156 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000157 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000158
159 if (input == 'q')
160 goto end;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000161
162 /* The user may have resized the terminal.
163 * Re-read the dimensions. */
Denys Vlasenko53f17912009-06-05 14:55:26 +0200164 if (ENABLE_FEATURE_USE_TERMIOS) {
165 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
166 terminal_height -= 1;
167 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000169
Denis Vlasenko033e5922007-08-15 20:42:52 +0000170 /* Crudely convert tabs into spaces, which are
171 * a bajillion times easier to deal with. */
172 if (c == '\t') {
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100173 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000174 c = ' ';
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000175 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000176
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000177 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000178 * There are two input streams to worry about here:
179 *
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000180 * c : the character we are reading from the file being "mored"
181 * input: a character received from the keyboard
Mark Whitleyb9913952000-06-16 00:26:51 +0000182 *
183 * If we hit a newline in the _file_ stream, we want to test and
184 * see if any characters have been hit in the _input_ stream. This
185 * allows the user to quit while in the middle of a file.
186 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000187 wrap = (++len > terminal_width);
188 if (c == '\n' || wrap) {
189 /* Then outputting this character
190 * will move us to a new line. */
191 if (++lines >= terminal_height || input == '\n')
192 please_display_more_prompt = 1;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000193 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000195 if (c != '\n' && wrap) {
196 /* Then outputting this will also put a character on
197 * the beginning of that new line. Thus we first want to
198 * display the prompt (if any), so we skip the putchar()
199 * and go back to the top of the loop, without reading
200 * a new character. */
201 goto loop_top;
202 }
203 /* My small mind cannot fathom backspaces and UTF-8 */
204 putchar(c);
Denys Vlasenko0d1d0f62010-12-13 14:31:59 +0100205 die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000206 }
207 fclose(file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100208 fflush_all();
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000209 } while (*argv && *++argv);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000210 end:
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000211 setTermSettings(cin_fileno, &initial_settings);
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000212 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213}