blob: a830dcbc14c7f4af3dd1fd6fa6599acb44a8c301 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010016//config:config MORE
Denys Vlasenkob097a842018-12-28 03:20:17 +010017//config: bool "more (7 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018//config: default y
19//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020020//config: more is a simple utility which allows you to read text one screen
21//config: sized page at a time. If you want to read text that is larger than
22//config: the screen, and you are using anything faster than a 300 baud modem,
23//config: you will probably find this utility very helpful. If you don't have
24//config: any need to reading text files, you can leave this disabled.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010025
26//applet:IF_MORE(APPLET(more, BB_DIR_BIN, BB_SUID_DROP))
27
28//kbuild:lib-$(CONFIG_MORE) += more.o
Eric Andersen4bea32a1999-10-06 00:30:51 +000029
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage:#define more_trivial_usage
31//usage: "[FILE]..."
32//usage:#define more_full_usage "\n\n"
33//usage: "View FILE (or stdin) one screenful at a time"
34//usage:
35//usage:#define more_example_usage
36//usage: "$ dmesg | more\n"
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020039#include "common_bufsiz.h"
Erik Andersen4f3f7572000-04-28 00:18:56 +000040
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000041struct globals {
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010042 int tty_fileno;
43 unsigned terminal_width;
44 unsigned terminal_height;
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000045 struct termios initial_settings;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010046} FIX_ALIASING;
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000047#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020048#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000049
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010050static void get_wh(void)
51{
52 /* never returns w, h <= 1 */
53 get_terminal_width_height(G.tty_fileno, &G.terminal_width, &G.terminal_height);
54 G.terminal_height -= 1;
55}
56
57static void tcsetattr_tty_TCSANOW(struct termios *settings)
58{
59 tcsetattr(G.tty_fileno, TCSANOW, settings);
60}
Glenn L McGrath78b0e372001-06-26 02:06:08 +000061
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000062static void gotsig(int sig UNUSED_PARAM)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000063{
Marek Polacek7b181072010-10-28 21:34:56 +020064 /* bb_putchar_stderr doesn't use stdio buffering,
65 * therefore it is safe in signal handler */
66 bb_putchar_stderr('\n');
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010067 tcsetattr_tty_TCSANOW(&G.initial_settings);
Marek Polacek7b181072010-10-28 21:34:56 +020068 _exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000069}
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000070
Denis Vlasenko033e5922007-08-15 20:42:52 +000071#define CONVERTED_TAB_SIZE 8
Eric Andersen50d63601999-11-09 01:47:36 +000072
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000073int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000074int more_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000075{
Denys Vlasenkod8b989f2010-07-12 03:14:17 +020076 int c = c; /* for compiler */
Denis Vlasenko033e5922007-08-15 20:42:52 +000077 int input = 0;
78 int spaces = 0;
79 int please_display_more_prompt;
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010080 FILE *tty;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000082 INIT_G();
83
Denys Vlasenko713b5132016-10-11 15:29:38 +020084 /* Parse options */
85 /* Accepted but ignored: */
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010086 /* -d Display help instead of ringing bell */
Natanael Copa3636d522022-11-21 14:09:57 +010087 /* -e Exit immediately after writing the last line */
Denys Vlasenko713b5132016-10-11 15:29:38 +020088 /* -f Count logical lines (IOW: long lines are not folded) */
89 /* -l Do not pause after any line containing a ^L (form feed) */
90 /* -s Squeeze blank lines into one */
91 /* -u Suppress underlining */
Natanael Copa3636d522022-11-21 14:09:57 +010092 getopt32(argv, "deflsu");
Denys Vlasenko713b5132016-10-11 15:29:38 +020093 argv += optind;
94
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000095 /* Another popular pager, most, detects when stdout
96 * is not a tty and turns into cat. This makes sense. */
97 if (!isatty(STDOUT_FILENO))
98 return bb_cat(argv);
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +010099 tty = fopen_for_read(CURRENT_TTY);
100 if (!tty)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000101 return bb_cat(argv);
Eric Andersen4bea32a1999-10-06 00:30:51 +0000102
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100103 G.tty_fileno = fileno(tty);
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100104
105 /* Turn on unbuffered input; turn off echoing */
106 set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0);
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100107 bb_signals(BB_FATAL_SIGS, gotsig);
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000108
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 do {
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100110 struct stat st;
111 FILE *file;
112 int len;
113 int lines;
114
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000115 file = stdin;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000116 if (*argv) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000117 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000118 if (!file)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000119 continue;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000120 }
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000121 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 fstat(fileno(file), &st);
123
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100124 get_wh();
Eric Andersen8efe9672003-09-15 08:33:45 +0000125
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100126 please_display_more_prompt = 0;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000127 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000128 lines = 0;
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100129 for (;;) {
Denis Vlasenko033e5922007-08-15 20:42:52 +0000130 int wrap;
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100131
Denis Vlasenko033e5922007-08-15 20:42:52 +0000132 if (spaces)
133 spaces--;
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100134 else {
135 c = getc(file);
136 if (c == EOF) break;
137 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000138 loop_top:
139 if (input != 'r' && please_display_more_prompt) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000140 len = printf("--More-- ");
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100141 if (st.st_size != 0) {
142 uoff_t d = (uoff_t)st.st_size / 100;
143 if (d == 0)
144 d = 1;
Denys Vlasenkoa3aa3e32009-12-11 12:36:10 +0100145 len += printf("(%u%% of %"OFF_FMT"u bytes)",
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100146 (int) ((uoff_t)ftello(file) / d),
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000147 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000148 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000149
150 /*
151 * We've just displayed the "--More--" prompt, so now we need
152 * to get input from the user.
153 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000154 for (;;) {
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100155 fflush_all();
156 input = getc(tty);
Denis Vlasenko033e5922007-08-15 20:42:52 +0000157 input = tolower(input);
Denis Vlasenko033e5922007-08-15 20:42:52 +0000158 /* Erase the last message */
159 printf("\r%*s\r", len, "");
160
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100161 if (input == 'q')
162 goto end;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000163 /* Due to various multibyte escape
164 * sequences, it's not ok to accept
165 * any input as a command to scroll
166 * the screen. We only allow known
167 * commands, else we show help msg. */
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100168 if (input == ' ' || input == '\n' || input == 'r')
Denis Vlasenko033e5922007-08-15 20:42:52 +0000169 break;
170 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
171 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000172 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000173 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000174 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000175
Denis Vlasenko033e5922007-08-15 20:42:52 +0000176 /* The user may have resized the terminal.
177 * Re-read the dimensions. */
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100178 get_wh();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000180
Denis Vlasenko033e5922007-08-15 20:42:52 +0000181 /* Crudely convert tabs into spaces, which are
182 * a bajillion times easier to deal with. */
183 if (c == '\t') {
Denys Vlasenko82e785c2011-03-07 02:01:52 +0100184 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000185 c = ' ';
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000186 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000187
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000188 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000189 * There are two input streams to worry about here:
190 *
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000191 * c : the character we are reading from the file being "mored"
192 * input: a character received from the keyboard
Mark Whitleyb9913952000-06-16 00:26:51 +0000193 *
194 * If we hit a newline in the _file_ stream, we want to test and
195 * see if any characters have been hit in the _input_ stream. This
196 * allows the user to quit while in the middle of a file.
197 */
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100198 wrap = (++len > G.terminal_width);
Denis Vlasenko033e5922007-08-15 20:42:52 +0000199 if (c == '\n' || wrap) {
200 /* Then outputting this character
201 * will move us to a new line. */
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100202 if (++lines >= G.terminal_height || input == '\n')
Denis Vlasenko033e5922007-08-15 20:42:52 +0000203 please_display_more_prompt = 1;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000204 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000206 if (c != '\n' && wrap) {
207 /* Then outputting this will also put a character on
208 * the beginning of that new line. Thus we first want to
209 * display the prompt (if any), so we skip the putchar()
210 * and go back to the top of the loop, without reading
211 * a new character. */
212 goto loop_top;
213 }
214 /* My small mind cannot fathom backspaces and UTF-8 */
215 putchar(c);
Denys Vlasenko0d1d0f62010-12-13 14:31:59 +0100216 die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 }
218 fclose(file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100219 fflush_all();
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000220 } while (*argv && *++argv);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000221 end:
Denys Vlasenkoc5891fe2017-01-11 10:38:52 +0100222 tcsetattr_tty_TCSANOW(&G.initial_settings);
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000223 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224}