more.c 6.6 KB

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