Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini less implementation for busybox |
| 4 | * |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005 by Rob Sullivan <cogito.ergo.cogito@gmail.com> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 11 | * TODO: |
| 12 | * - Add more regular expression support - search modifiers, certain matches, etc. |
| 13 | * - Add more complex bracket searching - currently, nested brackets are |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 14 | * not considered. |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 15 | * - Add support for "F" as an input. This causes less to act in |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 16 | * a similar way to tail -f. |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 17 | * - Allow horizontal scrolling. |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 18 | * |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 19 | * Notes: |
| 20 | * - the inp file pointer is used so that keyboard input works after |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 21 | * redirected input has been read from stdin |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 22 | */ |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 23 | |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 24 | //config:config LESS |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 25 | //config: bool "less (15 kb)" |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 26 | //config: default y |
| 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: 'less' is a pager, meaning that it displays text files. It possesses |
| 29 | //config: a wide array of features, and is an improvement over 'more'. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 30 | //config: |
| 31 | //config:config FEATURE_LESS_MAXLINES |
| 32 | //config: int "Max number of input lines less will try to eat" |
| 33 | //config: default 9999999 |
| 34 | //config: depends on LESS |
| 35 | //config: |
| 36 | //config:config FEATURE_LESS_BRACKETS |
| 37 | //config: bool "Enable bracket searching" |
| 38 | //config: default y |
| 39 | //config: depends on LESS |
| 40 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 41 | //config: This option adds the capability to search for matching left and right |
| 42 | //config: brackets, facilitating programming. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 43 | //config: |
| 44 | //config:config FEATURE_LESS_FLAGS |
Denys Vlasenko | 5377286 | 2012-04-17 16:11:25 +0200 | [diff] [blame] | 45 | //config: bool "Enable -m/-M" |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 46 | //config: default y |
| 47 | //config: depends on LESS |
| 48 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 49 | //config: The -M/-m flag enables a more sophisticated status line. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 50 | //config: |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 51 | //config:config FEATURE_LESS_TRUNCATE |
| 52 | //config: bool "Enable -S" |
| 53 | //config: default y |
| 54 | //config: depends on LESS |
| 55 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 56 | //config: The -S flag causes long lines to be truncated rather than |
| 57 | //config: wrapped. |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 58 | //config: |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 59 | //config:config FEATURE_LESS_MARKS |
| 60 | //config: bool "Enable marks" |
| 61 | //config: default y |
| 62 | //config: depends on LESS |
| 63 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 64 | //config: Marks enable positions in a file to be stored for easy reference. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 65 | //config: |
| 66 | //config:config FEATURE_LESS_REGEXP |
| 67 | //config: bool "Enable regular expressions" |
| 68 | //config: default y |
| 69 | //config: depends on LESS |
| 70 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 71 | //config: Enable regular expressions, allowing complex file searches. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 72 | //config: |
| 73 | //config:config FEATURE_LESS_WINCH |
| 74 | //config: bool "Enable automatic resizing on window size changes" |
| 75 | //config: default y |
| 76 | //config: depends on LESS |
| 77 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 78 | //config: Makes less track window size changes. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 79 | //config: |
| 80 | //config:config FEATURE_LESS_ASK_TERMINAL |
| 81 | //config: bool "Use 'tell me cursor position' ESC sequence to measure window" |
| 82 | //config: default y |
| 83 | //config: depends on FEATURE_LESS_WINCH |
| 84 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 85 | //config: Makes less track window size changes. |
| 86 | //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set, |
| 87 | //config: this option makes less perform a last-ditch effort to find it: |
| 88 | //config: position cursor to 999,999 and ask terminal to report real |
| 89 | //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin. |
| 90 | //config: This is not clean but helps a lot on serial lines and such. |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 91 | //config: |
| 92 | //config:config FEATURE_LESS_DASHCMD |
| 93 | //config: bool "Enable flag changes ('-' command)" |
| 94 | //config: default y |
| 95 | //config: depends on LESS |
| 96 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 97 | //config: This enables the ability to change command-line flags within |
| 98 | //config: less itself ('-' keyboard command). |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 99 | //config: |
| 100 | //config:config FEATURE_LESS_LINENUMS |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 101 | //config: bool "Enable -N (dynamic switching of line numbers)" |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 102 | //config: default y |
| 103 | //config: depends on FEATURE_LESS_DASHCMD |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 104 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 105 | //applet:IF_LESS(APPLET(less, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 106 | |
| 107 | //kbuild:lib-$(CONFIG_LESS) += less.o |
| 108 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 109 | //usage:#define less_trivial_usage |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 110 | //usage: "[-E" IF_FEATURE_LESS_REGEXP("I")IF_FEATURE_LESS_FLAGS("Mm") |
| 111 | //usage: "N" IF_FEATURE_LESS_TRUNCATE("S") "h~] [FILE]..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 112 | //usage:#define less_full_usage "\n\n" |
| 113 | //usage: "View FILE (or stdin) one screenful at a time\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 114 | //usage: "\n -E Quit once the end of a file is reached" |
Denys Vlasenko | 821e643 | 2014-01-22 16:36:22 +0100 | [diff] [blame] | 115 | //usage: IF_FEATURE_LESS_REGEXP( |
| 116 | //usage: "\n -I Ignore case in all searches" |
| 117 | //usage: ) |
Denys Vlasenko | 5377286 | 2012-04-17 16:11:25 +0200 | [diff] [blame] | 118 | //usage: IF_FEATURE_LESS_FLAGS( |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 119 | //usage: "\n -M,-m Display status line with line numbers" |
| 120 | //usage: "\n and percentage through the file" |
Denys Vlasenko | 5377286 | 2012-04-17 16:11:25 +0200 | [diff] [blame] | 121 | //usage: ) |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 122 | //usage: "\n -N Prefix line number to each line" |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 123 | //usage: IF_FEATURE_LESS_TRUNCATE( |
| 124 | //usage: "\n -S Truncate long lines" |
| 125 | //usage: ) |
Denys Vlasenko | 5377286 | 2012-04-17 16:11:25 +0200 | [diff] [blame] | 126 | //usage: "\n -~ Suppress ~s displayed past EOF" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 127 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 128 | #include <sched.h> /* sched_yield() */ |
Denis Vlasenko | 18d6fc1 | 2007-03-08 17:52:36 +0000 | [diff] [blame] | 129 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 130 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 131 | #include "common_bufsiz.h" |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 132 | #if ENABLE_FEATURE_LESS_REGEXP |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 133 | #include "xregex.h" |
| 134 | #endif |
| 135 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 136 | |
| 137 | #define ESC "\033" |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 138 | /* The escape codes for highlighted and normal text */ |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 139 | #define HIGHLIGHT ESC"[7m" |
Denys Vlasenko | 8187e01 | 2017-09-13 22:48:30 +0200 | [diff] [blame] | 140 | #define NORMAL ESC"[m" |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 141 | /* The escape code to home and clear to the end of screen */ |
Denys Vlasenko | 73af705 | 2017-09-13 19:20:27 +0200 | [diff] [blame] | 142 | #define CLEAR ESC"[H"ESC"[J" |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 143 | /* The escape code to clear to the end of line */ |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 144 | #define CLEAR_2_EOL ESC"[K" |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 145 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 146 | enum { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 147 | /* Absolute max of lines eaten */ |
| 148 | MAXLINES = CONFIG_FEATURE_LESS_MAXLINES, |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 149 | /* This many "after the end" lines we will show (at max) */ |
| 150 | TILDES = 1, |
| 151 | }; |
| 152 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 153 | /* Command line options */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 154 | enum { |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 155 | FLAG_E = 1 << 0, |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 156 | FLAG_M = 1 << 1, |
| 157 | FLAG_m = 1 << 2, |
| 158 | FLAG_N = 1 << 3, |
| 159 | FLAG_TILDE = 1 << 4, |
Bernhard Reutner-Fischer | 48a6773 | 2008-09-26 14:10:17 +0000 | [diff] [blame] | 160 | FLAG_I = 1 << 5, |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 161 | FLAG_S = (1 << 6) * ENABLE_FEATURE_LESS_TRUNCATE, |
Bernhard Reutner-Fischer | 882e60c | 2006-12-04 16:04:50 +0000 | [diff] [blame] | 162 | /* hijack command line options variable for internal state vars */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 163 | LESS_STATE_MATCH_BACKWARDS = 1 << 15, |
| 164 | }; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 166 | #if !ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 167 | enum { pattern_valid = 0 }; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 168 | #endif |
| 169 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 170 | struct globals { |
| 171 | int cur_fline; /* signed */ |
| 172 | int kbd_fd; /* fd to get input from */ |
Denys Vlasenko | 7fa799a | 2016-09-13 21:05:48 +0200 | [diff] [blame] | 173 | int kbd_fd_orig_flags; |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 174 | int less_gets_pos; |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 175 | /* last position in last line, taking into account tabs */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 176 | size_t last_line_pos; |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 177 | unsigned max_fline; |
| 178 | unsigned max_lineno; /* this one tracks linewrap */ |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 179 | unsigned max_displayed_line; |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 180 | unsigned width; |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 181 | #if ENABLE_FEATURE_LESS_WINCH |
| 182 | unsigned winch_counter; |
| 183 | #endif |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 184 | ssize_t eof_error; /* eof if 0, error if < 0 */ |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 185 | ssize_t readpos; |
| 186 | ssize_t readeof; /* must be signed */ |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 187 | const char **buffer; |
| 188 | const char **flines; |
| 189 | const char *empty_line_marker; |
| 190 | unsigned num_files; |
| 191 | unsigned current_file; |
| 192 | char *filename; |
| 193 | char **files; |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 194 | #if ENABLE_FEATURE_LESS_FLAGS |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 195 | int num_lines; /* a flag if < 0, line count if >= 0 */ |
| 196 | # define REOPEN_AND_COUNT (-1) |
| 197 | # define REOPEN_STDIN (-2) |
| 198 | # define NOT_REGULAR_FILE (-3) |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 199 | #endif |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 200 | #if ENABLE_FEATURE_LESS_MARKS |
| 201 | unsigned num_marks; |
| 202 | unsigned mark_lines[15][2]; |
| 203 | #endif |
| 204 | #if ENABLE_FEATURE_LESS_REGEXP |
| 205 | unsigned *match_lines; |
| 206 | int match_pos; /* signed! */ |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 207 | int wanted_match; /* signed! */ |
| 208 | int num_matches; |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 209 | regex_t pattern; |
| 210 | smallint pattern_valid; |
| 211 | #endif |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 212 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL |
| 213 | smallint winsize_err; |
| 214 | #endif |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 215 | smallint terminated; |
| 216 | struct termios term_orig, term_less; |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 217 | char kbd_input[KEYCODE_BUFFER_SIZE]; |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 218 | }; |
| 219 | #define G (*ptr_to_globals) |
| 220 | #define cur_fline (G.cur_fline ) |
| 221 | #define kbd_fd (G.kbd_fd ) |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 222 | #define less_gets_pos (G.less_gets_pos ) |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 223 | #define last_line_pos (G.last_line_pos ) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 224 | #define max_fline (G.max_fline ) |
| 225 | #define max_lineno (G.max_lineno ) |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 226 | #define max_displayed_line (G.max_displayed_line) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 227 | #define width (G.width ) |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 228 | #define winch_counter (G.winch_counter ) |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 229 | /* This one is 100% not cached by compiler on read access */ |
| 230 | #define WINCH_COUNTER (*(volatile unsigned *)&winch_counter) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 231 | #define eof_error (G.eof_error ) |
| 232 | #define readpos (G.readpos ) |
| 233 | #define readeof (G.readeof ) |
| 234 | #define buffer (G.buffer ) |
| 235 | #define flines (G.flines ) |
| 236 | #define empty_line_marker (G.empty_line_marker ) |
| 237 | #define num_files (G.num_files ) |
| 238 | #define current_file (G.current_file ) |
| 239 | #define filename (G.filename ) |
| 240 | #define files (G.files ) |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 241 | #define num_lines (G.num_lines ) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 242 | #define num_marks (G.num_marks ) |
| 243 | #define mark_lines (G.mark_lines ) |
Denis Vlasenko | 342b0ab | 2007-05-31 23:06:18 +0000 | [diff] [blame] | 244 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 245 | #define match_lines (G.match_lines ) |
| 246 | #define match_pos (G.match_pos ) |
| 247 | #define num_matches (G.num_matches ) |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 248 | #define wanted_match (G.wanted_match ) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 249 | #define pattern (G.pattern ) |
| 250 | #define pattern_valid (G.pattern_valid ) |
Denis Vlasenko | 342b0ab | 2007-05-31 23:06:18 +0000 | [diff] [blame] | 251 | #endif |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 252 | #define terminated (G.terminated ) |
| 253 | #define term_orig (G.term_orig ) |
| 254 | #define term_less (G.term_less ) |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 255 | #define kbd_input (G.kbd_input ) |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 256 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 257 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 258 | less_gets_pos = -1; \ |
| 259 | empty_line_marker = "~"; \ |
| 260 | num_files = 1; \ |
| 261 | current_file = 1; \ |
| 262 | eof_error = 1; \ |
| 263 | terminated = 1; \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 264 | IF_FEATURE_LESS_REGEXP(wanted_match = -1;) \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 265 | } while (0) |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 266 | |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 267 | /* flines[] are lines read from stdin, each in malloc'ed buffer. |
| 268 | * Line numbers are stored as uint32_t prepended to each line. |
| 269 | * Pointer is adjusted so that flines[i] points directly past |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 270 | * line number. Accessor: */ |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 271 | #define MEMPTR(p) ((char*)(p) - 4) |
| 272 | #define LINENO(p) (*(uint32_t*)((p) - 4)) |
| 273 | |
| 274 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 275 | /* Reset terminal input to normal */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 276 | static void set_tty_cooked(void) |
| 277 | { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 278 | fflush_all(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 279 | tcsetattr(kbd_fd, TCSANOW, &term_orig); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 280 | } |
| 281 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 282 | /* Move the cursor to a position (x,y), where (0,0) is the |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 283 | top-left corner of the console */ |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 284 | static void move_cursor(int line, int row) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 285 | { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 286 | printf(ESC"[%u;%uH", line, row); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 289 | static void clear_line(void) |
| 290 | { |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 291 | printf(ESC"[%u;0H" CLEAR_2_EOL, max_displayed_line + 2); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static void print_hilite(const char *str) |
| 295 | { |
| 296 | printf(HIGHLIGHT"%s"NORMAL, str); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 299 | static void print_statusline(const char *str) |
| 300 | { |
| 301 | clear_line(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 302 | printf(HIGHLIGHT"%.*s"NORMAL, width - 1, str); |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 305 | /* Exit the program gracefully */ |
| 306 | static void less_exit(int code) |
| 307 | { |
| 308 | set_tty_cooked(); |
Denys Vlasenko | 7fa799a | 2016-09-13 21:05:48 +0200 | [diff] [blame] | 309 | if (!(G.kbd_fd_orig_flags & O_NONBLOCK)) |
| 310 | ndelay_off(kbd_fd); |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 311 | clear_line(); |
| 312 | if (code < 0) |
| 313 | kill_myself_with_sig(- code); /* does not return */ |
| 314 | exit(code); |
| 315 | } |
| 316 | |
Denis Vlasenko | f3dcd3c | 2008-10-26 00:24:38 +0000 | [diff] [blame] | 317 | #if (ENABLE_FEATURE_LESS_DASHCMD && ENABLE_FEATURE_LESS_LINENUMS) \ |
| 318 | || ENABLE_FEATURE_LESS_WINCH |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 319 | static void re_wrap(void) |
| 320 | { |
| 321 | int w = width; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 322 | int new_line_pos; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 323 | int src_idx; |
| 324 | int dst_idx; |
| 325 | int new_cur_fline = 0; |
| 326 | uint32_t lineno; |
| 327 | char linebuf[w + 1]; |
| 328 | const char **old_flines = flines; |
| 329 | const char *s; |
| 330 | char **new_flines = NULL; |
| 331 | char *d; |
| 332 | |
| 333 | if (option_mask32 & FLAG_N) |
| 334 | w -= 8; |
| 335 | |
| 336 | src_idx = 0; |
| 337 | dst_idx = 0; |
| 338 | s = old_flines[0]; |
| 339 | lineno = LINENO(s); |
| 340 | d = linebuf; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 341 | new_line_pos = 0; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 342 | while (1) { |
| 343 | *d = *s; |
| 344 | if (*d != '\0') { |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 345 | new_line_pos++; |
Ron Yorston | 78cfa00 | 2015-07-19 21:41:09 +0100 | [diff] [blame] | 346 | if (*d == '\t') { /* tab */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 347 | new_line_pos += 7; |
Ron Yorston | 78cfa00 | 2015-07-19 21:41:09 +0100 | [diff] [blame] | 348 | new_line_pos &= (~7); |
| 349 | } |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 350 | s++; |
| 351 | d++; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 352 | if (new_line_pos >= w) { |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 353 | int sz; |
| 354 | /* new line is full, create next one */ |
| 355 | *d = '\0'; |
| 356 | next_new: |
| 357 | sz = (d - linebuf) + 1; /* + 1: NUL */ |
| 358 | d = ((char*)xmalloc(sz + 4)) + 4; |
| 359 | LINENO(d) = lineno; |
| 360 | memcpy(d, linebuf, sz); |
| 361 | new_flines = xrealloc_vector(new_flines, 8, dst_idx); |
| 362 | new_flines[dst_idx] = d; |
| 363 | dst_idx++; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 364 | if (new_line_pos < w) { |
| 365 | /* if we came here thru "goto next_new" */ |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 366 | if (src_idx > max_fline) |
| 367 | break; |
| 368 | lineno = LINENO(s); |
| 369 | } |
| 370 | d = linebuf; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 371 | new_line_pos = 0; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 372 | } |
| 373 | continue; |
| 374 | } |
| 375 | /* *d == NUL: old line ended, go to next old one */ |
| 376 | free(MEMPTR(old_flines[src_idx])); |
| 377 | /* btw, convert cur_fline... */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 378 | if (cur_fline == src_idx) |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 379 | new_cur_fline = dst_idx; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 380 | src_idx++; |
| 381 | /* no more lines? finish last new line (and exit the loop) */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 382 | if (src_idx > max_fline) |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 383 | goto next_new; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 384 | s = old_flines[src_idx]; |
| 385 | if (lineno != LINENO(s)) { |
| 386 | /* this is not a continuation line! |
| 387 | * create next _new_ line too */ |
| 388 | goto next_new; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | free(old_flines); |
| 393 | flines = (const char **)new_flines; |
| 394 | |
| 395 | max_fline = dst_idx - 1; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 396 | last_line_pos = new_line_pos; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 397 | cur_fline = new_cur_fline; |
| 398 | /* max_lineno is screen-size independent */ |
Denis Vlasenko | 31d58e5 | 2008-10-29 13:16:28 +0000 | [diff] [blame] | 399 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 400 | pattern_valid = 0; |
Denis Vlasenko | 31d58e5 | 2008-10-29 13:16:28 +0000 | [diff] [blame] | 401 | #endif |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 402 | } |
| 403 | #endif |
| 404 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 405 | #if ENABLE_FEATURE_LESS_REGEXP |
| 406 | static void fill_match_lines(unsigned pos); |
| 407 | #else |
| 408 | #define fill_match_lines(pos) ((void)0) |
| 409 | #endif |
| 410 | |
Ron Yorston | ad1b4d5 | 2015-07-24 14:28:50 +0100 | [diff] [blame] | 411 | static int at_end(void) |
| 412 | { |
| 413 | return (option_mask32 & FLAG_S) |
| 414 | ? !(cur_fline <= max_fline && |
| 415 | max_lineno > LINENO(flines[cur_fline]) + max_displayed_line) |
| 416 | : !(max_fline > cur_fline + max_displayed_line); |
| 417 | } |
| 418 | |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 419 | /* Devilishly complex routine. |
| 420 | * |
| 421 | * Has to deal with EOF and EPIPE on input, |
| 422 | * with line wrapping, with last line not ending in '\n' |
| 423 | * (possibly not ending YET!), with backspace and tabs. |
Denis Vlasenko | c2f011a | 2007-05-31 21:31:56 +0000 | [diff] [blame] | 424 | * It reads input again if last time we got an EOF (thus supporting |
| 425 | * growing files) or EPIPE (watching output of slow process like make). |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 426 | * |
| 427 | * Variables used: |
| 428 | * flines[] - array of lines already read. Linewrap may cause |
| 429 | * one source file line to occupy several flines[n]. |
| 430 | * flines[max_fline] - last line, possibly incomplete. |
| 431 | * terminated - 1 if flines[max_fline] is 'terminated' |
| 432 | * (if there was '\n' [which isn't stored itself, we just remember |
| 433 | * that it was seen]) |
| 434 | * max_lineno - last line's number, this one doesn't increment |
| 435 | * on line wrap, only on "real" new lines. |
| 436 | * readbuf[0..readeof-1] - small preliminary buffer. |
| 437 | * readbuf[readpos] - next character to add to current line. |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 438 | * last_line_pos - screen line position of next char to be read |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 439 | * (takes into account tabs and backspaces) |
| 440 | * eof_error - < 0 error, == 0 EOF, > 0 not EOF/error |
Denys Vlasenko | 69b114f | 2014-04-07 23:32:29 +0200 | [diff] [blame] | 441 | * |
| 442 | * "git log -p | less -m" on the kernel git tree is a good test for EAGAINs, |
| 443 | * "/search on very long input" and "reaching max line count" corner cases. |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 444 | */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 445 | static void read_lines(void) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 446 | { |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 447 | char *current_line, *p; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 448 | int w = width; |
| 449 | char last_terminated = terminated; |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 450 | time_t last_time = 0; |
| 451 | int retry_EAGAIN = 2; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 452 | #if ENABLE_FEATURE_LESS_REGEXP |
| 453 | unsigned old_max_fline = max_fline; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 454 | #endif |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 455 | |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 456 | #define readbuf bb_common_bufsiz1 |
| 457 | setup_common_bufsiz(); |
| 458 | |
Denys Vlasenko | 69b114f | 2014-04-07 23:32:29 +0200 | [diff] [blame] | 459 | /* (careful: max_fline can be -1) */ |
| 460 | if (max_fline + 1 > MAXLINES) |
| 461 | return; |
| 462 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 463 | if (option_mask32 & FLAG_N) |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 464 | w -= 8; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 465 | |
Ron Yorston | 26ccd3d | 2015-08-04 17:10:37 +0100 | [diff] [blame] | 466 | p = current_line = ((char*)xmalloc(w + 5)) + 4; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 467 | if (!last_terminated) { |
| 468 | const char *cp = flines[max_fline]; |
Denys Vlasenko | 865814a | 2014-09-22 21:17:24 +0200 | [diff] [blame] | 469 | p = stpcpy(p, cp); |
| 470 | free(MEMPTR(cp)); |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 471 | /* last_line_pos is still valid from previous read_lines() */ |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 472 | } else { |
Denys Vlasenko | 865814a | 2014-09-22 21:17:24 +0200 | [diff] [blame] | 473 | max_fline++; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 474 | last_line_pos = 0; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 475 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 476 | |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 477 | while (1) { /* read lines until we reach cur_fline or wanted_match */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 478 | *p = '\0'; |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 479 | terminated = 0; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 480 | while (1) { /* read chars until we have a line */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 481 | char c; |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 482 | /* if no unprocessed chars left, eat more */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 483 | if (readpos >= readeof) { |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 484 | int flags = ndelay_on(0); |
| 485 | |
| 486 | while (1) { |
| 487 | time_t t; |
| 488 | |
| 489 | errno = 0; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 490 | eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE); |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 491 | if (errno != EAGAIN) |
| 492 | break; |
| 493 | t = time(NULL); |
| 494 | if (t != last_time) { |
| 495 | last_time = t; |
| 496 | if (--retry_EAGAIN < 0) |
| 497 | break; |
| 498 | } |
| 499 | sched_yield(); |
| 500 | } |
| 501 | fcntl(0, F_SETFL, flags); /* ndelay_off(0) */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 502 | readpos = 0; |
| 503 | readeof = eof_error; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 504 | if (eof_error <= 0) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 505 | goto reached_eof; |
Denys Vlasenko | d6e7672 | 2014-09-22 21:14:02 +0200 | [diff] [blame] | 506 | retry_EAGAIN = 1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 507 | } |
| 508 | c = readbuf[readpos]; |
Denis Vlasenko | 50ddabc | 2006-12-31 19:36:01 +0000 | [diff] [blame] | 509 | /* backspace? [needed for manpages] */ |
| 510 | /* <tab><bs> is (a) insane and */ |
| 511 | /* (b) harder to do correctly, so we refuse to do it */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 512 | if (c == '\x8' && last_line_pos && p[-1] != '\t') { |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 513 | readpos++; /* eat it */ |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 514 | last_line_pos--; |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 515 | /* was buggy (p could end up <= current_line)... */ |
Denis Vlasenko | 50ddabc | 2006-12-31 19:36:01 +0000 | [diff] [blame] | 516 | *--p = '\0'; |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
Ron Yorston | 26ccd3d | 2015-08-04 17:10:37 +0100 | [diff] [blame] | 519 | { |
| 520 | size_t new_last_line_pos = last_line_pos + 1; |
| 521 | if (c == '\t') { |
| 522 | new_last_line_pos += 7; |
| 523 | new_last_line_pos &= (~7); |
| 524 | } |
| 525 | if ((int)new_last_line_pos > w) |
| 526 | break; |
| 527 | last_line_pos = new_last_line_pos; |
| 528 | } |
Denis Vlasenko | 95b3071 | 2006-12-31 19:23:31 +0000 | [diff] [blame] | 529 | /* ok, we will eat this char */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 530 | readpos++; |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 531 | if (c == '\n') { |
| 532 | terminated = 1; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 533 | last_line_pos = 0; |
Denis Vlasenko | 22a9a3c | 2007-05-31 15:56:10 +0000 | [diff] [blame] | 534 | break; |
| 535 | } |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 536 | /* NUL is substituted by '\n'! */ |
| 537 | if (c == '\0') c = '\n'; |
| 538 | *p++ = c; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 539 | *p = '\0'; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 540 | } /* end of "read chars until we have a line" loop */ |
Denys Vlasenko | 15943c8 | 2014-09-21 22:10:55 +0200 | [diff] [blame] | 541 | #if 0 |
| 542 | //BUG: also triggers on this: |
| 543 | // { printf "\nfoo\n"; sleep 1; printf "\nbar\n"; } | less |
| 544 | // (resulting in lost empty line between "foo" and "bar" lines) |
| 545 | // the "terminated" logic needs fixing (or explaining) |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 546 | /* Corner case: linewrap with only "" wrapping to next line */ |
| 547 | /* Looks ugly on screen, so we do not store this empty line */ |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 548 | if (!last_terminated && !current_line[0]) { |
| 549 | last_terminated = 1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 550 | max_lineno++; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 551 | continue; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 552 | } |
Denys Vlasenko | 15943c8 | 2014-09-21 22:10:55 +0200 | [diff] [blame] | 553 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 554 | reached_eof: |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 555 | last_terminated = terminated; |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 556 | flines = xrealloc_vector(flines, 8, max_fline); |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 557 | |
| 558 | flines[max_fline] = (char*)xrealloc(MEMPTR(current_line), strlen(current_line) + 1 + 4) + 4; |
| 559 | LINENO(flines[max_fline]) = max_lineno; |
| 560 | if (terminated) |
| 561 | max_lineno++; |
| 562 | |
Denis Vlasenko | fdcfa2a | 2007-05-31 23:55:39 +0000 | [diff] [blame] | 563 | if (max_fline >= MAXLINES) { |
| 564 | eof_error = 0; /* Pretend we saw EOF */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 565 | break; |
Denis Vlasenko | fdcfa2a | 2007-05-31 23:55:39 +0000 | [diff] [blame] | 566 | } |
Ron Yorston | ad1b4d5 | 2015-07-24 14:28:50 +0100 | [diff] [blame] | 567 | if (!at_end()) { |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 568 | #if !ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 569 | break; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 570 | #else |
| 571 | if (wanted_match >= num_matches) { /* goto_match called us */ |
| 572 | fill_match_lines(old_max_fline); |
| 573 | old_max_fline = max_fline; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 574 | } |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 575 | if (wanted_match < num_matches) |
| 576 | break; |
| 577 | #endif |
| 578 | } |
| 579 | if (eof_error <= 0) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | max_fline++; |
Ron Yorston | 26ccd3d | 2015-08-04 17:10:37 +0100 | [diff] [blame] | 583 | current_line = ((char*)xmalloc(w + 5)) + 4; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 584 | p = current_line; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 585 | last_line_pos = 0; |
Denis Vlasenko | 107fe7c | 2008-03-17 08:38:45 +0000 | [diff] [blame] | 586 | } /* end of "read lines until we reach cur_fline" loop */ |
Denys Vlasenko | 69b114f | 2014-04-07 23:32:29 +0200 | [diff] [blame] | 587 | |
| 588 | if (eof_error < 0) { |
| 589 | if (errno == EAGAIN) { |
| 590 | eof_error = 1; |
| 591 | } else { |
| 592 | print_statusline(bb_msg_read_error); |
| 593 | } |
| 594 | } |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 595 | #if ENABLE_FEATURE_LESS_FLAGS |
| 596 | else if (eof_error == 0) |
| 597 | num_lines = max_lineno; |
| 598 | #endif |
Denys Vlasenko | 69b114f | 2014-04-07 23:32:29 +0200 | [diff] [blame] | 599 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 600 | fill_match_lines(old_max_fline); |
Denis Vlasenko | 24f824e | 2008-04-13 08:32:51 +0000 | [diff] [blame] | 601 | #if ENABLE_FEATURE_LESS_REGEXP |
| 602 | /* prevent us from being stuck in search for a match */ |
| 603 | wanted_match = -1; |
| 604 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 605 | #undef readbuf |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 608 | #if ENABLE_FEATURE_LESS_FLAGS |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 609 | static int safe_lineno(int fline) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 610 | { |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 611 | if (fline >= max_fline) |
| 612 | fline = max_fline - 1; |
| 613 | |
| 614 | /* also catches empty file (max_fline == 0) */ |
| 615 | if (fline < 0) |
| 616 | return 0; |
| 617 | |
| 618 | return LINENO(flines[fline]) + 1; |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 621 | /* count number of lines in file */ |
| 622 | static void update_num_lines(void) |
| 623 | { |
| 624 | int count, fd; |
Ron Yorston | b27cf31 | 2015-07-31 17:33:24 +0100 | [diff] [blame] | 625 | struct stat stbuf; |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 626 | ssize_t len, i; |
| 627 | char buf[4096]; |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 628 | |
Ron Yorston | 70b84be | 2015-07-24 14:28:08 +0100 | [diff] [blame] | 629 | /* only do this for regular files */ |
Ron Yorston | b27cf31 | 2015-07-31 17:33:24 +0100 | [diff] [blame] | 630 | if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) { |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 631 | count = 0; |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 632 | fd = open("/proc/self/fd/0", O_RDONLY); |
| 633 | if (fd < 0 && num_lines == REOPEN_AND_COUNT) { |
| 634 | /* "filename" is valid only if REOPEN_AND_COUNT */ |
| 635 | fd = open(filename, O_RDONLY); |
| 636 | } |
Ron Yorston | 70b84be | 2015-07-24 14:28:08 +0100 | [diff] [blame] | 637 | if (fd < 0) { |
| 638 | /* somebody stole my file! */ |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 639 | num_lines = NOT_REGULAR_FILE; |
Ron Yorston | 70b84be | 2015-07-24 14:28:08 +0100 | [diff] [blame] | 640 | return; |
| 641 | } |
Ron Yorston | b27cf31 | 2015-07-31 17:33:24 +0100 | [diff] [blame] | 642 | if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) { |
| 643 | num_lines = NOT_REGULAR_FILE; |
| 644 | goto do_close; |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 645 | } |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 646 | while ((len = safe_read(fd, buf, sizeof(buf))) > 0) { |
| 647 | for (i = 0; i < len; ++i) { |
| 648 | if (buf[i] == '\n' && ++count == MAXLINES) |
| 649 | goto done; |
| 650 | } |
| 651 | } |
| 652 | done: |
| 653 | num_lines = count; |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 654 | do_close: |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 655 | close(fd); |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 659 | /* Print a status line if -M was specified */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 660 | static void m_status_print(void) |
| 661 | { |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 662 | int first, last; |
| 663 | unsigned percent; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 664 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 665 | if (less_gets_pos >= 0) /* don't touch statusline while input is done! */ |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 666 | return; |
| 667 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 668 | clear_line(); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 669 | printf(HIGHLIGHT"%s", filename); |
| 670 | if (num_files > 1) |
| 671 | printf(" (file %i of %i)", current_file, num_files); |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 672 | |
| 673 | first = safe_lineno(cur_fline); |
| 674 | last = (option_mask32 & FLAG_S) |
| 675 | ? MIN(first + max_displayed_line, max_lineno) |
| 676 | : safe_lineno(cur_fline + max_displayed_line); |
| 677 | printf(" lines %i-%i", first, last); |
| 678 | |
Ron Yorston | 159e032 | 2015-07-24 14:27:42 +0100 | [diff] [blame] | 679 | update_num_lines(); |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 680 | if (num_lines >= 0) |
| 681 | printf("/%i", num_lines); |
| 682 | |
Ron Yorston | ad1b4d5 | 2015-07-24 14:28:50 +0100 | [diff] [blame] | 683 | if (at_end()) { |
Ron Yorston | 193ba40 | 2015-07-21 22:28:09 +0200 | [diff] [blame] | 684 | printf(" (END)"); |
| 685 | if (num_files > 1 && current_file != num_files) |
| 686 | printf(" - next: %s", files[current_file]); |
| 687 | } else if (num_lines > 0) { |
| 688 | percent = (100 * last + num_lines/2) / num_lines; |
| 689 | printf(" %i%%", percent <= 100 ? percent : 100); |
| 690 | } |
| 691 | printf(NORMAL); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 692 | } |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 693 | #endif |
| 694 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 695 | /* Print the status line */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 696 | static void status_print(void) |
| 697 | { |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 698 | const char *p; |
| 699 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 700 | if (less_gets_pos >= 0) /* don't touch statusline while input is done! */ |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 701 | return; |
| 702 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 703 | /* Change the status if flags have been set */ |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 704 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 705 | if (option_mask32 & (FLAG_M|FLAG_m)) { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 706 | m_status_print(); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 707 | return; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 708 | } |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 709 | /* No flags set */ |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 710 | #endif |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 711 | |
| 712 | clear_line(); |
Ron Yorston | ad1b4d5 | 2015-07-24 14:28:50 +0100 | [diff] [blame] | 713 | if (cur_fline && !at_end()) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 714 | bb_putchar(':'); |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 715 | return; |
| 716 | } |
Denis Vlasenko | d51d14e | 2006-12-21 13:57:37 +0000 | [diff] [blame] | 717 | p = "(END)"; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 718 | if (!cur_fline) |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 719 | p = filename; |
| 720 | if (num_files > 1) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 721 | printf(HIGHLIGHT"%s (file %i of %i)"NORMAL, |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 722 | p, current_file, num_files); |
| 723 | return; |
| 724 | } |
| 725 | print_hilite(p); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 728 | static const char controls[] ALIGN1 = |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 729 | /* NUL: never encountered; TAB: not converted */ |
| 730 | /**/"\x01\x02\x03\x04\x05\x06\x07\x08" "\x0a\x0b\x0c\x0d\x0e\x0f" |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 731 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" |
| 732 | "\x7f\x9b"; /* DEL and infamous Meta-ESC :( */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 733 | static const char ctrlconv[] ALIGN1 = |
Denys Vlasenko | ecd90fd | 2010-01-30 18:09:18 +0100 | [diff] [blame] | 734 | /* why 40 instead of 4a below? - it is a replacement for '\n'. |
| 735 | * '\n' is a former NUL - we subst it with @, not J */ |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 736 | "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x40\x4b\x4c\x4d\x4e\x4f" |
| 737 | "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"; |
| 738 | |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 739 | static void print_lineno(const char *line) |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 740 | { |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 741 | const char *fmt = " "; |
| 742 | unsigned n = n; /* for compiler */ |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 743 | |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 744 | if (line != empty_line_marker) { |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 745 | /* Width of 7 preserves tab spacing in the text */ |
| 746 | fmt = "%7u "; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 747 | n = LINENO(line) + 1; |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 748 | if (n > 9999999 && MAXLINES > 9999999) { |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 749 | n %= 10000000; |
| 750 | fmt = "%07u "; |
| 751 | } |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 752 | } |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 753 | printf(fmt, n); |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 757 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 758 | static void print_found(const char *line) |
| 759 | { |
| 760 | int match_status; |
| 761 | int eflags; |
| 762 | char *growline; |
| 763 | regmatch_t match_structs; |
| 764 | |
Ron Yorston | 26ccd3d | 2015-08-04 17:10:37 +0100 | [diff] [blame] | 765 | char buf[width+1]; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 766 | const char *str = line; |
| 767 | char *p = buf; |
| 768 | size_t n; |
| 769 | |
| 770 | while (*str) { |
| 771 | n = strcspn(str, controls); |
| 772 | if (n) { |
| 773 | if (!str[n]) break; |
| 774 | memcpy(p, str, n); |
| 775 | p += n; |
| 776 | str += n; |
| 777 | } |
| 778 | n = strspn(str, controls); |
| 779 | memset(p, '.', n); |
| 780 | p += n; |
| 781 | str += n; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 782 | } |
| 783 | strcpy(p, str); |
| 784 | |
| 785 | /* buf[] holds quarantined version of str */ |
| 786 | |
| 787 | /* Each part of the line that matches has the HIGHLIGHT |
Denys Vlasenko | 6830ade | 2013-01-15 13:58:01 +0100 | [diff] [blame] | 788 | * and NORMAL escape sequences placed around it. |
| 789 | * NB: we regex against line, but insert text |
| 790 | * from quarantined copy (buf[]) */ |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 791 | str = buf; |
| 792 | growline = NULL; |
| 793 | eflags = 0; |
| 794 | goto start; |
| 795 | |
| 796 | while (match_status == 0) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 797 | char *new = xasprintf("%s%.*s"HIGHLIGHT"%.*s"NORMAL, |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 798 | growline ? growline : "", |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 799 | (int)match_structs.rm_so, str, |
| 800 | (int)(match_structs.rm_eo - match_structs.rm_so), |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 801 | str + match_structs.rm_so); |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 802 | free(growline); |
| 803 | growline = new; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 804 | str += match_structs.rm_eo; |
| 805 | line += match_structs.rm_eo; |
| 806 | eflags = REG_NOTBOL; |
| 807 | start: |
| 808 | /* Most of the time doesn't find the regex, optimize for that */ |
| 809 | match_status = regexec(&pattern, line, 1, &match_structs, eflags); |
Denis Vlasenko | 3fe4f98 | 2008-06-09 16:02:39 +0000 | [diff] [blame] | 810 | /* if even "" matches, treat it as "not a match" */ |
| 811 | if (match_structs.rm_so >= match_structs.rm_eo) |
| 812 | match_status = 1; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 815 | printf("%s%s\n", growline ? growline : "", str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 816 | free(growline); |
| 817 | } |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 818 | #else |
| 819 | void print_found(const char *line); |
| 820 | #endif |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 821 | |
| 822 | static void print_ascii(const char *str) |
| 823 | { |
Ron Yorston | 26ccd3d | 2015-08-04 17:10:37 +0100 | [diff] [blame] | 824 | char buf[width+1]; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 825 | char *p; |
| 826 | size_t n; |
| 827 | |
| 828 | while (*str) { |
| 829 | n = strcspn(str, controls); |
| 830 | if (n) { |
| 831 | if (!str[n]) break; |
Denis Vlasenko | 806116b | 2006-12-31 12:14:16 +0000 | [diff] [blame] | 832 | printf("%.*s", (int) n, str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 833 | str += n; |
| 834 | } |
| 835 | n = strspn(str, controls); |
| 836 | p = buf; |
| 837 | do { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 838 | if (*str == 0x7f) |
| 839 | *p++ = '?'; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 840 | else if (*str == (char)0x9b) |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 841 | /* VT100's CSI, aka Meta-ESC. Who's inventor? */ |
| 842 | /* I want to know who committed this sin */ |
| 843 | *p++ = '{'; |
| 844 | else |
| 845 | *p++ = ctrlconv[(unsigned char)*str]; |
| 846 | str++; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 847 | } while (--n); |
| 848 | *p = '\0'; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 849 | print_hilite(buf); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 850 | } |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 851 | puts(str); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 854 | /* Print the buffer */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 855 | static void buffer_print(void) |
| 856 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 857 | unsigned i; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 858 | |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 859 | move_cursor(0, 0); |
Aaro Koskinen | 307d26c | 2014-09-23 22:58:18 +0200 | [diff] [blame] | 860 | for (i = 0; i <= max_displayed_line; i++) { |
Ron Yorston | f06386a | 2015-07-18 16:20:03 +0100 | [diff] [blame] | 861 | printf(CLEAR_2_EOL); |
| 862 | if (option_mask32 & FLAG_N) |
| 863 | print_lineno(buffer[i]); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 864 | if (pattern_valid) |
| 865 | print_found(buffer[i]); |
| 866 | else |
| 867 | print_ascii(buffer[i]); |
Aaro Koskinen | 307d26c | 2014-09-23 22:58:18 +0200 | [diff] [blame] | 868 | } |
| 869 | if ((option_mask32 & FLAG_E) |
| 870 | && eof_error <= 0 |
| 871 | && (max_fline - cur_fline) <= max_displayed_line |
| 872 | ) { |
| 873 | less_exit(EXIT_SUCCESS); |
| 874 | } |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 875 | status_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 878 | static void buffer_fill_and_print(void) |
| 879 | { |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 880 | unsigned i; |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 881 | #if ENABLE_FEATURE_LESS_TRUNCATE |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 882 | int fpos = cur_fline; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 883 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 884 | if (option_mask32 & FLAG_S) { |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 885 | /* Go back to the beginning of this line */ |
| 886 | while (fpos && LINENO(flines[fpos]) == LINENO(flines[fpos-1])) |
| 887 | fpos--; |
| 888 | } |
| 889 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 890 | i = 0; |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 891 | while (i <= max_displayed_line && fpos <= max_fline) { |
| 892 | int lineno = LINENO(flines[fpos]); |
| 893 | buffer[i] = flines[fpos]; |
| 894 | i++; |
| 895 | do { |
| 896 | fpos++; |
| 897 | } while ((fpos <= max_fline) |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 898 | && (option_mask32 & FLAG_S) |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 899 | && lineno == LINENO(flines[fpos]) |
| 900 | ); |
| 901 | } |
Denis Vlasenko | d908395 | 2008-10-24 10:42:21 +0000 | [diff] [blame] | 902 | #else |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 903 | for (i = 0; i <= max_displayed_line && cur_fline + i <= max_fline; i++) { |
| 904 | buffer[i] = flines[cur_fline + i]; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 905 | } |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 906 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 907 | for (; i <= max_displayed_line; i++) { |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 908 | buffer[i] = empty_line_marker; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 909 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 910 | buffer_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 913 | /* move cur_fline to a given line number, reading lines if necessary */ |
| 914 | static void goto_lineno(int target) |
| 915 | { |
| 916 | if (target <= 0 ) { |
| 917 | cur_fline = 0; |
| 918 | } |
| 919 | else if (target > LINENO(flines[cur_fline])) { |
| 920 | retry: |
| 921 | while (LINENO(flines[cur_fline]) != target && cur_fline < max_fline) |
| 922 | ++cur_fline; |
| 923 | /* target not reached but more input is available */ |
| 924 | if (LINENO(flines[cur_fline]) != target && eof_error > 0) { |
| 925 | read_lines(); |
| 926 | goto retry; |
| 927 | } |
| 928 | } |
| 929 | else { |
| 930 | /* search backwards through already-read lines */ |
| 931 | while (LINENO(flines[cur_fline]) != target && cur_fline > 0) |
| 932 | --cur_fline; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | static void cap_cur_fline(void) |
| 937 | { |
| 938 | if ((option_mask32 & FLAG_S)) { |
| 939 | if (cur_fline > max_fline) |
| 940 | cur_fline = max_fline; |
| 941 | if (LINENO(flines[cur_fline]) + max_displayed_line > max_lineno + TILDES) { |
| 942 | goto_lineno(max_lineno - max_displayed_line + TILDES); |
| 943 | read_lines(); |
| 944 | } |
| 945 | } |
| 946 | else { |
| 947 | if (cur_fline + max_displayed_line > max_fline + TILDES) |
| 948 | cur_fline = max_fline - max_displayed_line + TILDES; |
| 949 | if (cur_fline < 0) |
| 950 | cur_fline = 0; |
| 951 | } |
| 952 | } |
| 953 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 954 | /* Move the buffer up and down in the file in order to scroll */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 955 | static void buffer_down(int nlines) |
| 956 | { |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 957 | if ((option_mask32 & FLAG_S)) |
| 958 | goto_lineno(LINENO(flines[cur_fline]) + nlines); |
| 959 | else |
| 960 | cur_fline += nlines; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 961 | read_lines(); |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 962 | cap_cur_fline(); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 963 | buffer_fill_and_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 966 | static void buffer_up(int nlines) |
| 967 | { |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 968 | if ((option_mask32 & FLAG_S)) { |
| 969 | goto_lineno(LINENO(flines[cur_fline]) - nlines); |
| 970 | } |
| 971 | else { |
| 972 | cur_fline -= nlines; |
| 973 | if (cur_fline < 0) |
| 974 | cur_fline = 0; |
| 975 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 976 | read_lines(); |
| 977 | buffer_fill_and_print(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 980 | /* display a given line where the argument can be either an index into |
| 981 | * the flines array or a line number */ |
| 982 | static void buffer_to_line(int linenum, int is_lineno) |
| 983 | { |
| 984 | if (linenum <= 0) |
| 985 | cur_fline = 0; |
| 986 | else if (is_lineno) |
| 987 | goto_lineno(linenum); |
| 988 | else |
| 989 | cur_fline = linenum; |
| 990 | read_lines(); |
| 991 | cap_cur_fline(); |
| 992 | buffer_fill_and_print(); |
| 993 | } |
| 994 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 995 | static void buffer_line(int linenum) |
| 996 | { |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 997 | buffer_to_line(linenum, FALSE); |
| 998 | } |
| 999 | |
| 1000 | static void buffer_lineno(int lineno) |
| 1001 | { |
| 1002 | buffer_to_line(lineno, TRUE); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1005 | static void open_file_and_read_lines(void) |
| 1006 | { |
| 1007 | if (filename) { |
Denis Vlasenko | 12abcb3 | 2008-12-10 14:14:09 +0000 | [diff] [blame] | 1008 | xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO); |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 1009 | #if ENABLE_FEATURE_LESS_FLAGS |
| 1010 | num_lines = REOPEN_AND_COUNT; |
| 1011 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1012 | } else { |
| 1013 | /* "less" with no arguments in argv[] */ |
| 1014 | /* For status line only */ |
| 1015 | filename = xstrdup(bb_msg_standard_input); |
Denys Vlasenko | 9dc526d | 2015-07-31 16:42:20 +0200 | [diff] [blame] | 1016 | #if ENABLE_FEATURE_LESS_FLAGS |
| 1017 | num_lines = REOPEN_STDIN; |
| 1018 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1019 | } |
| 1020 | readpos = 0; |
| 1021 | readeof = 0; |
Denis Vlasenko | e1ef89a | 2008-10-26 16:30:09 +0000 | [diff] [blame] | 1022 | last_line_pos = 0; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1023 | terminated = 1; |
| 1024 | read_lines(); |
| 1025 | } |
| 1026 | |
| 1027 | /* Reinitialize everything for a new file - free the memory and start over */ |
| 1028 | static void reinitialize(void) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1029 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1030 | unsigned i; |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1031 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1032 | if (flines) { |
| 1033 | for (i = 0; i <= max_fline; i++) |
Denis Vlasenko | 8f39b72 | 2008-10-23 22:02:30 +0000 | [diff] [blame] | 1034 | free(MEMPTR(flines[i])); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1035 | free(flines); |
| 1036 | flines = NULL; |
| 1037 | } |
"Vladimir N. Oleynik" | a0ae6de | 2005-09-19 10:28:43 +0000 | [diff] [blame] | 1038 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1039 | max_fline = -1; |
| 1040 | cur_fline = 0; |
| 1041 | max_lineno = 0; |
| 1042 | open_file_and_read_lines(); |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1043 | #if ENABLE_FEATURE_LESS_ASK_TERMINAL |
| 1044 | if (G.winsize_err) |
Denys Vlasenko | 8187e01 | 2017-09-13 22:48:30 +0200 | [diff] [blame] | 1045 | printf(ESC"[999;999H" ESC"[6n"); |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1046 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1047 | buffer_fill_and_print(); |
| 1048 | } |
| 1049 | |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1050 | static int64_t getch_nowait(void) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1051 | { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1052 | int rd; |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1053 | int64_t key64; |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1054 | struct pollfd pfd[2]; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1055 | |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1056 | pfd[0].fd = STDIN_FILENO; |
| 1057 | pfd[0].events = POLLIN; |
| 1058 | pfd[1].fd = kbd_fd; |
| 1059 | pfd[1].events = POLLIN; |
| 1060 | again: |
| 1061 | tcsetattr(kbd_fd, TCSANOW, &term_less); |
| 1062 | /* NB: select/poll returns whenever read will not block. Therefore: |
| 1063 | * if eof is reached, select/poll will return immediately |
| 1064 | * because read will immediately return 0 bytes. |
| 1065 | * Even if select/poll says that input is available, read CAN block |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1066 | * (switch fd into O_NONBLOCK'ed mode to avoid it) |
| 1067 | */ |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1068 | rd = 1; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1069 | /* Are we interested in stdin? */ |
Ron Yorston | ad1b4d5 | 2015-07-24 14:28:50 +0100 | [diff] [blame] | 1070 | if (at_end()) { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1071 | if (eof_error > 0) /* did NOT reach eof yet */ |
| 1072 | rd = 0; /* yes, we are interested in stdin */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1073 | } |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1074 | /* Position cursor if line input is done */ |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1075 | if (less_gets_pos >= 0) |
| 1076 | move_cursor(max_displayed_line + 2, less_gets_pos + 1); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1077 | fflush_all(); |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 1078 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1079 | if (kbd_input[0] == 0) { /* if nothing is buffered */ |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1080 | #if ENABLE_FEATURE_LESS_WINCH |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 1081 | while (1) { |
| 1082 | int r; |
| 1083 | /* NB: SIGWINCH interrupts poll() */ |
| 1084 | r = poll(pfd + rd, 2 - rd, -1); |
| 1085 | if (/*r < 0 && errno == EINTR &&*/ winch_counter) |
| 1086 | return '\\'; /* anything which has no defined function */ |
| 1087 | if (r) break; |
| 1088 | } |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1089 | #else |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 1090 | safe_poll(pfd + rd, 2 - rd, -1); |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1091 | #endif |
Denis Vlasenko | c51457c | 2008-11-02 00:55:41 +0000 | [diff] [blame] | 1092 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1093 | |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1094 | /* We have kbd_fd in O_NONBLOCK mode, read inside read_key() |
| 1095 | * would not block even if there is no input available */ |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1096 | key64 = read_key(kbd_fd, kbd_input, /*timeout off:*/ -2); |
| 1097 | if ((int)key64 == -1) { |
Denis Vlasenko | 6497479 | 2008-10-31 03:04:55 +0000 | [diff] [blame] | 1098 | if (errno == EAGAIN) { |
| 1099 | /* No keyboard input available. Since poll() did return, |
| 1100 | * we should have input on stdin */ |
| 1101 | read_lines(); |
| 1102 | buffer_fill_and_print(); |
| 1103 | goto again; |
| 1104 | } |
| 1105 | /* EOF/error (ssh session got killed etc) */ |
Denys Vlasenko | 363fb5e | 2017-09-14 09:42:40 +0200 | [diff] [blame^] | 1106 | less_exit(EXIT_SUCCESS); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1107 | } |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1108 | set_tty_cooked(); |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1109 | return key64; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 1112 | /* Grab a character from input without requiring the return key. |
| 1113 | * May return KEYCODE_xxx values. |
| 1114 | * Note that this function works best with raw input. */ |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1115 | static int64_t less_getch(int pos) |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1116 | { |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1117 | int64_t key64; |
| 1118 | int key; |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1119 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1120 | again: |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1121 | less_gets_pos = pos; |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1122 | key = key64 = getch_nowait(); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1123 | less_gets_pos = -1; |
Denis Vlasenko | 46340e3 | 2007-08-03 14:17:21 +0000 | [diff] [blame] | 1124 | |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1125 | /* Discard Ctrl-something chars. |
| 1126 | * (checking only lower 32 bits is a size optimization: |
| 1127 | * upper 32 bits are used only by KEYCODE_CURSOR_POS) |
| 1128 | */ |
| 1129 | if (key >= 0 && key < ' ' && key != 0x0d && key != 8) |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1130 | goto again; |
Denys Vlasenko | 4f54168 | 2011-08-16 01:53:12 +0200 | [diff] [blame] | 1131 | |
| 1132 | return key64; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static char* less_gets(int sz) |
| 1136 | { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1137 | int c; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1138 | unsigned i = 0; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1139 | char *result = xzalloc(1); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1140 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1141 | while (1) { |
Denis Vlasenko | 46340e3 | 2007-08-03 14:17:21 +0000 | [diff] [blame] | 1142 | c = '\0'; |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1143 | less_gets_pos = sz + i; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1144 | c = getch_nowait(); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1145 | if (c == 0x0d) { |
Denis Vlasenko | d553faf | 2008-02-23 12:22:17 +0000 | [diff] [blame] | 1146 | result[i] = '\0'; |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1147 | less_gets_pos = -1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1148 | return result; |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1149 | } |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1150 | if (c == 0x7f) |
| 1151 | c = 8; |
| 1152 | if (c == 8 && i) { |
| 1153 | printf("\x8 \x8"); |
| 1154 | i--; |
| 1155 | } |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1156 | if (c < ' ') /* filters out KEYCODE_xxx too (<0) */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1157 | continue; |
| 1158 | if (i >= width - sz - 1) |
| 1159 | continue; /* len limit */ |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1160 | bb_putchar(c); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1161 | result[i++] = c; |
| 1162 | result = xrealloc(result, i+1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1163 | } |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1166 | static void examine_file(void) |
| 1167 | { |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1168 | char *new_fname; |
| 1169 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1170 | print_statusline("Examine: "); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1171 | new_fname = less_gets(sizeof("Examine: ") - 1); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1172 | if (!new_fname[0]) { |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1173 | status_print(); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1174 | err: |
| 1175 | free(new_fname); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1176 | return; |
| 1177 | } |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1178 | if (access(new_fname, R_OK) != 0) { |
| 1179 | print_statusline("Cannot read this file"); |
| 1180 | goto err; |
| 1181 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1182 | free(filename); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1183 | filename = new_fname; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1184 | /* files start by = argv. why we assume that argv is infinitely long?? |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1185 | files[num_files] = filename; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1186 | current_file = num_files + 1; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1187 | num_files++; */ |
| 1188 | files[0] = filename; |
Denis Vlasenko | e147a72 | 2006-12-21 13:26:54 +0000 | [diff] [blame] | 1189 | num_files = current_file = 1; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1190 | reinitialize(); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1193 | /* This function changes the file currently being paged. direction can be one of the following: |
| 1194 | * -1: go back one file |
| 1195 | * 0: go to the first file |
Denis Vlasenko | 7cea262 | 2006-12-24 07:30:09 +0000 | [diff] [blame] | 1196 | * 1: go forward one file */ |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1197 | static void change_file(int direction) |
| 1198 | { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1199 | if (current_file != ((direction > 0) ? num_files : 1)) { |
| 1200 | current_file = direction ? current_file + direction : 1; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1201 | free(filename); |
| 1202 | filename = xstrdup(files[current_file - 1]); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1203 | reinitialize(); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1204 | } else { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1205 | print_statusline(direction > 0 ? "No next file" : "No previous file"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1209 | static void remove_current_file(void) |
| 1210 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1211 | unsigned i; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1212 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1213 | if (num_files < 2) |
| 1214 | return; |
| 1215 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1216 | if (current_file != 1) { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1217 | change_file(-1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1218 | for (i = 3; i <= num_files; i++) |
| 1219 | files[i - 2] = files[i - 1]; |
| 1220 | num_files--; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1221 | } else { |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1222 | change_file(1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1223 | for (i = 2; i <= num_files; i++) |
| 1224 | files[i - 2] = files[i - 1]; |
| 1225 | num_files--; |
| 1226 | current_file--; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1227 | } |
| 1228 | } |
| 1229 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1230 | static void colon_process(void) |
| 1231 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1232 | int keypress; |
| 1233 | |
| 1234 | /* Clear the current line and print a prompt */ |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1235 | print_statusline(" :"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1236 | |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1237 | keypress = less_getch(2); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1238 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1239 | case 'd': |
| 1240 | remove_current_file(); |
| 1241 | break; |
| 1242 | case 'e': |
| 1243 | examine_file(); |
| 1244 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1245 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1246 | case 'f': |
| 1247 | m_status_print(); |
| 1248 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1249 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1250 | case 'n': |
| 1251 | change_file(1); |
| 1252 | break; |
| 1253 | case 'p': |
| 1254 | change_file(-1); |
| 1255 | break; |
| 1256 | case 'q': |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1257 | less_exit(EXIT_SUCCESS); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1258 | break; |
| 1259 | case 'x': |
| 1260 | change_file(0); |
| 1261 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
Denis Vlasenko | 3bba545 | 2006-12-30 17:57:03 +0000 | [diff] [blame] | 1265 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1266 | static void normalize_match_pos(int match) |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1267 | { |
Denis Vlasenko | f65d133 | 2006-12-21 15:23:45 +0000 | [diff] [blame] | 1268 | if (match >= num_matches) |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1269 | match = num_matches - 1; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1270 | if (match < 0) |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1271 | match = 0; |
| 1272 | match_pos = match; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1275 | static void goto_match(int match) |
| 1276 | { |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1277 | if (!pattern_valid) |
| 1278 | return; |
| 1279 | if (match < 0) |
| 1280 | match = 0; |
| 1281 | /* Try to find next match if eof isn't reached yet */ |
| 1282 | if (match >= num_matches && eof_error > 0) { |
Denis Vlasenko | 24f824e | 2008-04-13 08:32:51 +0000 | [diff] [blame] | 1283 | wanted_match = match; /* "I want to read until I see N'th match" */ |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1284 | read_lines(); |
| 1285 | } |
| 1286 | if (num_matches) { |
| 1287 | normalize_match_pos(match); |
| 1288 | buffer_line(match_lines[match_pos]); |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 1289 | } else { |
Denis Vlasenko | 8465a99 | 2007-05-09 18:32:54 +0000 | [diff] [blame] | 1290 | print_statusline("No matches found"); |
Denis Vlasenko | b30418a | 2007-02-14 20:49:14 +0000 | [diff] [blame] | 1291 | } |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1294 | static void fill_match_lines(unsigned pos) |
| 1295 | { |
| 1296 | if (!pattern_valid) |
| 1297 | return; |
| 1298 | /* Run the regex on each line of the current file */ |
| 1299 | while (pos <= max_fline) { |
| 1300 | /* If this line matches */ |
| 1301 | if (regexec(&pattern, flines[pos], 0, NULL, 0) == 0 |
| 1302 | /* and we didn't match it last time */ |
| 1303 | && !(num_matches && match_lines[num_matches-1] == pos) |
| 1304 | ) { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 1305 | match_lines = xrealloc_vector(match_lines, 4, num_matches); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1306 | match_lines[num_matches++] = pos; |
| 1307 | } |
| 1308 | pos++; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1312 | static void regex_process(void) |
| 1313 | { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1314 | char *uncomp_regex, *err; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* Reset variables */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1317 | free(match_lines); |
| 1318 | match_lines = NULL; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1319 | match_pos = 0; |
| 1320 | num_matches = 0; |
| 1321 | if (pattern_valid) { |
| 1322 | regfree(&pattern); |
| 1323 | pattern_valid = 0; |
| 1324 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1325 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1326 | /* Get the uncompiled regular expression from the user */ |
| 1327 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1328 | bb_putchar((option_mask32 & LESS_STATE_MATCH_BACKWARDS) ? '?' : '/'); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1329 | uncomp_regex = less_gets(1); |
| 1330 | if (!uncomp_regex[0]) { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1331 | free(uncomp_regex); |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1332 | buffer_print(); |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1333 | return; |
| 1334 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 1335 | |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1336 | /* Compile the regex and check for errors */ |
Bernhard Reutner-Fischer | 48a6773 | 2008-09-26 14:10:17 +0000 | [diff] [blame] | 1337 | err = regcomp_or_errmsg(&pattern, uncomp_regex, |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1338 | (option_mask32 & FLAG_I) ? REG_ICASE : 0); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1339 | free(uncomp_regex); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1340 | if (err) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1341 | print_statusline(err); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1342 | free(err); |
| 1343 | return; |
| 1344 | } |
Denis Vlasenko | a1c6312 | 2007-03-08 18:12:01 +0000 | [diff] [blame] | 1345 | |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1346 | pattern_valid = 1; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1347 | match_pos = 0; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1348 | fill_match_lines(0); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1349 | while (match_pos < num_matches) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1350 | if ((int)match_lines[match_pos] > cur_fline) |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1351 | break; |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1352 | match_pos++; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1353 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1354 | if (option_mask32 & LESS_STATE_MATCH_BACKWARDS) |
| 1355 | match_pos--; |
Denis Vlasenko | a1c6312 | 2007-03-08 18:12:01 +0000 | [diff] [blame] | 1356 | |
| 1357 | /* It's possible that no matches are found yet. |
| 1358 | * goto_match() will read input looking for match, |
| 1359 | * if needed */ |
| 1360 | goto_match(match_pos); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1361 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1362 | #endif |
| 1363 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1364 | static void number_process(int first_digit) |
| 1365 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1366 | unsigned i; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1367 | int num; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1368 | int keypress; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1369 | char num_input[sizeof(int)*4]; /* more than enough */ |
"Vladimir N. Oleynik" | bc37480 | 2005-09-19 14:23:46 +0000 | [diff] [blame] | 1370 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1371 | num_input[0] = first_digit; |
| 1372 | |
| 1373 | /* Clear the current line, print a prompt, and then print the digit */ |
| 1374 | clear_line(); |
| 1375 | printf(":%c", first_digit); |
| 1376 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1377 | /* Receive input until a letter is given */ |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1378 | i = 1; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1379 | while (i < sizeof(num_input)-1) { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1380 | keypress = less_getch(i + 1); |
Ron Yorston | ae1a9e8 | 2015-07-21 20:12:31 +0100 | [diff] [blame] | 1381 | if ((unsigned)keypress > 255 || !isdigit(keypress)) |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1382 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1383 | num_input[i] = keypress; |
| 1384 | bb_putchar(keypress); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1385 | i++; |
| 1386 | } |
| 1387 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1388 | num_input[i] = '\0'; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1389 | num = bb_strtou(num_input, NULL, 10); |
| 1390 | /* on format error, num == -1 */ |
| 1391 | if (num < 1 || num > MAXLINES) { |
Rob Landley | a2e9804 | 2006-04-18 01:53:41 +0000 | [diff] [blame] | 1392 | buffer_print(); |
| 1393 | return; |
| 1394 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1395 | |
| 1396 | /* We now know the number and the letter entered, so we process them */ |
| 1397 | switch (keypress) { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1398 | case KEYCODE_DOWN: case 'z': case 'd': case 'e': case ' ': case '\015': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1399 | buffer_down(num); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1400 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1401 | case KEYCODE_UP: case 'b': case 'w': case 'y': case 'u': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1402 | buffer_up(num); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | case 'g': case '<': case 'G': case '>': |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 1405 | buffer_lineno(num - 1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | case 'p': case '%': |
Ron Yorston | d542d18 | 2015-07-24 14:29:13 +0100 | [diff] [blame] | 1408 | #if ENABLE_FEATURE_LESS_FLAGS |
| 1409 | update_num_lines(); |
| 1410 | num = num * (num_lines > 0 ? num_lines : max_lineno) / 100; |
| 1411 | #else |
| 1412 | num = num * max_lineno / 100; |
| 1413 | #endif |
| 1414 | buffer_lineno(num); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1415 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1416 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1417 | case 'n': |
| 1418 | goto_match(match_pos + num); |
| 1419 | break; |
| 1420 | case '/': |
| 1421 | option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; |
| 1422 | regex_process(); |
| 1423 | break; |
| 1424 | case '?': |
| 1425 | option_mask32 |= LESS_STATE_MATCH_BACKWARDS; |
| 1426 | regex_process(); |
| 1427 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1428 | #endif |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1432 | #if ENABLE_FEATURE_LESS_DASHCMD |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1433 | static void flag_change(void) |
| 1434 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1435 | int keypress; |
| 1436 | |
| 1437 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1438 | bb_putchar('-'); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1439 | keypress = less_getch(1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1440 | |
| 1441 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1442 | case 'M': |
| 1443 | option_mask32 ^= FLAG_M; |
| 1444 | break; |
| 1445 | case 'm': |
| 1446 | option_mask32 ^= FLAG_m; |
| 1447 | break; |
| 1448 | case 'E': |
| 1449 | option_mask32 ^= FLAG_E; |
| 1450 | break; |
| 1451 | case '~': |
| 1452 | option_mask32 ^= FLAG_TILDE; |
| 1453 | break; |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 1454 | #if ENABLE_FEATURE_LESS_TRUNCATE |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1455 | case 'S': |
| 1456 | option_mask32 ^= FLAG_S; |
| 1457 | buffer_fill_and_print(); |
| 1458 | break; |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 1459 | #endif |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1460 | #if ENABLE_FEATURE_LESS_LINENUMS |
| 1461 | case 'N': |
| 1462 | option_mask32 ^= FLAG_N; |
| 1463 | re_wrap(); |
| 1464 | buffer_fill_and_print(); |
| 1465 | break; |
| 1466 | #endif |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
Denis Vlasenko | 7b32e8f | 2008-10-26 00:15:36 +0000 | [diff] [blame] | 1470 | #ifdef BLOAT |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1471 | static void show_flag_status(void) |
| 1472 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1473 | int keypress; |
| 1474 | int flag_val; |
| 1475 | |
| 1476 | clear_line(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1477 | bb_putchar('_'); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1478 | keypress = less_getch(1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1479 | |
| 1480 | switch (keypress) { |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1481 | case 'M': |
| 1482 | flag_val = option_mask32 & FLAG_M; |
| 1483 | break; |
| 1484 | case 'm': |
| 1485 | flag_val = option_mask32 & FLAG_m; |
| 1486 | break; |
| 1487 | case '~': |
| 1488 | flag_val = option_mask32 & FLAG_TILDE; |
| 1489 | break; |
| 1490 | case 'N': |
| 1491 | flag_val = option_mask32 & FLAG_N; |
| 1492 | break; |
| 1493 | case 'E': |
| 1494 | flag_val = option_mask32 & FLAG_E; |
| 1495 | break; |
| 1496 | default: |
| 1497 | flag_val = 0; |
| 1498 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | clear_line(); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1502 | printf(HIGHLIGHT"The status of the flag is: %u"NORMAL, flag_val != 0); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1503 | } |
| 1504 | #endif |
| 1505 | |
Denis Vlasenko | 7b32e8f | 2008-10-26 00:15:36 +0000 | [diff] [blame] | 1506 | #endif /* ENABLE_FEATURE_LESS_DASHCMD */ |
| 1507 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1508 | static void save_input_to_file(void) |
| 1509 | { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1510 | const char *msg = ""; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1511 | char *current_line; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1512 | unsigned i; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1513 | FILE *fp; |
| 1514 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1515 | print_statusline("Log file: "); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1516 | current_line = less_gets(sizeof("Log file: ")-1); |
Denis Vlasenko | 3319637 | 2008-02-23 01:25:38 +0000 | [diff] [blame] | 1517 | if (current_line[0]) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 1518 | fp = fopen_for_write(current_line); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1519 | if (!fp) { |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1520 | msg = "Error opening log file"; |
| 1521 | goto ret; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1522 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1523 | for (i = 0; i <= max_fline; i++) |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1524 | fprintf(fp, "%s\n", flines[i]); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1525 | fclose(fp); |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1526 | msg = "Done"; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1527 | } |
Denis Vlasenko | 5a4f099 | 2006-12-25 01:23:02 +0000 | [diff] [blame] | 1528 | ret: |
| 1529 | print_statusline(msg); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1530 | free(current_line); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1533 | #if ENABLE_FEATURE_LESS_MARKS |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1534 | static void add_mark(void) |
| 1535 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1536 | int letter; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1537 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1538 | print_statusline("Mark: "); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1539 | letter = less_getch(sizeof("Mark: ") - 1); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1540 | |
| 1541 | if (isalpha(letter)) { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1542 | /* If we exceed 15 marks, start overwriting previous ones */ |
| 1543 | if (num_marks == 14) |
| 1544 | num_marks = 0; |
| 1545 | |
| 1546 | mark_lines[num_marks][0] = letter; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1547 | mark_lines[num_marks][1] = cur_fline; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1548 | num_marks++; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1549 | } else { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1550 | print_statusline("Invalid mark letter"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1554 | static void goto_mark(void) |
| 1555 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1556 | int letter; |
| 1557 | int i; |
| 1558 | |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1559 | print_statusline("Go to mark: "); |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1560 | letter = less_getch(sizeof("Go to mark: ") - 1); |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1561 | clear_line(); |
"Vladimir N. Oleynik" | a0ae6de | 2005-09-19 10:28:43 +0000 | [diff] [blame] | 1562 | |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1563 | if (isalpha(letter)) { |
| 1564 | for (i = 0; i <= num_marks; i++) |
| 1565 | if (letter == mark_lines[i][0]) { |
| 1566 | buffer_line(mark_lines[i][1]); |
| 1567 | break; |
| 1568 | } |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1569 | if (num_marks == 14 && letter != mark_lines[14][0]) |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1570 | print_statusline("Mark not set"); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1571 | } else |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1572 | print_statusline("Invalid mark letter"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1573 | } |
| 1574 | #endif |
| 1575 | |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1576 | #if ENABLE_FEATURE_LESS_BRACKETS |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1577 | static char opp_bracket(char bracket) |
| 1578 | { |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1579 | switch (bracket) { |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1580 | case '{': case '[': /* '}' == '{' + 2. Same for '[' */ |
| 1581 | bracket++; |
| 1582 | case '(': /* ')' == '(' + 1 */ |
| 1583 | bracket++; |
| 1584 | break; |
| 1585 | case '}': case ']': |
| 1586 | bracket--; |
| 1587 | case ')': |
| 1588 | bracket--; |
| 1589 | break; |
| 1590 | }; |
| 1591 | return bracket; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1594 | static void match_right_bracket(char bracket) |
| 1595 | { |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1596 | unsigned i = cur_fline; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1597 | |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1598 | if (i >= max_fline |
| 1599 | || strchr(flines[i], bracket) == NULL |
Denys Vlasenko | 2525596 | 2016-08-16 15:09:40 +0200 | [diff] [blame] | 1600 | ) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1601 | print_statusline("No bracket in top line"); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1602 | return; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1603 | } |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1604 | |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1605 | bracket = opp_bracket(bracket); |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1606 | for (; i < max_fline; i++) { |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1607 | if (strchr(flines[i], bracket) != NULL) { |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1608 | /* |
| 1609 | * Line with matched right bracket becomes |
| 1610 | * last visible line |
| 1611 | */ |
| 1612 | buffer_line(i - max_displayed_line); |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1613 | return; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1614 | } |
| 1615 | } |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1616 | print_statusline("No matching bracket found"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1619 | static void match_left_bracket(char bracket) |
| 1620 | { |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1621 | int i = cur_fline + max_displayed_line; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1622 | |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1623 | if (i >= max_fline |
| 1624 | || strchr(flines[i], bracket) == NULL |
Denys Vlasenko | 2525596 | 2016-08-16 15:09:40 +0200 | [diff] [blame] | 1625 | ) { |
Denis Vlasenko | f1282a8 | 2006-12-21 17:03:20 +0000 | [diff] [blame] | 1626 | print_statusline("No bracket in bottom line"); |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1627 | return; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1628 | } |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1629 | |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1630 | bracket = opp_bracket(bracket); |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1631 | for (; i >= 0; i--) { |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1632 | if (strchr(flines[i], bracket) != NULL) { |
Denys Vlasenko | 110c6bb | 2016-08-16 15:27:30 +0200 | [diff] [blame] | 1633 | /* |
| 1634 | * Line with matched left bracket becomes |
| 1635 | * first visible line |
| 1636 | */ |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1637 | buffer_line(i); |
| 1638 | return; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
Denis Vlasenko | b78d1c0 | 2008-02-24 19:18:18 +0000 | [diff] [blame] | 1641 | print_statusline("No matching bracket found"); |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1642 | } |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1643 | #endif /* FEATURE_LESS_BRACKETS */ |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1644 | |
Mike Frysinger | 3a2b103 | 2006-04-16 20:34:26 +0000 | [diff] [blame] | 1645 | static void keypress_process(int keypress) |
| 1646 | { |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1647 | switch (keypress) { |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1648 | case KEYCODE_DOWN: case 'e': case 'j': case 0x0d: |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1649 | buffer_down(1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1650 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1651 | case KEYCODE_UP: case 'y': case 'k': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1652 | buffer_up(1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1653 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1654 | case KEYCODE_PAGEDOWN: case ' ': case 'z': case 'f': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1655 | buffer_down(max_displayed_line + 1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1656 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1657 | case KEYCODE_PAGEUP: case 'w': case 'b': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1658 | buffer_up(max_displayed_line + 1); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | case 'd': |
| 1661 | buffer_down((max_displayed_line + 1) / 2); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | case 'u': |
| 1664 | buffer_up((max_displayed_line + 1) / 2); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1665 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1666 | case KEYCODE_HOME: case 'g': case 'p': case '<': case '%': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1667 | buffer_line(0); |
| 1668 | break; |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1669 | case KEYCODE_END: case 'G': case '>': |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1670 | cur_fline = MAXLINES; |
| 1671 | read_lines(); |
| 1672 | buffer_line(cur_fline); |
| 1673 | break; |
| 1674 | case 'q': case 'Q': |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 1675 | less_exit(EXIT_SUCCESS); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1676 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1677 | #if ENABLE_FEATURE_LESS_MARKS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1678 | case 'm': |
| 1679 | add_mark(); |
| 1680 | buffer_print(); |
| 1681 | break; |
| 1682 | case '\'': |
| 1683 | goto_mark(); |
| 1684 | buffer_print(); |
| 1685 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1686 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1687 | case 'r': case 'R': |
Denys Vlasenko | 2ef4214 | 2011-07-25 15:23:52 +0200 | [diff] [blame] | 1688 | /* TODO: (1) also bind ^R, ^L to this? |
| 1689 | * (2) re-measure window size? |
| 1690 | */ |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1691 | buffer_print(); |
| 1692 | break; |
| 1693 | /*case 'R': |
| 1694 | full_repaint(); |
| 1695 | break;*/ |
| 1696 | case 's': |
| 1697 | save_input_to_file(); |
| 1698 | break; |
| 1699 | case 'E': |
| 1700 | examine_file(); |
| 1701 | break; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1702 | #if ENABLE_FEATURE_LESS_FLAGS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1703 | case '=': |
| 1704 | m_status_print(); |
| 1705 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1706 | #endif |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1707 | #if ENABLE_FEATURE_LESS_REGEXP |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1708 | case '/': |
| 1709 | option_mask32 &= ~LESS_STATE_MATCH_BACKWARDS; |
| 1710 | regex_process(); |
| 1711 | break; |
| 1712 | case 'n': |
| 1713 | goto_match(match_pos + 1); |
| 1714 | break; |
| 1715 | case 'N': |
| 1716 | goto_match(match_pos - 1); |
| 1717 | break; |
| 1718 | case '?': |
| 1719 | option_mask32 |= LESS_STATE_MATCH_BACKWARDS; |
| 1720 | regex_process(); |
| 1721 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1722 | #endif |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1723 | #if ENABLE_FEATURE_LESS_DASHCMD |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1724 | case '-': |
| 1725 | flag_change(); |
| 1726 | buffer_print(); |
| 1727 | break; |
Denis Vlasenko | 7b32e8f | 2008-10-26 00:15:36 +0000 | [diff] [blame] | 1728 | #ifdef BLOAT |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1729 | case '_': |
| 1730 | show_flag_status(); |
| 1731 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1732 | #endif |
Denis Vlasenko | 7b32e8f | 2008-10-26 00:15:36 +0000 | [diff] [blame] | 1733 | #endif |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1734 | #if ENABLE_FEATURE_LESS_BRACKETS |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1735 | case '{': case '(': case '[': |
| 1736 | match_right_bracket(keypress); |
| 1737 | break; |
| 1738 | case '}': case ')': case ']': |
| 1739 | match_left_bracket(keypress); |
| 1740 | break; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1741 | #endif |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1742 | case ':': |
| 1743 | colon_process(); |
| 1744 | break; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1745 | } |
Rob Landley | d57ae8b | 2005-09-18 00:58:49 +0000 | [diff] [blame] | 1746 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1747 | if (isdigit(keypress)) |
| 1748 | number_process(keypress); |
| 1749 | } |
| 1750 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1751 | static void sig_catcher(int sig) |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1752 | { |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1753 | less_exit(- sig); |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1754 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1755 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1756 | #if ENABLE_FEATURE_LESS_WINCH |
| 1757 | static void sigwinch_handler(int sig UNUSED_PARAM) |
| 1758 | { |
| 1759 | winch_counter++; |
| 1760 | } |
| 1761 | #endif |
| 1762 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1763 | int less_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1764 | int less_main(int argc, char **argv) |
| 1765 | { |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1766 | char *tty_name; |
| 1767 | int tty_fd; |
| 1768 | |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 1769 | INIT_G(); |
| 1770 | |
Denys Vlasenko | 821e643 | 2014-01-22 16:36:22 +0100 | [diff] [blame] | 1771 | /* TODO: -x: do not interpret backspace, -xx: tab also |
| 1772 | * -xxx: newline also |
| 1773 | * -w N: assume width N (-xxx -w 32: hex viewer of sorts) |
| 1774 | * -s: condense many empty lines to one |
| 1775 | * (used by some setups for manpage display) |
| 1776 | */ |
Ron Yorston | 51aa861 | 2015-07-19 11:12:29 +0100 | [diff] [blame] | 1777 | getopt32(argv, "EMmN~I" IF_FEATURE_LESS_TRUNCATE("S") /*ignored:*/"s"); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1778 | argc -= optind; |
| 1779 | argv += optind; |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1780 | num_files = argc; |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1781 | files = argv; |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1782 | |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1783 | /* Another popular pager, most, detects when stdout |
| 1784 | * is not a tty and turns into cat. This makes sense. */ |
| 1785 | if (!isatty(STDOUT_FILENO)) |
| 1786 | return bb_cat(argv); |
| 1787 | |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1788 | if (!num_files) { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1789 | if (isatty(STDIN_FILENO)) { |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1790 | /* Just "less"? No args and no redirection? */ |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 1791 | bb_error_msg("missing filename"); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1792 | bb_show_usage(); |
| 1793 | } |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1794 | } else { |
Denis Vlasenko | 9a7cef9 | 2006-12-20 02:46:48 +0000 | [diff] [blame] | 1795 | filename = xstrdup(files[0]); |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1796 | } |
"Vladimir N. Oleynik" | 2b306e9 | 2005-09-16 12:32:22 +0000 | [diff] [blame] | 1797 | |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1798 | if (option_mask32 & FLAG_TILDE) |
| 1799 | empty_line_marker = ""; |
Denis Vlasenko | 3f3190e | 2006-12-21 00:22:03 +0000 | [diff] [blame] | 1800 | |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1801 | /* Some versions of less can survive w/o controlling tty, |
| 1802 | * try to do the same. This also allows to specify an alternative |
| 1803 | * tty via "less 1<>TTY". |
Denys Vlasenko | 3c0e579 | 2016-09-13 20:53:38 +0200 | [diff] [blame] | 1804 | * |
| 1805 | * We prefer not to use STDOUT_FILENO directly, |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1806 | * since we want to set this fd to non-blocking mode, |
Denys Vlasenko | 3c0e579 | 2016-09-13 20:53:38 +0200 | [diff] [blame] | 1807 | * and not interfere with other processes which share stdout with us. |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1808 | */ |
| 1809 | tty_name = xmalloc_ttyname(STDOUT_FILENO); |
| 1810 | if (tty_name) { |
| 1811 | tty_fd = open(tty_name, O_RDONLY); |
| 1812 | free(tty_name); |
| 1813 | if (tty_fd < 0) |
| 1814 | goto try_ctty; |
| 1815 | } else { |
| 1816 | /* Try controlling tty */ |
| 1817 | try_ctty: |
| 1818 | tty_fd = open(CURRENT_TTY, O_RDONLY); |
Denys Vlasenko | 3c0e579 | 2016-09-13 20:53:38 +0200 | [diff] [blame] | 1819 | if (tty_fd < 0) { |
Denys Vlasenko | 7fa799a | 2016-09-13 21:05:48 +0200 | [diff] [blame] | 1820 | /* If all else fails, less 481 uses stdout. Mimic that */ |
Denys Vlasenko | 3c0e579 | 2016-09-13 20:53:38 +0200 | [diff] [blame] | 1821 | tty_fd = STDOUT_FILENO; |
| 1822 | } |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1823 | } |
Denys Vlasenko | 7fa799a | 2016-09-13 21:05:48 +0200 | [diff] [blame] | 1824 | G.kbd_fd_orig_flags = ndelay_on(tty_fd); |
Denys Vlasenko | cc1c9ca | 2013-08-04 17:41:19 +0200 | [diff] [blame] | 1825 | kbd_fd = tty_fd; /* save in a global */ |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1826 | |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1827 | tcgetattr(kbd_fd, &term_orig); |
Denis Vlasenko | a1d24a0 | 2007-05-31 15:55:03 +0000 | [diff] [blame] | 1828 | term_less = term_orig; |
| 1829 | term_less.c_lflag &= ~(ICANON | ECHO); |
| 1830 | term_less.c_iflag &= ~(IXON | ICRNL); |
| 1831 | /*term_less.c_oflag &= ~ONLCR;*/ |
| 1832 | term_less.c_cc[VMIN] = 1; |
| 1833 | term_less.c_cc[VTIME] = 0; |
Denis Vlasenko | e865e81 | 2006-12-21 13:24:58 +0000 | [diff] [blame] | 1834 | |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1835 | IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line); |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1836 | /* 20: two tabstops + 4 */ |
| 1837 | if (width < 20 || max_displayed_line < 3) |
| 1838 | return bb_cat(argv); |
| 1839 | max_displayed_line -= 2; |
| 1840 | |
Denis Vlasenko | d553faf | 2008-02-23 12:22:17 +0000 | [diff] [blame] | 1841 | /* We want to restore term_orig on exit */ |
Denis Vlasenko | cf7cf62 | 2008-03-19 19:38:46 +0000 | [diff] [blame] | 1842 | bb_signals(BB_FATAL_SIGS, sig_catcher); |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1843 | #if ENABLE_FEATURE_LESS_WINCH |
| 1844 | signal(SIGWINCH, sigwinch_handler); |
| 1845 | #endif |
Denis Vlasenko | d553faf | 2008-02-23 12:22:17 +0000 | [diff] [blame] | 1846 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1847 | buffer = xmalloc((max_displayed_line+1) * sizeof(char *)); |
Denis Vlasenko | f4dff77 | 2006-12-24 07:14:17 +0000 | [diff] [blame] | 1848 | reinitialize(); |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1849 | while (1) { |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1850 | int64_t keypress; |
| 1851 | |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1852 | #if ENABLE_FEATURE_LESS_WINCH |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1853 | while (WINCH_COUNTER) { |
| 1854 | again: |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1855 | winch_counter--; |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1856 | IF_FEATURE_LESS_ASK_TERMINAL(G.winsize_err =) get_terminal_width_height(kbd_fd, &width, &max_displayed_line); |
| 1857 | IF_FEATURE_LESS_ASK_TERMINAL(got_size:) |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1858 | /* 20: two tabstops + 4 */ |
| 1859 | if (width < 20) |
| 1860 | width = 20; |
| 1861 | if (max_displayed_line < 3) |
| 1862 | max_displayed_line = 3; |
| 1863 | max_displayed_line -= 2; |
| 1864 | free(buffer); |
| 1865 | buffer = xmalloc((max_displayed_line+1) * sizeof(char *)); |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1866 | /* Avoid re-wrap and/or redraw if we already know |
| 1867 | * we need to do it again. These ops are expensive */ |
| 1868 | if (WINCH_COUNTER) |
| 1869 | goto again; |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1870 | re_wrap(); |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1871 | if (WINCH_COUNTER) |
| 1872 | goto again; |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1873 | buffer_fill_and_print(); |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 1874 | /* This took some time. Loop back and check, |
| 1875 | * were there another SIGWINCH? */ |
Denis Vlasenko | 53c80f0 | 2008-10-24 22:43:27 +0000 | [diff] [blame] | 1876 | } |
Denis Vlasenko | d2172c0 | 2008-02-23 11:54:37 +0000 | [diff] [blame] | 1877 | keypress = less_getch(-1); /* -1: do not position cursor */ |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 1878 | # if ENABLE_FEATURE_LESS_ASK_TERMINAL |
| 1879 | if ((int32_t)keypress == KEYCODE_CURSOR_POS) { |
| 1880 | uint32_t rc = (keypress >> 32); |
| 1881 | width = (rc & 0x7fff); |
| 1882 | max_displayed_line = ((rc >> 16) & 0x7fff); |
| 1883 | goto got_size; |
| 1884 | } |
| 1885 | # endif |
| 1886 | #else |
| 1887 | keypress = less_getch(-1); /* -1: do not position cursor */ |
| 1888 | #endif |
Rob Landley | 9200e79 | 2005-09-15 19:26:59 +0000 | [diff] [blame] | 1889 | keypress_process(keypress); |
| 1890 | } |
| 1891 | } |
Denis Vlasenko | e8efe81 | 2008-10-24 11:07:20 +0000 | [diff] [blame] | 1892 | |
| 1893 | /* |
| 1894 | Help text of less version 418 is below. |
| 1895 | If you are implementing something, keeping |
| 1896 | key and/or command line switch compatibility is a good idea: |
| 1897 | |
| 1898 | |
| 1899 | SUMMARY OF LESS COMMANDS |
| 1900 | |
| 1901 | Commands marked with * may be preceded by a number, N. |
| 1902 | Notes in parentheses indicate the behavior if N is given. |
| 1903 | h H Display this help. |
| 1904 | q :q Q :Q ZZ Exit. |
| 1905 | --------------------------------------------------------------------------- |
| 1906 | MOVING |
| 1907 | e ^E j ^N CR * Forward one line (or N lines). |
| 1908 | y ^Y k ^K ^P * Backward one line (or N lines). |
| 1909 | f ^F ^V SPACE * Forward one window (or N lines). |
| 1910 | b ^B ESC-v * Backward one window (or N lines). |
| 1911 | z * Forward one window (and set window to N). |
| 1912 | w * Backward one window (and set window to N). |
| 1913 | ESC-SPACE * Forward one window, but don't stop at end-of-file. |
| 1914 | d ^D * Forward one half-window (and set half-window to N). |
| 1915 | u ^U * Backward one half-window (and set half-window to N). |
| 1916 | ESC-) RightArrow * Left one half screen width (or N positions). |
| 1917 | ESC-( LeftArrow * Right one half screen width (or N positions). |
| 1918 | F Forward forever; like "tail -f". |
| 1919 | r ^R ^L Repaint screen. |
| 1920 | R Repaint screen, discarding buffered input. |
| 1921 | --------------------------------------------------- |
| 1922 | Default "window" is the screen height. |
| 1923 | Default "half-window" is half of the screen height. |
| 1924 | --------------------------------------------------------------------------- |
| 1925 | SEARCHING |
| 1926 | /pattern * Search forward for (N-th) matching line. |
| 1927 | ?pattern * Search backward for (N-th) matching line. |
| 1928 | n * Repeat previous search (for N-th occurrence). |
| 1929 | N * Repeat previous search in reverse direction. |
| 1930 | ESC-n * Repeat previous search, spanning files. |
| 1931 | ESC-N * Repeat previous search, reverse dir. & spanning files. |
| 1932 | ESC-u Undo (toggle) search highlighting. |
| 1933 | --------------------------------------------------- |
| 1934 | Search patterns may be modified by one or more of: |
| 1935 | ^N or ! Search for NON-matching lines. |
| 1936 | ^E or * Search multiple files (pass thru END OF FILE). |
| 1937 | ^F or @ Start search at FIRST file (for /) or last file (for ?). |
| 1938 | ^K Highlight matches, but don't move (KEEP position). |
| 1939 | ^R Don't use REGULAR EXPRESSIONS. |
| 1940 | --------------------------------------------------------------------------- |
| 1941 | JUMPING |
| 1942 | g < ESC-< * Go to first line in file (or line N). |
| 1943 | G > ESC-> * Go to last line in file (or line N). |
| 1944 | p % * Go to beginning of file (or N percent into file). |
| 1945 | t * Go to the (N-th) next tag. |
| 1946 | T * Go to the (N-th) previous tag. |
| 1947 | { ( [ * Find close bracket } ) ]. |
| 1948 | } ) ] * Find open bracket { ( [. |
| 1949 | ESC-^F <c1> <c2> * Find close bracket <c2>. |
| 1950 | ESC-^B <c1> <c2> * Find open bracket <c1> |
| 1951 | --------------------------------------------------- |
| 1952 | Each "find close bracket" command goes forward to the close bracket |
| 1953 | matching the (N-th) open bracket in the top line. |
| 1954 | Each "find open bracket" command goes backward to the open bracket |
| 1955 | matching the (N-th) close bracket in the bottom line. |
| 1956 | m<letter> Mark the current position with <letter>. |
| 1957 | '<letter> Go to a previously marked position. |
| 1958 | '' Go to the previous position. |
| 1959 | ^X^X Same as '. |
| 1960 | --------------------------------------------------- |
| 1961 | A mark is any upper-case or lower-case letter. |
| 1962 | Certain marks are predefined: |
| 1963 | ^ means beginning of the file |
| 1964 | $ means end of the file |
| 1965 | --------------------------------------------------------------------------- |
| 1966 | CHANGING FILES |
| 1967 | :e [file] Examine a new file. |
| 1968 | ^X^V Same as :e. |
| 1969 | :n * Examine the (N-th) next file from the command line. |
| 1970 | :p * Examine the (N-th) previous file from the command line. |
| 1971 | :x * Examine the first (or N-th) file from the command line. |
| 1972 | :d Delete the current file from the command line list. |
| 1973 | = ^G :f Print current file name. |
| 1974 | --------------------------------------------------------------------------- |
| 1975 | MISCELLANEOUS COMMANDS |
| 1976 | -<flag> Toggle a command line option [see OPTIONS below]. |
| 1977 | --<name> Toggle a command line option, by name. |
| 1978 | _<flag> Display the setting of a command line option. |
| 1979 | __<name> Display the setting of an option, by name. |
| 1980 | +cmd Execute the less cmd each time a new file is examined. |
| 1981 | !command Execute the shell command with $SHELL. |
| 1982 | |Xcommand Pipe file between current pos & mark X to shell command. |
| 1983 | v Edit the current file with $VISUAL or $EDITOR. |
| 1984 | V Print version number of "less". |
| 1985 | --------------------------------------------------------------------------- |
| 1986 | OPTIONS |
| 1987 | Most options may be changed either on the command line, |
| 1988 | or from within less by using the - or -- command. |
| 1989 | Options may be given in one of two forms: either a single |
Maninder Singh | 97c6491 | 2015-05-25 13:46:36 +0200 | [diff] [blame] | 1990 | character preceded by a -, or a name preceded by --. |
Denis Vlasenko | e8efe81 | 2008-10-24 11:07:20 +0000 | [diff] [blame] | 1991 | -? ........ --help |
| 1992 | Display help (from command line). |
| 1993 | -a ........ --search-skip-screen |
| 1994 | Forward search skips current screen. |
| 1995 | -b [N] .... --buffers=[N] |
| 1996 | Number of buffers. |
| 1997 | -B ........ --auto-buffers |
| 1998 | Don't automatically allocate buffers for pipes. |
| 1999 | -c ........ --clear-screen |
| 2000 | Repaint by clearing rather than scrolling. |
| 2001 | -d ........ --dumb |
| 2002 | Dumb terminal. |
| 2003 | -D [xn.n] . --color=xn.n |
| 2004 | Set screen colors. (MS-DOS only) |
| 2005 | -e -E .... --quit-at-eof --QUIT-AT-EOF |
| 2006 | Quit at end of file. |
| 2007 | -f ........ --force |
| 2008 | Force open non-regular files. |
| 2009 | -F ........ --quit-if-one-screen |
| 2010 | Quit if entire file fits on first screen. |
| 2011 | -g ........ --hilite-search |
| 2012 | Highlight only last match for searches. |
| 2013 | -G ........ --HILITE-SEARCH |
| 2014 | Don't highlight any matches for searches. |
| 2015 | -h [N] .... --max-back-scroll=[N] |
| 2016 | Backward scroll limit. |
| 2017 | -i ........ --ignore-case |
| 2018 | Ignore case in searches that do not contain uppercase. |
| 2019 | -I ........ --IGNORE-CASE |
| 2020 | Ignore case in all searches. |
| 2021 | -j [N] .... --jump-target=[N] |
| 2022 | Screen position of target lines. |
| 2023 | -J ........ --status-column |
| 2024 | Display a status column at left edge of screen. |
| 2025 | -k [file] . --lesskey-file=[file] |
| 2026 | Use a lesskey file. |
| 2027 | -L ........ --no-lessopen |
| 2028 | Ignore the LESSOPEN environment variable. |
| 2029 | -m -M .... --long-prompt --LONG-PROMPT |
| 2030 | Set prompt style. |
| 2031 | -n -N .... --line-numbers --LINE-NUMBERS |
| 2032 | Don't use line numbers. |
| 2033 | -o [file] . --log-file=[file] |
| 2034 | Copy to log file (standard input only). |
| 2035 | -O [file] . --LOG-FILE=[file] |
| 2036 | Copy to log file (unconditionally overwrite). |
| 2037 | -p [pattern] --pattern=[pattern] |
| 2038 | Start at pattern (from command line). |
| 2039 | -P [prompt] --prompt=[prompt] |
| 2040 | Define new prompt. |
| 2041 | -q -Q .... --quiet --QUIET --silent --SILENT |
| 2042 | Quiet the terminal bell. |
| 2043 | -r -R .... --raw-control-chars --RAW-CONTROL-CHARS |
| 2044 | Output "raw" control characters. |
| 2045 | -s ........ --squeeze-blank-lines |
| 2046 | Squeeze multiple blank lines. |
| 2047 | -S ........ --chop-long-lines |
| 2048 | Chop long lines. |
| 2049 | -t [tag] .. --tag=[tag] |
| 2050 | Find a tag. |
| 2051 | -T [tagsfile] --tag-file=[tagsfile] |
| 2052 | Use an alternate tags file. |
| 2053 | -u -U .... --underline-special --UNDERLINE-SPECIAL |
| 2054 | Change handling of backspaces. |
| 2055 | -V ........ --version |
| 2056 | Display the version number of "less". |
| 2057 | -w ........ --hilite-unread |
| 2058 | Highlight first new line after forward-screen. |
| 2059 | -W ........ --HILITE-UNREAD |
| 2060 | Highlight first new line after any forward movement. |
| 2061 | -x [N[,...]] --tabs=[N[,...]] |
| 2062 | Set tab stops. |
| 2063 | -X ........ --no-init |
| 2064 | Don't use termcap init/deinit strings. |
| 2065 | --no-keypad |
| 2066 | Don't use termcap keypad init/deinit strings. |
| 2067 | -y [N] .... --max-forw-scroll=[N] |
| 2068 | Forward scroll limit. |
| 2069 | -z [N] .... --window=[N] |
| 2070 | Set size of window. |
| 2071 | -" [c[c]] . --quotes=[c[c]] |
| 2072 | Set shell quote characters. |
| 2073 | -~ ........ --tilde |
| 2074 | Don't display tildes after end of file. |
| 2075 | -# [N] .... --shift=[N] |
| 2076 | Horizontal scroll amount (0 = one half screen width) |
| 2077 | |
| 2078 | --------------------------------------------------------------------------- |
| 2079 | LINE EDITING |
| 2080 | These keys can be used to edit text being entered |
| 2081 | on the "command line" at the bottom of the screen. |
| 2082 | RightArrow ESC-l Move cursor right one character. |
| 2083 | LeftArrow ESC-h Move cursor left one character. |
| 2084 | CNTL-RightArrow ESC-RightArrow ESC-w Move cursor right one word. |
| 2085 | CNTL-LeftArrow ESC-LeftArrow ESC-b Move cursor left one word. |
| 2086 | HOME ESC-0 Move cursor to start of line. |
| 2087 | END ESC-$ Move cursor to end of line. |
| 2088 | BACKSPACE Delete char to left of cursor. |
| 2089 | DELETE ESC-x Delete char under cursor. |
| 2090 | CNTL-BACKSPACE ESC-BACKSPACE Delete word to left of cursor. |
| 2091 | CNTL-DELETE ESC-DELETE ESC-X Delete word under cursor. |
| 2092 | CNTL-U ESC (MS-DOS only) Delete entire line. |
| 2093 | UpArrow ESC-k Retrieve previous command line. |
| 2094 | DownArrow ESC-j Retrieve next command line. |
| 2095 | TAB Complete filename & cycle. |
| 2096 | SHIFT-TAB ESC-TAB Complete filename & reverse cycle. |
| 2097 | CNTL-L Complete filename, list all. |
| 2098 | */ |