From 36941503bd8e28a1fac13edffc3ffc84346b32c5 Mon Sep 17 00:00:00 2001 From: Aaro Koskinen Date: Sun, 15 Apr 2018 01:24:24 +0300 Subject: less: implement -F Implement -F option: Exit if entire file fits on first screen. function old new delta buffer_print 622 633 +11 less_main 2446 2449 +3 buffer_fill_and_print 169 172 +3 packed_usage 32258 32236 -22 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/1 up/down: 17/-22) Total: -5 bytes Signed-off-by: Aaro Koskinen Signed-off-by: Denys Vlasenko --- miscutils/less.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'miscutils') diff --git a/miscutils/less.c b/miscutils/less.c index 51ef2a59d..3bce93247 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -121,11 +121,12 @@ //kbuild:lib-$(CONFIG_LESS) += less.o //usage:#define less_trivial_usage -//usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") +//usage: "[-EF" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") IF_FEATURE_LESS_RAW("R") "h~] [FILE]..." //usage:#define less_full_usage "\n\n" //usage: "View FILE (or stdin) one screenful at a time\n" //usage: "\n -E Quit once the end of a file is reached" +//usage: "\n -F Quit if entire file fits on first screen" //usage: IF_FEATURE_LESS_REGEXP( //usage: "\n -I Ignore case in all searches" //usage: ) @@ -175,8 +176,9 @@ enum { FLAG_N = 1 << 3, FLAG_TILDE = 1 << 4, FLAG_I = 1 << 5, - FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, - FLAG_R = (1 << 7) * ENABLE_FEATURE_LESS_RAW, + FLAG_F = 1 << 6, + FLAG_S = (1 << 7) * ENABLE_FEATURE_LESS_TRUNCATE, + FLAG_R = (1 << 8) * ENABLE_FEATURE_LESS_RAW, /* hijack command line options variable for internal state vars */ LESS_STATE_MATCH_BACKWARDS = 1 << 15, }; @@ -906,11 +908,12 @@ static void buffer_print(void) else print_ascii(buffer[i]); } - if ((option_mask32 & FLAG_E) + if ((option_mask32 & (FLAG_E|FLAG_F)) && eof_error <= 0 - && (max_fline - cur_fline) <= max_displayed_line ) { - less_exit(EXIT_SUCCESS); + i = option_mask32 & FLAG_F ? 0 : cur_fline; + if (max_fline - i <= max_displayed_line) + less_exit(EXIT_SUCCESS); } status_print(); } @@ -1814,7 +1817,7 @@ int less_main(int argc, char **argv) * -s: condense many empty lines to one * (used by some setups for manpage display) */ - getopt32(argv, "EMmN~I" + getopt32(argv, "EMmN~IF" IF_FEATURE_LESS_TRUNCATE("S") IF_FEATURE_LESS_RAW("R") /*ignored:*/"s" @@ -1828,6 +1831,9 @@ int less_main(int argc, char **argv) if (ENABLE_FEATURE_LESS_ENV) { char *c = getenv("LESS"); if (c) while (*c) switch (*c++) { + case 'F': + option_mask32 |= FLAG_F; + break; case 'M': option_mask32 |= FLAG_M; break; -- cgit v1.2.3-55-g6feb From 50aea2786b275c1f1d5f1b2df85b519dbaef6188 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 15 Apr 2018 13:14:51 +0200 Subject: less: remove unnecessary message Signed-off-by: Denys Vlasenko --- miscutils/less.c | 1 - 1 file changed, 1 deletion(-) (limited to 'miscutils') diff --git a/miscutils/less.c b/miscutils/less.c index 3bce93247..6029b6809 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -1856,7 +1856,6 @@ int less_main(int argc, char **argv) if (!num_files) { if (isatty(STDIN_FILENO)) { /* Just "less"? No args and no redirection? */ - bb_error_msg("missing filename"); bb_show_usage(); } } else { -- cgit v1.2.3-55-g6feb From 058a153b6970660f3c284ac259986ae87507df9a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 16 Apr 2018 10:24:48 +0200 Subject: less: fix fallout from "use common routine to set raw termios" Testcase: (sleep 10; ls) | busybox less [...] ~ LICENSE ~ Makefile ~ Makefile.custom ~ Makefile.flags [...] less did not want this part: + /* dont convert NL to CR+NL on output */ + newterm->c_oflag &= ~(ONLCR); function old new delta get_termios_and_make_raw 108 115 +7 Signed-off-by: Denys Vlasenko --- include/libbb.h | 8 +++++--- libbb/xfuncs.c | 13 +++++++++---- miscutils/less.c | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) (limited to 'miscutils') diff --git a/include/libbb.h b/include/libbb.h index c7bf33ef8..646c58bf2 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1597,9 +1597,11 @@ int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FU int get_terminal_width(int fd) FAST_FUNC; int tcsetattr_stdin_TCSANOW(const struct termios *tp) FAST_FUNC; -#define TERMIOS_CLEAR_ISIG (1 << 0) -#define TERMIOS_RAW_CRNL (1 << 1) -#define TERMIOS_RAW_INPUT (1 << 2) +#define TERMIOS_CLEAR_ISIG (1 << 0) +#define TERMIOS_RAW_CRNL_INPUT (1 << 1) +#define TERMIOS_RAW_CRNL_OUTPUT (1 << 2) +#define TERMIOS_RAW_CRNL (TERMIOS_RAW_CRNL_INPUT|TERMIOS_RAW_CRNL_OUTPUT) +#define TERMIOS_RAW_INPUT (1 << 3) int get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags) FAST_FUNC; int set_termios_to_raw(int fd, struct termios *oldterm, int flags) FAST_FUNC; diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index e8c027f17..b4d512bd6 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -330,7 +330,6 @@ int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct t newterm->c_cc[VMIN] = 1; /* no timeout (reads block forever) */ newterm->c_cc[VTIME] = 0; - if (flags & TERMIOS_RAW_CRNL) { /* IXON, IXOFF, and IXANY: * IXOFF=1: sw flow control is enabled on input queue: * tty transmits a STOP char when input queue is close to full @@ -340,9 +339,12 @@ int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct t * and resume sending if START is received, or if any char * is received and IXANY=1. */ + if (flags & TERMIOS_RAW_CRNL_INPUT) { /* IXON=0: XON/XOFF chars are treated as normal chars (why we do this?) */ /* dont convert CR to NL on input */ newterm->c_iflag &= ~(IXON | ICRNL); + } + if (flags & TERMIOS_RAW_CRNL_OUTPUT) { /* dont convert NL to CR+NL on output */ newterm->c_oflag &= ~(ONLCR); /* Maybe clear more c_oflag bits? Usually, only OPOST and ONLCR are set. @@ -363,9 +365,12 @@ int FAST_FUNC get_termios_and_make_raw(int fd, struct termios *newterm, struct t #ifndef IXANY # define IXANY 0 #endif - /* IXOFF=0: disable sending XON/XOFF if input buf is full */ - /* IXON=0: input XON/XOFF chars are not special */ - /* dont convert anything on input */ + /* IXOFF=0: disable sending XON/XOFF if input buf is full + * IXON=0: input XON/XOFF chars are not special + * BRKINT=0: dont send SIGINT on break + * IMAXBEL=0: dont echo BEL on input line too long + * INLCR,ICRNL,IUCLC: dont convert anything on input + */ newterm->c_iflag &= ~(IXOFF|IXON|IXANY|BRKINT|INLCR|ICRNL|IUCLC|IMAXBEL); } return r; diff --git a/miscutils/less.c b/miscutils/less.c index 6029b6809..938d9842f 100644 --- a/miscutils/less.c +++ b/miscutils/less.c @@ -1891,7 +1891,7 @@ int less_main(int argc, char **argv) G.kbd_fd_orig_flags = ndelay_on(tty_fd); kbd_fd = tty_fd; /* save in a global */ - get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL); + get_termios_and_make_raw(tty_fd, &term_less, &term_orig, TERMIOS_RAW_CRNL_INPUT); IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(tty_fd, &width, &max_displayed_line); /* 20: two tabstops + 4 */ -- cgit v1.2.3-55-g6feb