blob: ea5e22562165b1b78cf4644c6cf6bedebab7defd [file] [log] [blame]
Eric Andersen4bea32a1999-10-06 00:30:51 +00001/*
2 * Mini more implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Blended by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * based on the original more implementation and code from the Debian
8 * boot-floppies team.
Eric Andersen4bea32a1999-10-06 00:30:51 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
Eric Andersen3cf52d11999-10-12 22:26:06 +000026
27/* Turning this off makes things a bit smaller (and less pretty) */
28#define BB_MORE_TERM
29
30
31
Eric Andersencc8ed391999-10-05 16:24:54 +000032#include "internal.h"
33#include <stdio.h>
Eric Andersenf5a38381999-10-19 22:26:25 +000034#include <fcntl.h>
Eric Andersen4bea32a1999-10-06 00:30:51 +000035#include <signal.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersen3cf52d11999-10-12 22:26:06 +000037
Eric Andersene77ae3a1999-10-19 20:03:34 +000038static const char more_usage[] = "[file ...]";
Eric Andersencc8ed391999-10-05 16:24:54 +000039
Eric Andersen3cf52d11999-10-12 22:26:06 +000040
Eric Andersenf5a38381999-10-19 22:26:25 +000041/* ED: sparc termios is broken: revert back to old termio handling. */
Eric Andersen3cf52d11999-10-12 22:26:06 +000042#ifdef BB_MORE_TERM
Eric Andersenf5a38381999-10-19 22:26:25 +000043
Eric Andersenfbb39c81999-11-08 17:00:52 +000044#if #cpu(sparc)
Eric Andersenf5a38381999-10-19 22:26:25 +000045# define USE_OLD_TERMIO
46# include <termio.h>
47# include <sys/ioctl.h>
48# define termios termio
49# define stty(fd,argp) ioctl(fd,TCSETAF,argp)
50#else
51# include <termios.h>
52# define stty(fd,argp) tcsetattr(fd,TCSANOW,argp)
53#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000054
55 FILE *cin;
56 struct termios initial_settings, new_settings;
57
58 void gotsig(int sig) {
Eric Andersenf5a38381999-10-19 22:26:25 +000059 stty(fileno(cin), &initial_settings);
Eric Andersen3cf52d11999-10-12 22:26:06 +000060 exit( TRUE);
61 }
62#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000063
Eric Andersen4bea32a1999-10-06 00:30:51 +000064extern int more_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000065{
Eric Andersen21943ce1999-10-13 18:04:51 +000066 int c, lines=0, input=0;
Eric Andersenf5a38381999-10-19 22:26:25 +000067 int next_page=0;
Eric Andersen4bea32a1999-10-06 00:30:51 +000068 struct stat st;
Eric Andersenf5a38381999-10-19 22:26:25 +000069 FILE *file;
Eric Andersen4bea32a1999-10-06 00:30:51 +000070
Eric Andersen4bea32a1999-10-06 00:30:51 +000071 argc--;
72 argv++;
73
Eric Andersenfbb39c81999-11-08 17:00:52 +000074 if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) {
75 usage (more_usage);
76 }
77 do {
Eric Andersenf5a38381999-10-19 22:26:25 +000078 if (argc==0) {
79 file = stdin;
80 }
81 else
Eric Andersen9d3aba71999-10-06 09:04:55 +000082 file = fopen(*argv, "r");
Eric Andersenf5a38381999-10-19 22:26:25 +000083
Eric Andersen4bea32a1999-10-06 00:30:51 +000084 if (file == NULL) {
Eric Andersenef8b6c71999-10-20 08:05:35 +000085 perror(*argv);
Eric Andersen3cf52d11999-10-12 22:26:06 +000086 exit(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000087 }
Eric Andersen4bea32a1999-10-06 00:30:51 +000088 fstat(fileno(file), &st);
Eric Andersencc8ed391999-10-05 16:24:54 +000089
Eric Andersen3cf52d11999-10-12 22:26:06 +000090#ifdef BB_MORE_TERM
91 cin = fopen("/dev/tty", "r");
Eric Andersenf5a38381999-10-19 22:26:25 +000092 if (!cin)
93 cin = fopen("/dev/console", "r");
94#ifdef USE_OLD_TERMIO
95 ioctl(fileno(cin),TCGETA,&initial_settings);
96#else
Eric Andersen3cf52d11999-10-12 22:26:06 +000097 tcgetattr(fileno(cin),&initial_settings);
Eric Andersenf5a38381999-10-19 22:26:25 +000098#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +000099 new_settings = initial_settings;
100 new_settings.c_lflag &= ~ICANON;
101 new_settings.c_lflag &= ~ECHO;
Eric Andersenf5a38381999-10-19 22:26:25 +0000102 stty(fileno(cin), &new_settings);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000103
104 (void) signal(SIGINT, gotsig);
Eric Andersenfbb39c81999-11-08 17:00:52 +0000105 (void) signal(SIGQUIT, gotsig);
106 (void) signal(SIGTERM, gotsig);
107
Eric Andersen3cf52d11999-10-12 22:26:06 +0000108
Eric Andersen3cf52d11999-10-12 22:26:06 +0000109#endif
Eric Andersen4bea32a1999-10-06 00:30:51 +0000110 while ((c = getc(file)) != EOF) {
111 if ( next_page ) {
112 int len=0;
113 next_page = 0;
114 lines=0;
Eric Andersenf5a38381999-10-19 22:26:25 +0000115 len = fprintf(stdout, "--More-- ");
116 if (file != stdin) {
117 len += fprintf(stdout, "(%d%% of %ld bytes)",
Eric Andersen4bea32a1999-10-06 00:30:51 +0000118 (int) (100*( (double) ftell(file) / (double) st.st_size )),
Eric Andersenf5a38381999-10-19 22:26:25 +0000119 st.st_size);
120 }
121 len += fprintf(stdout, "%s",
Eric Andersen3cf52d11999-10-12 22:26:06 +0000122#ifdef BB_MORE_TERM
123 ""
124#else
125 "\n"
126#endif
127 );
128
Eric Andersen4bea32a1999-10-06 00:30:51 +0000129 fflush(stdout);
Eric Andersenf5a38381999-10-19 22:26:25 +0000130 input = getc( cin);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000131
132#ifdef BB_MORE_TERM
133 /* Erase the "More" message */
Eric Andersen4bea32a1999-10-06 00:30:51 +0000134 while(len-- > 0)
135 putc('\b', stdout);
Eric Andersenf5a38381999-10-19 22:26:25 +0000136 while(len++ < 79)
Eric Andersen9d3aba71999-10-06 09:04:55 +0000137 putc(' ', stdout);
Eric Andersen4bea32a1999-10-06 00:30:51 +0000138 while(len-- > 0)
139 putc('\b', stdout);
140 fflush(stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000141#endif
Eric Andersen3cf52d11999-10-12 22:26:06 +0000142
Eric Andersen4bea32a1999-10-06 00:30:51 +0000143 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000144 if (input=='q')
145 goto end;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000146 if (input=='\n' && c == '\n' )
Eric Andersen3cf52d11999-10-12 22:26:06 +0000147 next_page = 1;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000148 if ( c == ' ' && ++lines == 24 )
Eric Andersen4bea32a1999-10-06 00:30:51 +0000149 next_page = 1;
150 putc(c, stdout);
Eric Andersencc8ed391999-10-05 16:24:54 +0000151 }
Eric Andersen4bea32a1999-10-06 00:30:51 +0000152 fclose(file);
153 fflush(stdout);
154
Eric Andersen4bea32a1999-10-06 00:30:51 +0000155 argv++;
Eric Andersenfbb39c81999-11-08 17:00:52 +0000156 } while (--argc > 0);
Eric Andersen3cf52d11999-10-12 22:26:06 +0000157end:
158#ifdef BB_MORE_TERM
159 gotsig(0);
160#endif
161 exit(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000162}
Eric Andersen4bea32a1999-10-06 00:30:51 +0000163