more.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //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. /* -e Exit immediately after writing the last line */
  76. /* -f Count logical lines (IOW: long lines are not folded) */
  77. /* -l Do not pause after any line containing a ^L (form feed) */
  78. /* -s Squeeze blank lines into one */
  79. /* -u Suppress underlining */
  80. getopt32(argv, "deflsu");
  81. argv += optind;
  82. /* Another popular pager, most, detects when stdout
  83. * is not a tty and turns into cat. This makes sense. */
  84. if (!isatty(STDOUT_FILENO))
  85. return bb_cat(argv);
  86. tty = fopen_for_read(CURRENT_TTY);
  87. if (!tty)
  88. return bb_cat(argv);
  89. G.tty_fileno = fileno(tty);
  90. /* Turn on unbuffered input; turn off echoing */
  91. set_termios_to_raw(G.tty_fileno, &G.initial_settings, 0);
  92. bb_signals(BB_FATAL_SIGS, gotsig);
  93. do {
  94. struct stat st;
  95. FILE *file;
  96. int len;
  97. int lines;
  98. file = stdin;
  99. if (*argv) {
  100. file = fopen_or_warn(*argv, "r");
  101. if (!file)
  102. continue;
  103. }
  104. st.st_size = 0;
  105. fstat(fileno(file), &st);
  106. get_wh();
  107. please_display_more_prompt = 0;
  108. len = 0;
  109. lines = 0;
  110. for (;;) {
  111. int wrap;
  112. if (spaces)
  113. spaces--;
  114. else {
  115. c = getc(file);
  116. if (c == EOF) break;
  117. }
  118. loop_top:
  119. if (input != 'r' && please_display_more_prompt) {
  120. len = printf("--More-- ");
  121. if (st.st_size != 0) {
  122. uoff_t d = (uoff_t)st.st_size / 100;
  123. if (d == 0)
  124. d = 1;
  125. len += printf("(%u%% of %"OFF_FMT"u bytes)",
  126. (int) ((uoff_t)ftello(file) / d),
  127. st.st_size);
  128. }
  129. /*
  130. * We've just displayed the "--More--" prompt, so now we need
  131. * to get input from the user.
  132. */
  133. for (;;) {
  134. fflush_all();
  135. input = getc(tty);
  136. input = tolower(input);
  137. /* Erase the last message */
  138. printf("\r%*s\r", len, "");
  139. if (input == 'q')
  140. goto end;
  141. /* Due to various multibyte escape
  142. * sequences, it's not ok to accept
  143. * any input as a command to scroll
  144. * the screen. We only allow known
  145. * commands, else we show help msg. */
  146. if (input == ' ' || input == '\n' || input == 'r')
  147. break;
  148. len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
  149. }
  150. len = 0;
  151. lines = 0;
  152. please_display_more_prompt = 0;
  153. /* The user may have resized the terminal.
  154. * Re-read the dimensions. */
  155. get_wh();
  156. }
  157. /* Crudely convert tabs into spaces, which are
  158. * a bajillion times easier to deal with. */
  159. if (c == '\t') {
  160. spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
  161. c = ' ';
  162. }
  163. /*
  164. * There are two input streams to worry about here:
  165. *
  166. * c : the character we are reading from the file being "mored"
  167. * input: a character received from the keyboard
  168. *
  169. * If we hit a newline in the _file_ stream, we want to test and
  170. * see if any characters have been hit in the _input_ stream. This
  171. * allows the user to quit while in the middle of a file.
  172. */
  173. wrap = (++len > G.terminal_width);
  174. if (c == '\n' || wrap) {
  175. /* Then outputting this character
  176. * will move us to a new line. */
  177. if (++lines >= G.terminal_height || input == '\n')
  178. please_display_more_prompt = 1;
  179. len = 0;
  180. }
  181. if (c != '\n' && wrap) {
  182. /* Then outputting this will also put a character on
  183. * the beginning of that new line. Thus we first want to
  184. * display the prompt (if any), so we skip the putchar()
  185. * and go back to the top of the loop, without reading
  186. * a new character. */
  187. goto loop_top;
  188. }
  189. /* My small mind cannot fathom backspaces and UTF-8 */
  190. putchar(c);
  191. die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
  192. }
  193. fclose(file);
  194. fflush_all();
  195. } while (*argv && *++argv);
  196. end:
  197. tcsetattr_tty_TCSANOW(&G.initial_settings);
  198. return 0;
  199. }