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