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