7160b8b00b20dff4f85638379731e1bfb62e8bad
[platform/upstream/busybox.git] / util-linux / more.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini more implementation for busybox
4  *
5  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  *
8  * Latest version blended together by Erik Andersen <andersen@codepoet.org>,
9  * based on the original more implementation by Bruce, and code from the
10  * Debian boot-floppies team.
11  *
12  * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
13  *
14  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
15  */
16
17 #include "libbb.h"
18
19 /* Support for FEATURE_USE_TERMIOS */
20
21 struct globals {
22         int cin_fileno;
23         struct termios initial_settings;
24         struct termios new_settings;
25 } FIX_ALIASING;
26 #define G (*(struct globals*)bb_common_bufsiz1)
27 #define INIT_G() ((void)0)
28 #define initial_settings (G.initial_settings)
29 #define new_settings     (G.new_settings    )
30 #define cin_fileno       (G.cin_fileno      )
31
32 #define setTermSettings(fd, argp) \
33 do { \
34         if (ENABLE_FEATURE_USE_TERMIOS) \
35                 tcsetattr(fd, TCSANOW, argp); \
36 } while (0)
37 #define getTermSettings(fd, argp) tcgetattr(fd, argp)
38
39 static void gotsig(int sig UNUSED_PARAM)
40 {
41         /* bb_putchar_stderr doesn't use stdio buffering,
42          * therefore it is safe in signal handler */
43         bb_putchar_stderr('\n');
44         setTermSettings(cin_fileno, &initial_settings);
45         _exit(EXIT_FAILURE);
46 }
47
48 #define CONVERTED_TAB_SIZE 8
49
50 int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
51 int more_main(int argc UNUSED_PARAM, char **argv)
52 {
53         int c = c; /* for compiler */
54         int lines;
55         int input = 0;
56         int spaces = 0;
57         int please_display_more_prompt;
58         struct stat st;
59         FILE *file;
60         FILE *cin;
61         int len;
62         unsigned terminal_width;
63         unsigned terminal_height;
64
65         INIT_G();
66
67         argv++;
68         /* Another popular pager, most, detects when stdout
69          * is not a tty and turns into cat. This makes sense. */
70         if (!isatty(STDOUT_FILENO))
71                 return bb_cat(argv);
72         cin = fopen_for_read(CURRENT_TTY);
73         if (!cin)
74                 return bb_cat(argv);
75
76         if (ENABLE_FEATURE_USE_TERMIOS) {
77                 cin_fileno = fileno(cin);
78                 getTermSettings(cin_fileno, &initial_settings);
79                 new_settings = initial_settings;
80                 new_settings.c_lflag &= ~ICANON;
81                 new_settings.c_lflag &= ~ECHO;
82                 new_settings.c_cc[VMIN] = 1;
83                 new_settings.c_cc[VTIME] = 0;
84                 setTermSettings(cin_fileno, &new_settings);
85                 bb_signals(0
86                         + (1 << SIGINT)
87                         + (1 << SIGQUIT)
88                         + (1 << SIGTERM)
89                         , gotsig);
90         }
91
92         do {
93                 file = stdin;
94                 if (*argv) {
95                         file = fopen_or_warn(*argv, "r");
96                         if (!file)
97                                 continue;
98                 }
99                 st.st_size = 0;
100                 fstat(fileno(file), &st);
101
102                 please_display_more_prompt = 0;
103                 /* never returns w, h <= 1 */
104                 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
105                 terminal_height -= 1;
106
107                 len = 0;
108                 lines = 0;
109                 while (spaces || (c = getc(file)) != EOF) {
110                         int wrap;
111                         if (spaces)
112                                 spaces--;
113  loop_top:
114                         if (input != 'r' && please_display_more_prompt) {
115                                 len = printf("--More-- ");
116                                 if (st.st_size != 0) {
117                                         uoff_t d = (uoff_t)st.st_size / 100;
118                                         if (d == 0)
119                                                 d = 1;
120                                         len += printf("(%u%% of %"OFF_FMT"u bytes)",
121                                                 (int) ((uoff_t)ftello(file) / d),
122                                                 st.st_size);
123                                 }
124                                 fflush_all();
125
126                                 /*
127                                  * We've just displayed the "--More--" prompt, so now we need
128                                  * to get input from the user.
129                                  */
130                                 for (;;) {
131                                         input = getc(cin);
132                                         input = tolower(input);
133                                         if (!ENABLE_FEATURE_USE_TERMIOS)
134                                                 printf("\033[A"); /* cursor up */
135                                         /* Erase the last message */
136                                         printf("\r%*s\r", len, "");
137
138                                         /* Due to various multibyte escape
139                                          * sequences, it's not ok to accept
140                                          * any input as a command to scroll
141                                          * the screen. We only allow known
142                                          * commands, else we show help msg. */
143                                         if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
144                                                 break;
145                                         len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
146                                 }
147                                 len = 0;
148                                 lines = 0;
149                                 please_display_more_prompt = 0;
150
151                                 if (input == 'q')
152                                         goto end;
153
154                                 /* The user may have resized the terminal.
155                                  * Re-read the dimensions. */
156                                 if (ENABLE_FEATURE_USE_TERMIOS) {
157                                         get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
158                                         terminal_height -= 1;
159                                 }
160                         }
161
162                         /* Crudely convert tabs into spaces, which are
163                          * a bajillion times easier to deal with. */
164                         if (c == '\t') {
165                                 spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
166                                 c = ' ';
167                         }
168
169                         /*
170                          * There are two input streams to worry about here:
171                          *
172                          * c    : the character we are reading from the file being "mored"
173                          * input: a character received from the keyboard
174                          *
175                          * If we hit a newline in the _file_ stream, we want to test and
176                          * see if any characters have been hit in the _input_ stream. This
177                          * allows the user to quit while in the middle of a file.
178                          */
179                         wrap = (++len > terminal_width);
180                         if (c == '\n' || wrap) {
181                                 /* Then outputting this character
182                                  * will move us to a new line. */
183                                 if (++lines >= terminal_height || input == '\n')
184                                         please_display_more_prompt = 1;
185                                 len = 0;
186                         }
187                         if (c != '\n' && wrap) {
188                                 /* Then outputting this will also put a character on
189                                  * the beginning of that new line. Thus we first want to
190                                  * display the prompt (if any), so we skip the putchar()
191                                  * and go back to the top of the loop, without reading
192                                  * a new character. */
193                                 goto loop_top;
194                         }
195                         /* My small mind cannot fathom backspaces and UTF-8 */
196                         putchar(c);
197                         die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
198                 }
199                 fclose(file);
200                 fflush_all();
201         } while (*argv && *++argv);
202  end:
203         setTermSettings(cin_fileno, &initial_settings);
204         return 0;
205 }