aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2015-07-24 14:28:50 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-07-31 16:22:08 +0200
commitad1b4d5882586ad213992a97fc22f9c4bc9e0040 (patch)
tree10a3ee4cbfc543a506f52e1f316898a1428df005
parent70b84be9e85969491e542cecc3ae28fa7558a7ec (diff)
downloadbusybox-w32-ad1b4d5882586ad213992a97fc22f9c4bc9e0040.tar.gz
busybox-w32-ad1b4d5882586ad213992a97fc22f9c4bc9e0040.tar.bz2
busybox-w32-ad1b4d5882586ad213992a97fc22f9c4bc9e0040.zip
less: add a function to detect when display is at end of file
Add a function to package the test that detects whether enough has been read from the file to allow a screenful to be displayed. Also use this to determine when to display '(END)' in the status line. The previous code was incomplete and didn't handle truncated lines (-S flag) properly. function old new delta at_end - 63 +63 status_print 111 109 -2 read_lines 819 764 -55 getch_nowait 319 264 -55 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/3 up/down: 63/-112) Total: -49 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/less.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index 91a933a3a..2a1797c7b 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -406,6 +406,14 @@ static void fill_match_lines(unsigned pos);
406#define fill_match_lines(pos) ((void)0) 406#define fill_match_lines(pos) ((void)0)
407#endif 407#endif
408 408
409static int at_end(void)
410{
411 return (option_mask32 & FLAG_S)
412 ? !(cur_fline <= max_fline &&
413 max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
414 : !(max_fline > cur_fline + max_displayed_line);
415}
416
409/* Devilishly complex routine. 417/* Devilishly complex routine.
410 * 418 *
411 * Has to deal with EOF and EPIPE on input, 419 * Has to deal with EOF and EPIPE on input,
@@ -552,11 +560,7 @@ static void read_lines(void)
552 eof_error = 0; /* Pretend we saw EOF */ 560 eof_error = 0; /* Pretend we saw EOF */
553 break; 561 break;
554 } 562 }
555 if (!(option_mask32 & FLAG_S) 563 if (!at_end()) {
556 ? (max_fline > cur_fline + max_displayed_line)
557 : (max_fline >= cur_fline
558 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
559 ) {
560#if !ENABLE_FEATURE_LESS_REGEXP 564#if !ENABLE_FEATURE_LESS_REGEXP
561 break; 565 break;
562#else 566#else
@@ -662,7 +666,7 @@ static void m_status_print(void)
662 if (num_lines >= 0) 666 if (num_lines >= 0)
663 printf("/%i", num_lines); 667 printf("/%i", num_lines);
664 668
665 if (cur_fline >= (int)(max_fline - max_displayed_line)) { 669 if (at_end()) {
666 printf(" (END)"); 670 printf(" (END)");
667 if (num_files > 1 && current_file != num_files) 671 if (num_files > 1 && current_file != num_files)
668 printf(" - next: %s", files[current_file]); 672 printf(" - next: %s", files[current_file]);
@@ -692,7 +696,7 @@ static void status_print(void)
692#endif 696#endif
693 697
694 clear_line(); 698 clear_line();
695 if (cur_fline && cur_fline < (int)(max_fline - max_displayed_line)) { 699 if (cur_fline && !at_end()) {
696 bb_putchar(':'); 700 bb_putchar(':');
697 return; 701 return;
698 } 702 }
@@ -1009,12 +1013,7 @@ static int64_t getch_nowait(void)
1009 */ 1013 */
1010 rd = 1; 1014 rd = 1;
1011 /* Are we interested in stdin? */ 1015 /* Are we interested in stdin? */
1012//TODO: reuse code for determining this 1016 if (at_end()) {
1013 if (!(option_mask32 & FLAG_S)
1014 ? !(max_fline > cur_fline + max_displayed_line)
1015 : !(max_fline >= cur_fline
1016 && max_lineno > LINENO(flines[cur_fline]) + max_displayed_line)
1017 ) {
1018 if (eof_error > 0) /* did NOT reach eof yet */ 1017 if (eof_error > 0) /* did NOT reach eof yet */
1019 rd = 0; /* yes, we are interested in stdin */ 1018 rd = 0; /* yes, we are interested in stdin */
1020 } 1019 }