more.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 | ECHO);
  78. new_settings.c_cc[VMIN] = 1;
  79. new_settings.c_cc[VTIME] = 0;
  80. setTermSettings(cin_fileno, &new_settings);
  81. bb_signals(0
  82. + (1 << SIGINT)
  83. + (1 << SIGQUIT)
  84. + (1 << SIGTERM)
  85. , gotsig);
  86. }
  87. do {
  88. file = stdin;
  89. if (*argv) {
  90. file = fopen_or_warn(*argv, "r");
  91. if (!file)
  92. continue;
  93. }
  94. st.st_size = 0;
  95. fstat(fileno(file), &st);
  96. please_display_more_prompt = 0;
  97. /* never returns w, h <= 1 */
  98. get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
  99. terminal_height -= 1;
  100. len = 0;
  101. lines = 0;
  102. while (spaces || (c = getc(file)) != EOF) {
  103. int wrap;
  104. if (spaces)
  105. spaces--;
  106. loop_top:
  107. if (input != 'r' && please_display_more_prompt) {
  108. len = printf("--More-- ");
  109. if (st.st_size != 0) {
  110. uoff_t d = (uoff_t)st.st_size / 100;
  111. if (d == 0)
  112. d = 1;
  113. len += printf("(%u%% of %"OFF_FMT"u bytes)",
  114. (int) ((uoff_t)ftello(file) / d),
  115. st.st_size);
  116. }
  117. fflush_all();
  118. /*
  119. * We've just displayed the "--More--" prompt, so now we need
  120. * to get input from the user.
  121. */
  122. for (;;) {
  123. input = getc(cin);
  124. input = tolower(input);
  125. if (!ENABLE_FEATURE_USE_TERMIOS)
  126. printf("\033[A"); /* cursor up */
  127. /* Erase the last message */
  128. printf("\r%*s\r", len, "");
  129. /* Due to various multibyte escape
  130. * sequences, it's not ok to accept
  131. * any input as a command to scroll
  132. * the screen. We only allow known
  133. * commands, else we show help msg. */
  134. if (input == ' ' || input == '\n' || input == 'q' || input == 'r')
  135. break;
  136. len = printf("(Enter:next line Space:next page Q:quit R:show the rest)");
  137. }
  138. len = 0;
  139. lines = 0;
  140. please_display_more_prompt = 0;
  141. if (input == 'q')
  142. goto end;
  143. /* The user may have resized the terminal.
  144. * Re-read the dimensions. */
  145. if (ENABLE_FEATURE_USE_TERMIOS) {
  146. get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
  147. terminal_height -= 1;
  148. }
  149. }
  150. /* Crudely convert tabs into spaces, which are
  151. * a bajillion times easier to deal with. */
  152. if (c == '\t') {
  153. spaces = ((unsigned)~len) % CONVERTED_TAB_SIZE;
  154. c = ' ';
  155. }
  156. /*
  157. * There are two input streams to worry about here:
  158. *
  159. * c : the character we are reading from the file being "mored"
  160. * input: a character received from the keyboard
  161. *
  162. * If we hit a newline in the _file_ stream, we want to test and
  163. * see if any characters have been hit in the _input_ stream. This
  164. * allows the user to quit while in the middle of a file.
  165. */
  166. wrap = (++len > terminal_width);
  167. if (c == '\n' || wrap) {
  168. /* Then outputting this character
  169. * will move us to a new line. */
  170. if (++lines >= terminal_height || input == '\n')
  171. please_display_more_prompt = 1;
  172. len = 0;
  173. }
  174. if (c != '\n' && wrap) {
  175. /* Then outputting this will also put a character on
  176. * the beginning of that new line. Thus we first want to
  177. * display the prompt (if any), so we skip the putchar()
  178. * and go back to the top of the loop, without reading
  179. * a new character. */
  180. goto loop_top;
  181. }
  182. /* My small mind cannot fathom backspaces and UTF-8 */
  183. putchar(c);
  184. die_if_ferror_stdout(); /* if tty was destroyed (closed xterm, etc) */
  185. }
  186. fclose(file);
  187. fflush_all();
  188. } while (*argv && *++argv);
  189. end:
  190. setTermSettings(cin_fileno, &initial_settings);
  191. return 0;
  192. }