more.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "busybox.h"
  17. #if ENABLE_FEATURE_USE_TERMIOS
  18. static int cin_fileno;
  19. #include <termios.h>
  20. #define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
  21. #define getTermSettings(fd, argp) tcgetattr(fd, argp);
  22. static struct termios initial_settings, new_settings;
  23. static void set_tty_to_initial_mode(void)
  24. {
  25. setTermSettings(cin_fileno, &initial_settings);
  26. }
  27. static void gotsig(int sig)
  28. {
  29. putchar('\n');
  30. exit(EXIT_FAILURE);
  31. }
  32. #endif /* FEATURE_USE_TERMIOS */
  33. int more_main(int argc, char **argv)
  34. {
  35. int c, lines, input = 0;
  36. int please_display_more_prompt = 0;
  37. struct stat st;
  38. FILE *file;
  39. FILE *cin;
  40. int len, page_height;
  41. int terminal_width;
  42. int terminal_height;
  43. argc--;
  44. argv++;
  45. cin = stdin;
  46. /* use input from terminal unless we do "more >outfile" */
  47. if (isatty(STDOUT_FILENO)) {
  48. cin = fopen(CURRENT_TTY, "r");
  49. if (!cin)
  50. cin = xfopen(CONSOLE_DEV, "r");
  51. please_display_more_prompt = 2;
  52. #if ENABLE_FEATURE_USE_TERMIOS
  53. cin_fileno = fileno(cin);
  54. getTermSettings(cin_fileno, &initial_settings);
  55. new_settings = initial_settings;
  56. new_settings.c_lflag &= ~ICANON;
  57. new_settings.c_lflag &= ~ECHO;
  58. new_settings.c_cc[VMIN] = 1;
  59. new_settings.c_cc[VTIME] = 0;
  60. setTermSettings(cin_fileno, &new_settings);
  61. atexit(set_tty_to_initial_mode);
  62. signal(SIGINT, gotsig);
  63. signal(SIGQUIT, gotsig);
  64. signal(SIGTERM, gotsig);
  65. #endif
  66. }
  67. do {
  68. file = stdin;
  69. if (argc != 0) {
  70. file = fopen_or_warn(*argv, "r");
  71. if (!file)
  72. goto loop;
  73. }
  74. st.st_size = 0;
  75. fstat(fileno(file), &st);
  76. please_display_more_prompt &= ~1;
  77. get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height);
  78. if (terminal_height > 4)
  79. terminal_height -= 2;
  80. if (terminal_width > 0)
  81. terminal_width -= 1;
  82. len = 0;
  83. lines = 0;
  84. page_height = terminal_height;
  85. while ((c = getc(file)) != EOF) {
  86. if ((please_display_more_prompt & 3) == 3) {
  87. len = printf("--More-- ");
  88. if (file != stdin && st.st_size > 0) {
  89. len += printf("(%d%% of %"OFF_FMT"d bytes)",
  90. (int) (ftello(file)*100 / st.st_size),
  91. st.st_size);
  92. }
  93. fflush(stdout);
  94. /*
  95. * We've just displayed the "--More--" prompt, so now we need
  96. * to get input from the user.
  97. */
  98. input = getc(cin);
  99. #if !ENABLE_FEATURE_USE_TERMIOS
  100. printf("\033[A"); /* up cursor */
  101. #endif
  102. /* Erase the "More" message */
  103. printf("\r%*s\r", len, "");
  104. len = 0;
  105. lines = 0;
  106. page_height = terminal_height;
  107. please_display_more_prompt &= ~1;
  108. if (input == 'q')
  109. goto end;
  110. }
  111. /*
  112. * There are two input streams to worry about here:
  113. *
  114. * c : the character we are reading from the file being "mored"
  115. * input : a character received from the keyboard
  116. *
  117. * If we hit a newline in the _file_ stream, we want to test and
  118. * see if any characters have been hit in the _input_ stream. This
  119. * allows the user to quit while in the middle of a file.
  120. */
  121. if (c == '\n') {
  122. /* increment by just one line if we are at
  123. * the end of this line */
  124. if (input == '\n')
  125. please_display_more_prompt |= 1;
  126. /* Adjust the terminal height for any overlap, so that
  127. * no lines get lost off the top. */
  128. if (len >= terminal_width) {
  129. int quot, rem;
  130. quot = len / terminal_width;
  131. rem = len - (quot * terminal_width);
  132. if (quot) {
  133. if (rem)
  134. page_height -= quot;
  135. else
  136. page_height -= (quot - 1);
  137. }
  138. }
  139. if (++lines >= page_height) {
  140. please_display_more_prompt |= 1;
  141. }
  142. len = 0;
  143. }
  144. /*
  145. * If we just read a newline from the file being 'mored' and any
  146. * key other than a return is hit, scroll by one page
  147. */
  148. putc(c, stdout);
  149. len++;
  150. }
  151. fclose(file);
  152. fflush(stdout);
  153. loop:
  154. argv++;
  155. } while (--argc > 0);
  156. end:
  157. return 0;
  158. }