ash: fix signal handling
[oweals/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 tarball for details.
15  */
16
17 #include "busybox.h"
18
19
20 #if ENABLE_FEATURE_USE_TERMIOS
21 static int cin_fileno;
22 #include <termios.h>
23 #define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
24 #define getTermSettings(fd, argp) tcgetattr(fd, argp);
25
26 static struct termios initial_settings, new_settings;
27
28 static void set_tty_to_initial_mode(void)
29 {
30         setTermSettings(cin_fileno, &initial_settings);
31 }
32
33 static void gotsig(int sig)
34 {
35         putchar('\n');
36         exit(EXIT_FAILURE);
37 }
38 #endif /* FEATURE_USE_TERMIOS */
39
40
41 int more_main(int argc, char **argv);
42 int more_main(int argc, char **argv)
43 {
44         int c, lines, input = 0;
45         int please_display_more_prompt = 0;
46         struct stat st;
47         FILE *file;
48         FILE *cin;
49         int len, page_height;
50         int terminal_width;
51         int terminal_height;
52
53         argv++;
54         /* Another popular pager, most, detects when stdout
55          * is not a tty and turns into cat. This makes sense. */
56         if (!isatty(STDOUT_FILENO))
57                 return bb_cat(argv);
58         cin = fopen(CURRENT_TTY, "r");
59         if (!cin)
60                 return bb_cat(argv);
61
62 #if ENABLE_FEATURE_USE_TERMIOS
63         cin_fileno = fileno(cin);
64         getTermSettings(cin_fileno, &initial_settings);
65         new_settings = initial_settings;
66         new_settings.c_lflag &= ~ICANON;
67         new_settings.c_lflag &= ~ECHO;
68         new_settings.c_cc[VMIN] = 1;
69         new_settings.c_cc[VTIME] = 0;
70         setTermSettings(cin_fileno, &new_settings);
71         atexit(set_tty_to_initial_mode);
72         signal(SIGINT, gotsig);
73         signal(SIGQUIT, gotsig);
74         signal(SIGTERM, gotsig);
75 #endif
76         please_display_more_prompt = 2;
77
78         do {
79                 file = stdin;
80                 if (*argv) {
81                         file = fopen_or_warn(*argv, "r");
82                         if (!file)
83                                 continue;
84                 }
85                 st.st_size = 0;
86                 fstat(fileno(file), &st);
87
88                 please_display_more_prompt &= ~1;
89                 /* never returns w, h <= 1 */
90                 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
91                 terminal_width -= 1;
92                 terminal_height -= 1;
93
94                 len = 0;
95                 lines = 0;
96                 page_height = terminal_height;
97                 while ((c = getc(file)) != EOF) {
98
99                         if ((please_display_more_prompt & 3) == 3) {
100                                 len = printf("--More-- ");
101                                 if (/*file != stdin &&*/ st.st_size > 0) {
102                                         len += printf("(%d%% of %"OFF_FMT"d bytes)",
103                                                 (int) (ftello(file)*100 / st.st_size),
104                                                 st.st_size);
105                                 }
106                                 fflush(stdout);
107
108                                 /*
109                                  * We've just displayed the "--More--" prompt, so now we need
110                                  * to get input from the user.
111                                  */
112                                 input = getc(cin);
113 #if !ENABLE_FEATURE_USE_TERMIOS
114                                 printf("\033[A"); /* up cursor */
115 #endif
116                                 /* Erase the "More" message */
117                                 printf("\r%*s\r", len, "");
118                                 len = 0;
119                                 lines = 0;
120                                 /* Bottom line on page will become top line
121                                  * after one page forward. Thus -1: */
122                                 page_height = terminal_height - 1;
123                                 please_display_more_prompt &= ~1;
124
125                                 if (input == 'q')
126                                         goto end;
127                         }
128
129                         /*
130                          * There are two input streams to worry about here:
131                          *
132                          * c     : the character we are reading from the file being "mored"
133                          * input : a character received from the keyboard
134                          *
135                          * If we hit a newline in the _file_ stream, we want to test and
136                          * see if any characters have been hit in the _input_ stream. This
137                          * allows the user to quit while in the middle of a file.
138                          */
139                         if (c == '\n') {
140                                 /* increment by just one line if we are at
141                                  * the end of this line */
142                                 if (input == '\n')
143                                         please_display_more_prompt |= 1;
144                                 /* Adjust the terminal height for any overlap, so that
145                                  * no lines get lost off the top. */
146                                 if (len >= terminal_width) {
147                                         int quot, rem;
148                                         quot = len / terminal_width;
149                                         rem  = len - (quot * terminal_width);
150                                         page_height -= (quot - 1);
151                                         if (rem)
152                                                 page_height--;
153                                 }
154                                 if (++lines >= page_height) {
155                                         please_display_more_prompt |= 1;
156                                 }
157                                 len = 0;
158                         }
159                         /*
160                          * If we just read a newline from the file being 'mored' and any
161                          * key other than a return is hit, scroll by one page
162                          */
163                         putc(c, stdout);
164                         /* My small mind cannot fathom tabs, backspaces,
165                          * and UTF-8 */
166                         len++;
167                 }
168                 fclose(file);
169                 fflush(stdout);
170         } while (*argv && *++argv);
171  end:
172         return 0;
173 }