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