more.c 6.0 KB

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