rename functions to more understandable names
[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 #ifdef CONFIG_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 /* CONFIG_FEATURE_USE_TERMIOS */
39
40
41 int more_main(int argc, char **argv)
42 {
43         int c, lines, input = 0;
44         int please_display_more_prompt = 0;
45         struct stat st;
46         FILE *file;
47         FILE *cin;
48         int len, page_height;
49         int terminal_width;
50         int terminal_height;
51
52         argc--;
53         argv++;
54
55
56         /* not use inputing from terminal if usage: more > outfile */
57         if(isatty(STDOUT_FILENO)) {
58                 cin = fopen(CURRENT_TTY, "r");
59                 if (!cin)
60                         cin = xfopen(CONSOLE_DEV, "r");
61                 please_display_more_prompt = 2;
62 #ifdef CONFIG_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                 (void) signal(SIGINT, gotsig);
73                 (void) signal(SIGQUIT, gotsig);
74                 (void) signal(SIGTERM, gotsig);
75 #endif
76         } else {
77                 cin = stdin;
78         }
79
80         do {
81                 if (argc == 0) {
82                         file = stdin;
83                 } else
84                         file = fopen_or_warn(*argv, "r");
85                 if(file==0)
86                         goto loop;
87
88                 st.st_size = 0;
89                 fstat(fileno(file), &st);
90
91                 please_display_more_prompt &= ~1;
92
93                 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
94                 if (terminal_height > 4)
95                         terminal_height -= 2;
96                 if (terminal_width > 0)
97                         terminal_width -= 1;
98
99                 len=0;
100                 lines = 0;
101                 page_height = terminal_height;
102                 while ((c = getc(file)) != EOF) {
103
104                         if ((please_display_more_prompt & 3) == 3) {
105                                 len = printf("--More-- ");
106                                 if (file != stdin && st.st_size > 0) {
107 #if _FILE_OFFSET_BITS == 64
108                                         len += printf("(%d%% of %lld bytes)",
109                                                    (int) (100 * ((double) ftell(file) /
110                                                    (double) st.st_size)), (long long)st.st_size);
111 #else
112                                         len += printf("(%d%% of %ld bytes)",
113                                                    (int) (100 * ((double) ftell(file) /
114                                                    (double) st.st_size)), (long)st.st_size);
115 #endif
116                                 }
117
118                                 fflush(stdout);
119
120                                 /*
121                                  * We've just displayed the "--More--" prompt, so now we need
122                                  * to get input from the user.
123                                  */
124                                 input = getc(cin);
125 #ifndef CONFIG_FEATURE_USE_TERMIOS
126                                 printf("\033[A"); /* up cursor */
127 #endif
128                                 /* Erase the "More" message */
129                                 putc('\r', stdout);
130                                 while (--len >= 0)
131                                         putc(' ', stdout);
132                                 putc('\r', stdout);
133                                 len=0;
134                                 lines = 0;
135                                 page_height = terminal_height;
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                                         if (quot) {
164                                                 if (rem)
165                                                         page_height-=quot;
166                                                 else
167                                                         page_height-=(quot-1);
168                                         }
169                                 }
170                                 if (++lines >= page_height) {
171                                         please_display_more_prompt |= 1;
172                                 }
173                                 len=0;
174                         }
175                         /*
176                          * If we just read a newline from the file being 'mored' and any
177                          * key other than a return is hit, scroll by one page
178                          */
179                         putc(c, stdout);
180                         len++;
181                 }
182                 fclose(file);
183                 fflush(stdout);
184 loop:
185                 argv++;
186         } while (--argc > 0);
187   end:
188         return 0;
189 }