blob: 257f40168db6e779ef3e10bfda6ab9772f6d54c6 [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)
Denis Vlasenko033e5922007-08-15 20:42:52 +000031#define INIT_G() ((void)0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000032#define initial_settings (G.initial_settings)
33#define new_settings (G.new_settings )
34#define cin_fileno (G.cin_fileno )
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000035
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000036#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000037#define getTermSettings(fd, argp) tcgetattr(fd, argp)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000038
Denis Vlasenko68404f12008-03-17 09:00:54 +000039static void gotsig(int sig ATTRIBUTE_UNUSED)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000040{
Denis Vlasenko4daad902007-09-27 10:20:47 +000041 bb_putchar('\n');
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000042 setTermSettings(cin_fileno, &initial_settings);
Matt Kraai12f417e2001-01-18 02:57:08 +000043 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000044}
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000045
46#else /* !FEATURE_USE_TERMIOS */
47#define INIT_G() ((void)0)
48#define setTermSettings(fd, argp) ((void)0)
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000049#endif /* FEATURE_USE_TERMIOS */
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Denis Vlasenko033e5922007-08-15 20:42:52 +000051#define CONVERTED_TAB_SIZE 8
Eric Andersen50d63601999-11-09 01:47:36 +000052
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000053int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko68404f12008-03-17 09:00:54 +000054int more_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000055{
Denis Vlasenko033e5922007-08-15 20:42:52 +000056 int c = c; /* for gcc */
57 int lines;
58 int input = 0;
59 int spaces = 0;
60 int please_display_more_prompt;
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 struct stat st;
62 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000063 FILE *cin;
Denis Vlasenko033e5922007-08-15 20:42:52 +000064 int len;
Eric Andersenedd580a2004-03-27 09:49:57 +000065 int terminal_width;
66 int terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000067
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000068 INIT_G();
69
Eric Andersen4bea32a1999-10-06 00:30:51 +000070 argv++;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000071 /* Another popular pager, most, detects when stdout
72 * is not a tty and turns into cat. This makes sense. */
73 if (!isatty(STDOUT_FILENO))
74 return bb_cat(argv);
75 cin = fopen(CURRENT_TTY, "r");
76 if (!cin)
77 return bb_cat(argv);
Eric Andersen4bea32a1999-10-06 00:30:51 +000078
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000079#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000080 cin_fileno = fileno(cin);
81 getTermSettings(cin_fileno, &initial_settings);
82 new_settings = initial_settings;
83 new_settings.c_lflag &= ~ICANON;
84 new_settings.c_lflag &= ~ECHO;
85 new_settings.c_cc[VMIN] = 1;
86 new_settings.c_cc[VTIME] = 0;
87 setTermSettings(cin_fileno, &new_settings);
Denis Vlasenko25591c32008-02-16 22:58:56 +000088 bb_signals(0
89 + (1 << SIGINT)
90 + (1 << SIGQUIT)
91 + (1 << SIGTERM)
92 , gotsig);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000093#endif
Glenn L McGrath78b0e372001-06-26 02:06:08 +000094
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 do {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000096 file = stdin;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000097 if (*argv) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000098 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000099 if (!file)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000100 continue;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000101 }
Eric Andersen0ee0a8d2001-12-06 07:24:29 +0000102 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 fstat(fileno(file), &st);
104
Denis Vlasenko033e5922007-08-15 20:42:52 +0000105 please_display_more_prompt = 0;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000106 /* never returns w, h <= 1 */
Rob Landleyc44bc982006-05-28 01:19:06 +0000107 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000108 terminal_height -= 1;
Eric Andersen8efe9672003-09-15 08:33:45 +0000109
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000110 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000111 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000112 while (spaces || (c = getc(file)) != EOF) {
113 int wrap;
114 if (spaces)
115 spaces--;
116 loop_top:
117 if (input != 'r' && please_display_more_prompt) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000118 len = printf("--More-- ");
Denis Vlasenko033e5922007-08-15 20:42:52 +0000119 if (st.st_size > 0) {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000120 len += printf("(%d%% of %"OFF_FMT"d bytes)",
121 (int) (ftello(file)*100 / st.st_size),
122 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 fflush(stdout);
Mark Whitleyb9913952000-06-16 00:26:51 +0000125
126 /*
127 * We've just displayed the "--More--" prompt, so now we need
128 * to get input from the user.
129 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000130 for (;;) {
131 input = getc(cin);
132 input = tolower(input);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000133#if !ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko033e5922007-08-15 20:42:52 +0000134 printf("\033[A"); /* up cursor */
Erik Andersen4f3f7572000-04-28 00:18:56 +0000135#endif
Denis Vlasenko033e5922007-08-15 20:42:52 +0000136 /* Erase the last message */
137 printf("\r%*s\r", len, "");
138
139 /* Due to various multibyte escape
140 * sequences, it's not ok to accept
141 * any input as a command to scroll
142 * the screen. We only allow known
143 * commands, else we show help msg. */
144 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
145 break;
146 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
147 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000148 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000149 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000150 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000151
152 if (input == 'q')
153 goto end;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000154
155 /* The user may have resized the terminal.
156 * Re-read the dimensions. */
Denis Vlasenko856be772007-08-17 08:29:48 +0000157#if ENABLE_FEATURE_USE_TERMIOS
Denis Vlasenko033e5922007-08-15 20:42:52 +0000158 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
159 terminal_height -= 1;
Denis Vlasenko856be772007-08-17 08:29:48 +0000160#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000161 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000162
Denis Vlasenko033e5922007-08-15 20:42:52 +0000163 /* Crudely convert tabs into spaces, which are
164 * a bajillion times easier to deal with. */
165 if (c == '\t') {
166 spaces = CONVERTED_TAB_SIZE - 1;
167 c = ' ';
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000168 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000169
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000170 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000171 * There are two input streams to worry about here:
172 *
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000173 * c : the character we are reading from the file being "mored"
174 * input: a character received from the keyboard
Mark Whitleyb9913952000-06-16 00:26:51 +0000175 *
176 * If we hit a newline in the _file_ stream, we want to test and
177 * see if any characters have been hit in the _input_ stream. This
178 * allows the user to quit while in the middle of a file.
179 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000180 wrap = (++len > terminal_width);
181 if (c == '\n' || wrap) {
182 /* Then outputting this character
183 * will move us to a new line. */
184 if (++lines >= terminal_height || input == '\n')
185 please_display_more_prompt = 1;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000186 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000188 if (c != '\n' && wrap) {
189 /* Then outputting this will also put a character on
190 * the beginning of that new line. Thus we first want to
191 * display the prompt (if any), so we skip the putchar()
192 * and go back to the top of the loop, without reading
193 * a new character. */
194 goto loop_top;
195 }
196 /* My small mind cannot fathom backspaces and UTF-8 */
197 putchar(c);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000198 }
199 fclose(file);
200 fflush(stdout);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000201 } while (*argv && *++argv);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000202 end:
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000203 setTermSettings(cin_fileno, &initial_settings);
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000204 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205}