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