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