source: MondoRescue/branches/2.2.9/mindi-busybox/util-linux/more.c@ 2725

Last change on this file since 2725 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 5.3 KB
RevLine 
[821]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 *
[2725]14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]15 */
16
[1765]17#include "libbb.h"
[821]18
[2725]19/* Support for FEATURE_USE_TERMIOS */
[821]20
[1765]21struct globals {
22 int cin_fileno;
23 struct termios initial_settings;
24 struct termios new_settings;
[2725]25} FIX_ALIASING;
[1765]26#define G (*(struct globals*)bb_common_bufsiz1)
27#define INIT_G() ((void)0)
28#define initial_settings (G.initial_settings)
29#define new_settings (G.new_settings )
30#define cin_fileno (G.cin_fileno )
[821]31
[2725]32#define setTermSettings(fd, argp) \
33do { \
34 if (ENABLE_FEATURE_USE_TERMIOS) \
35 tcsetattr(fd, TCSANOW, argp); \
36} while (0)
[1765]37#define getTermSettings(fd, argp) tcgetattr(fd, argp)
[821]38
[2725]39static void gotsig(int sig UNUSED_PARAM)
[821]40{
[2725]41 /* bb_putchar_stderr doesn't use stdio buffering,
42 * therefore it is safe in signal handler */
43 bb_putchar_stderr('\n');
[1765]44 setTermSettings(cin_fileno, &initial_settings);
[2725]45 _exit(EXIT_FAILURE);
[821]46}
47
[1765]48#define CONVERTED_TAB_SIZE 8
49
[2725]50int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
51int more_main(int argc UNUSED_PARAM, char **argv)
[821]52{
[2725]53 int c = c; /* for compiler */
[1765]54 int lines;
55 int input = 0;
56 int spaces = 0;
57 int please_display_more_prompt;
[821]58 struct stat st;
59 FILE *file;
60 FILE *cin;
[1765]61 int len;
[2725]62 unsigned terminal_width;
63 unsigned terminal_height;
[821]64
[1765]65 INIT_G();
66
[821]67 argv++;
[1765]68 /* Another popular pager, most, detects when stdout
69 * is not a tty and turns into cat. This makes sense. */
70 if (!isatty(STDOUT_FILENO))
71 return bb_cat(argv);
[2725]72 cin = fopen_for_read(CURRENT_TTY);
[1765]73 if (!cin)
74 return bb_cat(argv);
[821]75
[2725]76 if (ENABLE_FEATURE_USE_TERMIOS) {
77 cin_fileno = fileno(cin);
78 getTermSettings(cin_fileno, &initial_settings);
79 new_settings = initial_settings;
80 new_settings.c_lflag &= ~ICANON;
81 new_settings.c_lflag &= ~ECHO;
82 new_settings.c_cc[VMIN] = 1;
83 new_settings.c_cc[VTIME] = 0;
84 setTermSettings(cin_fileno, &new_settings);
85 bb_signals(0
86 + (1 << SIGINT)
87 + (1 << SIGQUIT)
88 + (1 << SIGTERM)
89 , gotsig);
90 }
[821]91
92 do {
[1765]93 file = stdin;
94 if (*argv) {
95 file = fopen_or_warn(*argv, "r");
96 if (!file)
97 continue;
98 }
[821]99 st.st_size = 0;
100 fstat(fileno(file), &st);
101
[1765]102 please_display_more_prompt = 0;
103 /* never returns w, h <= 1 */
[821]104 get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
[1765]105 terminal_height -= 1;
[821]106
[1765]107 len = 0;
[821]108 lines = 0;
[1765]109 while (spaces || (c = getc(file)) != EOF) {
110 int wrap;
111 if (spaces)
112 spaces--;
113 loop_top:
114 if (input != 'r' && please_display_more_prompt) {
[821]115 len = printf("--More-- ");
[1765]116 if (st.st_size > 0) {
[2725]117 len += printf("(%u%% of %"OFF_FMT"u bytes)",
[1765]118 (int) (ftello(file)*100 / st.st_size),
119 st.st_size);
[821]120 }
[2725]121 fflush_all();
[821]122
123 /*
124 * We've just displayed the "--More--" prompt, so now we need
125 * to get input from the user.
126 */
[1765]127 for (;;) {
128 input = getc(cin);
129 input = tolower(input);
[2725]130 if (!ENABLE_FEATURE_USE_TERMIOS)
131 printf("\033[A"); /* cursor up */
[1765]132 /* Erase the last message */
133 printf("\r%*s\r", len, "");
134
135 /* Due to various multibyte escape
136 * sequences, it's not ok to accept
137 * any input as a command to scroll
138 * the screen. We only allow known
139 * commands, else we show help msg. */
140 if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
141 break;
142 len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
143 }
144 len = 0;
[821]145 lines = 0;
[1765]146 please_display_more_prompt = 0;
[821]147
148 if (input == 'q')
149 goto end;
[1765]150
151 /* The user may have resized the terminal.
152 * Re-read the dimensions. */
[2725]153 if (ENABLE_FEATURE_USE_TERMIOS) {
154 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
155 terminal_height -= 1;
156 }
[821]157 }
158
[1765]159 /* Crudely convert tabs into spaces, which are
160 * a bajillion times easier to deal with. */
161 if (c == '\t') {
162 spaces = CONVERTED_TAB_SIZE - 1;
163 c = ' ';
[2725]164 }
[1765]165
[821]166 /*
167 * There are two input streams to worry about here:
168 *
[1765]169 * c : the character we are reading from the file being "mored"
170 * input: a character received from the keyboard
[821]171 *
172 * If we hit a newline in the _file_ stream, we want to test and
173 * see if any characters have been hit in the _input_ stream. This
174 * allows the user to quit while in the middle of a file.
175 */
[1765]176 wrap = (++len > terminal_width);
177 if (c == '\n' || wrap) {
178 /* Then outputting this character
179 * will move us to a new line. */
180 if (++lines >= terminal_height || input == '\n')
181 please_display_more_prompt = 1;
182 len = 0;
[821]183 }
[1765]184 if (c != '\n' && wrap) {
185 /* Then outputting this will also put a character on
186 * the beginning of that new line. Thus we first want to
187 * display the prompt (if any), so we skip the putchar()
188 * and go back to the top of the loop, without reading
189 * a new character. */
190 goto loop_top;
191 }
192 /* My small mind cannot fathom backspaces and UTF-8 */
193 putchar(c);
[821]194 }
195 fclose(file);
[2725]196 fflush_all();
[1765]197 } while (*argv && *++argv);
198 end:
199 setTermSettings(cin_fileno, &initial_settings);
[821]200 return 0;
201}
Note: See TracBrowser for help on using the repository browser.