diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-10 16:32:14 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-10 16:32:14 +0000 |
commit | 4eb8b936cb0aeb27c3e12f9a93fc43aa1e9668f5 (patch) | |
tree | 8cb052205ab9f9e97233afe39ddb2c6253ab8d1a /util-linux | |
parent | 2b52b2797bae774656b3f79ed1939a08fbed7b0e (diff) | |
download | busybox-w32-4eb8b936cb0aeb27c3e12f9a93fc43aa1e9668f5.tar.gz busybox-w32-4eb8b936cb0aeb27c3e12f9a93fc43aa1e9668f5.tar.bz2 busybox-w32-4eb8b936cb0aeb27c3e12f9a93fc43aa1e9668f5.zip |
more: do not mess with "/dev/console" (!);
fill whole 1st screen (was "screen sans one line");
fall back to cat if redirected or no ctty
less: fall back to cat if no ctty (was exiting)
resize: cosmetics
Diffstat (limited to 'util-linux')
-rw-r--r-- | util-linux/more.c | 77 |
1 files changed, 35 insertions, 42 deletions
diff --git a/util-linux/more.c b/util-linux/more.c index 23ac526fa..2a38ef326 100644 --- a/util-linux/more.c +++ b/util-linux/more.c | |||
@@ -50,52 +50,46 @@ int more_main(int argc, char **argv) | |||
50 | int terminal_width; | 50 | int terminal_width; |
51 | int terminal_height; | 51 | int terminal_height; |
52 | 52 | ||
53 | argc--; | ||
54 | argv++; | 53 | argv++; |
54 | /* Another popular pager, most, detects when stdout | ||
55 | * is not a tty and turns into cat. This makes sense. */ | ||
56 | if (!isatty(STDOUT_FILENO)) | ||
57 | return bb_cat(argv); | ||
58 | cin = fopen(CURRENT_TTY, "r"); | ||
59 | if (!cin) | ||
60 | return bb_cat(argv); | ||
55 | 61 | ||
56 | cin = stdin; | ||
57 | /* use input from terminal unless we do "more >outfile" */ | ||
58 | if (isatty(STDOUT_FILENO)) { | ||
59 | cin = fopen(CURRENT_TTY, "r"); | ||
60 | /* Huh? why not just fail if "/dev/tty" isn't available? | ||
61 | * If user has no ctty, it's his own problem */ | ||
62 | if (!cin) | ||
63 | cin = xfopen(DEV_CONSOLE, "r"); | ||
64 | please_display_more_prompt = 2; | ||
65 | #if ENABLE_FEATURE_USE_TERMIOS | 62 | #if ENABLE_FEATURE_USE_TERMIOS |
66 | cin_fileno = fileno(cin); | 63 | cin_fileno = fileno(cin); |
67 | getTermSettings(cin_fileno, &initial_settings); | 64 | getTermSettings(cin_fileno, &initial_settings); |
68 | new_settings = initial_settings; | 65 | new_settings = initial_settings; |
69 | new_settings.c_lflag &= ~ICANON; | 66 | new_settings.c_lflag &= ~ICANON; |
70 | new_settings.c_lflag &= ~ECHO; | 67 | new_settings.c_lflag &= ~ECHO; |
71 | new_settings.c_cc[VMIN] = 1; | 68 | new_settings.c_cc[VMIN] = 1; |
72 | new_settings.c_cc[VTIME] = 0; | 69 | new_settings.c_cc[VTIME] = 0; |
73 | setTermSettings(cin_fileno, &new_settings); | 70 | setTermSettings(cin_fileno, &new_settings); |
74 | atexit(set_tty_to_initial_mode); | 71 | atexit(set_tty_to_initial_mode); |
75 | signal(SIGINT, gotsig); | 72 | signal(SIGINT, gotsig); |
76 | signal(SIGQUIT, gotsig); | 73 | signal(SIGQUIT, gotsig); |
77 | signal(SIGTERM, gotsig); | 74 | signal(SIGTERM, gotsig); |
78 | #endif | 75 | #endif |
79 | } | 76 | please_display_more_prompt = 2; |
80 | 77 | ||
81 | do { | 78 | do { |
82 | file = stdin; | 79 | file = stdin; |
83 | if (argc != 0) { | 80 | if (*argv) { |
84 | file = fopen_or_warn(*argv, "r"); | 81 | file = fopen_or_warn(*argv, "r"); |
85 | if (!file) | 82 | if (!file) |
86 | goto loop; | 83 | continue; |
87 | } | 84 | } |
88 | |||
89 | st.st_size = 0; | 85 | st.st_size = 0; |
90 | fstat(fileno(file), &st); | 86 | fstat(fileno(file), &st); |
91 | 87 | ||
92 | please_display_more_prompt &= ~1; | 88 | please_display_more_prompt &= ~1; |
93 | 89 | /* never returns w, h <= 1 */ | |
94 | get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height); | 90 | get_terminal_width_height(fileno(cin), &terminal_width, &terminal_height); |
95 | if (terminal_height > 4) | 91 | terminal_width -= 1; |
96 | terminal_height -= 2; | 92 | terminal_height -= 1; |
97 | if (terminal_width > 0) | ||
98 | terminal_width -= 1; | ||
99 | 93 | ||
100 | len = 0; | 94 | len = 0; |
101 | lines = 0; | 95 | lines = 0; |
@@ -104,7 +98,7 @@ int more_main(int argc, char **argv) | |||
104 | 98 | ||
105 | if ((please_display_more_prompt & 3) == 3) { | 99 | if ((please_display_more_prompt & 3) == 3) { |
106 | len = printf("--More-- "); | 100 | len = printf("--More-- "); |
107 | if (file != stdin && st.st_size > 0) { | 101 | if (/*file != stdin &&*/ st.st_size > 0) { |
108 | len += printf("(%d%% of %"OFF_FMT"d bytes)", | 102 | len += printf("(%d%% of %"OFF_FMT"d bytes)", |
109 | (int) (ftello(file)*100 / st.st_size), | 103 | (int) (ftello(file)*100 / st.st_size), |
110 | st.st_size); | 104 | st.st_size); |
@@ -123,7 +117,9 @@ int more_main(int argc, char **argv) | |||
123 | printf("\r%*s\r", len, ""); | 117 | printf("\r%*s\r", len, ""); |
124 | len = 0; | 118 | len = 0; |
125 | lines = 0; | 119 | lines = 0; |
126 | page_height = terminal_height; | 120 | /* Bottom line on page will become top line |
121 | * after one page forward. Thus -1: */ | ||
122 | page_height = terminal_height - 1; | ||
127 | please_display_more_prompt &= ~1; | 123 | please_display_more_prompt &= ~1; |
128 | 124 | ||
129 | if (input == 'q') | 125 | if (input == 'q') |
@@ -151,12 +147,9 @@ int more_main(int argc, char **argv) | |||
151 | int quot, rem; | 147 | int quot, rem; |
152 | quot = len / terminal_width; | 148 | quot = len / terminal_width; |
153 | rem = len - (quot * terminal_width); | 149 | rem = len - (quot * terminal_width); |
154 | if (quot) { | 150 | page_height -= (quot - 1); |
155 | if (rem) | 151 | if (rem) |
156 | page_height -= quot; | 152 | page_height--; |
157 | else | ||
158 | page_height -= (quot - 1); | ||
159 | } | ||
160 | } | 153 | } |
161 | if (++lines >= page_height) { | 154 | if (++lines >= page_height) { |
162 | please_display_more_prompt |= 1; | 155 | please_display_more_prompt |= 1; |
@@ -168,13 +161,13 @@ int more_main(int argc, char **argv) | |||
168 | * key other than a return is hit, scroll by one page | 161 | * key other than a return is hit, scroll by one page |
169 | */ | 162 | */ |
170 | putc(c, stdout); | 163 | putc(c, stdout); |
164 | /* My small mind cannot fathom tabs, backspaces, | ||
165 | * and UTF-8 */ | ||
171 | len++; | 166 | len++; |
172 | } | 167 | } |
173 | fclose(file); | 168 | fclose(file); |
174 | fflush(stdout); | 169 | fflush(stdout); |
175 | loop: | 170 | } while (*argv && *++argv); |
176 | argv++; | ||
177 | } while (--argc > 0); | ||
178 | end: | 171 | end: |
179 | return 0; | 172 | return 0; |
180 | } | 173 | } |