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