more.c 5.4 KB

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