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