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