ps: conditionally enable -T on non-DESKTOP build too
[oweals/busybox.git] / miscutils / less.c
index b5d225a122d4ce2b77d944781a85b6a4ff14af03..ce77ddd3a1e1ceecc3d22fef5d2fc4aef58bed6b 100644 (file)
@@ -97,6 +97,7 @@ struct globals {
 #endif
        smallint terminated;
        struct termios term_orig, term_less;
+       char kbd_input[KEYCODE_BUFFER_SIZE];
 };
 #define G (*ptr_to_globals)
 #define cur_fline           (G.cur_fline         )
@@ -133,6 +134,7 @@ struct globals {
 #define terminated          (G.terminated        )
 #define term_orig           (G.term_orig         )
 #define term_less           (G.term_less         )
+#define kbd_input           (G.kbd_input         )
 #define INIT_G() do { \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
        less_gets_pos = -1; \
@@ -141,7 +143,7 @@ struct globals {
        current_file = 1; \
        eof_error = 1; \
        terminated = 1; \
-       USE_FEATURE_LESS_REGEXP(wanted_match = -1;) \
+       IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \
 } while (0)
 
 /* flines[] are lines read from stdin, each in malloc'ed buffer.
@@ -322,7 +324,7 @@ static void read_lines(void)
        if (option_mask32 & FLAG_N)
                w -= 8;
 
USE_FEATURE_LESS_REGEXP(again0:)
IF_FEATURE_LESS_REGEXP(again0:)
 
        p = current_line = ((char*)xmalloc(w + 4)) + 4;
        max_fline += last_terminated;
@@ -622,7 +624,7 @@ static void print_found(const char *line)
 
        while (match_status == 0) {
                char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL,
-                               growline ? : "",
+                               growline ? growline : "",
                                match_structs.rm_so, str,
                                match_structs.rm_eo - match_structs.rm_so,
                                                str + match_structs.rm_so);
@@ -770,9 +772,7 @@ static void buffer_line(int linenum)
 static void open_file_and_read_lines(void)
 {
        if (filename) {
-               int fd = xopen(filename, O_RDONLY);
-               dup2(fd, 0);
-               if (fd) close(fd);
+               xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO);
        } else {
                /* "less" with no arguments in argv[] */
                /* For status line only */
@@ -804,9 +804,8 @@ static void reinitialize(void)
        buffer_fill_and_print();
 }
 
-static ssize_t getch_nowait(void)
+static int getch_nowait(void)
 {
-       char input[KEYCODE_BUFFER_SIZE];
        int rd;
        struct pollfd pfd[2];
 
@@ -837,36 +836,43 @@ static ssize_t getch_nowait(void)
        if (less_gets_pos >= 0)
                move_cursor(max_displayed_line + 2, less_gets_pos + 1);
        fflush(stdout);
+
+       if (kbd_input[0] == 0) { /* if nothing is buffered */
 #if ENABLE_FEATURE_LESS_WINCH
-       while (1) {
-               int r;
-               /* NB: SIGWINCH interrupts poll() */
-               r = poll(pfd + rd, 2 - rd, -1);
-               if (/*r < 0 && errno == EINTR &&*/ winch_counter)
-                       return '\\'; /* anything which has no defined function */
-               if (r) break;
-       }
+               while (1) {
+                       int r;
+                       /* NB: SIGWINCH interrupts poll() */
+                       r = poll(pfd + rd, 2 - rd, -1);
+                       if (/*r < 0 && errno == EINTR &&*/ winch_counter)
+                               return '\\'; /* anything which has no defined function */
+                       if (r) break;
+               }
 #else
-       safe_poll(pfd + rd, 2 - rd, -1);
+               safe_poll(pfd + rd, 2 - rd, -1);
 #endif
+       }
 
        /* We have kbd_fd in O_NONBLOCK mode, read inside read_key()
         * would not block even if there is no input available */
-       rd = read_key(kbd_fd, NULL, input);
-       if (rd == -1 && errno == EAGAIN) {
-               /* No keyboard input available. Since poll() did return,
-                * we should have input on stdin */
-               read_lines();
-               buffer_fill_and_print();
-               goto again;
+       rd = read_key(kbd_fd, kbd_input);
+       if (rd == -1) {
+               if (errno == EAGAIN) {
+                       /* No keyboard input available. Since poll() did return,
+                        * we should have input on stdin */
+                       read_lines();
+                       buffer_fill_and_print();
+                       goto again;
+               }
+               /* EOF/error (ssh session got killed etc) */
+               less_exit(0);
        }
        set_tty_cooked();
        return rd;
 }
 
-/* Grab a character from input without requiring the return key. If the
- * character is ASCII \033, get more characters and assign certain sequences
- * special return codes. Note that this function works best with raw input. */
+/* Grab a character from input without requiring the return key.
+ * May return KEYCODE_xxx values.
+ * Note that this function works best with raw input. */
 static int less_getch(int pos)
 {
        int i;
@@ -1501,7 +1507,7 @@ int less_main(int argc, char **argv)
        /* TODO: -x: do not interpret backspace, -xx: tab also */
        /* -xxx: newline also */
        /* -w N: assume width N (-xxx -w 32: hex viewer of sorts) */
-       getopt32(argv, "EMmN~I" USE_FEATURE_LESS_DASHCMD("S"));
+       getopt32(argv, "EMmN~I" IF_FEATURE_LESS_DASHCMD("S"));
        argc -= optind;
        argv += optind;
        num_files = argc;