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