123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- struct globals {
- int tty_fileno;
- unsigned terminal_width;
- unsigned terminal_height;
- struct termios initial_settings;
- } FIX_ALIASING;
- #define G (*(struct globals*)bb_common_bufsiz1)
- #define INIT_G() do { setup_common_bufsiz(); } while (0)
- static void get_wh(void)
- {
-
- get_terminal_width_height(G.tty_fileno, &G.terminal_width, &G.terminal_height);
- G.terminal_height -= 1;
- }
- static void tcsetattr_tty_TCSANOW(struct termios *settings)
- {
- tcsetattr(G.tty_fileno, TCSANOW, settings);
- }
- static void gotsig(int sig UNUSED_PARAM)
- {
-
- bb_putchar_stderr('\n');
- tcsetattr_tty_TCSANOW(&G.initial_settings);
- _exit(EXIT_FAILURE);
- }
- #define CONVERTED_TAB_SIZE 8
- int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int more_main(int argc UNUSED_PARAM, char **argv)
- {
- int c = c;
- int input = 0;
- int spaces = 0;
- int please_display_more_prompt;
- FILE *tty;
- INIT_G();
-
-
-
-
-
-
-
- getopt32(argv, "dflsu");
- argv += optind;
-
- if (!isatty(STDOUT_FILENO))
- return bb_cat(argv);
- tty = fopen_for_read(CURRENT_TTY);
- if (!tty)
- return bb_cat(argv);
- G.tty_fileno = fileno(tty);
-
- set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0);
- bb_signals(BB_FATAL_SIGS, gotsig);
- do {
- struct stat st;
- FILE *file;
- int len;
- int lines;
- file = stdin;
- if (*argv) {
- file = fopen_or_warn(*argv, "r");
- if (!file)
- continue;
- }
- st.st_size = 0;
- fstat(fileno(file), &st);
- get_wh();
- please_display_more_prompt = 0;
- len = 0;
- lines = 0;
- for (;;) {
- int wrap;
- if (spaces)
- spaces--;
- else {
- c = getc(file);
- if (c == EOF) break;
- }
- loop_top:
- if (input != 'r' && please_display_more_prompt) {
- len = printf("--More-- ");
- if (st.st_size != 0) {
- uoff_t d = (uoff_t)st.st_size / 100;
- if (d == 0)
- d = 1;
- len += printf("(%u%% of %"OFF_FMT"u bytes)",
- (int) ((uoff_t)ftello(file) / d),
- st.st_size);
- }
-
- for (;;) {
- fflush_all();
- input = getc(tty);
- input = tolower(input);
-
- printf("\r%*s\r", len, "");
- if (input == 'q')
- goto end;
-
- if (input == ' ' || input == '\n' || input == 'r')
- break;
- len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
- }
- len = 0;
- lines = 0;
- please_display_more_prompt = 0;
-
- get_wh();
- }
-
- if (c == '\t') {
- spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
- c = ' ';
- }
-
- wrap = (++len > G.terminal_width);
- if (c == '\n' || wrap) {
-
- if (++lines >= G.terminal_height || input == '\n')
- please_display_more_prompt = 1;
- len = 0;
- }
- if (c != '\n' && wrap) {
-
- goto loop_top;
- }
-
- putchar(c);
- die_if_ferror_stdout();
- }
- fclose(file);
- fflush_all();
- } while (*argv && *++argv);
- end:
- tcsetattr_tty_TCSANOW(&G.initial_settings);
- return 0;
- }
|