busybox/util-linux/more.c
<<
>>
Prefs
   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 *
  14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  15 */
  16//config:config MORE
  17//config:       bool "more (7 kb)"
  18//config:       default y
  19//config:       help
  20//config:       more is a simple utility which allows you to read text one screen
  21//config:       sized page at a time. If you want to read text that is larger than
  22//config:       the screen, and you are using anything faster than a 300 baud modem,
  23//config:       you will probably find this utility very helpful. If you don't have
  24//config:       any need to reading text files, you can leave this disabled.
  25
  26//applet:IF_MORE(APPLET(more, BB_DIR_BIN, BB_SUID_DROP))
  27
  28//kbuild:lib-$(CONFIG_MORE) += more.o
  29
  30//usage:#define more_trivial_usage
  31//usage:       "[FILE]..."
  32//usage:#define more_full_usage "\n\n"
  33//usage:       "View FILE (or stdin) one screenful at a time"
  34//usage:
  35//usage:#define more_example_usage
  36//usage:       "$ dmesg | more\n"
  37
  38#include "libbb.h"
  39#include "common_bufsiz.h"
  40
  41struct globals {
  42        int tty_fileno;
  43        unsigned terminal_width;
  44        unsigned terminal_height;
  45        struct termios initial_settings;
  46} FIX_ALIASING;
  47#define G (*(struct globals*)bb_common_bufsiz1)
  48#define INIT_G() do { setup_common_bufsiz(); } while (0)
  49
  50static void get_wh(void)
  51{
  52        /* never returns w, h <= 1 */
  53        get_terminal_width_height(G.tty_fileno, &G.terminal_width, &G.terminal_height);
  54        G.terminal_height -= 1;
  55}
  56
  57static void tcsetattr_tty_TCSANOW(struct termios *settings)
  58{
  59        tcsetattr(G.tty_fileno, TCSANOW, settings);
  60}
  61
  62static void gotsig(int sig UNUSED_PARAM)
  63{
  64        /* bb_putchar_stderr doesn't use stdio buffering,
  65         * therefore it is safe in signal handler */
  66        bb_putchar_stderr('\n');
  67        tcsetattr_tty_TCSANOW(&G.initial_settings);
  68        _exit(EXIT_FAILURE);
  69}
  70
  71#define CONVERTED_TAB_SIZE 8
  72
  73int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  74int more_main(int argc UNUSED_PARAM, char **argv)
  75{
  76        int c = c; /* for compiler */
  77        int input = 0;
  78        int spaces = 0;
  79        int please_display_more_prompt;
  80        FILE *tty;
  81
  82        INIT_G();
  83
  84        /* Parse options */
  85        /* Accepted but ignored: */
  86        /* -d   Display help instead of ringing bell */
  87        /* -e   Exit immediately after writing the last line */
  88        /* -f   Count logical lines (IOW: long lines are not folded) */
  89        /* -l   Do not pause after any line containing a ^L (form feed) */
  90        /* -s   Squeeze blank lines into one */
  91        /* -u   Suppress underlining */
  92        getopt32(argv, "deflsu");
  93        argv += optind;
  94
  95        /* Another popular pager, most, detects when stdout
  96         * is not a tty and turns into cat. This makes sense. */
  97        if (!isatty(STDOUT_FILENO))
  98                return bb_cat(argv);
  99        tty = fopen_for_read(CURRENT_TTY);
 100        if (!tty)
 101                return bb_cat(argv);
 102
 103        G.tty_fileno = fileno(tty);
 104
 105        /* Turn on unbuffered input; turn off echoing */
 106        set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0);
 107        bb_signals(BB_FATAL_SIGS, gotsig);
 108
 109        do {
 110                struct stat st;
 111                FILE *file;
 112                int len;
 113                int lines;
 114
 115                file = stdin;
 116                if (*argv) {
 117                        file = fopen_or_warn(*argv, "r");
 118                        if (!file)
 119                                continue;
 120                }
 121                st.st_size = 0;
 122                fstat(fileno(file), &st);
 123
 124                get_wh();
 125
 126                please_display_more_prompt = 0;
 127                len = 0;
 128                lines = 0;
 129                for (;;) {
 130                        int wrap;
 131
 132                        if (spaces)
 133                                spaces--;
 134                        else {
 135                                c = getc(file);
 136                                if (c == EOF) break;
 137                        }
 138 loop_top:
 139                        if (input != 'r' && please_display_more_prompt) {
 140                                len = printf("--More-- ");
 141                                if (st.st_size != 0) {
 142                                        uoff_t d = (uoff_t)st.st_size / 100;
 143                                        if (d == 0)
 144                                                d = 1;
 145                                        len += printf("(%u%% of %"OFF_FMT"u bytes)",
 146                                                (int) ((uoff_t)ftello(file) / d),
 147                                                st.st_size);
 148                                }
 149
 150                                /*
 151                                 * We've just displayed the "--More--" prompt, so now we need
 152                                 * to get input from the user.
 153                                 */
 154                                for (;;) {
 155                                        fflush_all();
 156                                        input = getc(tty);
 157                                        input = tolower(input);
 158                                        /* Erase the last message */
 159                                        printf("\r%*s\r", len, "");
 160
 161                                        if (input == 'q')
 162                                                goto end;
 163                                        /* Due to various multibyte escape
 164                                         * sequences, it's not ok to accept
 165                                         * any input as a command to scroll
 166                                         * the screen. We only allow known
 167                                         * commands, else we show help msg. */
 168                                        if (input == ' ' || input == '\n' || input == 'r')
 169                                                break;
 170                                        len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
 171                                }
 172                                len = 0;
 173                                lines = 0;
 174                                please_display_more_prompt = 0;
 175
 176                                /* The user may have resized the terminal.
 177                                 * Re-read the dimensions. */
 178                                get_wh();
 179                        }
 180
 181                        /* Crudely convert tabs into spaces, which are
 182                         * a bajillion times easier to deal with. */
 183                        if (c == '\t') {
 184                                spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
 185                                c = ' ';
 186                        }
 187
 188                        /*
 189                         * There are two input streams to worry about here:
 190                         *
 191                         * c    : the character we are reading from the file being "mored"
 192                         * input: a character received from the keyboard
 193                         *
 194                         * If we hit a newline in the _file_ stream, we want to test and
 195                         * see if any characters have been hit in the _input_ stream. This
 196                         * allows the user to quit while in the middle of a file.
 197                         */
 198                        wrap = (++len > G.terminal_width);
 199                        if (c == '\n' || wrap) {
 200                                /* Then outputting this character
 201                                 * will move us to a new line. */
 202                                if (++lines >= G.terminal_height || input == '\n')
 203                                        please_display_more_prompt = 1;
 204                                len = 0;
 205                        }
 206                        if (c != '\n' && wrap) {
 207                                /* Then outputting this will also put a character on
 208                                 * the beginning of that new line. Thus we first want to
 209                                 * display the prompt (if any), so we skip the putchar()
 210                                 * and go back to the top of the loop, without reading
 211                                 * a new character. */
 212                                goto loop_top;
 213                        }
 214                        /* My small mind cannot fathom backspaces and UTF-8 */
 215                        putchar(c);
 216                        die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
 217                }
 218                fclose(file);
 219                fflush_all();
 220        } while (*argv && *++argv);
 221 end:
 222        tcsetattr_tty_TCSANOW(&G.initial_settings);
 223        return 0;
 224}
 225