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