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