Extract usage information into a separate file.
[oweals/busybox.git] / more.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini more implementation for busybox
4  *
5  *
6  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7  *
8  * Latest version blended together by Erik Andersen <andersen@lineo.com>,
9  * based on the original more implementation by Bruce, and code from the 
10  * Debian boot-floppies team.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */
27
28 #include "internal.h"
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <sys/ioctl.h>
33 #define BB_DECLARE_EXTERN
34 #define bb_need_help
35 #include "messages.c"
36
37 /* ED: sparc termios is broken: revert back to old termio handling. */
38 #ifdef BB_FEATURE_USE_TERMIOS
39 #       if #cpu(sparc)
40 #               include <termio.h>
41 #               define termios termio
42 #               define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
43 #               define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
44 #       else
45 #               include <termios.h>
46 #               define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
47 #               define getTermSettings(fd,argp) tcgetattr(fd, argp);
48 #       endif
49
50 FILE *cin;
51
52 struct termios initial_settings, new_settings;
53
54 void gotsig(int sig)
55 {
56         setTermSettings(fileno(cin), &initial_settings);
57         fprintf(stdout, "\n");
58         exit(TRUE);
59 }
60 #endif /* BB_FEATURE_USE_TERMIOS */
61
62
63 static int terminal_width = 79; /* not 80 in case terminal has linefold bug */
64 static int terminal_height = 24;
65
66
67 extern int more_main(int argc, char **argv)
68 {
69         int c, lines = 0, input = 0;
70         int please_display_more_prompt = 0;
71         struct stat st;
72         FILE *file;
73
74 #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
75         struct winsize win = { 0, 0, 0, 0 };
76 #endif
77
78         argc--;
79         argv++;
80
81         if (argc > 0
82                 && (strcmp(*argv, dash_dash_help) == 0 || strcmp(*argv, "-h") == 0)) {
83                 usage(more_usage);
84         }
85         do {
86                 if (argc == 0) {
87                         file = stdin;
88                 } else
89                         file = fopen(*argv, "r");
90
91                 if (file == NULL) {
92                         perror(*argv);
93                         exit(FALSE);
94                 }
95                 fstat(fileno(file), &st);
96
97 #ifdef BB_FEATURE_USE_TERMIOS
98                 cin = fopen("/dev/tty", "r");
99                 if (!cin)
100                         cin = fopen("/dev/console", "r");
101                 getTermSettings(fileno(cin), &initial_settings);
102                 new_settings = initial_settings;
103                 new_settings.c_cc[VMIN] = 1;
104                 new_settings.c_cc[VTIME] = 0;
105                 new_settings.c_lflag &= ~ICANON;
106                 new_settings.c_lflag &= ~ECHO;
107                 setTermSettings(fileno(cin), &new_settings);
108
109 #       ifdef BB_FEATURE_AUTOWIDTH
110                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
111                 if (win.ws_row > 4)
112                         terminal_height = win.ws_row - 2;
113                 if (win.ws_col > 0)
114                         terminal_width = win.ws_col - 1;
115 #       endif
116
117                 (void) signal(SIGINT, gotsig);
118                 (void) signal(SIGQUIT, gotsig);
119                 (void) signal(SIGTERM, gotsig);
120
121 #endif
122                 while ((c = getc(file)) != EOF) {
123                         if (please_display_more_prompt) {
124                                 int len = 0;
125
126                                 please_display_more_prompt = 0;
127                                 lines = 0;
128                                 len = fprintf(stdout, "--More-- ");
129                                 if (file != stdin) {
130                                         len += fprintf(stdout, "(%d%% of %ld bytes)",
131                                                                    (int) (100 *
132                                                                                   ((double) ftell(file) /
133                                                                                    (double) st.st_size)),
134                                                                    st.st_size);
135                                 }
136                                 len += fprintf(stdout, "%s",
137 #ifdef BB_FEATURE_USE_TERMIOS
138                                                            ""
139 #else
140                                                            "\n"
141 #endif
142                                         );
143
144                                 fflush(stdout);
145
146                                 /*
147                                  * We've just displayed the "--More--" prompt, so now we need
148                                  * to get input from the user.
149                                  */
150 #ifdef BB_FEATURE_USE_TERMIOS
151                                 input = getc(cin);
152 #else
153                                 input = getc(stdin);
154 #endif
155
156 #ifdef BB_FEATURE_USE_TERMIOS
157                                 /* Erase the "More" message */
158                                 while (--len >= 0)
159                                         putc('\b', stdout);
160                                 while (++len <= terminal_width)
161                                         putc(' ', stdout);
162                                 while (--len >= 0)
163                                         putc('\b', stdout);
164                                 fflush(stdout);
165 #endif
166
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                         if (c == '\n') {
180                                 switch (input) {
181                                 case 'q':
182                                         goto end;
183                                 case '\n':
184                                         /* increment by just one line if we are at 
185                                          * the end of this line*/
186                                         please_display_more_prompt = 1;
187                                         break;
188                                 }
189                                 if (++lines == terminal_height)
190                                         please_display_more_prompt = 1;
191                         }
192                         /*
193                          * If we just read a newline from the file being 'mored' and any
194                          * key other than a return is hit, scroll by one page
195                          */
196                         putc(c, stdout);
197                 }
198                 fclose(file);
199                 fflush(stdout);
200
201                 argv++;
202         } while (--argc > 0);
203   end:
204 #ifdef BB_FEATURE_USE_TERMIOS
205         gotsig(0);
206 #endif
207         return(TRUE);
208 }