blob: 555f033d4cb92fa64dac52617c2bb92aaa843deb [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"
Erik Andersen4f3f7572000-04-28 00:18:56 +000018
Denys Vlasenko53f17912009-06-05 14:55:26 +020019/* Support for FEATURE_USE_TERMIOS */
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000020
21struct globals {
22 int cin_fileno;
23 struct termios initial_settings;
24 struct termios new_settings;
25};
26#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko033e5922007-08-15 20:42:52 +000027#define INIT_G() ((void)0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000028#define initial_settings (G.initial_settings)
29#define new_settings (G.new_settings )
30#define cin_fileno (G.cin_fileno )
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000031
Denys Vlasenko53f17912009-06-05 14:55:26 +020032#define setTermSettings(fd, argp) do { \
33 if (ENABLE_FEATURE_USE_TERMIOS) tcsetattr(fd, TCSANOW, argp); \
34 } while(0)
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000035#define getTermSettings(fd, argp) tcgetattr(fd, argp)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000036
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000037static void gotsig(int sig UNUSED_PARAM)
Glenn L McGrath78b0e372001-06-26 02:06:08 +000038{
Denis Vlasenko4daad902007-09-27 10:20:47 +000039 bb_putchar('\n');
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000040 setTermSettings(cin_fileno, &initial_settings);
Matt Kraai12f417e2001-01-18 02:57:08 +000041 exit(EXIT_FAILURE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000042}
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000043
Denis Vlasenko033e5922007-08-15 20:42:52 +000044#define CONVERTED_TAB_SIZE 8
Eric Andersen50d63601999-11-09 01:47:36 +000045
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000046int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000047int more_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000048{
Denis Vlasenko033e5922007-08-15 20:42:52 +000049 int c = c; /* for gcc */
50 int lines;
51 int input = 0;
52 int spaces = 0;
53 int please_display_more_prompt;
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 struct stat st;
55 FILE *file;
Eric Andersenedd580a2004-03-27 09:49:57 +000056 FILE *cin;
Denis Vlasenko033e5922007-08-15 20:42:52 +000057 int len;
Denis Vlasenko55995022008-05-18 22:28:26 +000058 unsigned terminal_width;
59 unsigned terminal_height;
Erik Andersene49d5ec2000-02-08 19:58:47 +000060
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +000061 INIT_G();
62
Eric Andersen4bea32a1999-10-06 00:30:51 +000063 argv++;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000064 /* Another popular pager, most, detects when stdout
65 * is not a tty and turns into cat. This makes sense. */
66 if (!isatty(STDOUT_FILENO))
67 return bb_cat(argv);
Denis Vlasenko5415c852008-07-21 23:05:26 +000068 cin = fopen_for_read(CURRENT_TTY);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000069 if (!cin)
70 return bb_cat(argv);
Eric Andersen4bea32a1999-10-06 00:30:51 +000071
Denys Vlasenko53f17912009-06-05 14:55:26 +020072 if (ENABLE_FEATURE_USE_TERMIOS) {
73 cin_fileno = fileno(cin);
74 getTermSettings(cin_fileno, &initial_settings);
75 new_settings = initial_settings;
76 new_settings.c_lflag &= ~ICANON;
77 new_settings.c_lflag &= ~ECHO;
78 new_settings.c_cc[VMIN] = 1;
79 new_settings.c_cc[VTIME] = 0;
80 setTermSettings(cin_fileno, &new_settings);
81 bb_signals(0
82 + (1 << SIGINT)
83 + (1 << SIGQUIT)
84 + (1 << SIGTERM)
85 , gotsig);
86 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +000087
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 do {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000089 file = stdin;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000090 if (*argv) {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000091 file = fopen_or_warn(*argv, "r");
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000092 if (!file)
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000093 continue;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +000094 }
Eric Andersen0ee0a8d2001-12-06 07:24:29 +000095 st.st_size = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 fstat(fileno(file), &st);
97
Denis Vlasenko033e5922007-08-15 20:42:52 +000098 please_display_more_prompt = 0;
Denis Vlasenko4eb8b932007-03-10 16:32:14 +000099 /* never returns w, h <= 1 */
Rob Landleyc44bc982006-05-28 01:19:06 +0000100 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000101 terminal_height -= 1;
Eric Andersen8efe9672003-09-15 08:33:45 +0000102
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000103 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000104 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000105 while (spaces || (c = getc(file)) != EOF) {
106 int wrap;
107 if (spaces)
108 spaces--;
109 loop_top:
110 if (input != 'r' && please_display_more_prompt) {
Matt Kraai12f417e2001-01-18 02:57:08 +0000111 len = printf("--More-- ");
Denis Vlasenko033e5922007-08-15 20:42:52 +0000112 if (st.st_size > 0) {
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000113 len += printf("(%d%% of %"OFF_FMT"d bytes)",
114 (int) (ftello(file)*100 / st.st_size),
115 st.st_size);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100117 fflush_all();
Mark Whitleyb9913952000-06-16 00:26:51 +0000118
119 /*
120 * We've just displayed the "--More--" prompt, so now we need
121 * to get input from the user.
122 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000123 for (;;) {
124 input = getc(cin);
125 input = tolower(input);
Denys Vlasenko53f17912009-06-05 14:55:26 +0200126 if (!ENABLE_FEATURE_USE_TERMIOS)
127 printf("\033[A"); /* cursor up */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000128 /* Erase the last message */
129 printf("\r%*s\r", len, "");
130
131 /* Due to various multibyte escape
132 * sequences, it's not ok to accept
133 * any input as a command to scroll
134 * the screen. We only allow known
135 * commands, else we show help msg. */
136 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
137 break;
138 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
139 }
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000140 len = 0;
Eric Andersen57239342001-02-23 01:39:26 +0000141 lines = 0;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000142 please_display_more_prompt = 0;
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000143
144 if (input == 'q')
145 goto end;
Denis Vlasenko033e5922007-08-15 20:42:52 +0000146
147 /* The user may have resized the terminal.
148 * Re-read the dimensions. */
Denys Vlasenko53f17912009-06-05 14:55:26 +0200149 if (ENABLE_FEATURE_USE_TERMIOS) {
150 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
151 terminal_height -= 1;
152 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 }
Mark Whitleyb9913952000-06-16 00:26:51 +0000154
Denis Vlasenko033e5922007-08-15 20:42:52 +0000155 /* Crudely convert tabs into spaces, which are
156 * a bajillion times easier to deal with. */
157 if (c == '\t') {
158 spaces = CONVERTED_TAB_SIZE - 1;
159 c = ' ';
Denis Vlasenkof7be20e2007-12-24 14:09:19 +0000160 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000161
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000162 /*
Mark Whitleyb9913952000-06-16 00:26:51 +0000163 * There are two input streams to worry about here:
164 *
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000165 * c : the character we are reading from the file being "mored"
166 * input: a character received from the keyboard
Mark Whitleyb9913952000-06-16 00:26:51 +0000167 *
168 * If we hit a newline in the _file_ stream, we want to test and
169 * see if any characters have been hit in the _input_ stream. This
170 * allows the user to quit while in the middle of a file.
171 */
Denis Vlasenko033e5922007-08-15 20:42:52 +0000172 wrap = (++len > terminal_width);
173 if (c == '\n' || wrap) {
174 /* Then outputting this character
175 * will move us to a new line. */
176 if (++lines >= terminal_height || input == '\n')
177 please_display_more_prompt = 1;
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000178 len = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000179 }
Denis Vlasenko033e5922007-08-15 20:42:52 +0000180 if (c != '\n' && wrap) {
181 /* Then outputting this will also put a character on
182 * the beginning of that new line. Thus we first want to
183 * display the prompt (if any), so we skip the putchar()
184 * and go back to the top of the loop, without reading
185 * a new character. */
186 goto loop_top;
187 }
188 /* My small mind cannot fathom backspaces and UTF-8 */
189 putchar(c);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 }
191 fclose(file);
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100192 fflush_all();
Denis Vlasenko4eb8b932007-03-10 16:32:14 +0000193 } while (*argv && *++argv);
Denis Vlasenkob15b7f72006-12-10 01:57:29 +0000194 end:
Denis Vlasenkoc2f011a2007-05-31 21:31:56 +0000195 setTermSettings(cin_fileno, &initial_settings);
Matt Kraaid6cde0b2001-04-12 20:51:01 +0000196 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197}