more.c 5.7 KB

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